aboutsummaryrefslogtreecommitdiff
path: root/src/calp.c
blob: 3f81affa971e41b0c5be262806bc938b5158366f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//calp: printable calendar generator

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cairo/cairo-pdf.h>
#include <pango/pangocairo.h>

#include "draw.h"
#include "date.h"
#include "color.h"
#include "image.h"

// Metric
#define A4_WIDTH 210
#define A4_HEIGHT 297

#define US_LETTER_WIDTH 216
#define US_LETTER_HEIGHT 279

// Font Settings
#define FONT_FAMILY "Sans"
#define FONT_SIZE_NORMAL 10
#define FONT_SIZE_LARGE 40

int main (void) {
   // Definitions
   // -----------
   double width = A4_WIDTH;
   double height = A4_HEIGHT;

   // Convert to inches, then 1 unit = 1/72th inch
   width = (width / 25.4) * 72.0;
   height = (height / 25.4) * 72.0;

   extern char *months[];
   int num_of_months = 12;
   extern int days_in_month[12];


   // 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 if
   // it is a leap year.
   int year = 3043;
   if (isleap(year)) {
      days_in_month[1]++;
   }

   int verbose_output = 0;


   // TBD: Add ability to choose filename
   // Defaults to the year being generated
   char filename[20];
   snprintf(filename, sizeof(filename), "%d.pdf", year);


   // Initialization
   // --------------
   // Calculates measurements, stores them in dimensions struct. See "draw.h"
   calculate_dimensions(width, height, &dim);

   if (!CAIRO_HAS_PDF_SURFACE) {
      printf("Error: PDF backend not available.\n");
      return 1;
   }

   cairo_surface_t *surface = cairo_pdf_surface_create(filename, dim.paper_width, dim.paper_height);
   cairo_t *paper = cairo_create(surface);

   // 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);
   // cd = context day
   cairo_t *cd = cairo_create(day);



   // Fill background with white color
   set_color(paper, WHITE);
   fill_bg(paper);




   // Construction
   // ------------
   // Print dimensions, for debugging

   if (verbose_output) {
      print_dimensions(&dim);
   }


   // Range of months to be rendered (Default: Full calendar year, 12 months)
   int first_month = 0;
   int end_month = num_of_months;


   // 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];
      month.month_position = i;
      month.minimum_rows = calculate_minimum_rows(&month, &dim);

      dim.header_width = dim.paper_width - (dim.margin * 2.0);
      dim.header_height = dim.paper_height - (dim.margin * 3.0) - (month.minimum_rows * dim.day_height);

      cairo_surface_t *header = cairo_surface_create_similar(surface, CAIRO_CONTENT_COLOR_ALPHA, dim.header_width, dim.header_height);
      cairo_t *ch = cairo_create(header);

      //set_color(ch, BLUE);
      //fill_bg(ch);

      set_color(ch, BLACK);
      draw_month_title(ch, &dim, "Sans", 40, months[i]);

      char path[20];

      snprintf(path, 20, "assets/%d.png", i + 1);

      prepare_month_art(path, &dim);

      if (verbose_output) {
         printf("Drawing month \'%s\'\n", months[i]);
      }
      draw_month_art(paper, &dim, path);
      

      cairo_set_source_surface(paper, header, dim.margin, dim.margin);
      cairo_paint(paper);

      draw_month(paper, cd, day, &month, &dim);
      draw_month_grid(paper, &month, &dim);

      // Go to the next page in pdf
      cairo_show_page(paper); 

      cairo_destroy(ch);
      cairo_surface_destroy(header);
   }


   if (verbose_output) {
      printf("Finished generating \'%s\'.\n", filename);
   }
   

   // Clean up
   // --------
   cairo_destroy(paper);
   cairo_surface_destroy(surface);

   cairo_destroy(cd);
   cairo_surface_destroy(day);

   return 0;
}