]> git.tdb.fi Git - ext/subsurface.git/blob - print.c
Fix dive location width problem
[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 #define FONT_NORMAL (12)
11 #define FONT_SMALL (FONT_NORMAL / 1.2)
12 #define FONT_LARGE (FONT_NORMAL * 1.2)
13
14 static void set_font(PangoLayout *layout, PangoFontDescription *font, double size, int align)
15 {
16         pango_font_description_set_size(font, size * PANGO_SCALE);
17         pango_layout_set_font_description(layout, font);
18         pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
19         pango_layout_set_alignment(layout, align);
20
21 }
22
23 /*
24  * You know what? Maybe somebody can do a real Pango layout thing.
25  * This is hacky.
26  */
27 static void show_dive_text(struct dive *dive, cairo_t *cr, double w, double h, PangoFontDescription *font)
28 {
29         int len, width, height, maxwidth, maxheight;
30         PangoLayout *layout;
31         struct tm *tm;
32         char buffer[1024], divenr[20];
33
34         maxwidth = w * PANGO_SCALE;
35         maxheight = h * PANGO_SCALE * 0.9;
36
37         layout = pango_cairo_create_layout(cr);
38         pango_layout_set_width(layout, maxwidth);
39         pango_layout_set_height(layout, maxheight);
40
41         *divenr = 0;
42         if (dive->number)
43                 snprintf(divenr, sizeof(divenr), "Dive #%d - ", dive->number);
44
45
46         tm = gmtime(&dive->when);
47         len = snprintf(buffer, sizeof(buffer),
48                 "%s%s, %s %d, %d   %d:%02d",
49                 divenr,
50                 weekday(tm->tm_wday),
51                 monthname(tm->tm_mon),
52                 tm->tm_mday, tm->tm_year + 1900,
53                 tm->tm_hour, tm->tm_min);
54
55         set_font(layout, font, FONT_LARGE, PANGO_ALIGN_LEFT);
56         pango_layout_set_text(layout, buffer, len);
57         pango_layout_get_size(layout, &width, &height);
58
59         cairo_move_to(cr, 0, 0);
60         pango_cairo_show_layout(cr, layout);
61
62         /*
63          * This is still problematic: a long dive location will clash
64          * with the depth/duration information. Need to mask that or
65          * create a box or something.
66          */
67         snprintf(buffer, sizeof(buffer),
68                 "Max depth: %d ft\n"
69                 "Duration: %d:%02d\n"
70                 "%s",
71                 to_feet(dive->maxdepth),
72                 dive->duration.seconds / 60,
73                 dive->duration.seconds % 60,
74                 dive->buddy ? :"");
75
76         set_font(layout, font, FONT_SMALL, PANGO_ALIGN_RIGHT);
77         pango_layout_set_text(layout, buffer, -1);
78
79         cairo_move_to(cr, 0, 0);
80         pango_cairo_show_layout(cr, layout);
81
82         /*
83          * Show the dive location
84          *
85          * .. or at least a space to get the size.
86          *
87          * Move down by the size of the date, and limit the
88          * width to the same width as the date string.
89          */
90         cairo_translate(cr, 0, height / (double) PANGO_SCALE);
91         maxheight -= height;
92         pango_layout_set_height(layout, 1);
93         pango_layout_set_width(layout, width);
94
95         set_font(layout, font, FONT_NORMAL, PANGO_ALIGN_LEFT);
96         pango_layout_set_text(layout, dive->location ? : " ", -1);
97
98         cairo_move_to(cr, 0, 0);
99         pango_cairo_show_layout(cr, layout);
100
101         pango_layout_get_size(layout, &width, &height);
102
103         /*
104          * Show the dive notes
105          */
106         if (dive->notes) {
107                 /* Move down by the size of the location (1.5) */
108                 height = height * 3 / 2;
109                 cairo_translate(cr, 0, height / (double) PANGO_SCALE);
110                 maxheight -= height;
111
112                 /* Use the full width and remaining height for notes */
113                 pango_layout_set_height(layout, maxheight);
114                 pango_layout_set_width(layout, maxwidth);
115                 pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
116                 pango_layout_set_justify(layout, 1);
117                 pango_layout_set_text(layout, dive->notes, -1);
118
119                 cairo_move_to(cr, 0, 0);
120                 pango_cairo_show_layout(cr, layout);
121         }
122         g_object_unref(layout);
123 }
124
125 static void show_dive_profile(struct dive *dive, cairo_t *cr, double w, double h)
126 {
127         struct graphics_context gc = {
128                 .printer = 1,
129                 .cr = cr
130         };
131         cairo_save(cr);
132         plot(&gc, w, h, dive);
133         cairo_restore(cr);
134 }
135
136 static void print(int divenr, cairo_t *cr, double x, double y, double w, double h, PangoFontDescription *font)
137 {
138         struct dive *dive;
139
140         dive = get_dive(divenr);
141         if (!dive)
142                 return;
143         cairo_save(cr);
144         cairo_translate(cr, x, y);
145
146         /* Plus 5% on all sides */
147         cairo_translate(cr, w/20, h/20);
148         w *= 0.9; h *= 0.9;
149
150         /* We actually want to scale the text and the lines now */
151         cairo_scale(cr, 0.5, 0.5);
152
153         /* Dive plot in the upper two thirds - note the scaling */
154         show_dive_profile(dive, cr, w*2, h*1.33);
155
156         /* Dive information in the lower third */
157         cairo_translate(cr, 0, h*1.33);
158
159         show_dive_text(dive, cr, w*2, h*0.67, font);
160
161         cairo_restore(cr);
162 }
163
164 static void draw_page(GtkPrintOperation *operation,
165                         GtkPrintContext *context,
166                         gint page_nr,
167                         gpointer user_data)
168 {
169         int nr;
170         cairo_t *cr;
171         double w, h;
172         PangoFontDescription *font;
173
174         cr = gtk_print_context_get_cairo_context(context);
175         font = pango_font_description_from_string("Sans");
176
177         w = gtk_print_context_get_width(context)/2;
178         h = gtk_print_context_get_height(context)/3;
179
180         nr = page_nr*6;
181         print(nr+0, cr, 0,   0, w, h, font);
182         print(nr+1, cr, w,   0, w, h, font);
183         print(nr+2, cr, 0,   h, w, h, font);
184         print(nr+3, cr, w,   h, w, h, font);
185         print(nr+4, cr, 0, 2*h, w, h, font);
186         print(nr+5, cr, w, 2*h, w, h, font);
187
188         pango_font_description_free(font);
189 }
190
191 static void begin_print(GtkPrintOperation *operation, gpointer user_data)
192 {
193 }
194
195 static GtkPrintSettings *settings = NULL;
196
197 void do_print(void)
198 {
199         int pages;
200         GtkPrintOperation *print;
201         GtkPrintOperationResult res;
202
203         repaint_dive();
204         print = gtk_print_operation_new();
205         if (settings != NULL)
206                 gtk_print_operation_set_print_settings(print, settings);
207         pages = (dive_table.nr + 5) / 6;
208         gtk_print_operation_set_n_pages(print, pages);
209         g_signal_connect(print, "begin_print", G_CALLBACK(begin_print), NULL);
210         g_signal_connect(print, "draw_page", G_CALLBACK(draw_page), NULL);
211         res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
212                                          GTK_WINDOW(main_window), NULL);
213         if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
214                 if (settings != NULL)
215                         g_object_unref(settings);
216                 settings = g_object_ref(gtk_print_operation_get_print_settings(print));
217         }
218         g_object_unref(print);
219 }