1 /* Copyright (C) 1999, 2000 artofcode LLC. All rights reserved. 2 3 This program is free software; you can redistribute it and/or modify it 4 under the terms of the GNU General Public License as published by the 5 Free Software Foundation; either version 2 of the License, or (at your 6 option) any later version. 7 8 This program is distributed in the hope that it will be useful, but 9 WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along 14 with this program; if not, write to the Free Software Foundation, Inc., 15 59 Temple Place, Suite 330, Boston, MA, 02111-1307. 16 17 */ 18 19 /*$Id: gdevpdff.h,v 1.11.2.1.2.1 2003/01/17 00:49:01 giles Exp $ */ 20 /* Font-related definitions for PDF-writing driver. */ 21 22 #ifndef gdevpdff_INCLUDED 23 # define gdevpdff_INCLUDED 24 25 /* ================ Types and structures ================ */ 26 27 /* 28 * The PDF writer creates several different kinds of font resources. 29 * The key differences between them are the values of num_chars, index, 30 * and descriptor. 31 * 32 * - Synthesized Type 3 bitmap fonts are identified by num_chars != 0 (or 33 * equivalently PDF_FONT_IS_SYNTHESIZED = true). They have index < 0, 34 * FontDescriptor == 0. All other fonts have num_chars == 0. 35 * 36 * - Type 0 fonts have num_chars == 0, index < 0, FontDescriptor == 0. 37 * All other non-synthesized fonts have FontDescriptor != 0. 38 * 39 * - The base 14 fonts (when not embedded) have num_chars == 0, index 40 * >= 0, FontDescriptor != 0, FontDescriptor->base_font == 0. All 41 * other fonts have index < 0. 42 * 43 * - All other fonts, embedded or not, have num_chars == 0, index < 0, 44 * FontDescriptor != 0, FontDescriptor->base_font != 0. A font is 45 * embedded iff FontDescriptor->FontFile_id != 0. 46 * 47 * For non-synthesized fonts, the structure representation is designed to 48 * represent directly the information that will be written in the font 49 * resource, Encoding, and FontDescriptor dictionaries. See the comments 50 * on the pdf_font_t structure below for more detail. 51 */ 52 53 /* 54 * PDF font names must be large enough for the 14 built-in fonts, 55 * and also large enough for any reasonable font name + 7 characters 56 * for the subsetting prefix + a suffix derived from the PDF object number. 57 */ 58 #define SUBSET_PREFIX_SIZE 7 59 #define MAX_PDF_FONT_NAME\ 60 (SUBSET_PREFIX_SIZE + gs_font_name_max + 1 + 1 + sizeof(long) * 2) 61 typedef struct pdf_font_name_s { 62 byte chars[MAX_PDF_FONT_NAME]; 63 uint size; 64 } pdf_font_name_t; 65 66 /* ---------------- Font descriptor (pseudo-resource) ---------------- */ 67 68 /* 69 * A FontDescriptor refers to an unscaled, possibly built-in base_font. 70 * Multiple pdf_fonts with the same outlines (but possibly different 71 * encodings, metrics, and scaling) may share a single FontDescriptor. 72 * Each such pdf_font refers to a corresponding gs_font object, and 73 * the gs_fonts of all such pdf_fonts will have the base_font of the 74 * FontDescriptor as their eventual base font (through a chain of 0 or 75 * more base pointers). 76 * 77 * Since gs_font objects may be freed at any time, we register a procedure 78 * to be called when that happens. The (gs_)font of a pdf_font may be freed 79 * either before or after the base_font in the FontDescriptor. Therefore, a 80 * pdf_font copies enough information from its gs_font that when the gs_font 81 * is freed, the pdf_font still has enough information to write the Font 82 * resource dictionary at a later time. When freeing a gs_font referenced 83 * from a pdf_font, we only clear the pointer to it from its referencing 84 * pdf_font. However, when the base_font of a FontDescriptor is about to be 85 * freed, we must write the FontDescriptor, and, if the font is embedded, 86 * the FontFile data. 87 */ 88 89 /* 90 * Font descriptors are handled as pseudo-resources. Multiple pdf_fonts 91 * may share a descriptor. We don't need to use reference counting to 92 * keep track of this, since all descriptors persist until the device 93 * is closed, even though the base_font they reference may have been 94 * freed. 95 */ 96 typedef struct pdf_font_descriptor_values_s { 97 font_type FontType; /* copied from font */ 98 /* Required elements */ 99 int Ascent, CapHeight, Descent, ItalicAngle, StemV; 100 gs_int_rect FontBBox; 101 uint Flags; 102 /* Optional elements (default to 0) */ 103 int AvgWidth, Leading, MaxWidth, MissingWidth, StemH, XHeight; 104 } pdf_font_descriptor_values_t; 105 106 #ifndef pdf_font_descriptor_DEFINED 107 # define pdf_font_descriptor_DEFINED 108 typedef struct pdf_font_descriptor_s pdf_font_descriptor_t; 109 #endif 110 111 /* 112 * Define whether an embedded font must, may, or must not be subsetted. 113 */ 114 typedef enum { 115 FONT_SUBSET_OK, 116 FONT_SUBSET_YES, 117 FONT_SUBSET_NO 118 } pdf_font_do_subset_t; 119 120 typedef enum { 121 FONT_EMBED_STANDARD, /* 14 standard fonts */ 122 FONT_EMBED_NO, 123 FONT_EMBED_YES 124 } pdf_font_embed_t; 125 126 struct pdf_font_descriptor_s { 127 pdf_resource_common(pdf_font_descriptor_t); 128 pdf_font_name_t FontName; 129 pdf_font_descriptor_values_t values; 130 gs_matrix orig_matrix; /* unscaled font matrix */ 131 uint chars_count; /* size of chars_used in bits */ 132 gs_string chars_used; /* 1 bit per character code or CID */ 133 uint glyphs_count; /* size of glyphs_used in bits */ 134 gs_string glyphs_used; /* 1 bit per glyph, for TrueType fonts */ 135 pdf_font_do_subset_t do_subset; 136 long FontFile_id; /* non-0 iff the font is embedded */ 137 gs_font *base_font; /* unscaled font defining the base encoding, */ 138 /* matrix, and character set, 0 iff */ 139 /* non-embedded standard font */ 140 bool notified; /* if true, base_font will notify on freeing */ 141 bool written; /* if true, descriptor has been written out */ 142 }; 143 /* Flag bits */ 144 /*#define FONT_IS_FIXED_WIDTH (1<<0)*/ /* defined in gxfont.h */ 145 #define FONT_IS_SERIF (1<<1) 146 #define FONT_IS_SYMBOLIC (1<<2) 147 #define FONT_IS_SCRIPT (1<<3) 148 /* 149 * There is confusion over the meaning of the 1<<5 bit. According to the 150 * published PDF documentation, in PDF 1.1, it meant "font uses 151 * StandardEncoding", and as of PDF 1.2, it means "font uses (a subset of) 152 * the Adobe standard Latin character set"; however, Acrobat Reader 3 and 4 153 * seem to use the former interpretation, even if the font is embedded and 154 * the file is identified as a PDF 1.2 file. We have to use the former 155 * interpretation in order to produce output that Acrobat will handle 156 * correctly. 157 */ 158 #define FONT_USES_STANDARD_ENCODING (1<<5) /* always used */ 159 #define FONT_IS_ADOBE_ROMAN (1<<5) /* never used */ 160 #define FONT_IS_ITALIC (1<<6) 161 #define FONT_IS_ALL_CAPS (1<<16) 162 #define FONT_IS_SMALL_CAPS (1<<17) 163 #define FONT_IS_FORCE_BOLD (1<<18) 164 /* 165 * Font descriptors are pseudo-resources, so their GC descriptors 166 * must be public. 167 */ 168 #define public_st_pdf_font_descriptor() /* in gdevpdff.c */\ 169 BASIC_PTRS(pdf_font_descriptor_ptrs) {\ 170 GC_STRING_ELT(pdf_font_descriptor_t, chars_used),\ 171 GC_STRING_ELT(pdf_font_descriptor_t, glyphs_used),\ 172 GC_OBJ_ELT(pdf_font_descriptor_t, base_font)\ 173 };\ 174 gs_public_st_basic_super(st_pdf_font_descriptor, pdf_font_descriptor_t,\ 175 "pdf_font_descriptor_t", pdf_font_descriptor_ptrs,\ 176 pdf_font_descriptor_data, &st_pdf_resource, 0) 177 178 /* ---------------- Font (resource) ---------------- */ 179 180 typedef struct pdf_char_proc_s pdf_char_proc_t; /* forward reference */ 181 /*typedef struct pdf_font_s pdf_font_t;*/ 182 typedef struct pdf_encoding_element_s { 183 gs_glyph glyph; 184 gs_const_string str; 185 } pdf_encoding_element_t; 186 #define private_st_pdf_encoding_element()\ 187 gs_private_st_composite(st_pdf_encoding_element, pdf_encoding_element_t,\ 188 "pdf_encoding_element_t[]", pdf_encoding_elt_enum_ptrs,\ 189 pdf_encoding_elt_reloc_ptrs) 190 /* 191 * Structure elements beginning with a capital letter correspond directly 192 * to keys in the font or Encoding dictionary. Currently these are: 193 * (font) FontType, FirstChar, LastChar, Widths, FontDescriptor, 194 * DescendantFont[s] 195 * (Encoding) BaseEncoding, Differences 196 * This structure is untidy: it is really a union of 4 different variants 197 * (plain font, composite font, CIDFont, synthesized font). It's simpler 198 * to "flatten" the union. 199 */ 200 struct pdf_font_s { 201 pdf_resource_common(pdf_font_t); 202 pdf_font_name_t fname; 203 font_type FontType; 204 gs_font *font; /* non-0 iff font will notify us; */ 205 /* should be a weak pointer */ 206 int index; /* in pdf_standard_fonts, -1 if not base 14 */ 207 pdf_font_embed_t embed; /* status of pdf_font_embed_status() */ 208 gs_matrix orig_matrix; /* FontMatrix of unscaled font for embedding */ 209 bool is_MM_instance; /* for Type 1/2 fonts, true iff the font */ 210 /* is a Multiple Master instance */ 211 212 /* Members for all non-synthesized fonts. */ 213 214 pdf_font_descriptor_t *FontDescriptor; /* 0 for composite */ 215 bool write_Widths; 216 int *Widths; /* [256] for non-composite, non-CID */ 217 byte *widths_known; /* 1 bit per Widths element */ 218 219 /* Members for non-synthesized fonts other than composite or CID. */ 220 221 int FirstChar, LastChar; 222 /* Encoding for base fonts. */ 223 gs_encoding_index_t BaseEncoding; 224 pdf_encoding_element_t *Differences; /* [256] */ 225 226 /* Members for composite fonts (with FMapType = fmap_CMap). */ 227 228 pdf_font_t *DescendantFont; /* CIDFont */ 229 char cmapname[max( /* standard name or <id> 0 R */ 230 16, /* UniJIS-UCS2-HW-H */ 231 sizeof(long) * 8 / 3 + 1 + 4 /* <id> 0 R */ 232 ) + 1]; 233 font_type sub_font_type; /* FontType of DescendantFont */ 234 235 /* Members for CIDFonts. */ 236 237 long CIDSystemInfo_id; 238 pdf_font_t *glyphshow_font; /* type 0 font created for glyphshow */ 239 ushort *CIDToGIDMap; /* CIDFontType 2 only */ 240 241 /* Members for synthesized fonts. */ 242 243 int num_chars; 244 #define PDF_FONT_IS_SYNTHESIZED(pdfont) ((pdfont)->num_chars != 0) 245 pdf_char_proc_t *char_procs; 246 int max_y_offset; 247 /* Pseudo-characters for spacing. */ 248 /* The range should be determined by the device resolution.... */ 249 /*#define X_SPACE_MIN xxx*/ /* in gdevpdfx.h */ 250 /*#define X_SPACE_MAX nnn*/ /* in gdevpdfx.h */ 251 byte spaces[X_SPACE_MAX - X_SPACE_MIN + 1]; 252 }; 253 254 /* The descriptor is public for pdf_resource_type_structs. */ 255 #define public_st_pdf_font()\ 256 gs_public_st_suffix_add9(st_pdf_font, pdf_font_t, "pdf_font_t",\ 257 pdf_font_enum_ptrs, pdf_font_reloc_ptrs, st_pdf_resource,\ 258 font, FontDescriptor, Widths, widths_known, Differences, DescendantFont,\ 259 glyphshow_font, CIDToGIDMap, char_procs) 260 261 /* CharProc pseudo-resources for synthesized fonts */ 262 struct pdf_char_proc_s { 263 pdf_resource_common(pdf_char_proc_t); 264 pdf_font_t *font; 265 pdf_char_proc_t *char_next; /* next char_proc for same font */ 266 int width, height; 267 int x_width; /* X escapement */ 268 int y_offset; /* of character (0,0) */ 269 byte char_code; 270 }; 271 272 /* The descriptor is public for pdf_resource_type_structs. */ 273 #define public_st_pdf_char_proc()\ 274 gs_public_st_suffix_add2(st_pdf_char_proc, pdf_char_proc_t,\ 275 "pdf_char_proc_t", pdf_char_proc_enum_ptrs,\ 276 pdf_char_proc_reloc_ptrs, st_pdf_resource, font, char_next) 277 278 /* ================ Procedures ================ */ 279 280 /* ---------------- Exported by gdevpdft.c ---------------- */ 281 282 /* For gdevpdfs.c */ 283 284 /* Define the text enumerator. */ 285 typedef struct pdf_text_enum_s { 286 gs_text_enum_common; 287 gs_text_enum_t *pte_default; 288 gs_fixed_point origin; 289 } pdf_text_enum_t; 290 #define private_st_pdf_text_enum() /* in gdevpdft.c */\ 291 extern_st(st_gs_text_enum);\ 292 gs_private_st_suffix_add1(st_pdf_text_enum, pdf_text_enum_t,\ 293 "pdf_text_enum_t", pdf_text_enum_enum_ptrs, pdf_text_enum_reloc_ptrs,\ 294 st_gs_text_enum, pte_default) 295 296 /* 297 * Set the current font and size, writing a Tf command if needed. 298 */ 299 int pdf_set_font_and_size(P3(gx_device_pdf * pdev, pdf_font_t * font, 300 floatp size)); 301 /* 302 * Set the text matrix for writing text, writing a Tm, TL, or Tj command 303 * if needed. 304 */ 305 int pdf_set_text_matrix(P2(gx_device_pdf * pdev, const gs_matrix * pmat)); 306 307 /* 308 * Append characters to a string being accumulated. 309 */ 310 int pdf_append_chars(P3(gx_device_pdf * pdev, const byte * str, uint size)); 311 312 /* For gdevpdfb.c */ 313 314 /* Begin a CharProc for an embedded (bitmap) font. */ 315 int pdf_begin_char_proc(P8(gx_device_pdf * pdev, int w, int h, int x_width, 316 int y_offset, gs_id id, pdf_char_proc_t **ppcp, 317 pdf_stream_position_t * ppos)); 318 319 /* End a CharProc. */ 320 int pdf_end_char_proc(P2(gx_device_pdf * pdev, pdf_stream_position_t * ppos)); 321 322 /* Put out a reference to an image as a character in an embedded font. */ 323 int pdf_do_char_image(P3(gx_device_pdf * pdev, const pdf_char_proc_t * pcp, 324 const gs_matrix * pimat)); 325 326 /* ---------------- Exported by gdevpdfs.c for gdevpdft.c ---------------- */ 327 328 /* ---------------- Exported by gdevpdff.c ---------------- */ 329 330 typedef struct pdf_standard_font_s { 331 const char *fname; 332 gs_encoding_index_t base_encoding; 333 } pdf_standard_font_t; 334 extern const pdf_standard_font_t pdf_standard_fonts[]; 335 336 /* Return the index of a standard font name, or -1 if missing. */ 337 int pdf_find_standard_font(P2(const byte *str, uint size)); 338 339 /* 340 * Compute and return the orig_matrix of a font. 341 */ 342 int pdf_font_orig_matrix(P2(const gs_font *font, gs_matrix *pmat)); 343 344 /* 345 * Find the original (unscaled) standard font corresponding to an 346 * arbitrary font, if any. Return its index in standard_fonts, or -1. 347 */ 348 int pdf_find_orig_font(P3(gx_device_pdf *pdev, gs_font *font, 349 gs_matrix *pfmat)); 350 351 /* 352 * Determine the embedding status of a font. If the font is in the base 353 * 14, store its index (0..13) in *pindex and its similarity to the base 354 * font (as determined by the font's same_font procedure) in *psame. 355 */ 356 pdf_font_embed_t pdf_font_embed_status(P4(gx_device_pdf *pdev, gs_font *font, 357 int *pindex, int *psame)); 358 359 /* 360 * Determine the resource type (resourceFont or resourceCIDFont) for 361 * a font. 362 */ 363 pdf_resource_type_t pdf_font_resource_type(P1(const gs_font *font)); 364 365 /* 366 * Allocate a font resource. If pfd != 0, a FontDescriptor is allocated, 367 * with its id, values, and char_count taken from *pfd. 368 * If font != 0, its FontType is used to determine whether the resource 369 * is of type Font or of (pseudo-)type CIDFont; in this case, pfres->font 370 * and pfres->FontType are also set. 371 */ 372 int pdf_alloc_font(P5(gx_device_pdf *pdev, gs_id rid, pdf_font_t **ppfres, 373 const pdf_font_descriptor_t *pfd, 374 gs_font *font)); 375 376 /* 377 * Create a new pdf_font for a gs_font. This procedure is only intended 378 * to be called from one place in gdevpdft.c. 379 */ 380 int pdf_create_pdf_font(P4(gx_device_pdf *pdev, gs_font *font, 381 const gs_matrix *pomat, pdf_font_t **pppf)); 382 383 /* 384 * Determine whether a font is a subset font by examining the name. 385 */ 386 bool pdf_has_subset_prefix(P2(const byte *str, uint size)); 387 388 /* 389 * Make the prefix for a subset font from the font's resource ID. 390 */ 391 void pdf_make_subset_prefix(P3(const gx_device_pdf *pdev, byte *str, 392 ulong id)); 393 394 /* 395 * Adjust the FontName of a newly created FontDescriptor so that it is 396 * unique if necessary. If the name was changed, return 1. 397 */ 398 int pdf_adjust_font_name(P3(const gx_device_pdf *pdev, 399 pdf_font_descriptor_t *pfd, 400 bool is_standard)); 401 402 /* Add an encoding difference to a font. */ 403 int pdf_add_encoding_difference(P5(gx_device_pdf *pdev, pdf_font_t *ppf, 404 int chr, gs_font_base *bfont, 405 gs_glyph glyph)); 406 407 /* 408 * Get the width of a given character in a (base) font. May add the width 409 * to the widths cache (ppf->Widths). 410 */ 411 int pdf_char_width(P4(pdf_font_t *ppf, int ch, gs_font *font, 412 int *pwidth /* may be NULL */)); 413 414 /* 415 * Get the width of a glyph in a (base) font. Return 1 if the width should 416 * not be cached. 417 */ 418 int pdf_glyph_width(P4(pdf_font_t *ppf, gs_glyph glyph, gs_font *font, 419 int *pwidth /* must not be NULL */)); 420 421 /* 422 * Find the range of character codes that includes all the defined 423 * characters in a font. 424 */ 425 void pdf_find_char_range(P3(gs_font *font, int *pfirst, int *plast)); 426 427 /* Compute the FontDescriptor for a font or a font subset. */ 428 int pdf_compute_font_descriptor(P4(gx_device_pdf *pdev, 429 pdf_font_descriptor_t *pfd, gs_font *font, 430 const byte *used /*[32]*/)); 431 432 /* Unregister the standard fonts when cleaning up. */ 433 void pdf_unregister_fonts(P1(gx_device_pdf *pdev)); 434 435 /* ---------------- Exported by gdevpdfw.c ---------------- */ 436 437 /* Register a font for eventual writing (embedded or not). */ 438 int pdf_register_font(P3(gx_device_pdf *pdev, gs_font *font, pdf_font_t *ppf)); 439 440 /* Write out the font resources when wrapping up the output. */ 441 int pdf_write_font_resources(P1(gx_device_pdf *pdev)); 442 443 /* 444 * Write a font descriptor. 445 * (Exported only for gdevpdfe.c.) 446 */ 447 int pdf_write_FontDescriptor(P2(gx_device_pdf *pdev, 448 const pdf_font_descriptor_t *pfd)); 449 450 /* 451 * Write CIDSystemInfo for a CIDFont or CMap. 452 * (Exported only for gdevpdff.c.) 453 */ 454 int pdf_write_CIDFont_system_info(P2(gx_device_pdf *pdev, 455 const gs_font *pcidfont)); 456 #ifndef gs_cmap_DEFINED 457 # define gs_cmap_DEFINED 458 typedef struct gs_cmap_s gs_cmap_t; 459 #endif 460 int pdf_write_CMap_system_info(P2(gx_device_pdf *pdev, 461 const gs_cmap_t *pcmap)); 462 463 /* ---------------- Exported by gdevpdfe.c ---------------- */ 464 465 /* 466 * Write the FontDescriptor and FontFile* data for an embedded font. 467 * Return a rangecheck error if the font can't be embedded. 468 * (Exported only for gdevpdfw.c.) 469 */ 470 int pdf_write_embedded_font(P2(gx_device_pdf *pdev, 471 pdf_font_descriptor_t *pfd)); 472 473 #endif /* gdevpdff_INCLUDED */ 474