Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / inc / sft.hxx
blob17efc970fa9baf0f83397d3b3522e3bce1b77c53
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 /**
21 * @file sft.hxx
22 * @brief Sun Font Tools
23 * @author Alexander Gelfenbain
27 * Generated fonts contain an XUID entry in the form of:
29 * 103 0 T C1 N C2 C3
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
44 #ifdef UNX
45 #include <sys/types.h>
46 #include <unistd.h>
47 #endif
49 #include <vcl/dllapi.h>
50 #include <vcl/fontcapabilities.hxx>
51 #include <i18nlangtag/lang.h>
53 #include <vector>
54 #include <cstdint>
56 namespace vcl
59 /*@{*/
60 typedef sal_Int16 F2Dot14; /**< fixed: 2.14 */
61 typedef sal_Int32 F16Dot16; /**< fixed: 16.16 */
62 /*@}*/
64 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
65 enum SFErrCodes {
66 SF_OK, /**< no error */
67 SF_BADFILE, /**< file not found */
68 SF_FILEIO, /**< file I/O error */
69 SF_MEMORY, /**< memory allocation error */
70 SF_GLYPHNUM, /**< incorrect number of glyphs */
71 SF_BADARG, /**< incorrect arguments */
72 SF_TTFORMAT, /**< incorrect TrueType font format */
73 SF_TABLEFORMAT, /**< incorrect format of a TrueType table */
74 SF_FONTNO /**< incorrect logical font number of a TTC font */
77 #ifndef FW_THIN /* WIN32 compilation would conflict */
78 /** Value of the weight member of the TTGlobalFontInfo struct */
79 enum WeightClass {
80 FW_THIN = 100, /**< Thin */
81 FW_EXTRALIGHT = 200, /**< Extra-light (Ultra-light) */
82 FW_LIGHT = 300, /**< Light */
83 FW_NORMAL = 400, /**< Normal (Regular) */
84 FW_MEDIUM = 500, /**< Medium */
85 FW_SEMIBOLD = 600, /**< Semi-bold (Demi-bold) */
86 FW_BOLD = 700, /**< Bold */
87 FW_EXTRABOLD = 800, /**< Extra-bold (Ultra-bold) */
88 FW_BLACK = 900 /**< Black (Heavy) */
90 #endif /* FW_THIN */
92 /** Value of the width member of the TTGlobalFontInfo struct */
93 enum WidthClass {
94 FWIDTH_ULTRA_CONDENSED = 1, /**< 50% of normal */
95 FWIDTH_EXTRA_CONDENSED = 2, /**< 62.5% of normal */
96 FWIDTH_CONDENSED = 3, /**< 75% of normal */
97 FWIDTH_SEMI_CONDENSED = 4, /**< 87.5% of normal */
98 FWIDTH_NORMAL = 5, /**< Medium, 100% */
99 FWIDTH_SEMI_EXPANDED = 6, /**< 112.5% of normal */
100 FWIDTH_EXPANDED = 7, /**< 125% of normal */
101 FWIDTH_EXTRA_EXPANDED = 8, /**< 150% of normal */
102 FWIDTH_ULTRA_EXPANDED = 9 /**< 200% of normal */
105 /** Type of the 'kern' table, stored in TrueTypeFont::kerntype */
106 enum KernType {
107 KT_NONE = 0, /**< no kern table */
108 KT_APPLE_NEW = 1, /**< new Apple kern table */
109 KT_MICROSOFT = 2 /**< Microsoft table */
112 /** Composite glyph flags definition */
113 enum CompositeFlags {
114 ARG_1_AND_2_ARE_WORDS = 1,
115 ARGS_ARE_XY_VALUES = 1<<1,
116 ROUND_XY_TO_GRID = 1<<2,
117 WE_HAVE_A_SCALE = 1<<3,
118 MORE_COMPONENTS = 1<<5,
119 WE_HAVE_AN_X_AND_Y_SCALE = 1<<6,
120 WE_HAVE_A_TWO_BY_TWO = 1<<7,
121 WE_HAVE_INSTRUCTIONS = 1<<8,
122 USE_MY_METRICS = 1<<9,
123 OVERLAP_COMPOUND = 1<<10
126 /** Structure used by GetTTSimpleGlyphMetrics() and GetTTSimpleCharMetrics() functions */
127 typedef struct {
128 sal_uInt16 adv; /**< advance width or height */
129 sal_Int16 sb; /**< left or top sidebearing */
130 } TTSimpleGlyphMetrics;
132 /** Structure used by the TrueType Creator and GetRawGlyphData() */
134 typedef struct {
135 sal_uInt32 glyphID; /**< glyph ID */
136 sal_uInt16 nbytes; /**< number of bytes in glyph data */
137 sal_uInt8 *ptr; /**< pointer to glyph data */
138 sal_uInt16 aw; /**< advance width */
139 sal_Int16 lsb; /**< left sidebearing */
140 bool compflag; /**< false- if non-composite */
141 sal_uInt16 npoints; /**< number of points */
142 sal_uInt16 ncontours; /**< number of contours */
143 /* */
144 sal_uInt32 newID; /**< used internally by the TTCR */
145 } GlyphData;
147 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
148 typedef struct {
149 sal_uInt16 platformID; /**< Platform ID */
150 sal_uInt16 encodingID; /**< Platform-specific encoding ID */
151 LanguageType languageID; /**< Language ID */
152 sal_uInt16 nameID; /**< Name ID */
153 sal_uInt16 slen; /**< String length in bytes */
154 sal_uInt8 *sptr; /**< Pointer to string data (not zero-terminated!) */
155 } NameRecord;
157 /** Return value of GetTTGlobalFontInfo() */
159 typedef struct {
160 char *family; /**< family name */
161 sal_Unicode *ufamily; /**< family name UCS2 */
162 char *subfamily; /**< subfamily name */
163 sal_Unicode *usubfamily; /**< subfamily name UCS2 */
164 char *psname; /**< PostScript name */
165 sal_uInt16 macStyle; /**< macstyle bits from 'HEAD' table */
166 int weight; /**< value of WeightClass or 0 if can't be determined */
167 int width; /**< value of WidthClass or 0 if can't be determined */
168 int pitch; /**< 0: proportional font, otherwise: monospaced */
169 int italicAngle; /**< in counter-clockwise degrees * 65536 */
170 int xMin; /**< global bounding box: xMin */
171 int yMin; /**< global bounding box: yMin */
172 int xMax; /**< global bounding box: xMax */
173 int yMax; /**< global bounding box: yMax */
174 int ascender; /**< typographic ascent. */
175 int descender; /**< typographic descent. */
176 int linegap; /**< typographic line gap.\ Negative values are treated as
177 zero in Win 3.1, System 6 and System 7. */
178 int vascent; /**< typographic ascent for vertical writing mode */
179 int vdescent; /**< typographic descent for vertical writing mode */
180 int typoAscender; /**< OS/2 portable typographic ascender */
181 int typoDescender; /**< OS/2 portable typographic descender */
182 int typoLineGap; /**< OS/2 portable typographic line gap */
183 int winAscent; /**< ascender metric for Windows */
184 int winDescent; /**< descender metric for Windows */
185 bool symbolEncoded; /**< true: MS symbol encoded */
186 int rangeFlag; /**< if set to 1 Unicode Range flags are applicable */
187 sal_uInt32 ur1; /**< bits 0 - 31 of Unicode Range flags */
188 sal_uInt32 ur2; /**< bits 32 - 63 of Unicode Range flags */
189 sal_uInt32 ur3; /**< bits 64 - 95 of Unicode Range flags */
190 sal_uInt32 ur4; /**< bits 96 - 127 of Unicode Range flags */
191 sal_uInt8 panose[10]; /**< PANOSE classification number */
192 sal_uInt32 typeFlags; /**< type flags (copyright bits + PS-OpenType flag) */
193 sal_uInt16 fsSelection; /**< OS/2 fsSelection */
194 } TTGlobalFontInfo;
196 #define TYPEFLAG_INVALID 0x8000000
197 #define TYPEFLAG_COPYRIGHT_MASK 0x000000E
198 #define TYPEFLAG_PS_OPENTYPE 0x0010000
200 /** Structure used by KernGlyphs() */
201 typedef struct {
202 int x; /**< positive: right, negative: left */
203 int y; /**< positive: up, negative: down */
204 } KernData;
206 /** ControlPoint structure used by GetTTGlyphPoints() */
207 typedef struct {
208 sal_uInt32 flags; /**< 00000000 00000000 e0000000 bbbbbbbb */
209 /**< b - byte flags from the glyf array */
210 /**< e == 0 - regular point */
211 /**< e == 1 - end contour */
212 sal_Int16 x; /**< X coordinate in EmSquare units */
213 sal_Int16 y; /**< Y coordinate in EmSquare units */
214 } ControlPoint;
216 struct TrueTypeFont;
219 * @defgroup sft Sun Font Tools Exported Functions
223 * Get the number of fonts contained in a TrueType collection
224 * @param fname - file name
225 * @return number of fonts or zero, if file is not a TTC file.
226 * @ingroup sft
228 int CountTTCFonts(const char* fname);
231 * TrueTypeFont constructor.
232 * The font file has to be provided as a memory buffer and length
233 * @param pBuffer - memory buffer
234 * @param nLen - size of memory buffer
235 * @param facenum - logical font number within a TTC file. This value is ignored
236 * for TrueType fonts
237 * @param ttf - array of TrueTypeFonts
238 * @return value of SFErrCodes enum
239 * @ingroup sft
241 int VCL_DLLPUBLIC OpenTTFontBuffer(const void* pBuffer, sal_uInt32 nLen, sal_uInt32 facenum, TrueTypeFont** ttf);
242 #if !defined(_WIN32)
244 * TrueTypeFont constructor.
245 * Reads the font file and allocates the memory for the structure.
246 * on WIN32 the font has to be provided as a memory buffer and length
247 * @param fname - name of TrueType font file
248 * @param facenum - logical font number within a TTC file. This value is ignored
249 * for TrueType fonts
250 * @param ttf - array of TrueTypeFonts
251 * @return value of SFErrCodes enum
252 * @ingroup sft
254 int VCL_DLLPUBLIC OpenTTFontFile(const char *fname, sal_uInt32 facenum, TrueTypeFont** ttf);
255 #endif
257 bool getTTCoverage(
258 boost::optional<std::bitset<UnicodeCoverage::MAX_UC_ENUM>> & rUnicodeCoverage,
259 boost::optional<std::bitset<CodePageCoverage::MAX_CP_ENUM>> & rCodePageCoverage,
260 const unsigned char* pTable, size_t nLength);
263 * TrueTypeFont destructor. Deallocates the memory.
264 * @ingroup sft
266 void VCL_DLLPUBLIC CloseTTFont(TrueTypeFont *);
269 * Extracts TrueType control points, and stores them in an allocated array pointed to
270 * by *pointArray. This function returns the number of extracted points.
272 * @param ttf pointer to the TrueTypeFont structure
273 * @param glyphID Glyph ID
274 * @param pointArray Return value - address of the pointer to the first element of the array
275 * of points allocated by the function
276 * @return Returns the number of points in *pointArray or -1 if glyphID is
277 * invalid.
278 * @ingroup sft
281 int GetTTGlyphPoints(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray);
284 * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
285 * GlyphData structure.
287 * @param ttf pointer to the TrueTypeFont structure
288 * @param glyphID Glyph ID
290 * @return pointer to an allocated GlyphData structure or NULL if
291 * glyphID is not present in the font
292 * @ingroup sft
295 GlyphData *GetTTRawGlyphData(TrueTypeFont *ttf, sal_uInt32 glyphID);
298 * For a specified glyph adds all component glyphs IDs to the list and
299 * return their number. If the glyph is a single glyph it has one component
300 * glyph (which is added to the list) and the function returns 1.
301 * For a composite glyphs it returns the number of component glyphs
302 * and adds all of them to the list.
304 * @param ttf pointer to the TrueTypeFont structure
305 * @param glyphID Glyph ID
306 * @param glyphlist list of glyphs
308 * @return number of component glyphs
309 * @ingroup sft
312 int GetTTGlyphComponents(TrueTypeFont *ttf, sal_uInt32 glyphID, std::vector< sal_uInt32 >& glyphlist);
315 * Extracts all Name Records from the font and stores them in an allocated
316 * array of NameRecord structs
318 * @param ttf pointer to the TrueTypeFont struct
319 * @param nr pointer to the array of NameRecord structs
321 * @return number of NameRecord structs
322 * @ingroup sft
325 int GetTTNameRecords(TrueTypeFont *ttf, NameRecord **nr);
328 * Deallocates previously allocated array of NameRecords.
330 * @param nr array of NameRecord structs
331 * @param n number of elements in the array
333 * @ingroup sft
335 void DisposeNameRecords(NameRecord* nr, int n);
338 * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
339 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
340 * @param ttf pointer to the TrueTypeFont structure
341 * @param outf the resulting font is written to this stream
342 * @param fname font name for the new font. If it is NULL the PostScript name of the
343 * original font will be used
344 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
345 * @param encoding array of encoding values. encoding[i] specifies the position of the glyph
346 * glyphArray[i] in the encoding vector of the resulting Type3 font
347 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
348 * @param wmode writing mode for the output file: 0 - horizontal, 1 - vertical
349 * @return return the value of SFErrCodes enum
350 * @see SFErrCodes
351 * @ingroup sft
354 int CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, sal_uInt16 *glyphArray, sal_uInt8 *encoding, int nGlyphs, int wmode);
357 * Generates a new TrueType font and dumps it to <b>outf</b> file.
358 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
359 * @param ttf pointer to the TrueTypeFont structure
360 * @param fname file name for the output TrueType font file
361 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
362 * element of this array has to be glyph 0 (default glyph)
363 * @param encoding array of encoding values. encoding[i] specifies character code for
364 * the glyphID glyphArray[i]. Character code 0 usually points to a default
365 * glyph (glyphID 0)
366 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
367 * @param nNameRecs number of NameRecords for the font, if 0 the name table from the
368 * original font will be used
369 * @param nr array of NameRecords
370 * @param flags or'ed TTCreationFlags
371 * @return return the value of SFErrCodes enum
372 * @see SFErrCodes
373 * @ingroup sft
376 int CreateTTFromTTGlyphs(TrueTypeFont *ttf,
377 const char *fname,
378 sal_uInt16 *glyphArray,
379 sal_uInt8 *encoding,
380 int nGlyphs,
381 int nNameRecs,
382 NameRecord *nr);
385 * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
386 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
387 * @param ttf pointer to the TrueTypeFont structure
388 * @param outf output stream for a resulting font
389 * @param psname PostScript name of the resulting font
390 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
391 * element of this array has to be glyph 0 (default glyph)
392 * @param encoding array of encoding values. encoding[i] specifies character code for
393 * the glyphID glyphArray[i]. Character code 0 usually points to a default
394 * glyph (glyphID 0)
395 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
396 * @return SF_OK - no errors
397 * SF_GLYPHNUM - too many glyphs (> 255)
398 * SF_TTFORMAT - corrupted TrueType fonts
400 * @see SFErrCodes
401 * @ingroup sft
404 int CreateT42FromTTGlyphs(TrueTypeFont *ttf,
405 FILE *outf,
406 const char *psname,
407 sal_uInt16 *glyphArray,
408 sal_uInt8 *encoding,
409 int nGlyphs);
412 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
414 * @param ttf pointer to the TrueTypeFont structure
415 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
416 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
417 * @param vertical writing mode: false - horizontal, true - vertical
418 * @ingroup sft
421 TTSimpleGlyphMetrics *GetTTSimpleGlyphMetrics(TrueTypeFont *ttf, sal_uInt16 *glyphArray, int nGlyphs, bool vertical);
423 #if defined(_WIN32) || defined(MACOSX) || defined(IOS)
425 * Maps a Unicode (UCS-2) character to a glyph ID and returns it. Missing glyph has
426 * a glyphID of 0 so this function can be used to test if a character is encoded in the font.
428 * @param ttf pointer to the TrueTypeFont structure
429 * @param ch Unicode (UCS-2) character
430 * @return glyph ID, if the character is missing in the font, the return value is 0.
431 * @ingroup sft
433 sal_uInt16 MapChar(TrueTypeFont *ttf, sal_uInt16 ch);
434 #endif
437 * Returns global font information about the TrueType font.
438 * @see TTGlobalFontInfo
440 * @param ttf pointer to a TrueTypeFont structure
441 * @param info pointer to a TTGlobalFontInfo structure
442 * @ingroup sft
445 void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info);
448 * Returns fonts metrics.
449 * @see TTGlobalFontInfo
451 * @param hhea hhea table data
452 * @param os2 OS/2 table data
453 * @param info pointer to a TTGlobalFontInfo structure
454 * @ingroup sft
457 void GetTTFontMterics(const std::vector<uint8_t>& hhea,
458 const std::vector<uint8_t>& os2,
459 TTGlobalFontInfo *info);
462 * returns the number of glyphs in a font
464 int GetTTGlyphCount( TrueTypeFont* ttf );
467 * provide access to the raw data of a SFNT-container's subtable
469 bool GetSfntTable( TrueTypeFont* ttf, int nSubtableIndex,
470 const sal_uInt8** ppRawBytes, int* pRawLength );
472 /*- private definitions */
474 struct TrueTypeFont {
475 sal_uInt32 tag;
477 char *fname;
478 sal_Int32 fsize;
479 sal_uInt8 *ptr;
481 char *psname;
482 char *family;
483 sal_Unicode *ufamily;
484 char *subfamily;
485 sal_Unicode *usubfamily;
487 sal_uInt32 ntables;
488 sal_uInt32 *goffsets;
489 sal_uInt32 nglyphs;
490 sal_uInt32 unitsPerEm;
491 sal_uInt32 numberOfHMetrics;
492 sal_uInt32 numOfLongVerMetrics; /* if this number is not 0, font has vertical metrics information */
493 const sal_uInt8* cmap;
494 int cmapType;
495 sal_uInt32 (*mapper)(const sal_uInt8 *, sal_uInt32, sal_uInt32); /* character to glyphID translation function */
496 const sal_uInt8 **tables; /* array of pointers to raw subtables in SFNT file */
497 sal_uInt32 *tlens; /* array of table lengths */
498 int kerntype; /* Defined in the KernType enum */
499 sal_uInt32 nkern; /* number of kern subtables */
500 const sal_uInt8** kerntables; /* array of pointers to kern subtables */
501 void *pGSubstitution; /* info provided by GSUB for UseGSUB() */
504 /* indexes into TrueTypeFont::tables[] and TrueTypeFont::tlens[] */
505 #define O_maxp 0 /* 'maxp' */
506 #define O_glyf 1 /* 'glyf' */
507 #define O_head 2 /* 'head' */
508 #define O_loca 3 /* 'loca' */
509 #define O_name 4 /* 'name' */
510 #define O_hhea 5 /* 'hhea' */
511 #define O_hmtx 6 /* 'hmtx' */
512 #define O_cmap 7 /* 'cmap' */
513 #define O_vhea 8 /* 'vhea' */
514 #define O_vmtx 9 /* 'vmtx' */
515 #define O_OS2 10 /* 'OS/2' */
516 #define O_post 11 /* 'post' */
517 #define O_kern 12 /* 'kern' */
518 #define O_cvt 13 /* 'cvt_' - only used in TT->TT generation */
519 #define O_prep 14 /* 'prep' - only used in TT->TT generation */
520 #define O_fpgm 15 /* 'fpgm' - only used in TT->TT generation */
521 #define O_gsub 16 /* 'GSUB' */
522 #define O_CFF 17 /* 'CFF' */
523 #define NUM_TAGS 18
525 } // namespace vcl
527 #endif // INCLUDED_VCL_INC_SFT_HXX
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */