aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcalpbin28432 -> 29736 bytes
-rw-r--r--src/calp.c4
-rw-r--r--src/draw.c74
-rw-r--r--src/draw.h4
4 files changed, 58 insertions, 24 deletions
diff --git a/calp b/calp
index e68ea9f..c93e33a 100755
--- a/calp
+++ b/calp
Binary files differ
diff --git a/src/calp.c b/src/calp.c
index 9360551..d1a1b62 100644
--- a/src/calp.c
+++ b/src/calp.c
@@ -21,6 +21,7 @@ int main (void) {
int num_of_months = 12;
extern int days_in_month[12];
+ // TBD: Add ability to choose filename
char *filename;
filename = "output.pdf";
@@ -61,7 +62,8 @@ int main (void) {
// Draw all months
for (int i = 0; i < num_of_months; i++) {
- draw_month(paper, cd, day, &dim, days_in_month[i]);
+ draw_month(paper, cd, day, &dim, days_in_month[i], day_of_week(1, i + 1, year));
+ printf("%d\n", day_of_week(1, i + 1, year));
// Goes to next page in pdf
cairo_show_page(paper);
}
diff --git a/src/draw.c b/src/draw.c
index a79c8a8..931bf61 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -28,6 +28,7 @@ struct dimensions {
// Fill entire background with current color
+// Function name is changed for clarity
int fill_bg(cairo_t *c) {
cairo_paint(c);
return 0;
@@ -36,6 +37,7 @@ int fill_bg(cairo_t *c) {
// Draw text at a certain point.
+// Needn't be a string pointer, can be fixed array of chars
// 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) {
@@ -59,7 +61,7 @@ int draw_text (cairo_t *c, double x, double y, char *text) {
-// Calculate dimenions for one page and stores it into the dimensions struct
+// Calculate dimenions for one page and stores it in 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;
@@ -76,6 +78,7 @@ int calculate_dimensions(double pw, double ph, struct dimensions *d) {
// Height is based on how tall # of weeks are
double month_height = box_height * 6.0;
+ // Set items in struct to calculated values
d->row_count = 6;
d->column_count = 7;
d->paper_width = pw;
@@ -117,35 +120,64 @@ 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 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;
+ int current_day = start_day;
+
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;
+ for (int k = current_day; k < d->column_count; k++) {
+ // When the increment reached the end of the month, end
+ // the function.
+ if (increment >= days_in_month) return 0;
+
+ // Fill bg with color
+ cairo_set_source_rgb (cd, 0.0, 0.0, 255.0);
+ fill_bg(cd);
+
+ // Change text color
+ cairo_set_source_rgb (cd, 255.0, 255.0, 255.0);
+
+ // draw_text(), and by extension 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
+ draw_text(cd, 4.0, 4.0, str);
+
+
+ // Position sub-surface on main surface
+ cairo_set_source_surface (c, s, cursor_x, cursor_y);
+ cairo_paint(c);
+
+ // Move cursor one box over
+ cursor_x = cursor_x + d->day_width;
+ increment++;
+ }
+ current_day = 0;
+
+
+ cursor_x = d->month_top_corner_x;
+ cursor_y = cursor_y + d->day_height;
}
+
cairo_stroke(cd);
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);
+}
diff --git a/src/draw.h b/src/draw.h
index ed600c8..3e7c1b3 100644
--- a/src/draw.h
+++ b/src/draw.h
@@ -22,6 +22,6 @@ 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);
-
+int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *d, int days_in_month, int start_day);
+int day_of_week (int d, int m, int y);