1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/graphics/graphicstools/bitmapfonttools/src/FNTRECRD.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,523 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Header FNTRECRD.CPP
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include "FNTRECRD.H"
1.24 +
1.25 +const int KNumberOfPopularIndices = 128;
1.26 +const int KNumberOfBitsInByte = 8;
1.27 +const int KNumberOfBitsInTwoBytes = 16;
1.28 +
1.29 +BitmapOffset::BitmapOffset(uint16 aBitmapOffset)
1.30 + : iBitmapOffset(aBitmapOffset)
1.31 + {}
1.32 +
1.33 +void BitmapOffset::Externalize(ostream& out)
1.34 + {
1.35 + out.write((char*) &iBitmapOffset,sizeof(iBitmapOffset));
1.36 + }
1.37 +
1.38 +CharacterMetrics::CharacterMetrics()
1.39 + : iAscentInPixels(0),
1.40 + iHeightInPixels(0),
1.41 + iLeftAdjustInPixels(0),
1.42 + iMoveInPixels(0),
1.43 + iRightAdjustInPixels(0)
1.44 + {
1.45 + }
1.46 +
1.47 +
1.48 +void CharacterMetrics::Externalize(ostream& out)
1.49 + {
1.50 + out.write((char*) &iAscentInPixels, sizeof(iAscentInPixels));
1.51 + out.write((char*) &iHeightInPixels, sizeof(iHeightInPixels));
1.52 + out.write((char*) &iLeftAdjustInPixels, sizeof(iLeftAdjustInPixels));
1.53 + out.write((char*) &iMoveInPixels, sizeof(iMoveInPixels));
1.54 + out.write((char*) &iRightAdjustInPixels, sizeof(iRightAdjustInPixels));
1.55 + }
1.56 +
1.57 +MetricDistributionMember::~MetricDistributionMember()
1.58 + {
1.59 + delete iMetric;
1.60 + }
1.61 +
1.62 +MetricDistributionMember::MetricDistributionMember()
1.63 + : iFrequency(0), iMetric(0)
1.64 + {}
1.65 +
1.66 +CharacterMetrics* MetricDistributionMember::Metric() const
1.67 + {
1.68 + return iMetric;
1.69 + }
1.70 +
1.71 +int MetricDistributionMember::Frequency() const
1.72 + {
1.73 + return iFrequency;
1.74 + }
1.75 +
1.76 +void MetricDistributionMember::SetFrequency(int aFrequency)
1.77 + {
1.78 + iFrequency = aFrequency;
1.79 + }
1.80 +
1.81 +void MetricDistributionMember::IncrementFrequency(int aIncrementBy)
1.82 + {
1.83 + iFrequency += aIncrementBy;
1.84 + }
1.85 +
1.86 +void MetricDistributionMember::SetMetric(CharacterMetrics* aMetric)
1.87 + {
1.88 + iMetric = aMetric;
1.89 + }
1.90 +
1.91 +void MetricDistributionMember::Externalize(ostream& out)
1.92 + {
1.93 + iMetric->Externalize(out);
1.94 + }
1.95 +
1.96 +MetricDistribution::~MetricDistribution()
1.97 + {
1.98 + iCharacterMetricsList.Destroy();
1.99 + }
1.100 +
1.101 +MetricDistribution* MetricDistribution::New()
1.102 + {
1.103 + return new MetricDistribution;
1.104 + }
1.105 +
1.106 +MetricDistribution::MetricDistribution()
1.107 + {}
1.108 +
1.109 +void MetricDistribution::SortMetricsByFrequency()
1.110 + { // Only need sort the most popular 128, since after this 2 bytes will always be used
1.111 + int maxIndex = iCharacterMetricsList.Size();
1.112 + if (maxIndex > KNumberOfPopularIndices)
1.113 + {
1.114 + maxIndex = KNumberOfPopularIndices;
1.115 + }
1.116 + for(int indexToSet = 0; indexToSet < maxIndex; indexToSet++)
1.117 + {
1.118 + const CharacterMetrics& mostPopularRemaining = MostPopular(indexToSet);
1.119 + SetIndex(mostPopularRemaining, indexToSet);
1.120 + }
1.121 + }
1.122 +
1.123 +void MetricDistribution::SetIndex(const CharacterMetrics& aMetrics, int aIndexToSet)
1.124 + {
1.125 + int currentPos = Index(aMetrics);
1.126 + if (currentPos != aIndexToSet)
1.127 + {
1.128 + MetricDistributionMember* match = iCharacterMetricsList[currentPos];
1.129 + MetricDistributionMember* swapPos = iCharacterMetricsList[aIndexToSet];
1.130 +
1.131 + CharacterMetrics* tempMet = match->Metric();
1.132 + const int tempFreq = match->Frequency();
1.133 +
1.134 + match->SetMetric(swapPos->Metric());
1.135 + match->SetFrequency(swapPos->Frequency());
1.136 + swapPos->SetMetric(tempMet);
1.137 + swapPos->SetFrequency(tempFreq);
1.138 + }
1.139 + }
1.140 +
1.141 +void MetricDistribution::AddOrIncrementMetric(const CharacterMetrics& aMetrics, int aFrequency)
1.142 + {
1.143 + boolean match = false;
1.144 + const CharacterMetrics* trial = NULL;
1.145 + MetricDistributionMember* link = NULL;
1.146 + int index;
1.147 + int maxIndex = iCharacterMetricsList.Size();
1.148 +
1.149 + for(index = 0; index < maxIndex && !match; index++)
1.150 + {
1.151 + link = iCharacterMetricsList[index];
1.152 + if (link)
1.153 + trial = link->Metric();
1.154 + if (trial && (trial->iAscentInPixels == aMetrics.iAscentInPixels)
1.155 + && (trial->iHeightInPixels == aMetrics.iHeightInPixels)
1.156 + && (trial->iLeftAdjustInPixels == aMetrics.iLeftAdjustInPixels)
1.157 + && (trial->iMoveInPixels == aMetrics.iMoveInPixels)
1.158 + && (trial->iRightAdjustInPixels == aMetrics.iRightAdjustInPixels))
1.159 + {
1.160 + match = true;
1.161 + }
1.162 + }
1.163 + if (match)
1.164 + {
1.165 + link->IncrementFrequency(aFrequency);
1.166 + }
1.167 + else
1.168 + {
1.169 + MetricDistributionMember* newLink = new MetricDistributionMember;
1.170 + newLink->IncrementFrequency(aFrequency);
1.171 + CharacterMetrics* newMetric = new CharacterMetrics(aMetrics);
1.172 + newLink->SetMetric(newMetric);
1.173 + iCharacterMetricsList.Add(newLink);
1.174 + }
1.175 + }
1.176 +
1.177 +const CharacterMetrics& MetricDistribution::MostPopular(int aStartIndex)
1.178 + {
1.179 + // finds the most popular metric above index aStartIndex. Allows for a fairly quick sort of the metircs to be done.
1.180 + MetricDistributionMember* link = NULL;
1.181 + const CharacterMetrics* mostPopular = NULL;
1.182 + int frequencyOfMostPopular = 0;
1.183 + int frequency = 0;
1.184 + int count;
1.185 + int total = 0; // for debugging
1.186 + const int size = iCharacterMetricsList.Size();
1.187 + for (count = aStartIndex; count < size; count++)
1.188 + {
1.189 + link = iCharacterMetricsList[count];
1.190 + frequency = link->Frequency();
1.191 + if (frequency>frequencyOfMostPopular)
1.192 + {
1.193 + mostPopular = link->Metric();
1.194 + frequencyOfMostPopular = frequency;
1.195 + }
1.196 + total += frequency;
1.197 + }
1.198 + return *mostPopular;
1.199 + }
1.200 +
1.201 +int MetricDistribution::Index(const CharacterMetrics& aMetrics)
1.202 + {
1.203 + boolean same = false;
1.204 + CharacterMetrics* match = NULL;
1.205 + int i;
1.206 + int size = iCharacterMetricsList.Size();
1.207 + // see if we have this one already
1.208 + for (i = 0; i < size; i++)
1.209 + {
1.210 + if (iCharacterMetricsList[i])
1.211 + {
1.212 + match = iCharacterMetricsList[i]->Metric();
1.213 + }
1.214 + if ((match->iAscentInPixels == aMetrics.iAscentInPixels)
1.215 + && (match->iHeightInPixels == aMetrics.iHeightInPixels)
1.216 + && (match->iLeftAdjustInPixels == aMetrics.iLeftAdjustInPixels)
1.217 + && (match->iMoveInPixels == aMetrics.iMoveInPixels)
1.218 + && (match->iRightAdjustInPixels == aMetrics.iRightAdjustInPixels))
1.219 + {
1.220 + same = true;
1.221 + break;
1.222 + }
1.223 + }
1.224 + if (!same)
1.225 + {
1.226 + i = -1; // not found
1.227 + }
1.228 + return i;
1.229 + }
1.230 +
1.231 +void MetricDistribution::Externalize(ostream& out)
1.232 + {
1.233 + streamoff idOffset = iStreamId;
1.234 + out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset));
1.235 + int32 numMetrics = iCharacterMetricsList.Size();
1.236 + out.write(reinterpret_cast<char*>(&numMetrics), sizeof(numMetrics));
1.237 + }
1.238 +
1.239 +void MetricDistribution::ExternalizeComponents(ostream& out)
1.240 + {
1.241 + iStreamId = out.tellp();
1.242 + iCharacterMetricsList.Externalize(out);
1.243 + }
1.244 +
1.245 +void Characters::Externalize(ostream& out)
1.246 + {
1.247 + iStreamId = out.tellp();
1.248 + iBitmapOffsetList.Externalize(out);
1.249 + }
1.250 +
1.251 +Characters::~Characters()
1.252 + {
1.253 + iBitmapOffsetList.Destroy();
1.254 + }
1.255 +
1.256 +ByteList::ByteList()
1.257 + : iString(), iOffset(0)
1.258 + {
1.259 + }
1.260 +
1.261 +void ByteList::AddBit(char aBit)
1.262 + {
1.263 + if (iOffset > 7)
1.264 + NewByte();
1.265 + const char mask = 1;
1.266 + aBit = char(aBit & mask);
1.267 + char byte = char(aBit << iOffset);
1.268 + int index = iString.Length() - 1;
1.269 + iString[index] = char(iString[index] | byte);
1.270 + iOffset++;
1.271 + }
1.272 +
1.273 +void ByteList::NewByte()
1.274 + {
1.275 + char byte = 0;
1.276 + iString += byte;
1.277 + iOffset = 0;
1.278 + }
1.279 +
1.280 +int ByteList::Length() const
1.281 + {
1.282 + return iString.Length();
1.283 + }
1.284 +
1.285 +void ByteList::Externalize(ostream& out)
1.286 + {
1.287 + int32 length = iString.Length();
1.288 + out.write((char*) &length, sizeof(length));
1.289 + out.write(iString.Text(), length);
1.290 + }
1.291 +
1.292 +void CharactersBitmap::Externalize(ostream& out)
1.293 + {
1.294 + iStreamId = out.tellp();
1.295 + iByteList.Externalize(out);
1.296 + }
1.297 +
1.298 +void CharactersBitmap::AddIndex(int aIndex)
1.299 + {// Add index to metrics into the bitmap code section
1.300 + // Use 1 byte for most popular indices, 2 bytes otherwise
1.301 + int power;
1.302 + if (aIndex < KNumberOfPopularIndices)
1.303 + {
1.304 + iByteList.AddBit(0);
1.305 + power = KNumberOfBitsInByte - 1;
1.306 + }
1.307 + else
1.308 + {
1.309 + iByteList.AddBit(1);
1.310 + power = KNumberOfBitsInTwoBytes - 1;
1.311 + }
1.312 +
1.313 + char sigBit = 0;
1.314 + // Add significant bits of index.
1.315 + for(int bitToAdd = 0; bitToAdd < power; bitToAdd++)
1.316 + {
1.317 + sigBit = char(aIndex >> bitToAdd);
1.318 + iByteList.AddBit(sigBit);
1.319 + }
1.320 + }
1.321 +
1.322 +void BitmapCodeSection::Externalize(ostream& out)
1.323 + {
1.324 + out.write((char*) &iStart, sizeof(iStart));
1.325 + out.write((char*) &iEnd, sizeof(iEnd));
1.326 + streamoff idOffset = iCharacters.iStreamId;
1.327 + out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset));
1.328 + idOffset = iCharactersBitmap.iStreamId;
1.329 + out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset));
1.330 + }
1.331 +
1.332 +void BitmapCodeSection::ExternalizeComponents(ostream& out)
1.333 + {
1.334 + iCharacters.Externalize(out);
1.335 + iCharactersBitmap.Externalize(out);
1.336 + }
1.337 +
1.338 +FontBitmap::FontBitmap()
1.339 + : iPosture(PostureUpright),
1.340 + iStrokeWeight(StrokeWeightNormal),
1.341 + iIsProportional(efalse),
1.342 + iCellHeightInPixels(0),
1.343 + iAscentInPixels(0),
1.344 + iMaxCharWidthInPixels(0),
1.345 + iMaxNormalCharWidthInPixels(0),
1.346 + iBitmapEncoding(0)
1.347 + {
1.348 + iCharacterMetrics = MetricDistribution::New();
1.349 + }
1.350 +
1.351 +void FontBitmap::Externalize(ostream& out)
1.352 + {
1.353 + iStreamId = out.tellp();
1.354 + out.write((char*) &iUid, sizeof(iUid));
1.355 + out.put((char) iPosture);
1.356 + out.put((char) iStrokeWeight);
1.357 + out.put((char) iIsProportional);
1.358 + out.write((char*) &iCellHeightInPixels, sizeof(iCellHeightInPixels));
1.359 + out.write((char*) &iAscentInPixels, sizeof(iAscentInPixels));
1.360 + out.write((char*) &iMaxCharWidthInPixels, sizeof(iMaxCharWidthInPixels));
1.361 + out.write((char*) &iMaxNormalCharWidthInPixels, sizeof(iMaxNormalCharWidthInPixels));
1.362 + out.write((char*) &iBitmapEncoding, sizeof(iBitmapEncoding));
1.363 + iCharacterMetrics->Externalize(out);
1.364 + iCodeSectionList.Externalize(out);
1.365 + }
1.366 +
1.367 +void FontBitmap::ExternalizeComponents(ostream& out)
1.368 + {
1.369 + // write out characters and chactersbitmap records
1.370 + iCharacterMetrics->ExternalizeComponents(out);
1.371 + int size = iCodeSectionList.Size();
1.372 + for (int i = 0; i < size; i++)
1.373 + {
1.374 + iCodeSectionList[i]->ExternalizeComponents(out);
1.375 + }
1.376 + }
1.377 +
1.378 +FontBitmap::~FontBitmap()
1.379 + {
1.380 + iCodeSectionList.Destroy();
1.381 + delete iCharacterMetrics;
1.382 + }
1.383 +
1.384 +TypefaceFontBitmap::TypefaceFontBitmap(FontBitmap* aFontBitmap)
1.385 + : iFontBitmap(aFontBitmap),
1.386 + iFontBitmapUid(KNullUid),
1.387 + iWidthFactor(1),
1.388 + iHeightFactor(1)
1.389 + {
1.390 + }
1.391 +
1.392 +TypefaceFontBitmap::TypefaceFontBitmap(uid aFontBitmapUid)
1.393 + : iFontBitmap(NULL),
1.394 + iFontBitmapUid(aFontBitmapUid),
1.395 + iWidthFactor(1),
1.396 + iHeightFactor(1)
1.397 + {
1.398 + }
1.399 +
1.400 +void TypefaceFontBitmap::Externalize(ostream& out)
1.401 + {
1.402 + if (iFontBitmap)
1.403 + out.write((char*) &iFontBitmap->iUid, sizeof(iFontBitmap->iUid));
1.404 + else
1.405 + out.write((char*) &iFontBitmapUid, sizeof(iFontBitmapUid));
1.406 + out.write((char*) &iWidthFactor, sizeof(iWidthFactor));
1.407 + out.write((char*) &iHeightFactor, sizeof(iHeightFactor));
1.408 + }
1.409 +
1.410 +void FntTypeface::Externalize(ostream& out)
1.411 + {
1.412 + iStreamId = out.tellp();
1.413 + Typeface::Externalize(out);
1.414 + iTypefaceFontBitmapList.Externalize(out);
1.415 + }
1.416 +
1.417 +FontStoreFile::FontStoreFile()
1.418 + : iCollectionUid(KNullUid),
1.419 + iKPixelAspectRatio(1000),
1.420 + iCopyrightInfo(),
1.421 + iDataStreamId(0)
1.422 + {
1.423 + }
1.424 +
1.425 +void FontStoreFile::AddTypeface(FntTypeface *aTypeface)
1.426 + {
1.427 + iTypefaceList.Add(aTypeface);
1.428 + for (int i = 0; i < aTypeface->iTypefaceFontBitmapList.Size(); i++)
1.429 + {
1.430 + if (aTypeface->iTypefaceFontBitmapList[i]->iFontBitmap)
1.431 + iFontBitmapList.Add(aTypeface->iTypefaceFontBitmapList[i]->iFontBitmap);
1.432 + }
1.433 + }
1.434 +
1.435 +void FontStoreFile::AddFontBitmap(FontBitmap* aFontBitmap)
1.436 + {
1.437 + iFontBitmapList.Add(aFontBitmap);
1.438 + }
1.439 +
1.440 +void FontStoreFile::Externalize(ostream& out)
1.441 + {
1.442 + ExternalizeHeader(out);
1.443 + ExternalizeComponents(out);
1.444 + }
1.445 +
1.446 +void FontStoreFile::ExternalizeHeader(ostream& out)
1.447 + {
1.448 + out.write((char*) &KStoreWriteOnceLayoutUid, sizeof(KStoreWriteOnceLayoutUid));
1.449 + out.write((char*) &KFontStoreFileUid, sizeof(KFontStoreFileUid));
1.450 + out.write((char*) &KNullUid, sizeof(KNullUid));
1.451 + out.write((char*) &KFontStoreFileChecksum, sizeof(KFontStoreFileChecksum));
1.452 + streamoff idOffset = iStreamId;
1.453 + out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset));
1.454 + iStreamId = out.tellp();
1.455 + out.write((char*) &KFnttranVersion, sizeof(KFnttranVersion));
1.456 + out.write((char*) &iCollectionUid, sizeof(iCollectionUid));
1.457 + out.write((char*) &iKPixelAspectRatio, sizeof(iKPixelAspectRatio));
1.458 + idOffset = iDataStreamId;
1.459 + out.write(reinterpret_cast<char*>(&idOffset), sizeof(idOffset));
1.460 + iCopyrightInfo.Externalize(out);
1.461 + }
1.462 +
1.463 +void FontStoreFile::ExternalizeComponents(ostream& out)
1.464 + {
1.465 + iDataStreamId = out.tellp();
1.466 + iFontBitmapList.Externalize(out);
1.467 + iTypefaceList.Externalize(out);
1.468 + iFontBitmapList.ExternalizeComponents(out);
1.469 + }
1.470 +
1.471 +boolean FontStore::Store(const String& aFilename)
1.472 + {
1.473 + boolean state = efalse;
1.474 + ofstream fout;
1.475 + String string = aFilename;
1.476 + fout.open(string.Text(), ios::binary);
1.477 + if (!fout.fail())
1.478 + {
1.479 + iFontStoreFile->Externalize(fout);
1.480 + fout.close();
1.481 + fout.open(string.Text(), ios::binary | ios::trunc);
1.482 + iFontStoreFile->Externalize(fout);
1.483 + fout.close();
1.484 + state = etrue;
1.485 + }
1.486 + return state;
1.487 + }
1.488 +
1.489 +void FontStore::AddFontStoreFile(FontStoreFile* aFontStoreFile)
1.490 + {
1.491 + iFontStoreFile = aFontStoreFile;
1.492 + }
1.493 +
1.494 +void FontStore::AddFontBitmap(FontBitmap *aFontBitmap)
1.495 + {
1.496 + iFontBitmapList.Add(aFontBitmap);
1.497 + }
1.498 +
1.499 +Record* FontStore::FindFontBitmap(String& aLabel)
1.500 + {
1.501 + return iFontBitmapList.LabelToRecord(aLabel);
1.502 + }
1.503 +
1.504 +void FontStore::AddTypeface(FntTypeface *aTypeface)
1.505 + {
1.506 + iTypefaceList.Add(aTypeface);
1.507 + }
1.508 +
1.509 +Record* FontStore::FindTypeface(String& aLabel)
1.510 + {
1.511 + return iTypefaceList.LabelToRecord(aLabel);
1.512 + }
1.513 +
1.514 +FontStore::FontStore()
1.515 + : iFontStoreFile(NULL),
1.516 + iFontBitmapList(),
1.517 + iTypefaceList()
1.518 + {
1.519 + }
1.520 +
1.521 +FontStore::~FontStore()
1.522 + {
1.523 + delete iFontStoreFile;
1.524 + iFontBitmapList.Destroy();
1.525 + iTypefaceList.Destroy();
1.526 + }