1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gdevpdtf.h 8621 2008-04-04 08:39:33Z ken $ */
15
/* Font and CMap resource structure and API for pdfwrite */
17
#ifndef gdevpdtf_INCLUDED
18
# define gdevpdtf_INCLUDED
22
/* ================ Types and structures ================ */
24
/* ---------------- Font resources ---------------- */
27
* pdfwrite manages several different flavors of font resources:
29
* Those that have neither a FontDescriptor nor a base_font:
30
* Type 0 (composite) fonts
31
* Those that have no FontDescriptor, but do have a base_font:
33
* Those that have a FontDescriptor but no base_font:
35
* Those that have a FontDescriptor with a base_font:
36
* Type 1 / Type 2 fonts
37
* Type 42 (TrueType) fonts
38
* CIDFontType 0 (Type 1/2) CIDFonts
39
* CIDFontType 2 (TrueType) CIDFonts
42
* Font names in PDF files have caused an enormous amount of trouble, so we
43
* document specifically how they are handled in each structure.
45
* The PDF Reference specifies the BaseFont of a font resource as follows,
46
* depending on the font type:
48
* Type 0 - if the descendant font is CIDFontType 0, the descendant font
49
* name followed by a hyphen and the CMap name (the value of Encoding,
50
* if a name, otherwise the CMapName from the CMap); if the descendant
51
* font is CIDFontType 2, the descendant font name.
53
* Type 1 - "usually" the same as the FontName in the base font.
55
* MM Type 1 - if embedded, the same as Type 1; if not embedded, spaces
56
* in the font name are replaced with underscores.
60
* TrueType - initially, the PostScript name from the 'name' table in
61
* the font; if none, the "name by which the font is known in the host
62
* operating system". Spaces are removed. Then, under circumstances
63
* not defined, the string ",Bold", ",Italic", or ",BoldItalic" is
64
* appended if the font has the corresponding style properties.
65
* [We do not do this: we simply use the key_name or font_name.]
67
* CIDFontType 0 - "usually" the same as the CIDFontName in the base font.
69
* CIDFontType 2 - the same as TrueType.
71
* In addition, the BaseFont has a XXXXXX+ prefix if the font is a subset
72
* (whether embedded or not).
74
* We would like to compute the BaseFont at the time that we create the font
75
* resource object. The font descriptor (which is needed to provide
76
* information about embedding) and the base font are both available at that
77
* time. Unfortunately, the information as to whether the font will be
78
* subsetted is not available. Therefore, we do compute the BaseFont from
79
* the base font name when the font resource is created, to allow checking
80
* for duplicate names and for standard font names, but we compute it again
81
* after writing out the base font.
84
#ifndef gs_cmap_DEFINED
85
# define gs_cmap_DEFINED
86
typedef struct gs_cmap_s gs_cmap_t;
89
#ifndef gs_font_type0_DEFINED
90
# define gs_font_type0_DEFINED
91
typedef struct gs_font_type0_s gs_font_type0;
94
#ifndef pdf_base_font_DEFINED
95
# define pdf_base_font_DEFINED
96
typedef struct pdf_base_font_s pdf_base_font_t;
99
#ifndef pdf_font_descriptor_DEFINED
100
# define pdf_font_descriptor_DEFINED
101
typedef struct pdf_font_descriptor_s pdf_font_descriptor_t;
104
#ifndef pdf_char_glyph_pair_DEFINED
105
# define pdf_char_glyph_pair_DEFINED
106
typedef struct pdf_char_glyph_pair_s pdf_char_glyph_pair_t;
109
struct pdf_char_glyph_pair_s {
115
* The write_contents procedure is set by the implementation when the
116
* font resource is created. It is called after generic code has opened
117
* the resource object and written the Type, BaseFont (if any),
118
* FontDescriptor reference (if any), ToUnicode CMap reference (if any),
119
* and additional dictionary entries (if any).
120
* The write_contents procedure must write any remaining entries specific
121
* to the FontType, followed by the closing ">>", and then call
122
* pdf_end_separate. The reason for this division of function is to allow
123
* the write_contents procedure to write additional objects that the
124
* resource object references, after closing the resource object.
126
typedef int (*pdf_font_write_contents_proc_t)
127
(gx_device_pdf *, pdf_font_resource_t *);
130
* Define an element of an Encoding. The element is unused if glyph ==
133
typedef struct pdf_encoding_element_s {
136
bool is_difference; /* true if must be written in Differences */
137
} pdf_encoding_element_t;
138
#define private_st_pdf_encoding1() /* gdevpdtf.c */\
139
gs_private_st_const_strings1(st_pdf_encoding1,\
140
pdf_encoding_element_t, "pdf_encoding_element_t",\
141
pdf_encoding1_enum_ptrs, pdf_encoding1_reloc_ptrs, str)
142
#define private_st_pdf_encoding_element() /* gdevpdtf.c */\
143
gs_private_st_element(st_pdf_encoding_element, pdf_encoding_element_t,\
144
"pdf_encoding_element_t[]", pdf_encoding_elt_enum_ptrs,\
145
pdf_encoding_elt_reloc_ptrs, st_pdf_encoding1)
148
struct pdf_base_font_s {
150
* For the standard 14 fonts, copied == complete is a complete copy
151
* of the font, and DO_SUBSET = NO.
153
* For fonts that MAY be subsetted, copied is a partial copy,
154
* complete is a complete copy, and DO_SUBSET = UNKNOWN until
155
* pdf_font_do_subset is called.
157
* For fonts that MUST be subsetted, copied == complete is a partial
158
* copy, and DO_SUBSET = YES.
160
gs_font_base *copied;
161
gs_font_base *complete;
163
DO_SUBSET_UNKNOWN = 0,
169
* For CIDFonts, which are always subsetted, num_glyphs is CIDCount.
170
* For optionally subsetted fonts, num_glyphs is the count of glyphs
171
* in the font when originally copied. Note that if the font is
172
* downloaded incrementally, num_glyphs may be 0.
175
byte *CIDSet; /* for CIDFonts */
178
cos_dict_t *FontFile;
180
#define private_st_pdf_base_font()\
181
BASIC_PTRS(pdf_base_font_ptrs) {\
182
GC_OBJ_ELT(pdf_base_font_t, copied),\
183
GC_OBJ_ELT(pdf_base_font_t, complete),\
184
GC_OBJ_ELT(pdf_base_font_t, CIDSet),\
185
GC_OBJ_ELT(pdf_base_font_t, FontFile),\
186
GC_STRING_ELT(pdf_base_font_t, font_name)\
192
pdf_resource_type_t type;
193
} pdf_resource_ref_t;
196
* Widths are the widths in the outlines: this is what PDF interpreters
197
* use, and what will be written in the PDF file. real_widths are the
198
* widths possibly modified by Metrics[2] and CDevProc: these define the
199
* actual advance widths of the characters in the PostScript text.
201
struct pdf_font_resource_s {
202
pdf_resource_common(pdf_font_resource_t);
203
font_type FontType; /* copied from font, if any */
204
pdf_font_write_contents_proc_t write_contents;
205
gs_string BaseFont; /* (not used for Type 3) */
206
pdf_font_descriptor_t *FontDescriptor; /* (not used for Type 0, Type 3, */
207
/* or standard 14 fonts) */
209
* The base_font member is only used for
210
* the standard 14 fonts, which do not have a FontDescriptor.
212
pdf_base_font_t *base_font; /* == FontDescriptor->base_font */
213
uint count; /* # of chars/CIDs */
214
double *Widths; /* [count] (not used for Type 0) */
215
byte *used; /* [ceil(count/8)] bitmap of chars/CIDs used */
216
/* (not used for Type 0 or Type 3) */
217
pdf_resource_t *res_ToUnicode; /* CMap (not used for CIDFonts) */
218
gs_cmap_t *cmap_ToUnicode; /* CMap (not used for CIDFonts) */
219
gs_glyph_mark_proc_t mark_glyph;
220
void *mark_glyph_data; /* closure data */
225
pdf_font_resource_t *DescendantFont; /* CIDFont */
227
* The Encoding_name must be long enough to hold either the
228
* longest standard CMap name defined in the PDF Reference,
229
* or the longest reference to an embedded CMap (# 0 R).
231
char Encoding_name[max( /* standard name or <id> 0 R */
232
17, /* /UniJIS-UCS2-HW-H */
233
sizeof(long) * 8 / 3 + 1 + 4 /* <id> 0 R */
234
) + 1 /* \0 terminator */
236
gs_const_string CMapName; /* copied from the original CMap, */
237
/* or references the table of standard names */
238
uint font_index; /* The index of the descendent font in the source CMap. */
239
bool cmap_is_standard;
240
int WMode; /* of CMap */
246
/* [D]W[2] is Widths. */
247
long CIDSystemInfo_id; /* (written when font is allocated) */
248
ushort *CIDToGIDMap; /* (CIDFontType 2 only) [count] */
249
gs_id glyphshow_font_id;
250
double *Widths2; /* [count * 2] (x, y) */
251
double *v; /* [count] */
252
byte *used2; /* [(count + 7) / 8] */
253
pdf_font_resource_t *parent;
259
int FirstChar, LastChar; /* 0 <= FirstChar <= LastChar <= 255 */
261
* The BaseEncoding can only be ENCODING_INDEX_WINANSI,
262
* ENCODING_INDEX_MACROMAN, ENCODING_INDEX_MACEXPERT, or -1.
264
gs_encoding_index_t BaseEncoding;
265
gs_encoding_index_t preferred_encoding_index;
266
pdf_encoding_element_t *Encoding; /* [256], not for Type 3 */
267
gs_point *v; /* [256], glyph origin for WMode 1 */
268
int last_reserved_char; /* Except for synthesised Type 3,
269
which stores such data in LastChar */
277
struct /*truetype*/ {
279
* No extra info needed, but the ANSI standard doesn't
280
* allow empty structs.
287
gs_matrix FontMatrix;
288
pdf_char_proc_ownership_t *char_procs;
291
cos_dict_t *Resources;
301
/* The GC descriptor for resource types must be public. */
302
#define public_st_pdf_font_resource() /* gdevpdtf.c */\
303
gs_public_st_composite(st_pdf_font_resource, pdf_font_resource_t,\
304
"pdf_font_resource_t", pdf_font_resource_enum_ptrs,\
305
pdf_font_resource_reloc_ptrs)
308
* Define the possible embedding statuses of a font.
311
FONT_EMBED_STANDARD, /* 14 standard fonts */
316
/* ---------------- Global structures ---------------- */
319
* Define a structure for keeping track of the (unique) resource for
320
* each standard font. Note that standard fonts do not have descriptors:
321
* the base_font and copied_font members of the font resource provide the
322
* necessary information.
324
typedef struct pdf_standard_font_s {
325
pdf_font_resource_t *pdfont;
326
gs_matrix orig_matrix; /* ****** do we need this? */
327
} pdf_standard_font_t;
328
#define private_st_pdf_standard_font() /* gdevpdtf.c */\
329
gs_private_st_ptrs1(st_pdf_standard_font, pdf_standard_font_t,\
330
"pdf_standard_font_t", pdf_std_font_enum_ptrs, pdf_std_font_reloc_ptrs,\
332
#define private_st_pdf_standard_font_element() /* gdevpdtf.c */\
333
gs_private_st_element(st_pdf_standard_font_element, pdf_standard_font_t,\
334
"pdf_standard_font_t[]", pdf_std_font_elt_enum_ptrs,\
335
pdf_std_font_elt_reloc_ptrs, st_pdf_standard_font)
338
* There is a single instance (per device) of a structure that tracks global
339
* information about outline fonts. It is defined here, rather than
340
* opaquely in the implementation file, because the text processing code
341
* needs access to it.
344
/*typedef struct pdf_outline_fonts_s pdf_outline_fonts_t;*/ /* gdevpdtx.h */
345
struct pdf_outline_fonts_s {
346
pdf_standard_font_t *standard_fonts; /* [PDF_NUM_STANDARD_FONTS] */
348
#define private_st_pdf_outline_fonts() /* gdevpdtf.c */\
349
gs_private_st_ptrs1(st_pdf_outline_fonts, pdf_outline_fonts_t,\
350
"pdf_outline_fonts_t", pdf_outline_fonts_enum_ptrs,\
351
pdf_outline_fonts_reloc_ptrs, standard_fonts)
353
/* ================ Procedures ================ */
355
/* ---------------- Font resources ---------------- */
358
* Allocate and initialize bookkeeping for outline fonts.
360
pdf_outline_fonts_t *pdf_outline_fonts_alloc(gs_memory_t *mem);
363
* Return the standard fonts array.
365
pdf_standard_font_t *pdf_standard_fonts(const gx_device_pdf *pdev);
368
* Clean the standard fonts array.
370
void pdf_clean_standard_fonts(const gx_device_pdf *pdev);
372
/* Free font cache. */
373
int pdf_free_font_cache(gx_device_pdf *pdev);
376
* Allocate specific types of font resource.
378
int pdf_font_type0_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
379
gs_id rid, pdf_font_resource_t *DescendantFont,
380
const gs_const_string *CMapName);
381
int pdf_font_type3_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
382
pdf_font_write_contents_proc_t write_contents);
383
int pdf_font_std_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
384
bool is_original, gs_id rid, gs_font_base *pfont, int index);
385
int pdf_font_simple_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
386
gs_id rid, pdf_font_descriptor_t *pfd);
387
int pdf_font_cidfont_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
388
gs_id rid, pdf_font_descriptor_t *pfd);
389
int pdf_obtain_cidfont_widths_arrays(gx_device_pdf *pdev, pdf_font_resource_t *pdfont,
390
int wmode, double **w, double **w0, double **v);
391
int font_resource_encoded_alloc(gx_device_pdf *pdev, pdf_font_resource_t **ppfres,
392
gs_id rid, font_type ftype,
393
pdf_font_write_contents_proc_t write_contents);
394
int pdf_assign_font_object_id(gx_device_pdf *pdev, pdf_font_resource_t *pdfont);
396
/* Resize font resource arrays. */
397
int pdf_resize_resource_arrays(gx_device_pdf *pdev, pdf_font_resource_t *pfres,
401
* Return the (copied, subset or complete) font associated with a font resource.
402
* If this font resource doesn't have a descriptor (Type 0, Type 3, or
403
* standard 14), return 0.
405
gs_font_base *pdf_font_resource_font(const pdf_font_resource_t *pdfont, bool complete);
408
* Determine the embedding status of a font. If the font is in the base
409
* 14, store its index (0..13) in *pindex and its similarity to the base
410
* font (as determined by the font's same_font procedure) in *psame.
411
* (pindex and/or psame may be NULL.)
413
pdf_font_embed_t pdf_font_embed_status(gx_device_pdf *pdev, gs_font *font,
415
pdf_char_glyph_pair_t *pairs, int num_glyphs);
418
* Compute the BaseFont of a font according to the algorithm described
421
int pdf_compute_BaseFont(gx_device_pdf *pdev, pdf_font_resource_t *pdfont, bool finish);
424
* Close the text-related parts of a document, including writing out font
425
* and related resources.
427
int pdf_close_text_document(gx_device_pdf *pdev); /* in gdevpdtw.c */
430
* Convert True Type fonts into CID fonts for PDF/A.
432
int pdf_convert_truetype_font(gx_device_pdf *pdev, pdf_resource_t *pres);
435
* Convert True Type font descriptor into CID font descriptor for PDF/A.
437
int pdf_convert_truetype_font_descriptor(gx_device_pdf *pdev, pdf_font_resource_t *pdfont);
439
/* ---------------- CMap resources ---------------- */
442
* Allocate a CMap resource.
444
int pdf_cmap_alloc(gx_device_pdf *pdev, const gs_cmap_t *pcmap,
445
pdf_resource_t **ppres /* CMap */, int font_index_only);
448
* Add a CID-to-GID mapping to a CIDFontType 2 font resource.
450
int pdf_font_add_cid_to_gid(pdf_font_resource_t *pdfont, uint cid, uint gid);
452
#endif /* gdevpdtf_INCLUDED */