1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 * @brief Sun Font Tools
23 * @author Alexander Gelfenbain
27 * Generated fonts contain an XUID entry in the form of:
31 * 103 - Sun's Adobe assigned XUID number. Contact person: Alexander Gelfenbain <gelf@eng.sun.com>
33 * T - font type. 0: Type 3, 1: Type 42
34 * C1 - CRC-32 of the entire source TrueType font
35 * N - number of glyphs in the subset
36 * C2 - CRC-32 of the array of glyph IDs used to generate the subset
37 * C3 - CRC-32 of the array of encoding numbers used to generate the subset
41 #ifndef INCLUDED_VCL_INC_SFT_HXX
42 #define INCLUDED_VCL_INC_SFT_HXX
45 #include <sys/types.h>
50 #include <sal/types.h>
52 #include "vcl/dllapi.h"
56 #include "vcl/fontcapabilities.hxx"
62 typedef sal_Int16 F2Dot14
; /**< fixed: 2.14 */
63 typedef sal_Int32 F16Dot16
; /**< fixed: 16.16 */
71 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
73 SF_OK
, /**< no error */
74 SF_BADFILE
, /**< file not found */
75 SF_FILEIO
, /**< file I/O error */
76 SF_MEMORY
, /**< memory allocation error */
77 SF_GLYPHNUM
, /**< incorrect number of glyphs */
78 SF_BADARG
, /**< incorrect arguments */
79 SF_TTFORMAT
, /**< incorrect TrueType font format */
80 SF_TABLEFORMAT
, /**< incorrect format of a TrueType table */
81 SF_FONTNO
/**< incorrect logical font number of a TTC font */
84 #ifndef FW_THIN /* WIN32 compilation would conflict */
85 /** Value of the weight member of the TTGlobalFontInfo struct */
87 FW_THIN
= 100, /**< Thin */
88 FW_EXTRALIGHT
= 200, /**< Extra-light (Ultra-light) */
89 FW_LIGHT
= 300, /**< Light */
90 FW_NORMAL
= 400, /**< Normal (Regular) */
91 FW_MEDIUM
= 500, /**< Medium */
92 FW_SEMIBOLD
= 600, /**< Semi-bold (Demi-bold) */
93 FW_BOLD
= 700, /**< Bold */
94 FW_EXTRABOLD
= 800, /**< Extra-bold (Ultra-bold) */
95 FW_BLACK
= 900 /**< Black (Heavy) */
98 /** Value of the width member of the TTGlobalFontInfo struct */
100 FWIDTH_ULTRA_CONDENSED
= 1, /**< 50% of normal */
101 FWIDTH_EXTRA_CONDENSED
= 2, /**< 62.5% of normal */
102 FWIDTH_CONDENSED
= 3, /**< 75% of normal */
103 FWIDTH_SEMI_CONDENSED
= 4, /**< 87.5% of normal */
104 FWIDTH_NORMAL
= 5, /**< Medium, 100% */
105 FWIDTH_SEMI_EXPANDED
= 6, /**< 112.5% of normal */
106 FWIDTH_EXPANDED
= 7, /**< 125% of normal */
107 FWIDTH_EXTRA_EXPANDED
= 8, /**< 150% of normal */
108 FWIDTH_ULTRA_EXPANDED
= 9 /**< 200% of normal */
112 /** Type of the 'kern' table, stored in _TrueTypeFont::kerntype */
114 KT_NONE
= 0, /**< no kern table */
115 KT_APPLE_NEW
= 1, /**< new Apple kern table */
116 KT_MICROSOFT
= 2 /**< Microsoft table */
119 /* Composite glyph flags definition */
120 enum CompositeFlags
{
121 ARG_1_AND_2_ARE_WORDS
= 1,
122 ARGS_ARE_XY_VALUES
= 1<<1,
123 ROUND_XY_TO_GRID
= 1<<2,
124 WE_HAVE_A_SCALE
= 1<<3,
125 MORE_COMPONENTS
= 1<<5,
126 WE_HAVE_AN_X_AND_Y_SCALE
= 1<<6,
127 WE_HAVE_A_TWO_BY_TWO
= 1<<7,
128 WE_HAVE_INSTRUCTIONS
= 1<<8,
129 USE_MY_METRICS
= 1<<9,
130 OVERLAP_COMPOUND
= 1<<10
133 /** Flags for TrueType generation */
134 enum TTCreationFlags
{
135 TTCF_AutoName
= 1, /**< Automatically generate a compact 'name' table.
136 If this flag is not set, name table is generated
137 either from an array of NameRecord structs passed as
138 arguments or if the array is NULL, 'name' table
139 of the generated TrueType file will be a copy
140 of the name table of the original file.
141 If this flag is set the array of NameRecord structs
142 is ignored and a very compact 'name' table is automatically
145 TTCF_IncludeOS2
= 2 /** If this flag is set OS/2 table from the original font will be
146 copied to the subset */
149 /** Structure used by GetTTSimpleGlyphMetrics() and GetTTSimpleCharMetrics() functions */
151 sal_uInt16 adv
; /**< advance width or height */
152 sal_Int16 sb
; /**< left or top sidebearing */
153 } TTSimpleGlyphMetrics
;
155 /** Structure used by the TrueType Creator and GetRawGlyphData() */
158 sal_uInt32 glyphID
; /**< glyph ID */
159 sal_uInt16 nbytes
; /**< number of bytes in glyph data */
160 sal_uInt8
*ptr
; /**< pointer to glyph data */
161 sal_uInt16 aw
; /**< advance width */
162 sal_Int16 lsb
; /**< left sidebearing */
163 bool compflag
; /**< false- if non-composite */
164 sal_uInt16 npoints
; /**< number of points */
165 sal_uInt16 ncontours
; /**< number of contours */
167 sal_uInt32 newID
; /**< used internally by the TTCR */
170 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
172 sal_uInt16 platformID
; /**< Platform ID */
173 sal_uInt16 encodingID
; /**< Platform-specific encoding ID */
174 sal_uInt16 languageID
; /**< Language ID */
175 sal_uInt16 nameID
; /**< Name ID */
176 sal_uInt16 slen
; /**< String length in bytes */
177 sal_uInt8
*sptr
; /**< Pointer to string data (not zero-terminated!) */
180 /** Return value of GetTTGlobalFontInfo() */
183 char *family
; /**< family name */
184 sal_uInt16
*ufamily
; /**< family name UCS2 */
185 char *subfamily
; /**< subfamily name */
186 sal_uInt16
*usubfamily
; /**< subfamily name UCS2 */
187 char *psname
; /**< PostScript name */
188 sal_uInt16 macStyle
; /**< macstyle bits from 'HEAD' table */
189 int weight
; /**< value of WeightClass or 0 if can't be determined */
190 int width
; /**< value of WidthClass or 0 if can't be determined */
191 int pitch
; /**< 0: proportianal font, otherwise: monospaced */
192 int italicAngle
; /**< in counter-clockwise degrees * 65536 */
193 int xMin
; /**< global bounding box: xMin */
194 int yMin
; /**< global bounding box: yMin */
195 int xMax
; /**< global bounding box: xMax */
196 int yMax
; /**< global bounding box: yMax */
197 int ascender
; /**< typographic ascent. */
198 int descender
; /**< typographic descent. */
199 int linegap
; /**< typographic line gap.\ Negative values are treated as
200 zero in Win 3.1, System 6 and System 7. */
201 int vascent
; /**< typographic ascent for vertical writing mode */
202 int vdescent
; /**< typographic descent for vertical writing mode */
203 int typoAscender
; /**< OS/2 portable typographic ascender */
204 int typoDescender
; /**< OS/2 portable typographic descender */
205 int typoLineGap
; /**< OS/2 portable typographc line gap */
206 int winAscent
; /**< ascender metric for Windows */
207 int winDescent
; /**< descender metric for Windows */
208 bool symbolEncoded
; /**< true: MS symbol encoded */
209 int rangeFlag
; /**< if set to 1 Unicode Range flags are applicable */
210 sal_uInt32 ur1
; /**< bits 0 - 31 of Unicode Range flags */
211 sal_uInt32 ur2
; /**< bits 32 - 63 of Unicode Range flags */
212 sal_uInt32 ur3
; /**< bits 64 - 95 of Unicode Range flags */
213 sal_uInt32 ur4
; /**< bits 96 - 127 of Unicode Range flags */
214 sal_uInt8 panose
[10]; /**< PANOSE classification number */
215 sal_uInt32 typeFlags
; /**< type flags (copyright bits + PS-OpenType flag) */
218 #define TYPEFLAG_INVALID 0x8000000
219 #define TYPEFLAG_COPYRIGHT_MASK 0x000000E
220 #define TYPEFLAG_PS_OPENTYPE 0x0010000
222 /** Structure used by KernGlyphs() */
224 int x
; /**< positive: right, negative: left */
225 int y
; /**< positive: up, negative: down */
228 /** ControlPoint structure used by GetTTGlyphPoints() */
230 sal_uInt32 flags
; /**< 00000000 00000000 e0000000 bbbbbbbb */
231 /**< b - byte flags from the glyf array */
232 /**< e == 0 - regular point */
233 /**< e == 1 - end contour */
234 sal_Int16 x
; /**< X coordinate in EmSquare units */
235 sal_Int16 y
; /**< Y coordinate in EmSquare units */
238 typedef struct _TrueTypeFont TrueTypeFont
;
241 * @defgroup sft Sun Font Tools Exported Functions
245 * Get the number of fonts contained in a TrueType collection
246 * @param fname - file name
247 * @return number of fonts or zero, if file is not a TTC file.
250 int CountTTCFonts(const char* fname
);
253 * TrueTypeFont constructor.
254 * The font file has to be provided as a memory buffer and length
255 * @param facenum - logical font number within a TTC file. This value is ignored
257 * @return value of SFErrCodes enum
260 int VCL_DLLPUBLIC
OpenTTFontBuffer(const void* pBuffer
, sal_uInt32 nLen
, sal_uInt32 facenum
, TrueTypeFont
** ttf
);
263 * TrueTypeFont constructor.
264 * Reads the font file and allocates the memory for the structure.
265 * on WIN32 the font has to be provided as a memory buffer and length
266 * @param facenum - logical font number within a TTC file. This value is ignored
268 * @return value of SFErrCodes enum
271 int VCL_DLLPUBLIC
OpenTTFontFile(const char *fname
, sal_uInt32 facenum
, TrueTypeFont
** ttf
);
274 void getTTScripts(std::vector
< sal_uInt32
> &rScriptTags
, const unsigned char* pTable
, size_t nLength
);
276 boost::dynamic_bitset
<sal_uInt32
> &rUnicodeCoverage
,
277 boost::dynamic_bitset
<sal_uInt32
> &rCodePageCoverage
,
278 const unsigned char* pTable
, size_t nLength
);
281 * TrueTypeFont destructor. Deallocates the memory.
284 void VCL_DLLPUBLIC
CloseTTFont(TrueTypeFont
*);
287 * Extracts TrueType control points, and stores them in an allocated array pointed to
288 * by *pointArray. This function returns the number of extracted points.
290 * @param ttf pointer to the TrueTypeFont structure
291 * @param glyphID Glyph ID
292 * @param pointArray Return value - address of the pointer to the first element of the array
293 * of points allocated by the function
294 * @return Returns the number of points in *pointArray or -1 if glyphID is
299 int GetTTGlyphPoints(TrueTypeFont
*ttf
, sal_uInt32 glyphID
, ControlPoint
**pointArray
);
302 * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
303 * GlyphData structure.
305 * @param ttf pointer to the TrueTypeFont structure
306 * @param glyphID Glyph ID
308 * @return pointer to an allocated GlyphData structure or NULL if
309 * glyphID is not present in the font
313 GlyphData
*GetTTRawGlyphData(TrueTypeFont
*ttf
, sal_uInt32 glyphID
);
316 * For a specified glyph adds all component glyphs IDs to the list and
317 * return their number. If the glyph is a single glyph it has one component
318 * glyph (which is added to the list) and the function returns 1.
319 * For a composite glyphs it returns the number of component glyphs
320 * and adds all of them to the list.
322 * @param ttf pointer to the TrueTypeFont structure
323 * @param glyphID Glyph ID
324 * @param glyphlist list of glyphs
326 * @return number of component glyphs
330 int GetTTGlyphComponents(TrueTypeFont
*ttf
, sal_uInt32 glyphID
, std::vector
< sal_uInt32
>& glyphlist
);
333 * Extracts all Name Records from the font and stores them in an allocated
334 * array of NameRecord structs
336 * @param ttf pointer to the TrueTypeFont struct
337 * @param nr pointer to the array of NameRecord structs
339 * @return number of NameRecord structs
343 int GetTTNameRecords(TrueTypeFont
*ttf
, NameRecord
**nr
);
346 * Deallocates previously allocated array of NameRecords.
348 * @param nr array of NameRecord structs
349 * @param n number of elements in the array
353 void DisposeNameRecords(NameRecord
* nr
, int n
);
356 * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
357 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
358 * @param ttf pointer to the TrueTypeFont structure
359 * @param outf the resulting font is written to this stream
360 * @param fname font name for the new font. If it is NULL the PostScript name of the
361 * original font will be used
362 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
363 * @param encoding array of encoding values. encoding[i] specifies the position of the glyph
364 * glyphArray[i] in the encoding vector of the resulting Type3 font
365 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
366 * @param wmode writing mode for the output file: 0 - horizontal, 1 - vertical
367 * @return return the value of SFErrCodes enum
372 int CreateT3FromTTGlyphs(TrueTypeFont
*ttf
, FILE *outf
, const char *fname
, sal_uInt16
*glyphArray
, sal_uInt8
*encoding
, int nGlyphs
, int wmode
);
375 * Generates a new TrueType font and dumps it to <b>outf</b> file.
376 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
377 * @param ttf pointer to the TrueTypeFont structure
378 * @param fname file name for the output TrueType font file
379 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
380 * element of this array has to be glyph 0 (default glyph)
381 * @param encoding array of encoding values. encoding[i] specifies character code for
382 * the glyphID glyphArray[i]. Character code 0 usually points to a default
384 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
385 * @param nNameRecs number of NameRecords for the font, if 0 the name table from the
386 * original font will be used
387 * @param nr array of NameRecords
388 * @param flags or'ed TTCreationFlags
389 * @return return the value of SFErrCodes enum
394 int CreateTTFromTTGlyphs(TrueTypeFont
*ttf
,
396 sal_uInt16
*glyphArray
,
404 * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
405 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
406 * @param ttf pointer to the TrueTypeFont structure
407 * @param outf output stream for a resulting font
408 * @param psname PostScript name of the resulting font
409 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
410 * element of this array has to be glyph 0 (default glyph)
411 * @param encoding array of encoding values. encoding[i] specifies character code for
412 * the glyphID glyphArray[i]. Character code 0 usually points to a default
414 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
415 * @return SF_OK - no errors
416 * SF_GLYPHNUM - too many glyphs (> 255)
417 * SF_TTFORMAT - corrupted TrueType fonts
423 int CreateT42FromTTGlyphs(TrueTypeFont
*ttf
,
426 sal_uInt16
*glyphArray
,
431 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
433 * @param ttf pointer to the TrueTypeFont structure
434 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
435 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
436 * @param vertical writing mode: false - horizontal, true - vertical
440 TTSimpleGlyphMetrics
*GetTTSimpleGlyphMetrics(TrueTypeFont
*ttf
, sal_uInt16
*glyphArray
, int nGlyphs
, bool vertical
);
443 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
444 * This function behaves just like GetTTSimpleGlyphMetrics() but it takes a range of Unicode
445 * characters instead of an array of glyphs.
447 * @param ttf pointer to the TrueTypeFont structure
448 * @param firstChar Unicode value of the first character in the range
449 * @param nChars number of Unicode characters in the range
450 * @param vertical writing mode: false - horizontal, true - vertical
452 * @see GetTTSimpleGlyphMetrics
456 TTSimpleGlyphMetrics
*GetTTSimpleCharMetrics(TrueTypeFont
*ttf
, sal_uInt16 firstChar
, int nChars
, bool vertical
);
459 * Maps a Unicode (UCS-2) string to a glyph array. Returns the number of glyphs in the array,
460 * which for TrueType fonts is always the same as the number of input characters.
462 * @param ttf pointer to the TrueTypeFont structure
463 * @param str pointer to a UCS-2 string
464 * @param nchars number of characters in <b>str</b>
465 * @param glyphArray pointer to the glyph array where glyph IDs are to be recorded.
467 * @return MapString() returns -1 if the TrueType font has no usable 'cmap' tables.
468 * Otherwise it returns the number of characters processed: <b>nChars</b>
470 * glyphIDs of TrueType fonts are 2 byte positive numbers. glyphID of 0 denotes a missing
471 * glyph and traditionally defaults to an empty square.
472 * glyphArray should be at least sizeof(sal_uInt16) * nchars bytes long. If glyphArray is NULL
473 * MapString() replaces the UCS-2 characters in str with glyphIDs.
476 int VCL_DLLPUBLIC
MapString(TrueTypeFont
*ttf
, sal_uInt16
*str
, int nchars
, sal_uInt16
*glyphArray
, bool bvertical
);
479 * Maps a Unicode (UCS-2) character to a glyph ID and returns it. Missing glyph has
480 * a glyphID of 0 so this function can be used to test if a character is encoded in the font.
482 * @param ttf pointer to the TrueTypeFont structure
483 * @param ch Unicode (UCS-2) character
484 * @return glyph ID, if the character is missing in the font, the return value is 0.
487 sal_uInt16
MapChar(TrueTypeFont
*ttf
, sal_uInt16 ch
, bool bvertical
);
490 * Returns 0 when the font does not substitute vertical glyphs
492 * @param ttf pointer to the TrueTypeFont structure
494 int DoesVerticalSubstitution( TrueTypeFont
*ttf
, int bvertical
);
497 * Returns global font information about the TrueType font.
498 * @see TTGlobalFontInfo
500 * @param ttf pointer to a TrueTypeFont structure
501 * @param info pointer to a TTGlobalFontInfo structure
505 void GetTTGlobalFontInfo(TrueTypeFont
*ttf
, TTGlobalFontInfo
*info
);
508 * Returns nonzero if font is a symbol encoded font
510 int CheckSymbolEncoding(TrueTypeFont
* ttf
);
513 * returns the number of glyphs in a font
515 int GetTTGlyphCount( TrueTypeFont
* ttf
);
518 * provide access to the raw data of a SFNT-container's subtable
520 bool GetSfntTable( TrueTypeFont
* ttf
, int nSubtableIndex
,
521 const sal_uInt8
** ppRawBytes
, int* pRawLength
);
523 /*- private definitions */
525 struct _TrueTypeFont
{
536 sal_uInt16
*usubfamily
;
539 sal_uInt32
*goffsets
;
541 sal_uInt32 unitsPerEm
;
542 sal_uInt32 numberOfHMetrics
;
543 sal_uInt32 numOfLongVerMetrics
; /* if this number is not 0, font has vertical metrics information */
544 const sal_uInt8
* cmap
;
546 sal_uInt32 (*mapper
)(const sal_uInt8
*, sal_uInt32
, sal_uInt32
); /* character to glyphID translation function */
547 const sal_uInt8
**tables
; /* array of pointers to raw subtables in SFNT file */
548 sal_uInt32
*tlens
; /* array of table lengths */
549 int kerntype
; /* Defined in the KernType enum */
550 sal_uInt32 nkern
; /* number of kern subtables */
551 const sal_uInt8
** kerntables
; /* array of pointers to kern subtables */
552 void *pGSubstitution
; /* info provided by GSUB for UseGSUB() */
555 /* indexes into _TrueTypeFont::tables[] and _TrueTypeFont::tlens[] */
556 #define O_maxp 0 /* 'maxp' */
557 #define O_glyf 1 /* 'glyf' */
558 #define O_head 2 /* 'head' */
559 #define O_loca 3 /* 'loca' */
560 #define O_name 4 /* 'name' */
561 #define O_hhea 5 /* 'hhea' */
562 #define O_hmtx 6 /* 'hmtx' */
563 #define O_cmap 7 /* 'cmap' */
564 #define O_vhea 8 /* 'vhea' */
565 #define O_vmtx 9 /* 'vmtx' */
566 #define O_OS2 10 /* 'OS/2' */
567 #define O_post 11 /* 'post' */
568 #define O_kern 12 /* 'kern' */
569 #define O_cvt 13 /* 'cvt_' - only used in TT->TT generation */
570 #define O_prep 14 /* 'prep' - only used in TT->TT generation */
571 #define O_fpgm 15 /* 'fpgm' - only used in TT->TT generation */
572 #define O_gsub 16 /* 'GSUB' */
573 #define O_CFF 17 /* 'CFF' */
578 #endif // INCLUDED_VCL_INC_SFT_HXX
580 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */