blob: b0020cdf5080dbdd5afe850b77d283fc5a5dd39b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 */
25
26
27/*
28 *
29 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
30 *
31 */
32
33#ifndef __OPENTYPELAYOUTENGINE_H
34#define __OPENTYPELAYOUTENGINE_H
35
36#include "LETypes.h"
37#include "LEGlyphFilter.h"
38#include "LEFontInstance.h"
39#include "LayoutEngine.h"
40
41#include "GlyphSubstitutionTables.h"
42#include "GlyphDefinitionTables.h"
43#include "GlyphPositioningTables.h"
44
45/**
46 * OpenTypeLayoutEngine implements complex text layout for OpenType fonts - that is
47 * fonts which have GSUB and GPOS tables associated with them. In order to do this,
48 * the glyph processsing step described for LayoutEngine is further broken into three
49 * steps:
50 *
51 * 1) Character processing - this step analyses the characters and assigns a list of OpenType
52 * feature tags to each one. It may also change, remove or add characters, and change
53 * their order.
54 *
55 * 2) Glyph processing - This step performs character to glyph mapping,and uses the GSUB
56 * table associated with the font to perform glyph substitutions, such as ligature substitution.
57 *
58 * 3) Glyph post processing - in cases where the font doesn't directly contain a GSUB table,
59 * the previous two steps may have generated "fake" glyph indices to use with a "canned" GSUB
60 * table. This step turns those glyph indices into actual font-specific glyph indices, and may
61 * perform any other adjustments requried by the previous steps.
62 *
63 * OpenTypeLayoutEngine will also use the font's GPOS table to apply position adjustments
64 * such as kerning and accent positioning.
65 *
66 * @see LayoutEngine
67 *
68 * @internal
69 */
70class OpenTypeLayoutEngine : public LayoutEngine
71{
72public:
73 /**
74 * This is the main constructor. It constructs an instance of OpenTypeLayoutEngine for
75 * a particular font, script and language. It takes the GSUB table as a parameter since
76 * LayoutEngine::layoutEngineFactory has to read the GSUB table to know that it has an
77 * OpenType font.
78 *
79 * @param fontInstance - the font
80 * @param scriptCode - the script
81 * @param langaugeCode - the language
82 * @param gsubTable - the GSUB table
83 *
84 * @see LayoutEngine::layoutEngineFactory
85 * @see ScriptAndLangaugeTags.h for script and language codes
86 *
87 * @internal
88 */
89 OpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode,
90 le_int32 typoFlags, const GlyphSubstitutionTableHeader *gsubTable);
91
92 /**
93 * This constructor is used when the font requires a "canned" GSUB table which can't be known
94 * until after this constructor has been invoked.
95 *
96 * @param fontInstance - the font
97 * @param scriptCode - the script
98 * @param languageCode - the language
99 *
100 * @internal
101 */
102 OpenTypeLayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags);
103
104 /**
105 * The destructor, virtual for correct polymorphic invocation.
106 *
107 * @internal
108 */
109 virtual ~OpenTypeLayoutEngine();
110
111 /**
112 * A convenience method used to convert the script code into
113 * the four byte script tag required by OpenType.
114 *
115 * @param scriptCode - the script code
116 *
117 * @return the four byte script tag
118 *
119 * @internal
120 */
121 static LETag getScriptTag(le_int32 scriptCode);
122
123 /**
124 * A convenience method used to convert the langauge code into
125 * the four byte langauge tag required by OpenType.
126 *
127 * @param languageCode - the language code
128 *
129 * @return the four byte language tag
130 *
131 * @internal
132 */
133 static LETag getLangSysTag(le_int32 languageCode);
134
135private:
136
137 /**
138 * This method is used by the constructors to convert the script
139 * and language codes to four byte tags and save them.
140 */
141 void setScriptAndLanguageTags();
142
143 /**
144 * The array of script tags, indexed by script code.
145 */
146 static const LETag scriptTags[];
147
148 /**
149 * The array of language tags, indexed by language code.
150 */
151 static const LETag languageTags[];
152
153protected:
154 /**
155 * A set of "default" features. The default characterProcessing method
156 * will apply all of these features to every glyph.
157 *
158 * @internal
159 */
160 FeatureMask fFeatureMask;
161
162 /**
163 * A set of mappings from feature tags to feature masks. These may
164 * be in the order in which the featues should be applied, but they
165 * don't need to be.
166 *
167 * @internal
168 */
169 const FeatureMap *fFeatureMap;
170
171 /**
172 * The length of the feature map.
173 *
174 * @internal
175 */
176 le_int32 fFeatureMapCount;
177
178 /**
179 * <code>TRUE</code> if the features in the
180 * feature map are in the order in which they
181 * must be applied.
182 *
183 * @internal
184 */
185 le_bool fFeatureOrder;
186
187 /**
188 * The address of the GSUB table.
189 *
190 * @internal
191 */
192 const GlyphSubstitutionTableHeader *fGSUBTable;
193
194 /**
195 * The address of the GDEF table.
196 *
197 * @internal
198 */
199 const GlyphDefinitionTableHeader *fGDEFTable;
200
201 /**
202 * The address of the GPOS table.
203 *
204 * @internal
205 */
206 const GlyphPositioningTableHeader *fGPOSTable;
207
208 /**
209 * An optional filter used to inhibit substitutions
210 * preformed by the GSUB table. This is used for some
211 * "canned" GSUB tables to restrict substitutions to
212 * glyphs that are in the font.
213 *
214 * @internal
215 */
216 LEGlyphFilter *fSubstitutionFilter;
217
218 /**
219 * The four byte script tag.
220 *
221 * @internal
222 */
223 LETag fScriptTag;
224
225 /**
226 * The four byte language tag
227 *
228 * @internal
229 */
230 LETag fLangSysTag;
231
232 /**
233 * This method does the OpenType character processing. It assigns the OpenType feature
234 * tags to the characters, and may generate output characters that differ from the input
235 * charcters due to insertions, deletions, or reorderings. In such cases, it will also
236 * generate an output character index array reflecting these changes.
237 *
238 * Subclasses must override this method.
239 *
240 * Input parameters:
241 * @param chars - the input character context
242 * @param offset - the index of the first character to process
243 * @param count - the number of characters to process
244 * @param max - the number of characters in the input context
245 * @param rightToLeft - TRUE if the characters are in a right to left directional run
246 *
247 * Output parameters:
248 * @param outChars - the output character array, if different from the input
249 * @param charIndices - the output character index array
250 * @param featureTags - the output feature tag array
251 * @param success - set to an error code if the operation fails
252 *
253 * @return the output character count (input character count if no change)
254 *
255 * @internal
256 */
257 virtual le_int32 characterProcessing(const LEUnicode /*chars*/[], le_int32 offset,
258 le_int32 count, le_int32 max, le_bool /*rightToLeft*/,
259 LEUnicode *&/*outChars*/, LEGlyphStorage &glyphStorage, LEErrorCode &success);
260
261 /**
262 * This method does character to glyph mapping, and applies the GSUB table. The
263 * default implementation calls mapCharsToGlyphs and then applies the GSUB table,
264 * if there is one.
265 *
266 * Note that in the case of "canned" GSUB tables, the output glyph indices may be
267 * "fake" glyph indices that need to be converted to "real" glyph indices by the
268 * glyphPostProcessing method.
269 *
270 * Input parameters:
271 * @param chars - the input character context
272 * @param offset - the index of the first character to process
273 * @param count - the number of characters to process
274 * @param max - the number of characters in the input context
275 * @param rightToLeft - TRUE if the characters are in a right to left directional run
276 * @param featureTags - the feature tag array
277 *
278 * Output parameters:
279 * @param glyphs - the output glyph index array
280 * @param charIndices - the output character index array
281 * @param success - set to an error code if the operation fails
282 *
283 * @return the number of glyphs in the output glyph index array
284 *
285 * Note: if the character index array was already set by the characterProcessing
286 * method, this method won't change it.
287 *
288 * @internal
289 */
290 virtual le_int32 glyphProcessing(const LEUnicode chars[], le_int32 offset,
291 le_int32 count, le_int32 max, le_bool rightToLeft,
292 LEGlyphStorage &glyphStorage, LEErrorCode &success);
293
294 /**
295 * This method does any processing necessary to convert "fake"
296 * glyph indices used by the glyphProcessing method into "real" glyph
297 * indices which can be used to render the text. Note that in some
298 * cases, such as CDAC Indic fonts, several "real" glyphs may be needed
299 * to render one "fake" glyph.
300 *
301 * The default implementation of this method just returns the input glyph
302 * index and character index arrays, assuming that no "fake" glyph indices
303 * were needed to do GSUB processing.
304 *
305 * Input paramters:
306 * @param tempGlyphs - the input "fake" glyph index array
307 * @param tempCharIndices - the input "fake" character index array
308 * @param tempGlyphCount - the number of "fake" glyph indices
309 *
310 * Output parameters:
311 * @param glyphs - the output glyph index array
312 * @param charIndices - the output character index array
313 * @param success - set to an error code if the operation fails
314 *
315 * @return the number of glyph indices in the output glyph index array
316 *
317 * @internal
318 */
319 virtual le_int32 glyphPostProcessing(LEGlyphStorage &tempGlyphStorage,
320 LEGlyphStorage &glyphStorage, LEErrorCode &success);
321
322 /**
323 * This method applies the characterProcessing, glyphProcessing and glyphPostProcessing
324 * methods. Most subclasses will not need to override this method.
325 *
326 * Input parameters:
327 * @param chars - the input character context
328 * @param offset - the index of the first character to process
329 * @param count - the number of characters to process
330 * @param max - the number of characters in the input context
331 * @param rightToLeft - TRUE if the text is in a right to left directional run
332 *
333 * Output parameters:
334 * @param glyphs - the glyph index array
335 * @param charIndices - the character index array
336 * @param success - set to an error code if the operation fails
337 *
338 * @return the number of glyphs in the glyph index array
339 *
340 * @see LayoutEngine::computeGlyphs
341 *
342 * @internal
343 */
344 virtual le_int32 computeGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count,
345 le_int32 max, le_bool rightToLeft, LEGlyphStorage &glyphStorage, LEErrorCode &success);
346
347 /**
348 * This method uses the GPOS table, if there is one, to adjust the glyph positions.
349 *
350 * Input parameters:
351 * @param glyphs - the input glyph array
352 * @param glyphCount - the number of glyphs in the glyph array
353 * @param x - the starting X position
354 * @param y - the starting Y position
355 *
356 * Output parameters:
357 * @param positions - the output X and Y positions (two entries per glyph)
358 * @param success - set to an error code if the operation fails
359 *
360 * @internal
361 */
362 virtual void adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count,
363 le_bool reverse, LEGlyphStorage &glyphStorage, LEErrorCode &success);
364
365 /**
366 * This method frees the feature tag array so that the
367 * OpenTypeLayoutEngine can be reused for different text.
368 * It is also called from our destructor.
369 *
370 * @internal
371 */
372 virtual void reset();
373};
374
375#endif