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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <stdio.h>
#include <stdlib.h>
#include "color.h"
/* 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". They should be
* measured in milimeters (mm) */
double paper_width; /* paper width */
double paper_height; /* paper height */
double month_width; /* month width */
double month_height; /* month height */
double month_x; /* Top left month x pos. */
double month_y; /* Top left month y pos. */
double day_width; /* day box width */
double day_height; /* day box height */
double header_width; /* header width */
double header_height; /* header height */
double margin; /* paper margin */
};
/* struct to hold Month-specific info, like what day of the week the first day
* lands on, how many days are in the month, etc. */
struct
month_info {
int month_position;
int first_day;
int num_of_days;
int column_count;
int row_count;
int minimum_rows;
};
/* struct for defining RGB colors */
struct
RGB {
double r;
double g;
double b;
};
/* Generic struct for storing where an object is and how it takes up space. */
/* TODO: Depricate dimensions struct in favor of atomic extents structs for
* each "object" (paper, month, header, etc.) */
struct extents
{
double x;
double y;
double width;
double height;
};
/* Alignment measurments about spacing. Measured in milimeters (mm). */
struct alignment
{
double margin;
double padding;
};
/* Convert Hex color strings to their RGB counterparts. Hex strings are easier
* to work with, so this is a nice helper function. returns a RGB struct value. */
struct
RGB hex_to_rgb(char *str)
{
unsigned int r, g, b;
struct RGB color;
sscanf(str, "%02x%02x%02x", &r, &g, &b);
/* Red, Green, and Blue values are from 0.0 - 1.0, so needs to be divided by
* 255.0. */
color.r = (r / 255.0);
color.g = (g / 255.0);
color.b = (b / 255.0);
return color;
}
/* Set a context's color using a HEX code. Color macros are defined in color.h. */
int
set_color(cairo_t *c, char *hex)
{
struct RGB color = hex_to_rgb(hex);
cairo_set_source_rgb (c, color.r, color.g, color.b);
return 0;
}
/* Get the day of the week for a given day. Used for calculating when the start
* of a month is relative to "Su Mo Tu ... 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);
}
/* Fill entire background with current color. Function name is changed for
* clarity. */
int
fill_bg(cairo_t *c) {
cairo_paint(c);
return 0;
}
/* Draw a string of text on the screen with specific font size, position, and
* font family. Needn't be a string pointer, can be fixed array of chars.
* Creates and destroys layout within the scope of the function
* TODO: Is this performant when looped over 365 operations? */
int
draw_text (cairo_t *c, double x, double y,
char *font_family, int font_size, char *text) {
char font[100];
/* Concat font string for pango_font_description_from_string */
snprintf(font, sizeof(font), "%s %d", font_family, font_size);
/* Initialize Pango Stuff */
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);
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
pango_layout_set_text(layout, text, -1);
cairo_move_to (c, x, y);
pango_cairo_show_layout (c, layout);
g_object_unref(layout);
return 0;
}
/* Get the dimensions of a string of text. Returns a PangoRectangle struct. */
PangoRectangle
get_logical_extents (cairo_t *c, char *font_family, int font_size, char *text) {
char font[100];
/* Concat font string for pango_font_description_from_string */
snprintf(font, sizeof(font), "%s %d", font_family, font_size);
PangoLayout *layout;
PangoFontDescription *desc;
PangoRectangle rect;
/* 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);
pango_layout_set_text(layout, text, -1);
pango_layout_get_pixel_extents(layout, &rect, NULL);
//pango_layout_get_extents(layout, NULL, &rect);
g_object_unref(layout);
return(rect);
}
/* Get the VISUAL dimensions of a string of text. Returns a PangoRectangle
* struct. */
PangoRectangle
get_ink_extents (cairo_t *c, char *font_family, int font_size, char *text) {
char font[100];
/* Concat font string for pango_font_description_from_string */
snprintf(font, sizeof(font), "%s %d", font_family, font_size);
PangoLayout *layout;
PangoFontDescription *desc;
PangoRectangle rect;
/* 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);
pango_layout_set_text(layout, text, -1);
pango_layout_get_pixel_extents(layout, &rect, NULL);
//pango_layout_get_extents(layout, NULL, &rect);
g_object_unref(layout);
return(rect);
}
/* Calculate dimenions for one page and store it in the dimensions struct. Used
* for determining where elements should be drawn on the page. */
int
calculate_dimensions(double pw, double ph, struct dimensions *d) {
d->row_count = 6;
d->column_count = 7;
/* Percentage of page that should be margin */
double margin = 0.05 * pw;
/* width of day is 1/7th of total width */
double box_width = pw / 10.0;
/* Everything other than margin */
double month_width = box_width * d->column_count;
/* 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 * d->row_count;
/* Set items in struct to calculated values */
d->paper_width = pw;
d->paper_height = ph;
d->margin = margin;
d->month_width = month_width;
d->month_x = (pw - (month_width)) / 2.0;
d->month_y = ph - margin - month_height;
d->month_height = month_height;
d->day_width = box_width;
d->day_height = box_height;
return 0;
}
// Calculate minimum amount of rows a month can fit in.
// Most months can fit within 4 or 5 rows instead of 6.
// TBD: This can probably done in a more efficient way
int
calculate_minimum_rows(struct month_info *m) {
int extra_rows = 0;
int days_in_first_week = (7 - m->first_day);
if (m->first_day == 0) {
days_in_first_week = m->first_day;
}
int days_in_last_week = (m->num_of_days - days_in_first_week) % 7;
int middle_weeks = (m->num_of_days - days_in_first_week - days_in_last_week) / 7;
if (days_in_first_week != 0) {
extra_rows++;
}
if (days_in_last_week != 0) {
extra_rows++;
}
int minimum_rows = middle_weeks + extra_rows;
return minimum_rows;
}
/* Draws the grid lines for a month */
int
draw_month_grid (cairo_t *c, struct month_info *m, struct extents *me, struct extents *de) {
double cursor_x = me->x;
// double cursor_x = month_grid_pos->x;
// make d->row_count reside in month_info *m
int row_offset = m->row_count - calculate_minimum_rows(m);
// double cursor_y = month_grid_pos->y;
double cursor_y = me->y + (row_offset * de->height);
double x = cursor_x;
double y = cursor_y;
set_color(c, BLACK);
cairo_set_line_width(c, 1.0);
/* Vertical lines */
for (int i = 0; i <= m->column_count; i++) {
cairo_move_to(c, cursor_x, cursor_y);
cairo_line_to(c, cursor_x, cursor_y + (me->height - (row_offset * de->height)));
cursor_x = cursor_x + de->width;
}
/* Recenter the cursor to the top left */
cursor_x = x;
cursor_y = y;
/* Horizontal lines */
for (int i = 0; i <= m->row_count; i++) {
cairo_move_to(c, cursor_x, cursor_y);
cairo_line_to(c, cursor_x + me->width, cursor_y);
cursor_y = cursor_y + de->height;
}
cairo_stroke(c);
return 0;
}
int
construct_day_box(cairo_t *cd, int month, int day, int week_day) {
char str[20];
double padding = 3.0;
set_color(cd, WHITE);
fill_bg(cd);
if ((month == 0) && (day == 1) && (week_day == 1)) {
}
/* draw_text(), and therefore pango_layout_set_text(), needs a string as
* input, so the current day's date is converted. */
snprintf(str, sizeof(str), "%d", day);
set_color(cd, BLACK);
draw_text(cd, padding, padding, "Monospace", 15, str);
return 0;
}
/* Draws one month using daybox sub-surface & context, main context, and
* dimension struct. */
int
draw_month_dates(cairo_t *c, cairo_t *cd, cairo_surface_t *s,
struct month_info *m, struct extents *me, struct extents *de) {
double cursor_x = me->x;
if (m->first_day != 0) cursor_x = cursor_x + (de->width * m->first_day);
/* Push bottom of month as far down the page as the margin allows. */
int row_offset = m->row_count - calculate_minimum_rows(m);
double cursor_y = me->y + (row_offset * de->height);
int current_day = m->first_day;
int increment = 0;
for (int i = 0; i < m->row_count; i++) {
for (int k = current_day; k < m->column_count; k++) {
/* When the increment reached the end of the month, end the function. */
if (increment >= m->num_of_days) return 0;
construct_day_box(cd, m->month_position, increment + 1, k);
/* Position the constructed day box 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 + de->width;
increment++;
}
current_day = 0;
cursor_x = me->x;
cursor_y = cursor_y + de->height;
}
cairo_stroke(cd);
return 0;
}
/* Draws the month name at the top of the month image */
int
draw_month_title(cairo_t *c, struct extents *he, struct alignment *pl,
char *font_family, int font_size, char *name) {
PangoRectangle rect;
rect = get_logical_extents(c, font_family, font_size, name);
double cursor_x = ((he->width - rect.width) / 2.0);
double cursor_y = he->height - pl->margin - rect.height;
draw_text(c, cursor_x, cursor_y, font_family, font_size, name);
return 0;
}
/* Draw the shorthand names for the days of the week on top of the month */
int
draw_week_names(cairo_t *c, struct extents *me) {
char *names[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *font_family = "Sans";
int font_size = 10;
for (int i = 0; i < 7; i++) {
draw_text(c, me->x, me->y, font_family, font_size, names[i]);
}
return 0;
}
|