xref: /aoo41x/main/vcl/source/fontsubset/ttcr.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 /**
29  *
30  * @file ttcr.h
31  * @brief TrueType font creator
32  * @author Alexander Gelfenbain
33  */
34 
35 #ifndef __TTCR_H
36 #define __TTCR_H
37 
38 #include "sft.hxx"
39 
40 namespace vcl
41 {
42     typedef struct _TrueTypeCreator TrueTypeCreator;
43 
44 /* TrueType data types */
45     typedef struct {
46         sal_uInt16 aw;
47         sal_Int16  lsb;
48     } longHorMetrics;
49 
50 /* A generic base class for all TrueType tables */
51     struct TrueTypeTable {
52         sal_uInt32  tag;                         /* table tag                                                */
53         sal_uInt8   *rawdata;                    /* raw data allocated by GetRawData_*()                     */
54         void        *data;                       /* table specific data                                      */
55     };
56 
57 /** Error codes for most functions */
58     enum TTCRErrCodes {
59         TTCR_OK = 0,                        /**< no error                                               */
60         TTCR_ZEROGLYPHS = 1,                /**< At least one glyph should be defined                   */
61         TTCR_UNKNOWN = 2,                   /**< Unknown TrueType table                                 */
62         TTCR_GLYPHSEQ = 3,                  /**< Glyph IDs are not sequential in the glyf table         */
63         TTCR_NONAMES = 4,                   /**< 'name' table does not contain any names                */
64         TTCR_NAMETOOLONG = 5,               /**< 'name' table is too long (string data > 64K)           */
65         TTCR_POSTFORMAT = 6                 /**< unsupported format of a 'post' table                   */
66     };
67 
68 /* ============================================================================
69  *
70  * TrueTypeCreator methods
71  *
72  * ============================================================================ */
73 
74 /**
75  * TrueTypeCreator constructor.
76  * Allocates all internal structures.
77  */
78     void TrueTypeCreatorNewEmpty(sal_uInt32 tag, TrueTypeCreator **_this);
79 
80 /**
81  * Adds a TrueType table to the TrueType creator.
82  * SF_TABLEFORMAT value.
83  * @return value of SFErrCodes type
84  */
85     int AddTable(TrueTypeCreator *_this, TrueTypeTable *table);
86 
87 /**
88  * Removes a TrueType table from the TrueType creator if it is stored there.
89  * It also calls a TrueTypeTable destructor.
90  * Note: all generic tables (with tag 0) will be removed if this function is
91  * called with the second argument of 0.
92  * @return value of SFErrCodes type
93  */
94     void RemoveTable(TrueTypeCreator *_this, sal_uInt32 tag);
95 
96 
97 
98 /**
99  * Writes a TrueType font generated by the TrueTypeCreator to a segment of
100  * memory that this method allocates. When it is not needed anymore the caller
101  * is supposed to call free() on it.
102  * @return value of SFErrCodes type
103  */
104     int StreamToMemory(TrueTypeCreator *_this, sal_uInt8 **ptr, sal_uInt32 *length);
105 
106 /**
107  * Writes a TrueType font  generated by the TrueTypeCreator to a file
108  * @return value of SFErrCodes type
109  */
110     int StreamToFile(TrueTypeCreator *_this, const char* fname);
111 
112 
113 /* ============================================================================
114  *
115  * TrueTypeTable methods
116  *
117  * ============================================================================ */
118 
119 
120 /**
121  * This function converts the data of a TrueType table to a raw array of bytes.
122  * It may allocates the memory for it and returns the size of the raw data in bytes.
123  * If memory is allocated it does not need to be freed by the caller of this function,
124  * since the pointer to it is stored in the TrueTypeTable and it is freed by the destructor
125  * @return TTCRErrCode
126  *
127  */
128 
129     int GetRawData(TrueTypeTable *, sal_uInt8 **ptr, sal_uInt32 *len, sal_uInt32 *tag);
130 
131 /**
132  *
133  * Creates a new raw TrueType table. The difference between this constructor and
134  * TrueTypeTableNew_tag constructors is that the latter create structured tables
135  * while this constructor just copies memory pointed to by ptr to its buffer
136  * and stores its length. This constructor is suitable for data that is not
137  * supposed to be processed in any way, just written to the resulting TTF file.
138  */
139     TrueTypeTable *TrueTypeTableNew(sal_uInt32 tag,
140                                     sal_uInt32 nbytes,
141                                     const sal_uInt8* ptr);
142 
143 /**
144  * Creates a new 'head' table for a TrueType font.
145  * Allocates memory for it. Since a lot of values in the 'head' table depend on the
146  * rest of the tables in the TrueType font this table should be the last one added
147  * to the font.
148  */
149     TrueTypeTable *TrueTypeTableNew_head(sal_uInt32 fontRevision,
150                                          sal_uInt16 flags,
151                                          sal_uInt16 unitsPerEm,
152                                          const sal_uInt8  *created,
153                                          sal_uInt16 macStyle,
154                                          sal_uInt16 lowestRecPPEM,
155                                          sal_Int16  fontDirectionHint);
156 
157 /**
158  * Creates a new 'hhea' table for a TrueType font.
159  * Allocates memory for it and stores it in the hhea pointer.
160  */
161     TrueTypeTable *TrueTypeTableNew_hhea(sal_Int16  ascender,
162                                          sal_Int16  descender,
163                                          sal_Int16  linegap,
164                                          sal_Int16  caretSlopeRise,
165                                          sal_Int16  caretSlopeRun);
166 
167 /**
168  * Creates a new empty 'loca' table for a TrueType font.
169  *
170  * INTERNAL: gets called only from ProcessTables();
171  */
172     TrueTypeTable *TrueTypeTableNew_loca(void);
173 
174 /**
175  * Creates a new 'maxp' table based on an existing maxp table.
176  * If maxp is 0, a new empty maxp table is created
177  * size specifies the size of existing maxp table for
178  * error-checking purposes
179  */
180     TrueTypeTable *TrueTypeTableNew_maxp( const sal_uInt8* maxp, int size);
181 
182 /**
183  * Creates a new empty 'glyf' table.
184  */
185     TrueTypeTable *TrueTypeTableNew_glyf(void);
186 
187 /**
188  * Creates a new empty 'cmap' table.
189  */
190     TrueTypeTable *TrueTypeTableNew_cmap(void);
191 
192 /**
193  * Creates a new 'name' table. If n != 0 the table gets populated by
194  * the Name Records stored in the nr array. This function allocates
195  * memory for its own copy of NameRecords, so nr array has to
196  * be explicitly deallocated when it is not needed.
197  */
198     TrueTypeTable *TrueTypeTableNew_name(int n, NameRecord *nr);
199 
200 /**
201  * Creates a new 'post' table of one of the supported formats
202  */
203     TrueTypeTable *TrueTypeTableNew_post(sal_uInt32 format,
204                                          sal_uInt32 italicAngle,
205                                          sal_Int16 underlinePosition,
206                                          sal_Int16 underlineThickness,
207                                          sal_uInt32 isFixedPitch);
208 
209 
210 /*------------------------------------------------------------------------------
211  *
212  *  Table manipulation functions
213  *
214  *------------------------------------------------------------------------------*/
215 
216 
217 /**
218  * Add a character/glyph pair to a cmap table
219  */
220     void cmapAdd(TrueTypeTable *, sal_uInt32 id, sal_uInt32 c, sal_uInt32 g);
221 
222 /**
223  * Add a glyph to a glyf table.
224  *
225  * @return glyphID of the glyph in the new font
226  *
227  * NOTE: This function does not duplicate GlyphData, so memory will be
228  * deallocated in the table destructor
229  */
230     sal_uInt32 glyfAdd(TrueTypeTable *, GlyphData *glyphdata, TrueTypeFont *fnt);
231 
232 /**
233  * Query the number of glyphs currently stored in the 'glyf' table
234  *
235  */
236     sal_uInt32 glyfCount(const TrueTypeTable *);
237 
238 /**
239  * Add a Name Record to a name table.
240  * NOTE: This function duplicates NameRecord, so the argument
241  * has to be deallocated by the caller (unlike glyfAdd)
242  */
243     void nameAdd(TrueTypeTable *, NameRecord *nr);
244 
245 } // namespace
246 
247 
248 extern "C"
249 {
250 /**
251  * Destructor for the TrueTypeTable object.
252  */
253  void TrueTypeTableDispose(vcl::TrueTypeTable *);
254 
255 /**
256  * TrueTypeCreator destructor. It calls destructors for all TrueTypeTables added to it.
257  */
258  void TrueTypeCreatorDispose(vcl::TrueTypeCreator *_this);
259 }
260 
261 #endif /* __TTCR_H */
262