aboutsummaryrefslogtreecommitdiff
path: root/src/calp.c
blob: 50cff02cffaad9015e9f0b90fbf60f310b4bec61 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//calp: printable calendar generator

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

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

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

int main (void) {
   // Definitions
   // -----------
   double paper_width = A4_WIDTH;
   double paper_height = A4_HEIGHT;

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


   

   int num_of_months = 12;
   extern int days_in_month[12];

   // Locale Setting
   // --------------

   char *week_names[7];
   char *months[num_of_months];

   nl_item locale_months[12] = 

   nl_item locale_days[7] = {ABDAY_1, ABDAY_2, ABDAY_3, ABDAY_4,
      ABDAY_5, ABDAY_6, ABDAY_7};

   void generate_locale_months(nl_item *months[12]) {
      locale_months = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6,
         MON_7, MON_8, MON_9, MON_10, MON_11, MON_12};

      setlocale(LC_ALL, "");

      for (int i = 0; i < 12; i++) {
         months[i] = nl_langinfo(locale_months[i]);
      }
   }

   setlocale(LC_ALL, "");

   for (int i = 0; i < 12; i++) {
      months[i] = nl_langinfo(locale_months[i]);
   }

   for (int i = 0; i < 7; i++) {
      week_names[i] = nl_langinfo(locale_days[i]);
      printf("%s\n", week_names[i]);
   }



   // Create dimensions struct to hold measurements
   struct dimensions dim;

   const int row_count = 6;
   const int column_count = 7;


   // Create dimensions struct to hold information about a
   // month
   struct month_info month = {
      .row_count = row_count,
      .column_count = column_count
   };



   // Leap year adjustment: increment February day count by one if
   // it is a leap year.
   const int year = 2026;

   if (isleap(year)) {
      days_in_month[1]++;
   }



   // 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(paper_width, paper_height, &dim);



   // Extent Declaration
   // ------------------
   
   // Height and width of the paper
   // .x and .y are empty
   struct extents paper_extents = {
      .width = paper_width, 
      .height = paper_height
   };

   const double margin = 0.05 * paper_extents.width;

   struct alignment page_layout = {
      .margin = margin
   };

   // Width and Height of the boxes are set
   // No position data is set until later.
   struct extents day_extents = {
   .width = paper_extents.width / 10.0,
   .height = paper_extents.width / 10.0
   };

   //struct extents week_name_extents = {
   //   .width = day_extents.width
   //};

   // Position and dimensions of the month "grid"
   struct extents month_extents = {
      .x = (paper_extents.width - (day_extents.width * column_count)) / 2.0,
      .y = paper_extents.height - margin - (day_extents.height * row_count),
      .width = day_extents.width * column_count,
      .height = day_extents.height * row_count,
   };




   // Position and width of the header
   // Height is set dynamically, based on the size of
   // the month grid
   struct extents header_extents = {
      .x = margin,
      .y = margin,
      .width = paper_extents.width - (margin * 2.0),
   };


   // Program fails if the PDF backend isn't available
   if (CAIRO_HAS_PDF_SURFACE == false) {
      printf("Error: PDF backend not available.\n");
      return 1;
   }

   // Main surface and context are created, with the size being
   // the paper
   cairo_surface_t *surface = 
      cairo_pdf_surface_create(
            filename,
            paper_extents.width,
            paper_extents.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,
            day_extents.width,
            day_extents.height
            );
   // cd = context day
   cairo_t *cd = cairo_create(day);



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




   // Construction
   // ------------
   
   // Month generation loop
   for (int i = 0; i < num_of_months; i++) {
      // Initialize month struct, setting values
      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);

      // Arbitrarily setting the distance between the month grid and the header
      double month_header_spacing = margin * 3.0;

      header_extents.height = paper_extents.height - month_header_spacing - ((month.minimum_rows + 1) * day_extents.height);


      // Create another surface and context for working with the 
      // header
      cairo_surface_t *header = cairo_surface_create_similar(
            surface,
            CAIRO_CONTENT_COLOR_ALPHA,
            header_extents.width,
            header_extents.height
            );

      cairo_t *ch = cairo_create(header);



      // Fill header with blue color for visibility
      set_color(ch, BLUE);
      fill_bg(ch);

      set_color(ch, BLACK);
      draw_month_title(ch, &header_extents, &page_layout, FONT_FAMILY, FONT_SIZE_LARGE, months[i]);
      

      // Draw the header
      cairo_set_source_surface(paper, header, header_extents.x, header_extents.y);
      cairo_paint(paper);

      draw_month_dates(paper, cd, day, &month, &month_extents, &day_extents);
      draw_month_grid(paper, &month, &month_extents, &day_extents);

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

      cairo_destroy(ch);
      cairo_surface_destroy(header);
   }


   

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

   cairo_destroy(cd);
   cairo_surface_destroy(day);

   return 0;
}