From 4b18819e5f34358cab1bf1c1514d443421d7968c Mon Sep 17 00:00:00 2001 From: foswret Date: Fri, 12 Dec 2025 15:13:48 -0600 Subject: All months are now able to be generated correctly in one PDF. Therefore, all absolutely core functionality is done. --- src/calp.c | 106 ++++++++++++++++++++++++++++++++----------------------------- src/calp.o | Bin 13272 -> 14008 bytes src/date.c | 4 +++ src/date.h | 2 ++ src/date.o | Bin 0 -> 3112 bytes 5 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 src/date.c create mode 100644 src/date.o (limited to 'src') diff --git a/src/calp.c b/src/calp.c index 278538e..d18498d 100644 --- a/src/calp.c +++ b/src/calp.c @@ -18,11 +18,12 @@ // int main (int argc, char *argv[]) { int main (void) { - /* Dimension initialization */ - int rows = 6; /* Number of weeks in a month */ - int columns = 7; /* Number of days in a week */ - double margin = 0.5; /* 0.5" */ - int number_of_days = 28; + // Dimension initialization + int rows = 6; // Number of weeks in a month + int columns = 7; // Number of days in a week + int months_in_year = 12; + double margin = 0.5; // 0.5" + //int number_of_days = 28; double month_origin_x = margin; double month_origin_y = 3.0; @@ -34,69 +35,72 @@ int main (void) { double day_box_height = month_height / rows; struct dimensions dim = { - paper_width, /* pw */ - paper_height, /* ph */ - month_width, /* mw */ - month_height, /* mh */ - day_box_width, /* bw */ - day_box_height, /* bh */ - rows, /* r */ - columns, /* c */ - margin, /* m */ + paper_width, // pw + paper_height, // ph + month_width, // mw + month_height, // mh + day_box_width, // bw + day_box_height, // bh + rows, // r + columns, // c + margin, // m }; - /* Date Setup */ + // Date Setup char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; - //int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + // If year is a leap year, isleap(year) = 1, and will be added to February. + int year = 2028; + int days_in_month[12] = {31, 28 + isleap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - // const char *weekday_names[] = { - // "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" - // }; - - // const char *weekdays_abbr[] = { - // "Su", "Mo", "Tue", "Wed", "Thurs", "Fri", "Sat" - // }; - - int year = 2025; - int month = 2; - - struct tm day; - day.tm_mon = month - 1; - day.tm_mday = 1; - day.tm_year = year - 1900; - - /* tm.hour, tm_min, & tm_sec are not used, but need to be zeroed. */ - day.tm_hour = 0; - day.tm_min = 0; - day.tm_sec = 0; - - mktime(&day); - - cairo_surface_t * surface = cairo_pdf_surface_create("0.pdf", dim.pw * 72, dim.ph * 72); + + // Because 1px = 1/72", paper width & paper height must be multiplied by 72 + cairo_surface_t * surface = cairo_pdf_surface_create("output.pdf", dim.pw * 72, dim.ph * 72); cairo_t *cr = cairo_create(surface); + // Once paper dimensions have been initialized, they can be scaled back by the same factor they + // were multiplied to make the dimensions are easier to work with. cairo_scale(cr, 72, 72); - set_color(cr, "GRAY"); cairo_set_line_width (cr, 0.03); - clock_t begin = clock(); - draw_calendar(cr, month_origin_x, month_origin_y, dim); - draw_numbers(cr, month_origin_x, month_origin_y, dim, day.tm_wday, number_of_days); - draw_month_name(cr, months[1], dim); - - /* Clean Up */ + struct tm day; + static const struct tm empty_day; + + + // For some reason, this loop requires a zeroing of the day struct & running mktime() twice. + // Or else, the January and December days will be offset by +1/-1. I assume this is a + // quirk of time.h. + for (int i = 0; i < months_in_year; i++) { + mktime(&day); + day.tm_year = year - 1900; + day.tm_mon = i; + day.tm_mday = 1; + + // tm.hour, tm_min, & tm_sec are not used, but need to be zeroed. + day.tm_hour = 0; + day.tm_min = 0; + day.tm_sec = 0; + + mktime(&day); + + draw_calendar(cr, month_origin_x, month_origin_y, dim); + draw_numbers(cr, month_origin_x, month_origin_y, dim, (day.tm_wday), days_in_month[i]); + draw_month_name(cr, months[i], dim); + + day = empty_day; + +// Moves to the next page of the pdf + cairo_show_page(cr); + } + + // Clean Up cairo_destroy(cr); cairo_surface_destroy(surface); - - clock_t end = clock(); - double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; - printf("Execution Time: %.3fs\n", time_spent); return(0); } diff --git a/src/calp.o b/src/calp.o index 23e90f5..19c2195 100644 Binary files a/src/calp.o and b/src/calp.o differ diff --git a/src/date.c b/src/date.c new file mode 100644 index 0000000..48642dd --- /dev/null +++ b/src/date.c @@ -0,0 +1,4 @@ +int isleap(int year) +{ + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); +} diff --git a/src/date.h b/src/date.h index 4ff3d32..becc5a2 100644 --- a/src/date.h +++ b/src/date.h @@ -1 +1,3 @@ /* date.h: Includes all the functions relating to calculating months, days, etc. */ + +int isleap(int year); diff --git a/src/date.o b/src/date.o new file mode 100644 index 0000000..bc27b4b Binary files /dev/null and b/src/date.o differ -- cgit v1.2.3