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