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 helper_cairo_create_scaled_font (const font_options_t *font_opts,
67 hb_font_t *font = hb_font_reference (font_opts->get_font ());
69 cairo_font_face_t *cairo_face;
70 FT_Face ft_face = hb_ft_font_get_face (font);
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);
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;
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);
88 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face,
93 cairo_font_options_destroy (font_options);
94 cairo_font_face_destroy (cairo_face);
96 static cairo_user_data_key_t key;
97 if (cairo_scaled_font_set_user_data (scaled_font,
100 (cairo_destroy_func_t) hb_font_destroy))
101 hb_font_destroy (font);
107 struct finalize_closure_t {
108 void (*callback)(finalize_closure_t *);
109 cairo_surface_t *surface;
110 cairo_write_func_t write_func;
113 static cairo_user_data_key_t finalize_closure_key;
117 finalize_ansi (finalize_closure_t *closure)
119 cairo_status_t status;
120 status = helper_cairo_surface_write_to_ansi_stream (closure->surface,
123 if (status != CAIRO_STATUS_SUCCESS)
124 fail (false, "Failed to write output: %s",
125 cairo_status_to_string (status));
128 static cairo_surface_t *
129 _cairo_ansi_surface_create_for_stream (cairo_write_func_t write_func,
133 cairo_content_t content)
135 cairo_surface_t *surface;
136 int w = ceil (width);
137 int h = ceil (height);
140 case CAIRO_CONTENT_ALPHA:
141 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
144 case CAIRO_CONTENT_COLOR:
145 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
147 case CAIRO_CONTENT_COLOR_ALPHA:
148 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
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));
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;
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);
172 #ifdef CAIRO_HAS_PNG_FUNCTIONS
175 finalize_png (finalize_closure_t *closure)
177 cairo_status_t status;
178 status = cairo_surface_write_to_png_stream (closure->surface,
181 if (status != CAIRO_STATUS_SUCCESS)
182 fail (false, "Failed to write output: %s",
183 cairo_status_to_string (status));
186 static cairo_surface_t *
187 _cairo_png_surface_create_for_stream (cairo_write_func_t write_func,
191 cairo_content_t content)
193 cairo_surface_t *surface;
194 int w = ceil (width);
195 int h = ceil (height);
198 case CAIRO_CONTENT_ALPHA:
199 surface = cairo_image_surface_create (CAIRO_FORMAT_A8, w, h);
202 case CAIRO_CONTENT_COLOR:
203 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
205 case CAIRO_CONTENT_COLOR_ALPHA:
206 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
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));
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;
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);
231 static cairo_status_t
232 stdio_write_func (void *closure,
233 const unsigned char *data,
236 FILE *fp = (FILE *) closure;
239 size_t ret = fwrite (data, 1, size, fp);
242 if (size && ferror (fp))
243 fail (false, "Failed to write output: %s", strerror (errno));
246 return CAIRO_STATUS_SUCCESS;
249 const char helper_cairo_supported_formats[] =
251 #ifdef CAIRO_HAS_PNG_FUNCTIONS
254 #ifdef CAIRO_HAS_SVG_SURFACE
257 #ifdef CAIRO_HAS_PDF_SURFACE
260 #ifdef CAIRO_HAS_PS_SURFACE
269 helper_cairo_create_context (double w, double h,
270 view_options_t *view_opts,
271 output_options_t *out_opts)
273 cairo_surface_t *(*constructor) (cairo_write_func_t write_func,
276 double height) = NULL;
277 cairo_surface_t *(*constructor2) (cairo_write_func_t write_func,
281 cairo_content_t content) = NULL;
283 const char *extension = out_opts->output_format;
286 if (isatty (fileno (out_opts->get_file_handle ())))
291 #ifdef CAIRO_HAS_PNG_FUNCTIONS
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;
306 #ifdef CAIRO_HAS_SVG_SURFACE
307 else if (0 == strcasecmp (extension, "svg"))
308 constructor = cairo_svg_surface_create_for_stream;
310 #ifdef CAIRO_HAS_PDF_SURFACE
311 else if (0 == strcasecmp (extension, "pdf"))
312 constructor = cairo_pdf_surface_create_for_stream;
314 #ifdef CAIRO_HAS_PS_SURFACE
315 else if (0 == strcasecmp (extension, "ps"))
316 constructor = cairo_ps_surface_create_for_stream;
318 else if (0 == strcasecmp (extension, "eps"))
319 constructor = _cairo_eps_surface_create_for_stream;
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);
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;
334 content = CAIRO_CONTENT_COLOR;
336 content = CAIRO_CONTENT_COLOR_ALPHA;
338 cairo_surface_t *surface;
339 FILE *f = out_opts->get_file_handle ();
341 surface = constructor (stdio_write_func, f, w, h);
342 else if (constructor2)
343 surface = constructor2 (stdio_write_func, f, w, h, content);
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");
350 cairo_t *cr = cairo_create (surface);
351 content = cairo_surface_get_content (surface);
354 case CAIRO_CONTENT_ALPHA:
355 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
356 cairo_set_source_rgba (cr, 1., 1., 1., br / 255.);
358 cairo_set_source_rgba (cr, 1., 1., 1.,
359 (fr / 255.) * (fa / 255.) + (br / 255) * (1 - (fa / 255.)));
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.);
367 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
368 cairo_set_source_rgba (cr, fr / 255., fg / 255., fb / 255., fa / 255.);
372 cairo_surface_destroy (surface);
377 helper_cairo_destroy_context (cairo_t *cr)
379 finalize_closure_t *closure = (finalize_closure_t *)
380 cairo_surface_get_user_data (cairo_get_target (cr),
381 &finalize_closure_key);
383 closure->callback (closure);
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));
394 helper_cairo_line_from_buffer (helper_cairo_line_t *l,
397 unsigned int text_len,
399 hb_bool_t utf8_clusters)
401 memset (l, 0, sizeof (*l));
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);
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)
415 l->clusters = cairo_text_cluster_allocate (l->num_clusters);
418 if ((l->num_glyphs && !l->glyphs) ||
419 (l->utf8_len && !l->utf8) ||
420 (l->num_clusters && !l->clusters))
426 hb_position_t x = 0, y = 0;
428 for (i = 0; i < (int) l->num_glyphs; i++)
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;
438 l->glyphs[i].index = -1;
439 l->glyphs[i].x = x * scale;
440 l->glyphs[i].y = y * scale;
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++;
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);
454 end = start + hb_glyph[i].cluster - hb_glyph[i+1].cluster;
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;
461 l->clusters[cluster].num_glyphs++;
463 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;
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);
469 end = start + hb_glyph[i].cluster - hb_glyph[i-1].cluster;
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;
476 l->clusters[cluster].num_glyphs++;
478 l->clusters[cluster].num_bytes = l->utf8 + text_len - start;