1 /* modified from sft.h in libgnomeprint */
4 * Copyright © 2002, 2003 Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * This software is provided "AS IS," without a warranty of any kind.
24 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
25 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
26 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
27 * SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR
28 * LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE,
29 * MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES.
30 * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
31 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
32 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
33 * THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
34 * SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
40 /* @(#)sft.h 1.17 03/01/08 SMI */
44 * @brief Sun Font Tools
45 * @author Alexander Gelfenbain <adg@sun.com>
50 * Generated fonts contain an XUID entry in the form of:
54 * 103 - Sun's Adobe assigned XUID number. Contact person: Alexander Gelfenbain <gelf@eng.sun.com>
56 * T - font type. 0: Type 3, 1: Type 42
57 * C1 - CRC-32 of the entire source TrueType font
58 * N - number of glyphs in the subset
59 * C2 - CRC-32 of the array of glyph IDs used to generate the subset
60 * C3 - CRC-32 of the array of encoding numbers used to generate the subset
68 #include <sys/types.h>
72 #define false 0 /**< standard false value */
73 #define true 1 /**< standard true value */
75 /* glib already deals with different compilers */
76 #include <glibconfig.h>
79 typedef gint16 F2Dot14; /**< fixed: 2.14 */
80 typedef gint32 F16Dot16; /**< fixed: 16.16 */
88 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
90 SF_OK, /**< no error */
91 SF_BADFILE, /**< file not found */
92 SF_FILEIO, /**< file I/O error */
93 SF_MEMORY, /**< memory allocation error */
94 SF_GLYPHNUM, /**< incorrect number of glyphs */
95 SF_BADARG, /**< incorrect arguments */
96 SF_TTFORMAT, /**< incorrect TrueType font format */
97 SF_TABLEFORMAT, /**< incorrect format of a TrueType table */
98 SF_FONTNO /**< incorrect logical font number of a TTC font */
101 #ifndef FW_THIN /* WIN32 compilation would conflict */
102 /** Value of the weight member of the TTGlobalFontInfo struct */
104 FW_THIN = 100, /**< Thin */
105 FW_EXTRALIGHT = 200, /**< Extra-light (Ultra-light) */
106 FW_LIGHT = 300, /**< Light */
107 FW_NORMAL = 400, /**< Normal (Regular) */
108 FW_MEDIUM = 500, /**< Medium */
109 FW_SEMIBOLD = 600, /**< Semi-bold (Demi-bold) */
110 FW_BOLD = 700, /**< Bold */
111 FW_EXTRABOLD = 800, /**< Extra-bold (Ultra-bold) */
112 FW_BLACK = 900 /**< Black (Heavy) */
115 /** Value of the width member of the TTGlobalFontInfo struct */
117 FWIDTH_ULTRA_CONDENSED = 1, /**< 50% of normal */
118 FWIDTH_EXTRA_CONDENSED = 2, /**< 62.5% of normal */
119 FWIDTH_CONDENSED = 3, /**< 75% of normal */
120 FWIDTH_SEMI_CONDENSED = 4, /**< 87.5% of normal */
121 FWIDTH_NORMAL = 5, /**< Medium, 100% */
122 FWIDTH_SEMI_EXPANDED = 6, /**< 112.5% of normal */
123 FWIDTH_EXPANDED = 7, /**< 125% of normal */
124 FWIDTH_EXTRA_EXPANDED = 8, /**< 150% of normal */
125 FWIDTH_ULTRA_EXPANDED = 9 /**< 200% of normal */
129 /** Type of the 'kern' table, stored in _TrueTypeFont::kerntype */
132 KT_NONE = 0, /**< no kern table */
133 KT_APPLE_NEW = 1, /**< new Apple kern table */
134 KT_MICROSOFT = 2 /**< Microsoft table */
137 /* Composite glyph flags definition */
138 enum CompositeFlags {
139 ARG_1_AND_2_ARE_WORDS = 1,
140 ARGS_ARE_XY_VALUES = 1<<1,
141 ROUND_XY_TO_GRID = 1<<2,
142 WE_HAVE_A_SCALE = 1<<3,
143 MORE_COMPONENTS = 1<<5,
144 WE_HAVE_AN_X_AND_Y_SCALE = 1<<6,
145 WE_HAVE_A_TWO_BY_TWO = 1<<7,
146 WE_HAVE_INSTRUCTIONS = 1<<8,
147 USE_MY_METRICS = 1<<9,
148 OVERLAP_COMPOUND = 1<<10
151 /** Flags for TrueType generation */
152 enum TTCreationFlags {
153 TTCF_AutoName = 1, /**< Automatically generate a compact 'name' table.
154 If this flag is not set, name table is generated
155 either from an array of NameRecord structs passed as
156 arguments or if the array is NULL, 'name' table
157 of the generated TrueType file will be a copy
158 of the name table of the original file.
159 If this flag is set the array of NameRecord structs
160 is ignored and a very compact 'name' table is automatically
163 TTCF_IncludeOS2 = 2 /** If this flag is set OS/2 table from the original font will be
164 copied to the subset */
167 /** Structure used by GetTTGlyphMetrics() */
168 /*- In horisontal writing mode right sidebearing is calculated using this formula
169 *- rsb = aw - (lsb + xMax - xMin) -*/
175 guint16 aw; /*- Advance Width (horisontal writing mode) */
176 gint16 lsb; /*- Left sidebearing (horisontal writing mode) */
177 guint16 ah; /*- advance height (vertical writing mode) */
178 gint16 tsb; /*- top sidebearing (vertical writing mode) */
182 /** Structure used by GetTTSimpleGlyphMetrics() and GetTTSimpleCharMetrics() functions */
184 guint16 adv; /**< advance width or height */
185 gint16 sb; /**< left or top sidebearing */
186 } TTSimpleGlyphMetrics;
188 /** Structure returned by ReadGlyphMetrics() */
192 } TTFullSimpleGlyphMetrics;
195 /** Structure used by the TrueType Creator and GetRawGlyphData() */
198 guint32 glyphID; /**< glyph ID */
199 guint16 nbytes; /**< number of bytes in glyph data */
200 guint8 *ptr; /**< pointer to glyph data */
201 guint16 aw; /**< advance width */
202 gint16 lsb; /**< left sidebearing */
203 guint16 compflag; /**< 0- if non-composite, 1- otherwise */
204 guint16 npoints; /**< number of points */
205 guint16 ncontours; /**< number of contours */
207 guint32 newID; /**< used internally by the TTCR */
211 /* STSF defines NameRecord and FUnitBBox structures in its own include file sttypes.h */
220 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
222 guint16 platformID; /**< Platform ID */
223 guint16 encodingID; /**< Platform-specific encoding ID */
224 guint16 languageID; /**< Language ID */
225 guint16 nameID; /**< Name ID */
226 guint16 slen; /**< String length in bytes */
227 guint8 *sptr; /**< Pointer to string data (not zero-terminated!) */
231 /** Return value of GetTTGlobalFontInfo() */
234 char *family; /**< family name */
235 guint16 *ufamily; /**< family name UCS2 */
236 char *subfamily; /**< subfamily name */
237 char *psname; /**< PostScript name */
238 int weight; /**< value of WeightClass or 0 if can't be determined */
239 int width; /**< value of WidthClass or 0 if can't be determined */
240 int pitch; /**< 0: proportianal font, otherwise: monospaced */
241 int italicAngle; /**< in counter-clockwise degrees * 65536 */
242 guint16 fsSelection; /**< fsSelection field of OS/2 table */
243 int xMin; /**< global bounding box: xMin */
244 int yMin; /**< global bounding box: yMin */
245 int xMax; /**< global bounding box: xMax */
246 int yMax; /**< global bounding box: yMax */
247 int ascender; /**< typographic ascent. */
248 int descender; /**< typographic descent. */
249 int linegap; /**< typographic line gap.\ Negative values are treated as
250 zero in Win 3.1, System 6 and System 7. */
251 int vascent; /**< typographic ascent for vertical writing mode */
252 int vdescent; /**< typographic descent for vertical writing mode */
253 int typoAscender; /**< OS/2 portable typographic ascender */
254 int typoDescender; /**< OS/2 portable typographic descender */
255 int typoLineGap; /**< OS/2 portable typographc line gap */
256 int winAscent; /**< ascender metric for Windows */
257 int winDescent; /**< descender metric for Windows */
258 int symbolEncoded; /**< 1: MS symbol encoded 0: not symbol encoded */
259 int rangeFlag; /**< if set to 1 Unicode Range flags are applicable */
260 guint32 ur1; /**< bits 0 - 31 of Unicode Range flags */
261 guint32 ur2; /**< bits 32 - 63 of Unicode Range flags */
262 guint32 ur3; /**< bits 64 - 95 of Unicode Range flags */
263 guint32 ur4; /**< bits 96 - 127 of Unicode Range flags */
264 guint8 panose[10]; /**< PANOSE classification number */
265 guint16 typeFlags; /**< type flags (copyright information) */
268 /** Structure used by KernGlyphs() */
270 int x; /**< positive: right, negative: left */
271 int y; /**< positive: up, negative: down */
275 /** ControlPoint structure used by GetTTGlyphPoints() */
277 guint32 flags; /**< 00000000 00000000 e0000000 bbbbbbbb */
278 /**< b - guint8 flags from the glyf array */
279 /**< e == 0 - regular point */
280 /**< e == 1 - end contour */
281 gint16 x; /**< X coordinate in EmSquare units */
282 gint16 y; /**< Y coordinate in EmSquare units */
285 typedef struct _TrueTypeFont TrueTypeFont;
288 * @defgroup sft Sun Font Tools Exported Functions
293 * Get the number of fonts contained in a TrueType collection
294 * @param fname - file name
295 * @return number of fonts or zero, if file is not a TTC file.
298 int CountTTCFonts(const char* fname);
302 * TrueTypeFont constructor.
303 * Reads the font file and allocates the memory for the structure.
304 * @param facenum - logical font number within a TTC file. This value is ignored
306 * @return value of SFErrCodes enum
309 int OpenTTFont(const char *fname, guint32 facenum, TrueTypeFont**);
312 * TrueTypeFont destructor. Deallocates the memory.
315 void CloseTTFont(TrueTypeFont *);
318 * Extracts TrueType control points, and stores them in an allocated array pointed to
319 * by *pointArray. This function returns the number of extracted points.
321 * @param ttf pointer to the TrueTypeFont structure
322 * @param glyphID Glyph ID
323 * @param pointArray Return value - address of the pointer to the first element of the array
324 * of points allocated by the function
325 * @return Returns the number of points in *pointArray or -1 if glyphID is
330 int GetTTGlyphPoints(TrueTypeFont *ttf, guint32 glyphID, ControlPoint **pointArray);
333 * Extracts bounding boxes in normalized FUnits (1000/em) for all glyphs in the
334 * font and allocates an array of FUnitBBox structures. There are ttf->nglyphs
335 * elements in the array.
337 * @param ttf pointer to the TrueTypeFont structure
339 * @return an array of FUnitBBox structures with values normalized to 1000 UPEm
343 FUnitBBox *GetTTGlyphBoundingBoxes(TrueTypeFont *ttf);
346 * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
347 * GlyphData structure.
349 * @param ttf pointer to the TrueTypeFont structure
350 * @param glyphID Glyph ID
352 * @return pointer to an allocated GlyphData structure or NULL if
353 * glyphID is not present in the font
357 GlyphData *GetTTRawGlyphData(TrueTypeFont *ttf, guint32 glyphID);
360 * Extracts all Name Records from the font and stores them in an allocated
361 * array of NameRecord structs
363 * @param ttf pointer to the TrueTypeFont struct
364 * @param nr pointer to the array of NameRecord structs
366 * @return number of NameRecord structs
370 int GetTTNameRecords(TrueTypeFont *ttf, NameRecord **nr);
373 * Deallocates previously allocated array of NameRecords.
375 * @param nr array of NameRecord structs
376 * @param n number of elements in the array
380 void DisposeNameRecords(NameRecord* nr, int n);
385 * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
386 * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
387 * @param ttf pointer to the TrueTypeFont structure
388 * @param outf the resulting font is written to this stream
389 * @param fname font name for the new font. If it is NULL the PostScript name of the
390 * original font will be used
391 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
392 * @param encoding array of encoding values. encoding[i] specifies the position of the glyph
393 * glyphArray[i] in the encoding vector of the resulting Type3 font
394 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
395 * @param wmode writing mode for the output file: 0 - horizontal, 1 - vertical
396 * @return return the value of SFErrCodes enum
401 int CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, guint16 *glyphArray, guint8 *encoding, int nGlyphs, int wmode);
406 * Generates a new TrueType font and dumps it to <b>outf</b> file.
407 * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
408 * @param ttf pointer to the TrueTypeFont structure
409 * @param fname file name for the output TrueType font file
410 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
411 * element of this array has to be glyph 0 (default glyph)
412 * @param encoding array of encoding values. encoding[i] specifies character code for
413 * the glyphID glyphArray[i]. Character code 0 usually points to a default
415 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
416 * @param nNameRecs number of NameRecords for the font, if 0 the name table from the
417 * original font will be used
418 * @param nr array of NameRecords
419 * @param flags or'ed TTCreationFlags
420 * @return return the value of SFErrCodes enum
425 int CreateTTFromTTGlyphs(TrueTypeFont *ttf,
437 * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
438 * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
439 * @param ttf pointer to the TrueTypeFont structure
440 * @param outf output stream for a resulting font
441 * @param psname PostScript name of the resulting font
442 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
443 * element of this array has to be glyph 0 (default glyph)
444 * @param encoding array of encoding values. encoding[i] specifies character code for
445 * the glyphID glyphArray[i]. Character code 0 usually points to a default
447 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
448 * @return SF_OK - no errors
449 * SF_GLYPHNUM - too many glyphs (> 255)
450 * SF_TTFORMAT - corrupted TrueType fonts
456 int CreateT42FromTTGlyphs(TrueTypeFont *ttf,
465 * Queries full glyph metrics for one glyph
467 void GetTTGlyphMetrics(TrueTypeFont *ttf, guint32 glyphID, TTGlyphMetrics *metrics);
471 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
473 * @param ttf pointer to the TrueTypeFont structure
474 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
475 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
476 * @param mode writing mode: 0 - horizontal, 1 - vertical
480 TTSimpleGlyphMetrics *GetTTSimpleGlyphMetrics(TrueTypeFont *ttf, guint16 *glyphArray, int nGlyphs, int mode);
484 * Queries character metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
485 * This function behaves just like GetTTSimpleGlyphMetrics() but it takes a range of Unicode
486 * characters instead of an array of glyphs.
488 * @param ttf pointer to the TrueTypeFont structure
489 * @param firstChar Unicode value of the first character in the range
490 * @param nChars number of Unicode characters in the range
491 * @param mode writing mode: 0 - horizontal, 1 - vertical
493 * @see GetTTSimpleGlyphMetrics
497 TTSimpleGlyphMetrics *GetTTSimpleCharMetrics(TrueTypeFont *ttf, guint16 firstChar, int nChars, int mode);
500 * Maps a Unicode (UCS-2) string to a glyph array. Returns the number of glyphs in the array,
501 * which for TrueType fonts is always the same as the number of input characters.
503 * @param ttf pointer to the TrueTypeFont structure
504 * @param str pointer to a UCS-2 string
505 * @param nchars number of characters in <b>str</b>
506 * @param glyphArray pointer to the glyph array where glyph IDs are to be recorded.
508 * @return MapString() returns -1 if the TrueType font has no usable 'cmap' tables.
509 * Otherwise it returns the number of characters processed: <b>nChars</b>
511 * glyphIDs of TrueType fonts are 2 guint8 positive numbers. glyphID of 0 denotes a missing
512 * glyph and traditionally defaults to an empty square.
513 * glyphArray should be at least sizeof(guint16) * nchars bytes long. If glyphArray is NULL
514 * MapString() replaces the UCS-2 characters in str with glyphIDs.
518 int MapString(TrueTypeFont *ttf, guint16 *str, int nchars, guint16 *glyphArray, int bvertical);
520 int MapString(TrueTypeFont *ttf, guint16 *str, int nchars, guint16 *glyphArray);
524 * Maps a Unicode (UCS-2) character to a glyph ID and returns it. Missing glyph has
525 * a glyphID of 0 so this function can be used to test if a character is encoded in the font.
527 * @param ttf pointer to the TrueTypeFont structure
528 * @param ch Unicode (UCS-2) character
529 * @return glyph ID, if the character is missing in the font, the return value is 0.
533 guint16 MapChar(TrueTypeFont *ttf, guint16 ch, int bvertical);
535 guint16 MapChar(TrueTypeFont *ttf, guint16 ch);
540 * Returns global font information about the TrueType font.
541 * @see TTGlobalFontInfo
543 * @param ttf pointer to a TrueTypeFont structure
544 * @param info pointer to a TTGlobalFontInfo structure
548 void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info);
551 * Returns kerning information for an array of glyphs.
552 * Kerning is not cumulative.
553 * kern[i] contains kerning information for a pair of glyphs at positions i and i+1
555 * @param ttf pointer to a TrueTypeFont structure
556 * @param glyphs array of source glyphs
557 * @param nglyphs number of glyphs in the array
558 * @param wmode writing mode: 0 - horizontal, 1 - vertical
559 * @param kern array of KernData structures. It should contain nglyphs-1 elements
564 void KernGlyphs(TrueTypeFont *ttf, guint16 *glyphs, int nglyphs, int wmode, KernData *kern);
567 * Returns nonzero if font is a symbol encoded font
569 int CheckSymbolEncoding(TrueTypeFont* ttf);
572 * Extracts a 'cmap' table from a font, allocates memory for it and returns a pointer to it.
573 * DEPRECATED - use ExtractTable instead
576 guint8 *ExtractCmap(TrueTypeFont *ttf);
580 * Extracts a table from a font, allocates memort for it and returns a pointer to it.
582 guint8 *ExtractTable(TrueTypeFont *ttf, guint32 tag);
585 * Returns a pointer to the table but does not allocate memory for it.
587 const guint8 *GetTable(TrueTypeFont *ttf, guint32 tag);
590 * Functions that do not use TrueTypeFont structure
591 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
595 * Reads full (vertical and horisontal) glyph metrics for an array of glyphs from hmtx and vmtx tables
597 * @param hmtx TrueType hmtx table
598 * @param vmtx TrueType vmtx table
599 * @param hcount numberOfHMetrics value
600 * @param vcount numOfLongVerMetrics value
601 * @param gcount total number of glyphs in the font
602 * @param UPEm units per Em value
603 * @param glyphArray array of source glyph IDs
604 * @param nGlyphs number of glyphs in the glyphArray array
606 * @return array of TTFullSimpleGlyphMetrics data structures
610 TTFullSimpleGlyphMetrics *ReadGlyphMetrics(guint8 *hmtx, guint8 *vmtx, int hcount, int vcount, int gcount, int UPEm, guint16 *glyphArray, int nGlyphs);
612 void ReadSingleGlyphMetrics(guint8 *hmtx, guint8 *vmtx, int hcount, int vcount, int gcount, int UPEm, guint16 glyphID, TTFullSimpleGlyphMetrics *metrics);
616 * Returns the length of the 'kern' subtable
618 * @param kern pointer to the 'kern' subtable
620 * @return number of bytes in it
622 guint32 GetKernSubtableLength(guint8 *kern);
625 * Kerns a pair of glyphs.
627 * @param kerntype type of the kern table
628 * @param nkern number of kern subtables
629 * @param kern array of pointers to kern subtables
630 * @pram unitsPerEm units per Em value
631 * @param wmode writing mode: 0 - horizontal, 1 - vertical
632 * @param a ID of the first glyoh
633 * @param b ID of the second glyoh
634 * @param x X-axis kerning value is returned here
635 * @param y Y-axis kerning value is returned here
637 void KernGlyphPair(int kerntype, guint32 nkern, guint8 **kern, int unitsPerEm, int wmode, guint32 a, guint32 b, int *x, int *y);
641 /*- private definitions */ /*FOLD00*/
643 struct _TrueTypeFont {
656 guint32 tdoffset; /* offset to the table directory (!= 0 for TrueType collections) */
660 int numberOfHMetrics;
661 int numOfLongVerMetrics; /* if this number is not 0, font has vertical metrics information */
664 guint16 (*mapper)(const guint8 *, guint16); /* character to glyphID translation function */
665 void **tables; /* array of pointers to tables */
666 guint32 *tlens; /* array of table lengths */
667 int kerntype; /* Defined in the KernType enum */
668 guint32 nkern; /* number of kern subtables */
669 guint8 **kerntables; /* array of pointers to kern subtables */
671 void *pGSubstitution; /* info provided by GSUB for UseGSUB() */
674 GnomePrintBuffer gp_buf;
678 #endif /* __SUBFONT_H */