aboutsummaryrefslogtreecommitdiff
path: root/src/draw.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/draw.c')
-rw-r--r--src/draw.c115
1 files changed, 99 insertions, 16 deletions
diff --git a/src/draw.c b/src/draw.c
index 931bf61..259f995 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -3,8 +3,6 @@
#include <stdio.h>
#include <stdlib.h>
-#define FONT "Monospace 10"
-
// struct for storing dimensions used for calculations, movement, etc.
struct dimensions {
// Multiplication factors
@@ -25,6 +23,15 @@ struct dimensions {
};
+// Get the day of the week for a given day
+// Mostly used for calculating when the start of a month is
+// Relative to "Su Mo ... 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
@@ -38,20 +45,28 @@ 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)
// Creates and destroys layout within the scope of the function
// Is that performant when looped over 365 operations?
-int draw_text (cairo_t *c, double x, double y, char *text) {
+int 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
+ snprintf(font, sizeof(font), "%s %d", font_family, font_size);
+
+ //Initialize Pango Stuff
PangoLayout *layout;
PangoFontDescription *desc;
+
// Create a PangoLayout, setting font & text
layout = pango_cairo_create_layout(c);
- desc = pango_font_description_from_string(FONT);
+ desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
+ pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
pango_layout_set_text(layout, text, -1);
- //printf("text: %s\n", text);
cairo_move_to (c, x, y);
pango_cairo_show_layout (c, layout);
@@ -60,6 +75,30 @@ int draw_text (cairo_t *c, double x, double y, char *text) {
}
+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
+ 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
int calculate_dimensions(double pw, double ph, struct dimensions *d) {
@@ -109,6 +148,35 @@ int print_dimensions(struct dimensions *d) {
return 0;
}
+// 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 extra_rows = 0;
+
+ int days_in_first_week = (d->column_count - first_day);
+
+ if (first_day == 0) {
+ days_in_first_week = first_day;
+ }
+
+ int days_in_last_week = (num_of_days - days_in_first_week) % 7;
+
+ int middle_weeks = (num_of_days - days_in_first_week - days_in_last_week) / 7;
+
+ if (days_in_first_week != 0) {
+ extra_rows++;
+ }
+
+ if (days_in_last_week != 0) {
+ extra_rows++;
+ }
+
+ int minimum_rows = middle_weeks + extra_rows;
+
+ return minimum_rows;
+}
+
// Draws the lines for a month
//int draw_month_grid (cairo_t *c, int month, int start_day) {
@@ -122,9 +190,12 @@ int print_dimensions(struct dimensions *d) {
// 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) {
double cursor_x = d->month_top_corner_x;
+
if (start_day != 0) cursor_x = cursor_x + (d->day_width * start_day);
- printf("%d\n", start_day);
- double cursor_y = d->month_top_corner_y;
+
+ // 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);
+ double cursor_y = d->month_top_corner_y + (row_offset * d->day_height);
int current_day = start_day;
@@ -138,8 +209,13 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *
// the function.
if (increment >= days_in_month) return 0;
- // Fill bg with color
+
+ 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);
+ }
+
fill_bg(cd);
// Change text color
@@ -151,7 +227,11 @@ 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, str);
+ 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, "▨");
+ }
// Position sub-surface on main surface
@@ -173,11 +253,14 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *
return 0;
}
-// Get the day of the week for a given day
-// Mostly used for calculating when the start of a month is
-// Relative to "Su Mo ... 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);
+int draw_month_title(cairo_t *c, struct dimensions *d, char *name) {
+ PangoRectangle rect;
+
+ rect = get_logical_extents(c, "Sans", 40, name);
+
+
+ double cursor_x = (d->paper_width / 2.0) - ((rect.width / 2.0));
+ double cursor_y = 50.0;
+ draw_text(c, cursor_x, cursor_y, "Sans", 40, name);
+ return 0;
}