]> git.cworth.org Git - acre/blob - acre.c
e3604dcff6a8de23ed264edc96bbeeaf374ee0fa
[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     layout = pango_cairo_create_layout (acre->cr);
351     pango_layout_set_font_description (layout, acre->font);
352     pango_layout_set_text (layout, text, -1);
353     pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
354
355     return layout;
356 }
357
358 #define PRINTF_FORMAT(fmt_index, va_index) __attribute__ ((__format__(__printf__, fmt_index, va_index)))
359
360 static PangoLayout *
361 _create_layout_vprintf (acre_t *acre, const char *fmt, va_list ap)
362 {
363     PangoLayout *layout;
364     char *text;
365
366     vasprintf (&text, fmt, ap);
367
368     layout = _create_layout (acre, text);
369
370     free (text);
371
372     return layout;
373 }
374
375 static PangoLayout *
376 _create_layout_printf (acre_t *acre, const char *fmt, ...)
377     PRINTF_FORMAT (2, 3);
378
379 static PangoLayout *
380 _create_layout_printf (acre_t *acre, const char *fmt, ...)
381 {
382     va_list ap;
383     PangoLayout *layout;
384
385     va_start (ap, fmt);
386
387     layout = _create_layout_vprintf (acre, fmt, ap);
388
389     va_end (ap);
390
391     return layout;
392 }
393
394 static void
395 _destroy_layout (PangoLayout *layout)
396 {
397     g_object_unref (layout);
398 }
399
400 static void
401 _show_layout (cairo_t *cr, PangoLayout *layout)
402 {
403     pango_cairo_show_layout (cr, layout);
404
405     _destroy_layout (layout);
406 }
407
408 static void
409 _draw_title_and_labels (acre_t *acre)
410 {
411     cairo_t *cr = acre->cr;
412     PangoFontDescription *title_font;
413     PangoLayout *title_layout, *x_axis_layout, *y_axis_layout;
414     PangoLayout *min_y, *max_y;
415     int min_y_width, max_y_width, y_axis_value_width;
416     int title_width, title_height;
417     int x_axis_width, x_axis_height;
418     int y_axis_width, y_axis_height;
419     PangoRectangle new_chart;
420
421     cairo_save (cr);
422
423     acre->font = pango_font_description_new ();
424     pango_font_description_set_family (acre->font, ACRE_FONT_FAMILY);
425     pango_font_description_set_absolute_size (acre->font,
426                                               ACRE_FONT_SIZE * PANGO_SCALE);
427
428     title_font = pango_font_description_new ();
429     pango_font_description_set_family (title_font, ACRE_FONT_FAMILY);
430     pango_font_description_set_absolute_size (title_font,
431                                               ACRE_TITLE_FONT_SIZE * PANGO_SCALE);
432
433     title_layout = _create_layout (acre, acre->title);
434     pango_layout_set_font_description (title_layout, title_font);
435
436     x_axis_layout = _create_layout (acre, acre->x_axis.label);
437     y_axis_layout = _create_layout (acre, acre->y_axis.label);
438
439     min_y = _create_layout_printf (acre, "%g",
440                                    round (acre->y_axis.view_min));
441     max_y = _create_layout_printf (acre, "%g",
442                                    round (acre->y_axis.view_max));
443
444     pango_layout_get_pixel_size (min_y, &min_y_width, NULL);
445     pango_layout_get_pixel_size (max_y, &max_y_width, NULL);
446     y_axis_value_width = MAX (min_y_width, max_y_width);
447
448     _destroy_layout (min_y);
449     _destroy_layout (max_y);
450
451     /* Iterate with the layout of the title and axis labels until they
452      * are stable, (this requires iteration since we don't know what
453      * to set their widths to in advance due to the wrapping of the
454      * other elements). */
455     while (1) {
456         pango_layout_set_width (title_layout, acre->chart.width * PANGO_SCALE);
457         pango_layout_set_width (x_axis_layout, acre->chart.width * PANGO_SCALE);
458         pango_layout_set_width (y_axis_layout, acre->chart.height * PANGO_SCALE);
459
460         pango_layout_get_pixel_size (title_layout, &title_width, &title_height);
461         pango_layout_get_pixel_size (x_axis_layout, &x_axis_width, &x_axis_height);
462         pango_layout_get_pixel_size (y_axis_layout, &y_axis_width, &y_axis_height);
463
464         new_chart.x = ACRE_PAD + y_axis_height +
465             ACRE_PAD + y_axis_value_width + ACRE_Y_TICK_VALUE_PAD;
466         new_chart.width = acre->width - acre->chart.x - ACRE_PAD;
467
468         new_chart.y = ACRE_PAD + title_height + ACRE_PAD;
469         new_chart.height = acre->height - acre->chart.y - 
470             (ACRE_X_TICK_VALUE_PAD + ACRE_FONT_SIZE +
471              ACRE_PAD + x_axis_height + ACRE_PAD);
472
473         if (new_chart.x == acre->chart.x &&
474             new_chart.y == acre->chart.y &&
475             new_chart.width == acre->chart.width &&
476             new_chart.height == acre->chart.height)
477         {
478             break;
479         }
480
481         acre->chart.x = new_chart.x;
482         acre->chart.y = new_chart.y;
483         acre->chart.width = new_chart.width;
484         acre->chart.height = new_chart.height;
485     }
486
487     cairo_set_source_rgb (cr, 0, 0, 0);
488
489     cairo_move_to (cr, acre->chart.x, ACRE_PAD);
490     _show_layout (cr, title_layout);
491
492     cairo_save (cr);
493     {
494         cairo_translate (cr, ACRE_PAD, acre->chart.y + acre->chart.height);
495         cairo_rotate (cr, - M_PI / 2.0);
496         cairo_move_to (cr, 0, 0);
497         _show_layout (cr, y_axis_layout);
498     }
499     cairo_restore (cr);
500
501     cairo_move_to (cr, acre->chart.x,
502                    acre->chart.y + acre->chart.height +
503                    ACRE_FONT_SIZE + ACRE_PAD);
504     _show_layout (cr, x_axis_layout);
505
506     cairo_restore (cr);
507 }
508
509 /* For a given axis range, compute a step size (in data space) to
510  * generate a suitable number of ticks (5 or so). */
511 static double
512 _step_for_range (double range, int *minor_divisions)
513 {
514     double step, scale_factor;
515
516     /* We want roughly 5 major ticks for the chart. */
517     step = range / 5;
518
519     /* Normalize the step so we can easily snap it to a desirable
520      * value. */
521     scale_factor = pow (10.0, floor (log10 (step)));
522     step /= scale_factor;
523
524     /* We want increments of 1, 2.5, 5, or 10 (times some power of
525      * 10). The threshold values between these are computed
526      * logarithmically. */
527     if (step < 3.535533905932738) {
528         if (step < 1.58113883008419) {
529             step = 1.0;
530             *minor_divisions = 4;
531         } else {
532             step = 2.5;
533             *minor_divisions = 5;
534         }
535     } else {
536         if (step < 7.071067811865475) {
537             step = 5.0;
538             *minor_divisions = 5;
539         } else {
540             step = 10.0;
541             *minor_divisions = 4;
542         }
543     }
544
545     /* Un-normalize and we now have the data value that we want to
546      * step at. */
547     return step * scale_factor;
548 }
549
550 /* Given an axis range, we can compute a desired data-space step
551  * amount for the major ticks (see _step_for_range). To get
552  * nice-looking pixel-snapped ticks we want to expand the range
553  * slightly. */
554 static void
555 _expand_range_for_width (double *axis_min, double *axis_max, int pixel_range)
556 {
557     double range, new_range, step, step_minor, pixel_step;
558     int minor_divisions;
559
560     range = *axis_max - *axis_min;
561
562     step = _step_for_range (range, &minor_divisions);
563     step_minor = step / minor_divisions;
564
565     pixel_step = step_minor * (pixel_range / range);
566
567     /* We expand the range by the ratio of the pixel step to the floor
568      * of the pixel_step.
569      */
570     new_range = range * pixel_step / floor (pixel_step);
571
572     /* And spread the increase out on either side of the range. */
573     *axis_min -= (new_range - range) / 2.0;
574     *axis_max += (new_range - range) / 2.0;
575 }
576
577 /* Setup a transformation in acre->cr such that data values plotted
578  * will appear where they should within the chart.
579  */
580 static void
581 _set_transform_to_data_space (acre_t *acre)
582 {
583     cairo_t *cr = acre->cr;
584
585     cairo_translate (cr,
586                      acre->chart.x,
587                      acre->chart.y + acre->chart.height);
588     cairo_scale (cr,
589                  acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
590                  - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
591     cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
592 }
593
594 static void
595 _find_x_range_given_y_range (acre_t *acre,
596                              double *x_min, double *x_max,
597                              double y_min, double y_max)
598 {
599     acre_data_t *data;
600     unsigned d, i;
601     bool first;
602
603     first = true;
604
605     for (d = 0; d < acre->num_data; d++) {
606         data = acre->data[d];
607         for (i = 0; i < data->num_points; i++) {
608             if (data->points[i].y >= y_min &&
609                 data->points[i].y <= y_max)
610             {
611                 if (first) {
612                     *x_min = data->points[i].x;
613                     *x_max = data->points[i].x;
614                     first = false;
615                 } else {
616                     if (data->points[i].x < *x_min)
617                         *x_min = data->points[i].x;
618                     if (data->points[i].x > *x_max)
619                         *x_max = data->points[i].x;
620                 }
621             }
622         }
623     }
624
625     /* If nothing is visible, punt to full X data range. */
626     if (first) {
627         *x_min = acre->x_axis.data_min;
628         *x_max = acre->x_axis.data_max;
629     }
630 }
631
632 static void
633 _find_y_range_given_x_range (acre_t *acre,
634                              double *y_min, double *y_max,
635                              double x_min, double x_max)
636 {
637     acre_data_t *data;
638     unsigned d, i;
639     bool first;
640
641     first = true;
642
643     for (d = 0; d < acre->num_data; d++) {
644         data = acre->data[d];
645         for (i = 0; i < data->num_points; i++) {
646             if (data->points[i].x >= x_min &&
647                 data->points[i].x <= x_max)
648             {
649                 if (first) {
650                     *y_min = data->points[i].y;
651                     *y_max = data->points[i].y;
652                     first = false;
653                 } else {
654                     if (data->points[i].y < *y_min)
655                         *y_min = data->points[i].y;
656                     if (data->points[i].y > *y_max)
657                         *y_max = data->points[i].y;
658                 }
659             }
660         }
661     }
662
663     /* If nothing is visible, punt to full Y data range. */
664     if (first) {
665         *y_min = acre->y_axis.data_min;
666         *y_max = acre->y_axis.data_max;
667     }
668 }
669
670 static void
671 _compute_axis_ranges (acre_t *acre)
672 {
673     double x_adjust, y_adjust;
674     cairo_t *cr = acre->cr;
675
676     /* If neither view range is set, set both to data ranges. */
677     if (! acre->x_axis.view_range_set && ! acre->y_axis.view_range_set)
678     {
679         acre->x_axis.view_min = acre->x_axis.data_min;
680         acre->x_axis.view_max = acre->x_axis.data_max;
681
682         acre->y_axis.view_min = acre->y_axis.data_min;
683         acre->y_axis.view_max = acre->y_axis.data_max;
684     } else {
685         /* Otherwise, auto-fit unset range based on data. */
686         if (acre->x_axis.view_range_set && ! acre->y_axis.view_range_set) {
687             _find_y_range_given_x_range (acre,
688                                          &acre->y_axis.view_min,
689                                          &acre->y_axis.view_max,
690                                          acre->x_axis.view_min,
691                                          acre->x_axis.view_max);
692         }
693         else if (acre->y_axis.view_range_set && ! acre->x_axis.view_range_set) {
694             _find_x_range_given_y_range (acre,
695                                          &acre->x_axis.view_min,
696                                          &acre->x_axis.view_max,
697                                          acre->y_axis.view_min,
698                                          acre->y_axis.view_max);
699         }
700     }
701
702     /* Next, we want to ensure that the data never collides with the
703      * ticks. So we expand each axis on its minimum side as needed. */
704     cairo_save (cr);
705     {
706         double x, y;
707
708         _set_transform_to_data_space (acre);
709
710         x = ACRE_TICK_MAJOR_SIZE + 2.0;
711         y = ACRE_TICK_MAJOR_SIZE + 2.0;
712         cairo_device_to_user_distance (cr, &x, &y);
713
714         acre->x_axis.view_min -= x;
715         acre->y_axis.view_min += y;
716     }
717     cairo_restore (cr);
718
719     /* Then, increase the axis ranges just enough so that the step
720      * sizes for the ticks will be integers.
721      */
722     _expand_range_for_width (&acre->x_axis.view_min,
723                              &acre->x_axis.view_max,
724                              acre->chart.width);
725
726     _expand_range_for_width (&acre->y_axis.view_min,
727                              &acre->y_axis.view_max,
728                              acre->chart.height);
729
730     /* Finally, we also translate the axis ranges slightly so that the
731      * ticks land on half-integer device-pixel positions.
732      */
733     cairo_save (cr);
734     {
735         _set_transform_to_data_space (acre);
736
737         x_adjust = 0.0;
738         y_adjust = 0.0;
739         cairo_user_to_device (cr, &x_adjust, &y_adjust);
740         x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
741         y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
742         cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
743
744         acre->x_axis.view_min -= x_adjust;
745         acre->x_axis.view_max -= x_adjust;
746
747         acre->y_axis.view_min -= y_adjust;
748         acre->y_axis.view_max -= y_adjust;
749     }
750     cairo_restore (cr);
751 }
752
753 static void
754 _acre_color_from_hsv (acre_color_t *color,
755                       double hue,
756                       double saturation,
757                       double value)
758 {
759     double f, p, q, t;
760     int hmod6;
761
762     hmod6 = (int) floor (hue / 60) % 6;
763     f = hue / 60 - floor (hue / 60);
764     p = value * (1 - saturation);
765     q = value * (1 - f * saturation);
766     t = value * (1 - (1 - f) * saturation);
767     
768     switch (hmod6) {
769       case 0:
770         color->red = value;
771         color->green = t;
772         color->blue = p;
773         break;
774       case 1:
775         color->red = q;
776         color->green = value;
777         color->blue = p;
778         break;
779       case 2:
780         color->red = p;
781         color->green = value;
782         color->blue = t;
783         break;
784       case 3:
785         color->red = p;
786         color->green = q;
787         color->blue = value;
788         break;
789       case 4:
790         color->red = t;
791         color->green = p;
792         color->blue = value;
793         break;
794       case 5:
795         color->red = value;
796         color->green = p;
797         color->blue = q;
798         break;
799     }
800 }
801
802 static void
803 _choose_colors (acre_t *acre)
804 {
805     cmsHPROFILE lab_profile, srgb_profile;
806     cmsHTRANSFORM lab_to_srgb;
807     int i;
808     double theta, radius, srgb[3];
809     cmsCIELab lab;
810
811     lab_profile = cmsCreateLabProfile (NULL); /* D50 */
812     srgb_profile = cmsCreate_sRGBProfile ();
813
814     lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
815                                       srgb_profile, TYPE_RGB_DBL,
816                                       INTENT_PERCEPTUAL, 0);
817
818     acre->num_colors = acre->num_data;
819
820     if (acre->num_colors > acre->colors_size) {
821         acre->colors_size = acre->num_colors;
822         acre->colors = xrealloc (acre->colors,
823                                  acre->colors_size * sizeof (acre_color_t));
824     }
825
826     lab.L = 36;
827     radius = 130;
828     for (i = 0; i < acre->num_colors; i++) {
829         theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
830         lab.a = radius * cos (theta);
831         lab.b = radius * sin (theta);
832
833         cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
834
835         acre->colors[i].red = srgb[0];
836         acre->colors[i].green = srgb[1];
837         acre->colors[i].blue = srgb[2];
838     }
839
840     cmsDeleteTransform (lab_to_srgb);
841     cmsCloseProfile (lab_profile);
842     cmsCloseProfile (srgb_profile);
843 }
844
845 static void
846 _draw_data (acre_t *acre)
847 {
848     cairo_t *cr = acre->cr;
849     unsigned int d, i;
850     acre_data_t *data;
851     bool pen_up;
852
853     cairo_save (cr);
854
855     cairo_set_source_rgb (cr, 0, 0, 0);
856
857     _set_transform_to_data_space (acre);
858
859     for (d = 0; d < acre->num_data; d++) {
860         int color = d % acre->num_colors;
861         cairo_set_source_rgb (cr,
862                               acre->colors[color].red,
863                               acre->colors[color].green,
864                               acre->colors[color].blue);
865         data = acre->data[d];
866
867         pen_up = true;
868         cairo_new_path (cr);
869
870         for (i = 0; i < data->num_points; i++) {
871             if (data->points[i].x >= acre->x_axis.view_min &&
872                 data->points[i].x <= acre->x_axis.view_max &&
873                 data->points[i].y >= acre->y_axis.view_min &&
874                 data->points[i].y <= acre->y_axis.view_max)
875             {
876                 if (pen_up)
877                     cairo_move_to (cr,
878                                    data->points[i].x,
879                                    data->points[i].y);
880                 else
881                     cairo_line_to (cr,
882                                    data->points[i].x,
883                                    data->points[i].y);
884
885                 pen_up = false;
886             } else {
887                 pen_up = true;
888             }
889         }
890         cairo_save (cr);
891         {
892             cairo_identity_matrix (cr);
893             cairo_set_line_width (cr, 1.0);
894             cairo_stroke (cr);
895         }
896         cairo_restore (cr);
897     }
898
899     cairo_restore (cr);
900 }
901
902 typedef enum _ticks { ACRE_TICKS_X, ACRE_TICKS_Y } acre_ticks_t;
903
904 static void
905 _draw_ticks (acre_t *acre,
906              double axis_min, double axis_max,
907              acre_ticks_t ticks)
908 {
909     cairo_t *cr = acre->cr;
910     double t, step, sub_step;
911     int minor_divisions;
912
913     cairo_save (cr);
914
915     _set_transform_to_data_space (acre);
916
917     step = _step_for_range (axis_max - axis_min, &minor_divisions);
918     sub_step = step / minor_divisions;
919
920     for (t = (floor (axis_min / sub_step) + 1) * sub_step;
921          t <= axis_max;
922          t += sub_step)
923     {
924         int tick_size;
925         if (fabs((t / step) - round (t / step)) < 0.5 * (sub_step / step))
926             tick_size = ACRE_TICK_MAJOR_SIZE;
927         else
928             tick_size = ACRE_TICK_MINOR_SIZE;
929
930         /* tick */
931         cairo_save (cr);
932         {
933             if (ticks == ACRE_TICKS_X)
934                 cairo_move_to (cr, t, acre->y_axis.view_min);
935             else
936                 cairo_move_to (cr, acre->x_axis.view_min, t);
937
938             cairo_identity_matrix (cr);
939
940             if (ticks == ACRE_TICKS_X) {
941                 cairo_rel_line_to (cr, 0, 0.5);
942                 cairo_rel_line_to (cr, 0, -tick_size - 0.5);
943             } else {
944                 cairo_rel_line_to (cr, -0.5, 0);
945                 cairo_rel_line_to (cr, tick_size + 0.5, 0);
946             }
947
948             cairo_set_line_width (cr, 1.0);
949             cairo_stroke (cr);
950         }
951         cairo_restore (cr);
952
953         /* label */
954         if (tick_size == ACRE_TICK_MAJOR_SIZE)
955         {
956             PangoLayout *layout;
957             int width, height;
958
959             cairo_save (cr);
960
961             layout = _create_layout_printf (acre, "%g", t);
962
963             if (ticks == ACRE_TICKS_X)
964                 cairo_move_to (cr, t, acre->y_axis.view_min);
965             else
966                 cairo_move_to (cr, acre->x_axis.view_min, t);
967
968             cairo_identity_matrix (cr);
969             pango_layout_get_pixel_size (layout, &width, &height);
970
971             if (ticks == ACRE_TICKS_X)
972                 cairo_rel_move_to (cr, -width / 2, ACRE_X_TICK_VALUE_PAD);
973             else
974                 cairo_rel_move_to (cr, -width - ACRE_Y_TICK_VALUE_PAD,
975                                    -height/2);
976
977             _show_layout (cr, layout);
978
979             cairo_restore (cr);
980         }
981     }
982
983     cairo_restore (cr);
984 }
985
986 static void
987 _draw_legend (acre_t *acre)
988 {
989     PangoLayout *layout;
990     int label_width, max_label_width = 0;
991     int width, height;
992     unsigned int i;
993     cairo_t *cr = acre->cr;
994
995     cairo_save (cr);
996
997     for (i = 0; i < acre->num_data; i++) {
998         layout = _create_layout (acre, acre->data[i]->name);
999         pango_layout_get_pixel_size (layout, &label_width, NULL);
1000         _destroy_layout (layout);
1001         if (label_width > max_label_width)
1002             max_label_width = label_width;
1003     }
1004
1005     width = ACRE_LEGEND_PAD + ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD + 
1006         max_label_width + ACRE_LEGEND_PAD;
1007     height = ACRE_LEGEND_PAD +
1008         acre->num_data * (ACRE_FONT_SIZE + ACRE_LEGEND_PAD);
1009
1010     cairo_translate (cr, acre->chart.x, acre->chart.y);
1011
1012     cairo_translate (cr,
1013                      acre->chart.width - ACRE_LEGEND_PAD - width,
1014                      ACRE_LEGEND_PAD);
1015
1016     cairo_rectangle (cr, -0.5, -0.5, width + 1.0, height + 1.0);
1017     cairo_set_source_rgb (cr, 0, 0, 0);
1018     cairo_set_line_width (cr, 1.0);
1019     cairo_stroke (cr);
1020
1021     cairo_translate (cr, ACRE_LEGEND_PAD, ACRE_LEGEND_PAD);
1022
1023     for (i = 0; i < acre->num_data; i++) {
1024         cairo_rectangle (cr,
1025                          0, ACRE_LEGEND_LINE_SIZE / 2,
1026                          ACRE_LEGEND_LINE_SIZE, ACRE_LEGEND_LINE_SIZE / 2);
1027         cairo_set_source_rgb (cr,
1028                               acre->colors[i % acre->num_colors].red,
1029                               acre->colors[i % acre->num_colors].green,
1030                               acre->colors[i % acre->num_colors].blue);
1031         cairo_fill (cr);
1032
1033         layout = _create_layout (acre, acre->data[i]->name);
1034         cairo_move_to (cr, ACRE_LEGEND_LINE_SIZE + ACRE_LEGEND_PAD, 0);
1035         cairo_set_source_rgb (cr, 0, 0, 0);
1036         _show_layout (cr, layout);
1037
1038         cairo_translate (cr, 0, ACRE_LEGEND_PAD + ACRE_FONT_SIZE);
1039     }
1040
1041     cairo_restore (cr);
1042 }
1043
1044 static void
1045 _draw_frame_and_ticks (acre_t *acre)
1046 {
1047     cairo_t *cr = acre->cr;
1048
1049     cairo_save (cr);
1050
1051     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
1052
1053     /* ticks */
1054     _draw_ticks (acre, acre->x_axis.view_min, acre->x_axis.view_max, ACRE_TICKS_X);
1055     _draw_ticks (acre, acre->y_axis.view_min, acre->y_axis.view_max, ACRE_TICKS_Y);
1056
1057     /* frame */
1058     cairo_rectangle (cr,
1059                      acre->chart.x - 0.5, acre->chart.y - 0.5,
1060                      acre->chart.width + 1.0, acre->chart.height + 1.0);
1061     cairo_set_line_width (cr, 1.0);
1062     cairo_stroke (cr);
1063
1064     cairo_restore (cr);
1065 }
1066
1067 /* Draw the plot to the given cairo context within a user-space
1068  * rectangle from (0, 0) to (width, height). This size includes all
1069  * space for extra-plot elements (such as the title, the axis labels,
1070  * etc.)
1071  */
1072 void
1073 acre_draw (acre_t *acre, cairo_t *cr, int width, int height)
1074 {
1075     acre->cr = cr;
1076
1077     acre->width = width;
1078     acre->height = height;
1079
1080     acre->chart.width = width;
1081     acre->chart.height = height;
1082
1083     cairo_save (cr);
1084
1085     cairo_set_source_rgb (cr, 1, 1, 1);
1086
1087     _choose_colors (acre);
1088
1089     /* We compute the axis ranges before doing label layout so that we
1090      * can account for the width of the y-axis value labels. */
1091     _compute_axis_ranges (acre);
1092
1093     _draw_title_and_labels (acre);
1094
1095     /* And we recompute the axis ranges now that the title and axis
1096      * label space is all measured and accounted for. */
1097     _compute_axis_ranges (acre);
1098
1099     _draw_data (acre);
1100
1101     if (acre->num_data > 1)
1102         _draw_legend (acre);
1103
1104     _draw_frame_and_ticks (acre);
1105 }
1106
1107 /* Create a new dataset---a collection of (x, y) datapoints. A single
1108  * plot can contain multiple datasets, (see acre_add_data). */
1109 acre_data_t *
1110 acre_data_create (void)
1111 {
1112     acre_data_t *data;
1113
1114     data = xmalloc (sizeof (acre_data_t));
1115
1116     data->name = NULL;
1117
1118     data->points = NULL;
1119     data->points_size = 0;
1120     data->num_points = 0;
1121
1122     return data;
1123 }
1124
1125 /* Destroy an acre dataset. Do not call this function if the dataset
1126  * has been added to an acre_t plot with acre_add_data. */
1127 void
1128 acre_data_destroy (acre_data_t *data)
1129 {
1130     free (data->points);
1131
1132     free (data);
1133 }
1134
1135 /* Set the label for this dataset (to appear in the plot's key). */
1136 void
1137 acre_data_set_name (acre_data_t *data, const char *name)
1138 {
1139     free (data->name);
1140
1141     data->name = strdup (name);
1142 }
1143
1144 /* Add a datapoint to the given dataset. */
1145 void
1146 acre_data_add_point_2d (acre_data_t *data, double x, double y)
1147 {
1148     if (data->num_points >= data->points_size) {
1149         data->points_size *= 2;
1150         if (data->points_size == 0)
1151             data->points_size = 16;
1152         data->points = xrealloc_ab (data->points,
1153                                     data->points_size,
1154                                     sizeof (acre_data_point_2d_t));
1155     }
1156
1157     data->points[data->num_points].x = x;
1158     data->points[data->num_points].y = y;
1159
1160     if (data->num_points == 0) {
1161         data->min.x = x;
1162         data->min.y = y;
1163
1164         data->max.x = x;
1165         data->max.y = y;
1166     } else {
1167         if (x < data->min.x)
1168             data->min.x = x;
1169         if (y < data->min.y)
1170             data->min.y = y;
1171
1172         if (x > data->max.x)
1173             data->max.x = x;
1174         if (y > data->max.y)
1175             data->max.y = y;
1176     }
1177
1178     data->num_points++;
1179 }