aboutsummaryrefslogtreecommitdiff
path: root/src/calp.c
blob: d18498d4da0c04ce9926551ef88cbda30056fe47 (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
/* Standard Libraries */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>

/* Cairo Libraries */
#include <cairo/cairo-pdf.h>
#include <cairo/cairo.h>

/* calp Libraries */
#include "color.h"
#include "date.h"
#include "draw.h"

// int main (int argc, char *argv[]) {
int main (void) {
	// Dimension initialization
	int rows = 6;			// Number of weeks in a month 
	int columns = 7;		// Number of days in a week 
	int months_in_year = 12;
	double margin = 0.5; 		// 0.5" 
	//int number_of_days = 28;

	double month_origin_x = margin;
	double month_origin_y = 3.0;
	double paper_width = 8.5;
	double paper_height = 11;
	double month_width = paper_width - (month_origin_x * 2);
	double month_height = paper_height - month_origin_y - margin;
	double day_box_width = month_width / columns;
	double day_box_height = month_height / rows;

	struct dimensions dim = {
		paper_width, 	// pw
		paper_height,	// ph
		month_width,	// mw
		month_height, 	// mh
		day_box_width,	// bw
		day_box_height,	// bh
		rows,		// r 
		columns,	// c 
		margin,		// m 
	};

	// Date Setup
		char *months[] = {
		"January", "February", "March", "April", "May",
		"June", "July", "August", "September", "October",
		"November", "December"
	};

	// If year is a leap year, isleap(year) = 1, and will be added to February.
	int year = 2028;
	int days_in_month[12] = {31, 28 + isleap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
	
	// Because 1px = 1/72", paper width & paper height must be multiplied by 72
	cairo_surface_t * surface = cairo_pdf_surface_create("output.pdf", dim.pw * 72, dim.ph * 72);
	cairo_t *cr = cairo_create(surface);


	// Once paper dimensions have been initialized, they can be scaled back by the same factor they
	// were multiplied to make the dimensions are easier to work with.
	cairo_scale(cr, 72, 72);
	cairo_set_line_width (cr, 0.03);


	struct tm day;
	static const struct tm empty_day;
	
	
	// For some reason, this loop requires a zeroing of the day struct & running mktime() twice.
	// Or else, the January and December days will be offset by +1/-1. I assume this is a 
	// quirk of time.h.
	for (int i = 0; i < months_in_year; i++) {
		mktime(&day);
		day.tm_year = year - 1900;
		day.tm_mon = i;
		day.tm_mday = 1;

		// tm.hour, tm_min, & tm_sec are not used, but need to be zeroed. 
		day.tm_hour = 0;
		day.tm_min = 0;			
		day.tm_sec = 0;

		mktime(&day);

		draw_calendar(cr, month_origin_x, month_origin_y, dim);
		draw_numbers(cr, month_origin_x, month_origin_y, dim, (day.tm_wday), days_in_month[i]);
		draw_month_name(cr, months[i], dim);

		day = empty_day;
		
// Moves to the next page of the pdf
		cairo_show_page(cr); 
	}

	// Clean Up 
	cairo_destroy(cr);
	cairo_surface_destroy(surface);
	return(0);
}