]> git.cworth.org Git - acre/blob - acre.c
Force an identity matrix when creating a pango layout
[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 <stdbool.h>
30 #include <math.h>
31
32 #include <lcms.h>
33
34 typedef struct _acre_data_point_2d {
35     double x;
36     double y;
37 } acre_data_point_2d_t;
38
39 struct _acre_data {
40     /* The name of this data set. */
41     char *name;
42
43     /* The style for rendering (line, bar, etc.) */
44     acre_style_t style;
45
46     /* Minimum and mximum extents of data. */
47     acre_data_point_2d_t min;
48     acre_data_point_2d_t max;
49
50     /* The data itself. */
51     acre_data_point_2d_t *points;
52     unsigned int points_size;
53     unsigned int num_points;
54
55     /* The names of data points (if any).
56      *
57      * This array is indexed with the same index as the 'points' array
58      * to provide names for points. It is legal for this array to be
59      * NULL or smaller than 'points', (in which case, the
60      * corresponding points simply have no names).
61      */
62     char **names;
63     unsigned int names_size;
64     unsigned int num_names;
65 };
66
67 typedef struct _acre_axis {
68     char *label;
69
70     /* Range of data */
71     double data_min;
72     double data_max;
73
74     /* Range of data to be viewed. */
75     double view_min;
76     double view_max;
77
78     /* Has the view range been set? */
79     bool view_range_set;
80 } acre_axis_t;
81
82 typedef struct _acre_color {
83     double red;
84     double green;
85     double blue;
86 } acre_color_t;
87
88 struct _acre {
89     char *title;
90     acre_axis_t x_axis;
91     acre_axis_t y_axis;
92
93     acre_data_t **data;
94     unsigned int data_size;
95     unsigned int num_data;
96
97     /* Data for drawing. */
98     cairo_t *cr;
99     PangoFontDescription *font;
100     acre_color_t *colors;
101     int colors_size;
102     int num_colors;
103
104     /* Total size including labels. */
105     int width;
106     int height;
107
108     /* Position and size of chart alone. */
109     PangoRectangle chart;
110 };
111
112 static void
113 _find_x_range_given_y_range (acre_t *acre,
114                              double *x_min, double *x_max,
115                              double y_min, double y_max);
116
117 static void
118 _find_y_range_given_x_range (acre_t *acre,
119                              double *y_min, double *y_max,
120                              double x_min, double x_max);
121
122 /* Create a new, empty plot. */
123 acre_t *
124 acre_create (void)
125 {
126     acre_t *acre;
127
128     acre = xmalloc (sizeof (acre_t));
129
130     acre->title = NULL;
131
132     acre->x_axis.label = NULL;
133     acre->x_axis.data_min = 0.0;
134     acre->x_axis.data_max = 0.0;
135     acre->x_axis.view_min = 0.0;
136     acre->x_axis.view_max = 0.0;
137     acre->x_axis.view_range_set = false;
138
139     acre->y_axis.label = NULL;
140     acre->y_axis.data_min = 0.0;
141     acre->y_axis.data_max = 0.0;
142     acre->y_axis.view_min = 0.0;
143     acre->y_axis.view_max = 0.0;
144     acre->y_axis.view_range_set = false;
145
146     acre->data = NULL;
147     acre->data_size = 0;
148     acre->num_data = 0;
149
150     acre->cr = NULL;
151     acre->font = NULL;
152     acre->colors = NULL;
153     acre->num_colors = 0;
154     acre->colors_size = 0;
155
156     acre->width = 0;
157     acre->height = 0;
158
159     acre->chart.x = 0;
160     acre->chart.y = 0;
161     acre->chart.width = 0;
162     acre->chart.height = 0;
163
164     return acre;
165 }
166
167 /* Destroy a plot. */
168 void
169 acre_destroy (acre_t *acre)
170 {
171     unsigned int i;
172
173     free (acre->title);
174     free (acre->x_axis.label);
175     free (acre->y_axis.label);
176
177     for (i = 0; i < acre->num_data; i++)
178         acre_data_destroy (acre->data[i]);
179
180     free (acre->data);
181
182     free (acre->colors);
183
184     free (acre);
185 }
186
187 void
188 acre_set_title (acre_t *acre, const char *title)
189 {
190     free (acre->title);
191
192     acre->title = strdup (title);
193 }
194
195 void
196 acre_set_x_axis_label (acre_t *acre, const char *label)
197 {
198     free (acre->x_axis.label);
199
200     acre->x_axis.label = strdup (label);
201 }
202
203 void
204 acre_set_y_axis_label (acre_t *acre, const char *label)
205 {
206     free (acre->y_axis.label);
207
208     acre->y_axis.label = strdup (label);
209 }
210
211 void
212 acre_get_x_axis_data_range (acre_t *acre, double *x_min, double *x_max)
213 {
214     if (x_min)
215         *x_min = acre->x_axis.data_min;
216
217     if (x_max)
218         *x_max = acre->x_axis.data_max;
219 }
220
221 void
222 acre_get_x_axis_range (acre_t *acre, double *x_min, double *x_max)
223 {
224     /* If an X range has been set, return that. */
225     if (acre->x_axis.view_range_set) {
226         if (x_min)
227             *x_min = acre->x_axis.view_min;
228
229         if (x_max)
230             *x_max = acre->x_axis.view_max;
231
232         return;
233     }
234
235     /* Otherwise, if a Y range has been set, use that to compute X. */
236     if (acre->y_axis.view_range_set) {
237         _find_x_range_given_y_range (acre, x_min, x_max,
238                                      acre->y_axis.view_min,
239                                      acre->y_axis.view_max);
240
241         return;
242     }
243
244     /* Neither view range set. Return full, data-based X range. */
245     acre_get_x_axis_data_range (acre, x_min, x_max);
246 }
247
248 void
249 acre_set_x_axis_range (acre_t *acre, double x_min, double x_max)
250 {
251     acre->x_axis.view_min = x_min;
252     acre->x_axis.view_max = x_max;
253
254     acre->x_axis.view_range_set = true;
255 }
256
257 void
258 acre_set_x_axis_range_auto (acre_t *acre)
259 {
260     acre->x_axis.view_range_set = false;
261 }
262
263 void
264 acre_get_y_axis_data_range (acre_t *acre, double *y_min, double *y_max)
265 {
266     if (y_min)
267         *y_min = acre->y_axis.data_min;
268
269     if (y_max)
270         *y_max = acre->y_axis.data_max;
271 }
272
273 void
274 acre_get_y_axis_range (acre_t *acre, double *y_min, double *y_max)
275 {
276     /* If a Y range has been set, return that. */
277     if (acre->y_axis.view_range_set) {
278         if (y_min)
279             *y_min = acre->y_axis.view_min;
280
281         if (y_max)
282             *y_max = acre->y_axis.view_max;
283
284         return;
285     }
286
287     /* Otherwise, if an X range has been set, use that to compute Y. */
288     if (acre->x_axis.view_range_set) {
289         _find_y_range_given_x_range (acre, y_min, y_max,
290                                      acre->x_axis.view_min,
291                                      acre->x_axis.view_max);
292
293         return;
294     }
295
296     /* Neither view range set. Return full data-based Y range. */
297     acre_get_y_axis_data_range (acre, y_min, y_max);
298 }
299
300 void
301 acre_set_y_axis_range (acre_t *acre, double y_min, double y_max)
302 {
303     acre->y_axis.view_min = y_min;
304     acre->y_axis.view_max = y_max;
305
306     acre->y_axis.view_range_set = true;
307 }
308
309 void
310 acre_set_y_axis_range_auto (acre_t *acre)
311 {
312     acre->y_axis.view_range_set = false;
313 }
314
315 /* Add a dataset to the plot. The plot assumes ownership of the
316  * dataset so it is not necessary to call acre_data_destroy on it. */
317 void
318 acre_add_data (acre_t *acre, acre_data_t *data)
319 {
320     if (acre->num_data >= acre->data_size) {
321         acre->data_size *= 2;
322         if (acre->data_size == 0)
323             acre->data_size = 1;
324         acre->data = xrealloc_ab (acre->data,
325                                   acre->data_size,
326                                   sizeof (acre_data_t *));
327     }
328
329     acre->data[acre->num_data] = data;
330
331     if (acre->num_data == 0) {
332         acre->x_axis.data_min = data->min.x;
333         acre->y_axis.data_min = data->min.y;
334
335         acre->x_axis.data_max = data->max.x;
336         acre->y_axis.data_max = data->max.y;
337     } else {
338         if (data->min.x < acre->x_axis.data_min)
339             acre->x_axis.data_min = data->min.x;
340         if (data->min.y < acre->y_axis.data_min)
341             acre->y_axis.data_min = data->min.y;
342
343         if (data->max.x > acre->x_axis.data_max)
344             acre->x_axis.data_max = data->max.x;
345         if (data->max.y > acre->y_axis.data_max)
346             acre->y_axis.data_max = data->max.y;
347     }
348
349     acre->num_data++;
350 }
351
352 #define ACRE_FONT_FAMILY "sans"
353 #define ACRE_FONT_SIZE 12
354 #define ACRE_TITLE_FONT_SIZE 20
355 #define ACRE_PAD (ACRE_FONT_SIZE)
356 #define ACRE_TICK_MAJOR_SIZE 6
357 #define ACRE_TICK_MINOR_SIZE 3
358 #define ACRE_X_TICK_VALUE_PAD 2
359 #define ACRE_Y_TICK_VALUE_PAD 4
360 #define ACRE_LEGEND_PAD 4
361 #define ACRE_LEGEND_LINE_SIZE 10
362
363 static PangoLayout *
364 _create_layout (acre_t *acre, const char *text)
365 {
366     PangoLayout *layout;
367
368     if (text == NULL)
369             text = "";
370
371     cairo_save (acre->cr);
372     cairo_identity_matrix (acre->cr);
373
374     layout = pango_cairo_create_layout (acre->cr);
375     pango_layout_set_font_description (layout, acre->font);
376     pango_layout_set_text (layout, text, -1);
377     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
378
379     cairo_restore (acre->cr);
380
381     return layout;
382 }
383
384 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
385
386 static PangoLayout *
387 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
388 {
389     PangoLayout *layout;
390     char *text;
391
392     vasprintf (&text, fmt, ap);
393
394     layout = _create_layout (acre, text);
395
396     free (text);
397
398     return layout;
399 }
400
401 static PangoLayout *
402 _create_layout_printf (acre_t *acre, const char *fmt, ...)
403     PRINTF_FORMAT (2, 3);
404
405 static PangoLayout *
406 _create_layout_printf (acre_t *acre, const char *fmt, ...)
407 {
408     va_list ap;
409     PangoLayout *layout;
410
411     va_start (ap, fmt);
412
413     layout = _create_layout_vprintf (acre, fmt, ap);
414
415     va_end (ap);
416
417     return layout;
418 }
419
420 static void
421 _destroy_layout (PangoLayout *layout)
422 {
423     g_object_unref (layout);
424 }
425
426 static void
427 _show_layout (cairo_t *cr, PangoLayout *layout)
428 {
429     pango_cairo_show_layout (cr, layout);
430
431     _destroy_layout (layout);
432 }
433
434 static void
435 _draw_title_and_labels (acre_t *acre)
436 {
437     cairo_t *cr = acre->cr;
438     PangoFontDescription *title_font;
439     PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
440     PangoLayout *min_y, *max_y;
441     int min_y_width, max_y_width, y_axis_value_width;
442     int title_width, title_height;
443     int x_axis_width, x_axis_height;
444     int y_axis_width, y_axis_height;
445     PangoRectangle new_chart;
446
447     cairo_save (cr);
448
449     acre->font = pango_font_description_new ();
450     pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
451     pango_font_description_set_absolute_size (acre->font,
452                                               ACRE_FONT_SIZE * PANGO_SCALE);
453
454     title_font = pango_font_description_new ();
455     pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
456     pango_font_description_set_absolute_size (title_font,
457                                               ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
458
459     title_layout = _create_layout (acre, acre->title);
460     pango_layout_set_font_description (title_layout, title_font);
461     pango_font_description_free (title_font);
462
463     x_axis_layout = _create_layout (acre, acre->x_axis.label);
464     y_axis_layout = _create_layout (acre, acre->y_axis.label);
465
466     min_y = _create_layout_printf (acre, "%g",
467                                    round (acre->y_axis.view_min));
468     max_y = _create_layout_printf (acre, "%g",
469                                    round (acre->y_axis.view_max));
470
471     pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
472     pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
473     y_axis_value_width = MAX (min_y_width, max_y_width);
474
475     _destroy_layout (min_y);
476     _destroy_layout (max_y);
477
478     /* Iterate with the layout of the title and axis labels until they
479      * are stable, (this requires iteration since we don't know what
480      * to set their widths to in advance due to the wrapping of the
481      * other elements). */
482     while (1) {
483         pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
484         pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
485         pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
486
487         pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
488         pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
489         pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
490
491         new_chart.x = ACRE_PAD + y_axis_height +
492             ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
493         new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
494
495         new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
496         new_chart.height = acre->height - acre->chart.y - 
497             (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
498              ACRE_PAD + x_axis_height + ACRE_PAD);
499
500         if (new_chart.x == acre->chart.x &&
501             new_chart.y == acre->chart.y &&
502             new_chart.width == acre->chart.width &&
503             new_chart.height == acre->chart.height)
504         {
505             break;
506         }
507
508         acre->chart.x = new_chart.x;
509         acre->chart.y = new_chart.y;
510         acre->chart.width = new_chart.width;
511         acre->chart.height = new_chart.height;
512     }
513
514     cairo_set_source_rgb (cr, 0, 0, 0);
515
516     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
517     _show_layout (cr, title_layout);
518
519     cairo_save (cr);
520     {
521         cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
522         cairo_rotate (cr, - M_PI / 2.0);
523         cairo_move_to (cr, 0, 0);
524         _show_layout (cr, y_axis_layout);
525     }
526     cairo_restore (cr);
527
528     cairo_move_to (cr, acre->chart.x,
529                    acre->chart.y + acre->chart.height +
530                    ACRE_FONT_SIZE + ACRE_PAD);
531     _show_layout (cr, x_axis_layout);
532
533     cairo_restore (cr);
534 }
535
536 /* For a given axis range, compute a step size (in data space) to
537  * generate a suitable number of ticks (5 or so). */
538 static double
539 _step_for_range (double range, int *minor_divisions)
540 {
541     double step, scale_factor;
542
543     /* We want roughly 5 major ticks for the chart. */
544     step = range / 5;
545
546     /* Normalize the step so we can easily snap it to a desirable
547      * value. */
548     scale_factor = pow (10.0, floor (log10 (step)));
549     step /= scale_factor;
550
551     /* We want increments of 1, 2.5, 5, or 10 (times some power of
552      * 10). The threshold values between these are computed
553      * logarithmically. */
554     if (step < 3.535533905932738) {
555         if (step < 1.58113883008419) {
556             step = 1.0;
557             *minor_divisions = 4;
558         } else {
559             step = 2.5;
560             *minor_divisions = 5;
561         }
562     } else {
563         if (step < 7.071067811865475) {
564             step = 5.0;
565             *minor_divisions = 5;
566         } else {
567             step = 10.0;
568             *minor_divisions = 4;
569         }
570     }
571
572     /* Un-normalize and we now have the data value that we want to
573      * step at. */
574     return step * scale_factor;
575 }
576
577 /* Given an axis range, we can compute a desired data-space step
578  * amount for the major ticks (see _step_for_range). To get
579  * nice-looking pixel-snapped ticks we want to expand the range
580  * slightly. */
581 static void
582 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
583 {
584     double range, new_range, step, step_minor, pixel_step;
585     int minor_divisions;
586
587     range = *axis_max - *axis_min;
588
589     step = _step_for_range (range, &minor_divisions);
590     step_minor = step / minor_divisions;
591
592     pixel_step = step_minor * (pixel_range / range);
593
594     /* We expand the range by the ratio of the pixel step to the floor
595      * of the pixel_step.
596      */
597     new_range = range * pixel_step / floor (pixel_step);
598
599     /* And spread the increase out on either side of the range. */
600     *axis_min -= (new_range - range) / 2.0;
601     *axis_max += (new_range - range) / 2.0;
602 }
603
604 /* Setup a transformation in acre->cr such that data values plotted
605  * will appear where they should within the chart.
606  */
607 static void
608 _set_transform_to_data_space (acre_t *acre)
609 {
610     cairo_t *cr = acre->cr;
611
612     cairo_translate (cr,
613                      acre->chart.x,
614                      acre->chart.y + acre->chart.height);
615     cairo_scale (cr,
616                  acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
617                  - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
618     cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
619 }
620
621 static void
622 _find_x_range_given_y_range (acre_t *acre,
623                              double *x_min, double *x_max,
624                              double y_min, double y_max)
625 {
626     acre_data_t *data;
627     unsigned d, i;
628     bool first;
629
630     first = true;
631
632     for (d = 0; d < acre->num_data; d++) {
633         data = acre->data[d];
634         for (i = 0; i < data->num_points; i++) {
635             if (data->points[i].y >= y_min &&
636                 data->points[i].y <= y_max)
637             {
638                 if (first) {
639                     *x_min = data->points[i].x;
640                     *x_max = data->points[i].x;
641                     first = false;
642                 } else {
643                     if (data->points[i].x < *x_min)
644                         *x_min = data->points[i].x;
645                     if (data->points[i].x > *x_max)
646                         *x_max = data->points[i].x;
647                 }
648             }
649         }
650     }
651
652     /* If nothing is visible, punt to full X data range. */
653     if (first) {
654         *x_min = acre->x_axis.data_min;
655         *x_max = acre->x_axis.data_max;
656     }
657 }
658
659 static void
660 _find_y_range_given_x_range (acre_t *acre,
661                              double *y_min, double *y_max,
662                              double x_min, double x_max)
663 {
664     acre_data_t *data;
665     unsigned d, i;
666     bool first;
667
668     first = true;
669
670     for (d = 0; d < acre->num_data; d++) {
671         data = acre->data[d];
672         for (i = 0; i < data->num_points; i++) {
673             if (data->points[i].x >= x_min &&
674                 data->points[i].x <= x_max)
675             {
676                 if (first) {
677                     *y_min = data->points[i].y;
678                     *y_max = data->points[i].y;
679                     first = false;
680                 } else {
681                     if (data->points[i].y < *y_min)
682                         *y_min = data->points[i].y;
683                     if (data->points[i].y > *y_max)
684                         *y_max = data->points[i].y;
685                 }
686             }
687         }
688     }
689
690     /* If nothing is visible, punt to full Y data range. */
691     if (first) {
692         *y_min = acre->y_axis.data_min;
693         *y_max = acre->y_axis.data_max;
694     }
695 }
696
697 static void
698 _compute_axis_ranges (acre_t *acre)
699 {
700     double x_adjust, y_adjust;
701     cairo_t *cr = acre->cr;
702
703     /* If neither view range is set, set both to data ranges. */
704     if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
705     {
706         acre->x_axis.view_min = acre->x_axis.data_min;
707         acre->x_axis.view_max = acre->x_axis.data_max;
708
709         acre->y_axis.view_min = acre->y_axis.data_min;
710         acre->y_axis.view_max = acre->y_axis.data_max;
711     } else {
712         /* Otherwise, auto-fit unset range based on data. */
713         if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
714             _find_y_range_given_x_range (acre,
715                                          &acre->y_axis.view_min,
716                                          &acre->y_axis.view_max,
717                                          acre->x_axis.view_min,
718                                          acre->x_axis.view_max);
719         }
720         else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
721             _find_x_range_given_y_range (acre,
722                                          &acre->x_axis.view_min,
723                                          &acre->x_axis.view_max,
724                                          acre->y_axis.view_min,
725                                          acre->y_axis.view_max);
726         }
727     }
728
729     /* Then, increase the axis ranges just enough so that the step
730      * sizes for the ticks will be integers.
731      */
732     _expand_range_for_width (&acre->x_axis.view_min,
733                              &acre->x_axis.view_max,
734                              acre->chart.width);
735
736     _expand_range_for_width (&acre->y_axis.view_min,
737                              &acre->y_axis.view_max,
738                              acre->chart.height);
739
740     /* Finally, we also translate the axis ranges slightly so that the
741      * ticks land on half-integer device-pixel positions.
742      */
743     cairo_save (cr);
744     {
745         _set_transform_to_data_space (acre);
746
747         x_adjust = 0.0;
748         y_adjust = 0.0;
749         cairo_user_to_device (cr, &x_adjust, &y_adjust);
750         x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
751         y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
752         cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
753
754         acre->x_axis.view_min -= x_adjust;
755         acre->x_axis.view_max -= x_adjust;
756
757         acre->y_axis.view_min -= y_adjust;
758         acre->y_axis.view_max -= y_adjust;
759     }
760     cairo_restore (cr);
761 }
762
763 static void
764 _choose_colors (acre_t *acre)
765 {
766     cmsHPROFILE lab_profile, srgb_profile;
767     cmsHTRANSFORM lab_to_srgb;
768     int i;
769     double theta, radius, srgb[3];
770     cmsCIELab lab;
771
772     lab_profile = cmsCreateLabProfile (NULL); /* D50 */
773     srgb_profile = cmsCreate_sRGBProfile ();
774
775     lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
776                                       srgb_profile, TYPE_RGB_DBL,
777                                       INTENT_PERCEPTUAL, 0);
778
779     acre->num_colors = acre->num_data;
780
781     if (acre->num_colors > acre->colors_size) {
782         acre->colors_size = acre->num_colors;
783         acre->colors = xrealloc (acre->colors,
784                                  acre->colors_size * sizeof (acre_color_t));
785     }
786
787     lab.L = 36;
788     radius = 130;
789     for (i = 0; i < acre->num_colors; i++) {
790         theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
791         lab.a = radius * cos (theta);
792         lab.b = radius * sin (theta);
793
794         cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
795
796         acre->colors[i].red = srgb[0];
797         acre->colors[i].green = srgb[1];
798         acre->colors[i].blue = srgb[2];
799     }
800
801     cmsDeleteTransform (lab_to_srgb);
802     cmsCloseProfile (lab_profile);
803     cmsCloseProfile (srgb_profile);
804 }
805
806 /* Draw the given dataset as a line. */
807 static void
808 _draw_data_line (acre_t *acre, acre_data_t *data)
809 {
810     unsigned i;
811     cairo_t *cr = acre->cr;
812
813     cairo_save (cr);
814
815     cairo_new_path (cr);
816
817     for (i = 0; i < data->num_points; i++) {
818         cairo_line_to (cr,
819                        data->points[i].x,
820                        data->points[i].y);
821     }
822
823     cairo_identity_matrix (cr);
824     cairo_set_line_width (cr, 1.0);
825     cairo_stroke (cr);
826
827     cairo_restore (cr);
828 }
829
830 /* Draw all the datasets of the chart. */
831 static void
832 _draw_data (acre_t *acre)
833 {
834     cairo_t *cr = acre->cr;
835     unsigned int i;
836     acre_data_t *data;
837
838     cairo_save (cr);
839
840     cairo_rectangle (cr,
841                      acre->chart.x, acre->chart.y,
842                      acre->chart.width, acre->chart.height);
843     cairo_clip (cr);
844
845     cairo_set_source_rgb (cr, 0, 0, 0);
846
847     _set_transform_to_data_space (acre);
848
849     for (i = 0; i < acre->num_data; i++) {
850         int color = i % acre->num_colors;
851         cairo_set_source_rgb (cr,
852                               acre->colors[color].red,
853                               acre->colors[color].green,
854                               acre->colors[color].blue);
855         data = acre->data[i];
856
857         switch (data->style) {
858         case ACRE_STYLE_LINE:
859             _draw_data_line (acre, data);
860             break;
861         }
862     }
863
864     cairo_restore (cr);
865 }
866
867 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
868
869 static void
870 _draw_ticks (acre_t *acre,
871              double axis_min, double axis_max,
872              acre_ticks_t ticks)
873 {
874     cairo_t *cr = acre->cr;
875     double t, step, sub_step;
876     int minor_divisions;
877
878     cairo_save (cr);
879
880     _set_transform_to_data_space (acre);
881
882     step = _step_for_range (axis_max - axis_min, &minor_divisions);
883     sub_step = step / minor_divisions;
884
885     for (t = (floor (axis_min / sub_step) + 1) * sub_step;
886          t <= axis_max;
887          t += sub_step)
888     {
889         int tick_size;
890         if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
891             tick_size = ACRE_TICK_MAJOR_SIZE;
892         else
893             tick_size = ACRE_TICK_MINOR_SIZE;
894
895         /* tick */
896         cairo_save (cr);
897         {
898             if (ticks == ACRE_TICKS_X)
899                 cairo_move_to (cr, t, acre->y_axis.view_min);
900             else
901                 cairo_move_to (cr, acre->x_axis.view_min, t);
902
903             cairo_identity_matrix (cr);
904
905             if (ticks == ACRE_TICKS_X) {
906                 cairo_rel_line_to (cr, 0, 0.5);
907                 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
908             } else {
909                 cairo_rel_line_to (cr, -0.5, 0);
910                 cairo_rel_line_to (cr, tick_size + 0.5, 0);
911             }
912
913             cairo_set_line_width (cr, 1.0);
914             cairo_stroke (cr);
915         }
916         cairo_restore (cr);
917
918         /* label */
919         if (tick_size == ACRE_TICK_MAJOR_SIZE)
920         {
921             PangoLayout *layout;
922             int width, height;
923             double label_value;
924
925             cairo_save (cr);
926
927             label_value = t;
928             if (fabs (label_value) < (sub_step / 1000.))
929                 label_value = 0.0;
930             layout = _create_layout_printf (acre, "%g", label_value);
931
932             if (ticks == ACRE_TICKS_X)
933                 cairo_move_to (cr, t, acre->y_axis.view_min);
934             else
935                 cairo_move_to (cr, acre->x_axis.view_min, t);
936
937             cairo_identity_matrix (cr);
938             pango_layout_get_pixel_size (layout, &width, &height);
939
940             if (ticks == ACRE_TICKS_X)
941                 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
942             else
943                 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
944                                    -height/2);
945
946             _show_layout (cr, layout);
947
948             cairo_restore (cr);
949         }
950     }
951
952     cairo_restore (cr);
953 }
954
955 static void
956 _draw_legend (acre_t *acre)
957 {
958     PangoLayout *layout;
959     int label_width, max_label_width = 0;
960     int width, height;
961     unsigned int i;
962     cairo_t *cr = acre->cr;
963
964     cairo_save (cr);
965
966     for (i = 0; i < acre->num_data; i++) {
967         layout = _create_layout (acre, acre->data[i]->name);
968         pango_layout_get_pixel_size (layout, &label_width, NULL);
969         _destroy_layout (layout);
970         if (label_width > max_label_width)
971             max_label_width = label_width;
972     }
973
974     width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD + 
975         max_label_width + ACRE_LEGEND_PAD;
976     height = ACRE_LEGEND_PAD +
977         acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
978
979     cairo_translate (cr, acre->chart.x, acre->chart.y);
980
981     cairo_translate (cr,
982                      acre->chart.width - ACRE_LEGEND_PAD - width,
983                      ACRE_LEGEND_PAD);
984
985     cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
986     cairo_set_source_rgb (cr, 0, 0, 0);
987     cairo_set_line_width (cr, 1.0);
988     cairo_stroke (cr);
989
990     cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
991
992     for (i = 0; i < acre->num_data; i++) {
993         cairo_rectangle (cr,
994                          0, ACRE_LEGEND_LINE_SIZE / 2,
995                          ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
996         cairo_set_source_rgb (cr,
997                               acre->colors[i % acre->num_colors].red,
998                               acre->colors[i % acre->num_colors].green,
999                               acre->colors[i % acre->num_colors].blue);
1000         cairo_fill (cr);
1001
1002         layout = _create_layout (acre, acre->data[i]->name);
1003         cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
1004         cairo_set_source_rgb (cr, 0, 0, 0);
1005         _show_layout (cr, layout);
1006
1007         cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
1008     }
1009
1010     cairo_restore (cr);
1011 }
1012
1013 static void
1014 _draw_frame_and_ticks (acre_t *acre)
1015 {
1016     cairo_t *cr = acre->cr;
1017
1018     cairo_save (cr);
1019
1020     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
1021
1022     /* ticks */
1023     _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
1024     _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
1025
1026     /* frame */
1027     cairo_rectangle (cr,
1028                      acre->chart.x - 0.5, acre->chart.y - 0.5,
1029                      acre->chart.width + 1.0, acre->chart.height + 1.0);
1030     cairo_set_line_width (cr, 1.0);
1031     cairo_stroke (cr);
1032
1033     cairo_restore (cr);
1034 }
1035
1036 /* Draw the plot to the given cairo context within a user-space
1037  * rectangle from (0, 0) to (width, height). This size includes all
1038  * space for extra-plot elements (such as the title, the axis labels,
1039  * etc.)
1040  */
1041 void
1042 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1043 {
1044     acre->cr = cr;
1045
1046     acre->width = width;
1047     acre->height = height;
1048
1049     acre->chart.width = width;
1050     acre->chart.height = height;
1051
1052     cairo_save (cr);
1053
1054     cairo_set_source_rgb (cr, 1, 1, 1);
1055
1056     _choose_colors (acre);
1057
1058     /* We compute the axis ranges before doing label layout so that we
1059      * can account for the width of the y-axis value labels. */
1060     _compute_axis_ranges (acre);
1061
1062     _draw_title_and_labels (acre);
1063
1064     /* And we recompute the axis ranges now that the title and axis
1065      * label space is all measured and accounted for. */
1066     _compute_axis_ranges (acre);
1067
1068     _draw_data (acre);
1069
1070     if (acre->num_data > 1)
1071         _draw_legend (acre);
1072
1073     _draw_frame_and_ticks (acre);
1074 }
1075
1076 /* Create a new dataset---a collection of (x, y) datapoints. A single
1077  * plot can contain multiple datasets, (see acre_add_data). */
1078 acre_data_t *
1079 acre_data_create (void)
1080 {
1081     acre_data_t *data;
1082
1083     data = xmalloc (sizeof (acre_data_t));
1084
1085     data->name = NULL;
1086
1087     data->style = ACRE_STYLE_LINE;
1088
1089     data->points = NULL;
1090     data->points_size = 0;
1091     data->num_points = 0;
1092
1093     return data;
1094 }
1095
1096 /* Destroy an acre dataset. Do not call this function if the dataset
1097  * has been added to an acre_t plot with acre_add_data. */
1098 void
1099 acre_data_destroy (acre_data_t *data)
1100 {
1101     unsigned i;
1102
1103     for (i = 0; i < data->num_names; i++) {
1104         if (data->names[i])
1105             free (data->names[i]);
1106     }
1107     free (data->names);
1108
1109     free (data->name);
1110
1111     free (data->points);
1112
1113     free (data);
1114 }
1115
1116 /* Set the label for this dataset (to appear in the plot's key). */
1117 void
1118 acre_data_set_name (acre_data_t *data, const char *name)
1119 {
1120     free (data->name);
1121
1122     data->name = strdup (name);
1123 }
1124
1125 /* Add a datapoint to the given dataset. */
1126 void
1127 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1128 {
1129     if (data->num_points >= data->points_size) {
1130         data->points_size *= 2;
1131         if (data->points_size == 0)
1132             data->points_size = 16;
1133         data->points = xrealloc_ab (data->points,
1134                                     data->points_size,
1135                                     sizeof (acre_data_point_2d_t));
1136     }
1137
1138     data->points[data->num_points].x = x;
1139     data->points[data->num_points].y = y;
1140
1141     if (data->num_points == 0) {
1142         data->min.x = x;
1143         data->min.y = y;
1144
1145         data->max.x = x;
1146         data->max.y = y;
1147     } else {
1148         if (x < data->min.x)
1149             data->min.x = x;
1150         if (y < data->min.y)
1151             data->min.y = y;
1152
1153         if (x > data->max.x)
1154             data->max.x = x;
1155         if (y > data->max.y)
1156             data->max.y = y;
1157     }
1158
1159     data->num_points++;
1160 }
1161
1162 /* Add a datapoint with a name to the given dataset. */
1163 void
1164 acre_data_add_point_2d_named (acre_data_t *data, double x, double y, const char *name)
1165 {
1166     unsigned i;
1167
1168     acre_data_add_point_2d (data, x, y);
1169
1170     if (data->names_size < data->points_size) {
1171         data->names_size = data->points_size;
1172         data->names = xrealloc_ab (data->names,
1173                                    data->names_size,
1174                                    sizeof (char *));
1175     }
1176
1177     /* Initialize any newly-created holes in the array to NULL. */
1178     for (i = data->num_names; i < data->num_points - 1; i++)
1179         data->names[i] = NULL;
1180
1181     data->num_names = data->num_points;
1182
1183     data->names[data->num_names - 1] = xstrdup (name);
1184 }