1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Font object structure */
18 /* Requires gsmatrix.h, gxdevice.h */
19 
20 #ifndef gxfont_INCLUDED
21 #  define gxfont_INCLUDED
22 
23 #include "gsccode.h"
24 #include "gsfont.h"
25 #include "gsgdata.h"
26 #include "gsmatrix.h"
27 #include "gsnotify.h"
28 #include "gsuid.h"
29 #include "gsstype.h"		/* for extern_st */
30 #include "gxftype.h"
31 
32 /* A font object as seen by clients. */
33 /* See the PostScript Language Reference Manual for details. */
34 
35 #ifndef gs_text_enum_DEFINED
36 #  define gs_text_enum_DEFINED
37 typedef struct gs_text_enum_s gs_text_enum_t;
38 #endif
39 
40 #ifndef gs_show_enum_DEFINED
41 #  define gs_show_enum_DEFINED
42 typedef struct gs_show_enum_s gs_show_enum;
43 #endif
44 
45 #ifndef gx_path_DEFINED
46 #  define gx_path_DEFINED
47 typedef struct gx_path_s gx_path;
48 #endif
49 
50 /*
51  * Define flags for font properties (Flags* members in the structure below.)
52  *  Currently these must match the ones defined for PDF.
53  */
54 #define FONT_IS_FIXED_WIDTH (1<<0)
55 /*
56  * Define the structure used to return information about the font as a
57  * whole.  Currently this structure almost exactly parallels the PDF
58  * FontDescriptor.
59  *
60  * Unless otherwise specified, values are in the font's native character
61  * space, scaled as requested by the call to the font_info procedure.
62  * Note that Type 1 fonts typically have 1000 units per em, while
63  * TrueType fonts typically have 1 unit per em.
64  */
65 typedef struct gs_font_info_s {
66     int members;
67 
68     /* The following members exactly parallel the PDF FontDescriptor flags. */
69 
70 #define FONT_INFO_ASCENT 0x0001
71     int Ascent;
72 #define FONT_INFO_AVG_WIDTH 0x0002
73     int AvgWidth;
74 #define FONT_INFO_BBOX 0x0004
75     gs_int_rect BBox;		/* computed from outlines, not FontBBox */
76 #define FONT_INFO_CAP_HEIGHT 0x0008
77     int CapHeight;
78 #define FONT_INFO_DESCENT 0x0010
79     int Descent;
80 #define FONT_INFO_FLAGS 0x0020
81     uint Flags;			/* a mask of FONT_IS_ bits */
82     uint Flags_requested;	/* client must set this if FLAGS requested */
83     uint Flags_returned;
84 #define FONT_INFO_ITALIC_ANGLE 0x0100
85     float ItalicAngle;		/* degrees CCW from vertical */
86 #define FONT_INFO_LEADING 0x0200
87     int Leading;
88 #define FONT_INFO_MAX_WIDTH 0x0400
89     int MaxWidth;
90 #define FONT_INFO_MISSING_WIDTH 0x0800
91     int MissingWidth;
92 #define FONT_INFO_STEM_H 0x00010000
93     int StemH;
94 #define FONT_INFO_STEM_V 0x00020000
95     int StemV;
96 #define FONT_INFO_UNDERLINE_POSITION 0x00040000
97     int UnderlinePosition;
98 #define FONT_INFO_UNDERLINE_THICKNESS 0x00080000
99     int UnderlineThickness;
100 #define FONT_INFO_X_HEIGHT 0x00100000
101     int XHeight;
102 
103     /* The following members do NOT appear in the PDF FontDescriptor. */
104 
105 #define FONT_INFO_COPYRIGHT 0x0040
106     gs_const_string Copyright;
107 #define FONT_INFO_NOTICE 0x0080
108     gs_const_string Notice;
109 #define FONT_INFO_FAMILY_NAME 0x1000
110     gs_const_string FamilyName;
111 #define FONT_INFO_FULL_NAME 0x2000
112     gs_const_string FullName;
113 #define FONT_INFO_EMBEDDING_RIGHTS 0x4000
114     int EmbeddingRights;
115 } gs_font_info_t;
116 
117 #define public_st_gs_font_info() /* in gsfont.c */\
118   BASIC_PTRS(gs_font_info_ptrs) {\
119     GC_CONST_STRING_ELT(gs_font_info_t, Copyright),\
120     GC_CONST_STRING_ELT(gs_font_info_t, Notice),\
121     GC_CONST_STRING_ELT(gs_font_info_t, FamilyName),\
122     GC_CONST_STRING_ELT(gs_font_info_t, FullName)\
123   };\
124   gs_public_st_basic(st_gs_font_info, gs_font_info_t, "gs_font_info_t",\
125                      gs_font_info_ptrs, gs_font_info_data)
126 
127 /*
128  * Define the structure used to return information about a glyph.
129  *
130  * Note that a glyph may have two different sets of widths: those stored in
131  * the outline (which includes hmtx for TrueType fonts), and those actually
132  * used when rendering the glyph.  Currently, these differ only for Type 1
133  * fonts that use Metrics or CDevProc to change the widths.  The glyph_info
134  * procedure normally returns the rendering widths: to get the outline
135  * widths, clients use GLYPH_INFO_OUTLINE_WIDTHS.  The glyph_info procedure
136  * should set GLYPH_INFO_OUTLINE_WIDTHS in the response (the `members' in
137  * the gs_glyph_info_t structure) iff it was set in the request *and* the
138  * glyph actually has two different sets of widths.  With this arrangement,
139  * fonts that don't distinguish the two sets of widths don't need to do
140  * anything special, and don't need to test for GLYPH_INFO_OUTLINE_WIDTHS.
141  */
142 typedef struct gs_glyph_info_s {
143     int members;		/* mask of which members are valid */
144 #define GLYPH_INFO_WIDTH0 1
145 #define GLYPH_INFO_WIDTH GLYPH_INFO_WIDTH0
146 #define GLYPH_INFO_WIDTH1 2	/* must be WIDTH0 << 1 */
147 #define GLYPH_INFO_WIDTHS (GLYPH_INFO_WIDTH0 | GLYPH_INFO_WIDTH1)
148     gs_point width[2];		/* width for WMode 0/1 */
149     gs_point v;			/* glyph origin for WMode 1 */
150 #define GLYPH_INFO_BBOX 4
151     gs_rect bbox;
152 #define GLYPH_INFO_NUM_PIECES 8
153     int num_pieces;		/* # of pieces if composite, 0 if not */
154 #define GLYPH_INFO_PIECES 16
155     gs_glyph *pieces;		/* pieces are stored here: the caller must */
156                                 /* preset pieces if INFO_PIECES is set. */
157 #define GLYPH_INFO_OUTLINE_WIDTHS 32 /* return unmodified widths, see above */
158 #define GLYPH_INFO_VVECTOR0 64
159 #define GLYPH_INFO_VVECTOR1 128	/* must be VVECTOR0 << 1 */
160 #define GLYPH_INFO_CDEVPROC 256	/* Allow CDevProc callout. */
161 } gs_glyph_info_t;
162 
163 /* Define the "object" procedures of fonts. */
164 
165 typedef struct gs_font_procs_s {
166 
167     /* ------ Font-level procedures ------ */
168 
169     /*
170      * Define any special handling of gs_definefont.
171      * We break this out so it can be different for composite fonts.
172      */
173 
174 #define font_proc_define_font(proc)\
175   int proc(gs_font_dir *, gs_font *)
176     font_proc_define_font((*define_font));
177 
178     /*
179      * Define any special handling of gs_makefont.
180      * We break this out so it can be different for composite fonts.
181      */
182 
183 #define font_proc_make_font(proc)\
184   int proc(gs_font_dir *, const gs_font *, const gs_matrix *, gs_font **)
185     font_proc_make_font((*make_font));
186 
187     /*
188      * Get information about the font, as specified by the members mask.
189      * Disregard the FontMatrix: scale the font as indicated by *pscale
190      * (pscale=NULL means no scaling).  Set info->members to the members
191      * that are actually returned.  Note that some member options require
192      * the caller to preset some of the elements of info.  Note also that
193      * this procedure may return more information than was requested.
194      */
195 
196 #define font_proc_font_info(proc)\
197   int proc(gs_font *font, const gs_point *pscale, int members,\
198            gs_font_info_t *info)
199     font_proc_font_info((*font_info));
200 
201     /*
202      * Determine whether this font is the "same as" another font in the ways
203      * specified by the mask.  The returned value is a subset of the mask.
204      * This procedure is allowed to be conservative (return false in cases
205      * of uncertainty).  Note that this procedure does not test the UniqueID
206      * or FontMatrix.
207      */
208 
209 #define FONT_SAME_OUTLINES 1
210 #define FONT_SAME_METRICS 2
211 #define FONT_SAME_ENCODING 4
212 
213 #define font_proc_same_font(proc)\
214   int proc(const gs_font *font, const gs_font *ofont, int mask)
215     font_proc_same_font((*same_font));
216 
217     /* ------ Glyph-level procedures ------ */
218 
219     /* Map a character code to a glyph.  Some PostScript fonts use both
220      * names and indices to identify glyphs: for example, in PostScript Type
221      * 42 fonts, the Encoding array maps character codes to glyph names, and
222      * the CharStrings dictionary maps glyph names to glyph indices.  In
223      * such fonts, the glyph_space argument determines which type of glyph
224      * is returned.
225      */
226 
227 #define font_proc_encode_char(proc)\
228   gs_glyph proc(gs_font *, gs_char, gs_glyph_space_t)
229     font_proc_encode_char((*encode_char));
230 
231     /* Map a glyph name to Unicode UTF-16.
232      */
233 
234 #define font_proc_decode_glyph(proc)\
235   gs_char proc(gs_font *, gs_glyph, int)
236     font_proc_decode_glyph((*decode_glyph));
237 
238     /*
239      * Get the next glyph in an enumeration of all glyphs defined by the
240      * font.  index = 0 means return the first one; a returned index of 0
241      * means the enumeration is finished.  The glyphs are enumerated in
242      * an unpredictable order.
243      */
244 
245 #define font_proc_enumerate_glyph(proc)\
246   int proc(gs_font *font, int *pindex, gs_glyph_space_t glyph_space,\
247            gs_glyph *pglyph)
248     font_proc_enumerate_glyph((*enumerate_glyph));
249 
250     /*
251      * Get information about a glyph, as specified by the members mask.
252      * pmat = NULL means get the scalable values; non-NULL pmat means get
253      * the scaled values.  Set info->members to the members that are
254      * actually returned.  Return gs_error_undefined if the glyph doesn't
255      * exist in the font; calling glyph_info with members = 0 is an
256      * efficient way to find out whether a given glyph exists.  Note that
257      * some member options require the caller to preset some of the elements
258      * of info.  Note also that this procedure may return more information
259      * than was requested.
260      *
261      * Implementations of this method must not access font->WMode,
262      * because for font descendents it is inherited from an upper font.
263      * Implementatios must derive WMode from requested flags specified
264      * in 'members' argument.
265      *
266      * Currently we do not handle requests, in which GLYPH_INFO_VVECTOR0
267      * is set, but GLYPH_INFO_WIDTH0 is not. Same for GLYPH_INFO_VVECTOR1
268      * and GLYPH_INFO_WIDTH1. Also requests, in which both GLYPH_INFO_WIDTH0 and
269      * GLYPH_INFO_WIDTH1 are set, may work wrongly. Such requests look never used
270      * and debugged, and the implementation code requires improvements.
271      */
272 
273 #define font_proc_glyph_info(proc)\
274   int proc(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,\
275            int members, gs_glyph_info_t *info)
276     font_proc_glyph_info((*glyph_info));
277 
278     /*
279      * Append the outline for a glyph to a path, with the glyph origin
280      * at the current point.  pmat is as for glyph_width.  The outline
281      * does include a final moveto for the advance width.
282      *
283      * Implementations of this method must not access font->WMode,
284      * because for font descendents it is inherited from an upper font.
285      * This is especially important for Type 42 fonts with hmtx and vmtx.
286      */
287     /* Currently glyph_outline retrieves sbw only with type 1,2,9 fonts. */
288 
289 #define font_proc_glyph_outline(proc)\
290   int proc(gs_font *font, int WMode, gs_glyph glyph, const gs_matrix *pmat,\
291            gx_path *ppath, double sbw[4])
292     font_proc_glyph_outline((*glyph_outline));
293 
294     /*
295      * Return the (string) name of a glyph.
296      */
297 
298 #define font_proc_glyph_name(proc)\
299   int proc(gs_font *font, gs_glyph glyph, gs_const_string *pstr)
300     font_proc_glyph_name((*glyph_name));
301 
302     /* ------ Glyph rendering procedures ------ */
303 
304     /*
305      * Define any needed procedure for initializing the composite
306      * font stack in a show enumerator.  This is a no-op for
307      * all but composite fonts.
308      */
309 
310 #define font_proc_init_fstack(proc)\
311   int proc(gs_text_enum_t *, gs_font *)
312     font_proc_init_fstack((*init_fstack));
313 
314     /*
315      * Define the font's algorithm for getting the next character or glyph
316      * from a string being shown.  This is trivial, except for composite
317      * fonts.  Returns 0 if the current (base) font didn't change, 1 if it
318      * did change, 2 if there are no more characters, or an error code.
319      *
320      * This procedure may set either *pchar to gs_no_char or *pglyph to
321      * gs_no_glyph, but not both.
322      */
323 
324 #define font_proc_next_char_glyph(proc)\
325   int proc(gs_text_enum_t *pte, gs_char *pchar, gs_glyph *pglyph)
326     font_proc_next_char_glyph((*next_char_glyph));
327 
328     /*
329      * Define a client-supplied BuildChar/BuildGlyph procedure.
330      * The gs_char may be gs_no_char (for BuildGlyph), or the gs_glyph
331      * may be gs_no_glyph (for BuildChar), but not both.  Return 0 for
332      * success, 1 if the procedure was unable to render the character
333      * (but no error occurred), <0 for error.
334      */
335 
336 #define font_proc_build_char(proc)\
337   int proc(gs_show_enum *, gs_state *, gs_font *, gs_char, gs_glyph)
338     font_proc_build_char((*build_char));
339 
340 } gs_font_procs;
341 
342 /* Default font-level font procedures in gsfont.c */
343 font_proc_define_font(gs_no_define_font);
344 font_proc_make_font(gs_no_make_font);
345 font_proc_make_font(gs_base_make_font);
346 font_proc_font_info(gs_default_font_info);
347 font_proc_same_font(gs_default_same_font);
348 font_proc_same_font(gs_base_same_font);
349 /* Default glyph-level font procedures in gsfont.c */
350 font_proc_encode_char(gs_no_encode_char);
351 font_proc_decode_glyph(gs_no_decode_glyph);
352 font_proc_enumerate_glyph(gs_no_enumerate_glyph);
353 font_proc_glyph_info(gs_default_glyph_info);
354 font_proc_glyph_outline(gs_no_glyph_outline);
355 font_proc_glyph_name(gs_no_glyph_name);
356 /* Default glyph rendering procedures in gstext.c */
357 font_proc_init_fstack(gs_default_init_fstack);
358 font_proc_next_char_glyph(gs_default_next_char_glyph);
359 font_proc_build_char(gs_no_build_char);
360 /* Default procedure vector in gsfont.c */
361 extern const gs_font_procs gs_font_procs_default;
362 
363 /* The font names are only needed for xfont lookup and high-level output. */
364 typedef struct gs_font_name_s {
365 #define gs_font_name_max 47	/* must be >= 40 */
366     /* The +1 is so we can null-terminate for debugging printout. */
367     byte chars[gs_font_name_max + 1];
368     uint size;
369 } gs_font_name;
370 
371 /*
372  * Define a generic font.  We include PaintType and StrokeWidth here because
373  * they affect rendering algorithms outside the Type 1 font machinery.
374  *
375  * ****** NOTE: If you define any subclasses of gs_font, you *must* define
376  * ****** the finalization procedure as gs_font_finalize.  Finalization
377  * ****** procedures are not automatically inherited.
378  */
379 #define gs_font_common\
380         gs_font *next, *prev;		/* chain for original font list or */\
381                                         /* scaled font cache */\
382         gs_memory_t *memory;		/* allocator for this font */\
383         gs_font_dir *dir;		/* directory where registered */\
384         bool is_resource;\
385         gs_notify_list_t notify_list;	/* clients to notify when freeing */\
386         gs_id id;			/* internal ID (no relation to UID) */\
387         gs_font *base;			/* original (unscaled) base font */\
388         void *client_data;		/* additional client data */\
389         gs_matrix FontMatrix;\
390         gs_matrix orig_FontMatrix;      /* The original font matrix or zeros. */\
391         font_type FontType;\
392         bool BitmapWidths;\
393         fbit_type ExactSize, InBetweenSize, TransformedChar;\
394         int WMode;			/* 0 or 1 */\
395         int PaintType;			/* PaintType for Type 1/4/42 fonts, */\
396                                         /* 0 for others */\
397         float StrokeWidth;		/* StrokeWidth for Type 1/4/42 */\
398                                         /* fonts (if present), 0 for others */\
399         bool is_cached;			/* Prevents redundant executions of */\
400                                         /* gs_purge_font_from_char_caches, */\
401                                         /* when it is called from 'font_restore' */\
402                                         /* and from gx_font_finalize. */\
403         gs_font_procs procs;\
404         /* We store both the FontDirectory key (key_name) and, */\
405         /* if present, the FontName (font_name). */\
406         gs_font_name key_name, font_name
407 /*typedef struct gs_font_s gs_font; *//* in gsfont.h and other places */
408 struct gs_font_s {
409     gs_font_common;
410 };
411 
412 extern_st(st_gs_font);		/* (abstract) */
413 struct_proc_finalize(gs_font_finalize);  /* public for concrete subclasses */
414 #define public_st_gs_font()	/* in gsfont.c */\
415   gs_public_st_complex_only(st_gs_font, gs_font, "gs_font",\
416     0, font_enum_ptrs, font_reloc_ptrs, gs_font_finalize)
417 #define st_gs_font_max_ptrs (st_gs_notify_list_max_ptrs + 5)
418 
419 #define private_st_gs_font_ptr()	/* in gsfont.c */\
420   gs_private_st_ptr(st_gs_font_ptr, gs_font *, "gs_font *",\
421     font_ptr_enum_ptrs, font_ptr_reloc_ptrs)
422 #define st_gs_font_ptr_max_ptrs 1
423 
424 extern_st(st_gs_font_ptr_element);
425 #define public_st_gs_font_ptr_element()	/* in gsfont.c */\
426   gs_public_st_element(st_gs_font_ptr_element, gs_font *, "gs_font *[]",\
427     font_ptr_element_enum_ptrs, font_ptr_element_reloc_ptrs, st_gs_font_ptr)
428 
429 /* Allocate and minimally initialize a font. */
430 /* Does not set: FontMatrix, FontType, key_name, font_name. */
431 gs_font *
432   gs_font_alloc(gs_memory_t *mem, gs_memory_type_ptr_t pstype,
433                 const gs_font_procs *procs, gs_font_dir *dir,
434                 client_name_t cname);
435 
436 /* Initialize the notification list for a font. */
437 void gs_font_notify_init(gs_font *font);
438 
439 /*
440  * Register/unregister a client for notification by a font.  Currently
441  * the clients are only notified when a font is freed.  Note that any
442  * such client must unregister itself when *it* is freed.
443  */
444 int gs_font_notify_register(gs_font *font, gs_notify_proc_t proc,
445                             void *proc_data);
446 int gs_font_notify_unregister(gs_font *font, gs_notify_proc_t proc,
447                               void *proc_data);
448 
449 #ifndef FAPI_server_DEFINED
450 #define FAPI_server_DEFINED
451 typedef struct FAPI_server_s FAPI_server;
452 #endif
453 
454 /* Define a base (not composite) font. */
455 #define gs_font_base_common\
456         gs_font_common;\
457         gs_rect FontBBox;\
458         gs_uid UID;\
459         FAPI_server *FAPI; \
460         void *FAPI_font_data; \
461         gs_encoding_index_t encoding_index;\
462         gs_encoding_index_t nearest_encoding_index  /* (may be >= 0 even if */\
463                                                 /* encoding_index = -1) */
464 #ifndef gs_font_base_DEFINED
465 #  define gs_font_base_DEFINED
466 typedef struct gs_font_base_s gs_font_base;
467 #endif
468 struct gs_font_base_s {
469     gs_font_base_common;
470 };
471 
472 extern_st(st_gs_font_base);
473 #define public_st_gs_font_base()	/* in gsfont.c */\
474   gs_public_st_suffix_add1_final(st_gs_font_base, gs_font_base,\
475     "gs_font_base", font_base_enum_ptrs, font_base_reloc_ptrs,\
476     gs_font_finalize, st_gs_font, UID.xvalues)
477 #define st_gs_font_base_max_ptrs (st_gs_font_max_ptrs + 1)
478 
479 /* Allocate and minimally initialize a base font. */
480 /* Does not set: same elements as gs_alloc_font. */
481 gs_font_base *
482   gs_font_base_alloc(gs_memory_t *mem, gs_memory_type_ptr_t pstype,
483                      const gs_font_procs *procs, gs_font_dir *dir,
484                      client_name_t cname);
485 
486 /* Define a string to interact with unique_name in lib/pdf_font.ps .
487    The string is used to resolve glyph name conflict while
488    converting PDF Widths into Metrics.
489  */
490 extern const char gx_extendeg_glyph_name_separator[];
491 
492 /*
493  * Test whether a glyph is the notdef glyph for a base font.
494  * The test is somewhat adhoc: perhaps this should be a virtual procedure.
495  */
496 bool gs_font_glyph_is_notdef(gs_font_base *bfont, gs_glyph glyph);
497 
498 /* Get font parent (a Type 9 font). */
499 const gs_font_base *gs_font_parent(const gs_font_base *pbfont);
500 
501 #ifdef DEBUG
502 /* Reserve a text enumerator instance id. */
503 ulong gs_next_text_enum_id(const gs_font *font);
504 #endif
505 
506 #endif /* gxfont_INCLUDED */
507