aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfoswret2026-05-05 10:59:05 -0500
committerfoswret2026-05-05 10:59:05 -0500
commit567b98381670eb96264fb8ae3621595ebfba1c5b (patch)
tree078b40fdabe8200f8cc471899765d45c04959918 /src
parent8d760e61cd8db7e299938f061fd0d9425dbbd12c (diff)
add color.h, create draw_image()
Diffstat (limited to 'src')
-rw-r--r--src/calp.c43
-rw-r--r--src/color.h4
-rw-r--r--src/date.obin4464 -> 5016 bytes
-rw-r--r--src/draw.c98
-rw-r--r--src/draw.h5
5 files changed, 101 insertions, 49 deletions
diff --git a/src/calp.c b/src/calp.c
index d9f0d3a..6748fd0 100644
--- a/src/calp.c
+++ b/src/calp.c
@@ -1,3 +1,5 @@
+//calp: printable calendar generator
+
#include <stdio.h>
#include <stdlib.h>
#include <cairo/cairo-pdf.h>
@@ -5,6 +7,7 @@
#include "draw.h"
#include "date.h"
+#include "color.h"
// Metric
#define A4_WIDTH 210
@@ -21,9 +24,6 @@
int main (void) {
// Definitions
// -----------
- //double width = US_LETTER_WIDTH;
- //double height = US_LETTER_HEIGHT;
-
double width = A4_WIDTH;
double height = A4_HEIGHT;
@@ -42,10 +42,13 @@ int main (void) {
// Create dimensions struct to hold measurements
struct dimensions dim;
+ // Create dimensions struct to hold information about a
+ // month
struct month_info month;
- // Leap year adjustment: increment February day count by one
+ // Leap year adjustment: increment February day count by one if
+ // it is a leap year.
int year = 2026;
if (isleap(year)) {
days_in_month[1]++;
@@ -53,7 +56,6 @@ int main (void) {
-
// Initialization
// --------------
// Calculates measurements, stores them in dimensions struct. See "draw.h"
@@ -72,7 +74,7 @@ int main (void) {
// PangoRectangle logical_extents;
// Fill background with white color
- cairo_set_source_rgb (paper, 255.0, 255.0, 255.0);
+ set_color(paper, WHITE);
fill_bg(paper);
@@ -84,29 +86,36 @@ int main (void) {
//print_dimensions(&dim);
// Draw all months
- //
- struct RGB color;
+ //struct RGB color;
+
+ //color = hex_to_rgb("81bbd3");
- color = hex_to_rgb("#81bbd3");
+ //printf("red: %f\n", color.r);
+ //printf("green: %f\n", color.g);
+ //printf("blue: %f\n", color.b);
- printf("red: %f\n", color.r);
- printf("green: %f\n", color.g);
- printf("blue: %f\n", color.b);
+ // Range of months to be rendered (Default: Full calendar year, 12 months)
+ int first_month = 0;
+ int end_month = num_of_months;
- for (int i = 0; i < num_of_months; i++) {
- // Set the
+
+ // Month generation loop
+ for (int i = first_month; i < end_month; i++) {
+ // Initialize month struct
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_image(paper, "assets/weather.png", 0, 0);
+
+
+ set_color(paper, BLACK);
draw_month_title(paper, &dim, months[i]);
- calculate_minimum_rows(&month, &dim);
draw_month(paper, cd, day, &month, &dim);
draw_month_grid(paper, &month, &dim);
- // Go to next page in pdf
+ // Go to the next page in pdf
cairo_show_page(paper);
}
diff --git a/src/color.h b/src/color.h
new file mode 100644
index 0000000..2ce4995
--- /dev/null
+++ b/src/color.h
@@ -0,0 +1,4 @@
+#define BLACK "000000"
+#define WHITE "FFFFFF"
+#define GREEN "008000"
+#define RED "FF2052"
diff --git a/src/date.o b/src/date.o
index 5fdd91f..29dd3ed 100644
--- a/src/date.o
+++ b/src/date.o
Binary files differ
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) {
+//
+//}
diff --git a/src/draw.h b/src/draw.h
index b11cdea..223dbbb 100644
--- a/src/draw.h
+++ b/src/draw.h
@@ -27,6 +27,8 @@ struct month_info {
struct RGB hex_to_rgb(char *str);
+int set_color(cairo_t *c, char *hex);
+
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);
@@ -44,3 +46,6 @@ 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(struct month_info *m, struct dimensions *d);
+
+int draw_image(cairo_t *c, char *path, double x, double y);
+