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
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 *
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_calloc (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
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
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
181
_cairo_type3_glyph_surface_finish (void *abstract_surface)
182
{
183
    cairo_type3_glyph_surface_t *surface = abstract_surface;
184

            
185
    cairo_status_t status = _cairo_pdf_operators_fini (&surface->pdf_operators);
186
    _cairo_surface_clipper_reset (&surface->clipper);
187
    return status;
188
}
189

            
190
static cairo_int_status_t
191
_cairo_type3_glyph_surface_paint (void			*abstract_surface,
192
				  cairo_operator_t	 op,
193
				  const cairo_pattern_t	*source,
194
				  const cairo_clip_t	*clip)
195
{
196
    cairo_type3_glyph_surface_t *surface = abstract_surface;
197
    const cairo_surface_pattern_t *pattern;
198
    cairo_image_surface_t *image;
199
    void *image_extra;
200
    cairo_status_t status;
201

            
202
    if (source->type != CAIRO_PATTERN_TYPE_SURFACE)
203
	return CAIRO_INT_STATUS_IMAGE_FALLBACK;
204

            
205
    status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
206
    if (unlikely (status))
207
	return status;
208

            
209
    pattern = (const cairo_surface_pattern_t *) source;
210
    if (pattern->surface->type == CAIRO_SURFACE_TYPE_RECORDING)
211
	return CAIRO_INT_STATUS_IMAGE_FALLBACK;
212

            
213
    status = _cairo_surface_acquire_source_image (pattern->surface,
214
						  &image, &image_extra);
215
    if (unlikely (status))
216
	goto fail;
217

            
218
    status = _cairo_type3_glyph_surface_emit_image_pattern (surface,
219
							    image,
220
							    &pattern->base.matrix);
221

            
222
fail:
223
    _cairo_surface_release_source_image (pattern->surface, image, image_extra);
224

            
225
    return status;
226
}
227

            
228
static cairo_int_status_t
229
_cairo_type3_glyph_surface_mask (void			*abstract_surface,
230
				 cairo_operator_t	 op,
231
				 const cairo_pattern_t	*source,
232
				 const cairo_pattern_t	*mask,
233
				 const cairo_clip_t	*clip)
234
{
235
    return _cairo_type3_glyph_surface_paint (abstract_surface,
236
					     op, mask,
237
					     clip);
238
}
239

            
240
static cairo_int_status_t
241
_cairo_type3_glyph_surface_stroke (void			*abstract_surface,
242
				   cairo_operator_t	 op,
243
				   const cairo_pattern_t *source,
244
				   const cairo_path_fixed_t	*path,
245
				   const cairo_stroke_style_t	*style,
246
				   const cairo_matrix_t	*ctm,
247
				   const cairo_matrix_t	*ctm_inverse,
248
				   double		 tolerance,
249
				   cairo_antialias_t	 antialias,
250
				   const cairo_clip_t	*clip)
251
{
252
    cairo_type3_glyph_surface_t *surface = abstract_surface;
253
    cairo_int_status_t status;
254

            
255
    status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
256
    if (unlikely (status))
257
	return status;
258

            
259
    return _cairo_pdf_operators_stroke (&surface->pdf_operators,
260
					path,
261
					style,
262
					ctm,
263
					ctm_inverse);
264
}
265

            
266
static cairo_int_status_t
267
_cairo_type3_glyph_surface_fill (void			*abstract_surface,
268
				 cairo_operator_t	 op,
269
				 const cairo_pattern_t	*source,
270
				 const cairo_path_fixed_t	*path,
271
				 cairo_fill_rule_t	 fill_rule,
272
				 double			 tolerance,
273
				 cairo_antialias_t	 antialias,
274
				 const cairo_clip_t		*clip)
275
{
276
    cairo_type3_glyph_surface_t *surface = abstract_surface;
277
    cairo_int_status_t status;
278

            
279
    status = _cairo_surface_clipper_set_clip (&surface->clipper, clip);
280
    if (unlikely (status))
281
	return status;
282

            
283
    return _cairo_pdf_operators_fill (&surface->pdf_operators,
284
				      path,
285
				      fill_rule);
286
}
287

            
288
static cairo_int_status_t
289
_cairo_type3_glyph_surface_show_glyphs (void		     *abstract_surface,
290
					cairo_operator_t      op,
291
					const cairo_pattern_t *source,
292
					cairo_glyph_t        *glyphs,
293
					int		      num_glyphs,
294
					cairo_scaled_font_t  *scaled_font,
295
					const cairo_clip_t     *clip)
296
{
297
    return CAIRO_INT_STATUS_IMAGE_FALLBACK;
298
}
299

            
300
static const cairo_surface_backend_t cairo_type3_glyph_surface_backend = {
301
    CAIRO_INTERNAL_SURFACE_TYPE_TYPE3_GLYPH,
302
    _cairo_type3_glyph_surface_finish,
303

            
304
    _cairo_default_context_create, /* XXX usable through a context? */
305

            
306
    NULL, /* create similar */
307
    NULL, /* create similar image */
308
    NULL, /* map to image */
309
    NULL, /* unmap image */
310

            
311
    NULL, /* source */
312
    NULL, /* acquire_source_image */
313
    NULL, /* release_source_image */
314
    NULL, /* snapshot */
315

            
316
    NULL, /* copy page */
317
    NULL, /* show page */
318

            
319
    NULL, /* _cairo_type3_glyph_surface_get_extents */
320
    NULL, /* _cairo_type3_glyph_surface_get_font_options */
321

            
322
    NULL, /* flush */
323
    NULL, /* mark_dirty_rectangle */
324

            
325
    _cairo_type3_glyph_surface_paint,
326
    _cairo_type3_glyph_surface_mask,
327
    _cairo_type3_glyph_surface_stroke,
328
    _cairo_type3_glyph_surface_fill,
329
    NULL, /* fill-stroke */
330
    _cairo_type3_glyph_surface_show_glyphs,
331
};
332

            
333
static void
334
_cairo_type3_glyph_surface_set_stream (cairo_type3_glyph_surface_t *surface,
335
				       cairo_output_stream_t       *stream)
336
{
337
    surface->stream = stream;
338
    _cairo_pdf_operators_set_stream (&surface->pdf_operators, stream);
339
}
340

            
341
static cairo_status_t
342
_cairo_type3_glyph_surface_emit_fallback_image (cairo_type3_glyph_surface_t *surface,
343
						unsigned long		     glyph_index)
344
{
345
    cairo_scaled_glyph_t *scaled_glyph;
346
    cairo_status_t status;
347
    cairo_image_surface_t *image;
348
    cairo_matrix_t mat;
349
    double x, y;
350

            
351
    status = _cairo_scaled_glyph_lookup (surface->scaled_font,
352
					 glyph_index,
353
					 CAIRO_SCALED_GLYPH_INFO_METRICS |
354
					 CAIRO_SCALED_GLYPH_INFO_SURFACE,
355
					 NULL, /* foreground color */
356
					 &scaled_glyph);
357
    if (unlikely (status))
358
	return status;
359

            
360
    image = scaled_glyph->surface;
361
    if (image->width == 0 || image->height == 0)
362
	return CAIRO_STATUS_SUCCESS;
363

            
364
    x = _cairo_fixed_to_double (scaled_glyph->bbox.p1.x);
365
    y = _cairo_fixed_to_double (scaled_glyph->bbox.p2.y);
366
    cairo_matrix_init(&mat, image->width, 0,
367
		      0, -image->height,
368
		      x, y);
369
    cairo_matrix_multiply (&mat, &mat, &surface->scaled_font->scale_inverse);
370

            
371
    return _cairo_type3_glyph_surface_emit_image (surface, image, &mat);
372
}
373

            
374
void
375
_cairo_type3_glyph_surface_set_font_subsets_callback (void		     		    *abstract_surface,
376
						      cairo_pdf_operators_use_font_subset_t  use_font_subset,
377
						      void				    *closure)
378
{
379
    cairo_type3_glyph_surface_t *surface = abstract_surface;
380

            
381
    if (unlikely (surface->base.status))
382
	return;
383

            
384
    _cairo_pdf_operators_set_font_subsets_callback (&surface->pdf_operators,
385
						    use_font_subset,
386
						    closure);
387
}
388

            
389
cairo_status_t
390
_cairo_type3_glyph_surface_analyze_glyph (void		     *abstract_surface,
391
					  unsigned long	      glyph_index)
392
{
393
    cairo_type3_glyph_surface_t *surface = abstract_surface;
394
    cairo_scaled_glyph_t *scaled_glyph;
395
    cairo_int_status_t status, status2;
396
    cairo_output_stream_t *null_stream;
397

            
398
    if (unlikely (surface->base.status))
399
	return surface->base.status;
400

            
401
    null_stream = _cairo_null_stream_create ();
402
    if (unlikely (null_stream->status))
403
	return null_stream->status;
404

            
405
    _cairo_type3_glyph_surface_set_stream (surface, null_stream);
406

            
407
    _cairo_scaled_font_freeze_cache (surface->scaled_font);
408
    status = _cairo_scaled_glyph_lookup (surface->scaled_font,
409
					 glyph_index,
410
					 CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE,
411
					 NULL, /* foreground color */
412
					 &scaled_glyph);
413

            
414
    if (_cairo_int_status_is_error (status))
415
	goto cleanup;
416

            
417
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
418
	status = CAIRO_INT_STATUS_SUCCESS;
419
	goto cleanup;
420
    }
421

            
422
    status = _cairo_recording_surface_replay (scaled_glyph->recording_surface,
423
					      &surface->base);
424
    if (unlikely (status))
425
	goto cleanup;
426

            
427
    status = _cairo_pdf_operators_flush (&surface->pdf_operators);
428
    if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK)
429
	status = CAIRO_INT_STATUS_SUCCESS;
430

            
431
cleanup:
432
    _cairo_scaled_font_thaw_cache (surface->scaled_font);
433

            
434
    status2 = _cairo_output_stream_destroy (null_stream);
435
    if (status == CAIRO_INT_STATUS_SUCCESS)
436
	status = status2;
437

            
438
    return status;
439
}
440

            
441
cairo_status_t
442
_cairo_type3_glyph_surface_emit_glyph (void		     *abstract_surface,
443
				       cairo_output_stream_t *stream,
444
				       unsigned long	      glyph_index,
445
				       cairo_box_t           *bbox,
446
				       double                *width)
447
{
448
    cairo_type3_glyph_surface_t *surface = abstract_surface;
449
    cairo_scaled_glyph_t *scaled_glyph;
450
    cairo_int_status_t status, status2;
451
    double x_advance, y_advance;
452
    cairo_matrix_t font_matrix_inverse;
453

            
454
    if (unlikely (surface->base.status))
455
	return surface->base.status;
456

            
457
    _cairo_type3_glyph_surface_set_stream (surface, stream);
458

            
459
    _cairo_scaled_font_freeze_cache (surface->scaled_font);
460

            
461
    /* Check if this is a color glyph and bail out if it is */
462
    status = _cairo_scaled_glyph_lookup (surface->scaled_font,
463
					 glyph_index,
464
					 CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE,
465
					 NULL, /* foreground color */
466
					 &scaled_glyph);
467
    if (status == CAIRO_INT_STATUS_SUCCESS) {
468
	status = CAIRO_INT_STATUS_UNSUPPORTED;
469
	goto FAIL;
470
    } status = _cairo_scaled_glyph_lookup (surface->scaled_font,
471
					   glyph_index,
472
					   CAIRO_SCALED_GLYPH_INFO_METRICS |
473
					   CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE,
474
					   NULL, /* foreground color */
475
					   &scaled_glyph);
476
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
477
	status = _cairo_scaled_glyph_lookup (surface->scaled_font,
478
					     glyph_index,
479
					     CAIRO_SCALED_GLYPH_INFO_METRICS,
480
					     NULL, /* foreground color */
481
					     &scaled_glyph);
482
	if (status == CAIRO_INT_STATUS_SUCCESS)
483
	    status = CAIRO_INT_STATUS_IMAGE_FALLBACK;
484
    }
485
    if (_cairo_int_status_is_error (status)) {
486
	_cairo_scaled_font_thaw_cache (surface->scaled_font);
487
	return status;
488
    }
489

            
490
    x_advance = scaled_glyph->metrics.x_advance;
491
    y_advance = scaled_glyph->metrics.y_advance;
492
    font_matrix_inverse = surface->scaled_font->font_matrix;
493
    status2 = cairo_matrix_invert (&font_matrix_inverse);
494

            
495
    /* The invertability of font_matrix is tested in
496
     * pdf_operators_show_glyphs before any glyphs are mapped to the
497
     * subset. */
498
    assert (status2 == CAIRO_INT_STATUS_SUCCESS);
499

            
500
    cairo_matrix_transform_distance (&font_matrix_inverse, &x_advance, &y_advance);
501
    *width = x_advance;
502

            
503
    *bbox = scaled_glyph->bbox;
504
    _cairo_matrix_transform_bounding_box_fixed (&surface->scaled_font->scale_inverse,
505
						bbox, NULL);
506

            
507
    _cairo_output_stream_printf (surface->stream,
508
				 "%f 0 %f %f %f %f d1\n",
509
                                 x_advance,
510
				 _cairo_fixed_to_double (bbox->p1.x),
511
				 _cairo_fixed_to_double (bbox->p1.y),
512
				 _cairo_fixed_to_double (bbox->p2.x),
513
				 _cairo_fixed_to_double (bbox->p2.y));
514

            
515
    if (status == CAIRO_INT_STATUS_SUCCESS) {
516
	cairo_output_stream_t *mem_stream;
517

            
518
	mem_stream = _cairo_memory_stream_create ();
519
	status = mem_stream->status;
520
	if (unlikely (status))
521
	    goto FAIL;
522

            
523
	_cairo_type3_glyph_surface_set_stream (surface, mem_stream);
524

            
525
	_cairo_output_stream_printf (surface->stream, "q\n");
526
	status = _cairo_recording_surface_replay (scaled_glyph->recording_surface,
527
						  &surface->base);
528

            
529
	status2 = _cairo_pdf_operators_flush (&surface->pdf_operators);
530
	if (status == CAIRO_INT_STATUS_SUCCESS)
531
	    status = status2;
532

            
533
	_cairo_output_stream_printf (surface->stream, "Q\n");
534

            
535
	_cairo_type3_glyph_surface_set_stream (surface, stream);
536
	if (status == CAIRO_INT_STATUS_SUCCESS)
537
	    _cairo_memory_stream_copy (mem_stream, stream);
538

            
539
	status2 = _cairo_output_stream_destroy (mem_stream);
540
	if (status == CAIRO_INT_STATUS_SUCCESS)
541
	    status = status2;
542
    }
543

            
544
    if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK)
545
	status = _cairo_type3_glyph_surface_emit_fallback_image (surface, glyph_index);
546

            
547
  FAIL:
548
    _cairo_scaled_font_thaw_cache (surface->scaled_font);
549

            
550
    return status;
551
}
552

            
553
#endif /* CAIRO_HAS_FONT_SUBSET */