aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfoswret <foswret@posteo.com>2025-11-10 23:13:02 -0600
committerfoswret <foswret@posteo.com>2025-11-10 23:13:02 -0600
commit8bb9c7ebf9d902774134bc6aa94b0886fbf82933 (patch)
tree5fbf5947592a62648e3d9ba38ecbddcf94114023
parentd6ff91295f1a6eb5f6f8a7e77c66d4c9cba694ac (diff)
moved all drawing functions to /include
-rw-r--r--0.pdfbin7444 -> 7285 bytes
-rw-r--r--include/color.h9
-rw-r--r--include/date.h1
-rw-r--r--include/draw.h94
-rw-r--r--main.c157
5 files changed, 154 insertions, 107 deletions
diff --git a/0.pdf b/0.pdf
index 7372a1e..2995589 100644
--- a/0.pdf
+++ b/0.pdf
Binary files differ
diff --git a/include/color.h b/include/color.h
index f7ac991..e2b4cc0 100644
--- a/include/color.h
+++ b/include/color.h
@@ -2,8 +2,17 @@
if (strcmp("BLACK", str) == 0) {
cairo_set_source_rgb (c, 0.0, 0.0, 0.0);
}
+ if (strcmp("RED", str) == 0) {
+ cairo_set_source_rgb (c, 255.0, 0.0, 0.0);
+ }
+ if (strcmp("GREEN", str) == 0) {
+ cairo_set_source_rgb (c, 0.0, 255.0, 0.0);
+ }
if (strcmp("BLUE", str) == 0) {
cairo_set_source_rgb (c, 0.0, 0.0, 255.0);
}
+ if (strcmp("GREY", str) == 0) {
+ cairo_set_source_rgb (c, 128.0, 128.0, 128.0);
+ }
return(0);
}
diff --git a/include/date.h b/include/date.h
new file mode 100644
index 0000000..4ff3d32
--- /dev/null
+++ b/include/date.h
@@ -0,0 +1 @@
+/* date.h: Includes all the functions relating to calculating months, days, etc. */
diff --git a/include/draw.h b/include/draw.h
new file mode 100644
index 0000000..503b9c5
--- /dev/null
+++ b/include/draw.h
@@ -0,0 +1,94 @@
+struct dimensions {
+ double pw; /* paper width */
+ double ph; /* paper height */
+ double mw; /* month width */
+ double mh; /* month height */
+ double bw; /* day box width */
+ double bh; /* day box height */
+ double r; /* rows */
+ double c; /* columns */
+ double m; /* margin */
+};
+
+
+
+int draw_calendar(cairo_t *c, double x, double y, struct dimensions d) {
+ double x1, y1, x2, y2;
+ x1 = x;
+ y1 = y;
+ x2 = d.pw - x1; /* Set the line endpoint to right side of the page */
+
+ for (int i = 0; i < d.r + 1; i++) { /* Draw Horizontal Lines */
+ cairo_move_to (c, x, y);
+ cairo_line_to (c, x2, y);
+ y = y + d.bh;
+ }
+
+ x = x1;
+ y = y1;
+ y2 = y1 + d.mh; /* Set the line endpoint as the bottom of the month */
+ for (int i = 0; i < d.c + 1; i++) { /* Draw Vertical Lines */
+ cairo_move_to (c, x, y);
+ cairo_line_to (c, x, y2);
+ x = x + d.bw;
+ }
+ cairo_stroke(c);
+ return(0);
+}
+
+
+
+int draw_numbers(cairo_t *c, double x, double y, struct dimensions d, int wd, int nd) {
+ char str[3];
+ cairo_text_extents_t te;
+ cairo_font_extents_t fe;
+
+ cairo_select_font_face (c, "sans-serif",
+ CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
+ cairo_set_font_size (c, d.bw / 2);
+
+ int iterative = 0;
+ cairo_font_extents (c, &fe);
+ double yi = y;
+ double xi = x;
+ int current_weekday = wd;
+ x = d.m + (wd * d.bw);
+ for (int j = 0; j < d.r; j++) {
+ y = y + d.bh - ((d.bh - fe.ascent) / 2);
+ for (int k = current_weekday; k < d.c; k++) {
+ if (iterative < nd) {
+ sprintf(str, "%d", iterative + 1);
+ cairo_text_extents (c, str, &te);
+ double box_margins = (d.bw - te.width) / 2;
+
+ cairo_move_to(c, x + box_margins - te.x_bearing, y); /* center the number within the box */
+ cairo_show_text(c, str);
+ cairo_move_to(c, x, y); /* Sort of backtrack to realign with the spacing of the boxes */
+ x = x + d.bw;
+ iterative++;
+ current_weekday++;
+ }
+ }
+ current_weekday = 0;
+ y = y - (d.bh - ((d.bh - fe.ascent) / 2));
+ y = y + d.bh;
+ x = xi;
+ }
+ return(0);
+}
+
+
+
+int draw_month_name(cairo_t *c, char *name, struct dimensions d) {
+ double x, y = d.m;
+ cairo_set_font_size(c, 1.0);
+ cairo_text_extents_t te;
+ cairo_font_extents_t fe;
+ cairo_text_extents(c, name, &te);
+ x = (d.pw / 2) - (te.width / 2);
+ y = 1.5;
+ cairo_move_to(c, x, y);
+ cairo_show_text(c, &name[0]);
+ return(0);
+}
+
diff --git a/main.c b/main.c
index daa1e40..11f8de8 100644
--- a/main.c
+++ b/main.c
@@ -1,131 +1,74 @@
-#include <cairo/cairo.h>
#include <cairo/cairo-pdf.h>
-#include <stdio.h>
+#include <cairo/cairo.h>
#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <ctype.h>
+#include <unistd.h>
#include "include/color.h"
+#include "include/date.h"
+#include "include/draw.h"
-double us_letter_paper_width = 8.5;
-double us_letter_paper_height = 11;
+#define US_LETTER_PAPER_WIDTH 8.5;
+#define US_LETTER_PAPER_HEIGHT 11;
-double paper_width = 8.5 * 72; /* 612.0 */
-double paper_height = 11 * 72; /* 792.0 */
-int number_of_days = 30;
-int rows = 6;
-int columns = 7;
-double margin = 0.5;
-int draw_calendar(cairo_t *c, double x, double y) {
- double x1, y1, x2, y2;
- double month_width = us_letter_paper_width - (x * 2);
- double month_height = us_letter_paper_height - y - margin;
- double day_box_width = month_width / columns;
- double day_box_height = month_height / rows;
+int main (int argc, char *argv[]) {
+ int rows = 5;
+ int columns = 7;
+ double margin = 0.5;
+ int number_of_days = 28;
- x1 = x;
- y1 = y;
- x2 = us_letter_paper_width - x1; /* Set the line endpoint to right side of the page */
-
- for (int i = 0; i < rows + 1; i++) { /* Draw Horizontal Lines */
- cairo_move_to (c, x, y);
- cairo_line_to (c, x2, y);
- y = y + day_box_height;
- }
-
- x = x1;
- y = y1;
- y2 = y1 + month_height; /* Set the line endpoint as the bottom of the month */
- for (int i = 0; i < columns + 1; i++) { /* Draw Vertical Lines */
- cairo_move_to (c, x, y);
- cairo_line_to (c, x, y2);
- x = x + day_box_width;
- }
- cairo_stroke(c);
- return(0);
-}
+ double month_origin_x = margin;
+ double month_origin_y = 3.0;
-int draw_numbers(cairo_t *c, double x, double y, int wd) {
- double month_width = us_letter_paper_width - (x * 2);
- double month_height = us_letter_paper_height - y - margin;
- double day_box_width = month_width / columns;
- double day_box_height = month_height / rows;
+ double paper_width = US_LETTER_PAPER_WIDTH;
+ double paper_height = US_LETTER_PAPER_HEIGHT;
- char str[3];
- cairo_text_extents_t te;
- cairo_font_extents_t fe;
-
- cairo_set_source_rgba(c, 0.0, 0.0, 0.0, 0.6);
- cairo_select_font_face (c, "sans-serif",
- CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
- cairo_set_font_size (c, 0.5);
-
- int iterative = 0;
- cairo_font_extents (c, &fe);
- double yi = y;
- double xi = x;
- int current_weekday = wd;
- x = margin + (wd * day_box_width);
- for (int j = 0; j < rows; j++) {
- y = y + day_box_height - ((day_box_height - fe.ascent) / 2);
- for (int k = current_weekday; k < columns; k++) {
- if (iterative < number_of_days) {
- sprintf(str, "%d", iterative + 1);
- cairo_text_extents (c, str, &te);
- double box_margins = (day_box_width - te.width) / 2;
-
- cairo_move_to(c, x + box_margins - te.x_bearing, y); /* center the number within the box */
- cairo_show_text(c, str);
- cairo_move_to(c, x, y); /* Sort of backtrack to realign with the spacing of the boxes */
- x = x + day_box_width;
- iterative++;
- current_weekday++;
- }
- }
- current_weekday = 0;
- y = y - (day_box_height - ((day_box_height - fe.ascent) / 2));
- y = y + day_box_height;
- x = xi;
- }
- return(0);
-}
+ double month_width = paper_width - (month_origin_x * 2);
+ double month_height = paper_height - month_origin_y - margin;
-int draw_month_name(cairo_t *c, char *name) {
- double x, y = margin;
- double month_width = us_letter_paper_width - (x * 2);
- double month_height = us_letter_paper_height - y - margin;
double day_box_width = month_width / columns;
double day_box_height = month_height / rows;
- cairo_set_source_rgb(c, 0.0, 0.0, 0.0);
- cairo_set_font_size(c, 1.0);
- cairo_text_extents_t te;
- cairo_font_extents_t fe;
- cairo_text_extents(c, name, &te);
- x = (us_letter_paper_width / 2) - (te.width / 2);
- y = 1.5;
- cairo_move_to(c, x, y);
- cairo_show_text(c, &name[0]);
- return(0);
-}
-int main (int argc, char *argv[]) {
+ struct dimensions dim = {
+ paper_width, /* pw */
+ paper_height, /* ph */
+ month_width, /* mw */
+ month_height, /* mh */
+ day_box_width, /* bw */
+ day_box_height, /* bh */
+ rows, /* r */
+ columns, /* c */
+ margin, /* m */
+ };
+
char date_strings[number_of_days][2];
- char months[12][10] = {"January", "February", "March", "April", "May",
- "June", "July", "August", "September", "October", "November", "December"};
+ char months[12][10] = {
+ "January", "February", "March", "April", "May",
+ "June", "July", "August", "September", "October",
+ "November", "December"
+ };
+
+ int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- char weekdays_abbr[7][5] = {"Su", "Mo", "Tue", "Wed",
- "Thurs", "Fri", "Sat"};
+ char weekday_abbr[7][5] = {
+ "Su", "Mo", "Tue", "Wed",
+ "Thurs", "Fri", "Sat"
+ };
- cairo_surface_t * surface = cairo_pdf_surface_create("0.pdf", paper_width, paper_height);
+ cairo_surface_t * surface = cairo_pdf_surface_create("0.pdf", paper_width * 72, paper_height * 72);
cairo_t *cr = cairo_create(surface);
cairo_scale(cr, 72, 72);
cairo_set_line_width(cr, 0.03);
- cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.3);
- double month_origin_x = margin;
- double month_origin_y = 3.0;
+ set_color(cr, "BLACK");
- draw_calendar(cr, month_origin_x, month_origin_y);
- draw_numbers(cr, month_origin_x, month_origin_y, 6);
- draw_month_name(cr, months[10]);
+ draw_calendar(cr, month_origin_x, month_origin_y, dim);
+ set_color(cr, "GRAY");
+ draw_numbers(cr, month_origin_x, month_origin_y, dim, 6, number_of_days);
+ draw_month_name(cr, months[1], dim);
/* Clean Up */
cairo_destroy(cr);