]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Printing: use pango markup for text generation
[ext/subsurface.git] / print.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <gtk/gtk.h>
5
6 #include "dive.h"
7 #include "display.h"
8 #include "display-gtk.h"
9
10 /* Why doesn't pango/gtk have these quoting functions? */
11 static inline int add_char(char *buffer, size_t size, int len, char c)
12 {
13         if (len < size)
14                 buffer[len++] = c;
15         return len;
16 }
17
18 /* Add an escape string "atomically" - all or nothing */
19 static int add_str(char *buffer, size_t size, int len, const char *s)
20 {
21         int oldlen = len;
22         char c;
23         while ((c = *s++) != 0) {
24                 if (len >= size)
25                         return oldlen;
26                 buffer[len++] = c;
27         }
28         return len;
29 }
30
31 static int add_quoted_string(char *buffer, size_t size, int len, const char *s)
32 {
33         if (!s)
34                 return len;
35
36         /* Room for '\0' */
37         size--;
38         for (;;) {
39                 const char *escape;
40                 unsigned char c = *s++;
41                 switch(c) {
42                 default:
43                         len = add_char(buffer, size, len, c);
44                         continue;
45                 case 0:
46                         escape = "\n";
47                         break;
48                 case '&':
49                         escape = "&amp;";
50                         break;
51                 case '>':
52                         escape = "&gt;";
53                         break;
54                 case '<':
55                         escape = "&lt;";
56                         break;
57                 }
58                 len = add_str(buffer, size, len, escape);
59                 if (c)
60                         continue;
61                 buffer[len] = 0;
62                 return len;
63         }
64 }
65
66 /*
67  * You know what? Maybe somebody can do a real Pango layout thing.
68  * This is hacky.
69  */
70 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h)
71 {
72         int len;
73         PangoLayout *layout;
74         struct tm *tm;
75         char buffer[1024], divenr[20];
76
77         layout = pango_cairo_create_layout(cr);
78         pango_layout_set_width(layout, w * PANGO_SCALE);
79         pango_layout_set_height(layout, h * PANGO_SCALE * 0.9);
80         pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
81
82         *divenr = 0;
83         if (dive->number)
84                 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
85
86
87         tm = gmtime(&dive->when);
88         len = snprintf(buffer, sizeof(buffer),
89                 "<span size=\"large\">%s%s, %s %d, %d   %d:%02d</span>\n",
90                 divenr,
91                 weekday(tm->tm_wday),
92                 monthname(tm->tm_mon),
93                 tm->tm_mday, tm->tm_year + 1900,
94                 tm->tm_hour, tm->tm_min);
95
96         len = add_quoted_string(buffer, sizeof(buffer), len, dive->location);
97         len = add_quoted_string(buffer, sizeof(buffer), len, dive->notes);
98
99         pango_layout_set_markup(layout, buffer, -1);
100
101         cairo_move_to(cr, 0, 0);
102         pango_cairo_show_layout(cr, layout);
103
104         g_object_unref(layout);
105 }
106
107 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
108 {
109         struct graphics_context gc = {
110                 .printer = 1,
111                 .cr = cr
112         };
113         plot(&gc, w, h, dive);
114 }
115
116 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h)
117 {
118         struct dive *dive;
119
120         dive = get_dive(divenr);
121         if (!dive)
122                 return;
123         cairo_save(cr);
124         cairo_translate(cr, x, y);
125
126         /* Plus 5% on all sides */
127         cairo_translate(cr, w/20, h/20);
128         w *= 0.9; h *= 0.9;
129
130         /* We actually want to scale the text and the lines now */
131         cairo_scale(cr, 0.5, 0.5);
132
133         /* Dive plot in the upper two thirds - note the scaling */
134         show_dive_profile(dive, cr, w*2, h*1.33);
135
136         /* Dive information in the lower third */
137         cairo_translate(cr, 0, h*1.33);
138
139         show_dive_text(dive, cr, w*2, h*0.67);
140
141         cairo_restore(cr);
142 }
143
144 static void draw_page(GtkPrintOperation *operation,
145                         GtkPrintContext *context,
146                         gint page_nr,
147                         gpointer user_data)
148 {
149         int nr;
150         cairo_t *cr;
151         double w, h;
152
153         cr = gtk_print_context_get_cairo_context(context);
154
155         w = gtk_print_context_get_width(context)/2;
156         h = gtk_print_context_get_height(context)/3;
157
158         nr = page_nr*6;
159         print(nr+0, cr, 0,   0, w, h);
160         print(nr+1, cr, w,   0, w, h);
161         print(nr+2, cr, 0,   h, w, h);
162         print(nr+3, cr, w,   h, w, h);
163         print(nr+4, cr, 0, 2*h, w, h);
164         print(nr+5, cr, w, 2*h, w, h);
165 }
166
167 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
168 {
169 }
170
171 static GtkPrintSettings *settings = NULL;
172
173 void do_print(void)
174 {
175         int pages;
176         GtkPrintOperation *print;
177         GtkPrintOperationResult res;
178
179         repaint_dive();
180         print = gtk_print_operation_new();
181         if (settings != NULL)
182                 gtk_print_operation_set_print_settings(print, settings);
183         pages = (dive_table.nr + 5) / 6;
184         gtk_print_operation_set_n_pages(print, pages);
185         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
186         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
187         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
188                                          GTK_WINDOW(main_window), NULL);
189         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
190                 if (settings != NULL)
191                         g_object_unref(settings);
192                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
193         }
194         g_object_unref(print);
195 }