nss: upgrade to release 3.73
[LibreOffice.git] / vcl / inc / sft.hxx
blob6a844002711d32d8310ce652303d0fc54e77be43
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
26 * Generated fonts contain an XUID entry in the form of:
28 * 103 0 T C1 N C2 C3
30 * 103 - Sun's Adobe assigned XUID number. Contact person: Alexander Gelfenbain <gelf@eng.sun.com>
32 * T - font type. 0: Type 3, 1: Type 42
33 * C1 - CRC-32 of the entire source TrueType font
34 * N - number of glyphs in the subset
35 * C2 - CRC-32 of the array of glyph IDs used to generate the subset
36 * C3 - CRC-32 of the array of encoding numbers used to generate the subset
40 #ifndef INCLUDED_VCL_INC_SFT_HXX
41 #define INCLUDED_VCL_INC_SFT_HXX
43 #include <vcl/dllapi.h>
44 #include <vcl/fontcapabilities.hxx>
45 #include <vcl/fontcharmap.hxx>
46 #include <i18nlangtag/lang.h>
48 #include <array>
49 #include <memory>
50 #include <vector>
51 #include <cstdint>
53 namespace vcl
56 /*@{*/
57 typedef sal_Int32 F16Dot16; /**< fixed: 16.16 */
58 /*@}*/
60 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
61 enum class SFErrCodes {
62 Ok, /**< no error */
63 BadFile, /**< file not found */
64 FileIo, /**< file I/O error */
65 Memory, /**< memory allocation error */
66 GlyphNum, /**< incorrect number of glyphs */
67 BadArg, /**< incorrect arguments */
68 TtFormat, /**< incorrect TrueType font format */
69 FontNo /**< incorrect logical font number of a TTC font */
72 #ifndef FW_THIN /* WIN32 compilation would conflict */
73 /** Value of the weight member of the TTGlobalFontInfo struct */
74 enum WeightClass {
75 FW_THIN = 100, /**< Thin */
76 FW_EXTRALIGHT = 200, /**< Extra-light (Ultra-light) */
77 FW_LIGHT = 300, /**< Light */
78 FW_NORMAL = 400, /**< Normal (Regular) */
79 FW_MEDIUM = 500, /**< Medium */
80 FW_SEMIBOLD = 600, /**< Semi-bold (Demi-bold) */
81 FW_BOLD = 700, /**< Bold */
82 FW_EXTRABOLD = 800, /**< Extra-bold (Ultra-bold) */
83 FW_BLACK = 900 /**< Black (Heavy) */
85 #endif /* FW_THIN */
87 /** Value of the width member of the TTGlobalFontInfo struct */
88 enum WidthClass {
89 FWIDTH_ULTRA_CONDENSED = 1, /**< 50% of normal */
90 FWIDTH_EXTRA_CONDENSED = 2, /**< 62.5% of normal */
91 FWIDTH_CONDENSED = 3, /**< 75% of normal */
92 FWIDTH_SEMI_CONDENSED = 4, /**< 87.5% of normal */
93 FWIDTH_NORMAL = 5, /**< Medium, 100% */
94 FWIDTH_SEMI_EXPANDED = 6, /**< 112.5% of normal */
95 FWIDTH_EXPANDED = 7, /**< 125% of normal */
96 FWIDTH_EXTRA_EXPANDED = 8, /**< 150% of normal */
97 FWIDTH_ULTRA_EXPANDED = 9 /**< 200% of normal */
100 /** Composite glyph flags definition */
101 enum CompositeFlags {
102 ARG_1_AND_2_ARE_WORDS = 1,
103 ARGS_ARE_XY_VALUES = 1<<1,
104 ROUND_XY_TO_GRID = 1<<2,
105 WE_HAVE_A_SCALE = 1<<3,
106 MORE_COMPONENTS = 1<<5,
107 WE_HAVE_AN_X_AND_Y_SCALE = 1<<6,
108 WE_HAVE_A_TWO_BY_TWO = 1<<7,
109 WE_HAVE_INSTRUCTIONS = 1<<8,
110 USE_MY_METRICS = 1<<9,
111 OVERLAP_COMPOUND = 1<<10
114 /** Structure used by GetTTSimpleCharMetrics() functions */
115 typedef struct {
116 sal_uInt16 adv; /**< advance width or height */
117 sal_Int16 sb; /**< left or top sidebearing */
118 } TTSimpleGlyphMetrics;
120 /** Structure used by the TrueType Creator and GetRawGlyphData() */
122 typedef struct {
123 sal_uInt32 glyphID; /**< glyph ID */
124 sal_uInt16 nbytes; /**< number of bytes in glyph data */
125 sal_uInt8 *ptr; /**< pointer to glyph data */
126 sal_uInt16 aw; /**< advance width */
127 sal_Int16 lsb; /**< left sidebearing */
128 bool compflag; /**< false- if non-composite */
129 sal_uInt16 npoints; /**< number of points */
130 sal_uInt16 ncontours; /**< number of contours */
131 /* */
132 sal_uInt32 newID; /**< used internally by the TTCR */
133 } GlyphData;
135 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
136 typedef struct {
137 sal_uInt16 platformID; /**< Platform ID */
138 sal_uInt16 encodingID; /**< Platform-specific encoding ID */
139 LanguageType languageID; /**< Language ID */
140 sal_uInt16 nameID; /**< Name ID */
141 sal_uInt16 slen; /**< String length in bytes */
142 sal_uInt8 *sptr; /**< Pointer to string data (not zero-terminated!) */
143 } NameRecord;
145 /** Return value of GetTTGlobalFontInfo() */
147 typedef struct TTGlobalFontInfo_ {
148 char *family; /**< family name */
149 sal_Unicode *ufamily; /**< family name UCS2 */
150 char *subfamily; /**< subfamily name */
151 sal_Unicode *usubfamily; /**< subfamily name UCS2 */
152 char *psname; /**< PostScript name */
153 sal_uInt16 macStyle; /**< macstyle bits from 'HEAD' table */
154 int weight; /**< value of WeightClass or 0 if can't be determined */
155 int width; /**< value of WidthClass or 0 if can't be determined */
156 int pitch; /**< 0: proportional font, otherwise: monospaced */
157 int italicAngle; /**< in counter-clockwise degrees * 65536 */
158 int xMin; /**< global bounding box: xMin */
159 int yMin; /**< global bounding box: yMin */
160 int xMax; /**< global bounding box: xMax */
161 int yMax; /**< global bounding box: yMax */
162 int ascender; /**< typographic ascent. */
163 int descender; /**< typographic descent. */
164 int linegap; /**< typographic line gap.\ Negative values are treated as
165 zero in Win 3.1, System 6 and System 7. */
166 int typoAscender; /**< OS/2 portable typographic ascender */
167 int typoDescender; /**< OS/2 portable typographic descender */
168 int typoLineGap; /**< OS/2 portable typographic line gap */
169 int winAscent; /**< ascender metric for Windows */
170 int winDescent; /**< descender metric for Windows */
171 bool symbolEncoded; /**< true: MS symbol encoded */
172 sal_uInt8 panose[10]; /**< PANOSE classification number */
173 sal_uInt32 typeFlags; /**< type flags (copyright bits) */
174 sal_uInt16 fsSelection; /**< OS/2 fsSelection */
175 } TTGlobalFontInfo;
177 /** ControlPoint structure used by GetTTGlyphPoints() */
178 typedef struct {
179 sal_uInt32 flags; /**< 00000000 00000000 e0000000 bbbbbbbb */
180 /**< b - byte flags from the glyf array */
181 /**< e == 0 - regular point */
182 /**< e == 1 - end contour */
183 sal_Int16 x; /**< X coordinate in EmSquare units */
184 sal_Int16 y; /**< Y coordinate in EmSquare units */
185 } ControlPoint;
189 Some table OS/2 consts
190 quick history:
191 OpenType has been created from TrueType
192 - original TrueType had an OS/2 table with a length of 68 bytes
193 (cf https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html)
194 - There have been 6 versions (from version 0 to 5)
195 (cf https://docs.microsoft.com/en-us/typography/opentype/otspec140/os2ver0)
197 For the record:
198 // From Initial TrueType version
199 TYPE NAME FROM BYTE
200 uint16 version 0
201 int16 xAvgCharWidth 2
202 uint16 usWeightClass 4
203 uint16 usWidthClass 6
204 uint16 fsType 8
205 int16 ySubscriptXSize 10
206 int16 ySubscriptYSize 12
207 int16 ySubscriptXOffset 14
208 int16 ySubscriptYOffset 16
209 int16 ySuperscriptXSize 18
210 int16 ySuperscriptYSize 20
211 int16 ySuperscriptXOffset 22
212 int16 ySuperscriptYOffset 24
213 int16 yStrikeoutSize 26
214 int16 yStrikeoutPosition 28
215 int16 sFamilyClass 30
216 uint8 panose[10] 32
217 uint32 ulUnicodeRange1 42
218 uint32 ulUnicodeRange2 46
219 uint32 ulUnicodeRange3 50
220 uint32 ulUnicodeRange4 54
221 Tag achVendID 58
222 uint16 fsSelection 62
223 uint16 usFirstCharIndex 64
224 uint16 usLastCharIndex 66
226 // From Version 0 of OpenType
227 int16 sTypoAscender 68
228 int16 sTypoDescender 70
229 int16 sTypoLineGap 72
230 uint16 usWinAscent 74
231 uint16 usWinDescent 76
233 => length for OpenType version 0 = 78 bytes
235 // From Version 1 of OpenType
236 uint32 ulCodePageRange1 78
237 uint32 ulCodePageRange2 82
239 => length for OpenType version 1 = 86 bytes
241 // From Version 2 of OpenType
242 // (idem for Versions 3 and 4)
243 int16 sxHeight 86
244 int16 sCapHeight 88
245 uint16 usDefaultChar 90
246 uint16 usBreakChar 92
247 uint16 usMaxContext 94
249 => length for OpenType version 2, 3 and 4 = 96 bytes
251 // From Version 5 of OpenType
252 uint16 usLowerOpticalPointSize 96
253 uint16 usUpperOpticalPointSize 98
254 END 100
256 => length for OS/2 table version 5 = 100 bytes
259 constexpr int OS2_Legacy_length = 68;
260 constexpr int OS2_V0_length = 78;
261 constexpr int OS2_V1_length = 86;
263 constexpr int OS2_usWeightClass_offset = 4;
264 constexpr int OS2_usWidthClass_offset = 6;
265 constexpr int OS2_fsType_offset = 8;
266 constexpr int OS2_panose_offset = 32;
267 constexpr int OS2_panoseNbBytes_offset = 10;
268 constexpr int OS2_ulUnicodeRange1_offset = 42;
269 constexpr int OS2_ulUnicodeRange2_offset = 46;
270 constexpr int OS2_ulUnicodeRange3_offset = 50;
271 constexpr int OS2_ulUnicodeRange4_offset = 54;
272 constexpr int OS2_fsSelection_offset = 62;
273 constexpr int OS2_typoAscender_offset = 68;
274 constexpr int OS2_typoDescender_offset = 70;
275 constexpr int OS2_typoLineGap_offset = 72;
276 constexpr int OS2_winAscent_offset = 74;
277 constexpr int OS2_winDescent_offset = 76;
278 constexpr int OS2_ulCodePageRange1_offset = 78;
279 constexpr int OS2_ulCodePageRange2_offset = 82;
282 Some table hhea consts
283 cf https://docs.microsoft.com/fr-fr/typography/opentype/spec/hhea
284 TYPE NAME FROM BYTE
285 uint16 majorVersion 0
286 uint16 minorVersion 2
287 FWORD ascender 4
288 FWORD descender 6
289 FWORD lineGap 8
290 UFWORD advanceWidthMax 10
291 FWORD minLeftSideBearing 12
292 FWORD minRightSideBearing 14
293 FWORD xMaxExtent 16
294 int16 caretSlopeRise 18
295 int16 caretSlopeRun 20
296 int16 caretOffset 22
297 int16 (reserved) 24
298 int16 (reserved) 26
299 int16 (reserved) 28
300 int16 (reserved) 30
301 int16 metricDataFormat 32
302 uint16 numberOfHMetrics 34
303 END 36
305 => length for hhea table = 36 bytes
308 constexpr int HHEA_Length = 36;
310 constexpr int HHEA_ascender_offset = 4;
311 constexpr int HHEA_descender_offset = 6;
312 constexpr int HHEA_lineGap_offset = 8;
313 constexpr int HHEA_caretSlopeRise_offset = 18;
314 constexpr int HHEA_caretSlopeRun_offset = 20;
317 Some table post consts
318 cf https://docs.microsoft.com/fr-fr/typography/opentype/spec/post
319 TYPE NAME FROM BYTE
320 Fixed version 0
321 Fixed italicAngle 4
322 FWord underlinePosition 8
323 FWord underlineThickness 10
324 uint32 isFixedPitch 12
328 constexpr int POST_italicAngle_offset = 4;
329 constexpr int POST_underlinePosition_offset = 8;
330 constexpr int POST_underlineThickness_offset = 10;
331 constexpr int POST_isFixedPitch_offset = 12;
334 Some table head consts
335 cf https://docs.microsoft.com/fr-fr/typography/opentype/spec/head
336 TYPE NAME FROM BYTE
337 uit16 majorVersion 0
338 uit16 minorVersion 2
339 Fixed fontRevision 4
340 uint32 checkSumAdjustment 8
341 uint32 magicNumber 12 (= 0x5F0F3CF5)
342 uint16 flags 16
343 uint16 unitsPerEm 18
344 LONGDATETIME created 20
345 LONGDATETIME modified 28
346 int16 xMin 36
347 int16 yMin 38
348 int16 xMax 40
349 int16 yMax 42
350 uint16 macStyle 44
351 uint16 lowestRecPPEM 46
352 int16 fontDirectionHint 48
353 int16 indexToLocFormat 50
354 int16 glyphDataFormat 52
356 END 54
358 => length head table = 54 bytes
360 constexpr int HEAD_Length = 54;
362 constexpr int HEAD_majorVersion_offset = 0;
363 constexpr int HEAD_fontRevision_offset = 4;
364 constexpr int HEAD_magicNumber_offset = 12;
365 constexpr int HEAD_flags_offset = 16;
366 constexpr int HEAD_unitsPerEm_offset = 18;
367 constexpr int HEAD_created_offset = 20;
368 constexpr int HEAD_xMin_offset = 36;
369 constexpr int HEAD_yMin_offset = 38;
370 constexpr int HEAD_xMax_offset = 40;
371 constexpr int HEAD_yMax_offset = 42;
372 constexpr int HEAD_macStyle_offset = 44;
373 constexpr int HEAD_lowestRecPPEM_offset = 46;
374 constexpr int HEAD_fontDirectionHint_offset = 48;
375 constexpr int HEAD_indexToLocFormat_offset = 50;
376 constexpr int HEAD_glyphDataFormat_offset = 52;
379 Some table maxp consts
380 cf https://docs.microsoft.com/fr-fr/typography/opentype/spec/maxp
381 For 0.5 version
382 TYPE NAME FROM BYTE
383 Fixed version 0
384 uint16 numGlyphs 4
386 For 1.0 Version
387 Fixed version 0
388 uint16 numGlyphs 4
389 uint16 maxPoints 6
390 uint16 maxContours 8
391 uint16 maxCompositePoints 10
392 uint16 maxCompositeContours 12
396 constexpr int MAXP_Version1Length = 32;
398 constexpr int MAXP_numGlyphs_offset = 4;
399 constexpr int MAXP_maxPoints_offset = 6;
400 constexpr int MAXP_maxContours_offset = 8;
401 constexpr int MAXP_maxCompositePoints_offset = 10;
402 constexpr int MAXP_maxCompositeContours_offset = 12;
405 Some table glyf consts
406 cf https://docs.microsoft.com/fr-fr/typography/opentype/spec/glyf
407 For 0.5 version
408 TYPE NAME FROM BYTE
409 int16 numberOfContours 0
410 int16 xMin 2
411 int16 yMin 4
412 int16 xMax 6
413 int16 yMax 8
415 END 10
417 => length glyf table = 10 bytes
420 constexpr int GLYF_Length = 10;
422 constexpr int GLYF_numberOfContours_offset = 0;
423 constexpr int GLYF_xMin_offset = 2;
424 constexpr int GLYF_yMin_offset = 4;
425 constexpr int GLYF_xMax_offset = 6;
426 constexpr int GLYF_yMax_offset = 8;
428 constexpr sal_uInt32 T_true = 0x74727565; /* 'true' */
429 constexpr sal_uInt32 T_ttcf = 0x74746366; /* 'ttcf' */
430 constexpr sal_uInt32 T_otto = 0x4f54544f; /* 'OTTO' */
432 // standard TrueType table tags
433 constexpr sal_uInt32 T_maxp = 0x6D617870;
434 constexpr sal_uInt32 T_glyf = 0x676C7966;
435 constexpr sal_uInt32 T_head = 0x68656164;
436 constexpr sal_uInt32 T_loca = 0x6C6F6361;
437 constexpr sal_uInt32 T_name = 0x6E616D65;
438 constexpr sal_uInt32 T_hhea = 0x68686561;
439 constexpr sal_uInt32 T_hmtx = 0x686D7478;
440 constexpr sal_uInt32 T_cmap = 0x636D6170;
441 constexpr sal_uInt32 T_vhea = 0x76686561;
442 constexpr sal_uInt32 T_vmtx = 0x766D7478;
443 constexpr sal_uInt32 T_OS2 = 0x4F532F32;
444 constexpr sal_uInt32 T_post = 0x706F7374;
445 constexpr sal_uInt32 T_cvt = 0x63767420;
446 constexpr sal_uInt32 T_prep = 0x70726570;
447 constexpr sal_uInt32 T_fpgm = 0x6670676D;
448 constexpr sal_uInt32 T_gsub = 0x47535542;
449 constexpr sal_uInt32 T_CFF = 0x43464620;
451 class AbstractTrueTypeFont;
452 class TrueTypeFont;
455 * @defgroup sft Sun Font Tools Exported Functions
459 * Get the number of fonts contained in a TrueType collection
460 * @param fname - file name
461 * @return number of fonts or zero, if file is not a TTC file.
462 * @ingroup sft
464 int CountTTCFonts(const char* fname);
467 * TrueTypeFont constructor.
468 * The font file has to be provided as a memory buffer and length
469 * @param pBuffer - memory buffer
470 * @param nLen - size of memory buffer
471 * @param facenum - logical font number within a TTC file. This value is ignored
472 * for TrueType fonts
473 * @param ttf - returns the opened TrueTypeFont
474 * @param xCharMap - optional parsed character map
475 * @return value of SFErrCodes enum
476 * @ingroup sft
478 SFErrCodes VCL_DLLPUBLIC OpenTTFontBuffer(const void* pBuffer, sal_uInt32 nLen, sal_uInt32 facenum,
479 TrueTypeFont** ttf, const FontCharMapRef xCharMap = nullptr);
480 #if !defined(_WIN32)
482 * TrueTypeFont constructor.
483 * Reads the font file and allocates the memory for the structure.
484 * on WIN32 the font has to be provided as a memory buffer and length
485 * @param fname - name of TrueType font file
486 * @param facenum - logical font number within a TTC file. This value is ignored
487 * for TrueType fonts
488 * @param ttf - returns the opened TrueTypeFont
489 * @param xCharMap - optional parsed character map
490 * @return value of SFErrCodes enum
491 * @ingroup sft
493 SFErrCodes VCL_DLLPUBLIC OpenTTFontFile(const char *fname, sal_uInt32 facenum, TrueTypeFont** ttf,
494 const FontCharMapRef xCharMap = nullptr);
495 #endif
497 bool VCL_DLLPUBLIC getTTCoverage(
498 std::optional<std::bitset<UnicodeCoverage::MAX_UC_ENUM>> & rUnicodeCoverage,
499 std::optional<std::bitset<CodePageCoverage::MAX_CP_ENUM>> & rCodePageCoverage,
500 const unsigned char* pTable, size_t nLength);
503 * TrueTypeFont destructor. Deallocates the memory.
504 * @ingroup sft
506 void VCL_DLLPUBLIC CloseTTFont(TrueTypeFont *);
509 * Extracts TrueType control points, and stores them in an allocated array pointed to
510 * by *pointArray. This function returns the number of extracted points.
512 * @param ttf pointer to the TrueTypeFont structure
513 * @param glyphID Glyph ID
514 * @param pointArray Return value - address of the pointer to the first element of the array
515 * of points allocated by the function
516 * @return Returns the number of points in *pointArray or -1 if glyphID is
517 * invalid.
518 * @ingroup sft
521 int GetTTGlyphPoints(AbstractTrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray);
524 * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
525 * GlyphData structure.
527 * @param ttf pointer to the TrueTypeFont structure
528 * @param glyphID Glyph ID
530 * @return pointer to an allocated GlyphData structure or NULL if
531 * glyphID is not present in the font
532 * @ingroup sft
535 GlyphData *GetTTRawGlyphData(AbstractTrueTypeFont *ttf, sal_uInt32 glyphID);
538 * For a specified glyph adds all component glyphs IDs to the list and
539 * return their number. If the glyph is a single glyph it has one component
540 * glyph (which is added to the list) and the function returns 1.
541 * For a composite glyphs it returns the number of component glyphs
542 * and adds all of them to the list.
544 * @param ttf pointer to the TrueTypeFont structure
545 * @param glyphID Glyph ID
546 * @param glyphlist list of glyphs
548 * @return number of component glyphs
549 * @ingroup sft
552 int GetTTGlyphComponents(AbstractTrueTypeFont *ttf, sal_uInt32 glyphID, std::vector< sal_uInt32 >& glyphlist);
555 * Extracts all Name Records from the font and stores them in an allocated
556 * array of NameRecord structs
558 * @param ttf pointer to the TrueTypeFont struct
559 * @param nr pointer to the array of NameRecord structs
561 * @return number of NameRecord structs
562 * @ingroup sft
565 int GetTTNameRecords(AbstractTrueTypeFont const *ttf, NameRecord **nr);
568 * Deallocates previously allocated array of NameRecords.
570 * @param nr array of NameRecord structs
571 * @param n number of elements in the array
573 * @ingroup sft
575 void DisposeNameRecords(NameRecord* nr, int n);
578 * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
579 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
580 * @param ttf pointer to the TrueTypeFont structure
581 * @param outf the resulting font is written to this stream
582 * @param fname font name for the new font. If it is NULL the PostScript name of the
583 * original font will be used
584 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
585 * @param encoding array of encoding values. encoding[i] specifies the position of the glyph
586 * glyphArray[i] in the encoding vector of the resulting Type3 font
587 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
588 * @param wmode writing mode for the output file: 0 - horizontal, 1 - vertical
589 * @return return the value of SFErrCodes enum
590 * @see SFErrCodes
591 * @ingroup sft
594 SFErrCodes CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, sal_uInt16 const *glyphArray, sal_uInt8 *encoding, int nGlyphs, int wmode);
597 * Generates a new TrueType font and dumps it to <b>outf</b> file.
598 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
599 * @param ttf pointer to the TrueTypeFont structure
600 * @param fname file name for the output TrueType font file
601 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
602 * element of this array has to be glyph 0 (default glyph)
603 * @param encoding array of encoding values. encoding[i] specifies character code for
604 * the glyphID glyphArray[i]. Character code 0 usually points to a default
605 * glyph (glyphID 0)
606 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
607 * @param flags or'ed TTCreationFlags
608 * @return return the value of SFErrCodes enum
609 * @see SFErrCodes
610 * @ingroup sft
613 VCL_DLLPUBLIC SFErrCodes CreateTTFromTTGlyphs(AbstractTrueTypeFont *ttf,
614 const char *fname,
615 sal_uInt16 const *glyphArray,
616 sal_uInt8 const *encoding,
617 int nGlyphs);
620 * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
621 * This function substitutes glyph 0 for all glyphIDs that are not found in the font.
622 * @param ttf pointer to the TrueTypeFont structure
623 * @param outf output stream for a resulting font
624 * @param psname PostScript name of the resulting font
625 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
626 * element of this array has to be glyph 0 (default glyph)
627 * @param encoding array of encoding values. encoding[i] specifies character code for
628 * the glyphID glyphArray[i]. Character code 0 usually points to a default
629 * glyph (glyphID 0)
630 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
631 * @return SFErrCodes::Ok - no errors
632 * SFErrCodes::GlyphNum - too many glyphs (> 255)
633 * SFErrCodes::TtFormat - corrupted TrueType fonts
635 * @see SFErrCodes
636 * @ingroup sft
639 SFErrCodes CreateT42FromTTGlyphs(TrueTypeFont *ttf,
640 FILE *outf,
641 const char *psname,
642 sal_uInt16 const *glyphArray,
643 sal_uInt8 *encoding,
644 int nGlyphs);
647 * Queries glyph metrics. Allocates an array of advance width/height values and returns it.
649 * @param ttf pointer to the TrueTypeFont structure
650 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
651 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
652 * @param vertical writing mode: false - horizontal, true - vertical
653 * @ingroup sft
656 VCL_DLLPUBLIC std::unique_ptr<sal_uInt16[]> GetTTSimpleGlyphMetrics(AbstractTrueTypeFont const *ttf, const sal_uInt16 *glyphArray, int nGlyphs, bool vertical);
659 * Returns global font information about the TrueType font.
660 * @see TTGlobalFontInfo
662 * @param ttf pointer to a TrueTypeFont structure
663 * @param info pointer to a TTGlobalFontInfo structure
664 * @ingroup sft
667 VCL_DLLPUBLIC void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info);
670 * Returns part of the head table info, normally collected by GetTTGlobalFontInfo.
672 * Just implemented separate, because this info not available via Qt API.
674 * @param ttf pointer to a AbstractTrueTypeFont structure
675 * @param xMin global glyph bounding box min X
676 * @param yMin global glyph bounding box min Y
677 * @param xMax global glyph bounding box max X
678 * @param yMax global glyph bounding box max Y
679 * @param macStyle encoded Mac style flags of the font
680 * @return true, if table data could be decoded
681 * @ingroup sft
683 VCL_DLLPUBLIC bool GetTTGlobalFontHeadInfo(const AbstractTrueTypeFont *ttf, int& xMin, int& yMin, int& xMax, int& yMax, sal_uInt16& macStyle);
686 * Returns fonts metrics.
687 * @see TTGlobalFontInfo
689 * @param hhea hhea table data
690 * @param os2 OS/2 table data
691 * @param info pointer to a TTGlobalFontInfo structure
692 * @ingroup sft
695 void GetTTFontMetrics(const uint8_t *pHhea, size_t nHhea,
696 const uint8_t *pOs2, size_t nOs2,
697 TTGlobalFontInfo *info);
699 /*- private definitions */
701 /* indexes into TrueTypeFont::tables[] and TrueTypeFont::tlens[] */
702 constexpr int O_maxp = 0;
703 constexpr int O_glyf = 1; /* 'glyf' */
704 constexpr int O_head = 2; /* 'head' */
705 constexpr int O_loca = 3; /* 'loca' */
706 constexpr int O_name = 4; /* 'name' */
707 constexpr int O_hhea = 5; /* 'hhea' */
708 constexpr int O_hmtx = 6; /* 'hmtx' */
709 constexpr int O_cmap = 7; /* 'cmap' */
710 constexpr int O_vhea = 8; /* 'vhea' */
711 constexpr int O_vmtx = 9; /* 'vmtx' */
712 constexpr int O_OS2 = 10; /* 'OS/2' */
713 constexpr int O_post = 11; /* 'post' */
714 constexpr int O_cvt = 12; /* 'cvt_' - only used in TT->TT generation */
715 constexpr int O_prep = 13; /* 'prep' - only used in TT->TT generation */
716 constexpr int O_fpgm = 14; /* 'fpgm' - only used in TT->TT generation */
717 constexpr int O_gsub = 15; /* 'GSUB' */
718 constexpr int O_CFF = 16; /* 'CFF' */
719 constexpr int NUM_TAGS = 17;
721 class VCL_DLLPUBLIC AbstractTrueTypeFont
723 char* m_pFileName;
724 sal_uInt32 m_nGlyphs;
725 sal_uInt32* m_pGlyphOffsets;
726 sal_uInt32 m_nHorzMetrics;
727 sal_uInt32 m_nVertMetrics; /* if not 0 => font has vertical metrics information */
728 sal_uInt32 m_nUnitsPerEm;
729 FontCharMapRef m_xCharMap;
731 protected:
732 SFErrCodes indexGlyphData();
734 public:
735 AbstractTrueTypeFont(const char* fileName = nullptr, const FontCharMapRef xCharMap = nullptr);
736 virtual ~AbstractTrueTypeFont();
738 const char* fileName() const { return m_pFileName; }
739 sal_uInt32 glyphCount() const { return m_nGlyphs; }
740 sal_uInt32 glyphOffset(sal_uInt32 glyphID) const { return m_pGlyphOffsets[glyphID]; }
741 sal_uInt32 horzMetricCount() const { return m_nHorzMetrics; }
742 sal_uInt32 vertMetricCount() const { return m_nVertMetrics; }
743 sal_uInt32 unitsPerEm() const { return m_nUnitsPerEm; }
744 FontCharMapRef GetCharMap() const { return m_xCharMap; }
746 virtual bool hasTable(sal_uInt32 ord) const = 0;
747 virtual const sal_uInt8* table(sal_uInt32 ord, sal_uInt32& size) const = 0;
750 class TrueTypeFont final : public AbstractTrueTypeFont
752 struct TTFontTable_
754 const sal_uInt8* pData = nullptr; /* pointer to a raw subtable in the SFNT file */
755 sal_uInt32 nSize = 0; /* table size */
758 std::array<struct TTFontTable_, NUM_TAGS> m_aTableList;
760 public:
761 sal_Int32 fsize;
762 sal_uInt8 *ptr;
764 char *psname;
765 char *family;
766 sal_Unicode *ufamily;
767 char *subfamily;
768 sal_Unicode *usubfamily;
770 sal_uInt32 ntables;
772 TrueTypeFont(const char* pFileName = nullptr, const FontCharMapRef xCharMap = nullptr);
773 ~TrueTypeFont() override;
775 SFErrCodes open(sal_uInt32 facenum);
777 bool hasTable(sal_uInt32 ord) const override { return m_aTableList[ord].pData != nullptr; }
778 inline const sal_uInt8* table(sal_uInt32 ord, sal_uInt32& size) const override;
781 const sal_uInt8* TrueTypeFont::table(sal_uInt32 ord, sal_uInt32& size) const
783 if (ord >= NUM_TAGS)
785 size = 0;
786 return nullptr;
789 auto& rTable = m_aTableList[ord];
790 size = rTable.nSize;
791 return rTable.pData;
794 } // namespace vcl
796 #endif // INCLUDED_VCL_INC_SFT_HXX
798 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */