diff options
| author | foswret | 2026-05-04 15:04:06 -0500 |
|---|---|---|
| committer | foswret | 2026-05-04 15:04:06 -0500 |
| commit | 8d760e61cd8db7e299938f061fd0d9425dbbd12c (patch) | |
| tree | dd65fbf1507bbdac4a21c1782e54c137a3e03603 | |
| parent | ff329ec93ddacae823704bac2d95e7a830a456b3 (diff) | |
draw month grid lines
| -rwxr-xr-x | calp | bin | 34376 -> 41952 bytes | |||
| -rw-r--r-- | src/calp.c | 32 | ||||
| -rw-r--r-- | src/draw.c | 122 | ||||
| -rw-r--r-- | src/draw.h | 21 |
4 files changed, 142 insertions, 33 deletions
| Binary files differ @@ -10,6 +10,9 @@ #define A4_WIDTH 210 #define A4_HEIGHT 297 +#define US_LETTER_WIDTH 216 +#define US_LETTER_HEIGHT 279 + // Font Settings #define FONT_FAMILY "Sans" #define FONT_SIZE_NORMAL 10 @@ -18,9 +21,15 @@ int main (void) { // Definitions // ----------- + //double width = US_LETTER_WIDTH; + //double height = US_LETTER_HEIGHT; + + double width = A4_WIDTH; + double height = A4_HEIGHT; + // Convert to inches, then 1 unit = 1/72th inch - double width = (A4_WIDTH / 25.4) * 72.0; - double height = (A4_HEIGHT / 25.4) * 72.0; + width = (width / 25.4) * 72.0; + height = (height / 25.4) * 72.0; extern char *months[]; int num_of_months = 12; @@ -33,6 +42,8 @@ int main (void) { // Create dimensions struct to hold measurements struct dimensions dim; + struct month_info month; + // Leap year adjustment: increment February day count by one int year = 2026; @@ -73,14 +84,27 @@ int main (void) { //print_dimensions(&dim); // Draw all months + // + struct RGB color; + + color = hex_to_rgb("#81bbd3"); + + printf("red: %f\n", color.r); + printf("green: %f\n", color.g); + printf("blue: %f\n", color.b); + for (int i = 0; i < num_of_months; i++) { + // Set the + month.first_day = day_of_week(1, i + 1, year); + month.num_of_days = days_in_month[i]; cairo_set_source_rgb (paper, 0.0, 0.0, 0.0); draw_month_title(paper, &dim, months[i]); - calculate_minimum_rows(day_of_week(1, i + 1, year), days_in_month[i], &dim); + calculate_minimum_rows(&month, &dim); - draw_month(paper, cd, day, &dim, days_in_month[i], day_of_week(1, i + 1, year)); + draw_month(paper, cd, day, &month, &dim); + draw_month_grid(paper, &month, &dim); // Go to next page in pdf cairo_show_page(paper); @@ -22,6 +22,36 @@ struct dimensions { double margin; /* margin */ }; +struct month_info { + int first_day; + int num_of_days; +}; + +struct RGB { + double r; + double g; + double b; +}; + +struct RGB hex_to_rgb(char *str) { + double r, g, b; + unsigned int ri, gi, bi; + struct RGB color; + + sscanf(str, "%02x%02x%02x", &ri, &gi, &bi); + + r = ri; + g = gi; + b = bi; + + printf("%d, %d, %d\n", ri, gi, bi); + + color.r = (r / 255.0); + color.g = (g / 255.0); + color.b = (b / 255.0); + + return color; +} // Get the day of the week for a given day // Mostly used for calculating when the start of a month is @@ -34,6 +64,7 @@ int day_of_week (int d, int m, int y) { + // Fill entire background with current color // Function name is changed for clarity int fill_bg(cairo_t *c) { @@ -43,6 +74,7 @@ int fill_bg(cairo_t *c) { + // Draw text at a certain point. // Needn't be a string pointer, can be fixed array of chars // Requires Font Family (string) and Size (int) @@ -151,18 +183,18 @@ int print_dimensions(struct dimensions *d) { // Calculate minimum amount of rows a month can fit in. // Most months can fit within 4 or 5 rows instead of 6. // TBD: This can probably done in a more efficient way -int calculate_minimum_rows(int first_day, int num_of_days, struct dimensions *d) { +int calculate_minimum_rows(struct month_info *m, struct dimensions *d) { int extra_rows = 0; - int days_in_first_week = (d->column_count - first_day); + int days_in_first_week = (d->column_count - m->first_day); - if (first_day == 0) { - days_in_first_week = first_day; + if (m->first_day == 0) { + days_in_first_week = m->first_day; } - int days_in_last_week = (num_of_days - days_in_first_week) % 7; + int days_in_last_week = (m->num_of_days - days_in_first_week) % 7; - int middle_weeks = (num_of_days - days_in_first_week - days_in_last_week) / 7; + int middle_weeks = (m->num_of_days - days_in_first_week - days_in_last_week) / 7; if (days_in_first_week != 0) { extra_rows++; @@ -180,24 +212,51 @@ int calculate_minimum_rows(int first_day, int num_of_days, struct dimensions *d) // Draws the lines for a month //int draw_month_grid (cairo_t *c, int month, int start_day) { -//int draw_month_grid (cairo_t *c, struct dimensions *d) { -// cairo_move_to (c, d->month_top_corner_x, d->month_top_corner_y); -// return 0; -//} +int draw_month_grid (cairo_t *c, struct month_info *m, struct dimensions *d) { + double cursor_x = d->month_top_corner_x; + + int row_offset = d->row_count - calculate_minimum_rows(m, d); + double cursor_y = d->month_top_corner_y + (row_offset * d->day_height); + + double x = cursor_x; + double y = cursor_y; + + cairo_set_source_rgb (c, 0.0, 0.0, 0.0); + cairo_set_line_width (c, 1.0); + + for (int i = 0; i <= d->column_count; i++) { + cairo_move_to(c, cursor_x, cursor_y); + cairo_line_to(c, cursor_x, cursor_y + (d->month_height - (row_offset * d->day_height))); + cursor_x = cursor_x + d->day_width; + } + + // Recenter the cursor + cursor_x = x; + cursor_y = y; + + for (int i = 0; i <= d->row_count; i++) { + cairo_move_to(c, cursor_x, cursor_y); + cairo_line_to(c, cursor_x + d->month_width, cursor_y); + cursor_y = cursor_y + d->day_height; + } + + cairo_stroke(c); + return 0; +} // Draws one month using daybox sub-surface & context, main context, and dimension struct -int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *d, int days_in_month, int start_day) { +int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *m, struct dimensions *d) { double cursor_x = d->month_top_corner_x; - if (start_day != 0) cursor_x = cursor_x + (d->day_width * start_day); + if (m->first_day != 0) cursor_x = cursor_x + (d->day_width * m->first_day); // Push bottom of month as far down the page as the margin allows - int row_offset = d->row_count - calculate_minimum_rows(start_day, days_in_month, d); + int row_offset = d->row_count - calculate_minimum_rows(m, d); double cursor_y = d->month_top_corner_y + (row_offset * d->day_height); - int current_day = start_day; + int current_day = m->first_day; int increment = 0; @@ -207,19 +266,21 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions * for (int k = current_day; k < d->column_count; k++) { // When the increment reached the end of the month, end // the function. - if (increment >= days_in_month) return 0; + if (increment >= m->num_of_days) return 0; + cairo_set_source_rgb (cd, 1.0, 1.0, 1.0); - if ((k == 0) || (k == 6)) { - cairo_set_source_rgb (cd, 255.0, 0.0, 0.0); - } else { - cairo_set_source_rgb (cd, 0.0, 0.0, 255.0); - } + //if ((k == 0) || (k == 6)) { + //cairo_set_source_rgb (cd, 0.76862745098, 0.533333333333, 0.18431372549); + //} else { + // cairo_set_source_rgb (cd, 0.5, 0.5, 0.5); + //} fill_bg(cd); // Change text color - cairo_set_source_rgb (cd, 255.0, 255.0, 255.0); + cairo_set_source_rgb (cd, 0.0, 0.0, 0.0); + //cairo_set_source_rgb (cd, 255.0, 255.0, 255.0); // draw_text(), and by extension pango_layout_set_text(), needs a // string as input, so the current day's date is converted. @@ -229,9 +290,9 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions * // Position within cd is arbitrary: (0,0) is too close to edge draw_text(cd, 4.0, 4.0, "Sans", 10, str); - if ((k == 0) || (k == 6)) { - draw_text(cd, d->day_width - 40.0, d->day_height - 40.0, "Sans", 10, "▨"); - } + //if ((k == 0) || (k == 6)) { + //draw_text(cd, d->day_width - 40.0, d->day_height - 40.0, "Sans", 10, "▨"); + //} // Position sub-surface on main surface @@ -264,3 +325,16 @@ int draw_month_title(cairo_t *c, struct dimensions *d, char *name) { draw_text(c, cursor_x, cursor_y, "Sans", 40, name); return 0; } + + +//int draw_weekday_names(cairo_t *c, struct dimensions *d) { +// +//} + + + + + + + + @@ -14,15 +14,28 @@ struct dimensions { int fill_bg(cairo_t *c); +struct RGB { + double r; + double g; + double b; +}; + +struct month_info { + int first_day; + int num_of_days; +}; + +struct RGB hex_to_rgb(char *str); + int draw_text (cairo_t *c, double x, double y, char *font_family, int font_size, char *text); int calculate_dimensions(double pw, double ph, struct dimensions *d); int print_dimensions(struct dimensions *d); -//int draw_month_grid (cairo_t *c, struct dimensions *d); +int draw_month_grid (cairo_t *c, struct month_info *m, struct dimensions *d); -int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *d, int days_in_month, int start_day); +int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *m, struct dimensions *d); int day_of_week (int d, int m, int y); @@ -30,6 +43,4 @@ int draw_month_title(cairo_t *c, struct dimensions *d, char *name); PangoRectangle get_logical_extents (cairo_t *c, char *font_family, int font_size, char *text); -int calculate_minimum_rows(int first_day, int num_of_days, struct dimensions *d); - - +int calculate_minimum_rows(struct month_info *m, struct dimensions *d); |
