src/gui/text/qfontengine_s60.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qfontengine_s60_p.h"
       
    43 #include "qtextengine_p.h"
       
    44 #include "qglobal.h"
       
    45 #include <private/qapplication_p.h>
       
    46 #include "qimage.h"
       
    47 #include "qt_s60_p.h"
       
    48 #include "qpixmap_s60_p.h"
       
    49 
       
    50 #include <e32base.h>
       
    51 #include <e32std.h>
       
    52 #include <EIKENV.H>
       
    53 #include <GDI.H>
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 QFontEngineS60Extensions::QFontEngineS60Extensions(CFont* fontOwner, COpenFont *font)
       
    58     : m_font(font)
       
    59     , m_cmap(0)
       
    60     , m_symbolCMap(false)
       
    61     , m_fontOwner(fontOwner)
       
    62 {
       
    63     TAny *shapingExtension = NULL;
       
    64     m_font->ExtendedInterface(KUidOpenFontShapingExtension, shapingExtension);
       
    65     m_shapingExtension = static_cast<MOpenFontShapingExtension*>(shapingExtension);
       
    66     TAny *trueTypeExtension = NULL;
       
    67     m_font->ExtendedInterface(KUidOpenFontTrueTypeExtension, trueTypeExtension);
       
    68     m_trueTypeExtension = static_cast<MOpenFontTrueTypeExtension*>(trueTypeExtension);
       
    69     Q_ASSERT(m_shapingExtension && m_trueTypeExtension);
       
    70 }
       
    71 
       
    72 QByteArray QFontEngineS60Extensions::getSfntTable(uint tag) const
       
    73 {
       
    74     Q_ASSERT(m_trueTypeExtension->HasTrueTypeTable(tag));
       
    75     TInt error = KErrNone;
       
    76     TInt tableByteLength = 0;
       
    77     TAny *table = q_check_ptr(m_trueTypeExtension->GetTrueTypeTable(error, tag, &tableByteLength));
       
    78     QByteArray result(static_cast<const char*>(table), tableByteLength);
       
    79     m_trueTypeExtension->ReleaseTrueTypeTable(table);
       
    80     return result;
       
    81 }
       
    82 
       
    83 const unsigned char *QFontEngineS60Extensions::cmap() const
       
    84 {
       
    85     if (!m_cmap) {
       
    86         m_cmapTable = getSfntTable(MAKE_TAG('c', 'm', 'a', 'p'));
       
    87         int size = 0;
       
    88         m_cmap = QFontEngineS60::getCMap(reinterpret_cast<const uchar *>(m_cmapTable.constData()), m_cmapTable.size(), &m_symbolCMap, &size);
       
    89     }
       
    90     return m_cmap;
       
    91 }
       
    92 
       
    93 QPainterPath QFontEngineS60Extensions::glyphOutline(glyph_t glyph) const
       
    94 {
       
    95     QPainterPath result;
       
    96     QPolygonF polygon;
       
    97     TInt glyphIndex = glyph;
       
    98     TInt pointNumber = 0;
       
    99     TInt x, y;
       
   100     while (m_shapingExtension->GlyphPointInFontUnits(glyphIndex, pointNumber++, x, y)) {
       
   101         const QPointF point(qreal(x) / 0xffff, qreal(y) / 0xffff);
       
   102         if (polygon.contains(point)) {
       
   103             result.addPolygon(polygon);
       
   104             result.closeSubpath();
       
   105             polygon.clear();
       
   106         } else {
       
   107             polygon.append(point);
       
   108         }
       
   109     }
       
   110     return result;
       
   111 }
       
   112 
       
   113 CFont *QFontEngineS60Extensions::fontOwner() const
       
   114 {
       
   115     return m_fontOwner;
       
   116 }
       
   117 
       
   118 
       
   119 // duplicated from qfontengine_xyz.cpp
       
   120 static inline unsigned int getChar(const QChar *str, int &i, const int len)
       
   121 {
       
   122     unsigned int uc = str[i].unicode();
       
   123     if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) {
       
   124         uint low = str[i+1].unicode();
       
   125        if (low >= 0xdc00 && low < 0xe000) {
       
   126             uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
       
   127             ++i;
       
   128         }
       
   129     }
       
   130     return uc;
       
   131 }
       
   132 
       
   133 QFontEngineS60::QFontEngineS60(const QFontDef &request, const QFontEngineS60Extensions *extensions)
       
   134     : m_extensions(extensions)
       
   135 {
       
   136     QFontEngine::fontDef = request;
       
   137     m_fontSizeInPixels = (request.pixelSize >= 0)?
       
   138         request.pixelSize:pointsToPixels(request.pointSize);
       
   139         
       
   140     QSymbianFbsHeapLock lock(QSymbianFbsHeapLock::Unlock);
       
   141     
       
   142     m_textRenderBitmap = q_check_ptr(new CFbsBitmap());	// CBase derived object needs check on new
       
   143     const TSize bitmapSize(1, 1); // It is just a dummy bitmap that I need to keep the font alive (or maybe not)
       
   144     qt_symbian_throwIfError(m_textRenderBitmap->Create(bitmapSize, EGray256));
       
   145     QT_TRAP_THROWING(m_textRenderBitmapDevice = CFbsBitmapDevice::NewL(m_textRenderBitmap));
       
   146     qt_symbian_throwIfError(m_textRenderBitmapDevice->CreateContext(m_textRenderBitmapGc));
       
   147     cache_cost = sizeof(QFontEngineS60) + bitmapSize.iHeight * bitmapSize.iWidth * 4;
       
   148 
       
   149     TFontSpec fontSpec(qt_QString2TPtrC(request.family), m_fontSizeInPixels);
       
   150     fontSpec.iFontStyle.SetBitmapType(EAntiAliasedGlyphBitmap);
       
   151     fontSpec.iFontStyle.SetPosture(request.style == QFont::StyleNormal?EPostureUpright:EPostureItalic);
       
   152     fontSpec.iFontStyle.SetStrokeWeight(request.weight > QFont::Normal?EStrokeWeightBold:EStrokeWeightNormal);
       
   153     const TInt errorCode = m_textRenderBitmapDevice->GetNearestFontInPixels(m_font, fontSpec);
       
   154     Q_ASSERT(errorCode == 0);
       
   155     m_textRenderBitmapGc->UseFont(m_font);
       
   156     
       
   157     lock.relock();
       
   158 }
       
   159 
       
   160 QFontEngineS60::~QFontEngineS60()
       
   161 {
       
   162     QSymbianFbsHeapLock lock(QSymbianFbsHeapLock::Unlock);
       
   163     
       
   164     m_textRenderBitmapGc->DiscardFont();
       
   165     delete m_textRenderBitmapGc;
       
   166     m_textRenderBitmapGc = NULL;
       
   167     m_textRenderBitmapDevice->ReleaseFont(m_font);
       
   168     delete m_textRenderBitmapDevice;
       
   169     m_textRenderBitmapDevice = NULL;
       
   170     delete m_textRenderBitmap;
       
   171     m_textRenderBitmap = NULL;
       
   172     
       
   173     lock.relock();
       
   174 }
       
   175 
       
   176 bool QFontEngineS60::stringToCMap(const QChar *characters, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
       
   177 {
       
   178     if (*nglyphs < len) {
       
   179         *nglyphs = len;
       
   180         return false;
       
   181     }
       
   182 
       
   183     HB_Glyph *g = glyphs->glyphs;
       
   184     const unsigned char* cmap = m_extensions->cmap();
       
   185     for (int i = 0; i < len; ++i) {
       
   186         const unsigned int uc = getChar(characters, i, len);
       
   187         *g++ = QFontEngine::getTrueTypeGlyphIndex(cmap, uc);
       
   188     }
       
   189 
       
   190     glyphs->numGlyphs = g - glyphs->glyphs;
       
   191     *nglyphs = glyphs->numGlyphs;
       
   192 
       
   193     if (flags & QTextEngine::GlyphIndicesOnly)
       
   194         return true;
       
   195 
       
   196     recalcAdvances(glyphs, flags);
       
   197     return true;
       
   198 }
       
   199 
       
   200 void QFontEngineS60::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
       
   201 {
       
   202     Q_UNUSED(flags);
       
   203     for (int i = 0; i < glyphs->numGlyphs; i++) {
       
   204         const glyph_metrics_t bbox = boundingBox_const(glyphs->glyphs[i]);
       
   205         glyphs->advances_x[i] = glyphs->offsets[i].x = bbox.xoff;
       
   206         glyphs->advances_y[i] = glyphs->offsets[i].y = bbox.yoff;
       
   207     }
       
   208 }
       
   209 
       
   210 QImage QFontEngineS60::alphaMapForGlyph(glyph_t glyph)
       
   211 {
       
   212     TOpenFontCharMetrics metrics;
       
   213     const TUint8 *glyphBitmapBytes;
       
   214     TSize glyphBitmapSize;
       
   215     getCharacterData(glyph, metrics, glyphBitmapBytes, glyphBitmapSize);
       
   216     QImage result(glyphBitmapBytes, glyphBitmapSize.iWidth, glyphBitmapSize.iHeight, glyphBitmapSize.iWidth, QImage::Format_Indexed8);
       
   217     result.setColorTable(grayPalette());
       
   218 
       
   219     // The above setColorTable() call detached the image data anyway, so why not shape tha data a bit, while we can.
       
   220     // CFont::GetCharacterData() returns 8-bit data that obviously was 4-bit data before, and converted to 8-bit incorrectly.
       
   221     // The data values are 0x00, 0x10 ... 0xe0, 0xf0. So, a real opaque 0xff is never reached, which we get punished
       
   222     // for every time we want to blit this glyph in the raster paint engine.
       
   223     // "Fix" is to convert all 0xf0 to 0xff. Is fine, quality wise, and I assume faster than correcting all values.
       
   224     // Blitting is however, evidentially faster now.
       
   225     const int bpl = result.bytesPerLine();
       
   226     for (int row = 0; row < result.height(); ++row) {
       
   227         uchar *scanLine = result.scanLine(row);
       
   228         for (int column = 0; column < bpl; ++column) {
       
   229             if (*scanLine == 0xf0)
       
   230                 *scanLine = 0xff;
       
   231             scanLine++;
       
   232         }
       
   233     }
       
   234 
       
   235     return result;
       
   236 }
       
   237 
       
   238 glyph_metrics_t QFontEngineS60::boundingBox(const QGlyphLayout &glyphs)
       
   239 {
       
   240    if (glyphs.numGlyphs == 0)
       
   241         return glyph_metrics_t();
       
   242 
       
   243     QFixed w = 0;
       
   244     for (int i = 0; i < glyphs.numGlyphs; ++i)
       
   245         w += glyphs.effectiveAdvance(i);
       
   246 
       
   247     return glyph_metrics_t(0, -ascent(), w, ascent()+descent()+1, w, 0);
       
   248 }
       
   249 
       
   250 glyph_metrics_t QFontEngineS60::boundingBox_const(glyph_t glyph) const
       
   251 {
       
   252     TOpenFontCharMetrics metrics;
       
   253     const TUint8 *glyphBitmapBytes;
       
   254     TSize glyphBitmapSize;
       
   255     getCharacterData(glyph, metrics, glyphBitmapBytes, glyphBitmapSize);
       
   256     TRect glyphBounds;
       
   257     metrics.GetHorizBounds(glyphBounds);
       
   258     const glyph_metrics_t result(
       
   259         glyphBounds.iTl.iX,
       
   260         glyphBounds.iTl.iY,
       
   261         glyphBounds.Width(),
       
   262         glyphBounds.Height(),
       
   263         metrics.HorizAdvance(),
       
   264         0
       
   265     );
       
   266     return result;
       
   267 }
       
   268 
       
   269 glyph_metrics_t QFontEngineS60::boundingBox(glyph_t glyph)
       
   270 {
       
   271     return boundingBox_const(glyph);
       
   272 }
       
   273 
       
   274 QFixed QFontEngineS60::ascent() const
       
   275 {
       
   276     return m_font->FontMaxAscent();
       
   277 }
       
   278 
       
   279 QFixed QFontEngineS60::descent() const
       
   280 {
       
   281     return m_font->FontMaxDescent();
       
   282 }
       
   283 
       
   284 QFixed QFontEngineS60::leading() const
       
   285 {
       
   286     return 0;
       
   287 }
       
   288 
       
   289 qreal QFontEngineS60::maxCharWidth() const
       
   290 {
       
   291     return m_font->MaxCharWidthInPixels();
       
   292 }
       
   293 
       
   294 const char *QFontEngineS60::name() const
       
   295 {
       
   296     return "QFontEngineS60";
       
   297 }
       
   298 
       
   299 bool QFontEngineS60::canRender(const QChar *string, int len)
       
   300 {
       
   301     const unsigned char *cmap = m_extensions->cmap();
       
   302     for (int i = 0; i < len; ++i) {
       
   303         const unsigned int uc = getChar(string, i, len);
       
   304         if (QFontEngine::getTrueTypeGlyphIndex(cmap, uc) == 0)
       
   305             return false;
       
   306     }
       
   307     return true;
       
   308 }
       
   309 
       
   310 QByteArray QFontEngineS60::getSfntTable(uint tag) const
       
   311 {
       
   312     return m_extensions->getSfntTable(tag);
       
   313 }
       
   314 
       
   315 QFontEngine::Type QFontEngineS60::type() const
       
   316 {
       
   317     return QFontEngine::S60FontEngine;
       
   318 }
       
   319 
       
   320 void QFontEngineS60::getCharacterData(glyph_t glyph, TOpenFontCharMetrics& metrics, const TUint8*& bitmap, TSize& bitmapSize) const
       
   321 {
       
   322     // Setting the most significant bit tells GetCharacterData
       
   323     // that 'code' is a Glyph ID, rather than a UTF-16 value
       
   324     const TUint specialCode = (TUint)glyph | 0x80000000;
       
   325 
       
   326     const CFont::TCharacterDataAvailability availability =
       
   327         m_font->GetCharacterData(specialCode, metrics, bitmap, bitmapSize);
       
   328     const glyph_t fallbackGlyph = '?';
       
   329     if (availability != CFont::EAllCharacterData) {
       
   330         const CFont::TCharacterDataAvailability fallbackAvailability =
       
   331             m_font->GetCharacterData(fallbackGlyph, metrics, bitmap, bitmapSize);
       
   332         Q_ASSERT(fallbackAvailability == CFont::EAllCharacterData);
       
   333     }
       
   334 }
       
   335 
       
   336 QT_END_NAMESPACE