/src/libreoffice/vcl/source/fontsubset/ttcr.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | /** |
21 | | * @file ttcr.hxx |
22 | | * @brief TrueType font creator |
23 | | */ |
24 | | |
25 | | #pragma once |
26 | | |
27 | | #include <sft.hxx> |
28 | | #include <vector> |
29 | | |
30 | | namespace vcl |
31 | | { |
32 | | class TrueTypeTable; |
33 | | struct tdata_loca; |
34 | | struct table_cmap; |
35 | | struct TableEntry; |
36 | | |
37 | | |
38 | | /* TrueType data types */ |
39 | | typedef struct { |
40 | | sal_uInt16 aw; |
41 | | sal_Int16 lsb; |
42 | | } longHorMetrics; |
43 | | |
44 | | /** Error codes for most functions */ |
45 | | enum TTCRErrCodes { |
46 | | TTCR_OK, /**< no error */ |
47 | | TTCR_ZEROGLYPHS, /**< At least one glyph should be defined */ |
48 | | TTCR_UNKNOWN, /**< Unknown TrueType table */ |
49 | | TTCR_NONAMES, /**< 'name' table does not contain any names */ |
50 | | TTCR_NAMETOOLONG, /**< 'name' table is too long (string data > 64K) */ |
51 | | TTCR_POSTFORMAT /**< unsupported format of a 'post' table */ |
52 | | }; |
53 | | |
54 | | class TrueTypeCreator { |
55 | | public: |
56 | | /** |
57 | | * TrueTypeCreator constructor. |
58 | | * Allocates all internal structures. |
59 | | */ |
60 | | TrueTypeCreator(sal_uInt32 tag); |
61 | | ~TrueTypeCreator(); |
62 | | /** |
63 | | * Adds a TrueType table to the TrueType creator. |
64 | | */ |
65 | | void AddTable(std::unique_ptr<TrueTypeTable> table); |
66 | | /** |
67 | | * Removes a TrueType table from the TrueType creator if it is stored there. |
68 | | * It also calls a TrueTypeTable destructor. |
69 | | * Note: all generic tables (with tag 0) will be removed if this function is |
70 | | * called with the second argument of 0. |
71 | | * @return value of SFErrCodes type |
72 | | */ |
73 | | void RemoveTable(sal_uInt32 tag); |
74 | | /** |
75 | | * Writes a TrueType font generated by the TrueTypeCreator to a segment of |
76 | | * memory that this method allocates. When it is not needed anymore the caller |
77 | | * is supposed to call free() on it. |
78 | | * @return value of SFErrCodes type |
79 | | */ |
80 | | SFErrCodes StreamToMemory(std::vector<sal_uInt8>& rOutBuffer); |
81 | | |
82 | | private: |
83 | | TrueTypeTable *FindTable(sal_uInt32 tag); |
84 | | void ProcessTables(); |
85 | | |
86 | | sal_uInt32 m_tag; /**< TrueType file tag */ |
87 | | std::vector<std::unique_ptr<TrueTypeTable>> m_tables; /**< List of table tags and pointers */ |
88 | | }; |
89 | | |
90 | | /* A generic base class for all TrueType tables */ |
91 | | class TrueTypeTable { |
92 | | protected: |
93 | 83.6k | TrueTypeTable(sal_uInt32 tag_) : m_tag(tag_) {} |
94 | | |
95 | | public: |
96 | | virtual ~TrueTypeTable(); |
97 | | |
98 | | /** |
99 | | * This function converts the data of a TrueType table to a raw array of bytes. |
100 | | * It may allocates the memory for it and returns the size of the raw data in bytes. |
101 | | * If memory is allocated it does not need to be freed by the caller of this function, |
102 | | * since the pointer to it is stored in the TrueTypeTable and it is freed by the destructor |
103 | | * @return TTCRErrCode |
104 | | * |
105 | | */ |
106 | | virtual int GetRawData(TableEntry*) = 0; |
107 | | |
108 | | sal_uInt32 m_tag = 0; /* table tag */ |
109 | | std::unique_ptr<sal_uInt8[]> m_rawdata; /* raw data allocated by GetRawData_*() */ |
110 | | }; |
111 | | |
112 | | class TrueTypeTableGeneric : public TrueTypeTable |
113 | | { |
114 | | public: |
115 | | /** |
116 | | * |
117 | | * Creates a new raw TrueType table. The difference between this constructor and |
118 | | * TrueTypeTableNew_tag constructors is that the latter create structured tables |
119 | | * while this constructor just copies memory pointed to by ptr to its buffer |
120 | | * and stores its length. This constructor is suitable for data that is not |
121 | | * supposed to be processed in any way, just written to the resulting TTF file. |
122 | | */ |
123 | | TrueTypeTableGeneric(sal_uInt32 tag, |
124 | | sal_uInt32 nbytes, |
125 | | const sal_uInt8* ptr); |
126 | | TrueTypeTableGeneric(sal_uInt32 tag, |
127 | | sal_uInt32 nbytes, |
128 | | std::unique_ptr<sal_uInt8[]> ptr); |
129 | | virtual ~TrueTypeTableGeneric() override; |
130 | | virtual int GetRawData(TableEntry*) override; |
131 | | private: |
132 | | sal_uInt32 m_nbytes; |
133 | | std::unique_ptr<sal_uInt8[]> m_ptr; |
134 | | }; |
135 | | |
136 | | /** |
137 | | * Creates a new 'head' table for a TrueType font. |
138 | | * Allocates memory for it. Since a lot of values in the 'head' table depend on the |
139 | | * rest of the tables in the TrueType font this table should be the last one added |
140 | | * to the font. |
141 | | */ |
142 | | class TrueTypeTableHead : public TrueTypeTable |
143 | | { |
144 | | public: |
145 | | TrueTypeTableHead(sal_uInt32 fontRevision, |
146 | | sal_uInt16 flags, |
147 | | sal_uInt16 unitsPerEm, |
148 | | const sal_uInt8 *created, |
149 | | sal_uInt16 macStyle, |
150 | | sal_uInt16 lowestRecPPEM, |
151 | | sal_Int16 fontDirectionHint); |
152 | | virtual ~TrueTypeTableHead() override; |
153 | | virtual int GetRawData(TableEntry*) override; |
154 | | |
155 | | std::unique_ptr<sal_uInt8[]> m_head; |
156 | | }; |
157 | | |
158 | | /** |
159 | | * Creates a new 'hhea' table for a TrueType font. |
160 | | * Allocates memory for it and stores it in the hhea pointer. |
161 | | */ |
162 | | class TrueTypeTableHhea : public TrueTypeTable |
163 | | { |
164 | | public: |
165 | | TrueTypeTableHhea(sal_Int16 ascender, |
166 | | sal_Int16 descender, |
167 | | sal_Int16 linegap, |
168 | | sal_Int16 caretSlopeRise, |
169 | | sal_Int16 caretSlopeRun); |
170 | | virtual ~TrueTypeTableHhea() override; |
171 | | virtual int GetRawData(TableEntry*) override; |
172 | | |
173 | | std::unique_ptr<sal_uInt8[]> m_hhea; |
174 | | }; |
175 | | |
176 | | /** |
177 | | * Creates a new empty 'loca' table for a TrueType font. |
178 | | * |
179 | | * INTERNAL: gets called only from ProcessTables(); |
180 | | */ |
181 | | class TrueTypeTableLoca : public TrueTypeTable |
182 | | { |
183 | | public: |
184 | | TrueTypeTableLoca(); |
185 | | virtual ~TrueTypeTableLoca() override; |
186 | | virtual int GetRawData(TableEntry*) override; |
187 | | |
188 | | std::unique_ptr<tdata_loca> m_loca; |
189 | | }; |
190 | | |
191 | | /** |
192 | | * Creates a new 'maxp' table based on an existing maxp table. |
193 | | * If maxp is 0, a new empty maxp table is created |
194 | | * size specifies the size of existing maxp table for |
195 | | * error-checking purposes |
196 | | */ |
197 | | class TrueTypeTableMaxp : public TrueTypeTable |
198 | | { |
199 | | public: |
200 | | TrueTypeTableMaxp(const sal_uInt8* maxp, int size); |
201 | | virtual ~TrueTypeTableMaxp() override; |
202 | | virtual int GetRawData(TableEntry*) override; |
203 | | |
204 | | std::unique_ptr<sal_uInt8[]> m_maxp; |
205 | | }; |
206 | | |
207 | | /** |
208 | | * Creates a new empty 'glyf' table. |
209 | | */ |
210 | | class TrueTypeTableGlyf : public TrueTypeTable |
211 | | { |
212 | | public: |
213 | | TrueTypeTableGlyf(); |
214 | | virtual ~TrueTypeTableGlyf() override; |
215 | | virtual int GetRawData(TableEntry*) override; |
216 | | |
217 | | /** |
218 | | * Add a glyph to a glyf table. |
219 | | * |
220 | | * @return glyphID of the glyph in the new font |
221 | | * |
222 | | * NOTE: This function does not duplicate GlyphData, so memory will be |
223 | | * deallocated in the table destructor |
224 | | */ |
225 | | sal_uInt32 glyfAdd(std::unique_ptr<GlyphData> glyphdata, AbstractTrueTypeFont *fnt); |
226 | | |
227 | | std::vector<std::unique_ptr<GlyphData>> m_list; |
228 | | }; |
229 | | |
230 | | /** |
231 | | * Creates a new empty 'cmap' table. |
232 | | */ |
233 | | class TrueTypeTableCmap : public TrueTypeTable |
234 | | { |
235 | | public: |
236 | | TrueTypeTableCmap(); |
237 | | virtual ~TrueTypeTableCmap() override; |
238 | | virtual int GetRawData(TableEntry*) override; |
239 | | |
240 | | /** |
241 | | * Add a character/glyph pair to a cmap table |
242 | | */ |
243 | | void cmapAdd(sal_uInt32 id, sal_uInt32 c, sal_uInt32 g); |
244 | | |
245 | | private: |
246 | | std::unique_ptr<table_cmap> m_cmap; |
247 | | }; |
248 | | |
249 | | /** |
250 | | * Creates a new 'name' table. If n != 0 the table gets populated by |
251 | | * the Name Records stored in the nr array. This function allocates |
252 | | * memory for its own copy of NameRecords, so nr array has to |
253 | | * be explicitly deallocated when it is not needed. |
254 | | */ |
255 | | class TrueTypeTableName : public TrueTypeTable |
256 | | { |
257 | | public: |
258 | | TrueTypeTableName(std::vector<NameRecord> nr); |
259 | | virtual ~TrueTypeTableName() override; |
260 | | virtual int GetRawData(TableEntry*) override; |
261 | | private: |
262 | | std::vector<NameRecord> m_list; |
263 | | }; |
264 | | |
265 | | /** |
266 | | * Creates a new 'post' table of one of the supported formats |
267 | | */ |
268 | | class TrueTypeTablePost : public TrueTypeTable |
269 | | { |
270 | | public: |
271 | | TrueTypeTablePost(sal_Int32 format, |
272 | | sal_Int32 italicAngle, |
273 | | sal_Int16 underlinePosition, |
274 | | sal_Int16 underlineThickness, |
275 | | sal_uInt32 isFixedPitch); |
276 | | virtual ~TrueTypeTablePost() override; |
277 | | virtual int GetRawData(TableEntry*) override; |
278 | | private: |
279 | | sal_uInt32 m_format; |
280 | | sal_uInt32 m_italicAngle; |
281 | | sal_Int16 m_underlinePosition; |
282 | | sal_Int16 m_underlineThickness; |
283 | | sal_uInt32 m_isFixedPitch; |
284 | | }; |
285 | | |
286 | | } // namespace |
287 | | |
288 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |