diff options
| author | foswret | 2026-05-23 14:09:36 -0500 |
|---|---|---|
| committer | foswret | 2026-05-23 14:09:36 -0500 |
| commit | 5ead4f6c3d36557c56e27cf52835409d81ae6ed8 (patch) | |
| tree | 006ecd1051d3b0fd142fe55a90973e5746d3e5fb /src/draw.c | |
| parent | 924ca70a4006abbffc8f43f27c4f64c8fc517a08 (diff) | |
remove month art drawing for now, working toward removing dimension struct in favor of generic extent structs
Diffstat (limited to 'src/draw.c')
| -rw-r--r-- | src/draw.c | 99 |
1 files changed, 52 insertions, 47 deletions
@@ -5,6 +5,7 @@ #include "color.h" + // struct for storing dimensions used for calculations, movement, etc. struct dimensions { @@ -34,6 +35,8 @@ month_info { int month_position; int first_day; int num_of_days; + int column_count; + int row_count; int minimum_rows; }; @@ -56,7 +59,10 @@ struct extents { double height; }; - +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 @@ -170,6 +176,30 @@ get_logical_extents (cairo_t *c, char *font_family, int font_size, char *text) { } +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 + snprintf(font, sizeof(font), "%s %d", font_family, font_size); + + PangoLayout *layout; + PangoFontDescription *desc; + PangoRectangle rect; + + // 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); + pango_font_description_free(desc); + + pango_layout_set_text(layout, text, -1); + pango_layout_get_pixel_extents(layout, &rect, NULL); + //pango_layout_get_extents(layout, NULL, &rect); + + g_object_unref(layout); + 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 @@ -208,27 +238,6 @@ calculate_dimensions(double pw, double ph, struct dimensions *d) { } -// Print the contents of a dimensions struct. -// Use for debugging -int -print_dimensions(struct dimensions *d) { - printf("row count: %d\n", d->row_count); - printf("column count: %d\n", d->column_count); - printf("paper width: %.2f\n", d->paper_width); - printf("paper height: %.2f\n", d->paper_height); - printf("margin: %.2f\n", d->margin); - printf("month width: %.2f\n", d->month_width); - printf("month height: %.2f\n", d->month_height); - printf("month_top_corner_x: %f\n", d->month_top_corner_x); - printf("month_top_corner_y: %f\n", d->month_top_corner_y); - printf("day width: %.2f\n", d->day_width); - printf("day height: %.2f\n", d->day_height); - //printf("header width: %.2f\n", d->header_width); - //printf("header height: %.2f\n", d->header_height); - return 0; -} - - // Calculate minimum amount of rows a month can fit in. // Most months can fit within 4 or 5 rows instead of 6. @@ -266,13 +275,10 @@ calculate_minimum_rows(struct month_info *m) { int draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct extents *de) { double cursor_x = me->x; - - int column_count = 7; - int row_count = 6; // double cursor_x = month_grid_pos->x; // make d->row_count reside in month_info *m - int row_offset = row_count - calculate_minimum_rows(m); + int row_offset = m->row_count - calculate_minimum_rows(m); // double cursor_y = month_grid_pos->y; double cursor_y = me->y + (row_offset * de->height); @@ -283,7 +289,7 @@ draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct ex cairo_set_line_width(c, 1.0); // Vertical lines - for (int i = 0; i <= column_count; i++) { + 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; @@ -294,7 +300,7 @@ draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct ex cursor_y = y; // Horizontal lines - for (int i = 0; i <= row_count; i++) { + 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); cursor_y = cursor_y + de->height; @@ -309,7 +315,7 @@ draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct ex int construct_day_box(cairo_t *cd, int month, int day, int week_day) { char str[20]; - double padding = 8.0; + double padding = 3.0; set_color(cd, WHITE); fill_bg(cd); @@ -330,22 +336,22 @@ 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 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; +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 + (d->day_width * m->first_day); + 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 - int row_offset = d->row_count - calculate_minimum_rows(m); - double cursor_y = d->month_top_corner_y + (row_offset * d->day_height); + int row_offset = m->row_count - calculate_minimum_rows(m); + double cursor_y = me->y + (row_offset * de->height); int current_day = m->first_day; int increment = 0; - for (int i = 0; i < d->row_count; i++) { - for (int k = current_day; k < d->column_count; k++) { + 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. if (increment >= m->num_of_days) return 0; @@ -359,14 +365,14 @@ draw_month(cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *m, st cairo_paint(c); // Move cursor one "box" over - cursor_x = cursor_x + d->day_width; + cursor_x = cursor_x + de->width; increment++; } current_day = 0; - cursor_x = d->month_top_corner_x; - cursor_y = cursor_y + d->day_height; + cursor_x = me->x; + cursor_y = cursor_y + de->height; } cairo_stroke(cd); @@ -377,21 +383,20 @@ draw_month(cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *m, st // Draws the month being drawn at the top of the page int -draw_month_title(cairo_t *c, struct dimensions *d, 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; rect = get_logical_extents(c, font_family, font_size, name); - //double cursor_x = (d->header_height / 2.0) - ((rect.width / 2.0)); - double cursor_x = ((d->header_width - rect.width) / 2.0); - //double cursor_y = height + d->margin; - double cursor_y = d->header_height - rect.height - d->margin; + double cursor_x = ((he->width - rect.width) / 2.0); + double cursor_y = he->height - pl->margin - rect.height; + draw_text(c, cursor_x, cursor_y, font_family, font_size, name); return 0; } - -//int draw_weekday_names(cairo_t *c, struct dimensions *d) { -// +//int +//draw_week_names(cairo_t *c, struct month_info *m, struct extents *me, struct extents *dte, char **names) { +// return 0; //} |
