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 * If NO_MAPPERS is defined, MapChar() and MapString() and consequently GetTTSimpleCharMetrics()
28 * don't get compiled in. This is done to avoid including a large chunk of code (TranslateXY() from
29 * xlat.c in the projects that don't require it.
31 * If NO_TYPE3 is defined CreateT3FromTTGlyphs() does not get compiled in.
32 * If NO_TYPE42 is defined Type42-related code is excluded
33 * If NO_TTCR is defined TrueType creation related code is excluded\
37 * Generated fonts contain an XUID entry in the form of:
41 * 103 - Sun's Adobe assigned XUID number. Contact person: Alexander Gelfenbain <gelf@eng.sun.com>
43 * T - font type. 0: Type 3, 1: Type 42
44 * C1 - CRC-32 of the entire source TrueType font
45 * N - number of glyphs in the subset
46 * C2 - CRC-32 of the array of glyph IDs used to generate the subset
47 * C3 - CRC-32 of the array of encoding numbers used to generate the subset
56 #include <sys/types.h>
61 #include <sal/types.h>
63 #include "vcl/dllapi.h"
67 #include "vcl/fontcapabilities.hxx"
73 typedef sal_Int16 F2Dot14
; /**< fixed: 2.14 */
74 typedef sal_Int32 F16Dot16
; /**< fixed: 16.16 */
82 /** Return value of OpenTTFont() and CreateT3FromTTGlyphs() */
84 SF_OK
, /**< no error */
85 SF_BADFILE
, /**< file not found */
86 SF_FILEIO
, /**< file I/O error */
87 SF_MEMORY
, /**< memory allocation error */
88 SF_GLYPHNUM
, /**< incorrect number of glyphs */
89 SF_BADARG
, /**< incorrect arguments */
90 SF_TTFORMAT
, /**< incorrect TrueType font format */
91 SF_TABLEFORMAT
, /**< incorrect format of a TrueType table */
92 SF_FONTNO
/**< incorrect logical font number of a TTC font */
95 #ifndef FW_THIN /* WIN32 compilation would conflict */
96 /** Value of the weight member of the TTGlobalFontInfo struct */
98 FW_THIN
= 100, /**< Thin */
99 FW_EXTRALIGHT
= 200, /**< Extra-light (Ultra-light) */
100 FW_LIGHT
= 300, /**< Light */
101 FW_NORMAL
= 400, /**< Normal (Regular) */
102 FW_MEDIUM
= 500, /**< Medium */
103 FW_SEMIBOLD
= 600, /**< Semi-bold (Demi-bold) */
104 FW_BOLD
= 700, /**< Bold */
105 FW_EXTRABOLD
= 800, /**< Extra-bold (Ultra-bold) */
106 FW_BLACK
= 900 /**< Black (Heavy) */
109 /** Value of the width member of the TTGlobalFontInfo struct */
111 FWIDTH_ULTRA_CONDENSED
= 1, /**< 50% of normal */
112 FWIDTH_EXTRA_CONDENSED
= 2, /**< 62.5% of normal */
113 FWIDTH_CONDENSED
= 3, /**< 75% of normal */
114 FWIDTH_SEMI_CONDENSED
= 4, /**< 87.5% of normal */
115 FWIDTH_NORMAL
= 5, /**< Medium, 100% */
116 FWIDTH_SEMI_EXPANDED
= 6, /**< 112.5% of normal */
117 FWIDTH_EXPANDED
= 7, /**< 125% of normal */
118 FWIDTH_EXTRA_EXPANDED
= 8, /**< 150% of normal */
119 FWIDTH_ULTRA_EXPANDED
= 9 /**< 200% of normal */
123 /** Type of the 'kern' table, stored in _TrueTypeFont::kerntype */
125 KT_NONE
= 0, /**< no kern table */
126 KT_APPLE_NEW
= 1, /**< new Apple kern table */
127 KT_MICROSOFT
= 2 /**< Microsoft table */
130 /* Composite glyph flags definition */
131 enum CompositeFlags
{
132 ARG_1_AND_2_ARE_WORDS
= 1,
133 ARGS_ARE_XY_VALUES
= 1<<1,
134 ROUND_XY_TO_GRID
= 1<<2,
135 WE_HAVE_A_SCALE
= 1<<3,
136 MORE_COMPONENTS
= 1<<5,
137 WE_HAVE_AN_X_AND_Y_SCALE
= 1<<6,
138 WE_HAVE_A_TWO_BY_TWO
= 1<<7,
139 WE_HAVE_INSTRUCTIONS
= 1<<8,
140 USE_MY_METRICS
= 1<<9,
141 OVERLAP_COMPOUND
= 1<<10
145 /** Flags for TrueType generation */
146 enum TTCreationFlags
{
147 TTCF_AutoName
= 1, /**< Automatically generate a compact 'name' table.
148 If this flag is not set, name table is generated
149 either from an array of NameRecord structs passed as
150 arguments or if the array is NULL, 'name' table
151 of the generated TrueType file will be a copy
152 of the name table of the original file.
153 If this flag is set the array of NameRecord structs
154 is ignored and a very compact 'name' table is automatically
157 TTCF_IncludeOS2
= 2 /** If this flag is set OS/2 table from the original font will be
158 copied to the subset */
165 /** Structure used by GetTTSimpleGlyphMetrics() and GetTTSimpleCharMetrics() functions */
167 sal_uInt16 adv
; /**< advance width or height */
168 sal_Int16 sb
; /**< left or top sidebearing */
169 } TTSimpleGlyphMetrics
;
173 /** Structure used by the TrueType Creator and GetRawGlyphData() */
176 sal_uInt32 glyphID
; /**< glyph ID */
177 sal_uInt16 nbytes
; /**< number of bytes in glyph data */
178 sal_uInt8
*ptr
; /**< pointer to glyph data */
179 sal_uInt16 aw
; /**< advance width */
180 sal_Int16 lsb
; /**< left sidebearing */
181 sal_uInt16 compflag
; /**< 0- if non-composite, 1- otherwise */
182 sal_uInt16 npoints
; /**< number of points */
183 sal_uInt16 ncontours
; /**< number of contours */
185 sal_uInt32 newID
; /**< used internally by the TTCR */
188 /** Structure used by the TrueType Creator and CreateTTFromTTGlyphs() */
190 sal_uInt16 platformID
; /**< Platform ID */
191 sal_uInt16 encodingID
; /**< Platform-specific encoding ID */
192 sal_uInt16 languageID
; /**< Language ID */
193 sal_uInt16 nameID
; /**< Name ID */
194 sal_uInt16 slen
; /**< String length in bytes */
195 sal_uInt8
*sptr
; /**< Pointer to string data (not zero-terminated!) */
200 /** Return value of GetTTGlobalFontInfo() */
203 char *family
; /**< family name */
204 sal_uInt16
*ufamily
; /**< family name UCS2 */
205 char *subfamily
; /**< subfamily name */
206 sal_uInt16
*usubfamily
; /**< subfamily name UCS2 */
207 char *psname
; /**< PostScript name */
208 sal_uInt16 macStyle
; /**< macstyle bits from 'HEAD' table */
209 int weight
; /**< value of WeightClass or 0 if can't be determined */
210 int width
; /**< value of WidthClass or 0 if can't be determined */
211 int pitch
; /**< 0: proportianal font, otherwise: monospaced */
212 int italicAngle
; /**< in counter-clockwise degrees * 65536 */
213 int xMin
; /**< global bounding box: xMin */
214 int yMin
; /**< global bounding box: yMin */
215 int xMax
; /**< global bounding box: xMax */
216 int yMax
; /**< global bounding box: yMax */
217 int ascender
; /**< typographic ascent. */
218 int descender
; /**< typographic descent. */
219 int linegap
; /**< typographic line gap.\ Negative values are treated as
220 zero in Win 3.1, System 6 and System 7. */
221 int vascent
; /**< typographic ascent for vertical writing mode */
222 int vdescent
; /**< typographic descent for vertical writing mode */
223 int typoAscender
; /**< OS/2 portable typographic ascender */
224 int typoDescender
; /**< OS/2 portable typographic descender */
225 int typoLineGap
; /**< OS/2 portable typographc line gap */
226 int winAscent
; /**< ascender metric for Windows */
227 int winDescent
; /**< descender metric for Windows */
228 int symbolEncoded
; /**< 1: MS symbol encoded 0: not symbol encoded */
229 int rangeFlag
; /**< if set to 1 Unicode Range flags are applicable */
230 sal_uInt32 ur1
; /**< bits 0 - 31 of Unicode Range flags */
231 sal_uInt32 ur2
; /**< bits 32 - 63 of Unicode Range flags */
232 sal_uInt32 ur3
; /**< bits 64 - 95 of Unicode Range flags */
233 sal_uInt32 ur4
; /**< bits 96 - 127 of Unicode Range flags */
234 sal_uInt8 panose
[10]; /**< PANOSE classification number */
235 sal_uInt32 typeFlags
; /**< type flags (copyright bits + PS-OpenType flag) */
238 #define TYPEFLAG_INVALID 0x8000000
239 #define TYPEFLAG_COPYRIGHT_MASK 0x000000E
240 #define TYPEFLAG_PS_OPENTYPE 0x0010000
242 /** Structure used by KernGlyphs() */
244 int x
; /**< positive: right, negative: left */
245 int y
; /**< positive: up, negative: down */
249 /** ControlPoint structure used by GetTTGlyphPoints() */
251 sal_uInt32 flags
; /**< 00000000 00000000 e0000000 bbbbbbbb */
252 /**< b - byte flags from the glyf array */
253 /**< e == 0 - regular point */
254 /**< e == 1 - end contour */
255 sal_Int16 x
; /**< X coordinate in EmSquare units */
256 sal_Int16 y
; /**< Y coordinate in EmSquare units */
259 typedef struct _TrueTypeFont TrueTypeFont
;
262 * @defgroup sft Sun Font Tools Exported Functions
267 * Get the number of fonts contained in a TrueType collection
268 * @param fname - file name
269 * @return number of fonts or zero, if file is not a TTC file.
272 int CountTTCFonts(const char* fname
);
276 * TrueTypeFont constructor.
277 * The font file has to be provided as a memory buffer and length
278 * @param facenum - logical font number within a TTC file. This value is ignored
280 * @return value of SFErrCodes enum
283 int VCL_DLLPUBLIC
OpenTTFontBuffer(const void* pBuffer
, sal_uInt32 nLen
, sal_uInt32 facenum
, TrueTypeFont
** ttf
); /*FOLD01*/
286 * TrueTypeFont constructor.
287 * Reads the font file and allocates the memory for the structure.
288 * on WIN32 the font has to be provided as a memory buffer and length
289 * @param facenum - logical font number within a TTC file. This value is ignored
291 * @return value of SFErrCodes enum
294 int VCL_DLLPUBLIC
OpenTTFontFile(const char *fname
, sal_uInt32 facenum
, TrueTypeFont
** ttf
);
297 void getTTScripts(std::vector
< sal_uInt32
> &rScriptTags
, const unsigned char* pTable
, size_t nLength
);
299 boost::dynamic_bitset
<sal_uInt32
> &rUnicodeCoverage
,
300 boost::dynamic_bitset
<sal_uInt32
> &rCodePageCoverage
,
301 const unsigned char* pTable
, size_t nLength
);
304 * TrueTypeFont destructor. Deallocates the memory.
307 void VCL_DLLPUBLIC
CloseTTFont(TrueTypeFont
*);
310 * Extracts TrueType control points, and stores them in an allocated array pointed to
311 * by *pointArray. This function returns the number of extracted points.
313 * @param ttf pointer to the TrueTypeFont structure
314 * @param glyphID Glyph ID
315 * @param pointArray Return value - address of the pointer to the first element of the array
316 * of points allocated by the function
317 * @return Returns the number of points in *pointArray or -1 if glyphID is
322 int GetTTGlyphPoints(TrueTypeFont
*ttf
, sal_uInt32 glyphID
, ControlPoint
**pointArray
);
325 * Extracts raw glyph data from the 'glyf' table and returns it in an allocated
326 * GlyphData structure.
328 * @param ttf pointer to the TrueTypeFont structure
329 * @param glyphID Glyph ID
331 * @return pointer to an allocated GlyphData structure or NULL if
332 * glyphID is not present in the font
336 GlyphData
*GetTTRawGlyphData(TrueTypeFont
*ttf
, sal_uInt32 glyphID
);
339 * For a specified glyph adds all component glyphs IDs to the list and
340 * return their number. If the glyph is a single glyph it has one component
341 * glyph (which is added to the list) and the function returns 1.
342 * For a composite glyphs it returns the number of component glyphs
343 * and adds all of them to the list.
345 * @param ttf pointer to the TrueTypeFont structure
346 * @param glyphID Glyph ID
347 * @param glyphlist list of glyphs
349 * @return number of component glyphs
353 int GetTTGlyphComponents(TrueTypeFont
*ttf
, sal_uInt32 glyphID
, std::vector
< sal_uInt32
>& glyphlist
);
356 * Extracts all Name Records from the font and stores them in an allocated
357 * array of NameRecord structs
359 * @param ttf pointer to the TrueTypeFont struct
360 * @param nr pointer to the array of NameRecord structs
362 * @return number of NameRecord structs
366 int GetTTNameRecords(TrueTypeFont
*ttf
, NameRecord
**nr
);
369 * Deallocates previously allocated array of NameRecords.
371 * @param nr array of NameRecord structs
372 * @param n number of elements in the array
376 void DisposeNameRecords(NameRecord
* nr
, int n
);
381 * Generates a new PostScript Type 3 font and dumps it to <b>outf</b> file.
382 * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
383 * @param ttf pointer to the TrueTypeFont structure
384 * @param outf the resulting font is written to this stream
385 * @param fname font name for the new font. If it is NULL the PostScript name of the
386 * original font will be used
387 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
388 * @param encoding array of encoding values. encoding[i] specifies the position of the glyph
389 * glyphArray[i] in the encoding vector of the resulting Type3 font
390 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
391 * @param wmode writing mode for the output file: 0 - horizontal, 1 - vertical
392 * @return return the value of SFErrCodes enum
397 int CreateT3FromTTGlyphs(TrueTypeFont
*ttf
, FILE *outf
, const char *fname
, sal_uInt16
*glyphArray
, sal_uInt8
*encoding
, int nGlyphs
, int wmode
);
402 * Generates a new TrueType font and dumps it to <b>outf</b> file.
403 * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
404 * @param ttf pointer to the TrueTypeFont structure
405 * @param fname file name for the output TrueType font file
406 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
407 * element of this array has to be glyph 0 (default glyph)
408 * @param encoding array of encoding values. encoding[i] specifies character code for
409 * the glyphID glyphArray[i]. Character code 0 usually points to a default
411 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
412 * @param nNameRecs number of NameRecords for the font, if 0 the name table from the
413 * original font will be used
414 * @param nr array of NameRecords
415 * @param flags or'ed TTCreationFlags
416 * @return return the value of SFErrCodes enum
421 int CreateTTFromTTGlyphs(TrueTypeFont
*ttf
,
423 sal_uInt16
*glyphArray
,
433 * Generates a new PostScript Type42 font and dumps it to <b>outf</b> file.
434 * This functions subsititues glyph 0 for all glyphIDs that are not found in the font.
435 * @param ttf pointer to the TrueTypeFont structure
436 * @param outf output stream for a resulting font
437 * @param psname PostScript name of the resulting font
438 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf. The first
439 * element of this array has to be glyph 0 (default glyph)
440 * @param encoding array of encoding values. encoding[i] specifies character code for
441 * the glyphID glyphArray[i]. Character code 0 usually points to a default
443 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
444 * @return SF_OK - no errors
445 * SF_GLYPHNUM - too many glyphs (> 255)
446 * SF_TTFORMAT - corrupted TrueType fonts
452 int CreateT42FromTTGlyphs(TrueTypeFont
*ttf
,
455 sal_uInt16
*glyphArray
,
462 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
464 * @param ttf pointer to the TrueTypeFont structure
465 * @param glyphArray pointer to an array of glyphs that are to be extracted from ttf
466 * @param nGlyphs number of glyph IDs in glyphArray and encoding values in encoding
467 * @param mode writing mode: 0 - horizontal, 1 - vertical
471 TTSimpleGlyphMetrics
*GetTTSimpleGlyphMetrics(TrueTypeFont
*ttf
, sal_uInt16
*glyphArray
, int nGlyphs
, int mode
);
475 * Queries glyph metrics. Allocates an array of TTSimpleGlyphMetrics structs and returns it.
476 * This function behaves just like GetTTSimpleGlyphMetrics() but it takes a range of Unicode
477 * characters instead of an array of glyphs.
479 * @param ttf pointer to the TrueTypeFont structure
480 * @param firstChar Unicode value of the first character in the range
481 * @param nChars number of Unicode characters in the range
482 * @param mode writing mode: 0 - horizontal, 1 - vertical
484 * @see GetTTSimpleGlyphMetrics
488 TTSimpleGlyphMetrics
*GetTTSimpleCharMetrics(TrueTypeFont
*ttf
, sal_uInt16 firstChar
, int nChars
, int mode
);
491 * Maps a Unicode (UCS-2) string to a glyph array. Returns the number of glyphs in the array,
492 * which for TrueType fonts is always the same as the number of input characters.
494 * @param ttf pointer to the TrueTypeFont structure
495 * @param str pointer to a UCS-2 string
496 * @param nchars number of characters in <b>str</b>
497 * @param glyphArray pointer to the glyph array where glyph IDs are to be recorded.
499 * @return MapString() returns -1 if the TrueType font has no usable 'cmap' tables.
500 * Otherwise it returns the number of characters processed: <b>nChars</b>
502 * glyphIDs of TrueType fonts are 2 byte positive numbers. glyphID of 0 denotes a missing
503 * glyph and traditionally defaults to an empty square.
504 * glyphArray should be at least sizeof(sal_uInt16) * nchars bytes long. If glyphArray is NULL
505 * MapString() replaces the UCS-2 characters in str with glyphIDs.
508 int VCL_DLLPUBLIC
MapString(TrueTypeFont
*ttf
, sal_uInt16
*str
, int nchars
, sal_uInt16
*glyphArray
, int bvertical
);
511 * Maps a Unicode (UCS-2) character to a glyph ID and returns it. Missing glyph has
512 * a glyphID of 0 so this function can be used to test if a character is encoded in the font.
514 * @param ttf pointer to the TrueTypeFont structure
515 * @param ch Unicode (UCS-2) character
516 * @return glyph ID, if the character is missing in the font, the return value is 0.
519 sal_uInt16
MapChar(TrueTypeFont
*ttf
, sal_uInt16 ch
, int bvertical
);
522 * Returns 0 when the font does not substitute vertical glyphs
524 * @param ttf pointer to the TrueTypeFont structure
526 int DoesVerticalSubstitution( TrueTypeFont
*ttf
, int bvertical
);
531 * Returns global font information about the TrueType font.
532 * @see TTGlobalFontInfo
534 * @param ttf pointer to a TrueTypeFont structure
535 * @param info pointer to a TTGlobalFontInfo structure
539 void GetTTGlobalFontInfo(TrueTypeFont
*ttf
, TTGlobalFontInfo
*info
);
542 * Returns nonzero if font is a symbol encoded font
544 int CheckSymbolEncoding(TrueTypeFont
* ttf
);
547 * returns the number of glyphs in a font
549 int GetTTGlyphCount( TrueTypeFont
* ttf
);
552 * provide access to the raw data of a SFNT-container's subtable
554 bool GetSfntTable( TrueTypeFont
* ttf
, int nSubtableIndex
,
555 const sal_uInt8
** ppRawBytes
, int* pRawLength
);
557 /*- private definitions */ /*FOLD00*/
559 struct _TrueTypeFont
{
570 sal_uInt16
*usubfamily
;
573 sal_uInt32
*goffsets
;
575 sal_uInt32 unitsPerEm
;
576 sal_uInt32 numberOfHMetrics
;
577 sal_uInt32 numOfLongVerMetrics
; /* if this number is not 0, font has vertical metrics information */
578 const sal_uInt8
* cmap
;
580 sal_uInt32 (*mapper
)(const sal_uInt8
*, sal_uInt32
); /* character to glyphID translation function */
581 const sal_uInt8
**tables
; /* array of pointers to raw subtables in SFNT file */
582 sal_uInt32
*tlens
; /* array of table lengths */
583 int kerntype
; /* Defined in the KernType enum */
584 sal_uInt32 nkern
; /* number of kern subtables */
585 const sal_uInt8
** kerntables
; /* array of pointers to kern subtables */
586 void *pGSubstitution
; /* info provided by GSUB for UseGSUB() */
589 /* indexes into _TrueTypeFont::tables[] and _TrueTypeFont::tlens[] */
590 #define O_maxp 0 /* 'maxp' */
591 #define O_glyf 1 /* 'glyf' */
592 #define O_head 2 /* 'head' */
593 #define O_loca 3 /* 'loca' */
594 #define O_name 4 /* 'name' */
595 #define O_hhea 5 /* 'hhea' */
596 #define O_hmtx 6 /* 'hmtx' */
597 #define O_cmap 7 /* 'cmap' */
598 #define O_vhea 8 /* 'vhea' */
599 #define O_vmtx 9 /* 'vmtx' */
600 #define O_OS2 10 /* 'OS/2' */
601 #define O_post 11 /* 'post' */
602 #define O_kern 12 /* 'kern' */
603 #define O_cvt 13 /* 'cvt_' - only used in TT->TT generation */
604 #define O_prep 14 /* 'prep' - only used in TT->TT generation */
605 #define O_fpgm 15 /* 'fpgm' - only used in TT->TT generation */
606 #define O_gsub 16 /* 'GSUB' */
607 #define O_CFF 17 /* 'CFF' */
612 #endif /* __SUBFONT_H */
614 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */