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