diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rwxr-xr-x | calp | bin | 21992 -> 28432 bytes | |||
| -rw-r--r-- | src/calp.c | 69 | ||||
| -rw-r--r-- | src/date.c | 13 | ||||
| -rw-r--r-- | src/date.h | 1 | ||||
| -rw-r--r-- | src/date.o | bin | 0 -> 4464 bytes | |||
| -rw-r--r-- | src/draw.c | 139 | ||||
| -rw-r--r-- | src/draw.h | 26 |
8 files changed, 220 insertions, 30 deletions
@@ -2,7 +2,7 @@ VERSION = 0.0 PREFIX = /usr/local CC = cc DIR = src -OBJS = $(DIR)/calp.o $(DIR)/draw.o +OBJS = $(DIR)/calp.o $(DIR)/draw.o $(DIR)/date.o .PHONY: clean all install TARGET = calp Binary files differ@@ -1,43 +1,76 @@ #include <stdio.h> +#include <stdlib.h> #include <cairo/cairo-pdf.h> #include <pango/pangocairo.h> #include "draw.h" +#include "date.h" // Metric -#define US_LETTER_WIDTH 216 -#define US_LETTER_HEIGHT 279 - #define A4_WIDTH 210 #define A4_HEIGHT 297 -double width = A4_WIDTH; -double height = A4_HEIGHT; +int main (void) { + // Definitions + // ----------- + // Convert to inches, then 1 unit = 1/72th inch + double width = (A4_WIDTH / 25.4) * 72.0; + double height = (A4_HEIGHT / 25.4) * 72.0; + + //extern char *months[]; + int num_of_months = 12; + extern int days_in_month[12]; -int main (int argc, char **argv) { char *filename; - - // TBD clean this up - filename = argv[1]; + filename = "output.pdf"; + + // Create dimensions struct to hold measurements + struct dimensions dim; - if (argc == 1) { - filename = "output.pdf"; + + // Leap year adjustment: increment February day count by one + int year = 2026; + if (isleap(year)) { + days_in_month[1]++; } + + + + // Initialization - cairo_surface_t *surface = cairo_pdf_surface_create(filename, width, height); - cairo_t *cr = cairo_create(surface); + // -------------- + // Calculates measurements, stores them in dimensions struct. See "draw.h" + calculate_dimensions(width, height, &dim); - // Draw stuff here - fill_bg(cr, width, height); + cairo_surface_t *surface = cairo_pdf_surface_create(filename, dim.paper_width, dim.paper_height); + cairo_t *paper = cairo_create(surface); - // Draw text in upper left corner - draw_text(cr, 0.0, 0.0, "1"); + // Sub-surface for rendering content in day boxes + cairo_surface_t *day = + cairo_surface_create_similar(surface, CAIRO_CONTENT_COLOR_ALPHA, dim.day_width, dim.day_height); + cairo_t *cd = cairo_create(day); + + // Fill background with white color + cairo_set_source_rgb (paper, 255.0, 255.0, 255.0); + fill_bg(paper); + // Print dimensions, for debugging + //print_dimensions(&dim); + + // Draw all months + for (int i = 0; i < num_of_months; i++) { + draw_month(paper, cd, day, &dim, days_in_month[i]); + // Goes to next page in pdf + cairo_show_page(paper); + } + + // Clean up - cairo_destroy(cr); + // -------- + cairo_destroy(paper); cairo_surface_destroy(surface); return 0; } diff --git a/src/date.c b/src/date.c new file mode 100644 index 0000000..43250f1 --- /dev/null +++ b/src/date.c @@ -0,0 +1,13 @@ +char *months[] = { + "January", "February", "March", "April", "May", + "June", "July", "August", "September", "October", + "November", "December" +}; + +// Februrary is altered in calp.c if year is a leap year. defaults to 28. +int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +int isleap(int year) { + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); +} + diff --git a/src/date.h b/src/date.h new file mode 100644 index 0000000..ce297ed --- /dev/null +++ b/src/date.h @@ -0,0 +1 @@ +int isleap(int year); diff --git a/src/date.o b/src/date.o Binary files differnew file mode 100644 index 0000000..5fdd91f --- /dev/null +++ b/src/date.o @@ -1,32 +1,151 @@ #include <cairo/cairo.h> #include <pango/pangocairo.h> +#include <stdio.h> +#include <stdlib.h> -int fill_bg(cairo_t *c, double w, double h) { - cairo_set_source_rgb (c, 255.0, 255.0, 255.0); - cairo_rectangle(c, 0, 0, w, h); - cairo_fill(c); - return 0; +#define FONT "Monospace 10" + +// struct for storing dimensions used for calculations, movement, etc. +struct dimensions { + // Multiplication factors + int row_count; /* rows */ + int column_count; /* columns */ + + // All of these are in units chosen by width in "calp.c" + // Most likely milimeters (mm) + double paper_width; /* paper width */ + double paper_height; /* paper height */ + double month_width; /* month width */ + double month_height; /* month height */ + double month_top_corner_x; + double month_top_corner_y; + double day_width; /* day box width */ + double day_height; /* day box height */ + double margin; /* margin */ +}; + + + + +// Fill entire background with current color +int fill_bg(cairo_t *c) { + cairo_paint(c); + return 0; } + + +// Draw text at a certain point. +// 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) { -#define FONT "Sans Bold 27" PangoLayout *layout; PangoFontDescription *desc; // 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); - cairo_set_source_rgb (c, 0.0, 0.0, 0.0); pango_layout_set_text(layout, text, -1); - - //pango_cairo_update_layout (c, layout); + //printf("text: %s\n", text); cairo_move_to (c, x, y); pango_cairo_show_layout (c, layout); g_object_unref(layout); return 0; } + + + +// Calculate dimenions for one page and stores it into the dimensions struct +int calculate_dimensions(double pw, double ph, struct dimensions *d) { + // Percentage of page that should be margin + double margin = 0.1 * pw; + + // Everything other than margin + double month_width = 0.8 * pw; + + // width of day is 1/7th of total width + double box_width = month_width / 7.0; + + // Width = Height; Day Box is a square + double box_height = box_width; + + // Height is based on how tall # of weeks are + double month_height = box_height * 6.0; + + d->row_count = 6; + d->column_count = 7; + d->paper_width = pw; + d->paper_height = ph; + d->margin = margin; + d->month_width = month_width; + d->month_top_corner_x = margin; + d->month_top_corner_y = ph - margin - month_height; + d->month_height = month_height; + d->day_width = box_width; + d->day_height = box_height; + return 0; +} + + +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); + return 0; +} + + +// Draws the lines for a month +//int draw_month_grid (cairo_t *c, int month, int start_day) { +//int draw_month_grid (cairo_t *c, struct dimensions *d) { +// cairo_move_to (c, d->month_top_corner_x, d->month_top_corner_y); +// return 0; +//} + + + +// 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) { + double cursor_x = d->month_top_corner_x; + double cursor_y = d->month_top_corner_y; + + int increment = 0; + + char str[20]; + + for (int i = 0; i < d->row_count; i++) { + for (int k = 0; k < d->column_count; k++) { + if (increment < days_in_month) { + cairo_set_source_rgb (cd, 0.0, 0.0, 255.0); + cairo_paint(cd); + cairo_set_source_rgb (cd, 255.0, 255.0, 255.0); + snprintf(str, sizeof(str), "%d", increment + 1); + draw_text(cd, 4.0, 4.0, str); + + cairo_set_source_surface (c, s, cursor_x, cursor_y); + cursor_x = cursor_x + d->day_width; + cairo_paint(c); + increment++; + } else { + return 0; + } + } + + cursor_x = d->month_top_corner_x; + cursor_y = cursor_y + d->day_height; + } + cairo_stroke(cd); + return 0; +} @@ -1,3 +1,27 @@ -int fill_bg(cairo_t *c, double w, double h); +struct dimensions { + int row_count; /* rows */ + int column_count; /* columns */ + double paper_width; /* paper width */ + double paper_height; /* paper height */ + double month_width; /* month width */ + double month_height; /* month height */ + double month_top_corner_x; + double month_top_corner_y; + double day_width; /* day box width */ + double day_height; /* day box height */ + double margin; /* margin */ +}; + +int fill_bg(cairo_t *c); int draw_text(cairo_t *c, double x, double y, char *text); + +int calculate_dimensions(double pw, double ph, struct dimensions *d); + +int print_dimensions(struct dimensions *d); + +//int draw_month_grid (cairo_t *c, struct dimensions *d); + +int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *d, int days_in_month); + + |
