2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #include "helper-cairo.hh"
32 #include "helper-cairo-ansi.hh"
33 #ifdef CAIRO_HAS_SVG_SURFACE
34 # include <cairo-svg.h>
36 #ifdef CAIRO_HAS_PDF_SURFACE
37 # include <cairo-pdf.h>
39 #ifdef CAIRO_HAS_PS_SURFACE
40 # include <cairo-ps.h>
41 # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
44 static cairo_surface_t *
45 _cairo_eps_surface_create_for_stream (cairo_write_func_t write_func,
50 cairo_surface_t *surface;
52 surface = cairo_ps_surface_create_for_stream (write_func, closure, width, height);
53 cairo_ps_surface_set_eps (surface, true);
64 static FT_Library ft_library;
67 void free_ft_library (void)
69 FT_Done_FreeType (ft_library);
73 helper_cairo_create_scaled_font (const font_options_t *font_opts)
75 hb_font_t *font = hb_font_reference (font_opts->get_font ());
77 cairo_font_face_t *cairo_face;
78 FT_Face ft_face = hb_ft_font_get_face (font);
83 FT_Init_FreeType (&ft_library);
85 atexit (free_ft_library);
88 FT_New_Face (ft_library,
90 font_opts->face_index,
95 /* This allows us to get some boxes at least... */
96 cairo_face = cairo_toy_font_face_create ("@cairo:sans",
97 CAIRO_FONT_SLANT_NORMAL,
98 CAIRO_FONT_WEIGHT_NORMAL);
101 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
102 cairo_matrix_t ctm, font_matrix;
103 cairo_font_options_t *font_options;
105 cairo_matrix_init_identity (&ctm);
106 cairo_matrix_init_scale (&font_matrix,
107 font_opts->font_size_x,
108 font_opts->font_size_y);
109 font_options = cairo_font_options_create ();
110 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
111 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
113 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
118 cairo_font_options_destroy (font_options);
119 cairo_font_face_destroy (cairo_face);
121 static cairo_user_data_key_t key;
122 if (cairo_scaled_font_set_user_data (scaled_font,
125 (cairo_destroy_func_t) hb_font_destroy))
126 hb_font_destroy (font);
132 struct finalize_closure_t {
133 void (*callback)(finalize_closure_t *);
134 cairo_surface_t *surface;
135 cairo_write_func_t write_func;
138 static cairo_user_data_key_t finalize_closure_key;
142 finalize_ansi (finalize_closure_t *closure)
144 cairo_status_t status;
145 status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
148 if (status != CAIRO_STATUS_SUCCESS)
149 fail (false, "Failed to write output: %s",
150 cairo_status_to_string (status));
153 static cairo_surface_t *
154 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
158 cairo_content_t content)
160 cairo_surface_t *surface;
161 int w = ceil (width);
162 int h = ceil (height);
165 case CAIRO_CONTENT_ALPHA:
166 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
169 case CAIRO_CONTENT_COLOR:
170 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
172 case CAIRO_CONTENT_COLOR_ALPHA:
173 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
176 cairo_status_t status = cairo_surface_status (surface);
177 if (status != CAIRO_STATUS_SUCCESS)
178 fail (false, "Failed to create cairo surface: %s",
179 cairo_status_to_string (status));
181 finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1);
182 ansi_closure->callback = finalize_ansi;
183 ansi_closure->surface = surface;
184 ansi_closure->write_func = write_func;
185 ansi_closure->closure = closure;
187 if (cairo_surface_set_user_data (surface,
188 &finalize_closure_key,
189 (void *) ansi_closure,
190 (cairo_destroy_func_t) g_free))
191 g_free ((void *) closure);
197 #ifdef CAIRO_HAS_PNG_FUNCTIONS
200 finalize_png (finalize_closure_t *closure)
202 cairo_status_t status;
203 status = cairo_surface_write_to_png_stream (closure->surface,
206 if (status != CAIRO_STATUS_SUCCESS)
207 fail (false, "Failed to write output: %s",
208 cairo_status_to_string (status));
211 static cairo_surface_t *
212 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
216 cairo_content_t content)
218 cairo_surface_t *surface;
219 int w = ceil (width);
220 int h = ceil (height);
223 case CAIRO_CONTENT_ALPHA:
224 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
227 case CAIRO_CONTENT_COLOR:
228 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
230 case CAIRO_CONTENT_COLOR_ALPHA:
231 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
234 cairo_status_t status = cairo_surface_status (surface);
235 if (status != CAIRO_STATUS_SUCCESS)
236 fail (false, "Failed to create cairo surface: %s",
237 cairo_status_to_string (status));
239 finalize_closure_t *png_closure = g_new0 (finalize_closure_t, 1);
240 png_closure->callback = finalize_png;
241 png_closure->surface = surface;
242 png_closure->write_func = write_func;
243 png_closure->closure = closure;
245 if (cairo_surface_set_user_data (surface,
246 &finalize_closure_key,
247 (void *) png_closure,
248 (cairo_destroy_func_t) g_free))
249 g_free ((void *) closure);
256 static cairo_status_t
257 stdio_write_func (void *closure,
258 const unsigned char *data,
261 FILE *fp = (FILE *) closure;
264 size_t ret = fwrite (data, 1, size, fp);
267 if (size && ferror (fp))
268 fail (false, "Failed to write output: %s", strerror (errno));
271 return CAIRO_STATUS_SUCCESS;
274 const char *helper_cairo_supported_formats[] =
277 #ifdef CAIRO_HAS_PNG_FUNCTIONS
280 #ifdef CAIRO_HAS_SVG_SURFACE
283 #ifdef CAIRO_HAS_PDF_SURFACE
286 #ifdef CAIRO_HAS_PS_SURFACE
296 helper_cairo_create_context (double w, double h,
297 view_options_t *view_opts,
298 output_options_t *out_opts)
300 cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
303 double height) = NULL;
304 cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
308 cairo_content_t content) = NULL;
310 const char *extension = out_opts->output_format;
313 if (isatty (fileno (out_opts->get_file_handle ())))
318 #ifdef CAIRO_HAS_PNG_FUNCTIONS
327 else if (0 == strcasecmp (extension, "ansi"))
328 constructor2 = _cairo_ansi_surface_create_for_stream;
329 #ifdef CAIRO_HAS_PNG_FUNCTIONS
330 else if (0 == strcasecmp (extension, "png"))
331 constructor2 = _cairo_png_surface_create_for_stream;
333 #ifdef CAIRO_HAS_SVG_SURFACE
334 else if (0 == strcasecmp (extension, "svg"))
335 constructor = cairo_svg_surface_create_for_stream;
337 #ifdef CAIRO_HAS_PDF_SURFACE
338 else if (0 == strcasecmp (extension, "pdf"))
339 constructor = cairo_pdf_surface_create_for_stream;
341 #ifdef CAIRO_HAS_PS_SURFACE
342 else if (0 == strcasecmp (extension, "ps"))
343 constructor = cairo_ps_surface_create_for_stream;
345 else if (0 == strcasecmp (extension, "eps"))
346 constructor = _cairo_eps_surface_create_for_stream;
351 unsigned int fr, fg, fb, fa, br, bg, bb, ba;
352 br = bg = bb = 0; ba = 255;
353 sscanf (view_opts->back + (*view_opts->back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
354 fr = fg = fb = 0; fa = 255;
355 sscanf (view_opts->fore + (*view_opts->fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
357 cairo_content_t content;
358 if (!view_opts->annotate && ba == 255 && br == bg && bg == bb && fr == fg && fg == fb)
359 content = CAIRO_CONTENT_ALPHA;
361 content = CAIRO_CONTENT_COLOR;
363 content = CAIRO_CONTENT_COLOR_ALPHA;
365 cairo_surface_t *surface;
366 FILE *f = out_opts->get_file_handle ();
368 surface = constructor (stdio_write_func, f, w, h);
369 else if (constructor2)
370 surface = constructor2 (stdio_write_func, f, w, h, content);
372 fail (false, "Unknown output format `%s'; supported formats are: %s%s",
374 g_strjoinv ("/", const_cast<char**> (helper_cairo_supported_formats)),
375 out_opts->explicit_output_format ? "" :
376 "\nTry setting format using --output-format");
378 cairo_t *cr = cairo_create (surface);
379 content = cairo_surface_get_content (surface);
382 case CAIRO_CONTENT_ALPHA:
383 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
384 cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
386 cairo_set_source_rgba (cr, 1., 1., 1.,
387 (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
390 case CAIRO_CONTENT_COLOR:
391 case CAIRO_CONTENT_COLOR_ALPHA:
392 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
393 cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
395 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
396 cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
400 cairo_surface_destroy (surface);
405 helper_cairo_destroy_context (cairo_t *cr)
407 finalize_closure_t *closure = (finalize_closure_t *)
408 cairo_surface_get_user_data (cairo_get_target (cr),
409 &finalize_closure_key);
411 closure->callback (closure);
413 cairo_status_t status = cairo_status (cr);
414 if (status != CAIRO_STATUS_SUCCESS)
415 fail (false, "Failed: %s",
416 cairo_status_to_string (status));
422 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
425 unsigned int text_len,
427 hb_bool_t utf8_clusters)
429 memset (l, 0, sizeof (*l));
431 l->num_glyphs = hb_buffer_get_length (buffer);
432 hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (buffer, NULL);
433 hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (buffer, NULL);
434 l->glyphs = cairo_glyph_allocate (l->num_glyphs + 1);
437 l->utf8 = g_strndup (text, text_len);
438 l->utf8_len = text_len;
439 l->num_clusters = l->num_glyphs ? 1 : 0;
440 for (unsigned int i = 1; i < l->num_glyphs; i++)
441 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
443 l->clusters = cairo_text_cluster_allocate (l->num_clusters);
446 if ((l->num_glyphs && !l->glyphs) ||
447 (l->utf8_len && !l->utf8) ||
448 (l->num_clusters && !l->clusters))
454 hb_position_t x = 0, y = 0;
456 for (i = 0; i < (int) l->num_glyphs; i++)
458 l->glyphs[i].index = hb_glyph[i].codepoint;
459 l->glyphs[i].x = scalbn ( hb_position->x_offset + x, scale_bits);
460 l->glyphs[i].y = scalbn (-hb_position->y_offset + y, scale_bits);
461 x += hb_position->x_advance;
462 y += -hb_position->y_advance;
466 l->glyphs[i].index = -1;
467 l->glyphs[i].x = scalbn (x, scale_bits);
468 l->glyphs[i].y = scalbn (y, scale_bits);
470 if (l->num_clusters) {
471 memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0]));
472 hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
473 l->cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
474 unsigned int cluster = 0;
475 const char *start = l->utf8, *end;
476 l->clusters[cluster].num_glyphs++;
478 for (i = l->num_glyphs - 2; i >= 0; i--) {
479 if (hb_glyph[i].cluster != hb_glyph[i+1].cluster) {
480 g_assert (hb_glyph[i].cluster > hb_glyph[i+1].cluster);
482 end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
484 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i+1].cluster);
485 l->clusters[cluster].num_bytes = end - start;
489 l->clusters[cluster].num_glyphs++;
491 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
493 for (i = 1; i < (int) l->num_glyphs; i++) {
494 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) {
495 g_assert (hb_glyph[i].cluster > hb_glyph[i-1].cluster);
497 end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
499 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i-1].cluster);
500 l->clusters[cluster].num_bytes = end - start;
504 l->clusters[cluster].num_glyphs++;
506 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;