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 /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
79 * cairo will reset the face size. As such, create new face... */
80 FT_Face ft_face = NULL;//hb_ft_font_get_face (font);
85 FT_Init_FreeType (&ft_library);
87 atexit (free_ft_library);
90 FT_New_Face (ft_library,
92 font_opts->face_index,
97 /* This allows us to get some boxes at least... */
98 cairo_face = cairo_toy_font_face_create ("@cairo:sans",
99 CAIRO_FONT_SLANT_NORMAL,
100 CAIRO_FONT_WEIGHT_NORMAL);
103 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
104 cairo_matrix_t ctm, font_matrix;
105 cairo_font_options_t *font_options;
107 cairo_matrix_init_identity (&ctm);
108 cairo_matrix_init_scale (&font_matrix,
109 font_opts->font_size_x,
110 font_opts->font_size_y);
111 font_options = cairo_font_options_create ();
112 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
113 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
115 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
120 cairo_font_options_destroy (font_options);
121 cairo_font_face_destroy (cairo_face);
123 static cairo_user_data_key_t key;
124 if (cairo_scaled_font_set_user_data (scaled_font,
127 (cairo_destroy_func_t) hb_font_destroy))
128 hb_font_destroy (font);
134 helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
138 FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
141 if (FT_HAS_COLOR (ft_face))
143 cairo_ft_scaled_font_unlock_face (scaled_font);
150 struct finalize_closure_t {
151 void (*callback)(finalize_closure_t *);
152 cairo_surface_t *surface;
153 cairo_write_func_t write_func;
156 static cairo_user_data_key_t finalize_closure_key;
160 finalize_ansi (finalize_closure_t *closure)
162 cairo_status_t status;
163 status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
166 if (status != CAIRO_STATUS_SUCCESS)
167 fail (false, "Failed to write output: %s",
168 cairo_status_to_string (status));
171 static cairo_surface_t *
172 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
176 cairo_content_t content)
178 cairo_surface_t *surface;
179 int w = ceil (width);
180 int h = ceil (height);
183 case CAIRO_CONTENT_ALPHA:
184 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
187 case CAIRO_CONTENT_COLOR:
188 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
190 case CAIRO_CONTENT_COLOR_ALPHA:
191 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
194 cairo_status_t status = cairo_surface_status (surface);
195 if (status != CAIRO_STATUS_SUCCESS)
196 fail (false, "Failed to create cairo surface: %s",
197 cairo_status_to_string (status));
199 finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1);
200 ansi_closure->callback = finalize_ansi;
201 ansi_closure->surface = surface;
202 ansi_closure->write_func = write_func;
203 ansi_closure->closure = closure;
205 if (cairo_surface_set_user_data (surface,
206 &finalize_closure_key,
207 (void *) ansi_closure,
208 (cairo_destroy_func_t) g_free))
209 g_free ((void *) closure);
215 #ifdef CAIRO_HAS_PNG_FUNCTIONS
218 finalize_png (finalize_closure_t *closure)
220 cairo_status_t status;
221 status = cairo_surface_write_to_png_stream (closure->surface,
224 if (status != CAIRO_STATUS_SUCCESS)
225 fail (false, "Failed to write output: %s",
226 cairo_status_to_string (status));
229 static cairo_surface_t *
230 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
234 cairo_content_t content)
236 cairo_surface_t *surface;
237 int w = ceil (width);
238 int h = ceil (height);
241 case CAIRO_CONTENT_ALPHA:
242 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
245 case CAIRO_CONTENT_COLOR:
246 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
248 case CAIRO_CONTENT_COLOR_ALPHA:
249 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
252 cairo_status_t status = cairo_surface_status (surface);
253 if (status != CAIRO_STATUS_SUCCESS)
254 fail (false, "Failed to create cairo surface: %s",
255 cairo_status_to_string (status));
257 finalize_closure_t *png_closure = g_new0 (finalize_closure_t, 1);
258 png_closure->callback = finalize_png;
259 png_closure->surface = surface;
260 png_closure->write_func = write_func;
261 png_closure->closure = closure;
263 if (cairo_surface_set_user_data (surface,
264 &finalize_closure_key,
265 (void *) png_closure,
266 (cairo_destroy_func_t) g_free))
267 g_free ((void *) closure);
274 static cairo_status_t
275 stdio_write_func (void *closure,
276 const unsigned char *data,
279 FILE *fp = (FILE *) closure;
282 size_t ret = fwrite (data, 1, size, fp);
285 if (size && ferror (fp))
286 fail (false, "Failed to write output: %s", strerror (errno));
289 return CAIRO_STATUS_SUCCESS;
292 const char *helper_cairo_supported_formats[] =
295 #ifdef CAIRO_HAS_PNG_FUNCTIONS
298 #ifdef CAIRO_HAS_SVG_SURFACE
301 #ifdef CAIRO_HAS_PDF_SURFACE
304 #ifdef CAIRO_HAS_PS_SURFACE
314 helper_cairo_create_context (double w, double h,
315 view_options_t *view_opts,
316 output_options_t *out_opts,
317 cairo_content_t content)
319 cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
322 double height) = NULL;
323 cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
327 cairo_content_t content) = NULL;
329 const char *extension = out_opts->output_format;
332 if (isatty (fileno (out_opts->get_file_handle ())))
337 #ifdef CAIRO_HAS_PNG_FUNCTIONS
346 else if (0 == g_ascii_strcasecmp (extension, "ansi"))
347 constructor2 = _cairo_ansi_surface_create_for_stream;
348 #ifdef CAIRO_HAS_PNG_FUNCTIONS
349 else if (0 == g_ascii_strcasecmp (extension, "png"))
350 constructor2 = _cairo_png_surface_create_for_stream;
352 #ifdef CAIRO_HAS_SVG_SURFACE
353 else if (0 == g_ascii_strcasecmp (extension, "svg"))
354 constructor = cairo_svg_surface_create_for_stream;
356 #ifdef CAIRO_HAS_PDF_SURFACE
357 else if (0 == g_ascii_strcasecmp (extension, "pdf"))
358 constructor = cairo_pdf_surface_create_for_stream;
360 #ifdef CAIRO_HAS_PS_SURFACE
361 else if (0 == g_ascii_strcasecmp (extension, "ps"))
362 constructor = cairo_ps_surface_create_for_stream;
364 else if (0 == g_ascii_strcasecmp (extension, "eps"))
365 constructor = _cairo_eps_surface_create_for_stream;
370 unsigned int fr, fg, fb, fa, br, bg, bb, ba;
372 br = bg = bb = 0; ba = 255;
373 color = view_opts->back ? view_opts->back : DEFAULT_BACK;
374 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
375 fr = fg = fb = 0; fa = 255;
376 color = view_opts->fore ? view_opts->fore : DEFAULT_FORE;
377 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
379 if (content == CAIRO_CONTENT_ALPHA)
381 if (view_opts->annotate ||
382 br != bg || bg != bb ||
383 fr != fg || fg != fb)
384 content = CAIRO_CONTENT_COLOR;
387 content = CAIRO_CONTENT_COLOR_ALPHA;
389 cairo_surface_t *surface;
390 FILE *f = out_opts->get_file_handle ();
392 surface = constructor (stdio_write_func, f, w, h);
393 else if (constructor2)
394 surface = constructor2 (stdio_write_func, f, w, h, content);
396 fail (false, "Unknown output format `%s'; supported formats are: %s%s",
398 g_strjoinv ("/", const_cast<char**> (helper_cairo_supported_formats)),
399 out_opts->explicit_output_format ? "" :
400 "\nTry setting format using --output-format");
402 cairo_t *cr = cairo_create (surface);
403 content = cairo_surface_get_content (surface);
406 case CAIRO_CONTENT_ALPHA:
407 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
408 cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
410 cairo_set_source_rgba (cr, 1., 1., 1.,
411 (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
414 case CAIRO_CONTENT_COLOR:
415 case CAIRO_CONTENT_COLOR_ALPHA:
416 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
417 cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
419 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
420 cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
424 cairo_surface_destroy (surface);
429 helper_cairo_destroy_context (cairo_t *cr)
431 finalize_closure_t *closure = (finalize_closure_t *)
432 cairo_surface_get_user_data (cairo_get_target (cr),
433 &finalize_closure_key);
435 closure->callback (closure);
437 cairo_status_t status = cairo_status (cr);
438 if (status != CAIRO_STATUS_SUCCESS)
439 fail (false, "Failed: %s",
440 cairo_status_to_string (status));
446 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
449 unsigned int text_len,
451 hb_bool_t utf8_clusters)
453 memset (l, 0, sizeof (*l));
455 l->num_glyphs = hb_buffer_get_length (buffer);
456 hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (buffer, NULL);
457 hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (buffer, NULL);
458 l->glyphs = cairo_glyph_allocate (l->num_glyphs + 1);
461 l->utf8 = g_strndup (text, text_len);
462 l->utf8_len = text_len;
463 l->num_clusters = l->num_glyphs ? 1 : 0;
464 for (unsigned int i = 1; i < l->num_glyphs; i++)
465 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
467 l->clusters = cairo_text_cluster_allocate (l->num_clusters);
470 if ((l->num_glyphs && !l->glyphs) ||
471 (l->utf8_len && !l->utf8) ||
472 (l->num_clusters && !l->clusters))
478 hb_position_t x = 0, y = 0;
480 for (i = 0; i < (int) l->num_glyphs; i++)
482 l->glyphs[i].index = hb_glyph[i].codepoint;
483 l->glyphs[i].x = scalbn ((double) hb_position->x_offset + x, scale_bits);
484 l->glyphs[i].y = scalbn ((double) -hb_position->y_offset + y, scale_bits);
485 x += hb_position->x_advance;
486 y += -hb_position->y_advance;
490 l->glyphs[i].index = -1;
491 l->glyphs[i].x = scalbn ((double) x, scale_bits);
492 l->glyphs[i].y = scalbn ((double) y, scale_bits);
494 if (l->num_clusters) {
495 memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0]));
496 hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
497 l->cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
498 unsigned int cluster = 0;
499 const char *start = l->utf8, *end;
500 l->clusters[cluster].num_glyphs++;
502 for (i = l->num_glyphs - 2; i >= 0; i--) {
503 if (hb_glyph[i].cluster != hb_glyph[i+1].cluster) {
504 g_assert (hb_glyph[i].cluster > hb_glyph[i+1].cluster);
506 end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
508 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i+1].cluster);
509 l->clusters[cluster].num_bytes = end - start;
513 l->clusters[cluster].num_glyphs++;
515 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
517 for (i = 1; i < (int) l->num_glyphs; i++) {
518 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) {
519 g_assert (hb_glyph[i].cluster > hb_glyph[i-1].cluster);
521 end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
523 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i-1].cluster);
524 l->clusters[cluster].num_bytes = end - start;
528 l->clusters[cluster].num_glyphs++;
530 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;