aboutsummaryrefslogtreecommitdiff
path: root/src/draw.c
diff options
context:
space:
mode:
authorfoswret2026-05-05 10:59:05 -0500
committerfoswret2026-05-05 10:59:05 -0500
commit567b98381670eb96264fb8ae3621595ebfba1c5b (patch)
tree078b40fdabe8200f8cc471899765d45c04959918 /src/draw.c
parent8d760e61cd8db7e299938f061fd0d9425dbbd12c (diff)
add color.h, create draw_image()
Diffstat (limited to 'src/draw.c')
-rw-r--r--src/draw.c98
1 files changed, 66 insertions, 32 deletions
diff --git a/src/draw.c b/src/draw.c
index 2489ada..3fc8244 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include "color.h"
+
// struct for storing dimensions used for calculations, movement, etc.
struct dimensions {
// Multiplication factors
@@ -22,17 +24,24 @@ struct dimensions {
double margin; /* margin */
};
+// struct to hold Month-specific info, like what day of the week the first
+// day lands on, how many days are in the month, etc.
struct month_info {
int first_day;
int num_of_days;
};
+
struct RGB {
double r;
double g;
double b;
};
+
+// Convert Hex color strings to their RGB counterparts.
+// IMO, hex strings are easier to work with, so this is
+// a nice helper function.
struct RGB hex_to_rgb(char *str) {
double r, g, b;
unsigned int ri, gi, bi;
@@ -44,8 +53,6 @@ struct RGB hex_to_rgb(char *str) {
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);
@@ -53,9 +60,20 @@ struct RGB hex_to_rgb(char *str) {
return color;
}
+
+
+// Set a context's color using a HEX code. Color macros are defined
+// in color.h
+int set_color(cairo_t *c, char *hex) {
+ struct RGB color = hex_to_rgb(hex);
+ cairo_set_source_rgb (c, color.r, color.g, color.b);
+ 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
+// Used for calculating when the start of a month is
+// Relative to "Su Mo Tu ... 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) {
@@ -75,7 +93,6 @@ 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
@@ -107,6 +124,8 @@ int draw_text (cairo_t *c, double x, double y, char *font_family, int font_size,
}
+// Get the dimensions of a string of text
+// Returns a PangoRectangle struct
PangoRectangle get_logical_extents (cairo_t *c, char *font_family, int font_size, char *text) {
char font[100];
@@ -133,6 +152,7 @@ PangoRectangle get_logical_extents (cairo_t *c, char *font_family, int font_size
// Calculate dimenions for one page and stores it in the dimensions struct
+// Used for determining where elements should be drawn on the page
int calculate_dimensions(double pw, double ph, struct dimensions *d) {
// Percentage of page that should be margin
double margin = 0.1 * pw;
@@ -165,6 +185,8 @@ int 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);
@@ -180,6 +202,8 @@ 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
@@ -210,8 +234,8 @@ int calculate_minimum_rows(struct month_info *m, struct dimensions *d) {
}
-// Draws the lines for a month
-//int draw_month_grid (cairo_t *c, int month, int start_day) {
+
+// Draws the grid lines for a month
int draw_month_grid (cairo_t *c, struct month_info *m, struct dimensions *d) {
double cursor_x = d->month_top_corner_x;
@@ -221,7 +245,7 @@ int draw_month_grid (cairo_t *c, struct month_info *m, struct dimensions *d) {
double x = cursor_x;
double y = cursor_y;
- cairo_set_source_rgb (c, 0.0, 0.0, 0.0);
+ set_color(c, BLACK);
cairo_set_line_width (c, 1.0);
for (int i = 0; i <= d->column_count; i++) {
@@ -268,38 +292,28 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *
// the function.
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, 0.76862745098, 0.533333333333, 0.18431372549);
- //} else {
- // cairo_set_source_rgb (cd, 0.5, 0.5, 0.5);
- //}
-
+ set_color(cd, WHITE);
fill_bg(cd);
// Change text color
- cairo_set_source_rgb (cd, 0.0, 0.0, 0.0);
- //cairo_set_source_rgb (cd, 255.0, 255.0, 255.0);
+ set_color(cd, BLACK);
- // draw_text(), and by extension pango_layout_set_text(), needs a
+
+ // draw_text(), and therefore pango_layout_set_text(), needs a
// string as input, so the current day's date is converted.
snprintf(str, sizeof(str), "%d", increment + 1);
- // Position within cd is arbitrary: (0,0) is too close to edge
+ // Position within cd is arbitrary: (0,0) is too close to edge,
+ // so the text is offset a little bit.
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
cairo_set_source_surface (c, s, cursor_x, cursor_y);
cairo_paint(c);
- // Move cursor one box over
+ // Move cursor one "box" over
cursor_x = cursor_x + d->day_width;
increment++;
}
@@ -314,27 +328,47 @@ int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct month_info *
return 0;
}
+
+
+// Draws the month being drawn at the top of the page
int draw_month_title(cairo_t *c, struct dimensions *d, char *name) {
PangoRectangle rect;
- rect = get_logical_extents(c, "Sans", 40, name);
+ char *font = "Serif";
+ int font_size = 40;
+ rect = get_logical_extents(c, font, font_size, 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);
+ double cursor_y = d->margin;
+ draw_text(c, cursor_x, cursor_y, font, font_size, name);
return 0;
}
-//int draw_weekday_names(cairo_t *c, struct dimensions *d) {
-//
-//}
-
+// Draw an image at a specified point
+int draw_image(cairo_t *c, char *path, double x, double y) {
+ cairo_surface_t *image;
+ image = cairo_image_surface_create_from_png(path);
+ cairo_set_source_surface(c, image, x, y);
+ cairo_paint(c);
+ cairo_surface_destroy (image);
+ return 0;
+}
+// Draw month art, if specified, above the calendar but below the
+// month's name. Images are cropped if they are too big or don't fit
+// the dimensions
+//int draw_month_art(cairo_t *c, struct dimensions *d) {
+//
+// return 0;
+//}
+//int draw_weekday_names(cairo_t *c, struct dimensions *d) {
+//
+//}