diff options
| -rw-r--r-- | Makefile | 17 | ||||
| -rw-r--r-- | src/calp.c | 114 | ||||
| -rw-r--r-- | src/date.c | 2 | ||||
| -rw-r--r-- | src/draw.c | 210 | ||||
| -rw-r--r-- | src/draw.h | 3 | ||||
| -rw-r--r-- | src/image.c | 19 |
6 files changed, 196 insertions, 169 deletions
@@ -1,27 +1,28 @@ VERSION = 0.0 PREFIX = /usr/local -CC = cc +CC = c99 DIR = src -OBJS = $(DIR)/calp.o $(DIR)/draw.o $(DIR)/date.o $(DIR)/image.o +OBJS = $(DIR)/calp.o $(DIR)/draw.o $(DIR)/date.o .PHONY: clean all install TARGET = calp -CFLAGS = `pkgconf --cflags pangocairo` -O2 -g -Wall -Werror -Wextra -Winit-self -Wuninitialized -pedantic -LFLAGS = `pkgconf --libs pangocairo` -lm - +# `Magick-config --cflags` +CFLAGS = `pkg-config --cflags pangocairo` -O2 -g -Wall -Werror -Wextra -Winit-self -Wuninitialized -pedantic +LFLAGS = `pkg-config --libs pangocairo` -lm +# `Magick-config --ldflags --libs` all: $(TARGET) $(TARGET): $(OBJS) $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LFLAGS) clean: - -rm -f $(OBJS) calp + -rm -f $(OBJS) $(TARGET) install: all mkdir -p ${DESTDIR}${PREFIX}/bin cp -f calp ${DESTDIR}${PREFIX}/bin - chmod 755 ${DESTDIR}${PREFIX}/bin/calp + chmod 755 ${DESTDIR}${PREFIX}/bin/$(TARGET) uninstall: - rm -f ${DESTDIR}${PREFIX}/bin/calp + rm -f ${DESTDIR}${PREFIX}/bin/$(TARGET) @@ -1,4 +1,4 @@ -//calp: printable calendar generator +/* calp: printable calendar generator */ #include <stdio.h> #include <stdlib.h> @@ -13,18 +13,18 @@ #include "color.h" #include "image.h" -// Font Settings +/* Font Settings */ #define FONT_FAMILY "Sans" #define FONT_SIZE_NORMAL 10 #define FONT_SIZE_LARGE 40 int main (void) { - // Definitions - // ----------- + /* Variable Definitions */ double paper_width = A4_WIDTH; double paper_height = A4_HEIGHT; - // Convert to inches, then 1 unit = 1/72th inch + /* Convert the paper dimensions from milimeters to inches. Then, inches are + * converted to cairo units (1 unit = 1/72th inch). */ paper_width = (paper_width / 25.4) * 72.0; paper_height = (paper_height / 25.4) * 72.0; @@ -34,28 +34,17 @@ int main (void) { int num_of_months = 12; extern int days_in_month[12]; - // Locale Setting - // -------------- + /* Setting the locale for the calendar. */ char *week_names[7]; char *months[num_of_months]; - nl_item locale_months[12] = + nl_item locale_months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, + MON_7, MON_8, MON_9, MON_10, MON_11, MON_12}; nl_item locale_days[7] = {ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7}; - void generate_locale_months(nl_item *months[12]) { - locale_months = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, - MON_7, MON_8, MON_9, MON_10, MON_11, MON_12}; - - setlocale(LC_ALL, ""); - - for (int i = 0; i < 12; i++) { - months[i] = nl_langinfo(locale_months[i]); - } - } - setlocale(LC_ALL, ""); for (int i = 0; i < 12; i++) { @@ -69,7 +58,7 @@ int main (void) { - // Create dimensions struct to hold measurements + /* Create dimensions struct to hold measurements */ struct dimensions dim; const int row_count = 6; @@ -85,9 +74,9 @@ int main (void) { - // Leap year adjustment: increment February day count by one if - // it is a leap year. - const int year = 2026; + /* Leap year adjustment: increment February day count by one if it is a + * leap year. */ + const int year = 2000; if (isleap(year)) { days_in_month[1]++; @@ -95,23 +84,21 @@ int main (void) { - // Defaults to the year being generated + /* Default the name of the pdf generated to be the year being generated. */ char filename[20]; snprintf(filename, sizeof(filename), "%d.pdf", year); - // Initialization - // -------------- - // Calculates measurements, stores them in dimensions struct. See "draw.h" + /* Initialization */ + + /* Calculates measurements, stores them in dimensions struct. See "draw.h" */ calculate_dimensions(paper_width, paper_height, &dim); - // Extent Declaration - // ------------------ + /* Extent Declaration */ - // Height and width of the paper - // .x and .y are empty + /* Height and width of the paper. .x and .y are empty */ struct extents paper_extents = { .width = paper_width, .height = paper_height @@ -123,8 +110,8 @@ int main (void) { .margin = margin }; - // Width and Height of the boxes are set - // No position data is set until later. + /* Width and Height of the day boxes are set. No position data is set until + * later. */ struct extents day_extents = { .width = paper_extents.width / 10.0, .height = paper_extents.width / 10.0 @@ -134,7 +121,7 @@ int main (void) { // .width = day_extents.width //}; - // Position and dimensions of the month "grid" + /* Position and dimensions of the grid of days for the month. */ struct extents month_extents = { .x = (paper_extents.width - (day_extents.width * column_count)) / 2.0, .y = paper_extents.height - margin - (day_extents.height * row_count), @@ -145,9 +132,8 @@ int main (void) { - // Position and width of the header - // Height is set dynamically, based on the size of - // the month grid + /* Position and width of the header. Height is set dynamically, based on the + * size of the month grid. */ struct extents header_extents = { .x = margin, .y = margin, @@ -155,14 +141,14 @@ int main (void) { }; - // Program fails if the PDF backend isn't available + /* Terminate program if the PDF backend isn't available */ if (CAIRO_HAS_PDF_SURFACE == false) { printf("Error: PDF backend not available.\n"); return 1; } - // Main surface and context are created, with the size being - // the paper + /* Main surface and context are created, with the size being the full + * dimensions of the paper */ cairo_surface_t *surface = cairo_pdf_surface_create( filename, @@ -173,7 +159,7 @@ int main (void) { cairo_t *paper = cairo_create(surface); - // Sub-surface for rendering content in day boxes + /* Sub-surface for rendering content in day boxes */ cairo_surface_t *day = cairo_surface_create_similar( surface, @@ -181,37 +167,43 @@ int main (void) { day_extents.width, day_extents.height ); - // cd = context day + + /* cd = context day */ cairo_t *cd = cairo_create(day); - // Fill background with white color + /* Fill background with white color */ set_color(paper, WHITE); fill_bg(paper); - // Construction - // ------------ + /* Main construction area */ - // Month generation loop + /* Month generation loop: Iterate through range of months and render header + * and days. */ for (int i = 0; i < num_of_months; i++) { - // Initialize month struct, setting values + + /* Initialize month struct, setting values depending on the various + * properties of the month like what day of the week the first day lands + * on, how many days are in the month, and the minimum number of rows + * that can be rendered. */ month.first_day = day_of_week(1, i + 1, year); month.num_of_days = days_in_month[i]; month.month_position = i; month.minimum_rows = calculate_minimum_rows(&month); - // Arbitrarily setting the distance between the month grid and the header + /* Arbitrarily setting the distance between the month grid and the header */ double month_header_spacing = margin * 3.0; - header_extents.height = paper_extents.height - month_header_spacing - ((month.minimum_rows + 1) * day_extents.height); + header_extents.height = + paper_extents.height - month_header_spacing - + ((month.minimum_rows + 1) * day_extents.height); - // Create another surface and context for working with the - // header + /* Create another surface and context for working with the header */ cairo_surface_t *header = cairo_surface_create_similar( surface, CAIRO_CONTENT_COLOR_ALPHA, @@ -221,26 +213,31 @@ int main (void) { cairo_t *ch = cairo_create(header); - - - // Fill header with blue color for visibility + /* Fill header with blue color for visibility */ set_color(ch, BLUE); fill_bg(ch); + /* Draw the title of the month in the header */ set_color(ch, BLACK); draw_month_title(ch, &header_extents, &page_layout, FONT_FAMILY, FONT_SIZE_LARGE, months[i]); - - // Draw the header + /* Draw the header */ cairo_set_source_surface(paper, header, header_extents.x, header_extents.y); cairo_paint(paper); + /* Draw the days of the month */ draw_month_dates(paper, cd, day, &month, &month_extents, &day_extents); + + /* Draw the grid lines for the month */ draw_month_grid(paper, &month, &month_extents, &day_extents); - // Go to the next page in pdf + /* Draw the name of the days of the week */ + draw_week_names(paper, &month, &month_extents, &day_extents); + + /* Go to the next page in pdf */ cairo_show_page(paper); + /* Destroy header context, and header surface */ cairo_destroy(ch); cairo_surface_destroy(header); } @@ -248,8 +245,7 @@ int main (void) { - // Clean up - // -------- + /* Clean up */ cairo_destroy(paper); cairo_surface_destroy(surface); @@ -1,4 +1,4 @@ -// Februrary is altered in calp.c if year is a leap year. defaults to 28. +/* Februrary is altered in calp.c if year is a leap year. defaults to 28. */ int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; @@ -6,30 +6,36 @@ #include "color.h" -// struct for storing dimensions used for calculations, movement, etc. +/* struct for storing dimensions used for calculations, movement, etc. */ struct dimensions { - // Multiplication factors + /* Multiplication factors */ int row_count; /* rows */ int column_count; /* columns */ - // All of these are in units chosen by width in "calp.c" - // Most likely milimeters (mm) - double paper_width; /* paper width */ - double paper_height; /* paper height */ - double month_width; /* month width */ - double month_height; /* month height */ - double month_top_corner_x; - double month_top_corner_y; - double day_width; /* day box width */ - double day_height; /* day box height */ - double margin; /* margin */ - double header_width; - double header_height; + + /* All of these are in units chosen by width in "calp.c". They should be + * measured in milimeters (mm) */ + double paper_width; /* paper width */ + double paper_height; /* paper height */ + + double month_width; /* month width */ + double month_height; /* month height */ + + double month_x; /* Top left month x pos. */ + double month_y; /* Top left month y pos. */ + + double day_width; /* day box width */ + double day_height; /* day box height */ + + double header_width; /* header width */ + double header_height; /* header height */ + + double margin; /* paper margin */ }; -// struct to hold Month-specific info, like what day of the week the first -// day lands on, how many days are in the month, etc. +/* struct to hold Month-specific info, like what day of the week the first day + * lands on, how many days are in the month, etc. */ struct month_info { int month_position; @@ -41,7 +47,7 @@ month_info { }; -// struct for defining RGB colors +/* struct for defining RGB colors */ struct RGB { double r; @@ -50,31 +56,36 @@ RGB { }; -// Generic struct for storing where an object is and how -// it takes up space. -struct extents { +/* Generic struct for storing where an object is and how it takes up space. */ +/* TODO: Depricate dimensions struct in favor of atomic extents structs for + * each "object" (paper, month, header, etc.) */ +struct extents +{ double x; double y; double width; double height; }; -struct alignment { +/* Alignment measurments about spacing. Measured in milimeters (mm). */ +struct alignment +{ double margin; double padding; }; -// Convert Hex color strings to their RGB counterparts. -// IMO, hex strings are easier to work with, so this is -// a nice helper function. -struct -RGB hex_to_rgb(char *str) { +/* Convert Hex color strings to their RGB counterparts. Hex strings are easier + * to work with, so this is a nice helper function. returns a RGB struct value. */ + struct +RGB hex_to_rgb(char *str) +{ unsigned int r, g, b; struct RGB color; sscanf(str, "%02x%02x%02x", &r, &g, &b); - - // R, G, B values are from 0.0 - 1.0, so needs to be divided by 255.0 + + /* Red, Green, and Blue values are from 0.0 - 1.0, so needs to be divided by + * 255.0. */ color.r = (r / 255.0); color.g = (g / 255.0); color.b = (b / 255.0); @@ -84,29 +95,30 @@ RGB hex_to_rgb(char *str) { -// Set a context's color using a HEX code. Color macros are defined -// in color.h -int -set_color(cairo_t *c, char *hex) { +/* Set a context's color using a HEX code. Color macros are defined in color.h. */ + int +set_color(cairo_t *c, char *hex) +{ struct RGB color = hex_to_rgb(hex); cairo_set_source_rgb (c, color.r, color.g, color.b); return 0; } -// Get the day of the week for a given day -// Used for calculating when the start of a month is -// Relative to "Su Mo Tu ... Sa" frame, not when Su and Sa are together -// Range is 0 -> 6 -// https://wiki.c2.com/?PerpetualCalendarAlgorithm -int -day_of_week (int d, int m, int y) { +/* Get the day of the week for a given day. Used for calculating when the start + * of a month is relative to "Su Mo Tu ... Sa" frame, not when Su and Sa are + * together. Range is 0 -> 6. + * + * https://wiki.c2.com/?PerpetualCalendarAlgorithm */ + int +day_of_week (int d, int m, int y) +{ return((d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7); } -// Fill entire background with current color -// Function name is changed for clarity +/* Fill entire background with current color. Function name is changed for + * clarity. */ int fill_bg(cairo_t *c) { cairo_paint(c); @@ -116,23 +128,25 @@ fill_bg(cairo_t *c) { -// Needn't be a string pointer, can be fixed array of chars -// Requires Font Family (string) and Size (int) -// Creates and destroys layout within the scope of the function -// Is that performant when looped over 365 operations? +/* Draw a string of text on the screen with specific font size, position, and + * font family. Needn't be a string pointer, can be fixed array of chars. + * Creates and destroys layout within the scope of the function + * TODO: Is this performant when looped over 365 operations? */ int -draw_text (cairo_t *c, double x, double y, char *font_family, int font_size, char *text) { +draw_text (cairo_t *c, double x, double y, + char *font_family, int font_size, char *text) { + char font[100]; - // Concat font string for pango_font_description_from_string + /* Concat font string for pango_font_description_from_string */ snprintf(font, sizeof(font), "%s %d", font_family, font_size); - //Initialize Pango Stuff + /* Initialize Pango Stuff */ PangoLayout *layout; PangoFontDescription *desc; - // Create a PangoLayout, setting font & text + /* Create a PangoLayout, setting font & text */ layout = pango_cairo_create_layout(c); desc = pango_font_description_from_string(font); pango_layout_set_font_description(layout, desc); @@ -148,20 +162,19 @@ draw_text (cairo_t *c, double x, double y, char *font_family, int font_size, cha } -// Get the dimensions of a string of text -// Returns a PangoRectangle struct +/* Get the dimensions of a string of text. Returns a PangoRectangle struct. */ PangoRectangle get_logical_extents (cairo_t *c, char *font_family, int font_size, char *text) { char font[100]; - // Concat font string for pango_font_description_from_string + /* Concat font string for pango_font_description_from_string */ snprintf(font, sizeof(font), "%s %d", font_family, font_size); PangoLayout *layout; PangoFontDescription *desc; PangoRectangle rect; - // Create a PangoLayout, setting font & text + /* Create a PangoLayout, setting font & text */ layout = pango_cairo_create_layout(c); desc = pango_font_description_from_string(font); pango_layout_set_font_description(layout, desc); @@ -175,19 +188,20 @@ get_logical_extents (cairo_t *c, char *font_family, int font_size, char *text) { return(rect); } - +/* Get the VISUAL dimensions of a string of text. Returns a PangoRectangle + * struct. */ PangoRectangle get_ink_extents (cairo_t *c, char *font_family, int font_size, char *text) { char font[100]; - // Concat font string for pango_font_description_from_string + /* Concat font string for pango_font_description_from_string */ snprintf(font, sizeof(font), "%s %d", font_family, font_size); PangoLayout *layout; PangoFontDescription *desc; PangoRectangle rect; - // Create a PangoLayout, setting font & text + /* Create a PangoLayout, setting font & text */ layout = pango_cairo_create_layout(c); desc = pango_font_description_from_string(font); pango_layout_set_font_description(layout, desc); @@ -201,36 +215,36 @@ get_ink_extents (cairo_t *c, char *font_family, int font_size, char *text) { return(rect); } -// Calculate dimenions for one page and stores it in the dimensions struct -// Used for determining where elements should be drawn on the page +/* Calculate dimenions for one page and store it in the dimensions struct. Used + * for determining where elements should be drawn on the page. */ int calculate_dimensions(double pw, double ph, struct dimensions *d) { d->row_count = 6; d->column_count = 7; - // Percentage of page that should be margin + /* Percentage of page that should be margin */ double margin = 0.05 * pw; - - // width of day is 1/7th of total width + + /* width of day is 1/7th of total width */ double box_width = pw / 10.0; - // Everything other than margin + /* Everything other than margin */ double month_width = box_width * d->column_count; - // Width = Height; Day Box is a square + /* Width = Height; Day Box is a square */ double box_height = box_width; - // Height is based on how tall # of weeks are + /* Height is based on how tall # of weeks are */ double month_height = box_height * d->row_count; - // Set items in struct to calculated values + /* Set items in struct to calculated values */ d->paper_width = pw; d->paper_height = ph; d->margin = margin; d->month_width = month_width; - d->month_top_corner_x = (pw - (month_width)) / 2.0; - d->month_top_corner_y = ph - margin - month_height; + d->month_x = (pw - (month_width)) / 2.0; + d->month_y = ph - margin - month_height; d->month_height = month_height; d->day_width = box_width; d->day_height = box_height; @@ -245,7 +259,7 @@ calculate_dimensions(double pw, double ph, struct dimensions *d) { int calculate_minimum_rows(struct month_info *m) { int extra_rows = 0; - + int days_in_first_week = (7 - m->first_day); if (m->first_day == 0) { @@ -271,7 +285,7 @@ calculate_minimum_rows(struct month_info *m) { -// Draws the grid lines for a month +/* Draws the grid lines for a month */ int draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct extents *de) { double cursor_x = me->x; @@ -288,18 +302,18 @@ draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct ex set_color(c, BLACK); cairo_set_line_width(c, 1.0); - // Vertical lines + /* Vertical lines */ for (int i = 0; i <= m->column_count; i++) { cairo_move_to(c, cursor_x, cursor_y); cairo_line_to(c, cursor_x, cursor_y + (me->height - (row_offset * de->height))); cursor_x = cursor_x + de->width; } - // Recenter the cursor + /* Recenter the cursor to the top left */ cursor_x = x; cursor_y = y; - // Horizontal lines + /* Horizontal lines */ for (int i = 0; i <= m->row_count; i++) { cairo_move_to(c, cursor_x, cursor_y); cairo_line_to(c, cursor_x + me->width, cursor_y); @@ -322,9 +336,9 @@ construct_day_box(cairo_t *cd, int month, int day, int week_day) { if ((month == 0) && (day == 1) && (week_day == 1)) { } - - // draw_text(), and therefore pango_layout_set_text(), needs a - // string as input, so the current day's date is converted. + + /* draw_text(), and therefore pango_layout_set_text(), needs a string as + * input, so the current day's date is converted. */ snprintf(str, sizeof(str), "%d", day); set_color(cd, BLACK); @@ -334,14 +348,17 @@ construct_day_box(cairo_t *cd, int month, int day, int week_day) { -// Draws one month using daybox sub-surface & context, main context, and dimension struct +/* Draws one month using daybox sub-surface & context, main context, and + * dimension struct. */ int -draw_month_dates(cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *m, struct extents *me, struct extents *de) { +draw_month_dates(cairo_t *c, cairo_t *cd, cairo_surface_t *s, + struct month_info *m, struct extents *me, struct extents *de) { + double cursor_x = me->x; if (m->first_day != 0) cursor_x = cursor_x + (de->width * m->first_day); - // Push bottom of month as far down the page as the margin allows + /* Push bottom of month as far down the page as the margin allows. */ int row_offset = m->row_count - calculate_minimum_rows(m); double cursor_y = me->y + (row_offset * de->height); @@ -352,27 +369,26 @@ draw_month_dates(cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info for (int i = 0; i < m->row_count; i++) { for (int k = current_day; k < m->column_count; k++) { - // When the increment reached the end of the month, end - // the function. + /* When the increment reached the end of the month, end the function. */ if (increment >= m->num_of_days) return 0; construct_day_box(cd, m->month_position, increment + 1, k); - // Position the constructed day box on main surface + /* Position the constructed day box on main surface */ cairo_set_source_surface (c, s, cursor_x, cursor_y); cairo_paint(c); - // Move cursor one "box" over + /* Move cursor one "box" over */ cursor_x = cursor_x + de->width; increment++; } current_day = 0; - - cursor_x = me->x; - cursor_y = cursor_y + de->height; + + cursor_x = me->x; + cursor_y = cursor_y + de->height; } cairo_stroke(cd); @@ -381,9 +397,10 @@ draw_month_dates(cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info -// Draws the month being drawn at the top of the page +/* Draws the month name at the top of the month image */ int -draw_month_title(cairo_t *c, struct extents *he, struct alignment *pl, char *font_family, int font_size, char *name) { +draw_month_title(cairo_t *c, struct extents *he, struct alignment *pl, + char *font_family, int font_size, char *name) { PangoRectangle rect; @@ -396,7 +413,16 @@ draw_month_title(cairo_t *c, struct extents *he, struct alignment *pl, char *fon return 0; } -//int -//draw_week_names(cairo_t *c, struct month_info *m, struct extents *me, struct extents *dte, char **names) { -// return 0; -//} +/* Draw the shorthand names for the days of the week on top of the month */ +int +draw_week_names(cairo_t *c, struct extents *me) { + char *names[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + + char *font_family = "Sans"; + int font_size = 10; + + for (int i = 0; i < 7; i++) { + draw_text(c, me->x, me->y, font_family, font_size, names[i]); + } + return 0; +} @@ -95,3 +95,6 @@ calculate_minimum_rows(struct month_info *m); int construct_day_box(cairo_t *cd, int month, int day, int week_day); + +int +draw_week_names(cairo_t *c, struct month_info *m, struct extents *me, struct extents *dte); diff --git a/src/image.c b/src/image.c index d9ee5fa..7452e8c 100644 --- a/src/image.c +++ b/src/image.c @@ -6,16 +6,19 @@ -// Draw an image at a specific point +/* Draw an image at a specific point */ +/* TODO: require extent struct as function argument, so both position and size + * needs to be declared. */ int draw_image(cairo_t *c, char *path, double x, double y) { cairo_surface_t *image; - // Checks if the file exists. If it doesn't, warn the user that it was not found. - // A file not existing will not terminate the program, but simply not render the image. + /* Checks if the file exists. If it doesn't, warn the user that it was not + * found. A file not existing will not terminate the program, but simply not + * render the image. */ FILE *file; if((file = fopen(path, "r")) != NULL) { - // file exists + /* file exists */ fclose(file); } else @@ -33,7 +36,7 @@ int draw_image(cairo_t *c, char *path, double x, double y) { double fake_height = 100; double width_scale = fake_width / width; - double height_scale = fake_height / height; + double height_scle = fake_height / height; cairo_scale(c, width_scale, height_scale); @@ -48,11 +51,11 @@ int draw_image(cairo_t *c, char *path, double x, double y) { -// Draw month art +/* Draw month art */ int draw_month_art(cairo_t *c, struct dimensions *d, struct extents *he, struct alignment *pl, char *path) { cairo_save(c); double width = he->width; - // TODO: Make this relative to text height + /* TODO: Make this relative to text height. */ double height = he->height - (pl->margin * 3.0); double cursor_x = pl->margin; @@ -64,5 +67,3 @@ int draw_month_art(cairo_t *c, struct dimensions *d, struct extents *he, struct cairo_restore(c); return 0; } - - |
