]> git.saurik.com Git - apple/icu.git/blob - icuSources/layout/LayoutEngine.cpp
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / layout / LayoutEngine.cpp
1
2 /*
3 *
4 * (C) Copyright IBM Corp. 1998-2008 - All Rights Reserved
5 *
6 */
7
8 #include "LETypes.h"
9 #include "LEScripts.h"
10 #include "LELanguages.h"
11
12 #include "LayoutEngine.h"
13 #include "ArabicLayoutEngine.h"
14 #include "CanonShaping.h"
15 #include "HanLayoutEngine.h"
16 #include "HangulLayoutEngine.h"
17 #include "IndicLayoutEngine.h"
18 #include "KhmerLayoutEngine.h"
19 #include "ThaiLayoutEngine.h"
20 #include "TibetanLayoutEngine.h"
21 #include "GXLayoutEngine.h"
22 #include "ScriptAndLanguageTags.h"
23 #include "CharSubstitutionFilter.h"
24
25 #include "LEGlyphStorage.h"
26
27 #include "OpenTypeUtilities.h"
28 #include "GlyphSubstitutionTables.h"
29 #include "GlyphDefinitionTables.h"
30 #include "MorphTables.h"
31
32 #include "DefaultCharMapper.h"
33
34 #include "KernTable.h"
35
36 U_NAMESPACE_BEGIN
37
38 /* Leave this copyright notice here! It needs to go somewhere in this library. */
39 static const char copyright[] = U_COPYRIGHT_STRING;
40
41 const LEUnicode32 DefaultCharMapper::controlChars[] = {
42 0x0009, 0x000A, 0x000D,
43 /*0x200C, 0x200D,*/ 0x200E, 0x200F,
44 0x2028, 0x2029, 0x202A, 0x202B, 0x202C, 0x202D, 0x202E,
45 0x206A, 0x206B, 0x206C, 0x206D, 0x206E, 0x206F
46 };
47
48 const le_int32 DefaultCharMapper::controlCharsCount = LE_ARRAY_SIZE(controlChars);
49
50 LEUnicode32 DefaultCharMapper::mapChar(LEUnicode32 ch) const
51 {
52 if (fFilterControls) {
53 le_int32 index = OpenTypeUtilities::search((le_uint32)ch, (le_uint32 *)controlChars, controlCharsCount);
54
55 if (controlChars[index] == ch) {
56 return 0xFFFF;
57 }
58 }
59
60 if (fMirror) {
61 le_int32 index = OpenTypeUtilities::search((le_uint32) ch, (le_uint32 *)DefaultCharMapper::mirroredChars, DefaultCharMapper::mirroredCharsCount);
62
63 if (mirroredChars[index] == ch) {
64 return DefaultCharMapper::srahCderorrim[index];
65 }
66 }
67
68 return ch;
69 }
70
71 // This is here to get it out of LEGlyphFilter.h.
72 // No particular reason to put it here, other than
73 // this is a good central location...
74 LEGlyphFilter::~LEGlyphFilter()
75 {
76 // nothing to do
77 }
78
79 CharSubstitutionFilter::CharSubstitutionFilter(const LEFontInstance *fontInstance)
80 : fFontInstance(fontInstance)
81 {
82 // nothing to do
83 }
84
85 CharSubstitutionFilter::~CharSubstitutionFilter()
86 {
87 // nothing to do
88 }
89
90 class CanonMarkFilter : public UMemory, public LEGlyphFilter
91 {
92 private:
93 const GlyphClassDefinitionTable *classDefTable;
94
95 CanonMarkFilter(const CanonMarkFilter &other); // forbid copying of this class
96 CanonMarkFilter &operator=(const CanonMarkFilter &other); // forbid copying of this class
97
98 public:
99 CanonMarkFilter(const GlyphDefinitionTableHeader *gdefTable);
100 virtual ~CanonMarkFilter();
101
102 virtual le_bool accept(LEGlyphID glyph) const;
103 };
104
105 CanonMarkFilter::CanonMarkFilter(const GlyphDefinitionTableHeader *gdefTable)
106 {
107 classDefTable = gdefTable->getMarkAttachClassDefinitionTable();
108 }
109
110 CanonMarkFilter::~CanonMarkFilter()
111 {
112 // nothing to do?
113 }
114
115 le_bool CanonMarkFilter::accept(LEGlyphID glyph) const
116 {
117 le_int32 glyphClass = classDefTable->getGlyphClass(glyph);
118
119 return glyphClass != 0;
120 }
121
122 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LayoutEngine)
123
124 #define ccmpFeatureTag LE_CCMP_FEATURE_TAG
125
126 #define ccmpFeatureMask 0x80000000UL
127
128 #define canonFeatures (ccmpFeatureMask)
129
130 static const FeatureMap canonFeatureMap[] =
131 {
132 {ccmpFeatureTag, ccmpFeatureMask}
133 };
134
135 static const le_int32 canonFeatureMapCount = LE_ARRAY_SIZE(canonFeatureMap);
136
137 LayoutEngine::LayoutEngine(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags)
138 : fGlyphStorage(NULL), fFontInstance(fontInstance), fScriptCode(scriptCode), fLanguageCode(languageCode),
139 fTypoFlags(typoFlags), fFilterZeroWidth(TRUE)
140 {
141 fGlyphStorage = new LEGlyphStorage();
142 }
143
144 le_int32 LayoutEngine::getGlyphCount() const
145 {
146 return fGlyphStorage->getGlyphCount();
147 }
148
149 void LayoutEngine::getCharIndices(le_int32 charIndices[], le_int32 indexBase, LEErrorCode &success) const
150 {
151 fGlyphStorage->getCharIndices(charIndices, indexBase, success);
152 }
153
154 void LayoutEngine::getCharIndices(le_int32 charIndices[], LEErrorCode &success) const
155 {
156 fGlyphStorage->getCharIndices(charIndices, success);
157 }
158
159 // Copy the glyphs into caller's (32-bit) glyph array, OR in extraBits
160 void LayoutEngine::getGlyphs(le_uint32 glyphs[], le_uint32 extraBits, LEErrorCode &success) const
161 {
162 fGlyphStorage->getGlyphs(glyphs, extraBits, success);
163 }
164
165 void LayoutEngine::getGlyphs(LEGlyphID glyphs[], LEErrorCode &success) const
166 {
167 fGlyphStorage->getGlyphs(glyphs, success);
168 }
169
170
171 void LayoutEngine::getGlyphPositions(float positions[], LEErrorCode &success) const
172 {
173 fGlyphStorage->getGlyphPositions(positions, success);
174 }
175
176 void LayoutEngine::getGlyphPosition(le_int32 glyphIndex, float &x, float &y, LEErrorCode &success) const
177 {
178 fGlyphStorage->getGlyphPosition(glyphIndex, x, y, success);
179 }
180
181 le_int32 LayoutEngine::characterProcessing(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
182 LEUnicode *&outChars, LEGlyphStorage &glyphStorage, LEErrorCode &success)
183 {
184 if (LE_FAILURE(success)) {
185 return 0;
186 }
187
188 if (offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
189 success = LE_ILLEGAL_ARGUMENT_ERROR;
190 return 0;
191 }
192
193 const GlyphSubstitutionTableHeader *canonGSUBTable = (GlyphSubstitutionTableHeader *) CanonShaping::glyphSubstitutionTable;
194 LETag scriptTag = OpenTypeLayoutEngine::getScriptTag(fScriptCode);
195 LETag langSysTag = OpenTypeLayoutEngine::getLangSysTag(fLanguageCode);
196 le_int32 i, dir = 1, out = 0, outCharCount = count;
197
198 if (canonGSUBTable->coversScript(scriptTag)) {
199 CharSubstitutionFilter *substitutionFilter = new CharSubstitutionFilter(fFontInstance);
200 const LEUnicode *inChars = &chars[offset];
201 LEUnicode *reordered = NULL;
202 LEGlyphStorage fakeGlyphStorage;
203
204 fakeGlyphStorage.allocateGlyphArray(count, rightToLeft, success);
205
206 if (LE_FAILURE(success)) {
207 return 0;
208 }
209
210 // This is the cheapest way to get mark reordering only for Hebrew.
211 // We could just do the mark reordering for all scripts, but most
212 // of them probably don't need it...
213 if (fScriptCode == hebrScriptCode) {
214 reordered = LE_NEW_ARRAY(LEUnicode, count);
215
216 if (reordered == NULL) {
217 success = LE_MEMORY_ALLOCATION_ERROR;
218 return 0;
219 }
220
221 CanonShaping::reorderMarks(&chars[offset], count, rightToLeft, reordered, fakeGlyphStorage);
222 inChars = reordered;
223 }
224
225 fakeGlyphStorage.allocateAuxData(success);
226
227 if (LE_FAILURE(success)) {
228 return 0;
229 }
230
231 if (rightToLeft) {
232 out = count - 1;
233 dir = -1;
234 }
235
236 for (i = 0; i < count; i += 1, out += dir) {
237 fakeGlyphStorage[out] = (LEGlyphID) inChars[i];
238 fakeGlyphStorage.setAuxData(out, canonFeatures, success);
239 }
240
241 if (reordered != NULL) {
242 LE_DELETE_ARRAY(reordered);
243 }
244
245 outCharCount = canonGSUBTable->process(fakeGlyphStorage, rightToLeft, scriptTag, langSysTag, NULL, substitutionFilter, canonFeatureMap, canonFeatureMapCount, FALSE);
246
247 out = (rightToLeft? outCharCount - 1 : 0);
248
249 /*
250 * The char indices array in fakeGlyphStorage has the correct mapping
251 * back to the original input characters. Save it in glyphStorage. The
252 * subsequent call to glyphStoratge.allocateGlyphArray will keep this
253 * array rather than allocating and initializing a new one.
254 */
255 glyphStorage.adoptCharIndicesArray(fakeGlyphStorage);
256
257 outChars = LE_NEW_ARRAY(LEUnicode, outCharCount);
258 for (i = 0; i < outCharCount; i += 1, out += dir) {
259 outChars[out] = (LEUnicode) LE_GET_GLYPH(fakeGlyphStorage[i]);
260 }
261
262 delete substitutionFilter;
263 }
264
265 return outCharCount;
266 }
267
268 le_int32 LayoutEngine::computeGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
269 LEGlyphStorage &glyphStorage, LEErrorCode &success)
270 {
271 if (LE_FAILURE(success)) {
272 return 0;
273 }
274
275 if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
276 success = LE_ILLEGAL_ARGUMENT_ERROR;
277 return 0;
278 }
279
280 LEUnicode *outChars = NULL;
281 le_int32 outCharCount = characterProcessing(chars, offset, count, max, rightToLeft, outChars, glyphStorage, success);
282
283 if (outChars != NULL) {
284 mapCharsToGlyphs(outChars, 0, outCharCount, rightToLeft, rightToLeft, glyphStorage, success);
285 LE_DELETE_ARRAY(outChars); // FIXME: a subclass may have allocated this, in which case this delete might not work...
286 } else {
287 mapCharsToGlyphs(chars, offset, count, rightToLeft, rightToLeft, glyphStorage, success);
288 }
289
290 return glyphStorage.getGlyphCount();
291 }
292
293 // Input: glyphs
294 // Output: positions
295 void LayoutEngine::positionGlyphs(LEGlyphStorage &glyphStorage, float x, float y, LEErrorCode &success)
296 {
297 if (LE_FAILURE(success)) {
298 return;
299 }
300
301 glyphStorage.allocatePositions(success);
302
303 if (LE_FAILURE(success)) {
304 return;
305 }
306
307 le_int32 i, glyphCount = glyphStorage.getGlyphCount();
308
309 for (i = 0; i < glyphCount; i += 1) {
310 LEPoint advance;
311
312 glyphStorage.setPosition(i, x, y, success);
313
314 fFontInstance->getGlyphAdvance(glyphStorage[i], advance);
315 x += advance.fX;
316 y += advance.fY;
317 }
318
319 glyphStorage.setPosition(glyphCount, x, y, success);
320 }
321
322 void LayoutEngine::adjustGlyphPositions(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse,
323 LEGlyphStorage &glyphStorage, LEErrorCode &success)
324 {
325 if (LE_FAILURE(success)) {
326 return;
327 }
328
329 if (chars == NULL || offset < 0 || count < 0) {
330 success = LE_ILLEGAL_ARGUMENT_ERROR;
331 return;
332 }
333
334 GlyphDefinitionTableHeader *gdefTable = (GlyphDefinitionTableHeader *) CanonShaping::glyphDefinitionTable;
335 CanonMarkFilter filter(gdefTable);
336
337 adjustMarkGlyphs(&chars[offset], count, reverse, glyphStorage, &filter, success);
338
339 if (fTypoFlags & 0x1) { /* kerning enabled */
340 static const le_uint32 kernTableTag = LE_KERN_TABLE_TAG;
341
342 KernTable kt(fFontInstance, getFontTable(kernTableTag));
343 kt.process(glyphStorage);
344 }
345
346 // default is no adjustments
347 return;
348 }
349
350 void LayoutEngine::adjustMarkGlyphs(LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, LEErrorCode &success)
351 {
352 float xAdjust = 0;
353 le_int32 p, glyphCount = glyphStorage.getGlyphCount();
354
355 if (LE_FAILURE(success)) {
356 return;
357 }
358
359 if (markFilter == NULL) {
360 success = LE_ILLEGAL_ARGUMENT_ERROR;
361 return;
362 }
363
364 float ignore, prev;
365
366 glyphStorage.getGlyphPosition(0, prev, ignore, success);
367
368 for (p = 0; p < glyphCount; p += 1) {
369 float next, xAdvance;
370
371 glyphStorage.getGlyphPosition(p + 1, next, ignore, success);
372
373 xAdvance = next - prev;
374 glyphStorage.adjustPosition(p, xAdjust, 0, success);
375
376 if (markFilter->accept(glyphStorage[p])) {
377 xAdjust -= xAdvance;
378 }
379
380 prev = next;
381 }
382
383 glyphStorage.adjustPosition(glyphCount, xAdjust, 0, success);
384 }
385
386 void LayoutEngine::adjustMarkGlyphs(const LEUnicode chars[], le_int32 charCount, le_bool reverse, LEGlyphStorage &glyphStorage, LEGlyphFilter *markFilter, LEErrorCode &success)
387 {
388 float xAdjust = 0;
389 le_int32 c = 0, direction = 1, p;
390 le_int32 glyphCount = glyphStorage.getGlyphCount();
391
392 if (LE_FAILURE(success)) {
393 return;
394 }
395
396 if (markFilter == NULL) {
397 success = LE_ILLEGAL_ARGUMENT_ERROR;
398 return;
399 }
400
401 if (reverse) {
402 c = glyphCount - 1;
403 direction = -1;
404 }
405
406 float ignore, prev;
407
408 glyphStorage.getGlyphPosition(0, prev, ignore, success);
409
410 for (p = 0; p < charCount; p += 1, c += direction) {
411 float next, xAdvance;
412
413 glyphStorage.getGlyphPosition(p + 1, next, ignore, success);
414
415 xAdvance = next - prev;
416 glyphStorage.adjustPosition(p, xAdjust, 0, success);
417
418 if (markFilter->accept(chars[c])) {
419 xAdjust -= xAdvance;
420 }
421
422 prev = next;
423 }
424
425 glyphStorage.adjustPosition(glyphCount, xAdjust, 0, success);
426 }
427
428 const void *LayoutEngine::getFontTable(LETag tableTag) const
429 {
430 return fFontInstance->getFontTable(tableTag);
431 }
432
433 void LayoutEngine::mapCharsToGlyphs(const LEUnicode chars[], le_int32 offset, le_int32 count, le_bool reverse, le_bool mirror,
434 LEGlyphStorage &glyphStorage, LEErrorCode &success)
435 {
436 if (LE_FAILURE(success)) {
437 return;
438 }
439
440 glyphStorage.allocateGlyphArray(count, reverse, success);
441
442 DefaultCharMapper charMapper(TRUE, mirror);
443
444 fFontInstance->mapCharsToGlyphs(chars, offset, count, reverse, &charMapper, fFilterZeroWidth, glyphStorage);
445 }
446
447 // Input: characters, font?
448 // Output: glyphs, positions, char indices
449 // Returns: number of glyphs
450 le_int32 LayoutEngine::layoutChars(const LEUnicode chars[], le_int32 offset, le_int32 count, le_int32 max, le_bool rightToLeft,
451 float x, float y, LEErrorCode &success)
452 {
453 if (LE_FAILURE(success)) {
454 return 0;
455 }
456
457 if (chars == NULL || offset < 0 || count < 0 || max < 0 || offset >= max || offset + count > max) {
458 success = LE_ILLEGAL_ARGUMENT_ERROR;
459 return 0;
460 }
461
462 le_int32 glyphCount;
463
464 if (fGlyphStorage->getGlyphCount() > 0) {
465 fGlyphStorage->reset();
466 }
467
468 glyphCount = computeGlyphs(chars, offset, count, max, rightToLeft, *fGlyphStorage, success);
469 positionGlyphs(*fGlyphStorage, x, y, success);
470 adjustGlyphPositions(chars, offset, count, rightToLeft, *fGlyphStorage, success);
471
472 return glyphCount;
473 }
474
475 void LayoutEngine::reset()
476 {
477 fGlyphStorage->reset();
478 }
479
480 LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, LEErrorCode &success)
481 {
482 // 3 -> kerning and ligatures
483 return LayoutEngine::layoutEngineFactory(fontInstance, scriptCode, languageCode, 3, success);
484 }
485
486 LayoutEngine *LayoutEngine::layoutEngineFactory(const LEFontInstance *fontInstance, le_int32 scriptCode, le_int32 languageCode, le_int32 typoFlags, LEErrorCode &success)
487 {
488 static const le_uint32 gsubTableTag = LE_GSUB_TABLE_TAG;
489 static const le_uint32 mortTableTag = LE_MORT_TABLE_TAG;
490
491 if (LE_FAILURE(success)) {
492 return NULL;
493 }
494
495 const GlyphSubstitutionTableHeader *gsubTable = (const GlyphSubstitutionTableHeader *) fontInstance->getFontTable(gsubTableTag);
496 LayoutEngine *result = NULL;
497 LETag scriptTag = 0x00000000;
498 LETag languageTag = 0x00000000;
499
500 if (gsubTable != NULL && gsubTable->coversScript(scriptTag = OpenTypeLayoutEngine::getScriptTag(scriptCode))) {
501 switch (scriptCode) {
502 case bengScriptCode:
503 case devaScriptCode:
504 case gujrScriptCode:
505 case kndaScriptCode:
506 case mlymScriptCode:
507 case oryaScriptCode:
508 case guruScriptCode:
509 case tamlScriptCode:
510 case teluScriptCode:
511 case sinhScriptCode:
512 result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
513 break;
514
515 case arabScriptCode:
516 result = new ArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
517 break;
518
519 case hangScriptCode:
520 result = new HangulOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
521 break;
522
523 case haniScriptCode:
524 languageTag = OpenTypeLayoutEngine::getLangSysTag(languageCode);
525
526 switch (languageCode) {
527 case korLanguageCode:
528 case janLanguageCode:
529 case zhtLanguageCode:
530 case zhsLanguageCode:
531 if (gsubTable->coversScriptAndLanguage(scriptTag, languageTag, TRUE)) {
532 result = new HanOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
533 break;
534 }
535
536 // note: falling through to default case.
537 default:
538 result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
539 break;
540 }
541
542 break;
543
544 case tibtScriptCode:
545 result = new TibetanOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
546 break;
547
548 case khmrScriptCode:
549 result = new KhmerOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
550 break;
551
552 default:
553 result = new OpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags, gsubTable);
554 break;
555 }
556 } else {
557 const MorphTableHeader *morphTable = (MorphTableHeader *) fontInstance->getFontTable(mortTableTag);
558
559 if (morphTable != NULL) {
560 result = new GXLayoutEngine(fontInstance, scriptCode, languageCode, morphTable);
561 } else {
562 switch (scriptCode) {
563 case bengScriptCode:
564 case devaScriptCode:
565 case gujrScriptCode:
566 case kndaScriptCode:
567 case mlymScriptCode:
568 case oryaScriptCode:
569 case guruScriptCode:
570 case tamlScriptCode:
571 case teluScriptCode:
572 case sinhScriptCode:
573 {
574 result = new IndicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
575 break;
576 }
577
578 case arabScriptCode:
579 //case hebrScriptCode:
580 result = new UnicodeArabicOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
581 break;
582
583 //case hebrScriptCode:
584 // return new HebrewOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
585
586 case thaiScriptCode:
587 result = new ThaiLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
588 break;
589
590 case hangScriptCode:
591 result = new HangulOpenTypeLayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
592 break;
593
594 default:
595 result = new LayoutEngine(fontInstance, scriptCode, languageCode, typoFlags);
596 break;
597 }
598 }
599 }
600
601 if (result == NULL) {
602 success = LE_MEMORY_ALLOCATION_ERROR;
603 }
604
605 return result;
606 }
607
608 LayoutEngine::~LayoutEngine() {
609 delete fGlyphStorage;
610 }
611
612 U_NAMESPACE_END