diff options
| author | foswret | 2026-10-10 20:55:15 -0500 |
|---|---|---|
| committer | foswret | 2026-10-10 20:55:15 -0500 |
| commit | f9bb2adaa11be25f880c323f66210a13868fa90d (patch) | |
| tree | 877403139ac5d177fe72737e6b99058e7f461ad2 /src/calp.c | |
| parent | 2fd20536872d24db4fe7507ed7da22db99bb7c14 (diff) | |
Diffstat (limited to 'src/calp.c')
| -rw-r--r-- | src/calp.c | 114 |
1 files changed, 55 insertions, 59 deletions
@@ -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); |
