gdevpdff.h 17 KB

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