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>
49 #include <vcl/dllapi.h>
50 #include <vcl/fontcapabilities.hxx>
59 typedef sal_Int16 F2Dot14
; /**< fixed: 2.14 */
60 typedef sal_Int32 F16Dot16
; /**< fixed: 16.16 */
63 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
65 SF_OK
, /**< no error */
66 SF_BADFILE
, /**< file not found */
67 SF_FILEIO
, /**< file I/O error */
68 SF_MEMORY
, /**< memory allocation error */
69 SF_GLYPHNUM
, /**< incorrect number of glyphs */
70 SF_BADARG
, /**< incorrect arguments */
71 SF_TTFORMAT
, /**< incorrect TrueType font format */
72 SF_TABLEFORMAT
, /**< incorrect format of a TrueType table */
73 SF_FONTNO
/**< incorrect logical font number of a TTC font */
76 #ifndef FW_THIN /* WIN32 compilation would conflict */
77 /** Value of the weight member of the TTGlobalFontInfo struct */
79 FW_THIN
= 100, /**< Thin */
80 FW_EXTRALIGHT
= 200, /**< Extra-light (Ultra-light) */
81 FW_LIGHT
= 300, /**< Light */
82 FW_NORMAL
= 400, /**< Normal (Regular) */
83 FW_MEDIUM
= 500, /**< Medium */
84 FW_SEMIBOLD
= 600, /**< Semi-bold (Demi-bold) */
85 FW_BOLD
= 700, /**< Bold */
86 FW_EXTRABOLD
= 800, /**< Extra-bold (Ultra-bold) */
87 FW_BLACK
= 900 /**< Black (Heavy) */
91 /** Value of the width member of the TTGlobalFontInfo struct */
93 FWIDTH_ULTRA_CONDENSED
= 1, /**< 50% of normal */
94 FWIDTH_EXTRA_CONDENSED
= 2, /**< 62.5% of normal */
95 FWIDTH_CONDENSED
= 3, /**< 75% of normal */
96 FWIDTH_SEMI_CONDENSED
= 4, /**< 87.5% of normal */
97 FWIDTH_NORMAL
= 5, /**< Medium, 100% */
98 FWIDTH_SEMI_EXPANDED
= 6, /**< 112.5% of normal */
99 FWIDTH_EXPANDED
= 7, /**< 125% of normal */
100 FWIDTH_EXTRA_EXPANDED
= 8, /**< 150% of normal */
101 FWIDTH_ULTRA_EXPANDED
= 9 /**< 200% of normal */
104 /** Type of the 'kern' table, stored in TrueTypeFont::kerntype */
106 KT_NONE
= 0, /**< no kern table */
107 KT_APPLE_NEW
= 1, /**< new Apple kern table */
108 KT_MICROSOFT
= 2 /**< Microsoft table */
111 /* Composite glyph flags definition */
112 enum CompositeFlags
{
113 ARG_1_AND_2_ARE_WORDS
= 1,
114 ARGS_ARE_XY_VALUES
= 1<<1,
115 ROUND_XY_TO_GRID
= 1<<2,
116 WE_HAVE_A_SCALE
= 1<<3,
117 MORE_COMPONENTS
= 1<<5,
118 WE_HAVE_AN_X_AND_Y_SCALE
= 1<<6,
119 WE_HAVE_A_TWO_BY_TWO
= 1<<7,
120 WE_HAVE_INSTRUCTIONS
= 1<<8,
121 USE_MY_METRICS
= 1<<9,
122 OVERLAP_COMPOUND
= 1<<10
125 /** Flags for TrueType generation */
126 enum TTCreationFlags
{
127 TTCF_AutoName
= 1, /**< Automatically generate a compact 'name' table.
128 If this flag is not set, name table is generated
129 either from an array of NameRecord structs passed as
130 arguments or if the array is NULL, 'name' table
131 of the generated TrueType file will be a copy
132 of the name table of the original file.
133 If this flag is set the array of NameRecord structs
134 is ignored and a very compact 'name' table is automatically
137 TTCF_IncludeOS2
= 2 /** If this flag is set OS/2 table from the original font will be
138 copied to the subset */
141 /** Structure used by GetTTSimpleGlyphMetrics() and GetTTSimpleCharMetrics() functions */
143 sal_uInt16 adv
; /**< advance width or height */
144 sal_Int16 sb
; /**< left or top sidebearing */
145 } TTSimpleGlyphMetrics
;
147 /** Structure used by the TrueType Creator and GetRawGlyphData() */
150 sal_uInt32 glyphID
; /**< glyph ID */
151 sal_uInt16 nbytes
; /**< number of bytes in glyph data */
152 sal_uInt8
*ptr
; /**< pointer to glyph data */
153 sal_uInt16 aw
; /**< advance width */
154 sal_Int16 lsb
; /**< left sidebearing */
155 bool compflag
; /**< false- if non-composite */
156 sal_uInt16 npoints
; /**< number of points */
157 sal_uInt16 ncontours
; /**< number of contours */
159 sal_uInt32 newID
; /**< used internally by the TTCR */
162 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
164 sal_uInt16 platformID
; /**< Platform ID */
165 sal_uInt16 encodingID
; /**< Platform-specific encoding ID */
166 sal_uInt16 languageID
; /**< Language ID */
167 sal_uInt16 nameID
; /**< Name ID */
168 sal_uInt16 slen
; /**< String length in bytes */
169 sal_uInt8
*sptr
; /**< Pointer to string data (not zero-terminated!) */
172 /** Return value of GetTTGlobalFontInfo() */
175 char *family
; /**< family name */
176 sal_Unicode
*ufamily
; /**< family name UCS2 */
177 char *subfamily
; /**< subfamily name */
178 sal_Unicode
*usubfamily
; /**< subfamily name UCS2 */
179 char *psname
; /**< PostScript name */
180 sal_uInt16 macStyle
; /**< macstyle bits from 'HEAD' table */
181 int weight
; /**< value of WeightClass or 0 if can't be determined */
182 int width
; /**< value of WidthClass or 0 if can't be determined */
183 int pitch
; /**< 0: proportional font, otherwise: monospaced */
184 int italicAngle
; /**< in counter-clockwise degrees * 65536 */
185 int xMin
; /**< global bounding box: xMin */
186 int yMin
; /**< global bounding box: yMin */
187 int xMax
; /**< global bounding box: xMax */
188 int yMax
; /**< global bounding box: yMax */
189 int ascender
; /**< typographic ascent. */
190 int descender
; /**< typographic descent. */
191 int linegap
; /**< typographic line gap.\ Negative values are treated as
192 zero in Win 3.1, System 6 and System 7. */
193 int typoAscender
; /**< OS/2 portable typographic ascender */
194 int typoDescender
; /**< OS/2 portable typographic descender */
195 int typoLineGap
; /**< OS/2 portable typographic line gap */
196 int winAscent
; /**< ascender metric for Windows */
197 int winDescent
; /**< descender metric for Windows */
198 bool symbolEncoded
; /**< true: MS symbol encoded */
199 sal_uInt8 panose
[10]; /**< PANOSE classification number */
200 sal_uInt32 typeFlags
; /**< type flags (copyright bits + PS-OpenType flag) */
201 sal_uInt16 fsSelection
; /**< OS/2 fsSelection */
204 #define TYPEFLAG_INVALID 0x8000000
205 #define TYPEFLAG_COPYRIGHT_MASK 0x000000E
206 #define TYPEFLAG_PS_OPENTYPE 0x0010000
208 /** Structure used by KernGlyphs() */
210 int x
; /**< positive: right, negative: left */
211 int y
; /**< positive: up, negative: down */
214 /** ControlPoint structure used by GetTTGlyphPoints() */
216 sal_uInt32 flags
; /**< 00000000 00000000 e0000000 bbbbbbbb */
217 /**< b - byte flags from the glyf array */
218 /**< e == 0 - regular point */
219 /**< e == 1 - end contour */
220 sal_Int16 x
; /**< X coordinate in EmSquare units */
221 sal_Int16 y
; /**< Y coordinate in EmSquare units */
227 * @defgroup sft Sun Font Tools Exported Functions
231 * Get the number of fonts contained in a TrueType collection
232 * @param fname - file name
233 * @return number of fonts or zero, if file is not a TTC file.
236 int CountTTCFonts(const char* fname
);
239 * TrueTypeFont constructor.
240 * The font file has to be provided as a memory buffer and length
241 * @param pBuffer - memory buffer
242 * @param nLen - size of memory buffer
243 * @param facenum - logical font number within a TTC file. This value is ignored
245 * @param ttf - array of TrueTypeFonts
246 * @return value of SFErrCodes enum
249 int VCL_DLLPUBLIC
OpenTTFontBuffer(const void* pBuffer
, sal_uInt32 nLen
, sal_uInt32 facenum
, TrueTypeFont
** ttf
);
252 * TrueTypeFont constructor.
253 * Reads the font file and allocates the memory for the structure.
254 * on WIN32 the font has to be provided as a memory buffer and length
255 * @param fname - name of TrueType font file
256 * @param facenum - logical font number within a TTC file. This value is ignored
258 * @param ttf - array of TrueTypeFonts
259 * @return value of SFErrCodes enum
262 int VCL_DLLPUBLIC
OpenTTFontFile(const char *fname
, sal_uInt32 facenum
, TrueTypeFont
** ttf
);
265 void getTTScripts(std::vector
< sal_uInt32
> &rScriptTags
, const unsigned char* pTable
, size_t nLength
);
267 boost::optional
<std::bitset
<UnicodeCoverage::MAX_UC_ENUM
>> & rUnicodeCoverage
,
268 boost::optional
<std::bitset
<CodePageCoverage::MAX_CP_ENUM
>> & rCodePageCoverage
,
269 const unsigned char* pTable
, size_t nLength
);
272 * TrueTypeFont destructor. Deallocates the memory.
275 void VCL_DLLPUBLIC
CloseTTFont(TrueTypeFont
*);
278 * Extracts TrueType control points, and stores them in an allocated array pointed to
279 * by *pointArray. This function returns the number of extracted points.
281 * @param ttf pointer to the TrueTypeFont structure
282 * @param glyphID Glyph ID
283 * @param pointArray Return value - address of the pointer to the first element of the array
284 * of points allocated by the function
285 * @return Returns the number of points in *pointArray or -1 if glyphID is
290 int GetTTGlyphPoints(TrueTypeFont
*ttf
, sal_uInt32 glyphID
, ControlPoint
**pointArray
);
293 * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
294 * GlyphData structure.
296 * @param ttf pointer to the TrueTypeFont structure
297 * @param glyphID Glyph ID
299 * @return pointer to an allocated GlyphData structure or NULL if
300 * glyphID is not present in the font
304 GlyphData
*GetTTRawGlyphData(TrueTypeFont
*ttf
, sal_uInt32 glyphID
);
307 * For a specified glyph adds all component glyphs IDs to the list and
308 * return their number. If the glyph is a single glyph it has one component
309 * glyph (which is added to the list) and the function returns 1.
310 * For a composite glyphs it returns the number of component glyphs
311 * and adds all of them to the list.
313 * @param ttf pointer to the TrueTypeFont structure
314 * @param glyphID Glyph ID
315 * @param glyphlist list of glyphs
317 * @return number of component glyphs
321 int GetTTGlyphComponents(TrueTypeFont
*ttf
, sal_uInt32 glyphID
, std::vector
< sal_uInt32
>& glyphlist
);
324 * Extracts all Name Records from the font and stores them in an allocated
325 * array of NameRecord structs
327 * @param ttf pointer to the TrueTypeFont struct
328 * @param nr pointer to the array of NameRecord structs
330 * @return number of NameRecord structs
334 int GetTTNameRecords(TrueTypeFont
*ttf
, NameRecord
**nr
);
337 * Deallocates previously allocated array of NameRecords.
339 * @param nr array of NameRecord structs
340 * @param n number of elements in the array
344 void DisposeNameRecords(NameRecord
* nr
, int n
);
347 * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
348 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
349 * @param ttf pointer to the TrueTypeFont structure
350 * @param outf the resulting font is written to this stream
351 * @param fname font name for the new font. If it is NULL the PostScript name of the
352 * original font will be used
353 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
354 * @param encoding array of encoding values. encoding[i] specifies the position of the glyph
355 * glyphArray[i] in the encoding vector of the resulting Type3 font
356 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
357 * @param wmode writing mode for the output file: 0 - horizontal, 1 - vertical
358 * @return return the value of SFErrCodes enum
363 int CreateT3FromTTGlyphs(TrueTypeFont
*ttf
, FILE *outf
, const char *fname
, sal_uInt16
*glyphArray
, sal_uInt8
*encoding
, int nGlyphs
, int wmode
);
366 * Generates a new TrueType font and dumps it to <b>outf</b> file.
367 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
368 * @param ttf pointer to the TrueTypeFont structure
369 * @param fname file name for the output TrueType font file
370 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
371 * element of this array has to be glyph 0 (default glyph)
372 * @param encoding array of encoding values. encoding[i] specifies character code for
373 * the glyphID glyphArray[i]. Character code 0 usually points to a default
375 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
376 * @param nNameRecs number of NameRecords for the font, if 0 the name table from the
377 * original font will be used
378 * @param nr array of NameRecords
379 * @param flags or'ed TTCreationFlags
380 * @return return the value of SFErrCodes enum
385 int CreateTTFromTTGlyphs(TrueTypeFont
*ttf
,
387 sal_uInt16
*glyphArray
,
395 * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
396 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
397 * @param ttf pointer to the TrueTypeFont structure
398 * @param outf output stream for a resulting font
399 * @param psname PostScript name of the resulting font
400 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
401 * element of this array has to be glyph 0 (default glyph)
402 * @param encoding array of encoding values. encoding[i] specifies character code for
403 * the glyphID glyphArray[i]. Character code 0 usually points to a default
405 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
406 * @return SF_OK - no errors
407 * SF_GLYPHNUM - too many glyphs (> 255)
408 * SF_TTFORMAT - corrupted TrueType fonts
414 int CreateT42FromTTGlyphs(TrueTypeFont
*ttf
,
417 sal_uInt16
*glyphArray
,
422 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
424 * @param ttf pointer to the TrueTypeFont structure
425 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
426 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
427 * @param vertical writing mode: false - horizontal, true - vertical
431 TTSimpleGlyphMetrics
*GetTTSimpleGlyphMetrics(TrueTypeFont
*ttf
, sal_uInt16
*glyphArray
, int nGlyphs
, bool vertical
);
434 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
435 * This function behaves just like GetTTSimpleGlyphMetrics() but it takes a range of Unicode
436 * characters instead of an array of glyphs.
438 * @param ttf pointer to the TrueTypeFont structure
439 * @param firstChar Unicode value of the first character in the range
440 * @param nChars number of Unicode characters in the range
441 * @param vertical writing mode: false - horizontal, true - vertical
443 * @see GetTTSimpleGlyphMetrics
447 TTSimpleGlyphMetrics
*GetTTSimpleCharMetrics(TrueTypeFont
*ttf
, sal_uInt16 firstChar
, int nChars
, bool vertical
);
450 * Maps a Unicode (UCS-2) string to a glyph array. Returns the number of glyphs in the array,
451 * which for TrueType fonts is always the same as the number of input characters.
453 * @param ttf pointer to the TrueTypeFont structure
454 * @param str pointer to a UCS-2 string
455 * @param nchars number of characters in <b>str</b>
456 * @param glyphArray pointer to the glyph array where glyph IDs are to be recorded.
457 * @param bvertical vertical text
459 * @return MapString() returns -1 if the TrueType font has no usable 'cmap' tables.
460 * Otherwise it returns the number of characters processed: <b>nChars</b>
462 * glyphIDs of TrueType fonts are 2 byte positive numbers. glyphID of 0 denotes a missing
463 * glyph and traditionally defaults to an empty square.
464 * glyphArray should be at least sizeof(sal_uInt16) * nchars bytes long. If glyphArray is NULL
465 * MapString() replaces the UCS-2 characters in str with glyphIDs.
468 int VCL_DLLPUBLIC
MapString(TrueTypeFont
*ttf
, sal_uInt16
*str
, int nchars
, sal_uInt16
*glyphArray
, bool bvertical
);
470 #if defined(_WIN32) || defined(MACOSX) || defined(IOS)
472 * Maps a Unicode (UCS-2) character to a glyph ID and returns it. Missing glyph has
473 * a glyphID of 0 so this function can be used to test if a character is encoded in the font.
475 * @param ttf pointer to the TrueTypeFont structure
476 * @param ch Unicode (UCS-2) character
477 * @param bvertical flag to function that we want to find the vertical
478 * GlobalSUBstitution attribute
479 * @return glyph ID, if the character is missing in the font, the return value is 0.
482 sal_uInt16
MapChar(TrueTypeFont
*ttf
, sal_uInt16 ch
, bool bvertical
);
486 * Returns 0 when the font does not substitute vertical glyphs
488 * @param ttf pointer to the TrueTypeFont structure
489 * @param bvertical flag to function that we want to find the vertical
490 * GlobalSUBstitution attribute
492 int DoesVerticalSubstitution( TrueTypeFont
*ttf
, int bvertical
);
495 * Returns global font information about the TrueType font.
496 * @see TTGlobalFontInfo
498 * @param ttf pointer to a TrueTypeFont structure
499 * @param info pointer to a TTGlobalFontInfo structure
503 void GetTTGlobalFontInfo(TrueTypeFont
*ttf
, TTGlobalFontInfo
*info
);
506 * Returns fonts metrics.
507 * @see TTGlobalFontInfo
509 * @param hhea hhea table data
510 * @param os2 OS/2 table data
511 * @param info pointer to a TTGlobalFontInfo structure
515 void GetTTFontMterics(const std::vector
<uint8_t>& hhea
,
516 const std::vector
<uint8_t>& os2
,
517 TTGlobalFontInfo
*info
);
520 * returns the number of glyphs in a font
522 int GetTTGlyphCount( TrueTypeFont
* ttf
);
525 * provide access to the raw data of a SFNT-container's subtable
527 bool GetSfntTable( TrueTypeFont
* ttf
, int nSubtableIndex
,
528 const sal_uInt8
** ppRawBytes
, int* pRawLength
);
530 /*- private definitions */
532 struct TrueTypeFont
{
539 sal_Unicode
*ufamily
;
541 sal_Unicode
*usubfamily
;
544 sal_uInt32
*goffsets
;
546 sal_uInt32 unitsPerEm
;
547 sal_uInt32 numberOfHMetrics
;
548 sal_uInt32 numOfLongVerMetrics
; /* if this number is not 0, font has vertical metrics information */
549 const sal_uInt8
* cmap
;
551 sal_uInt32 (*mapper
)(const sal_uInt8
*, sal_uInt32
, sal_uInt32
); /* character to glyphID translation function */
552 const sal_uInt8
**tables
; /* array of pointers to raw subtables in SFNT file */
553 sal_uInt32
*tlens
; /* array of table lengths */
554 int kerntype
; /* Defined in the KernType enum */
555 sal_uInt32 nkern
; /* number of kern subtables */
556 const sal_uInt8
** kerntables
; /* array of pointers to kern subtables */
557 void *pGSubstitution
; /* info provided by GSUB for UseGSUB() */
560 /* indexes into TrueTypeFont::tables[] and TrueTypeFont::tlens[] */
561 #define O_maxp 0 /* 'maxp' */
562 #define O_glyf 1 /* 'glyf' */
563 #define O_head 2 /* 'head' */
564 #define O_loca 3 /* 'loca' */
565 #define O_name 4 /* 'name' */
566 #define O_hhea 5 /* 'hhea' */
567 #define O_hmtx 6 /* 'hmtx' */
568 #define O_cmap 7 /* 'cmap' */
569 #define O_vhea 8 /* 'vhea' */
570 #define O_vmtx 9 /* 'vmtx' */
571 #define O_OS2 10 /* 'OS/2' */
572 #define O_post 11 /* 'post' */
573 #define O_kern 12 /* 'kern' */
574 #define O_cvt 13 /* 'cvt_' - only used in TT->TT generation */
575 #define O_prep 14 /* 'prep' - only used in TT->TT generation */
576 #define O_fpgm 15 /* 'fpgm' - only used in TT->TT generation */
577 #define O_gsub 16 /* 'GSUB' */
578 #define O_CFF 17 /* 'CFF' */
583 #endif // INCLUDED_VCL_INC_SFT_HXX
585 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */