]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Sue me: I'm not a fan of Serif
[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, PangoFontDescription *font)
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_font_description(layout, font);
79         pango_layout_set_width(layout, w * PANGO_SCALE);
80         pango_layout_set_height(layout, h * PANGO_SCALE * 0.9);
81         pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
82
83         *divenr = 0;
84         if (dive->number)
85                 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
86
87
88         tm = gmtime(&dive->when);
89         len = snprintf(buffer, sizeof(buffer),
90                 "<span size=\"large\">"
91                 "%s%s, %s %d, %d   %d:%02d"
92                 "</span>\n",
93                 divenr,
94                 weekday(tm->tm_wday),
95                 monthname(tm->tm_mon),
96                 tm->tm_mday, tm->tm_year + 1900,
97                 tm->tm_hour, tm->tm_min);
98
99         /*
100          * Leave an empty line even if no location: otherwise the notes can
101          * overrun the depth/duration information.
102          */
103         if (dive->location)
104                 len = add_quoted_string(buffer, sizeof(buffer), len, dive->location);
105         else
106                 len = add_char(buffer, sizeof(buffer), len, '\n');
107
108         if (dive->notes) {
109                 len = add_char(buffer, sizeof(buffer), len, '\n');
110                 len = add_quoted_string(buffer, sizeof(buffer), len, dive->notes);
111         }
112
113         pango_layout_set_justify(layout, 1);
114         pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
115         pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
116         pango_layout_set_markup(layout, buffer, len);
117
118         cairo_move_to(cr, 0, 0);
119         pango_cairo_show_layout(cr, layout);
120
121         /*
122          * This is still problematic: a long dive location will clash
123          * with the depth/duration information. Need to mask that or
124          * create a box or something.
125          */
126         snprintf(buffer, sizeof(buffer),
127                 "<span size=\"small\">"
128                 "Max depth: %d ft\n"
129                 "Duration: %d:%02d"
130                 "</span>",
131                 to_feet(dive->maxdepth),
132                 dive->duration.seconds / 60,
133                 dive->duration.seconds % 60);
134
135         pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
136         pango_layout_set_markup(layout, buffer, -1);
137
138         cairo_move_to(cr, 0, 0);
139         pango_cairo_show_layout(cr, layout);
140
141         g_object_unref(layout);
142 }
143
144 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
145 {
146         struct graphics_context gc = {
147                 .printer = 1,
148                 .cr = cr
149         };
150         cairo_save(cr);
151         plot(&gc, w, h, dive);
152         cairo_restore(cr);
153 }
154
155 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
156 {
157         struct dive *dive;
158
159         dive = get_dive(divenr);
160         if (!dive)
161                 return;
162         cairo_save(cr);
163         cairo_translate(cr, x, y);
164
165         /* Plus 5% on all sides */
166         cairo_translate(cr, w/20, h/20);
167         w *= 0.9; h *= 0.9;
168
169         /* We actually want to scale the text and the lines now */
170         cairo_scale(cr, 0.5, 0.5);
171
172         /* Dive plot in the upper two thirds - note the scaling */
173         show_dive_profile(dive, cr, w*2, h*1.33);
174
175         /* Dive information in the lower third */
176         cairo_translate(cr, 0, h*1.33);
177
178         show_dive_text(dive, cr, w*2, h*0.67, font);
179
180         cairo_restore(cr);
181 }
182
183 static void draw_page(GtkPrintOperation *operation,
184                         GtkPrintContext *context,
185                         gint page_nr,
186                         gpointer user_data)
187 {
188         int nr;
189         cairo_t *cr;
190         double w, h;
191         PangoFontDescription *font;
192
193         cr = gtk_print_context_get_cairo_context(context);
194         font = pango_font_description_from_string("Sans");
195
196         w = gtk_print_context_get_width(context)/2;
197         h = gtk_print_context_get_height(context)/3;
198
199         nr = page_nr*6;
200         print(nr+0, cr, 0,   0, w, h, font);
201         print(nr+1, cr, w,   0, w, h, font);
202         print(nr+2, cr, 0,   h, w, h, font);
203         print(nr+3, cr, w,   h, w, h, font);
204         print(nr+4, cr, 0, 2*h, w, h, font);
205         print(nr+5, cr, w, 2*h, w, h, font);
206
207         pango_font_description_free(font);
208 }
209
210 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
211 {
212 }
213
214 static GtkPrintSettings *settings = NULL;
215
216 void do_print(void)
217 {
218         int pages;
219         GtkPrintOperation *print;
220         GtkPrintOperationResult res;
221
222         repaint_dive();
223         print = gtk_print_operation_new();
224         if (settings != NULL)
225                 gtk_print_operation_set_print_settings(print, settings);
226         pages = (dive_table.nr + 5) / 6;
227         gtk_print_operation_set_n_pages(print, pages);
228         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
229         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
230         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
231                                          GTK_WINDOW(main_window), NULL);
232         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
233                 if (settings != NULL)
234                         g_object_unref(settings);
235                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
236         }
237         g_object_unref(print);
238 }