xref: /plan9/sys/src/cmd/gs/src/gdevpdtt.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2002 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevpdtt.h,v 1.25 2004/10/15 18:24:31 igor Exp $ */
18 /* Internal text processing interface for pdfwrite */
19 
20 #ifndef gdevpdtt_INCLUDED
21 #  define gdevpdtt_INCLUDED
22 
23 /*
24  * This file is only used internally to define the interface between
25  * gdevpdtt.c, gdevpdtc.c, and gdevpdte.c.
26  */
27 
28 /* ---------------- Coordinate systems ---------------- */
29 
30 /*
31 
32   The current text code has to deal with 6 different coordinate systems.
33   This situation is complex, confusing, and fragile, but despite literally
34   years of struggle we have been unable to understand how to simplify it.
35 
36   1) PostScript user space at the time a text operator is invoked.  The
37      width values for ashow, xshow, etc. are specified in this space: these
38      values appear in the delta_all, delta_space, x_widths, and y_widths
39      members of the text member of the common part of the text enumerator
40      (pdf_text_enum_t).
41 
42   2) Device space.  For the pdfwrite device, this is a straightforward space
43      with (0,0) in the lower left corner.  The usual resolution is 720 dpi,
44      but this is user-settable to provide a tradeoff between file size and
45      bitmap resolution (for rendered Patterns and fonts that have to be
46      converted to bitmaps).
47 
48   3) PDF user space.  During the processing of text operators, this is
49      always the same as the default PDF user space, in which 1 unit = 1/72",
50      because an Acrobat quirk apparently requires this.  (See stream_to_text
51      in gdevpdfu.c.)
52 
53   4) Font design space.  This is the space in which the font's character
54      outlines and advance widths are specified, including the width values
55      returned by font->procs.glyph_info and by pdf_char_widths.
56 
57   5) PDF unscaled text space.  This space is used for the PDF width
58      adjustment values (Tc, Tw, and TL parameters, and " operator) and
59      positioning coordinates (Td and TD operators).
60 
61   6) PDF text space.  This space is used for the width adjustments for the
62      TJ operator.
63 
64   The following convert between these spaces:
65 
66   - The PostScript CTM (pte->pis->ctm) maps #1 to #2.
67 
68   - The mapping from #3 to #2 is a scaling by pdev->HWResolution / 72.
69 
70   - The mapping from #5 to #6 is a scaling by the size member of
71     pdf_text_state_values_t, which is the size value for the Tf operator.
72 
73   - The matrix member of pdf_text_state_values_t maps #5 to #2, which is the
74     matrix for the Tm operator or its abbreviations.
75 
76   - The FontMatrix of a font maps #4 to #1.
77 
78   Note that the PDF text matrix (set by the Tm operator) maps #5 to #3.
79   However, this is not actually stored anywhere: it is computed by
80   multiplying the #5->#2 matrix by the #2->#3 scaling.
81 
82 */
83 
84 /* ---------------- Types and structures ---------------- */
85 
86 #ifndef pdf_char_glyph_pair_DEFINED
87 #  define pdf_char_glyph_pair_DEFINED
88 typedef struct pdf_char_glyph_pair_s pdf_char_glyph_pair_t;
89 #endif
90 
91 #ifndef pdf_char_glyph_pairs_DEFINED
92 #  define pdf_char_glyph_pairs_DEFINED
93 typedef struct pdf_char_glyph_pairs_s pdf_char_glyph_pairs_t;
94 #endif
95 
96 /* Define a structure for a text characters list. */
97 /* It must not contain pointers due to variable length. */
98 struct pdf_char_glyph_pairs_s {
99     int num_all_chars;
100     int num_unused_chars;
101     int unused_offset;  /* The origin of the unused character table.*/
102     pdf_char_glyph_pair_t s[1];  /* Variable length. */
103 };
104 
105 /* Define the text enumerator. */
106 typedef struct pdf_text_enum_s {
107     gs_text_enum_common;
108     gs_text_enum_t *pte_default;
109     gs_fixed_point origin;
110     bool charproc_accum;
111     bool cdevproc_callout;
112     double cdevproc_result[10];
113     pdf_char_glyph_pairs_t *cgp;
114 } pdf_text_enum_t;
115 #define private_st_pdf_text_enum()\
116   extern_st(st_gs_text_enum);\
117   gs_private_st_suffix_add2(st_pdf_text_enum, pdf_text_enum_t,\
118     "pdf_text_enum_t", pdf_text_enum_enum_ptrs, pdf_text_enum_reloc_ptrs,\
119     st_gs_text_enum, pte_default, cgp)
120 
121 /*
122  * Define quantities derived from the current font and CTM, used within
123  * the text processing loop.  NOTE: This structure has no GC descriptor
124  * and must therefore only be used locally (allocated on the C stack).
125  */
126 typedef struct pdf_text_process_state_s {
127     pdf_text_state_values_t values;
128     gs_font *font;
129 } pdf_text_process_state_t;
130 
131 /*
132  * Define the structure used to return glyph width information.  Note that
133  * there are two different sets of width information: real-number (x,y)
134  * values, which give the true advance width, and an integer value, which
135  * gives an X advance width for WMode = 0 or a Y advance width for WMode = 1.
136  * The return value from pdf_glyph_width() indicates which of these is/are
137  * valid.
138  */
139 typedef struct pdf_glyph_width_s {
140     double w;
141     gs_point xy;
142     gs_point v;				/* glyph origin shift */
143 } pdf_glyph_width_t;
144 typedef struct pdf_glyph_widths_s {
145     pdf_glyph_width_t Width;		/* unmodified, for Widths */
146     pdf_glyph_width_t real_width;	/* possibly modified, for rendering */
147     bool replaced_v;
148 } pdf_glyph_widths_t;
149 
150 /* ---------------- Procedures ---------------- */
151 
152 /*
153  * Declare the procedures for processing different species of text.
154  * These procedures may, but need not, copy pte->text into buf
155  * (a caller-supplied buffer large enough to hold the string).
156  */
157 #define PROCESS_TEXT_PROC(proc)\
158   int proc(gs_text_enum_t *pte, void *vbuf, uint bsize)
159 
160 /* ------ gdevpdtt.c ------ */
161 
162 /*
163  * Compute and return the orig_matrix of a font.
164  */
165 int pdf_font_orig_matrix(const gs_font *font, gs_matrix *pmat);
166 int font_orig_scale(const gs_font *font, double *sx);
167 
168 /*
169  * Check the Encoding compatibility
170  */
171 bool pdf_check_encoding_compatibility(const pdf_font_resource_t *pdfont,
172 		const pdf_char_glyph_pair_t *pairs, int num_chars);
173 
174 /*
175  * Create or find a font resource object for a text.
176  */
177 int
178 pdf_obtain_font_resource(pdf_text_enum_t *penum,
179 		const gs_string *pstr, pdf_font_resource_t **ppdfont);
180 
181 /*
182  * Create or find a font resource object for a glyphshow text.
183  */
184 int pdf_obtain_font_resource_unencoded(pdf_text_enum_t *penum,
185 	    const gs_string *pstr, pdf_font_resource_t **ppdfont, const gs_glyph *gdata);
186 
187 /*
188  * Create or find a CID font resource object for a glyph set.
189  */
190 int pdf_obtain_cidfont_resource(gx_device_pdf *pdev, gs_font *subfont,
191 			    pdf_font_resource_t **ppdsubf,
192 			    pdf_char_glyph_pairs_t *cgp);
193 
194 /*
195  * Create or find a parent Type 0 font resource object for a CID font resource.
196  */
197 int pdf_obtain_parent_type0_font_resource(gx_device_pdf *pdev, pdf_font_resource_t *pdsubf,
198 		const gs_const_string *CMapName, pdf_font_resource_t **pdfont);
199 
200 /*
201  * Retrive font resource attached to a font.
202  * allocating glyph_usage and real_widths on request.
203  */
204 int pdf_attached_font_resource(gx_device_pdf *pdev, gs_font *font,
205 			   pdf_font_resource_t **pdfont, byte **glyph_usage,
206 			   double **real_widths, int *num_chars, int *num_widths);
207 
208 /*
209  * Attach font resource to a font.
210  */
211 int pdf_attach_font_resource(gx_device_pdf *pdev, gs_font *font,
212 			 pdf_font_resource_t *pdfont);
213 
214 /*
215  * Create a font resource object for a gs_font of Type 3.
216  */
217 int pdf_make_font3_resource(gx_device_pdf *pdev, gs_font *font,
218 		       pdf_font_resource_t **ppdfont);
219 
220 /*
221  * Compute the cached values in the text processing state from the text
222  * parameters, pdfont, and pis->ctm.  Return either an error code (< 0) or a
223  * mask of operation attributes that the caller must emulate.  Currently the
224  * only such attributes are TEXT_ADD_TO_ALL_WIDTHS and
225  * TEXT_ADD_TO_SPACE_WIDTH.
226  */
227 int pdf_update_text_state(pdf_text_process_state_t *ppts,
228 			  const pdf_text_enum_t *penum,
229 			  pdf_font_resource_t *pdfont,
230 			  const gs_matrix *pfmat);
231 
232 /*
233  * Set up commands to make the output state match the processing state.
234  * General graphics state commands are written now; text state commands
235  * are written later.
236  */
237 int pdf_set_text_process_state(gx_device_pdf *pdev,
238 			       const gs_text_enum_t *pte, /*for pdcolor, pis*/
239 			       pdf_text_process_state_t *ppts);
240 
241 /*
242  * Get the widths (unmodified and possibly modified) of a glyph in a (base)
243  * font.  If the width is cachable (implying that the w values area valid),
244  * return 0; if only the xy values are valid, or the width is not cachable
245  * for some other reason, return 1.
246  * Return TEXT_PROCESS_CDEVPROC if a CDevProc callout is needed.
247  * cdevproc_result != NULL if we restart after a CDevProc callout.
248  */
249 int pdf_glyph_widths(pdf_font_resource_t *pdfont, int wmode, gs_glyph glyph,
250 		     gs_font *font, pdf_glyph_widths_t *pwidths,
251 		     const double cdevproc_result[10]);
252 
253 /*
254  * Fall back to the default text processing code when needed.
255  */
256 int pdf_default_text_begin(gs_text_enum_t *pte, const gs_text_params_t *text,
257 			   gs_text_enum_t **ppte);
258 
259 /*
260  * Check for simple font.
261  */
262 bool pdf_is_simple_font(gs_font *font);
263 
264 /*
265  * Check for CID font.
266  */
267 bool pdf_is_CID_font(gs_font *font);
268 
269 /* Get a synthesized Type 3 font scale. */
270 void pdf_font3_scale(gx_device_pdf *pdev, gs_font *font, double *scale);
271 
272 /* Release a text characters colloction. */
273 void pdf_text_release_cgp(pdf_text_enum_t *penum);
274 
275 
276 /* ------ gdevpdtc.c ------ */
277 
278 PROCESS_TEXT_PROC(process_composite_text);
279 PROCESS_TEXT_PROC(process_cmap_text);
280 PROCESS_TEXT_PROC(process_cid_text);
281 
282 /* ------ gdevpdte.c ------ */
283 
284 PROCESS_TEXT_PROC(process_plain_text);
285 
286 /*
287  * Encode and process a string with a simple gs_font.
288  */
289 int pdf_encode_process_string(pdf_text_enum_t *penum, gs_string *pstr,
290 			      const gs_glyph *gdata, const gs_matrix *pfmat,
291 			      pdf_text_process_state_t *ppts);
292 
293 /*
294  * Emulate TEXT_ADD_TO_ALL_WIDTHS and/or TEXT_ADD_TO_SPACE_WIDTH,
295  * and implement TEXT_REPLACE_WIDTHS if requested.
296  * Uses and updates ppts->values.matrix; uses ppts->values.pdfont.
297  */
298 int process_text_modify_width(pdf_text_enum_t *pte, gs_font *font,
299 			  pdf_text_process_state_t *ppts,
300 			  const gs_const_string *pstr,
301 			  gs_point *pdpt);
302 
303 /*
304  * Add char code pair to ToUnicode CMap,
305  * creating the CMap on neccessity.
306  */
307 int
308 pdf_add_ToUnicode(gx_device_pdf *pdev, gs_font *font, pdf_font_resource_t *pdfont,
309 		  gs_glyph glyph, gs_char ch);
310 
311 /*
312  * Get character code from a glyph code.
313  * An usage of this function is very undesirable,
314  * because a glyph may be unlisted in Encoding.
315  */
316 int pdf_encode_glyph(gs_font_base *bfont, gs_glyph glyph0,
317 	    byte *buf, int buf_size, int *char_code_length);
318 
319 int pdf_shift_text_currentpoint(pdf_text_enum_t *penum, gs_point *wpt);
320 
321 #endif /* gdevpdtt_INCLUDED */
322