]> git.cworth.org Git - acre/blob - acre.c
Add code to cleanup PangoLayout objects when done with them
[acre] / acre.c
1 /* acre - A cairo-based library for creating plots and charts.
2  *
3  * Copyright © 2009 Carl Worth <cworth@cworth.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18  */
19
20 #define _ISOC99_SOURCE /* for round() */
21 #define _XOPEN_SOURCE 500
22 #define _GNU_SOURCE /* for asprintf() */
23
24 #include "acre.h"
25 #include "xmalloc.h"
26
27 #include <string.h>
28 #include <math.h>
29
30 typedef struct _acre_data_point_2d {
31     double x;
32     double y;
33 } acre_data_point_2d_t;
34
35 struct _acre_data {
36     char *name;
37
38     acre_data_point_2d_t *points;
39     unsigned int points_size;
40     unsigned int num_points;
41 };
42
43 typedef struct _acre_axis {
44     char *label;
45     double min;
46     double max;
47 } acre_axis_t;
48
49 struct _acre {
50     char *title;
51     acre_axis_t x_axis;
52     acre_axis_t y_axis;
53
54     acre_data_t **data;
55     unsigned int data_size;
56     unsigned int num_data;
57
58     /* Data for drawing. */
59     cairo_t *cr;
60     PangoFontDescription *font;
61
62     /* Total size including labels. */
63     int width;
64     int height;
65
66     /* Position and size of chart alone. */
67     PangoRectangle chart;
68 };
69
70 /* Create a new, empty plot. */
71 acre_t *
72 acre_create (void)
73 {
74     acre_t *acre;
75
76     acre = xmalloc (sizeof (acre_t));
77
78     acre->title = NULL;
79
80     acre->x_axis.label = NULL;
81     acre->x_axis.min = 0.0;
82     acre->x_axis.max = 0.0;
83
84     acre->y_axis.label = NULL;
85     acre->y_axis.min = 0.0;
86     acre->y_axis.max = 0.0;
87
88     acre->data = NULL;
89     acre->data_size = 0;
90     acre->num_data = 0;
91
92     return acre;
93 }
94
95 /* Destroy a plot. */
96 void
97 acre_destroy (acre_t *acre)
98 {
99     unsigned int i;
100
101     free (acre->title);
102     free (acre->x_axis.label);
103     free (acre->y_axis.label);
104
105     for (i = 0; i < acre->num_data; i++)
106         acre_data_destroy (acre->data[i]);
107
108     free (acre->data);
109
110     free (acre);
111 }
112
113 void
114 acre_set_title (acre_t *acre, const char *title)
115 {
116     free (acre->title);
117
118     acre->title = strdup (title);
119 }
120
121 void
122 acre_set_x_axis_label (acre_t *acre, const char *label)
123 {
124     free (acre->x_axis.label);
125
126     acre->x_axis.label = strdup (label);
127 }
128
129 void
130 acre_set_y_axis_label (acre_t *acre, const char *label)
131 {
132     free (acre->y_axis.label);
133
134     acre->y_axis.label = strdup (label);
135 }
136
137 /* Add a dataset to the plot. The plot assumes ownership of the
138  * dataset so it is not necessary to call acre_data_destroy on it. */
139 void
140 acre_add_data (acre_t *acre, acre_data_t *data)
141 {
142     if (acre->num_data >= acre->data_size) {
143         acre->data_size *= 2;
144         if (acre->data_size == 0)
145             acre->data_size = 1;
146         acre->data = xrealloc_ab (acre->data,
147                                   acre->data_size,
148                                   sizeof (acre_data_t *));
149     }
150
151     acre->data[acre->num_data] = data;
152     acre->num_data++;
153 }
154
155 #define ACRE_FONT_FAMILY "sans"
156 #define ACRE_FONT_SIZE 12
157 #define ACRE_TITLE_FONT_SIZE 32
158 #define ACRE_PAD (ACRE_FONT_SIZE)
159 #define ACRE_TICK_SIZE 6
160
161 static PangoLayout *
162 _create_layout (acre_t *acre, const char *text)
163 {
164     PangoLayout *layout;
165
166     layout = pango_cairo_create_layout (acre->cr);
167     pango_layout_set_font_description (layout, acre->font);
168     pango_layout_set_text (layout, text, -1);
169     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
170
171     return layout;
172 }
173
174 static void
175 _destroy_layout (PangoLayout *layout)
176 {
177     g_object_unref (layout);
178 }
179
180 static void
181 _show_layout (cairo_t *cr, PangoLayout *layout)
182 {
183     pango_cairo_show_layout (cr, layout);
184
185     _destroy_layout (layout);
186 }
187
188 static void
189 _draw_title_and_labels (acre_t *acre)
190 {
191     cairo_t *cr = acre->cr;
192     PangoFontDescription *title_font;
193     PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
194     int title_width, title_height;
195     int x_axis_width, x_axis_height;
196     int y_axis_width, y_axis_height;
197     PangoRectangle new_chart;
198
199     cairo_save (cr);
200
201     acre->font = pango_font_description_new ();
202     pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
203     pango_font_description_set_absolute_size (acre->font,
204                                               ACRE_FONT_SIZE * PANGO_SCALE);
205
206     title_font = pango_font_description_new ();
207     pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
208     pango_font_description_set_absolute_size (title_font,
209                                               ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
210
211     title_layout = _create_layout (acre, acre->title);
212     pango_layout_set_font_description (title_layout, title_font);
213
214     x_axis_layout = _create_layout (acre, acre->x_axis.label);
215     y_axis_layout = _create_layout (acre, acre->y_axis.label);
216
217     /* Iterate with the layout of the title and axis labels until they
218      * are stable, (this requires iteration since we don't know what
219      * to set their widths to in advance due to the wrapping of the
220      * other elements). */
221     acre->chart.x = 0;
222     acre->chart.y = 0;
223     acre->chart.width = acre->width;
224     acre->chart.height = acre->height;
225     while (1) {
226         pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
227         pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
228         pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
229
230         pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
231         pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
232         pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
233
234         new_chart.x = ACRE_PAD + y_axis_height +
235             ACRE_PAD + ACRE_FONT_SIZE;
236         new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
237
238         new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
239         new_chart.height = acre->height - acre->chart.y - (ACRE_FONT_SIZE + ACRE_PAD + x_axis_height + ACRE_PAD);
240
241         if (new_chart.x == acre->chart.x &&
242             new_chart.y == acre->chart.y &&
243             new_chart.width == acre->chart.width &&
244             new_chart.height == acre->chart.height)
245         {
246             break;
247         }
248
249         acre->chart.x = new_chart.x;
250         acre->chart.y = new_chart.y;
251         acre->chart.width = new_chart.width;
252         acre->chart.height = new_chart.height;
253     }
254
255     cairo_set_source_rgb (cr, 0, 0, 0);
256
257     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
258     _show_layout (cr, title_layout);
259
260     cairo_save (cr);
261     {
262         cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
263         cairo_rotate (cr, - M_PI / 2.0);
264         cairo_move_to (cr, 0, 0);
265         _show_layout (cr, y_axis_layout);
266     }
267     cairo_restore (cr);
268
269     cairo_move_to (cr, acre->chart.x,
270                    acre->chart.y + acre->chart.height +
271                    ACRE_FONT_SIZE + ACRE_PAD);
272     _show_layout (cr, x_axis_layout);
273
274     cairo_restore (cr);
275 }
276
277 /* For a given axis range, compute a step size (in data space) to
278  * generate a suitable number of ticks (5 or so). */
279 static double
280 _step_for_range (double range)
281 {
282     double step, scale_factor;
283
284     /* We want roughly 5 major ticks for the chart. */
285     step = range / 5;
286
287     /* Normalize the step so we can easily snap it to a desirable
288      * value. */
289     scale_factor = pow (10.0, floor (log10 (step)));
290     step /= scale_factor;
291
292     /* We want increments of 1, 2.5, 5, or 10 (times some power of
293      * 10). The threshold values between these are computed
294      * logarithmically. */
295     if (step < 3.535533905932738) {
296         if (step < 1.58113883008419)
297             step = 1.0;
298         else
299             step = 2.5;
300     } else {
301         if (step < 7.071067811865475)
302             step = 5.0;
303         else
304             step = 10.0;
305     }
306
307     /* Un-normalize and we now have the data value that we want to
308      * step at. */
309     return step * scale_factor;
310 }
311
312 /* Given an axis range, we can compute a desired data-space step
313  * amount for the major ticks (see _step_for_range). To get
314  * nice-looking pixel-snapped ticks we want to expand the range
315  * slightly. */
316 static void
317 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_size)
318 {
319     double range, new_range, step, pixel_step;
320
321     range = *axis_max - *axis_min;
322
323     step = _step_for_range (range);
324     pixel_step = step * pixel_size / range;
325
326     /* We expand the range by the ratio of the pixel step to the floor
327      * of the pixel_step.
328      */
329     new_range = range * pixel_step / floor (pixel_step);
330
331     /* And spread the increase out on either side of the range. */
332     *axis_min -= (new_range - range) / 2.0;
333     *axis_max += (new_range - range) / 2.0;
334 }
335
336 /* Setup a transformation in acre->cr such that data values plotted
337  * will appear where they should within the chart.
338  */
339 static void
340 _set_transform_to_data_space (acre_t *acre)
341 {
342     cairo_t *cr = acre->cr;
343
344     cairo_translate (cr,
345                      acre->chart.x,
346                      acre->chart.y + acre->chart.height);
347     cairo_scale (cr,
348                  acre->chart.width / (acre->x_axis.max - acre->x_axis.min),
349                  - acre->chart.height /(acre->y_axis.max - acre->y_axis.min));
350     cairo_translate (cr, -acre->x_axis.min, -acre->y_axis.min);
351 }
352
353 static void
354 _compute_axis_ranges (acre_t *acre)
355 {
356     unsigned int d, i;
357     acre_data_t *data;
358     double x_adjust, y_adjust;
359     cairo_t *cr = acre->cr;
360
361     /* First, simply find the extrema of the data. */
362     for (d = 0; d < acre->num_data; d++) {
363         data = acre->data[d];
364         for (i = 0; i < data->num_points; i++) {
365             if (data->points[i].x < acre->x_axis.min)
366                 acre->x_axis.min = data->points[i].x;
367             if (data->points[i].x > acre->x_axis.max)
368                 acre->x_axis.max = data->points[i].x;
369
370             if (data->points[i].y < acre->y_axis.min)
371                 acre->y_axis.min = data->points[i].y;
372             if (data->points[i].y > acre->y_axis.max)
373                 acre->y_axis.max = data->points[i].y;
374         }
375     }
376
377     /* Next, increase the axis ranges just enough so that the step
378      * sizes for the ticks will be integers.
379      */
380     _expand_range_for_width (&acre->x_axis.min,
381                              &acre->x_axis.max,
382                              acre->chart.width);
383
384     _expand_range_for_width (&acre->y_axis.min,
385                              &acre->y_axis.max,
386                              acre->chart.height);
387
388     /* Finally, we also translate the axis ranges slightly so that the
389      * ticks land on half-integer device-pixel positions.
390      */
391     cairo_save (cr);
392     {
393         _set_transform_to_data_space (acre);
394
395         x_adjust = 0.0;
396         y_adjust = 0.0;
397         cairo_user_to_device (cr, &x_adjust, &y_adjust);
398         x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
399         y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
400         cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
401
402         acre->x_axis.min -= x_adjust;
403         acre->x_axis.max -= x_adjust;
404
405         acre->y_axis.min -= y_adjust;
406         acre->y_axis.max -= y_adjust;
407     }
408     cairo_restore (cr);
409 }
410
411 static void
412 _draw_data (acre_t *acre)
413 {
414     cairo_t *cr = acre->cr;
415     unsigned int d, i;
416     acre_data_t *data;
417
418     cairo_save (cr);
419
420     cairo_set_source_rgb (cr, 0, 0, 0);
421
422     _set_transform_to_data_space (acre);
423
424     for (d = 0; d < acre->num_data; d++) {
425         data = acre->data[d];
426         cairo_new_path (cr);
427         for (i = 0; i < data->num_points; i++) {
428             cairo_line_to (cr,
429                            data->points[i].x,
430                            data->points[i].y);
431         }
432         cairo_save (cr);
433         {
434             cairo_identity_matrix (cr);
435             cairo_set_line_width (cr, 1.0);
436             cairo_stroke (cr);
437         }
438         cairo_restore (cr);
439     }
440
441     cairo_restore (cr);
442 }
443
444 static void
445 _draw_frame_and_ticks (acre_t *acre)
446 {
447     cairo_t *cr = acre->cr;
448     double step, x, y;
449
450     cairo_save (cr);
451
452     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
453
454     /* First the ticks within data space. */
455     cairo_save (cr);
456     {
457         _set_transform_to_data_space (acre);
458
459         step = _step_for_range (acre->x_axis.max -acre->x_axis.min);
460         x = (floor (acre->x_axis.min / step) + 1) * step;
461         while (x <= acre->x_axis.max) {
462             cairo_move_to (cr, x, acre->y_axis.min);
463             cairo_save (cr);
464             {
465                 /* tick */
466                 cairo_save (cr);
467                 {
468                     cairo_identity_matrix (cr);
469                     cairo_rel_line_to (cr, 0, 0.5);
470                     cairo_rel_line_to (cr, 0, -ACRE_TICK_SIZE-0.5);
471                     cairo_set_line_width (cr, 1.0);
472                     cairo_stroke (cr);
473                 }
474                 cairo_restore (cr);
475
476                 /* label */
477                 cairo_save (cr);
478                 {
479                     PangoLayout *layout;
480                     char *label;
481                     int width, height;
482                     asprintf (&label, "%g", x);
483                     layout = _create_layout (acre, label);
484                     free (label);
485                     cairo_move_to (cr, x, acre->y_axis.min);
486                     cairo_identity_matrix (cr);
487                     pango_layout_get_pixel_size (layout, &width, &height);
488                     cairo_rel_move_to (cr, -width / 2, 2);
489                     _show_layout (cr, layout);
490                 }
491                 cairo_restore (cr);
492             }
493             cairo_restore (cr);
494             x += step;
495         }
496
497         step = _step_for_range (acre->y_axis.max -acre->y_axis.min);
498         y = (floor (acre->y_axis.min / step) + 1) * step;
499         while (y <= acre->y_axis.max) {
500             cairo_move_to (cr, acre->x_axis.min, y);
501             cairo_save (cr);
502             {
503                 /* tick */
504                 cairo_save (cr);
505                 {
506                     cairo_identity_matrix (cr);
507                     cairo_rel_line_to (cr, -0.5, 0);
508                     cairo_rel_line_to (cr, ACRE_TICK_SIZE+0.5, 0);
509                     cairo_set_line_width (cr, 1.0);
510                     cairo_stroke (cr);
511                 }
512                 cairo_restore (cr);
513
514                 /* label */
515                 cairo_save (cr);
516                 {
517                     PangoLayout *layout;
518                     char *label;
519                     int width, height;
520                     asprintf (&label, "%g", y);
521                     layout = _create_layout (acre, label);
522                     free (label);
523                     cairo_move_to (cr, acre->x_axis.min, y);
524                     cairo_identity_matrix (cr);
525                     pango_layout_get_pixel_size (layout, &width, &height);
526                     cairo_rel_move_to (cr, -width-2, -height/2);
527                     _show_layout (cr, layout);
528                 }
529                 cairo_restore (cr);
530             }
531             cairo_restore (cr);
532             y += step;
533         }
534     }
535     cairo_restore (cr);
536
537     /* Then the frame drawn in pixel space. */
538     cairo_rectangle (cr,
539                      acre->chart.x - 0.5, acre->chart.y - 0.5,
540                      acre->chart.width + 1.0, acre->chart.height + 1.0);
541     cairo_set_line_width (cr, 1.0);
542     cairo_stroke (cr);
543
544     cairo_restore (cr);
545 }
546
547 /* Draw the plot to the given cairo context within a user-space
548  * rectangle from (0, 0) to (width, height). This size includes all
549  * space for extra-plot elements (such as the title, the axis labels,
550  * etc.)
551  */
552 void
553 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
554 {
555     acre->cr = cr;
556     acre->width = width;
557     acre->height = height;
558
559     cairo_save (cr);
560
561     cairo_set_source_rgb (cr, 1, 1, 1);
562
563     cairo_paint (cr);
564
565     _draw_title_and_labels (acre);
566
567     _compute_axis_ranges (acre);
568
569     _draw_data (acre);
570
571     _draw_frame_and_ticks (acre);
572 }
573
574 /* Create a new dataset---a collection of (x, y) datapoints. A single
575  * plot can contain multiple datasets, (see acre_add_data). */
576 acre_data_t *
577 acre_data_create (void)
578 {
579     acre_data_t *data;
580
581     data = xmalloc (sizeof (acre_data_t));
582
583     data->name = NULL;
584
585     data->points = NULL;
586     data->points_size = 0;
587     data->num_points = 0;
588
589     return data;
590 }
591
592 /* Destroy an acre dataset. Do not call this function if the dataset
593  * has been added to an acre_t plot with acre_add_data. */
594 void
595 acre_data_destroy (acre_data_t *data)
596 {
597     free (data->points);
598
599     free (data);
600 }
601
602 /* Set the label for this dataset (to appear in the plot's key). */
603 void
604 acre_data_set_name (acre_data_t *data, const char *name)
605 {
606     free (data->name);
607
608     data->name = strdup (name);
609 }
610
611 /* Add a datapoint to the given dataset. */
612 void
613 acre_data_add_point_2d (acre_data_t *data, double x, double y)
614 {
615     if (data->num_points >= data->points_size) {
616         data->points_size *= 2;
617         if (data->points_size == 0)
618             data->points_size = 16;
619         data->points = xrealloc_ab (data->points,
620                                     data->points_size,
621                                     sizeof (acre_data_point_2d_t));
622     }
623
624     data->points[data->num_points].x = x;
625     data->points[data->num_points].y = y;
626     data->num_points++;
627 }