|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Symbian implementation of LEFontInstance |
|
16 * |
|
17 */ |
|
18 |
|
19 // Symbian includes |
|
20 #include <stdio.h> |
|
21 #include <gdi.h> |
|
22 #include <openfont.h> |
|
23 #include <fntstore.h> |
|
24 #include <string.h> |
|
25 |
|
26 // Icu includes |
|
27 #include "layout/LETypes.h" |
|
28 #include "layout/LEFontInstance.h" |
|
29 #include "layout/LESwaps.h" |
|
30 |
|
31 #include "SymbianFontInstance.h" |
|
32 |
|
33 // |
|
34 // Finds the high bit by binary searching |
|
35 // through the bits in n. |
|
36 // |
|
37 le_int8 SymbianFontInstance::highBit(le_int32 value) |
|
38 { |
|
39 if (value <= 0) |
|
40 { |
|
41 return -32; |
|
42 } |
|
43 |
|
44 le_uint8 bit = 0; |
|
45 |
|
46 if (value >= 1 << 16) |
|
47 { |
|
48 value >>= 16; |
|
49 bit += 16; |
|
50 } |
|
51 |
|
52 if (value >= 1 << 8) |
|
53 { |
|
54 value >>= 8; |
|
55 bit += 8; |
|
56 } |
|
57 |
|
58 if (value >= 1 << 4) |
|
59 { |
|
60 value >>= 4; |
|
61 bit += 4; |
|
62 } |
|
63 |
|
64 if (value >= 1 << 2) |
|
65 { |
|
66 value >>= 2; |
|
67 bit += 2; |
|
68 } |
|
69 |
|
70 if (value >= 1 << 1) |
|
71 { |
|
72 value >>= 1; |
|
73 bit += 1; |
|
74 } |
|
75 |
|
76 return bit; |
|
77 } |
|
78 |
|
79 // This is used to build a shaper which does not require the rasteriser |
|
80 // to support table access. This is required for interworking testing. |
|
81 // #define TRUETYPE_EXTENSION_NOT_SUPPORTED |
|
82 #ifndef TRUETYPE_EXTENSION_NOT_SUPPORTED |
|
83 |
|
84 |
|
85 SymbianFontInstance::SymbianFontInstance(CBitmapFont *aBitmapFont, |
|
86 LEErrorCode &status, le_bool aKeepGlyphOfZWJ) |
|
87 : fFile(NULL), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0), |
|
88 fDirectory(NULL), fCMAPMapper(NULL), fHMTXTable(0), fNumGlyphs(0), |
|
89 fNumLongHorMetrics(0), iFont(aBitmapFont), iKeepGlyphOfZWJ(aKeepGlyphOfZWJ) |
|
90 |
|
91 { |
|
92 // get the OpenFont details |
|
93 if (!aBitmapFont->IsOpenFont()) |
|
94 { |
|
95 // the font supplied was not a Openfont so .. |
|
96 status = LE_ILLEGAL_ARGUMENT_ERROR; |
|
97 return; |
|
98 } |
|
99 |
|
100 COpenFont* font = aBitmapFont->OpenFont(); |
|
101 |
|
102 // get the extended interface |
|
103 TAny* ext = 0; |
|
104 font->ExtendedInterface(KUidOpenFontShapingExtension, ext); |
|
105 MOpenFontShapingExtension* extensionInterface |
|
106 = reinterpret_cast<MOpenFontShapingExtension*>(ext); |
|
107 |
|
108 font->ExtendedInterface(KUidOpenFontTrueTypeExtension, ext); |
|
109 MOpenFontTrueTypeExtension* trueTypeExtensionInterface |
|
110 = reinterpret_cast<MOpenFontTrueTypeExtension*>(ext); |
|
111 |
|
112 /* Currently if the trueTypeExtensionInterface is not available the |
|
113 truetype tables are accessed directly rather than via the rasteriser */ |
|
114 if (!extensionInterface /* || !trueTypeExtensionInterface*/) |
|
115 { |
|
116 // this rasterizer does not support the required interface |
|
117 // so do not bother to shape |
|
118 status = LE_ILLEGAL_ARGUMENT_ERROR; |
|
119 return ; |
|
120 } |
|
121 |
|
122 iTrueTypeExtensionInterface = trueTypeExtensionInterface; |
|
123 iExtensionInterface = extensionInterface; |
|
124 |
|
125 // get the font file name from the COpenFontFile in the COpenFont |
|
126 TBuf8<KMaxFileName> fontFileName; |
|
127 fontFileName.Copy(font->File()->FileName()); |
|
128 fontFileName.ZeroTerminate(); |
|
129 |
|
130 /* get the font metrics via the rasterizer */ |
|
131 MOpenFontShapingExtension::TExtensionFontMetrics fontMetrics; |
|
132 extensionInterface->GetExtensionFontMetrics(fontMetrics); |
|
133 |
|
134 /* The number of font design units per em. */ |
|
135 fUnitsPerEM = fontMetrics.iUnitsPerEm; |
|
136 |
|
137 /* The size of the font's em square in pixels. */ |
|
138 fXPixelsPerEm = fontMetrics.iXPixelsPerEm; |
|
139 fYPixelsPerEm = fontMetrics.iYPixelsPerEm; |
|
140 |
|
141 /* Scaling factors, pixels per font unit. */ |
|
142 fDeviceScaleY = fontMetrics.iYScaleFactor; |
|
143 fDeviceScaleX = fontMetrics.iXScaleFactor; |
|
144 |
|
145 // open the font file |
|
146 fFile = fopen( (char *)fontFileName.Ptr(), "rb"); |
|
147 if (fFile == 0) |
|
148 { |
|
149 status = LE_FONT_FILE_NOT_FOUND_ERROR; |
|
150 return; |
|
151 } |
|
152 |
|
153 // read in the directory |
|
154 SFNTDirectory tempDir; |
|
155 |
|
156 fread(&tempDir, sizeof tempDir, 1, fFile); |
|
157 |
|
158 le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry)); |
|
159 const LETag headTag = LE_HEAD_TABLE_TAG; |
|
160 const LETag hheaTag = LE_HHEA_TABLE_TAG; |
|
161 const HEADTable *headTable = NULL; |
|
162 const HHEATable *hheaTable = NULL; |
|
163 le_uint16 numTables = 0; |
|
164 |
|
165 //coverity[incorrect_multiplication] |
|
166 //coverity[buffer_alloc] |
|
167 // dirSize is The actually sizoe of fDirectory which indeed contains more data thant SFNTDirectory defined. |
|
168 fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize); |
|
169 |
|
170 if (fDirectory == NULL) |
|
171 { |
|
172 status = LE_MEMORY_ALLOCATION_ERROR; |
|
173 goto error_exit; |
|
174 } |
|
175 |
|
176 fseek(fFile, 0L, SEEK_SET); |
|
177 fread((void *) fDirectory, sizeof(char), dirSize, fFile); |
|
178 |
|
179 // |
|
180 // We calculate these numbers 'cause some fonts |
|
181 // have bogus values for them in the directory header. |
|
182 // |
|
183 numTables = SWAPW(fDirectory->numTables); |
|
184 fDirPower = 1 << highBit(numTables); |
|
185 fDirExtra = numTables - fDirPower; |
|
186 |
|
187 // read unitsPerEm from 'head' table |
|
188 headTable = (const HEADTable *) readFontTable(headTag); |
|
189 |
|
190 if (headTable == NULL) |
|
191 { |
|
192 status = LE_MISSING_FONT_TABLE_ERROR; |
|
193 goto error_exit; |
|
194 } |
|
195 |
|
196 fUnitsPerEM = SWAPW(headTable->unitsPerEm); |
|
197 deleteTable(headTable); |
|
198 |
|
199 hheaTable = (HHEATable *) readFontTable(hheaTag); |
|
200 |
|
201 if (hheaTable == NULL) |
|
202 { |
|
203 status = LE_MISSING_FONT_TABLE_ERROR; |
|
204 goto error_exit; |
|
205 } |
|
206 |
|
207 fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent)); |
|
208 fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent)); |
|
209 fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap)); |
|
210 |
|
211 fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics); |
|
212 |
|
213 deleteTable((void *) hheaTable); |
|
214 |
|
215 fCMAPMapper = findUnicodeMapper(); |
|
216 |
|
217 if (fCMAPMapper == NULL) |
|
218 { |
|
219 status = LE_MISSING_FONT_TABLE_ERROR; |
|
220 goto error_exit; |
|
221 } |
|
222 |
|
223 return; |
|
224 |
|
225 error_exit: |
|
226 fclose(fFile); |
|
227 fFile = NULL; |
|
228 return; |
|
229 } |
|
230 |
|
231 SymbianFontInstance::~SymbianFontInstance() |
|
232 { |
|
233 if (fFile != NULL) |
|
234 { |
|
235 fclose(fFile); |
|
236 deleteTable(fHMTXTable); |
|
237 delete fCMAPMapper; |
|
238 LE_DELETE_ARRAY(fDirectory); |
|
239 } |
|
240 }; |
|
241 |
|
242 #else |
|
243 /* read the font directly. This is used if rasterizers do not support |
|
244 the trueTypeExtensionInterface */ |
|
245 |
|
246 SymbianFontInstance::SymbianFontInstance(CBitmapFont *aBitmapFont, |
|
247 LEErrorCode &status) |
|
248 : fFile(NULL), fUnitsPerEM(0), fAscent(0), fDescent(0), fLeading(0), |
|
249 fDirectory(NULL), fCMAPMapper(NULL), fHMTXTable(0), fNumGlyphs(0), |
|
250 fNumLongHorMetrics(0), iFont(aBitmapFont) |
|
251 |
|
252 { |
|
253 // get the OpenFont details |
|
254 if (!aBitmapFont->IsOpenFont()) |
|
255 { |
|
256 // the font supplied was not a Openfont so .. |
|
257 status = LE_ILLEGAL_ARGUMENT_ERROR; |
|
258 return; |
|
259 } |
|
260 |
|
261 COpenFont* font = aBitmapFont->OpenFont(); |
|
262 |
|
263 // get the extended interface |
|
264 TAny* ext = 0; |
|
265 font->ExtendedInterface(KUidOpenFontShapingExtension, ext); |
|
266 iExtensionInterface = reinterpret_cast<MOpenFontShapingExtension*>(ext); |
|
267 |
|
268 // do not use the true type interface |
|
269 iTrueTypeExtensionInterface = 0; |
|
270 |
|
271 if (!iExtensionInterface ) |
|
272 { |
|
273 // this rasterizer does not support the required interface |
|
274 // so do not bother to shape |
|
275 status = LE_ILLEGAL_ARGUMENT_ERROR; |
|
276 return ; |
|
277 } |
|
278 |
|
279 // get the font file name from the COpenFontFile in the COpenFont |
|
280 TBuf8<KMaxFileName> fontFileName; |
|
281 fontFileName.Copy(font->File()->FileName()); |
|
282 fontFileName.ZeroTerminate(); |
|
283 |
|
284 /* get the font metrics via the rasterizer */ |
|
285 MOpenFontShapingExtension::TExtensionFontMetrics fontMetrics; |
|
286 iExtensionInterface->GetExtensionFontMetrics(fontMetrics); |
|
287 |
|
288 /* The number of font design units per em. */ |
|
289 fUnitsPerEM = fontMetrics.iUnitsPerEm; |
|
290 |
|
291 /* The size of the font's em square in pixels. */ |
|
292 fXPixelsPerEm = fontMetrics.iXPixelsPerEm; |
|
293 fYPixelsPerEm = fontMetrics.iYPixelsPerEm; |
|
294 |
|
295 /* Scaling factors, pixels per font unit. */ |
|
296 fDeviceScaleY = fontMetrics.iYScaleFactor; |
|
297 fDeviceScaleX = fontMetrics.iXScaleFactor; |
|
298 |
|
299 // open the font file |
|
300 fFile = fopen( (char *)fontFileName.Ptr(), "rb"); |
|
301 if (fFile == 0) |
|
302 { |
|
303 status = LE_FONT_FILE_NOT_FOUND_ERROR; |
|
304 return; |
|
305 } |
|
306 |
|
307 // read in the directory |
|
308 SFNTDirectory tempDir; |
|
309 |
|
310 fread(&tempDir, sizeof tempDir, 1, fFile); |
|
311 |
|
312 le_int32 dirSize = sizeof tempDir + ((SWAPW(tempDir.numTables) - ANY_NUMBER) * sizeof(DirectoryEntry)); |
|
313 const LETag headTag = LE_HEAD_TABLE_TAG; |
|
314 const LETag hheaTag = LE_HHEA_TABLE_TAG; |
|
315 const HEADTable *headTable = NULL; |
|
316 const HHEATable *hheaTable = NULL; |
|
317 le_uint16 numTables = 0; |
|
318 |
|
319 fDirectory = (const SFNTDirectory *) LE_NEW_ARRAY(char, dirSize); |
|
320 |
|
321 if (fDirectory == NULL) |
|
322 { |
|
323 status = LE_MEMORY_ALLOCATION_ERROR; |
|
324 goto error_exit; |
|
325 } |
|
326 |
|
327 fseek(fFile, 0L, SEEK_SET); |
|
328 fread((void *) fDirectory, sizeof(char), dirSize, fFile); |
|
329 |
|
330 // |
|
331 // We calculate these numbers 'cause some fonts |
|
332 // have bogus values for them in the directory header. |
|
333 // |
|
334 numTables = SWAPW(fDirectory->numTables); |
|
335 fDirPower = 1 << highBit(numTables); |
|
336 fDirExtra = numTables - fDirPower; |
|
337 |
|
338 // read unitsPerEm from 'head' table |
|
339 headTable = (const HEADTable *) readFontTable(headTag); |
|
340 |
|
341 if (headTable == NULL) |
|
342 { |
|
343 status = LE_MISSING_FONT_TABLE_ERROR; |
|
344 goto error_exit; |
|
345 } |
|
346 |
|
347 fUnitsPerEM = SWAPW(headTable->unitsPerEm); |
|
348 deleteTable(headTable); |
|
349 |
|
350 hheaTable = (HHEATable *) readFontTable(hheaTag); |
|
351 |
|
352 if (hheaTable == NULL) |
|
353 { |
|
354 status = LE_MISSING_FONT_TABLE_ERROR; |
|
355 goto error_exit; |
|
356 } |
|
357 |
|
358 fAscent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->ascent)); |
|
359 fDescent = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->descent)); |
|
360 fLeading = (le_int32) yUnitsToPoints((float) SWAPW(hheaTable->lineGap)); |
|
361 |
|
362 fNumLongHorMetrics = SWAPW(hheaTable->numOfLongHorMetrics); |
|
363 |
|
364 deleteTable((void *) hheaTable); |
|
365 |
|
366 fCMAPMapper = findUnicodeMapper(); |
|
367 |
|
368 if (fCMAPMapper == NULL) |
|
369 { |
|
370 status = LE_MISSING_FONT_TABLE_ERROR; |
|
371 goto error_exit; |
|
372 } |
|
373 |
|
374 return; |
|
375 |
|
376 error_exit: |
|
377 fclose(fFile); |
|
378 fFile = NULL; |
|
379 return; |
|
380 } |
|
381 |
|
382 SymbianFontInstance::~SymbianFontInstance() |
|
383 { |
|
384 if (fFile != NULL) |
|
385 { |
|
386 fclose(fFile); |
|
387 |
|
388 deleteTable(fHMTXTable); |
|
389 |
|
390 delete fCMAPMapper; |
|
391 |
|
392 LE_DELETE_ARRAY(fDirectory); |
|
393 } |
|
394 }; |
|
395 |
|
396 #endif |
|
397 |
|
398 |
|
399 void SymbianFontInstance::deleteTable(const void *table) const |
|
400 { |
|
401 LE_DELETE_ARRAY(table); |
|
402 } |
|
403 |
|
404 const DirectoryEntry *SymbianFontInstance::findTable(LETag tag) const |
|
405 { |
|
406 if (fDirectory != NULL) |
|
407 { |
|
408 le_uint16 table = 0; |
|
409 le_uint16 probe = fDirPower; |
|
410 |
|
411 if (SWAPL(fDirectory->tableDirectory[fDirExtra].tag) <= tag) |
|
412 { |
|
413 table = fDirExtra; |
|
414 } |
|
415 |
|
416 while (probe > (1 << 0)) |
|
417 { |
|
418 probe >>= 1; |
|
419 |
|
420 if (SWAPL(fDirectory->tableDirectory[table + probe].tag) <= tag) |
|
421 { |
|
422 table += probe; |
|
423 } |
|
424 } |
|
425 |
|
426 if (SWAPL(fDirectory->tableDirectory[table].tag) == tag) |
|
427 { |
|
428 return &fDirectory->tableDirectory[table]; |
|
429 } |
|
430 } |
|
431 |
|
432 return NULL; |
|
433 } |
|
434 |
|
435 const void *SymbianFontInstance::readTable(LETag aTag, le_uint32 *aLength) const |
|
436 { |
|
437 void *table = NULL; |
|
438 |
|
439 /* If the current rasteriser supports the TrueTypeExtensionInterface |
|
440 use it to access the true Type tables */ |
|
441 if (iTrueTypeExtensionInterface) |
|
442 { |
|
443 TInt error; |
|
444 TInt length; |
|
445 TAny * trueTypeTable = iTrueTypeExtensionInterface-> |
|
446 GetTrueTypeTable(error, aTag, &length); |
|
447 |
|
448 if (!trueTypeTable) |
|
449 { |
|
450 *aLength = 0; |
|
451 return 0; |
|
452 } |
|
453 |
|
454 if (aLength) |
|
455 *aLength = length; |
|
456 |
|
457 // allocate some memory for the table & copy table into it |
|
458 table = LE_NEW_ARRAY(char, length); |
|
459 if (table) |
|
460 { |
|
461 // copy the table from the rasteriser |
|
462 Mem::Copy(table, (char *) trueTypeTable, length); |
|
463 } |
|
464 |
|
465 iTrueTypeExtensionInterface->ReleaseTrueTypeTable(trueTypeTable); |
|
466 } |
|
467 else |
|
468 { |
|
469 // old method, read the table directly |
|
470 const DirectoryEntry *entry = findTable(aTag); |
|
471 |
|
472 if (entry == NULL) |
|
473 { |
|
474 if (aLength) |
|
475 *aLength = 0; |
|
476 return NULL; |
|
477 } |
|
478 |
|
479 TInt length = SWAPL(entry->length); |
|
480 if (aLength) |
|
481 *aLength = length; |
|
482 |
|
483 table = LE_NEW_ARRAY(char, length); |
|
484 |
|
485 if (table != NULL) |
|
486 { |
|
487 fseek(fFile, SWAPL(entry->offset), SEEK_SET); |
|
488 fread(table, sizeof(char), length, fFile); |
|
489 } |
|
490 } |
|
491 |
|
492 return table; |
|
493 } |
|
494 |
|
495 const void *SymbianFontInstance::getFontTable(LETag tableTag) const |
|
496 { |
|
497 if (iTrueTypeExtensionInterface) |
|
498 { |
|
499 TInt error; |
|
500 return iTrueTypeExtensionInterface->GetTrueTypeTable(error, tableTag, 0); |
|
501 } |
|
502 return FontTableCache::find(tableTag); |
|
503 } |
|
504 |
|
505 const void *SymbianFontInstance::readFontTable(LETag tableTag) const |
|
506 { |
|
507 return readTable(tableTag, 0); |
|
508 } |
|
509 |
|
510 CMAPMapper *SymbianFontInstance::findUnicodeMapper() |
|
511 { |
|
512 LETag cmapTag = LE_CMAP_TABLE_TAG; |
|
513 const CMAPTable *cmap = (CMAPTable *) readFontTable(cmapTag); |
|
514 |
|
515 return cmap ? CMAPMapper::createUnicodeMapper(cmap) : NULL; |
|
516 } |
|
517 |
|
518 void SymbianFontInstance::getGlyphAdvance( |
|
519 LEGlyphID aGlyph, LEPoint &aAdvance) const |
|
520 { |
|
521 TInt glyph = LE_GET_GLYPH(aGlyph) | 0x80000000; |
|
522 TOpenFontCharMetrics metrics; |
|
523 const TUint8* bitmap = 0; |
|
524 |
|
525 // If an FFFF glyph code is received, avoid trying to rasterize it as there |
|
526 // is no character data associated with FFFF. |
|
527 // This will avoid the overhead of trying to rasterize it. |
|
528 if (glyph == 0x8000FFFF) |
|
529 { |
|
530 aAdvance.fX = 0; |
|
531 aAdvance.fY = 0; |
|
532 return; |
|
533 } |
|
534 |
|
535 if (!iFont->GetCharacterData(iSessionHandle, glyph, metrics, bitmap)) |
|
536 { |
|
537 // Glyph not yet rasterized; rasterize it ourselves |
|
538 iFont->Rasterize(iSessionHandle, glyph, 0); |
|
539 if (!iFont->GetCharacterData(iSessionHandle, glyph, metrics, bitmap)) |
|
540 { |
|
541 aAdvance.fX = 0; |
|
542 aAdvance.fY = 0; |
|
543 return; |
|
544 } |
|
545 } |
|
546 aAdvance.fX = metrics.HorizAdvance(); |
|
547 aAdvance.fY = 0; |
|
548 return; |
|
549 } |
|
550 |
|
551 le_bool SymbianFontInstance::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const |
|
552 { |
|
553 TReal x; |
|
554 TReal y; |
|
555 if (!iExtensionInterface->GlyphPointInHintedPixels( |
|
556 glyph, pointNumber, x, y )) |
|
557 return FALSE; |
|
558 point.fX = x; |
|
559 point.fY = y; |
|
560 return TRUE; |
|
561 } |
|
562 |
|
563 void SymbianFontInstance::transformFunits(float xFunits, float yFunits, LEPoint &pixels) const |
|
564 { |
|
565 pixels.fX = xFunits / fUnitsPerEM * fXPixelsPerEm; |
|
566 pixels.fY = yFunits / fUnitsPerEM * fYPixelsPerEm; |
|
567 } |
|
568 |
|
569 float SymbianFontInstance::xUnitsToPoints(float xUnits) const |
|
570 { |
|
571 // Seems like this function should have been called xUnitsToPixels?! |
|
572 return xUnits / fUnitsPerEM * fXPixelsPerEm; |
|
573 } |
|
574 |
|
575 float SymbianFontInstance::yUnitsToPoints(float yUnits) const |
|
576 { |
|
577 // Seems like this function should have been called yUnitsToPixels?! |
|
578 return yUnits / fUnitsPerEM * fYPixelsPerEm; |
|
579 } |
|
580 |
|
581 float SymbianFontInstance::getXPixelsPerEm() const |
|
582 { |
|
583 return fXPixelsPerEm; |
|
584 } |
|
585 |
|
586 float SymbianFontInstance::getYPixelsPerEm() const |
|
587 { |
|
588 return fYPixelsPerEm; |
|
589 } |
|
590 |
|
591 float SymbianFontInstance::getScaleFactorX() const |
|
592 { |
|
593 return fDeviceScaleX; |
|
594 } |
|
595 |
|
596 float SymbianFontInstance::getScaleFactorY() const |
|
597 { |
|
598 return fDeviceScaleY; |
|
599 } |