|
1 /* |
|
2 * Copyright (c) 1997-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 * Header FNTRECRD.CPP |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "FNTRECRD.H" |
|
21 |
|
22 const int KNumberOfPopularIndices = 128; |
|
23 const int KNumberOfBitsInByte = 8; |
|
24 const int KNumberOfBitsInTwoBytes = 16; |
|
25 |
|
26 BitmapOffset::BitmapOffset(uint16 aBitmapOffset) |
|
27 : iBitmapOffset(aBitmapOffset) |
|
28 {} |
|
29 |
|
30 void BitmapOffset::Externalize(ostream& out) |
|
31 { |
|
32 out.write((char*) &iBitmapOffset,sizeof(iBitmapOffset)); |
|
33 } |
|
34 |
|
35 CharacterMetrics::CharacterMetrics() |
|
36 : iAscentInPixels(0), |
|
37 iHeightInPixels(0), |
|
38 iLeftAdjustInPixels(0), |
|
39 iMoveInPixels(0), |
|
40 iRightAdjustInPixels(0) |
|
41 { |
|
42 } |
|
43 |
|
44 |
|
45 void CharacterMetrics::Externalize(ostream& out) |
|
46 { |
|
47 out.write((char*) &iAscentInPixels, sizeof(iAscentInPixels)); |
|
48 out.write((char*) &iHeightInPixels, sizeof(iHeightInPixels)); |
|
49 out.write((char*) &iLeftAdjustInPixels, sizeof(iLeftAdjustInPixels)); |
|
50 out.write((char*) &iMoveInPixels, sizeof(iMoveInPixels)); |
|
51 out.write((char*) &iRightAdjustInPixels, sizeof(iRightAdjustInPixels)); |
|
52 } |
|
53 |
|
54 MetricDistributionMember::~MetricDistributionMember() |
|
55 { |
|
56 delete iMetric; |
|
57 } |
|
58 |
|
59 MetricDistributionMember::MetricDistributionMember() |
|
60 : iFrequency(0), iMetric(0) |
|
61 {} |
|
62 |
|
63 CharacterMetrics* MetricDistributionMember::Metric() const |
|
64 { |
|
65 return iMetric; |
|
66 } |
|
67 |
|
68 int MetricDistributionMember::Frequency() const |
|
69 { |
|
70 return iFrequency; |
|
71 } |
|
72 |
|
73 void MetricDistributionMember::SetFrequency(int aFrequency) |
|
74 { |
|
75 iFrequency = aFrequency; |
|
76 } |
|
77 |
|
78 void MetricDistributionMember::IncrementFrequency(int aIncrementBy) |
|
79 { |
|
80 iFrequency += aIncrementBy; |
|
81 } |
|
82 |
|
83 void MetricDistributionMember::SetMetric(CharacterMetrics* aMetric) |
|
84 { |
|
85 iMetric = aMetric; |
|
86 } |
|
87 |
|
88 void MetricDistributionMember::Externalize(ostream& out) |
|
89 { |
|
90 iMetric->Externalize(out); |
|
91 } |
|
92 |
|
93 MetricDistribution::~MetricDistribution() |
|
94 { |
|
95 iCharacterMetricsList.Destroy(); |
|
96 } |
|
97 |
|
98 MetricDistribution* MetricDistribution::New() |
|
99 { |
|
100 return new MetricDistribution; |
|
101 } |
|
102 |
|
103 MetricDistribution::MetricDistribution() |
|
104 {} |
|
105 |
|
106 void MetricDistribution::SortMetricsByFrequency() |
|
107 { // Only need sort the most popular 128, since after this 2 bytes will always be used |
|
108 int maxIndex = iCharacterMetricsList.Size(); |
|
109 if (maxIndex > KNumberOfPopularIndices) |
|
110 { |
|
111 maxIndex = KNumberOfPopularIndices; |
|
112 } |
|
113 for(int indexToSet = 0; indexToSet < maxIndex; indexToSet++) |
|
114 { |
|
115 const CharacterMetrics& mostPopularRemaining = MostPopular(indexToSet); |
|
116 SetIndex(mostPopularRemaining, indexToSet); |
|
117 } |
|
118 } |
|
119 |
|
120 void MetricDistribution::SetIndex(const CharacterMetrics& aMetrics, int aIndexToSet) |
|
121 { |
|
122 int currentPos = Index(aMetrics); |
|
123 if (currentPos != aIndexToSet) |
|
124 { |
|
125 MetricDistributionMember* match = iCharacterMetricsList[currentPos]; |
|
126 MetricDistributionMember* swapPos = iCharacterMetricsList[aIndexToSet]; |
|
127 |
|
128 CharacterMetrics* tempMet = match->Metric(); |
|
129 const int tempFreq = match->Frequency(); |
|
130 |
|
131 match->SetMetric(swapPos->Metric()); |
|
132 match->SetFrequency(swapPos->Frequency()); |
|
133 swapPos->SetMetric(tempMet); |
|
134 swapPos->SetFrequency(tempFreq); |
|
135 } |
|
136 } |
|
137 |
|
138 void MetricDistribution::AddOrIncrementMetric(const CharacterMetrics& aMetrics, int aFrequency) |
|
139 { |
|
140 boolean match = false; |
|
141 const CharacterMetrics* trial = NULL; |
|
142 MetricDistributionMember* link = NULL; |
|
143 int index; |
|
144 int maxIndex = iCharacterMetricsList.Size(); |
|
145 |
|
146 for(index = 0; index < maxIndex && !match; index++) |
|
147 { |
|
148 link = iCharacterMetricsList[index]; |
|
149 if (link) |
|
150 trial = link->Metric(); |
|
151 if (trial && (trial->iAscentInPixels == aMetrics.iAscentInPixels) |
|
152 && (trial->iHeightInPixels == aMetrics.iHeightInPixels) |
|
153 && (trial->iLeftAdjustInPixels == aMetrics.iLeftAdjustInPixels) |
|
154 && (trial->iMoveInPixels == aMetrics.iMoveInPixels) |
|
155 && (trial->iRightAdjustInPixels == aMetrics.iRightAdjustInPixels)) |
|
156 { |
|
157 match = true; |
|
158 } |
|
159 } |
|
160 if (match) |
|
161 { |
|
162 link->IncrementFrequency(aFrequency); |
|
163 } |
|
164 else |
|
165 { |
|
166 MetricDistributionMember* newLink = new MetricDistributionMember; |
|
167 newLink->IncrementFrequency(aFrequency); |
|
168 CharacterMetrics* newMetric = new CharacterMetrics(aMetrics); |
|
169 newLink->SetMetric(newMetric); |
|
170 iCharacterMetricsList.Add(newLink); |
|
171 } |
|
172 } |
|
173 |
|
174 const CharacterMetrics& MetricDistribution::MostPopular(int aStartIndex) |
|
175 { |
|
176 // finds the most popular metric above index aStartIndex. Allows for a fairly quick sort of the metircs to be done. |
|
177 MetricDistributionMember* link = NULL; |
|
178 const CharacterMetrics* mostPopular = NULL; |
|
179 int frequencyOfMostPopular = 0; |
|
180 int frequency = 0; |
|
181 int count; |
|
182 int total = 0; // for debugging |
|
183 const int size = iCharacterMetricsList.Size(); |
|
184 for (count = aStartIndex; count < size; count++) |
|
185 { |
|
186 link = iCharacterMetricsList[count]; |
|
187 frequency = link->Frequency(); |
|
188 if (frequency>frequencyOfMostPopular) |
|
189 { |
|
190 mostPopular = link->Metric(); |
|
191 frequencyOfMostPopular = frequency; |
|
192 } |
|
193 total += frequency; |
|
194 } |
|
195 return *mostPopular; |
|
196 } |
|
197 |
|
198 int MetricDistribution::Index(const CharacterMetrics& aMetrics) |
|
199 { |
|
200 boolean same = false; |
|
201 CharacterMetrics* match = NULL; |
|
202 int i; |
|
203 int size = iCharacterMetricsList.Size(); |
|
204 // see if we have this one already |
|
205 for (i = 0; i < size; i++) |
|
206 { |
|
207 if (iCharacterMetricsList[i]) |
|
208 { |
|
209 match = iCharacterMetricsList[i]->Metric(); |
|
210 } |
|
211 if ((match->iAscentInPixels == aMetrics.iAscentInPixels) |
|
212 && (match->iHeightInPixels == aMetrics.iHeightInPixels) |
|
213 && (match->iLeftAdjustInPixels == aMetrics.iLeftAdjustInPixels) |
|
214 && (match->iMoveInPixels == aMetrics.iMoveInPixels) |
|
215 && (match->iRightAdjustInPixels == aMetrics.iRightAdjustInPixels)) |
|
216 { |
|
217 same = true; |
|
218 break; |
|
219 } |
|
220 } |
|
221 if (!same) |
|
222 { |
|
223 i = -1; // not found |
|
224 } |
|
225 return i; |
|
226 } |
|
227 |
|
228 void MetricDistribution::Externalize(ostream& out) |
|
229 { |
|
230 streamoff idOffset = iStreamId; |
|
231 out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset)); |
|
232 int32 numMetrics = iCharacterMetricsList.Size(); |
|
233 out.write(reinterpret_cast<char*>(&numMetrics), sizeof(numMetrics)); |
|
234 } |
|
235 |
|
236 void MetricDistribution::ExternalizeComponents(ostream& out) |
|
237 { |
|
238 iStreamId = out.tellp(); |
|
239 iCharacterMetricsList.Externalize(out); |
|
240 } |
|
241 |
|
242 void Characters::Externalize(ostream& out) |
|
243 { |
|
244 iStreamId = out.tellp(); |
|
245 iBitmapOffsetList.Externalize(out); |
|
246 } |
|
247 |
|
248 Characters::~Characters() |
|
249 { |
|
250 iBitmapOffsetList.Destroy(); |
|
251 } |
|
252 |
|
253 ByteList::ByteList() |
|
254 : iString(), iOffset(0) |
|
255 { |
|
256 } |
|
257 |
|
258 void ByteList::AddBit(char aBit) |
|
259 { |
|
260 if (iOffset > 7) |
|
261 NewByte(); |
|
262 const char mask = 1; |
|
263 aBit = char(aBit & mask); |
|
264 char byte = char(aBit << iOffset); |
|
265 int index = iString.Length() - 1; |
|
266 iString[index] = char(iString[index] | byte); |
|
267 iOffset++; |
|
268 } |
|
269 |
|
270 void ByteList::NewByte() |
|
271 { |
|
272 char byte = 0; |
|
273 iString += byte; |
|
274 iOffset = 0; |
|
275 } |
|
276 |
|
277 int ByteList::Length() const |
|
278 { |
|
279 return iString.Length(); |
|
280 } |
|
281 |
|
282 void ByteList::Externalize(ostream& out) |
|
283 { |
|
284 int32 length = iString.Length(); |
|
285 out.write((char*) &length, sizeof(length)); |
|
286 out.write(iString.Text(), length); |
|
287 } |
|
288 |
|
289 void CharactersBitmap::Externalize(ostream& out) |
|
290 { |
|
291 iStreamId = out.tellp(); |
|
292 iByteList.Externalize(out); |
|
293 } |
|
294 |
|
295 void CharactersBitmap::AddIndex(int aIndex) |
|
296 {// Add index to metrics into the bitmap code section |
|
297 // Use 1 byte for most popular indices, 2 bytes otherwise |
|
298 int power; |
|
299 if (aIndex < KNumberOfPopularIndices) |
|
300 { |
|
301 iByteList.AddBit(0); |
|
302 power = KNumberOfBitsInByte - 1; |
|
303 } |
|
304 else |
|
305 { |
|
306 iByteList.AddBit(1); |
|
307 power = KNumberOfBitsInTwoBytes - 1; |
|
308 } |
|
309 |
|
310 char sigBit = 0; |
|
311 // Add significant bits of index. |
|
312 for(int bitToAdd = 0; bitToAdd < power; bitToAdd++) |
|
313 { |
|
314 sigBit = char(aIndex >> bitToAdd); |
|
315 iByteList.AddBit(sigBit); |
|
316 } |
|
317 } |
|
318 |
|
319 void BitmapCodeSection::Externalize(ostream& out) |
|
320 { |
|
321 out.write((char*) &iStart, sizeof(iStart)); |
|
322 out.write((char*) &iEnd, sizeof(iEnd)); |
|
323 streamoff idOffset = iCharacters.iStreamId; |
|
324 out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset)); |
|
325 idOffset = iCharactersBitmap.iStreamId; |
|
326 out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset)); |
|
327 } |
|
328 |
|
329 void BitmapCodeSection::ExternalizeComponents(ostream& out) |
|
330 { |
|
331 iCharacters.Externalize(out); |
|
332 iCharactersBitmap.Externalize(out); |
|
333 } |
|
334 |
|
335 FontBitmap::FontBitmap() |
|
336 : iPosture(PostureUpright), |
|
337 iStrokeWeight(StrokeWeightNormal), |
|
338 iIsProportional(efalse), |
|
339 iCellHeightInPixels(0), |
|
340 iAscentInPixels(0), |
|
341 iMaxCharWidthInPixels(0), |
|
342 iMaxNormalCharWidthInPixels(0), |
|
343 iBitmapEncoding(0) |
|
344 { |
|
345 iCharacterMetrics = MetricDistribution::New(); |
|
346 } |
|
347 |
|
348 void FontBitmap::Externalize(ostream& out) |
|
349 { |
|
350 iStreamId = out.tellp(); |
|
351 out.write((char*) &iUid, sizeof(iUid)); |
|
352 out.put((char) iPosture); |
|
353 out.put((char) iStrokeWeight); |
|
354 out.put((char) iIsProportional); |
|
355 out.write((char*) &iCellHeightInPixels, sizeof(iCellHeightInPixels)); |
|
356 out.write((char*) &iAscentInPixels, sizeof(iAscentInPixels)); |
|
357 out.write((char*) &iMaxCharWidthInPixels, sizeof(iMaxCharWidthInPixels)); |
|
358 out.write((char*) &iMaxNormalCharWidthInPixels, sizeof(iMaxNormalCharWidthInPixels)); |
|
359 out.write((char*) &iBitmapEncoding, sizeof(iBitmapEncoding)); |
|
360 iCharacterMetrics->Externalize(out); |
|
361 iCodeSectionList.Externalize(out); |
|
362 } |
|
363 |
|
364 void FontBitmap::ExternalizeComponents(ostream& out) |
|
365 { |
|
366 // write out characters and chactersbitmap records |
|
367 iCharacterMetrics->ExternalizeComponents(out); |
|
368 int size = iCodeSectionList.Size(); |
|
369 for (int i = 0; i < size; i++) |
|
370 { |
|
371 iCodeSectionList[i]->ExternalizeComponents(out); |
|
372 } |
|
373 } |
|
374 |
|
375 FontBitmap::~FontBitmap() |
|
376 { |
|
377 iCodeSectionList.Destroy(); |
|
378 delete iCharacterMetrics; |
|
379 } |
|
380 |
|
381 TypefaceFontBitmap::TypefaceFontBitmap(FontBitmap* aFontBitmap) |
|
382 : iFontBitmap(aFontBitmap), |
|
383 iFontBitmapUid(KNullUid), |
|
384 iWidthFactor(1), |
|
385 iHeightFactor(1) |
|
386 { |
|
387 } |
|
388 |
|
389 TypefaceFontBitmap::TypefaceFontBitmap(uid aFontBitmapUid) |
|
390 : iFontBitmap(NULL), |
|
391 iFontBitmapUid(aFontBitmapUid), |
|
392 iWidthFactor(1), |
|
393 iHeightFactor(1) |
|
394 { |
|
395 } |
|
396 |
|
397 void TypefaceFontBitmap::Externalize(ostream& out) |
|
398 { |
|
399 if (iFontBitmap) |
|
400 out.write((char*) &iFontBitmap->iUid, sizeof(iFontBitmap->iUid)); |
|
401 else |
|
402 out.write((char*) &iFontBitmapUid, sizeof(iFontBitmapUid)); |
|
403 out.write((char*) &iWidthFactor, sizeof(iWidthFactor)); |
|
404 out.write((char*) &iHeightFactor, sizeof(iHeightFactor)); |
|
405 } |
|
406 |
|
407 void FntTypeface::Externalize(ostream& out) |
|
408 { |
|
409 iStreamId = out.tellp(); |
|
410 Typeface::Externalize(out); |
|
411 iTypefaceFontBitmapList.Externalize(out); |
|
412 } |
|
413 |
|
414 FontStoreFile::FontStoreFile() |
|
415 : iCollectionUid(KNullUid), |
|
416 iKPixelAspectRatio(1000), |
|
417 iCopyrightInfo(), |
|
418 iDataStreamId(0) |
|
419 { |
|
420 } |
|
421 |
|
422 void FontStoreFile::AddTypeface(FntTypeface *aTypeface) |
|
423 { |
|
424 iTypefaceList.Add(aTypeface); |
|
425 for (int i = 0; i < aTypeface->iTypefaceFontBitmapList.Size(); i++) |
|
426 { |
|
427 if (aTypeface->iTypefaceFontBitmapList[i]->iFontBitmap) |
|
428 iFontBitmapList.Add(aTypeface->iTypefaceFontBitmapList[i]->iFontBitmap); |
|
429 } |
|
430 } |
|
431 |
|
432 void FontStoreFile::AddFontBitmap(FontBitmap* aFontBitmap) |
|
433 { |
|
434 iFontBitmapList.Add(aFontBitmap); |
|
435 } |
|
436 |
|
437 void FontStoreFile::Externalize(ostream& out) |
|
438 { |
|
439 ExternalizeHeader(out); |
|
440 ExternalizeComponents(out); |
|
441 } |
|
442 |
|
443 void FontStoreFile::ExternalizeHeader(ostream& out) |
|
444 { |
|
445 out.write((char*) &KStoreWriteOnceLayoutUid, sizeof(KStoreWriteOnceLayoutUid)); |
|
446 out.write((char*) &KFontStoreFileUid, sizeof(KFontStoreFileUid)); |
|
447 out.write((char*) &KNullUid, sizeof(KNullUid)); |
|
448 out.write((char*) &KFontStoreFileChecksum, sizeof(KFontStoreFileChecksum)); |
|
449 streamoff idOffset = iStreamId; |
|
450 out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset)); |
|
451 iStreamId = out.tellp(); |
|
452 out.write((char*) &KFnttranVersion, sizeof(KFnttranVersion)); |
|
453 out.write((char*) &iCollectionUid, sizeof(iCollectionUid)); |
|
454 out.write((char*) &iKPixelAspectRatio, sizeof(iKPixelAspectRatio)); |
|
455 idOffset = iDataStreamId; |
|
456 out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset)); |
|
457 iCopyrightInfo.Externalize(out); |
|
458 } |
|
459 |
|
460 void FontStoreFile::ExternalizeComponents(ostream& out) |
|
461 { |
|
462 iDataStreamId = out.tellp(); |
|
463 iFontBitmapList.Externalize(out); |
|
464 iTypefaceList.Externalize(out); |
|
465 iFontBitmapList.ExternalizeComponents(out); |
|
466 } |
|
467 |
|
468 boolean FontStore::Store(const String& aFilename) |
|
469 { |
|
470 boolean state = efalse; |
|
471 ofstream fout; |
|
472 String string = aFilename; |
|
473 fout.open(string.Text(), ios::binary); |
|
474 if (!fout.fail()) |
|
475 { |
|
476 iFontStoreFile->Externalize(fout); |
|
477 fout.close(); |
|
478 fout.open(string.Text(), ios::binary | ios::trunc); |
|
479 iFontStoreFile->Externalize(fout); |
|
480 fout.close(); |
|
481 state = etrue; |
|
482 } |
|
483 return state; |
|
484 } |
|
485 |
|
486 void FontStore::AddFontStoreFile(FontStoreFile* aFontStoreFile) |
|
487 { |
|
488 iFontStoreFile = aFontStoreFile; |
|
489 } |
|
490 |
|
491 void FontStore::AddFontBitmap(FontBitmap *aFontBitmap) |
|
492 { |
|
493 iFontBitmapList.Add(aFontBitmap); |
|
494 } |
|
495 |
|
496 Record* FontStore::FindFontBitmap(String& aLabel) |
|
497 { |
|
498 return iFontBitmapList.LabelToRecord(aLabel); |
|
499 } |
|
500 |
|
501 void FontStore::AddTypeface(FntTypeface *aTypeface) |
|
502 { |
|
503 iTypefaceList.Add(aTypeface); |
|
504 } |
|
505 |
|
506 Record* FontStore::FindTypeface(String& aLabel) |
|
507 { |
|
508 return iTypefaceList.LabelToRecord(aLabel); |
|
509 } |
|
510 |
|
511 FontStore::FontStore() |
|
512 : iFontStoreFile(NULL), |
|
513 iFontBitmapList(), |
|
514 iTypefaceList() |
|
515 { |
|
516 } |
|
517 |
|
518 FontStore::~FontStore() |
|
519 { |
|
520 delete iFontStoreFile; |
|
521 iFontBitmapList.Destroy(); |
|
522 iTypefaceList.Destroy(); |
|
523 } |