1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
3 *
4 * Copyright © 2008 Adrian Johnson
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
13 *
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
19 *
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
24 *
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
28 *
29 * The Original Code is the cairo graphics library.
30 *
31 * The Initial Developer of the Original Code is Adrian Johnson.
32 *
33 * Contributor(s):
34 * Adrian Johnson <ajohnson@redneon.com>
35 */
36
37 #include "cairoint.h"
38
39 #if CAIRO_HAS_FONT_SUBSET
40
41 #include "cairo-type3-glyph-surface-private.h"
42 #include "cairo-output-stream-private.h"
43 #include "cairo-recording-surface-private.h"
44 #include "cairo-analysis-surface-private.h"
45 #include "cairo-default-context-private.h"
46 #include "cairo-error-private.h"
47 #include "cairo-image-surface-private.h"
48 #include "cairo-surface-clipper-private.h"
49
50 static const cairo_surface_backend_t cairo_type3_glyph_surface_backend;
51
52 static cairo_status_t
_cairo_type3_glyph_surface_clipper_intersect_clip_path(cairo_surface_clipper_t * clipper,cairo_path_fixed_t * path,cairo_fill_rule_t fill_rule,double tolerance,cairo_antialias_t antialias)53 _cairo_type3_glyph_surface_clipper_intersect_clip_path (cairo_surface_clipper_t *clipper,
54 cairo_path_fixed_t *path,
55 cairo_fill_rule_t fill_rule,
56 double tolerance,
57 cairo_antialias_t antialias)
58 {
59 cairo_type3_glyph_surface_t *surface = cairo_container_of (clipper,
60 cairo_type3_glyph_surface_t,
61 clipper);
62
63 if (path == NULL) {
64 _cairo_output_stream_printf (surface->stream, "Q q\n");
65 return CAIRO_STATUS_SUCCESS;
66 }
67
68 return _cairo_pdf_operators_clip (&surface->pdf_operators,
69 path,
70 fill_rule);
71 }
72
73 cairo_surface_t *
_cairo_type3_glyph_surface_create(cairo_scaled_font_t * scaled_font,cairo_output_stream_t * stream,cairo_type3_glyph_surface_emit_image_t emit_image,cairo_scaled_font_subsets_t * font_subsets,cairo_bool_t ps)74 _cairo_type3_glyph_surface_create (cairo_scaled_font_t *scaled_font,
75 cairo_output_stream_t *stream,
76 cairo_type3_glyph_surface_emit_image_t emit_image,
77 cairo_scaled_font_subsets_t *font_subsets,
78 cairo_bool_t ps)
79 {
80 cairo_type3_glyph_surface_t *surface;
81
82 if (unlikely (stream != NULL && stream->status))
83 return _cairo_surface_create_in_error (stream->status);
84
85 surface = _cairo_malloc (sizeof (cairo_type3_glyph_surface_t));
86 if (unlikely (surface == NULL))
87 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
88
89 _cairo_surface_init (&surface->base,
90 &cairo_type3_glyph_surface_backend,
91 NULL, /* device */
92 CAIRO_CONTENT_COLOR_ALPHA,
93 TRUE); /* is_vector */
94
95 surface->scaled_font = scaled_font;
96 surface->stream = stream;
97 surface->emit_image = emit_image;
98
99 /* Setup the transform from the user-font device space to Type 3
100 * font space. The Type 3 font space is defined by the FontMatrix
101 * entry in the Type 3 dictionary. In the PDF backend this is an
102 * identity matrix. */
103 surface->cairo_to_pdf = scaled_font->scale_inverse;
104
105 _cairo_pdf_operators_init (&surface->pdf_operators,
106 surface->stream,
107 &surface->cairo_to_pdf,
108 font_subsets,
109 ps);
110
111 _cairo_surface_clipper_init (&surface->clipper,
112 _cairo_type3_glyph_surface_clipper_intersect_clip_path);
113
114 return &surface->base;
115 }
116
117 static cairo_status_t
_cairo_type3_glyph_surface_emit_image(cairo_type3_glyph_surface_t * surface,cairo_image_surface_t * image,cairo_matrix_t * image_matrix)118 _cairo_type3_glyph_surface_emit_image (cairo_type3_glyph_surface_t *surface,
119 cairo_image_surface_t *image,
120 cairo_matrix_t *image_matrix)
121 {
122 cairo_status_t status;
123
124 /* The only image type supported by Type 3 fonts are 1-bit masks */
125 image = _cairo_image_surface_coerce_to_format (image, CAIRO_FORMAT_A1);
126 status = image->base.status;
127 if (unlikely (status))
128 return status;
129
130 _cairo_output_stream_printf (surface->stream,
131 "q %f %f %f %f %f %f cm\n",
132 image_matrix->xx,
133 image_matrix->xy,
134 image_matrix->yx,
135 image_matrix->yy,
136 image_matrix->x0,
137 image_matrix->y0);
138
139 status = surface->emit_image (image, surface->stream);
140 cairo_surface_destroy (&image->base);
141
142 _cairo_output_stream_printf (surface->stream,
143 "Q\n");
144
145 return status;
146 }
147
148 static cairo_status_t
_cairo_type3_glyph_surface_emit_image_pattern(cairo_type3_glyph_surface_t * surface,cairo_image_surface_t * image,const cairo_matrix_t * pattern_matrix)149 _cairo_type3_glyph_surface_emit_image_pattern (cairo_type3_glyph_surface_t *surface,
150 cairo_image_surface_t *image,
151 const cairo_matrix_t *pattern_matrix)
152 {
153 cairo_matrix_t mat, upside_down;
154 cairo_status_t status;
155
156 if (image->width == 0 || image->height == 0)
157 return CAIRO_STATUS_SUCCESS;
158
159 mat = *pattern_matrix;
160
161 /* Get the pattern space to user space matrix */
162 status = cairo_matrix_invert (&mat);
163
164 /* cairo_pattern_set_matrix ensures the matrix is invertible */
165 assert (status == CAIRO_STATUS_SUCCESS);
166
167 /* Make this a pattern space to Type 3 font space matrix */
168 cairo_matrix_multiply (&mat, &mat, &surface->cairo_to_pdf);
169
170 /* PDF images are in a 1 unit by 1 unit image space. Turn the 1 by
171 * 1 image upside down to convert to flip the Y-axis going from
172 * cairo to PDF. Then scale the image up to the required size. */
173 cairo_matrix_scale (&mat, image->width, image->height);
174 cairo_matrix_init (&upside_down, 1, 0, 0, -1, 0, 1);
175 cairo_matrix_multiply (&mat, &upside_down, &mat);
176
177 return _cairo_type3_glyph_surface_emit_image (surface, image, &mat);
178 }
179
180 static cairo_status_t
_cairo_type3_glyph_surface_finish(void * abstract_surface)181 _cairo_type3_glyph_surface_finish (void *abstract_surface)
182 {
183 cairo_type3_glyph_surface_t *surface = abstract_surface;
184
185 return _cairo_pdf_operators_fini (&surface->pdf_operators);
186 }
187
188 static cairo_int_status_t
_cairo_type3_glyph_surface_paint(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_clip_t * clip)189 _cairo_type3_glyph_surface_paint (void *abstract_surface,
190 cairo_operator_t op,
191 const cairo_pattern_t *source,
192 const cairo_clip_t *clip)
193 {
194 cairo_type3_glyph_surface_t *surface = abstract_surface;
195 const cairo_surface_pattern_t *pattern;
196 cairo_image_surface_t *image;
197 void *image_extra;
198 cairo_status_t status;
199
200 if (source->type != CAIRO_PATTERN_TYPE_SURFACE)
201 return CAIRO_INT_STATUS_IMAGE_FALLBACK;
202
203 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
204 if (unlikely (status))
205 return status;
206
207 pattern = (const cairo_surface_pattern_t *) source;
208 status = _cairo_surface_acquire_source_image (pattern->surface,
209 &image, &image_extra);
210 if (unlikely (status))
211 goto fail;
212
213 status = _cairo_type3_glyph_surface_emit_image_pattern (surface,
214 image,
215 &pattern->base.matrix);
216
217 fail:
218 _cairo_surface_release_source_image (pattern->surface, image, image_extra);
219
220 return status;
221 }
222
223 static cairo_int_status_t
_cairo_type3_glyph_surface_mask(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_pattern_t * mask,const cairo_clip_t * clip)224 _cairo_type3_glyph_surface_mask (void *abstract_surface,
225 cairo_operator_t op,
226 const cairo_pattern_t *source,
227 const cairo_pattern_t *mask,
228 const cairo_clip_t *clip)
229 {
230 return _cairo_type3_glyph_surface_paint (abstract_surface,
231 op, mask,
232 clip);
233 }
234
235 static cairo_int_status_t
_cairo_type3_glyph_surface_stroke(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_path_fixed_t * path,const cairo_stroke_style_t * style,const cairo_matrix_t * ctm,const cairo_matrix_t * ctm_inverse,double tolerance,cairo_antialias_t antialias,const cairo_clip_t * clip)236 _cairo_type3_glyph_surface_stroke (void *abstract_surface,
237 cairo_operator_t op,
238 const cairo_pattern_t *source,
239 const cairo_path_fixed_t *path,
240 const cairo_stroke_style_t *style,
241 const cairo_matrix_t *ctm,
242 const cairo_matrix_t *ctm_inverse,
243 double tolerance,
244 cairo_antialias_t antialias,
245 const cairo_clip_t *clip)
246 {
247 cairo_type3_glyph_surface_t *surface = abstract_surface;
248 cairo_int_status_t status;
249
250 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
251 if (unlikely (status))
252 return status;
253
254 return _cairo_pdf_operators_stroke (&surface->pdf_operators,
255 path,
256 style,
257 ctm,
258 ctm_inverse);
259 }
260
261 static cairo_int_status_t
_cairo_type3_glyph_surface_fill(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,const cairo_path_fixed_t * path,cairo_fill_rule_t fill_rule,double tolerance,cairo_antialias_t antialias,const cairo_clip_t * clip)262 _cairo_type3_glyph_surface_fill (void *abstract_surface,
263 cairo_operator_t op,
264 const cairo_pattern_t *source,
265 const cairo_path_fixed_t *path,
266 cairo_fill_rule_t fill_rule,
267 double tolerance,
268 cairo_antialias_t antialias,
269 const cairo_clip_t *clip)
270 {
271 cairo_type3_glyph_surface_t *surface = abstract_surface;
272 cairo_int_status_t status;
273
274 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
275 if (unlikely (status))
276 return status;
277
278 return _cairo_pdf_operators_fill (&surface->pdf_operators,
279 path,
280 fill_rule);
281 }
282
283 static cairo_int_status_t
_cairo_type3_glyph_surface_show_glyphs(void * abstract_surface,cairo_operator_t op,const cairo_pattern_t * source,cairo_glyph_t * glyphs,int num_glyphs,cairo_scaled_font_t * scaled_font,const cairo_clip_t * clip)284 _cairo_type3_glyph_surface_show_glyphs (void *abstract_surface,
285 cairo_operator_t op,
286 const cairo_pattern_t *source,
287 cairo_glyph_t *glyphs,
288 int num_glyphs,
289 cairo_scaled_font_t *scaled_font,
290 const cairo_clip_t *clip)
291 {
292 cairo_type3_glyph_surface_t *surface = abstract_surface;
293 cairo_int_status_t status;
294 cairo_scaled_font_t *font;
295 cairo_matrix_t new_ctm;
296
297 status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
298 if (unlikely (status))
299 return status;
300
301 cairo_matrix_multiply (&new_ctm, &surface->cairo_to_pdf, &scaled_font->ctm);
302 font = cairo_scaled_font_create (scaled_font->font_face,
303 &scaled_font->font_matrix,
304 &new_ctm,
305 &scaled_font->options);
306 if (unlikely (font->status))
307 return font->status;
308
309 status = _cairo_pdf_operators_show_text_glyphs (&surface->pdf_operators,
310 NULL, 0,
311 glyphs, num_glyphs,
312 NULL, 0,
313 FALSE,
314 font);
315
316 cairo_scaled_font_destroy (font);
317
318 return status;
319 }
320
321 static const cairo_surface_backend_t cairo_type3_glyph_surface_backend = {
322 CAIRO_INTERNAL_SURFACE_TYPE_TYPE3_GLYPH,
323 _cairo_type3_glyph_surface_finish,
324
325 _cairo_default_context_create, /* XXX usable through a context? */
326
327 NULL, /* create similar */
328 NULL, /* create similar image */
329 NULL, /* map to image */
330 NULL, /* unmap image */
331
332 NULL, /* source */
333 NULL, /* acquire_source_image */
334 NULL, /* release_source_image */
335 NULL, /* snapshot */
336
337 NULL, /* copy page */
338 NULL, /* show page */
339
340 NULL, /* _cairo_type3_glyph_surface_get_extents */
341 NULL, /* _cairo_type3_glyph_surface_get_font_options */
342
343 NULL, /* flush */
344 NULL, /* mark_dirty_rectangle */
345
346 _cairo_type3_glyph_surface_paint,
347 _cairo_type3_glyph_surface_mask,
348 _cairo_type3_glyph_surface_stroke,
349 _cairo_type3_glyph_surface_fill,
350 NULL, /* fill-stroke */
351 _cairo_type3_glyph_surface_show_glyphs,
352 };
353
354 static void
_cairo_type3_glyph_surface_set_stream(cairo_type3_glyph_surface_t * surface,cairo_output_stream_t * stream)355 _cairo_type3_glyph_surface_set_stream (cairo_type3_glyph_surface_t *surface,
356 cairo_output_stream_t *stream)
357 {
358 surface->stream = stream;
359 _cairo_pdf_operators_set_stream (&surface->pdf_operators, stream);
360 }
361
362 static cairo_status_t
_cairo_type3_glyph_surface_emit_fallback_image(cairo_type3_glyph_surface_t * surface,unsigned long glyph_index)363 _cairo_type3_glyph_surface_emit_fallback_image (cairo_type3_glyph_surface_t *surface,
364 unsigned long glyph_index)
365 {
366 cairo_scaled_glyph_t *scaled_glyph;
367 cairo_status_t status;
368 cairo_image_surface_t *image;
369 cairo_matrix_t mat;
370 double x, y;
371
372 status = _cairo_scaled_glyph_lookup (surface->scaled_font,
373 glyph_index,
374 CAIRO_SCALED_GLYPH_INFO_METRICS |
375 CAIRO_SCALED_GLYPH_INFO_SURFACE,
376 &scaled_glyph);
377 if (unlikely (status))
378 return status;
379
380 image = scaled_glyph->surface;
381 if (image->width == 0 || image->height == 0)
382 return CAIRO_STATUS_SUCCESS;
383
384 x = _cairo_fixed_to_double (scaled_glyph->bbox.p1.x);
385 y = _cairo_fixed_to_double (scaled_glyph->bbox.p2.y);
386 cairo_matrix_init(&mat, image->width, 0,
387 0, -image->height,
388 x, y);
389 cairo_matrix_multiply (&mat, &mat, &surface->scaled_font->scale_inverse);
390
391 return _cairo_type3_glyph_surface_emit_image (surface, image, &mat);
392 }
393
394 void
_cairo_type3_glyph_surface_set_font_subsets_callback(void * abstract_surface,cairo_pdf_operators_use_font_subset_t use_font_subset,void * closure)395 _cairo_type3_glyph_surface_set_font_subsets_callback (void *abstract_surface,
396 cairo_pdf_operators_use_font_subset_t use_font_subset,
397 void *closure)
398 {
399 cairo_type3_glyph_surface_t *surface = abstract_surface;
400
401 if (unlikely (surface->base.status))
402 return;
403
404 _cairo_pdf_operators_set_font_subsets_callback (&surface->pdf_operators,
405 use_font_subset,
406 closure);
407 }
408
409 cairo_status_t
_cairo_type3_glyph_surface_analyze_glyph(void * abstract_surface,unsigned long glyph_index)410 _cairo_type3_glyph_surface_analyze_glyph (void *abstract_surface,
411 unsigned long glyph_index)
412 {
413 cairo_type3_glyph_surface_t *surface = abstract_surface;
414 cairo_scaled_glyph_t *scaled_glyph;
415 cairo_int_status_t status, status2;
416 cairo_output_stream_t *null_stream;
417
418 if (unlikely (surface->base.status))
419 return surface->base.status;
420
421 null_stream = _cairo_null_stream_create ();
422 if (unlikely (null_stream->status))
423 return null_stream->status;
424
425 _cairo_type3_glyph_surface_set_stream (surface, null_stream);
426
427 _cairo_scaled_font_freeze_cache (surface->scaled_font);
428 status = _cairo_scaled_glyph_lookup (surface->scaled_font,
429 glyph_index,
430 CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE,
431 &scaled_glyph);
432
433 if (_cairo_int_status_is_error (status))
434 goto cleanup;
435
436 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
437 status = CAIRO_INT_STATUS_SUCCESS;
438 goto cleanup;
439 }
440
441 status = _cairo_recording_surface_replay (scaled_glyph->recording_surface,
442 &surface->base);
443 if (unlikely (status))
444 goto cleanup;
445
446 status = _cairo_pdf_operators_flush (&surface->pdf_operators);
447 if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK)
448 status = CAIRO_INT_STATUS_SUCCESS;
449
450 cleanup:
451 _cairo_scaled_font_thaw_cache (surface->scaled_font);
452
453 status2 = _cairo_output_stream_destroy (null_stream);
454 if (status == CAIRO_INT_STATUS_SUCCESS)
455 status = status2;
456
457 return status;
458 }
459
460 cairo_status_t
_cairo_type3_glyph_surface_emit_glyph(void * abstract_surface,cairo_output_stream_t * stream,unsigned long glyph_index,cairo_box_t * bbox,double * width)461 _cairo_type3_glyph_surface_emit_glyph (void *abstract_surface,
462 cairo_output_stream_t *stream,
463 unsigned long glyph_index,
464 cairo_box_t *bbox,
465 double *width)
466 {
467 cairo_type3_glyph_surface_t *surface = abstract_surface;
468 cairo_scaled_glyph_t *scaled_glyph;
469 cairo_int_status_t status, status2;
470 double x_advance, y_advance;
471 cairo_matrix_t font_matrix_inverse;
472
473 if (unlikely (surface->base.status))
474 return surface->base.status;
475
476 _cairo_type3_glyph_surface_set_stream (surface, stream);
477
478 _cairo_scaled_font_freeze_cache (surface->scaled_font);
479 status = _cairo_scaled_glyph_lookup (surface->scaled_font,
480 glyph_index,
481 CAIRO_SCALED_GLYPH_INFO_METRICS |
482 CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE,
483 &scaled_glyph);
484 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
485 status = _cairo_scaled_glyph_lookup (surface->scaled_font,
486 glyph_index,
487 CAIRO_SCALED_GLYPH_INFO_METRICS,
488 &scaled_glyph);
489 if (status == CAIRO_INT_STATUS_SUCCESS)
490 status = CAIRO_INT_STATUS_IMAGE_FALLBACK;
491 }
492 if (_cairo_int_status_is_error (status)) {
493 _cairo_scaled_font_thaw_cache (surface->scaled_font);
494 return status;
495 }
496
497 x_advance = scaled_glyph->metrics.x_advance;
498 y_advance = scaled_glyph->metrics.y_advance;
499 font_matrix_inverse = surface->scaled_font->font_matrix;
500 status2 = cairo_matrix_invert (&font_matrix_inverse);
501
502 /* The invertability of font_matrix is tested in
503 * pdf_operators_show_glyphs before any glyphs are mapped to the
504 * subset. */
505 assert (status2 == CAIRO_INT_STATUS_SUCCESS);
506
507 cairo_matrix_transform_distance (&font_matrix_inverse, &x_advance, &y_advance);
508 *width = x_advance;
509
510 *bbox = scaled_glyph->bbox;
511 _cairo_matrix_transform_bounding_box_fixed (&surface->scaled_font->scale_inverse,
512 bbox, NULL);
513
514 _cairo_output_stream_printf (surface->stream,
515 "%f 0 %f %f %f %f d1\n",
516 x_advance,
517 _cairo_fixed_to_double (bbox->p1.x),
518 _cairo_fixed_to_double (bbox->p1.y),
519 _cairo_fixed_to_double (bbox->p2.x),
520 _cairo_fixed_to_double (bbox->p2.y));
521
522 if (status == CAIRO_INT_STATUS_SUCCESS) {
523 cairo_output_stream_t *mem_stream;
524
525 mem_stream = _cairo_memory_stream_create ();
526 status = mem_stream->status;
527 if (unlikely (status))
528 goto FAIL;
529
530 _cairo_type3_glyph_surface_set_stream (surface, mem_stream);
531
532 _cairo_output_stream_printf (surface->stream, "q\n");
533 status = _cairo_recording_surface_replay (scaled_glyph->recording_surface,
534 &surface->base);
535
536 status2 = _cairo_pdf_operators_flush (&surface->pdf_operators);
537 if (status == CAIRO_INT_STATUS_SUCCESS)
538 status = status2;
539
540 _cairo_output_stream_printf (surface->stream, "Q\n");
541
542 _cairo_type3_glyph_surface_set_stream (surface, stream);
543 if (status == CAIRO_INT_STATUS_SUCCESS)
544 _cairo_memory_stream_copy (mem_stream, stream);
545
546 status2 = _cairo_output_stream_destroy (mem_stream);
547 if (status == CAIRO_INT_STATUS_SUCCESS)
548 status = status2;
549 }
550
551 if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK)
552 status = _cairo_type3_glyph_surface_emit_fallback_image (surface, glyph_index);
553
554 FAIL:
555 _cairo_scaled_font_thaw_cache (surface->scaled_font);
556
557 return status;
558 }
559
560 #endif /* CAIRO_HAS_FONT_SUBSET */
561