bc44e8e886724ae00fc1562000c9fd0fa7e4dc80
[platform/upstream/harfbuzz.git] / util / helper-cairo.cc
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #include "helper-cairo.hh"
28
29 #include <cairo-ft.h>
30 #include <hb-ft.h>
31
32 #include "helper-cairo-ansi.hh"
33 #ifdef CAIRO_HAS_SVG_SURFACE
34 #  include <cairo-svg.h>
35 #endif
36 #ifdef CAIRO_HAS_PDF_SURFACE
37 #  include <cairo-pdf.h>
38 #endif
39 #ifdef CAIRO_HAS_PS_SURFACE
40 #  include <cairo-ps.h>
41 #  if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
42 #    define HAS_EPS 1
43
44 static cairo_surface_t *
45 _cairo_eps_surface_create_for_stream (cairo_write_func_t  write_func,
46                                       void               *closure,
47                                       double              width,
48                                       double              height)
49 {
50   cairo_surface_t *surface;
51
52   surface = cairo_ps_surface_create_for_stream (write_func, closure, width, height);
53   cairo_ps_surface_set_eps (surface, true);
54
55   return surface;
56 }
57
58 #  else
59 #    undef HAS_EPS
60 #  endif
61 #endif
62
63 cairo_scaled_font_t *
64 helper_cairo_create_scaled_font (const font_options_t *font_opts,
65                                  double font_size)
66 {
67   hb_font_t *font = hb_font_reference (font_opts->get_font ());
68
69   cairo_font_face_t *cairo_face;
70   FT_Face ft_face = hb_ft_font_get_face (font);
71   if (!ft_face)
72     /* This allows us to get some boxes at least... */
73     cairo_face = cairo_toy_font_face_create ("@cairo:sans",
74                                              CAIRO_FONT_SLANT_NORMAL,
75                                              CAIRO_FONT_WEIGHT_NORMAL);
76   else
77     cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
78   cairo_matrix_t ctm, font_matrix;
79   cairo_font_options_t *font_options;
80
81   cairo_matrix_init_identity (&ctm);
82   cairo_matrix_init_scale (&font_matrix,
83                            font_size, font_size);
84   font_options = cairo_font_options_create ();
85   cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
86   cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
87
88   cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
89                                                                &font_matrix,
90                                                                &ctm,
91                                                                font_options);
92
93   cairo_font_options_destroy (font_options);
94   cairo_font_face_destroy (cairo_face);
95
96   static cairo_user_data_key_t key;
97   if (cairo_scaled_font_set_user_data (scaled_font,
98                                        &key,
99                                        (void *) font,
100                                        (cairo_destroy_func_t) hb_font_destroy))
101     hb_font_destroy (font);
102
103   return scaled_font;
104 }
105
106
107 struct finalize_closure_t {
108   void (*callback)(finalize_closure_t *);
109   cairo_surface_t *surface;
110   cairo_write_func_t write_func;
111   void *closure;
112 };
113 static cairo_user_data_key_t finalize_closure_key;
114
115
116 static void
117 finalize_ansi (finalize_closure_t *closure)
118 {
119   cairo_status_t status;
120   status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
121                                                       closure->write_func,
122                                                       closure->closure);
123   if (status != CAIRO_STATUS_SUCCESS)
124     fail (false, "Failed to write output: %s",
125           cairo_status_to_string (status));
126 }
127
128 static cairo_surface_t *
129 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
130                                        void *closure,
131                                        double width,
132                                        double height,
133                                        cairo_content_t content)
134 {
135   cairo_surface_t *surface;
136   int w = ceil (width);
137   int h = ceil (height);
138
139   switch (content) {
140     case CAIRO_CONTENT_ALPHA:
141       surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
142       break;
143     default:
144     case CAIRO_CONTENT_COLOR:
145       surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
146       break;
147     case CAIRO_CONTENT_COLOR_ALPHA:
148       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
149       break;
150   }
151   cairo_status_t status = cairo_surface_status (surface);
152   if (status != CAIRO_STATUS_SUCCESS)
153     fail (false, "Failed to create cairo surface: %s",
154           cairo_status_to_string (status));
155
156   finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1);
157   ansi_closure->callback = finalize_ansi;
158   ansi_closure->surface = surface;
159   ansi_closure->write_func = write_func;
160   ansi_closure->closure = closure;
161
162   if (cairo_surface_set_user_data (surface,
163                                    &finalize_closure_key,
164                                    (void *) ansi_closure,
165                                    (cairo_destroy_func_t) g_free))
166     g_free ((void *) closure);
167
168   return surface;
169 }
170
171
172 #ifdef CAIRO_HAS_PNG_FUNCTIONS
173
174 static void
175 finalize_png (finalize_closure_t *closure)
176 {
177   cairo_status_t status;
178   status = cairo_surface_write_to_png_stream (closure->surface,
179                                               closure->write_func,
180                                               closure->closure);
181   if (status != CAIRO_STATUS_SUCCESS)
182     fail (false, "Failed to write output: %s",
183           cairo_status_to_string (status));
184 }
185
186 static cairo_surface_t *
187 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
188                                       void *closure,
189                                       double width,
190                                       double height,
191                                       cairo_content_t content)
192 {
193   cairo_surface_t *surface;
194   int w = ceil (width);
195   int h = ceil (height);
196
197   switch (content) {
198     case CAIRO_CONTENT_ALPHA:
199       surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
200       break;
201     default:
202     case CAIRO_CONTENT_COLOR:
203       surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
204       break;
205     case CAIRO_CONTENT_COLOR_ALPHA:
206       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
207       break;
208   }
209   cairo_status_t status = cairo_surface_status (surface);
210   if (status != CAIRO_STATUS_SUCCESS)
211     fail (false, "Failed to create cairo surface: %s",
212           cairo_status_to_string (status));
213
214   finalize_closure_t *png_closure = g_new0 (finalize_closure_t, 1);
215   png_closure->callback = finalize_png;
216   png_closure->surface = surface;
217   png_closure->write_func = write_func;
218   png_closure->closure = closure;
219
220   if (cairo_surface_set_user_data (surface,
221                                    &finalize_closure_key,
222                                    (void *) png_closure,
223                                    (cairo_destroy_func_t) g_free))
224     g_free ((void *) closure);
225
226   return surface;
227 }
228
229 #endif
230
231 static cairo_status_t
232 stdio_write_func (void                *closure,
233                   const unsigned char *data,
234                   unsigned int         size)
235 {
236   FILE *fp = (FILE *) closure;
237
238   while (size) {
239     size_t ret = fwrite (data, 1, size, fp);
240     size -= ret;
241     data += ret;
242     if (size && ferror (fp))
243       fail (false, "Failed to write output: %s", strerror (errno));
244   }
245
246   return CAIRO_STATUS_SUCCESS;
247 }
248
249 const char helper_cairo_supported_formats[] =
250   "ansi"
251   #ifdef CAIRO_HAS_PNG_FUNCTIONS
252   "/png"
253   #endif
254   #ifdef CAIRO_HAS_SVG_SURFACE
255   "/svg"
256   #endif
257   #ifdef CAIRO_HAS_PDF_SURFACE
258   "/pdf"
259   #endif
260   #ifdef CAIRO_HAS_PS_SURFACE
261   "/ps"
262    #ifdef HAS_EPS
263     "/eps"
264    #endif
265   #endif
266 ;
267
268 cairo_t *
269 helper_cairo_create_context (double w, double h,
270                              view_options_t *view_opts,
271                              output_options_t *out_opts)
272 {
273   cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
274                                    void *closure,
275                                    double width,
276                                    double height) = NULL;
277   cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
278                                     void *closure,
279                                     double width,
280                                     double height,
281                                     cairo_content_t content) = NULL;
282
283   const char *extension = out_opts->output_format;
284   if (!extension) {
285 #if HAVE_ISATTY
286     if (isatty (fileno (out_opts->get_file_handle ())))
287       extension = "ansi";
288     else
289 #endif
290     {
291 #ifdef CAIRO_HAS_PNG_FUNCTIONS
292       extension = "png";
293 #else
294       extension = "ansi";
295 #endif
296     }
297   }
298   if (0)
299     ;
300     else if (0 == strcasecmp (extension, "ansi"))
301       constructor2 = _cairo_ansi_surface_create_for_stream;
302   #ifdef CAIRO_HAS_PNG_FUNCTIONS
303     else if (0 == strcasecmp (extension, "png"))
304       constructor2 = _cairo_png_surface_create_for_stream;
305   #endif
306   #ifdef CAIRO_HAS_SVG_SURFACE
307     else if (0 == strcasecmp (extension, "svg"))
308       constructor = cairo_svg_surface_create_for_stream;
309   #endif
310   #ifdef CAIRO_HAS_PDF_SURFACE
311     else if (0 == strcasecmp (extension, "pdf"))
312       constructor = cairo_pdf_surface_create_for_stream;
313   #endif
314   #ifdef CAIRO_HAS_PS_SURFACE
315     else if (0 == strcasecmp (extension, "ps"))
316       constructor = cairo_ps_surface_create_for_stream;
317    #ifdef HAS_EPS
318     else if (0 == strcasecmp (extension, "eps"))
319       constructor = _cairo_eps_surface_create_for_stream;
320    #endif
321   #endif
322
323
324   unsigned int fr, fg, fb, fa, br, bg, bb, ba;
325   br = bg = bb = 0; ba = 255;
326   sscanf (view_opts->back + (*view_opts->back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
327   fr = fg = fb = 0; fa = 255;
328   sscanf (view_opts->fore + (*view_opts->fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
329
330   cairo_content_t content;
331   if (!view_opts->annotate && ba == 255 && br == bg && bg == bb && fr == fg && fg == fb)
332     content = CAIRO_CONTENT_ALPHA;
333   else if (ba == 255)
334     content = CAIRO_CONTENT_COLOR;
335   else
336     content = CAIRO_CONTENT_COLOR_ALPHA;
337
338   cairo_surface_t *surface;
339   FILE *f = out_opts->get_file_handle ();
340   if (constructor)
341     surface = constructor (stdio_write_func, f, w, h);
342   else if (constructor2)
343     surface = constructor2 (stdio_write_func, f, w, h, content);
344   else
345     fail (false, "Unknown output format `%s'; supported formats are: %s%s",
346           extension, helper_cairo_supported_formats,
347           out_opts->explicit_output_format ? "" :
348           "\nTry setting format using --output-format");
349
350   cairo_t *cr = cairo_create (surface);
351   content = cairo_surface_get_content (surface);
352
353   switch (content) {
354     case CAIRO_CONTENT_ALPHA:
355       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
356       cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
357       cairo_paint (cr);
358       cairo_set_source_rgba (cr, 1., 1., 1.,
359                              (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
360       break;
361     default:
362     case CAIRO_CONTENT_COLOR:
363     case CAIRO_CONTENT_COLOR_ALPHA:
364       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
365       cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
366       cairo_paint (cr);
367       cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
368       cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
369       break;
370   }
371
372   cairo_surface_destroy (surface);
373   return cr;
374 }
375
376 void
377 helper_cairo_destroy_context (cairo_t *cr)
378 {
379   finalize_closure_t *closure = (finalize_closure_t *)
380                                 cairo_surface_get_user_data (cairo_get_target (cr),
381                                                              &finalize_closure_key);
382   if (closure)
383     closure->callback (closure);
384
385   cairo_status_t status = cairo_status (cr);
386   if (status != CAIRO_STATUS_SUCCESS)
387     fail (false, "Failed: %s",
388           cairo_status_to_string (status));
389   cairo_destroy (cr);
390 }
391
392
393 void
394 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
395                                hb_buffer_t         *buffer,
396                                const char          *text,
397                                unsigned int         text_len,
398                                double               scale,
399                                hb_bool_t            utf8_clusters)
400 {
401   memset (l, 0, sizeof (*l));
402
403   l->num_glyphs = hb_buffer_get_length (buffer);
404   hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (buffer, NULL);
405   hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (buffer, NULL);
406   l->glyphs = cairo_glyph_allocate (l->num_glyphs + 1);
407
408   if (text) {
409     l->utf8 = g_strndup (text, text_len);
410     l->utf8_len = text_len;
411     l->num_clusters = l->num_glyphs ? 1 : 0;
412     for (unsigned int i = 1; i < l->num_glyphs; i++)
413       if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
414         l->num_clusters++;
415     l->clusters = cairo_text_cluster_allocate (l->num_clusters);
416   }
417
418   if ((l->num_glyphs && !l->glyphs) ||
419       (l->utf8_len && !l->utf8) ||
420       (l->num_clusters && !l->clusters))
421   {
422     l->finish ();
423     return;
424   }
425
426   hb_position_t x = 0, y = 0;
427   int i;
428   for (i = 0; i < (int) l->num_glyphs; i++)
429   {
430     l->glyphs[i].index = hb_glyph[i].codepoint;
431     l->glyphs[i].x = ( hb_position->x_offset + x) * scale;
432     l->glyphs[i].y = (-hb_position->y_offset + y) * scale;
433     x +=  hb_position->x_advance;
434     y += -hb_position->y_advance;
435
436     hb_position++;
437   }
438   l->glyphs[i].index = -1;
439   l->glyphs[i].x = x * scale;
440   l->glyphs[i].y = y * scale;
441
442   if (l->num_clusters) {
443     memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0]));
444     hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
445     l->cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
446     unsigned int cluster = 0;
447     const char *start = l->utf8, *end;
448     l->clusters[cluster].num_glyphs++;
449     if (backward) {
450       for (i = l->num_glyphs - 2; i >= 0; i--) {
451         if (hb_glyph[i].cluster != hb_glyph[i+1].cluster) {
452           g_assert (hb_glyph[i].cluster > hb_glyph[i+1].cluster);
453           if (utf8_clusters)
454             end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
455           else
456             end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i+1].cluster);
457           l->clusters[cluster].num_bytes = end - start;
458           start = end;
459           cluster++;
460         }
461         l->clusters[cluster].num_glyphs++;
462       }
463       l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
464     } else {
465       for (i = 1; i < (int) l->num_glyphs; i++) {
466         if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) {
467           g_assert (hb_glyph[i].cluster > hb_glyph[i-1].cluster);
468           if (utf8_clusters)
469             end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
470           else
471             end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i-1].cluster);
472           l->clusters[cluster].num_bytes = end - start;
473           start = end;
474           cluster++;
475         }
476         l->clusters[cluster].num_glyphs++;
477       }
478       l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
479     }
480   }
481 }