]> git.donarmstrong.com Git - xournal.git/blob - src/ttsubset/sft.h
Print via gtk-print instead of libgnomeprint
[xournal.git] / src / ttsubset / sft.h
1 /*
2  * Copyright © 2002, 2003 Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of Sun Microsystems, Inc. nor the names of 
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind.
21  *
22  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
25  * SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR
26  * LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE,
27  * MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES.
28  * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
29  * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
30  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
31  * THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
32  * SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  */
35
36 /* $Id$ */
37 /* @(#)sft.h 1.17 03/01/08 SMI */
38
39 /*
40  * @file sft.h
41  * @brief Sun Font Tools
42  * @author Alexander Gelfenbain <adg@sun.com>
43  * @version 1.0
44  */
45
46 /*
47  *        If NO_MAPPERS is defined, MapChar() and MapString() and consequently GetTTSimpleCharMetrics()
48  *        don't get compiled in. This is done to avoid including a large chunk of code (TranslateXY() from
49  *        xlat.c in the projects that don't require it.
50  *
51  *        If NO_TYPE3 is defined CreateT3FromTTGlyphs() does not get compiled in.
52  *        If NO_TYPE42 is defined Type42-related code is excluded
53  *        If NO_TTCR is defined TrueType creation related code is excluded\
54  *        If NO_LIST is defined list.h and piblic functions that use it don't get compiled
55  *        If USE_GSUB is *not* defined Philipp's GSUB code does not get included
56  *
57  *        When STSF is defined several data types are defined elsewhere
58  */
59
60 /*
61  *        Generated fonts contain an XUID entry in the form of:
62  *
63  *                  103 0 T C1 N C2 C3
64  *
65  *        103 - Sun's Adobe assigned XUID number. Contact person: Alexander Gelfenbain <gelf@eng.sun.com>
66  *
67  *        T  - font type. 0: Type 3, 1: Type 42
68  *        C1 - CRC-32 of the entire source TrueType font
69  *        N  - number of glyphs in the subset
70  *        C2 - CRC-32 of the array of glyph IDs used to generate the subset
71  *        C3 - CRC-32 of the array of encoding numbers used to generate the subset
72  *
73  */
74
75 #ifndef __SUBFONT_H
76 #define __SUBFONT_H
77
78 #include <sys/types.h>
79 #ifdef HAVE_UNISTD_H
80 #include <unistd.h>
81 #endif
82 #include <stdio.h>
83 #ifndef NO_LIST
84 #include "list.h"
85 #endif
86
87 #ifdef STSF
88 #include <sttypes.h>
89 #endif
90
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94
95 #if 0   /* Use glib's G_BYTE_ORDER == G_BIG_ENDIAN instead */
96 #ifdef __sparc
97 #ifndef G_BIG_ENDIAN
98 #define G_BIG_ENDIAN
99 #endif
100 #endif
101
102 #if defined(__powerpc__) || defined(POWERPC)
103 #ifndef G_BIG_ENDIAN
104 #define G_BIG_ENDIAN
105 #endif
106 #endif
107
108 #ifdef __i386
109 #ifndef G_LITTLE_ENDIAN
110 #define G_LITTLE_ENDIAN
111 #endif
112 #endif
113
114 #ifdef __mips
115 #ifndef G_BIG_ENDIAN
116 #define G_BIG_ENDIAN
117 #endif
118 #endif
119
120 #ifdef __BIG_ENDIAN__
121 #define G_BIG_ENDIAN
122 #endif
123
124 #ifdef __LITTLE_ENDIAN__
125 #define G_LITTLE_ENDIAN
126 #endif
127
128 #if !defined(G_BIG_ENDIAN) && !defined(G_LITTLE_ENDIAN)
129 #error "Either G_BIG_ENDIAN or G_LITTLE_ENDIAN should be defined."
130 #endif
131
132 #if defined(G_BIG_ENDIAN) && defined(G_LITTLE_ENDIAN)
133 #error "This is bizarre"
134 #endif
135 #endif
136
137 #if 0  /* These should be defined in the makefile */
138 #define DEBUG      /* Generate debugging output */
139 #define DEBUG2     /* More detailed debugging output */
140 #define DEBUG3     /* Dump of TrueType outlines */
141 #endif
142
143     
144
145
146 /*@{*/
147 #define false 0               /**< standard false value */
148 #define true  1               /**< standard true value */
149 /*@}*/
150
151 #ifndef STSF
152 /* glib already deals with different compilers */
153 #include <glibconfig.h>
154
155 /*@{*/
156     typedef gint16       F2Dot14;            /**< fixed: 2.14 */
157     typedef gint32       F16Dot16;           /**< fixed: 16.16 */
158 /*@}*/
159
160     typedef struct {
161         guint16 s;
162         guint16 d;
163     } uint16pair;
164 #endif
165
166 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
167     enum SFErrCodes {
168         SF_OK,                              /**< no error                                     */
169         SF_BADFILE,                         /**< file not found                               */
170         SF_FILEIO,                          /**< file I/O error                               */
171         SF_MEMORY,                          /**< memory allocation error                      */
172         SF_GLYPHNUM,                        /**< incorrect number of glyphs                   */
173         SF_BADARG,                          /**< incorrect arguments                          */
174         SF_TTFORMAT,                        /**< incorrect TrueType font format               */
175         SF_TABLEFORMAT,                     /**< incorrect format of a TrueType table         */
176         SF_FONTNO                           /**< incorrect logical font number of a TTC font  */
177     };
178
179 #ifndef FW_THIN /* WIN32 compilation would conflict */
180 /** Value of the weight member of the TTGlobalFontInfo struct */
181     enum WeightClass {
182         FW_THIN = 100,                      /**< Thin                               */
183         FW_EXTRALIGHT = 200,                /**< Extra-light (Ultra-light)          */
184         FW_LIGHT = 300,                     /**< Light                              */
185         FW_NORMAL = 400,                    /**< Normal (Regular)                   */
186         FW_MEDIUM = 500,                    /**< Medium                             */
187         FW_SEMIBOLD = 600,                  /**< Semi-bold (Demi-bold)              */
188         FW_BOLD = 700,                      /**< Bold                               */
189         FW_EXTRABOLD = 800,                 /**< Extra-bold (Ultra-bold)            */
190         FW_BLACK = 900                      /**< Black (Heavy)                      */
191     };
192
193 /** Value of the width member of the TTGlobalFontInfo struct */
194     enum WidthClass {
195         FWIDTH_ULTRA_CONDENSED = 1,         /**< 50% of normal                      */
196         FWIDTH_EXTRA_CONDENSED = 2,         /**< 62.5% of normal                    */
197         FWIDTH_CONDENSED = 3,               /**< 75% of normal                      */
198         FWIDTH_SEMI_CONDENSED = 4,          /**< 87.5% of normal                    */
199         FWIDTH_NORMAL = 5,                  /**< Medium, 100%                       */
200         FWIDTH_SEMI_EXPANDED = 6,           /**< 112.5% of normal                   */
201         FWIDTH_EXPANDED = 7,                /**< 125% of normal                     */
202         FWIDTH_EXTRA_EXPANDED = 8,          /**< 150% of normal                     */
203         FWIDTH_ULTRA_EXPANDED = 9           /**< 200% of normal                     */
204     };
205 #endif /* FW_THIN */
206
207 /** Type of the 'kern' table, stored in _TrueTypeFont::kerntype */
208     enum KernType {
209
210         KT_NONE         = 0,                /**< no kern table                      */
211         KT_APPLE_NEW    = 1,                /**< new Apple kern table               */
212         KT_MICROSOFT    = 2                 /**< Microsoft table                    */
213     };
214
215 /* Composite glyph flags definition */
216     enum CompositeFlags {
217         ARG_1_AND_2_ARE_WORDS     = 1,
218         ARGS_ARE_XY_VALUES        = 1<<1,
219         ROUND_XY_TO_GRID          = 1<<2,
220         WE_HAVE_A_SCALE           = 1<<3,
221         MORE_COMPONENTS           = 1<<5,
222         WE_HAVE_AN_X_AND_Y_SCALE  = 1<<6,
223         WE_HAVE_A_TWO_BY_TWO      = 1<<7,
224         WE_HAVE_INSTRUCTIONS      = 1<<8,
225         USE_MY_METRICS            = 1<<9,
226         OVERLAP_COMPOUND          = 1<<10
227     };
228
229 #ifndef NO_TTCR
230 /** Flags for TrueType generation */
231     enum TTCreationFlags {
232         TTCF_AutoName = 1,                  /**< Automatically generate a compact 'name' table.
233                                                If this flag is not set, name table is generated
234                                                either from an array of NameRecord structs passed as
235                                                arguments or if the array is NULL, 'name' table
236                                                of the generated TrueType file will be a copy
237                                                of the name table of the original file.
238                                                If this flag is set the array of NameRecord structs
239                                                is ignored and a very compact 'name' table is automatically
240                                                generated. */
241
242         TTCF_IncludeOS2 = 2                 /** If this flag is set OS/2 table from the original font will be
243                                                 copied to the subset */
244     };
245 #endif
246
247     /** Structure used by GetTTGlyphMetrics() */
248     /*- In horisontal writing mode right sidebearing is calculated using this formula
249      *- rsb = aw - (lsb + xMax - xMin) -*/
250      typedef struct {
251          gint16  xMin;
252          gint16  yMin;
253          gint16  xMax;
254          gint16  yMax;
255          guint16 aw;                /*- Advance Width (horisontal writing mode)    */
256          gint16  lsb;               /*- Left sidebearing (horisontal writing mode) */
257          guint16 ah;                /*- advance height (vertical writing mode)     */
258          gint16  tsb;               /*- top sidebearing (vertical writing mode)    */
259      } TTGlyphMetrics;
260
261
262     /** Structure used by GetTTSimpleGlyphMetrics() and GetTTSimpleCharMetrics() functions */
263     typedef struct {
264         guint16 adv;                         /**< advance width or height            */
265         gint16 sb;                           /**< left or top sidebearing            */
266     } TTSimpleGlyphMetrics;
267
268     /** Structure returned by ReadGlyphMetrics() */
269     typedef struct {
270         guint16 aw, ah;
271         gint16 lsb, tsb;
272     } TTFullSimpleGlyphMetrics;
273
274
275 /** Structure used by the TrueType Creator and GetRawGlyphData() */
276
277     typedef struct {
278         guint32 glyphID;                     /**< glyph ID                           */
279         guint16 nbytes;                      /**< number of bytes in glyph data      */
280         guint8  *ptr;                         /**< pointer to glyph data              */
281         guint16 aw;                          /**< advance width                      */
282         gint16  lsb;                         /**< left sidebearing                   */
283         guint16 compflag;                    /**< 0- if non-composite, 1- otherwise  */
284         guint16 npoints;                     /**< number of points                   */
285         guint16 ncontours;                   /**< number of contours                 */
286         /* */
287         guint32 newID;                       /**< used internally by the TTCR        */
288     } GlyphData;
289
290
291 #ifndef STSF
292     /* STSF defines NameRecord and FUnitBBox structures in its own include file sttypes.h */
293
294     typedef struct {
295         gint16 xMin;
296         gint16 yMin;
297         gint16 xMax;
298         gint16 yMax;
299     } FUnitBBox;
300
301 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
302     typedef struct {
303         guint16 platformID;                  /**< Platform ID                                            */
304         guint16 encodingID;                  /**< Platform-specific encoding ID                          */
305         guint16 languageID;                  /**< Language ID                                            */
306         guint16 nameID;                      /**< Name ID                                                */
307         guint16 slen;                        /**< String length in bytes                                 */
308         guint8  *sptr;                        /**< Pointer to string data (not zero-terminated!)          */
309     } NameRecord;
310 #endif
311
312
313
314 /** Return value of GetTTGlobalFontInfo() */
315
316     typedef struct {
317         char *family;             /**< family name                                             */
318         guint16 *ufamily;                 /**< family name UCS2                                         */
319         char *subfamily;          /**< subfamily name                                          */
320         char *psname;             /**< PostScript name                                         */
321         int   weight;             /**< value of WeightClass or 0 if can't be determined        */
322         int   width;              /**< value of WidthClass or 0 if can't be determined         */
323         int   pitch;              /**< 0: proportianal font, otherwise: monospaced             */
324         int   italicAngle;        /**< in counter-clockwise degrees * 65536                    */
325         guint16 fsSelection;       /**< fsSelection field of OS/2 table                         */
326         int   xMin;               /**< global bounding box: xMin                               */
327         int   yMin;               /**< global bounding box: yMin                               */
328         int   xMax;               /**< global bounding box: xMax                               */
329         int   yMax;               /**< global bounding box: yMax                               */
330         int   ascender;           /**< typographic ascent.                                     */
331         int   descender;          /**< typographic descent.                                    */
332         int   linegap;            /**< typographic line gap.\ Negative values are treated as
333                                      zero in Win 3.1, System 6 and System 7.                 */
334         int   vascent;            /**< typographic ascent for vertical writing mode            */
335         int   vdescent;           /**< typographic descent for vertical writing mode           */
336         int   typoAscender;       /**< OS/2 portable typographic ascender                      */
337         int   typoDescender;      /**< OS/2 portable typographic descender                     */
338         int   typoLineGap;        /**< OS/2 portable typographc line gap                       */
339         int   winAscent;          /**< ascender metric for Windows                             */
340         int   winDescent;         /**< descender metric for Windows                            */
341         int   symbolEncoded;      /**< 1: MS symbol encoded 0: not symbol encoded              */
342         int   rangeFlag;          /**< if set to 1 Unicode Range flags are applicable          */
343         guint32 ur1;               /**< bits 0 - 31 of Unicode Range flags                      */
344         guint32 ur2;               /**< bits 32 - 63 of Unicode Range flags                     */
345         guint32 ur3;               /**< bits 64 - 95 of Unicode Range flags                     */
346         guint32 ur4;               /**< bits 96 - 127 of Unicode Range flags                    */
347         guint8   panose[10];        /**< PANOSE classification number                            */
348         guint16 typeFlags;                /**< type flags (copyright information)                      */
349     } TTGlobalFontInfo;
350
351 /** Structure used by KernGlyphs()      */
352     typedef struct {
353         int x;                    /**< positive: right, negative: left                        */
354         int y;                    /**< positive: up, negative: down                           */
355     } KernData;
356
357
358 /** ControlPoint structure used by GetTTGlyphPoints() */
359     typedef struct {
360         guint32 flags;             /**< 00000000 00000000 e0000000 bbbbbbbb */
361         /**< b - guint8 flags from the glyf array  */
362         /**< e == 0 - regular point              */
363         /**< e == 1 - end contour                */
364         gint16 x;                  /**< X coordinate in EmSquare units      */
365         gint16 y;                  /**< Y coordinate in EmSquare units      */
366     } ControlPoint;
367
368     typedef struct _TrueTypeFont TrueTypeFont;
369
370 /*
371  * @defgroup sft Sun Font Tools Exported Functions
372  */
373
374
375 /*
376  * Get the number of fonts contained in a TrueType collection
377  * @param  fname - file name
378  * @return number of fonts or zero, if file is not a TTC file.
379  * @ingroup sft
380  */
381     int CountTTCFonts(const char* fname);
382
383
384 /*
385  * TrueTypeFont constructor. 
386  * Reads the font file and allocates the memory for the structure.
387  * @param  facenum - logical font number within a TTC file. This value is ignored
388  *                   for TrueType fonts
389  * @return value of SFErrCodes enum
390  * @ingroup sft
391  */
392     int  OpenTTFont(const char *fname, guint32 facenum, TrueTypeFont**);
393
394 /*
395  * TrueTypeFont destructor. Deallocates the memory.
396  * @ingroup sft
397  */
398     void CloseTTFont(TrueTypeFont *);
399
400 /*
401  * Extracts TrueType control points, and stores them in an allocated array pointed to
402  * by *pointArray. This function returns the number of extracted points.
403  *
404  * @param ttf         pointer to the TrueTypeFont structure
405  * @param glyphID     Glyph ID
406  * @param pointArray  Return value - address of the pointer to the first element of the array
407  *                    of points allocated by the function
408  * @return            Returns the number of points in *pointArray or -1 if glyphID is
409  *                    invalid.
410  * @ingroup sft
411  *
412  */
413 int GetTTGlyphPoints(TrueTypeFont *ttf, guint32 glyphID, ControlPoint **pointArray);
414
415 /*
416  * Extracts bounding boxes in normalized FUnits (1000/em) for all glyphs in the
417  * font and allocates an array of FUnitBBox structures. There are ttf->nglyphs
418  * elements in the array.
419  *
420  * @param ttf     pointer to the TrueTypeFont structure
421  *
422  * @return        an array of FUnitBBox structures with values normalized to 1000 UPEm
423  *
424  * @ingroup sft
425  */
426 FUnitBBox *GetTTGlyphBoundingBoxes(TrueTypeFont *ttf);
427
428 /*
429  * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
430  * GlyphData structure.
431  *
432  * @param ttf         pointer to the TrueTypeFont structure
433  * @param glyphID     Glyph ID
434  *
435  * @return            pointer to an allocated GlyphData structure or NULL if
436  *                    glyphID is not present in the font
437  * @ingroup sft
438  *
439  */
440     GlyphData *GetTTRawGlyphData(TrueTypeFont *ttf, guint32 glyphID);
441
442 #ifndef NO_LIST
443 /*
444  * For a specified glyph adds all component glyphs IDs to the list and
445  * return their number. If the glyph is a single glyph it has one component
446  * glyph (which is added to the list) and the function returns 1.
447  * For a composite glyphs it returns the number of component glyphs
448  * and adds all of them to the list.
449  *
450  * @param ttf         pointer to the TrueTypeFont structure
451  * @param glyphID     Glyph ID
452  * @param glyphlist   list of glyphs
453  *
454  * @return            number of component glyphs
455  * @ingroup sft
456  *
457  */
458     int GetTTGlyphComponents(TrueTypeFont *ttf, guint32 glyphID, list glyphlist);
459 #endif
460
461 /*
462  * Extracts all Name Records from the font and stores them in an allocated
463  * array of NameRecord structs
464  *
465  * @param ttf       pointer to the TrueTypeFont struct
466  * @param nr        pointer to the array of NameRecord structs
467  *
468  * @return          number of NameRecord structs
469  * @ingroup sft
470  */
471
472     int GetTTNameRecords(TrueTypeFont *ttf, NameRecord **nr);
473
474 /*
475  * Deallocates previously allocated array of NameRecords.
476  *
477  * @param nr        array of NameRecord structs
478  * @param n         number of elements in the array
479  *
480  * @ingroup sft
481  */
482     void DisposeNameRecords(NameRecord* nr, int n);
483
484
485 #ifndef NO_TYPE3
486 /*
487  * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
488  * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
489  * @param ttf         pointer to the TrueTypeFont structure
490  * @param outf        the resulting font is written to this stream
491  * @param fname       font name for the new font. If it is NULL the PostScript name of the
492  *                    original font will be used
493  * @param glyphArray  pointer to an array of glyphs that are to be extracted from ttf
494  * @param encoding    array of encoding values. encoding[i] specifies the position of the glyph
495  *                    glyphArray[i] in the encoding vector of the resulting Type3 font
496  * @param nGlyphs     number of glyph IDs in glyphArray and encoding values in encoding
497  * @param wmode       writing mode for the output file: 0 - horizontal, 1 - vertical
498  * @return            return the value of SFErrCodes enum
499  * @see               SFErrCodes
500  * @ingroup sft
501  *
502  */
503     int  CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, guint16 *glyphArray, guint8 *encoding, int nGlyphs, int wmode);
504 #endif
505
506 #ifndef NO_TTCR
507 /*
508  * Generates a new TrueType font and dumps it to <b>outf</b> file.
509  * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
510  * @param ttf         pointer to the TrueTypeFont structure
511  * @param fname       file name for the output TrueType font file
512  * @param glyphArray  pointer to an array of glyphs that are to be extracted from ttf. The first
513  *                    element of this array has to be glyph 0 (default glyph)
514  * @param encoding    array of encoding values. encoding[i] specifies character code for
515  *                    the glyphID glyphArray[i]. Character code 0 usually points to a default
516  *                    glyph (glyphID 0)
517  * @param nGlyphs     number of glyph IDs in glyphArray and encoding values in encoding
518  * @param nNameRecs   number of NameRecords for the font, if 0 the name table from the
519  *                    original font will be used
520  * @param nr          array of NameRecords
521  * @param flags       or'ed TTCreationFlags
522  * @return            return the value of SFErrCodes enum
523  * @see               SFErrCodes
524  * @ingroup sft
525  *
526  */
527     int  CreateTTFromTTGlyphs(TrueTypeFont  *ttf,
528                               const char    *fname,
529                               guint16        *glyphArray,
530                               guint8          *encoding,
531                               int            nGlyphs,
532                               int            nNameRecs,
533                               NameRecord    *nr,
534                               guint32        flags);
535
536 // the same, but to memory (output to out_buf, out_len)
537     int  CreateTTFromTTGlyphs_tomemory
538                              (TrueTypeFont  *ttf,
539                               guint8        **out_buf,
540                               guint32       *out_len,
541                               guint16        *glyphArray,
542                               guint8          *encoding,
543                               int            nGlyphs,
544                               int            nNameRecs,
545                               NameRecord    *nr,
546                               guint32        flags);
547
548 #endif
549
550 #ifndef NO_TYPE42
551 /*
552  * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
553  * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
554  * @param ttf         pointer to the TrueTypeFont structure
555  * @param outf        output stream for a resulting font
556  * @param psname      PostScript name of the resulting font
557  * @param glyphArray  pointer to an array of glyphs that are to be extracted from ttf. The first
558  *                    element of this array has to be glyph 0 (default glyph)
559  * @param encoding    array of encoding values. encoding[i] specifies character code for
560  *                    the glyphID glyphArray[i]. Character code 0 usually points to a default
561  *                    glyph (glyphID 0)
562  * @param nGlyphs     number of glyph IDs in glyphArray and encoding values in encoding
563  * @return            SF_OK - no errors
564  *                    SF_GLYPHNUM - too many glyphs (> 255)
565  *                    SF_TTFORMAT - corrupted TrueType fonts
566  *
567  * @see               SFErrCodes
568  * @ingroup sft
569  *
570  */
571     int  CreateT42FromTTGlyphs(TrueTypeFont  *ttf,
572                                FILE          *outf,
573                                const char    *psname,
574                                guint16        *glyphArray,
575                                guint8          *encoding,
576                                int            nGlyphs);
577 #endif
578
579 /*
580  * Queries full glyph metrics for one glyph
581  */
582 void GetTTGlyphMetrics(TrueTypeFont *ttf, guint32 glyphID, TTGlyphMetrics *metrics);
583
584
585 /*
586  * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
587  *
588  * @param ttf         pointer to the TrueTypeFont structure
589  * @param glyphArray  pointer to an array of glyphs that are to be extracted from ttf
590  * @param nGlyphs     number of glyph IDs in glyphArray and encoding values in encoding
591  * @param mode        writing mode: 0 - horizontal, 1 - vertical
592  * @ingroup sft
593  *
594  */
595 TTSimpleGlyphMetrics *GetTTSimpleGlyphMetrics(TrueTypeFont *ttf, guint16 *glyphArray, int nGlyphs, int mode);
596
597 #ifndef NO_MAPPERS
598 /*
599  * Queries character metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
600  * This function behaves just like GetTTSimpleGlyphMetrics() but it takes a range of Unicode
601  * characters instead of an array of glyphs.
602  *
603  * @param ttf         pointer to the TrueTypeFont structure
604  * @param firstChar   Unicode value of the first character in the range
605  * @param nChars      number of Unicode characters in the range
606  * @param mode        writing mode: 0 - horizontal, 1 - vertical
607  *
608  * @see GetTTSimpleGlyphMetrics
609  * @ingroup sft
610  *
611  */
612     TTSimpleGlyphMetrics *GetTTSimpleCharMetrics(TrueTypeFont *ttf, guint16 firstChar, int nChars, int mode);
613
614 /*
615  * Maps a Unicode (UCS-2) string to a glyph array. Returns the number of glyphs in the array,
616  * which for TrueType fonts is always the same as the number of input characters.
617  *
618  * @param ttf         pointer to the TrueTypeFont structure
619  * @param str         pointer to a UCS-2 string
620  * @param nchars      number of characters in <b>str</b>
621  * @param glyphArray  pointer to the glyph array where glyph IDs are to be recorded.
622  *
623  * @return MapString() returns -1 if the TrueType font has no usable 'cmap' tables.
624  *         Otherwise it returns the number of characters processed: <b>nChars</b>
625  *
626  * glyphIDs of TrueType fonts are 2 guint8 positive numbers. glyphID of 0 denotes a missing
627  * glyph and traditionally defaults to an empty square.
628  * glyphArray should be at least sizeof(guint16) * nchars bytes long. If glyphArray is NULL
629  * MapString() replaces the UCS-2 characters in str with glyphIDs.
630  * @ingroup sft
631  */
632 #ifdef USE_GSUB
633     int MapString(TrueTypeFont *ttf, guint16 *str, int nchars, guint16 *glyphArray, int bvertical);
634 #else
635     int MapString(TrueTypeFont *ttf, guint16 *str, int nchars, guint16 *glyphArray);
636 #endif
637
638 /*
639  * Maps a Unicode (UCS-2) character to a glyph ID and returns it. Missing glyph has
640  * a glyphID of 0 so this function can be used to test if a character is encoded in the font.
641  *
642  * @param ttf         pointer to the TrueTypeFont structure
643  * @param ch          Unicode (UCS-2) character
644  * @return glyph ID, if the character is missing in the font, the return value is 0.
645  * @ingroup sft
646  */
647 #ifdef USE_GSUB
648     guint16 MapChar(TrueTypeFont *ttf, guint16 ch, int bvertical);
649 #else
650     guint16 MapChar(TrueTypeFont *ttf, guint16 ch);
651 #endif
652 #endif
653
654 /*
655  * Returns global font information about the TrueType font.
656  * @see TTGlobalFontInfo
657  *
658  * @param ttf         pointer to a TrueTypeFont structure
659  * @param info        pointer to a TTGlobalFontInfo structure
660  * @ingroup sft
661  *
662  */
663     void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info);
664
665 /*
666  * Returns kerning information for an array of glyphs.
667  * Kerning is not cumulative.
668  * kern[i] contains kerning information for a pair of glyphs at positions i and i+1
669  *
670  * @param ttf         pointer to a TrueTypeFont structure
671  * @param glyphs      array of source glyphs
672  * @param nglyphs     number of glyphs in the array
673  * @param wmode       writing mode: 0 - horizontal, 1 - vertical
674  * @param kern        array of KernData structures. It should contain nglyphs-1 elements
675  * @see KernData
676  * @ingroup sft
677  *
678  */
679 void KernGlyphs(TrueTypeFont *ttf, guint16 *glyphs, int nglyphs, int wmode, KernData *kern);
680
681 /*
682  * Returns nonzero if font is a symbol encoded font
683  */
684 int CheckSymbolEncoding(TrueTypeFont* ttf);
685
686 /*
687  * Extracts a 'cmap' table from a font, allocates memory for it and returns a pointer to it.
688  * DEPRECATED - use ExtractTable instead
689  */
690 #if 0
691 guint8 *ExtractCmap(TrueTypeFont *ttf);
692 #endif
693
694 /*
695  * Extracts a table from a font, allocates memort for it and returns a pointer to it.
696  */
697 guint8 *ExtractTable(TrueTypeFont *ttf, guint32 tag);
698
699 /*
700  * Returns a pointer to the table but does not allocate memory for it.
701  */
702 const guint8 *GetTable(TrueTypeFont *ttf, guint32 tag);
703
704 /*
705  * Functions that do not use TrueTypeFont structure
706  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
707  */
708
709 /*
710  * Reads full (vertical and horisontal) glyph metrics for an array of glyphs from hmtx and vmtx tables
711  *
712  * @param hmtx       TrueType hmtx table
713  * @param vmtx       TrueType vmtx table
714  * @param hcount     numberOfHMetrics value
715  * @param vcount     numOfLongVerMetrics value
716  * @param gcount     total number of glyphs in the font
717  * @param UPEm       units per Em value
718  * @param glyphArray array of source glyph IDs
719  * @param nGlyphs    number of glyphs in the glyphArray array
720  *
721  * @return           array of TTFullSimpleGlyphMetrics data structures
722  *
723  */
724
725 TTFullSimpleGlyphMetrics *ReadGlyphMetrics(guint8 *hmtx, guint8 *vmtx, int hcount, int vcount, int gcount, int UPEm, guint16 *glyphArray, int nGlyphs);
726
727 void ReadSingleGlyphMetrics(guint8 *hmtx, guint8 *vmtx, int hcount, int vcount, int gcount, int UPEm, guint16 glyphID, TTFullSimpleGlyphMetrics *metrics);
728
729
730 /*
731  * Returns the length of the 'kern' subtable
732  *
733  * @param kern       pointer to the 'kern' subtable
734  *
735  * @return           number of bytes in it
736  */
737 guint32 GetKernSubtableLength(guint8 *kern);
738
739 /*
740  *  Kerns a pair of glyphs.
741  *
742  * @param kerntype   type of the kern table
743  * @param nkern      number of kern subtables
744  * @param kern       array of pointers to kern subtables
745  * @pram unitsPerEm  units per Em value
746  * @param wmode      writing mode: 0 - horizontal, 1 - vertical
747  * @param a          ID of the first glyoh
748  * @param b          ID of the second glyoh
749  * @param x          X-axis kerning value is returned here
750  * @param y          Y-axis kerning value is returned here
751  */
752 void KernGlyphPair(int kerntype, guint32 nkern, guint8 **kern, int unitsPerEm, int wmode, guint32 a, guint32 b, int *x, int *y);
753
754
755
756 /*- private definitions */ /*FOLD00*/
757
758 struct _TrueTypeFont {
759     guint32 tag;
760
761     char   *fname;
762     off_t  fsize;
763     guint8   *ptr;
764
765     char   *psname;
766     char   *family;
767     guint16  *ufamily;
768     char   *subfamily;
769
770     guint32 ntables;
771     guint32 tdoffset;                              /* offset to the table directory (!= 0 for TrueType collections)      */
772     guint32 *goffsets;
773     int    nglyphs;
774     int    unitsPerEm;
775     int    numberOfHMetrics;
776     int    numOfLongVerMetrics;                   /* if this number is not 0, font has vertical metrics information */
777     guint8   *cmap;
778     int    cmapType;
779     guint16 (*mapper)(const guint8 *, guint16);       /* character to glyphID translation function                          */
780     void   **tables;                              /* array of pointers to tables                                        */
781     guint32 *tlens;                                /* array of table lengths                                             */
782     int    kerntype;                              /* Defined in the KernType enum                                       */
783     guint32 nkern;                                 /* number of kern subtables                                           */
784     guint8   **kerntables;                          /* array of pointers to kern subtables                                */
785 #ifdef USE_GSUB
786     void   *pGSubstitution;                       /* info provided by GSUB for UseGSUB()                                */
787 #endif
788 };
789
790 #ifdef __cplusplus
791 }
792 #endif
793
794 #endif /* __SUBFONT_H */