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;
68 void free_ft_library (void)
70 FT_Done_FreeType (ft_library);
74 helper_cairo_create_scaled_font (const font_options_t *font_opts)
76 hb_font_t *font = hb_font_reference (font_opts->get_font ());
78 cairo_font_face_t *cairo_face;
79 /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because
80 * cairo will reset the face size. As such, create new face...
81 * TODO Perhaps add API to hb-ft to encapsulate this code. */
82 FT_Face ft_face = NULL;//hb_ft_font_get_face (font);
87 FT_Init_FreeType (&ft_library);
89 atexit (free_ft_library);
92 FT_New_Face (ft_library,
94 font_opts->face_index,
99 /* This allows us to get some boxes at least... */
100 cairo_face = cairo_toy_font_face_create ("@cairo:sans",
101 CAIRO_FONT_SLANT_NORMAL,
102 CAIRO_FONT_WEIGHT_NORMAL);
106 unsigned int num_coords;
107 const int *coords = hb_font_get_var_coords_normalized (font, &num_coords);
110 FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed));
113 for (unsigned int i = 0; i < num_coords; i++)
114 ft_coords[i] = coords[i] << 2;
115 FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords);
120 cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
122 cairo_matrix_t ctm, font_matrix;
123 cairo_font_options_t *font_options;
125 cairo_matrix_init_identity (&ctm);
126 cairo_matrix_init_scale (&font_matrix,
127 font_opts->font_size_x,
128 font_opts->font_size_y);
129 font_options = cairo_font_options_create ();
130 cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
131 cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
133 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
138 cairo_font_options_destroy (font_options);
139 cairo_font_face_destroy (cairo_face);
141 static cairo_user_data_key_t key;
142 if (cairo_scaled_font_set_user_data (scaled_font,
145 (cairo_destroy_func_t) hb_font_destroy))
146 hb_font_destroy (font);
152 helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font)
156 FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
159 if (FT_HAS_COLOR (ft_face))
161 cairo_ft_scaled_font_unlock_face (scaled_font);
168 struct finalize_closure_t {
169 void (*callback)(finalize_closure_t *);
170 cairo_surface_t *surface;
171 cairo_write_func_t write_func;
174 static cairo_user_data_key_t finalize_closure_key;
178 finalize_ansi (finalize_closure_t *closure)
180 cairo_status_t status;
181 status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
184 if (status != CAIRO_STATUS_SUCCESS)
185 fail (false, "Failed to write output: %s",
186 cairo_status_to_string (status));
189 static cairo_surface_t *
190 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
194 cairo_content_t content)
196 cairo_surface_t *surface;
197 int w = ceil (width);
198 int h = ceil (height);
201 case CAIRO_CONTENT_ALPHA:
202 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
205 case CAIRO_CONTENT_COLOR:
206 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
208 case CAIRO_CONTENT_COLOR_ALPHA:
209 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
212 cairo_status_t status = cairo_surface_status (surface);
213 if (status != CAIRO_STATUS_SUCCESS)
214 fail (false, "Failed to create cairo surface: %s",
215 cairo_status_to_string (status));
217 finalize_closure_t *ansi_closure = g_new0 (finalize_closure_t, 1);
218 ansi_closure->callback = finalize_ansi;
219 ansi_closure->surface = surface;
220 ansi_closure->write_func = write_func;
221 ansi_closure->closure = closure;
223 if (cairo_surface_set_user_data (surface,
224 &finalize_closure_key,
225 (void *) ansi_closure,
226 (cairo_destroy_func_t) g_free))
227 g_free ((void *) closure);
233 #ifdef CAIRO_HAS_PNG_FUNCTIONS
236 finalize_png (finalize_closure_t *closure)
238 cairo_status_t status;
239 status = cairo_surface_write_to_png_stream (closure->surface,
242 if (status != CAIRO_STATUS_SUCCESS)
243 fail (false, "Failed to write output: %s",
244 cairo_status_to_string (status));
247 static cairo_surface_t *
248 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
252 cairo_content_t content)
254 cairo_surface_t *surface;
255 int w = ceil (width);
256 int h = ceil (height);
259 case CAIRO_CONTENT_ALPHA:
260 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
263 case CAIRO_CONTENT_COLOR:
264 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
266 case CAIRO_CONTENT_COLOR_ALPHA:
267 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
270 cairo_status_t status = cairo_surface_status (surface);
271 if (status != CAIRO_STATUS_SUCCESS)
272 fail (false, "Failed to create cairo surface: %s",
273 cairo_status_to_string (status));
275 finalize_closure_t *png_closure = g_new0 (finalize_closure_t, 1);
276 png_closure->callback = finalize_png;
277 png_closure->surface = surface;
278 png_closure->write_func = write_func;
279 png_closure->closure = closure;
281 if (cairo_surface_set_user_data (surface,
282 &finalize_closure_key,
283 (void *) png_closure,
284 (cairo_destroy_func_t) g_free))
285 g_free ((void *) closure);
292 static cairo_status_t
293 stdio_write_func (void *closure,
294 const unsigned char *data,
297 FILE *fp = (FILE *) closure;
300 size_t ret = fwrite (data, 1, size, fp);
303 if (size && ferror (fp))
304 fail (false, "Failed to write output: %s", strerror (errno));
307 return CAIRO_STATUS_SUCCESS;
310 const char *helper_cairo_supported_formats[] =
313 #ifdef CAIRO_HAS_PNG_FUNCTIONS
316 #ifdef CAIRO_HAS_SVG_SURFACE
319 #ifdef CAIRO_HAS_PDF_SURFACE
322 #ifdef CAIRO_HAS_PS_SURFACE
332 helper_cairo_create_context (double w, double h,
333 view_options_t *view_opts,
334 output_options_t *out_opts,
335 cairo_content_t content)
337 cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
340 double height) = NULL;
341 cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
345 cairo_content_t content) = NULL;
347 const char *extension = out_opts->output_format;
350 if (isatty (fileno (out_opts->get_file_handle ())))
355 #ifdef CAIRO_HAS_PNG_FUNCTIONS
364 else if (0 == g_ascii_strcasecmp (extension, "ansi"))
365 constructor2 = _cairo_ansi_surface_create_for_stream;
366 #ifdef CAIRO_HAS_PNG_FUNCTIONS
367 else if (0 == g_ascii_strcasecmp (extension, "png"))
368 constructor2 = _cairo_png_surface_create_for_stream;
370 #ifdef CAIRO_HAS_SVG_SURFACE
371 else if (0 == g_ascii_strcasecmp (extension, "svg"))
372 constructor = cairo_svg_surface_create_for_stream;
374 #ifdef CAIRO_HAS_PDF_SURFACE
375 else if (0 == g_ascii_strcasecmp (extension, "pdf"))
376 constructor = cairo_pdf_surface_create_for_stream;
378 #ifdef CAIRO_HAS_PS_SURFACE
379 else if (0 == g_ascii_strcasecmp (extension, "ps"))
380 constructor = cairo_ps_surface_create_for_stream;
382 else if (0 == g_ascii_strcasecmp (extension, "eps"))
383 constructor = _cairo_eps_surface_create_for_stream;
388 unsigned int fr, fg, fb, fa, br, bg, bb, ba;
390 br = bg = bb = 0; ba = 255;
391 color = view_opts->back ? view_opts->back : DEFAULT_BACK;
392 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
393 fr = fg = fb = 0; fa = 255;
394 color = view_opts->fore ? view_opts->fore : DEFAULT_FORE;
395 sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
397 if (content == CAIRO_CONTENT_ALPHA)
399 if (view_opts->annotate ||
400 br != bg || bg != bb ||
401 fr != fg || fg != fb)
402 content = CAIRO_CONTENT_COLOR;
405 content = CAIRO_CONTENT_COLOR_ALPHA;
407 cairo_surface_t *surface;
408 FILE *f = out_opts->get_file_handle ();
410 surface = constructor (stdio_write_func, f, w, h);
411 else if (constructor2)
412 surface = constructor2 (stdio_write_func, f, w, h, content);
414 fail (false, "Unknown output format `%s'; supported formats are: %s%s",
416 g_strjoinv ("/", const_cast<char**> (helper_cairo_supported_formats)),
417 out_opts->explicit_output_format ? "" :
418 "\nTry setting format using --output-format");
420 cairo_t *cr = cairo_create (surface);
421 content = cairo_surface_get_content (surface);
424 case CAIRO_CONTENT_ALPHA:
425 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
426 cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
428 cairo_set_source_rgba (cr, 1., 1., 1.,
429 (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
432 case CAIRO_CONTENT_COLOR:
433 case CAIRO_CONTENT_COLOR_ALPHA:
434 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
435 cairo_set_source_rgba (cr, br / 255., bg / 255., bb / 255., ba / 255.);
437 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
438 cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
442 cairo_surface_destroy (surface);
447 helper_cairo_destroy_context (cairo_t *cr)
449 finalize_closure_t *closure = (finalize_closure_t *)
450 cairo_surface_get_user_data (cairo_get_target (cr),
451 &finalize_closure_key);
453 closure->callback (closure);
455 cairo_status_t status = cairo_status (cr);
456 if (status != CAIRO_STATUS_SUCCESS)
457 fail (false, "Failed: %s",
458 cairo_status_to_string (status));
464 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
467 unsigned int text_len,
469 hb_bool_t utf8_clusters)
471 memset (l, 0, sizeof (*l));
473 l->num_glyphs = hb_buffer_get_length (buffer);
474 hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (buffer, NULL);
475 hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (buffer, NULL);
476 l->glyphs = cairo_glyph_allocate (l->num_glyphs + 1);
479 l->utf8 = g_strndup (text, text_len);
480 l->utf8_len = text_len;
481 l->num_clusters = l->num_glyphs ? 1 : 0;
482 for (unsigned int i = 1; i < l->num_glyphs; i++)
483 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster)
485 l->clusters = cairo_text_cluster_allocate (l->num_clusters);
488 if ((l->num_glyphs && !l->glyphs) ||
489 (l->utf8_len && !l->utf8) ||
490 (l->num_clusters && !l->clusters))
496 hb_position_t x = 0, y = 0;
498 for (i = 0; i < (int) l->num_glyphs; i++)
500 l->glyphs[i].index = hb_glyph[i].codepoint;
501 l->glyphs[i].x = scalbn ((double) hb_position->x_offset + x, scale_bits);
502 l->glyphs[i].y = scalbn ((double) -hb_position->y_offset + y, scale_bits);
503 x += hb_position->x_advance;
504 y += -hb_position->y_advance;
508 l->glyphs[i].index = -1;
509 l->glyphs[i].x = scalbn ((double) x, scale_bits);
510 l->glyphs[i].y = scalbn ((double) y, scale_bits);
512 if (l->num_clusters) {
513 memset ((void *) l->clusters, 0, l->num_clusters * sizeof (l->clusters[0]));
514 hb_bool_t backward = HB_DIRECTION_IS_BACKWARD (hb_buffer_get_direction (buffer));
515 l->cluster_flags = backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : (cairo_text_cluster_flags_t) 0;
516 unsigned int cluster = 0;
517 const char *start = l->utf8, *end;
518 l->clusters[cluster].num_glyphs++;
520 for (i = l->num_glyphs - 2; i >= 0; i--) {
521 if (hb_glyph[i].cluster != hb_glyph[i+1].cluster) {
522 g_assert (hb_glyph[i].cluster > hb_glyph[i+1].cluster);
524 end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
526 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i+1].cluster);
527 l->clusters[cluster].num_bytes = end - start;
531 l->clusters[cluster].num_glyphs++;
533 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
535 for (i = 1; i < (int) l->num_glyphs; i++) {
536 if (hb_glyph[i].cluster != hb_glyph[i-1].cluster) {
537 g_assert (hb_glyph[i].cluster > hb_glyph[i-1].cluster);
539 end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
541 end = g_utf8_offset_to_pointer (start, hb_glyph[i].cluster - hb_glyph[i-1].cluster);
542 l->clusters[cluster].num_bytes = end - start;
546 l->clusters[cluster].num_glyphs++;
548 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;