Update contrib.
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 EXPORT_C CPdrResources::CPdrResources():
26 __DECLARE_NAME(_S("CPdrResources"));
29 EXPORT_C void CPdrResources::RestoreL(CStreamStore& aStore, TStreamId aStreamId)
31 RStoreReadStream stream;
32 stream.OpenLC(aStore, aStreamId);
33 iNumResources = stream.ReadInt32L();
34 iResourceList = new(ELeave) TPdrResource[iNumResources];
35 for (TInt i = 0; i < iNumResources; i++)
36 iResourceList[i].InternalizeL(stream);
37 CleanupStack::PopAndDestroy();
40 EXPORT_C CPdrResources::~CPdrResources()
42 delete[] iResourceList;
47 EXPORT_C TPtrC8 CPdrResources::ResourceString(TInt anId) const
50 TPdrResource* pEnd = iResourceList + iNumResources;
52 for( p = iResourceList; (p < pEnd) && (p->iId != anId); p++)
60 CInfoFont::CInfoFont(TInt aBaselineOffsetInPixels, const TFontSpec& aFontSpecInTwips, TInt aFontInfoHeightInTwips, TInt aHeightInPixels, CPdrTranslates* aTranslates, const TDesC8& aCommandString, CPdrDevice* aPdrDevice):
62 iCommandString(aCommandString),
63 iBaselineOffsetInPixels(aBaselineOffsetInPixels),
64 iFontSpecInTwips(aFontSpecInTwips),
65 iFontInfoHeightInTwips(aFontInfoHeightInTwips),
66 iHeightInPixels(aHeightInPixels),
68 iTranslates(aTranslates),
69 iPdrDevice(aPdrDevice),
72 CreateBandedFontIfRequired();
75 CInfoFont::~CInfoFont()
78 if (iPdrDevice->iControl)
79 ((CPdrControl*)(iPdrDevice->iControl))->BandedDevice()->ReleaseFont(iRealFont);
82 void CInfoFont::CreateBandedFontIfRequired()
86 if (iPdrDevice->iControl)
88 if (((CPdrControl*)(iPdrDevice->iControl))->BandedDevice())
89 ((CPdrControl*)(iPdrDevice->iControl))->BandedDevice()->GetNearestFontToDesignHeightInTwips(iRealFont, iFontSpecInTwips);
94 EXPORT_C TUid CInfoFont::DoTypeUid() const
96 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
97 return TUid::Uid(KCInfoFontUidVal);
100 EXPORT_C TInt CInfoFont::DoHeightInPixels() const
102 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
103 return iHeightInPixels;
106 EXPORT_C TInt CInfoFont::DoAscentInPixels() const
108 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
109 return Height(iFontInfo->iAscentInPixels);
112 EXPORT_C TInt CInfoFont::DoCharWidthInPixels(TChar aChar) const
115 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
116 if (RepertoireContains(aChar))
118 width = iFontInfo->CharWidthInPixels(TUint(aChar));
119 width = Width(width);
124 width = iRealFont->CharWidthInPixels(TUint(aChar));
131 EXPORT_C TInt CInfoFont::DoTextWidthInPixels(const TDesC &aText) const
133 TMeasureTextOutput output;
134 TInt advance_width = MeasureText(aText,NULL,&output);
135 return Max(advance_width,output.iBounds.Width());
138 EXPORT_C TInt CInfoFont::DoBaselineOffsetInPixels() const
140 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
141 return iBaselineOffsetInPixels;
144 EXPORT_C TInt CInfoFont::DoTextCount(const TDesC &aText, TInt aWidthInPixels) const
148 TInt length = aText.Length();
149 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
150 for (count = 0; (count < length) && ((width += MeasureText(aText.Mid(count, 1))) < aWidthInPixels); )
157 EXPORT_C TInt CInfoFont::DoTextCount(const TDesC &aText, TInt aWidthInPixels, TInt &aExcessWidthInPixels) const
159 TInt count = TextCount(aText, aWidthInPixels);
160 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
161 aExcessWidthInPixels = aWidthInPixels - MeasureText(aText.Left(count));
165 EXPORT_C TInt CInfoFont::DoMaxCharWidthInPixels() const
167 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
168 TInt width = Width(iFontInfo->iMaxCharWidthInPixels);
170 return Max(iRealFont->MaxCharWidthInPixels(),width);
174 EXPORT_C TInt CInfoFont::DoMaxNormalCharWidthInPixels() const
176 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
177 TInt width = Width(iFontInfo->iMaxNormalCharWidthInPixels);
179 return Max(iRealFont->MaxNormalCharWidthInPixels(),width);
183 EXPORT_C TFontSpec CInfoFont::DoFontSpecInTwips() const
185 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
186 return iFontSpecInTwips;
189 EXPORT_C HBufC8* CInfoFont::TranslateStringL(const TDesC& aString) const
191 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
192 return iTranslates->TranslateStringL(aString);
195 EXPORT_C TPtrC8 CInfoFont::CommandString() const
198 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
199 ptr.Set(iCommandString);
203 EXPORT_C TBool CInfoFont::RepertoireContains(TChar aChar) const
205 CFontInfo* fontInfo = FontInfo();
206 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
207 for (TInt i = 0; i < fontInfo->NumCodeSections(); i++)
209 if (((TInt)(TUint)aChar >= fontInfo->CodeSection(i).iStart) && ((TInt)(TUint)aChar<= fontInfo->CodeSection(i).iEnd))
215 EXPORT_C TBool CInfoFont::AllCharsInFontRepertoire(const TDesC& aString, TInt& aFirstCharNotInRepertoire, TInt& aLength) const
217 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
218 for (aFirstCharNotInRepertoire = 0; aFirstCharNotInRepertoire < aString.Length(); aFirstCharNotInRepertoire++)
219 if (!(RepertoireContains(aString[aFirstCharNotInRepertoire])))
221 if (aFirstCharNotInRepertoire == 0)
223 for (aLength = aFirstCharNotInRepertoire; aLength < aString.Length(); aLength++)
224 if ((RepertoireContains(aString[aLength])))
232 CFont* CInfoFont::RealFont() const
234 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
238 TInt CInfoFont::Width(TInt aNum) const
240 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
241 return (aNum * iFontSpecInTwips.iHeight + (iFontInfoHeightInTwips / 2)) / iFontInfoHeightInTwips;
244 TInt CInfoFont::Height(TInt aNum) const
246 CONST_CAST(CInfoFont*,this)->CreateBandedFontIfRequired();
247 return (aNum * iFontSpecInTwips.iHeight + (iFontInfoHeightInTwips / 2)) / iFontInfoHeightInTwips;
250 EXPORT_C void TTypefaceFontsEntry::InternalizeL(RReadStream& aStream)
252 aStream >> iStreamId;
253 iNotInPortrait = aStream.ReadUint8L();
254 iNotInLandscape = aStream.ReadUint8L();
257 EXPORT_C CPdrModelInfo::CPdrModelInfo():
259 iKPixelWidthInTwips(0),
260 iKPixelHeightInTwips(0),
261 iPortraitOffsetInPixels(),
262 iLandscapeOffsetInPixels(),
263 // iMinMarginsInPixels(),
264 iDisplayMode(EGray2),
265 iNumTypefaceFonts(0),
266 iTypefaceFontsEntryList(NULL),
267 iResourcesStreamId(KNullStreamId),
268 iSpareStreamId(KNullStreamId)
270 __DECLARE_NAME(_S("CPdrModelInfo"));
273 EXPORT_C CPdrModelInfo::~CPdrModelInfo()
275 delete[] iTypefaceFontsEntryList;
278 EXPORT_C void CPdrModelInfo::InternalizeL(RReadStream& aStream)
280 TInt pdrtranversion = aStream.ReadInt32L();
281 if (pdrtranversion < KPdrtranVersion)
282 User::Leave(KErrNotSupported);
283 iFlags = aStream.ReadInt32L();
284 iKPixelWidthInTwips = aStream.ReadInt32L();
285 iKPixelHeightInTwips = aStream.ReadInt32L();
286 iPortraitOffsetInPixels.iX = aStream.ReadInt32L();
287 iPortraitOffsetInPixels.iY = aStream.ReadInt32L();
288 iLandscapeOffsetInPixels.iX = aStream.ReadInt32L();
289 iLandscapeOffsetInPixels.iY = aStream.ReadInt32L();
290 iMinMarginsInPixels.InternalizeL(aStream);
291 iDisplayMode = (TDisplayMode)aStream.ReadInt32L();
292 iNumTypefaceFonts = aStream.ReadInt32L();
293 iTypefaceFontsEntryList = new(ELeave) TTypefaceFontsEntry[iNumTypefaceFonts];
294 TTypefaceFontsEntry* pEnd = iTypefaceFontsEntryList+iNumTypefaceFonts;
295 for(TTypefaceFontsEntry* p = iTypefaceFontsEntryList; p < pEnd; p++)
296 p->InternalizeL(aStream);
297 aStream >> iResourcesStreamId;
298 aStream >> iSpareStreamId;
301 CPdrTypefaceStore::CPdrTypefaceStore(CStreamStore& aStore, TInt aKPixelHeightInTwips, CPdrDevice* aPdrDevice):
302 iPdrDevice(aPdrDevice),
304 iKPixelHeightInTwips(aKPixelHeightInTwips)
308 void CPdrTypefaceStore::ConstructL(TInt aNumTypefaceFonts, TTypefaceFontsEntry* aTypefaceFontsEntryList, TPageSpec::TPageOrientation aPageOrientation)
310 CTypefaceStore::ConstructL();
311 iTranslatesList = new(ELeave) CArrayPtrFlat<CPdrTranslates>(8);
312 iTypefaceFontsList = new(ELeave) CArrayPtrFlat<CTypefaceFonts>(8);
313 iPortraitTypefaceFontsList = new(ELeave) CArrayPtrFlat<CTypefaceFonts>(8);
314 iLandscapeTypefaceFontsList = new(ELeave) CArrayPtrFlat<CTypefaceFonts>(8);
315 iFontInfoList = new(ELeave) CArrayPtrFlat<CFontInfo>(8);
316 for (TInt i = 0; i < aNumTypefaceFonts; i++)
318 CTypefaceFonts* typefacefonts = new(ELeave) CTypefaceFonts;
319 CleanupStack::PushL(typefacefonts);
320 RStoreReadStream stream;
321 stream.OpenLC(*iStore, aTypefaceFontsEntryList[i].iStreamId);
322 typefacefonts->InternalizeL(stream);
323 CleanupStack::PopAndDestroy();
324 iTypefaceFontsList->AppendL(typefacefonts);
326 typefacefonts->iTranslates=TranslatesL(typefacefonts->iTranslates.AsId());
327 if (!aTypefaceFontsEntryList[i].iNotInPortrait)
328 iPortraitTypefaceFontsList->AppendL(typefacefonts);
329 if (!aTypefaceFontsEntryList[i].iNotInLandscape)
330 iLandscapeTypefaceFontsList->AppendL(typefacefonts);
332 SetPageOrientation(aPageOrientation);
335 EXPORT_C CPdrTypefaceStore* CPdrTypefaceStore::NewL(CStreamStore& aStore, TInt aNumTypefacesFonts, TTypefaceFontsEntry* aTypefaceFontsEntryList, TPageSpec::TPageOrientation aPageOrientation, TInt aKPixelHeightInTwips, CPdrDevice* aPdrDevice)
337 CPdrTypefaceStore* typefacestore = new(ELeave) CPdrTypefaceStore(aStore, aKPixelHeightInTwips, aPdrDevice);
338 CleanupStack::PushL(typefacestore);
339 typefacestore->ConstructL(aNumTypefacesFonts, aTypefaceFontsEntryList, aPageOrientation);
341 return typefacestore;
344 EXPORT_C CPdrTypefaceStore::~CPdrTypefaceStore()
347 iTranslatesList->ResetAndDestroy();
348 delete iTranslatesList;
349 if (iTypefaceFontsList)
350 iTypefaceFontsList->ResetAndDestroy();
351 delete iTypefaceFontsList;
352 delete iPortraitTypefaceFontsList;
353 delete iLandscapeTypefaceFontsList;
355 iFontInfoList->ResetAndDestroy();
356 delete iFontInfoList;
362 EXPORT_C TInt CPdrTypefaceStore::GetNearestFontInTwips(CFont*& aFont, const TFontSpec& aFontSpec)
364 return GetNearestFontToDesignHeightInTwips(aFont, aFontSpec);
370 EXPORT_C TInt CPdrTypefaceStore::GetNearestFontToDesignHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec)
373 const TInt count = iCurrentTypefaceFontsList->Count();
377 TTypeface typeface = aFontSpec.iTypeface;
378 for (index = 0; (index < count) && typeface.iName.CompareF((*iCurrentTypefaceFontsList)[index]->Typeface().iName); index++)
379 { // tries matching typeface name
383 if (!typeface.IsSymbol())
385 for (index = 0; (index < count) && (((*iCurrentTypefaceFontsList)[index]->Typeface().IsSymbol()) ||
386 (typeface.IsProportional() != (*iCurrentTypefaceFontsList)[index]->Typeface().IsProportional()) ||
387 (typeface.IsSerif() != (*iCurrentTypefaceFontsList)[index]->Typeface().IsSerif())); index++)
388 { // tries matching typeface flags
391 for (index = 0; (index < count) && (((*iCurrentTypefaceFontsList)[index]->Typeface().IsSymbol()) ||
392 (typeface.IsProportional() != (*iCurrentTypefaceFontsList)[index]->Typeface().IsProportional())); index++)
393 { // tries matching typeface flag
396 for (index = 0; (index < count) && ((*iCurrentTypefaceFontsList)[index]->Typeface().IsSymbol()); index++)
397 { // tries matching typeface flag
402 for (index = 0; (index < count) && (!(*iCurrentTypefaceFontsList)[index]->Typeface().IsSymbol()); index++)
403 { // finds first symbol typeface
409 CTypefaceFonts* typefacefonts = (*iCurrentTypefaceFontsList)[index];
410 if (typefacefonts->NumFontHeights())
412 TFontSpec fontspec(aFontSpec);
413 fontspec.iTypeface = typefacefonts->Typeface();
414 TInt i = GetNearestFontHeightIndex(index, aFontSpec.iHeight);
415 fontspec.iHeight = typefacefonts->FontHeightInTwips(i);
416 if (fontspec.iFontStyle.PrintPosition() != EPrintPosNormal)
418 i = GetNearestFontHeightIndex(index, SuperSubHeight(fontspec.iHeight, fontspec.iFontStyle.PrintPosition()));
420 fontspec.iFontStyle = GetNearestFontStyle(index, i, fontspec.iFontStyle);
421 TInt heightintwips = typefacefonts->FontHeightInTwips(i);
422 TInt height = VerticalTwipsToPixels(heightintwips);
423 if (IsFontLoaded(aFont, fontspec, height))
425 TInt baselineoffset = BaselineOffset(VerticalTwipsToPixels(fontspec.iHeight), fontspec.iFontStyle.PrintPosition());
426 TInt fontinfoheight = ((fontspec.iHeight * typefacefonts->FontInfoHeightInTwips(i) + (heightintwips / 2))) / heightintwips;
427 CPdrTranslates* translates = typefacefonts->iTranslates;
428 TCommandString commandstring;
429 typefacefonts->CommandString(commandstring, i);
430 TStreamId fontinfostreamid = typefacefonts->Style(i, fontspec.iFontStyle)->iFontInfoStreamId;
431 TRAPD(ret, aFont = NewFontL(baselineoffset, fontspec, fontinfoheight, height, translates, commandstring, fontinfostreamid));
441 EXPORT_C TInt CPdrTypefaceStore::GetNearestFontToMaxHeightInTwips(CFont*& /*aFont*/, const TFontSpec& /*aFontSpec*/, TInt /* aMaxHeight */)
443 return KErrNotSupported;
446 EXPORT_C TInt CPdrTypefaceStore::NumTypefaces() const
448 return iCurrentTypefaceFontsList->Count();
451 EXPORT_C TInt CPdrTypefaceStore::FontHeightInTwips(TInt aTypefaceIndex, TInt aHeightIndex) const
454 __ASSERT_DEBUG((aTypefaceIndex >= 0) && (aTypefaceIndex < NumTypefaces()), Panic(EPdrHeightIndexOutOfRange));
455 CTypefaceFonts* typefacefonts = (*iCurrentTypefaceFontsList)[aTypefaceIndex];
456 height = typefacefonts->FontHeightInTwips(aHeightIndex);
460 EXPORT_C void CPdrTypefaceStore::TypefaceSupport(TTypefaceSupport &aTypefaceSupport, TInt aTypefaceIndex) const
462 __ASSERT_DEBUG((aTypefaceIndex >= 0) && (aTypefaceIndex < NumTypefaces()), Panic(EPdrHeightIndexOutOfRange));
463 CTypefaceFonts* typefacefonts = (*iCurrentTypefaceFontsList)[aTypefaceIndex];
464 aTypefaceSupport.iTypeface = typefacefonts->Typeface();
465 aTypefaceSupport.iIsScalable = typefacefonts->IsScalable();
466 aTypefaceSupport.iNumHeights = typefacefonts->NumFontHeights();
467 aTypefaceSupport.iMinHeightInTwips = FontHeightInTwips(aTypefaceIndex, 0); // Font heights must be in ascending order
468 aTypefaceSupport.iMaxHeightInTwips = FontHeightInTwips(aTypefaceIndex, aTypefaceSupport.iNumHeights - 1);
471 EXPORT_C void CPdrTypefaceStore::SetPageOrientation(TPageSpec::TPageOrientation aPageOrientation)
473 if (aPageOrientation == TPageSpec::EPortrait)
474 iCurrentTypefaceFontsList = iPortraitTypefaceFontsList;
476 iCurrentTypefaceFontsList = iLandscapeTypefaceFontsList;
479 CFontInfo* CPdrTypefaceStore::FontInfoL(TStreamId aStreamId) const
483 const TInt count = iFontInfoList->Count();
484 for (i = 0; (i < count) && ((*iFontInfoList)[i]->iStreamId != aStreamId); i++)
485 { // Searches for FontInfo with same Id
487 if (i < count) // Found
488 fontinfo = (*iFontInfoList)[i];
491 RStoreReadStream stream;
492 fontinfo = new(ELeave) CFontInfo(aStreamId);
493 CleanupStack::PushL(fontinfo);
494 stream.OpenLC(*iStore, aStreamId);
495 fontinfo->InternalizeL(stream);
496 CleanupStack::PopAndDestroy();
497 iFontInfoList->AppendL(fontinfo);
503 CPdrTranslates* CPdrTypefaceStore::TranslatesL(TStreamId aStreamId) const
505 CPdrTranslates* translates;
507 const TInt count = iTranslatesList->Count();
508 for (i = 0; (i < count) && ((*iTranslatesList)[i]->iStreamId != aStreamId); i++)
509 { // Searches for Translate with same Id
511 if (i < count) // Found
512 translates = (*iTranslatesList)[i];
515 RStoreReadStream stream;
516 translates = new(ELeave) CPdrTranslates;
517 CleanupStack::PushL(translates);
518 translates->iStreamId = aStreamId;
519 stream.OpenLC(*iStore, aStreamId);
520 translates->InternalizeL(stream);
521 CleanupStack::PopAndDestroy();
522 iTranslatesList->AppendL(translates);
528 TInt CPdrTypefaceStore::GetNearestFontHeightIndex(TInt aTypefaceIndex, TInt aHeightInTwips) const
530 CTypefaceFonts* typefacefonts = (*iCurrentTypefaceFontsList)[aTypefaceIndex];
532 TInt size = typefacefonts->NumFontHeights();
533 for (i = size - 1; (i > 0) && (aHeightInTwips < typefacefonts->FontHeightInTwips(i)); i--)
534 { // Finds fontheight less than or equal to fontspec height
539 TFontStyle CPdrTypefaceStore::GetNearestFontStyle(TInt aTypefaceIndex, TInt aHeightIndex, const TFontStyle& aFontStyle) const
541 TFontStyle fontstyle(aFontStyle);
542 CTypefaceFonts* typefacefonts = (*iCurrentTypefaceFontsList)[aTypefaceIndex];
543 while (!typefacefonts->Style(aHeightIndex, fontstyle)->iIsAvailable)
544 { // finds first available style
545 if ((fontstyle.StrokeWeight() == EStrokeWeightBold) && (fontstyle.Posture() == EPostureItalic))
546 fontstyle.SetPosture(EPostureUpright);
549 fontstyle.SetPosture(EPostureUpright);
550 fontstyle.SetStrokeWeight(EStrokeWeightNormal);
556 TBool CPdrTypefaceStore::IsFontLoaded(CFont*& aFont, const TFontSpec& aFontSpecInTwips, TInt aHeightInPixels) const
558 @see CFbsTypefaceStore::IsFontLoaded
559 @see CFontStore::IsFontLoaded
563 const TInt count = iFontAccess->Count();
565 !(aHeightInPixels == (*iFontAccess)[i].iFont->HeightInPixels() &&
566 aFontSpecInTwips == (*iFontAccess)[i].iFont->FontSpecInTwips()); i++)
571 aFont = (*iFontAccess)[i].iFont;
572 (*iFontAccess)[i].iAccessCount++;
578 CInfoFont* CPdrTypefaceStore::NewFontL(TInt aBaselineOffsetInPixels, const TFontSpec& aFontSpecInTwips, TInt aFontInfoHeightInTwips, TInt aHeightInPixels, CPdrTranslates* aTranslates, const TDesC8& aCommandString, TStreamId aFontInfoStreamId)
580 CInfoFont* infofont = new(ELeave) CInfoFont(aBaselineOffsetInPixels, aFontSpecInTwips, aFontInfoHeightInTwips, aHeightInPixels, aTranslates, aCommandString, iPdrDevice);
581 CleanupStack::PushL(infofont);
584 TRAPD(ret, infofont->iFontInfo = FontInfoL(aFontInfoStreamId));
587 ReleaseFont(infofont);
593 TInt CPdrTypefaceStore::VerticalTwipsToPixels(TInt aTwipsHeight) const
595 return (1000 * aTwipsHeight + (iKPixelHeightInTwips / 2)) / iKPixelHeightInTwips;