aboutsummaryrefslogtreecommitdiff
path: root/src/draw.c
blob: a79c8a8596efa29d966f44e7b4d6d252b587f669 (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
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <stdio.h>
#include <stdlib.h>

#define FONT "Monospace 10"

// 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"
   // Most likely milimeters (mm)
   double paper_width; 	/* paper width */
   double paper_height;	/* paper height */
   double month_width;	/* month width */
   double month_height;	/* month height */
   double month_top_corner_x;
   double month_top_corner_y;
   double day_width;	/* day box width */
   double day_height;	/* day box height */
   double margin;	/* margin */
};




// Fill entire background with current color 
int fill_bg(cairo_t *c) {
   cairo_paint(c);
   return 0;
}



// Draw text at a certain point.
// Creates and destroys layout within the scope of the function
// Is that performant when looped over 365 operations?
int draw_text (cairo_t *c, double x, double y, char *text) {
   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_text(layout, text, -1);
   //printf("text: %s\n", text);

   cairo_move_to (c, x, y);
   pango_cairo_show_layout (c, layout);
   g_object_unref(layout);
   return 0;
}



// Calculate dimenions for one page and stores it into the dimensions struct
int calculate_dimensions(double pw, double ph, struct dimensions *d) {
   // Percentage of page that should be margin
   double margin = 0.1 * pw;

   // Everything other than margin
   double month_width = 0.8 * pw; 

   // width of day is 1/7th of total width
   double box_width = month_width / 7.0;

   // 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 * 6.0;

   d->row_count = 6;
   d->column_count = 7;
   d->paper_width = pw;
   d->paper_height = ph;
   d->margin = margin;
   d->month_width = month_width;
   d->month_top_corner_x = margin;
   d->month_top_corner_y = ph - margin - month_height;
   d->month_height = month_height;
   d->day_width = box_width;
   d->day_height = box_height;
   return 0;
}


int print_dimensions(struct dimensions *d) {
   printf("row count:    %d\n", d->row_count);
   printf("column count:   %d\n", d->column_count);
   printf("paper width:    %.2f\n", d->paper_width);
   printf("paper height:   %.2f\n", d->paper_height);
   printf("margin:         %.2f\n", d->margin);
   printf("month width:    %.2f\n", d->month_width);
   printf("month height:   %.2f\n", d->month_height);
   printf("month_top_corner_x: %f\n", d->month_top_corner_x);
   printf("month_top_corner_y: %f\n", d->month_top_corner_y);
   printf("day width:      %.2f\n", d->day_width);
   printf("day height:     %.2f\n", d->day_height);
   return 0;
}


// Draws the lines for a month
//int draw_month_grid (cairo_t *c, int month, int start_day) {
//int draw_month_grid (cairo_t *c, struct dimensions *d) {
//   cairo_move_to (c, d->month_top_corner_x, d->month_top_corner_y);
//   return 0;
//}



// Draws one month using daybox sub-surface & context, main context, and dimension struct
int draw_month (cairo_t *c, cairo_t *cd, cairo_surface_t *s, struct dimensions *d, int days_in_month) {
   double cursor_x = d->month_top_corner_x;
   double cursor_y = d->month_top_corner_y;

   int increment = 0;

   char str[20];

   for (int i = 0; i < d->row_count; i++) {
      for (int k = 0; k < d->column_count; k++) {
         if (increment < days_in_month) {
            cairo_set_source_rgb (cd, 0.0, 0.0, 255.0);
            cairo_paint(cd);
            cairo_set_source_rgb (cd, 255.0, 255.0, 255.0);
            snprintf(str, sizeof(str), "%d", increment + 1);
            draw_text(cd, 4.0, 4.0, str);

            cairo_set_source_surface (c, s, cursor_x, cursor_y);
            cursor_x = cursor_x + d->day_width;
            cairo_paint(c);
            increment++;
         } else {
            return 0;
         }
      }

      cursor_x = d->month_top_corner_x;
      cursor_y = cursor_y + d->day_height;
   }
   cairo_stroke(cd);
   return 0;
}