src/gui/text/qfontengine_s60.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
child 22 79de32ba3296
--- a/src/gui/text/qfontengine_s60.cpp	Tue Feb 02 00:43:10 2010 +0200
+++ b/src/gui/text/qfontengine_s60.cpp	Wed Mar 31 11:06:36 2010 +0300
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
@@ -44,8 +44,7 @@
 #include "qglobal.h"
 #include <private/qapplication_p.h>
 #include "qimage.h"
-#include "qt_s60_p.h"
-#include "qpixmap_s60_p.h"
+#include <private/qt_s60_p.h>
 
 #include <e32base.h>
 #include <e32std.h>
@@ -130,47 +129,62 @@
     return uc;
 }
 
+CFont *QFontEngineS60::fontWithSize(qreal size) const
+{
+    CFont *result = 0;
+    TFontSpec fontSpec(qt_QString2TPtrC(QFontEngine::fontDef.family), TInt(size));
+    fontSpec.iFontStyle.SetBitmapType(EAntiAliasedGlyphBitmap);
+    fontSpec.iFontStyle.SetPosture(QFontEngine::fontDef.style == QFont::StyleNormal?EPostureUpright:EPostureItalic);
+    fontSpec.iFontStyle.SetStrokeWeight(QFontEngine::fontDef.weight > QFont::Normal?EStrokeWeightBold:EStrokeWeightNormal);
+    const TInt errorCode = S60->screenDevice()->GetNearestFontToDesignHeightInPixels(result, fontSpec);
+    Q_ASSERT(result && (errorCode == 0));
+    return result;
+}
+
+void QFontEngineS60::setFontScale(qreal scale)
+{
+    if (qFuzzyCompare(scale, qreal(1))) {
+        if (!m_originalFont)
+            m_originalFont = fontWithSize(m_originalFontSizeInPixels);
+        m_activeFont = m_originalFont;
+    } else {
+        const qreal scaledFontSizeInPixels = m_originalFontSizeInPixels * scale;
+        if (!m_scaledFont ||
+                (TInt(scaledFontSizeInPixels) != TInt(m_scaledFontSizeInPixels))) {
+            releaseFont(m_scaledFont);
+            m_scaledFontSizeInPixels = scaledFontSizeInPixels;
+            m_scaledFont = fontWithSize(m_scaledFontSizeInPixels);
+        }
+        m_activeFont = m_scaledFont;
+    }
+}
+
+void QFontEngineS60::releaseFont(CFont *&font)
+{
+    if (font) {
+        S60->screenDevice()->ReleaseFont(font);
+        font = 0;
+    }
+}
+
 QFontEngineS60::QFontEngineS60(const QFontDef &request, const QFontEngineS60Extensions *extensions)
     : m_extensions(extensions)
+    , m_originalFont(0)
+    , m_originalFontSizeInPixels((request.pixelSize >= 0)?
+            request.pixelSize:pointsToPixels(request.pointSize))
+    , m_scaledFont(0)
+    , m_scaledFontSizeInPixels(0)
+    , m_activeFont(0)
 {
     QFontEngine::fontDef = request;
-    m_fontSizeInPixels = (request.pixelSize >= 0)?
-        request.pixelSize:pointsToPixels(request.pointSize);
-        
-    QSymbianFbsHeapLock lock(QSymbianFbsHeapLock::Unlock);
-    
-    m_textRenderBitmap = q_check_ptr(new CFbsBitmap());	// CBase derived object needs check on new
-    const TSize bitmapSize(1, 1); // It is just a dummy bitmap that I need to keep the font alive (or maybe not)
-    qt_symbian_throwIfError(m_textRenderBitmap->Create(bitmapSize, EGray256));
-    QT_TRAP_THROWING(m_textRenderBitmapDevice = CFbsBitmapDevice::NewL(m_textRenderBitmap));
-    qt_symbian_throwIfError(m_textRenderBitmapDevice->CreateContext(m_textRenderBitmapGc));
-    cache_cost = sizeof(QFontEngineS60) + bitmapSize.iHeight * bitmapSize.iWidth * 4;
-
-    TFontSpec fontSpec(qt_QString2TPtrC(request.family), m_fontSizeInPixels);
-    fontSpec.iFontStyle.SetBitmapType(EAntiAliasedGlyphBitmap);
-    fontSpec.iFontStyle.SetPosture(request.style == QFont::StyleNormal?EPostureUpright:EPostureItalic);
-    fontSpec.iFontStyle.SetStrokeWeight(request.weight > QFont::Normal?EStrokeWeightBold:EStrokeWeightNormal);
-    const TInt errorCode = m_textRenderBitmapDevice->GetNearestFontInPixels(m_font, fontSpec);
-    Q_ASSERT(errorCode == 0);
-    m_textRenderBitmapGc->UseFont(m_font);
-    
-    lock.relock();
+    setFontScale(1.0);
+    cache_cost = sizeof(QFontEngineS60);
 }
 
 QFontEngineS60::~QFontEngineS60()
 {
-    QSymbianFbsHeapLock lock(QSymbianFbsHeapLock::Unlock);
-    
-    m_textRenderBitmapGc->DiscardFont();
-    delete m_textRenderBitmapGc;
-    m_textRenderBitmapGc = NULL;
-    m_textRenderBitmapDevice->ReleaseFont(m_font);
-    delete m_textRenderBitmapDevice;
-    m_textRenderBitmapDevice = NULL;
-    delete m_textRenderBitmap;
-    m_textRenderBitmap = NULL;
-    
-    lock.relock();
+    releaseFont(m_originalFont);
+    releaseFont(m_scaledFont);
 }
 
 bool QFontEngineS60::stringToCMap(const QChar *characters, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
@@ -273,12 +287,12 @@
 
 QFixed QFontEngineS60::ascent() const
 {
-    return m_font->FontMaxAscent();
+    return m_originalFont->FontMaxAscent();
 }
 
 QFixed QFontEngineS60::descent() const
 {
-    return m_font->FontMaxDescent();
+    return m_originalFont->FontMaxDescent();
 }
 
 QFixed QFontEngineS60::leading() const
@@ -288,7 +302,7 @@
 
 qreal QFontEngineS60::maxCharWidth() const
 {
-    return m_font->MaxCharWidthInPixels();
+    return m_originalFont->MaxCharWidthInPixels();
 }
 
 const char *QFontEngineS60::name() const
@@ -324,11 +338,11 @@
     const TUint specialCode = (TUint)glyph | 0x80000000;
 
     const CFont::TCharacterDataAvailability availability =
-        m_font->GetCharacterData(specialCode, metrics, bitmap, bitmapSize);
+            m_activeFont->GetCharacterData(specialCode, metrics, bitmap, bitmapSize);
     const glyph_t fallbackGlyph = '?';
     if (availability != CFont::EAllCharacterData) {
         const CFont::TCharacterDataAvailability fallbackAvailability =
-            m_font->GetCharacterData(fallbackGlyph, metrics, bitmap, bitmapSize);
+                m_activeFont->GetCharacterData(fallbackGlyph, metrics, bitmap, bitmapSize);
         Q_ASSERT(fallbackAvailability == CFont::EAllCharacterData);
     }
 }