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
|
#include <cairo/cairo.h>
#include <cairo/cairo-pdf.h>
#include <stdio.h>
#include <string.h>
#include "include/color.h"
double us_letter_paper_width = 8.5;
double us_letter_paper_height = 11;
double paper_width = 8.5 * 72; /* 612.0 */
double paper_height = 11 * 72; /* 792.0 */
int number_of_days = 30;
int rows = 6;
int columns = 7;
double margin = 0.5;
int draw_calendar(cairo_t *c, double x, double y) {
double x1, y1, x2, y2;
double month_width = us_letter_paper_width - (x * 2);
double month_height = us_letter_paper_height - y - margin;
double day_box_width = month_width / columns;
double day_box_height = month_height / rows;
x1 = x;
y1 = y;
x2 = us_letter_paper_width - x1; /* Set the line endpoint to right side of the page */
for (int i = 0; i < rows + 1; i++) { /* Draw Horizontal Lines */
cairo_move_to (c, x, y);
cairo_line_to (c, x2, y);
y = y + day_box_height;
}
x = x1;
y = y1;
y2 = y1 + month_height; /* Set the line endpoint as the bottom of the month */
for (int i = 0; i < columns + 1; i++) { /* Draw Vertical Lines */
cairo_move_to (c, x, y);
cairo_line_to (c, x, y2);
x = x + day_box_width;
}
cairo_stroke(c);
return(0);
}
int draw_numbers(cairo_t *c, double x, double y, int wd) {
double month_width = us_letter_paper_width - (x * 2);
double month_height = us_letter_paper_height - y - margin;
double day_box_width = month_width / columns;
double day_box_height = month_height / rows;
char str[3];
cairo_text_extents_t te;
cairo_font_extents_t fe;
cairo_set_source_rgba(c, 0.0, 0.0, 0.0, 0.6);
cairo_select_font_face (c, "sans-serif",
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (c, 0.5);
int iterative = 0;
cairo_font_extents (c, &fe);
double yi = y;
double xi = x;
int current_weekday = wd;
x = margin + (wd * day_box_width);
for (int j = 0; j < rows; j++) {
y = y + day_box_height - ((day_box_height - fe.ascent) / 2);
for (int k = current_weekday; k < columns; k++) {
if (iterative < number_of_days) {
sprintf(str, "%d", iterative + 1);
cairo_text_extents (c, str, &te);
double box_margins = (day_box_width - te.width) / 2;
cairo_move_to(c, x + box_margins - te.x_bearing, y); /* center the number within the box */
cairo_show_text(c, str);
cairo_move_to(c, x, y); /* Sort of backtrack to realign with the spacing of the boxes */
x = x + day_box_width;
iterative++;
current_weekday++;
}
}
current_weekday = 0;
y = y - (day_box_height - ((day_box_height - fe.ascent) / 2));
y = y + day_box_height;
x = xi;
}
return(0);
}
int draw_month_name(cairo_t *c, char *name) {
double x, y = margin;
double month_width = us_letter_paper_width - (x * 2);
double month_height = us_letter_paper_height - y - margin;
double day_box_width = month_width / columns;
double day_box_height = month_height / rows;
cairo_set_source_rgb(c, 0.0, 0.0, 0.0);
cairo_set_font_size(c, 1.0);
cairo_text_extents_t te;
cairo_font_extents_t fe;
cairo_text_extents(c, name, &te);
x = (us_letter_paper_width / 2) - (te.width / 2);
y = 1.5;
cairo_move_to(c, x, y);
cairo_show_text(c, &name[0]);
return(0);
}
int main (int argc, char *argv[]) {
char date_strings[number_of_days][2];
char months[12][10] = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
char weekdays_abbr[7][5] = {"Su", "Mo", "Tue", "Wed",
"Thurs", "Fri", "Sat"};
cairo_surface_t * surface = cairo_pdf_surface_create("0.pdf", paper_width, paper_height);
cairo_t *cr = cairo_create(surface);
cairo_scale(cr, 72, 72);
cairo_set_line_width(cr, 0.03);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.3);
double month_origin_x = margin;
double month_origin_y = 3.0;
draw_calendar(cr, month_origin_x, month_origin_y);
draw_numbers(cr, month_origin_x, month_origin_y, 6);
draw_month_name(cr, months[10]);
/* Clean Up */
cairo_destroy(cr);
cairo_surface_destroy(surface);
return(0);
}
|