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