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"
31 #include FT_MULTIPLE_MASTERS_H
33 #include "helper-cairo-ansi.hh"
34 #ifdef CAIRO_HAS_SVG_SURFACE
35 # include <cairo-svg.h>
37 #ifdef CAIRO_HAS_PDF_SURFACE
38 # include <cairo-pdf.h>
40 #ifdef CAIRO_HAS_PS_SURFACE
41 # include <cairo-ps.h>
42 # if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
45 static cairo_surface_t *
46 _cairo_eps_surface_create_for_stream (cairo_write_func_t write_func,
51 cairo_surface_t *surface;
53 surface = cairo_ps_surface_create_for_stream (write_func, closure, width, height);
54 cairo_ps_surface_set_eps (surface, true);
65 static FT_Library ft_library;
69 void free_ft_library ()
71 FT_Done_FreeType (ft_library);
76 helper_cairo_create_scaled_font (const font_options_t *font_opts)
78 hb_font_t *font = hb_font_reference (font_opts->get_font ());
80 cairo_font_face_t *cairo_face;
81 /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
82 * cairo will reset the face size. As such, create new face...
83 * TODO Perhaps add API to hb-ft to encapsulate this code. */
84 FT_Face ft_face = nullptr;//hb_ft_font_get_face (font);
89 FT_Init_FreeType (&ft_library);
91 atexit (free_ft_library);
95 unsigned int blob_length;
96 const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length);
98 if (FT_New_Memory_Face (ft_library,
99 (const FT_Byte *) blob_data,
101 font_opts->face_index,
103 fail (false, "FT_New_Memory_Face fail");
107 /* This allows us to get some boxes at least... */
108 cairo_face = cairo_toy_font_face_create ("@cairo:sans",
109 CAIRO_FONT_SLANT_NORMAL,
110 CAIRO_FONT_WEIGHT_NORMAL);
114 #ifdef HAVE_FT_SET_VAR_BLEND_COORDINATES
115 unsigned int num_coords;
116 const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
119 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
122 for (unsigned int i = 0; i < num_coords; i++)
123 ft_coords[i] = coords[i] << 2;
124 FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
130 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags);
132 cairo_matrix_t ctm, font_matrix;
133 cairo_font_options_t *font_options;
135 cairo_matrix_init_identity (&ctm);
136 cairo_matrix_init_scale (&font_matrix,
137 font_opts->font_size_x,
138 font_opts->font_size_y);
139 font_options = cairo_font_options_create ();
140 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
141 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
143 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
148 cairo_font_options_destroy (font_options);
149 cairo_font_face_destroy (cairo_face);
151 static cairo_user_data_key_t key;
152 if (cairo_scaled_font_set_user_data (scaled_font,
155 (cairo_destroy_func_t) hb_font_destroy))
156 hb_font_destroy (font);
162 helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
166 FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
169 if (FT_HAS_COLOR (ft_face))
171 cairo_ft_scaled_font_unlock_face (scaled_font);
178 struct finalize_closure_t {
179 void (*callback)(finalize_closure_t *);
180 cairo_surface_t *surface;
181 cairo_write_func_t write_func;
184 static cairo_user_data_key_t finalize_closure_key;
188 finalize_ansi (finalize_closure_t *closure)
190 cairo_status_t status;
191 status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
194 if (status != CAIRO_STATUS_SUCCESS)
195 fail (false, "Failed to write output: %s",
196 cairo_status_to_string (status));
199 static cairo_surface_t *
200 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
204 cairo_content_t content)
206 cairo_surface_t *surface;
207 int w = ceil (width);
208 int h = ceil (height);
211 case CAIRO_CONTENT_ALPHA:
212 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
215 case CAIRO_CONTENT_COLOR:
216 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
218 case CAIRO_CONTENT_COLOR_ALPHA:
219 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
222 cairo_status_t status = cairo_surface_status (surface);
223 if (status != CAIRO_STATUS_SUCCESS)
224 fail (false, "Failed to create cairo surface: %s",
225 cairo_status_to_string (status));
227 finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1);
228 ansi_closure->callback = finalize_ansi;
229 ansi_closure->surface = surface;
230 ansi_closure->write_func = write_func;
231 ansi_closure->closure = closure;
233 if (cairo_surface_set_user_data (surface,
234 &finalize_closure_key,
235 (void *) ansi_closure,
236 (cairo_destroy_func_t) g_free))
237 g_free ((void *) closure);
243 #ifdef CAIRO_HAS_PNG_FUNCTIONS
246 finalize_png (finalize_closure_t *closure)
248 cairo_status_t status;
249 status = cairo_surface_write_to_png_stream (closure->surface,
252 if (status != CAIRO_STATUS_SUCCESS)
253 fail (false, "Failed to write output: %s",
254 cairo_status_to_string (status));
257 static cairo_surface_t *
258 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
262 cairo_content_t content)
264 cairo_surface_t *surface;
265 int w = ceil (width);
266 int h = ceil (height);
269 case CAIRO_CONTENT_ALPHA:
270 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
273 case CAIRO_CONTENT_COLOR:
274 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
276 case CAIRO_CONTENT_COLOR_ALPHA:
277 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
280 cairo_status_t status = cairo_surface_status (surface);
281 if (status != CAIRO_STATUS_SUCCESS)
282 fail (false, "Failed to create cairo surface: %s",
283 cairo_status_to_string (status));
285 finalize_closure_t *png_closure = g_new0 (finalize_closure_t, 1);
286 png_closure->callback = finalize_png;
287 png_closure->surface = surface;
288 png_closure->write_func = write_func;
289 png_closure->closure = closure;
291 if (cairo_surface_set_user_data (surface,
292 &finalize_closure_key,
293 (void *) png_closure,
294 (cairo_destroy_func_t) g_free))
295 g_free ((void *) closure);
302 static cairo_status_t
303 stdio_write_func (void *closure,
304 const unsigned char *data,
307 FILE *fp = (FILE *) closure;
310 size_t ret = fwrite (data, 1, size, fp);
313 if (size && ferror (fp))
314 fail (false, "Failed to write output: %s", strerror (errno));
317 return CAIRO_STATUS_SUCCESS;
320 const char *helper_cairo_supported_formats[] =
323 #ifdef CAIRO_HAS_PNG_FUNCTIONS
326 #ifdef CAIRO_HAS_SVG_SURFACE
329 #ifdef CAIRO_HAS_PDF_SURFACE
332 #ifdef CAIRO_HAS_PS_SURFACE
342 helper_cairo_create_context (double w, double h,
343 view_options_t *view_opts,
344 output_options_t *out_opts,
345 cairo_content_t content)
347 cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
350 double height) = nullptr;
351 cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
355 cairo_content_t content) = nullptr;
357 const char *extension = out_opts->output_format;
360 if (isatty (fileno (out_opts->get_file_handle ())))
365 #ifdef CAIRO_HAS_PNG_FUNCTIONS
374 else if (0 == g_ascii_strcasecmp (extension, "ansi"))
375 constructor2 = _cairo_ansi_surface_create_for_stream;
376 #ifdef CAIRO_HAS_PNG_FUNCTIONS
377 else if (0 == g_ascii_strcasecmp (extension, "png"))
378 constructor2 = _cairo_png_surface_create_for_stream;
380 #ifdef CAIRO_HAS_SVG_SURFACE
381 else if (0 == g_ascii_strcasecmp (extension, "svg"))
382 constructor = cairo_svg_surface_create_for_stream;
384 #ifdef CAIRO_HAS_PDF_SURFACE
385 else if (0 == g_ascii_strcasecmp (extension, "pdf"))
386 constructor = cairo_pdf_surface_create_for_stream;
388 #ifdef CAIRO_HAS_PS_SURFACE
389 else if (0 == g_ascii_strcasecmp (extension, "ps"))
390 constructor = cairo_ps_surface_create_for_stream;
392 else if (0 == g_ascii_strcasecmp (extension, "eps"))
393 constructor = _cairo_eps_surface_create_for_stream;
398 unsigned int fr, fg, fb, fa, br, bg, bb, ba;
400 br = bg = bb = 0; ba = 255;
401 color = view_opts->back ? view_opts->back : DEFAULT_BACK;
402 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
403 fr = fg = fb = 0; fa = 255;
404 color = view_opts->fore ? view_opts->fore : DEFAULT_FORE;
405 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
407 if (content == CAIRO_CONTENT_ALPHA)
409 if (view_opts->annotate ||
410 br != bg || bg != bb ||
411 fr != fg || fg != fb)
412 content = CAIRO_CONTENT_COLOR;
415 content = CAIRO_CONTENT_COLOR_ALPHA;
417 cairo_surface_t *surface;
418 FILE *f = out_opts->get_file_handle ();
420 surface = constructor (stdio_write_func, f, w, h);
421 else if (constructor2)
422 surface = constructor2 (stdio_write_func, f, w, h, content);
424 fail (false, "Unknown output format `%s'; supported formats are: %s%s",
426 g_strjoinv ("/", const_cast<char**> (helper_cairo_supported_formats)),
427 out_opts->explicit_output_format ? "" :
428 "\nTry setting format using --output-format");
430 cairo_t *cr = cairo_create (surface);
431 content = cairo_surface_get_content (surface);
434 case CAIRO_CONTENT_ALPHA:
435 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
436 cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
438 cairo_set_source_rgba (cr, 1., 1., 1.,
439 (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
442 case CAIRO_CONTENT_COLOR:
443 case CAIRO_CONTENT_COLOR_ALPHA:
444 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
445 cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
447 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
448 cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
452 cairo_surface_destroy (surface);
457 helper_cairo_destroy_context (cairo_t *cr)
459 finalize_closure_t *closure = (finalize_closure_t *)
460 cairo_surface_get_user_data (cairo_get_target (cr),
461 &finalize_closure_key);
463 closure->callback (closure);
465 cairo_status_t status = cairo_status (cr);
466 if (status != CAIRO_STATUS_SUCCESS)
467 fail (false, "Failed: %s",
468 cairo_status_to_string (status));
474 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
477 unsigned int text_len,
479 hb_bool_t utf8_clusters)
481 memset (l, 0, sizeof (*l));
483 l->num_glyphs = hb_buffer_get_length (buffer);
484 hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (buffer, nullptr);
485 hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (buffer, nullptr);
486 l->glyphs = cairo_glyph_allocate (l->num_glyphs + 1);
489 l->utf8 = g_strndup (text, text_len);
490 l->utf8_len = text_len;
491 l->num_clusters = l->num_glyphs ? 1 : 0;
492 for (unsigned int i = 1; i < l->num_glyphs; i++)
493 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
495 l->clusters = cairo_text_cluster_allocate (l->num_clusters);
498 if ((l->num_glyphs && !l->glyphs) ||
499 (l->utf8_len && !l->utf8) ||
500 (l->num_clusters && !l->clusters))
506 hb_position_t x = 0, y = 0;
508 for (i = 0; i < (int) l->num_glyphs; i++)
510 l->glyphs[i].index = hb_glyph[i].codepoint;
511 l->glyphs[i].x = scalbn ((double) hb_position->x_offset + x, scale_bits);
512 l->glyphs[i].y = scalbn ((double) -hb_position->y_offset + y, scale_bits);
513 x += hb_position->x_advance;
514 y += -hb_position->y_advance;
518 l->glyphs[i].index = -1;
519 l->glyphs[i].x = scalbn ((double) x, scale_bits);
520 l->glyphs[i].y = scalbn ((double) y, scale_bits);
522 if (l->num_clusters) {
523 memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0]));
524 hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
525 l->cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
526 unsigned int cluster = 0;
527 const char *start = l->utf8, *end;
528 l->clusters[cluster].num_glyphs++;
530 for (i = l->num_glyphs - 2; i >= 0; i--) {
531 if (hb_glyph[i].cluster != hb_glyph[i+1].cluster) {
532 g_assert (hb_glyph[i].cluster > hb_glyph[i+1].cluster);
534 end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
536 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i+1].cluster);
537 l->clusters[cluster].num_bytes = end - start;
541 l->clusters[cluster].num_glyphs++;
543 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
545 for (i = 1; i < (int) l->num_glyphs; i++) {
546 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) {
547 g_assert (hb_glyph[i].cluster > hb_glyph[i-1].cluster);
549 end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
551 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i-1].cluster);
552 l->clusters[cluster].num_bytes = end - start;
556 l->clusters[cluster].num_glyphs++;
558 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;