1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qfontengine_s60_p.h"
43 #include "qtextengine_p.h"
46 #include <private/qapplication_p.h>
48 #include <private/qt_s60_p.h>
49 #include <private/qpixmap_s60_p.h>
55 #if defined(Q_SYMBIAN_HAS_GLYPHOUTLINE_API)
56 #include <graphics/gdi/gdiplatapi.h>
57 #endif // Q_SYMBIAN_HAS_GLYPHOUTLINE_API
59 // Replication of TGetFontTableParam & friends.
60 // There is unfortunately no compile time flag like SYMBIAN_FONT_TABLE_API
61 // that would help us to only replicate these things for Symbian versions
62 // that do not yet have the font table Api. Symbian's public SDK does
63 // generally not define any usable macros.
64 class QSymbianTGetFontTableParam
71 const TUid QSymbianKFontGetFontTable = {0x102872C1};
72 const TUid QSymbianKFontReleaseFontTable = {0x2002AC24};
76 QSymbianTypeFaceExtras::QSymbianTypeFaceExtras(CFont* cFont, COpenFont *openFont)
79 , m_openFont(openFont)
81 if (!symbianFontTableApiAvailable()) {
82 TAny *trueTypeExtension = NULL;
83 m_openFont->ExtendedInterface(KUidOpenFontTrueTypeExtension, trueTypeExtension);
84 m_trueTypeExtension = static_cast<MOpenFontTrueTypeExtension*>(trueTypeExtension);
85 Q_ASSERT(m_trueTypeExtension);
89 QSymbianTypeFaceExtras::~QSymbianTypeFaceExtras()
91 if (symbianFontTableApiAvailable())
92 S60->screenDevice()->ReleaseFont(m_cFont);
95 QByteArray QSymbianTypeFaceExtras::getSfntTable(uint tag) const
97 if (symbianFontTableApiAvailable()) {
98 QSymbianTGetFontTableParam fontTableParams = { tag, 0, 0 };
99 if (m_cFont->ExtendedFunction(QSymbianKFontGetFontTable, &fontTableParams) == KErrNone) {
100 const char* const fontTableContent =
101 static_cast<const char *>(fontTableParams.iContent);
102 const QByteArray fontTable(fontTableContent, fontTableParams.iLength);
103 m_cFont->ExtendedFunction(QSymbianKFontReleaseFontTable, &fontTableParams);
108 Q_ASSERT(m_trueTypeExtension->HasTrueTypeTable(tag));
109 TInt error = KErrNone;
110 TInt tableByteLength = 0;
111 TAny *table = m_trueTypeExtension->GetTrueTypeTable(error, tag, &tableByteLength);
113 const QByteArray result(static_cast<const char*>(table), tableByteLength);
114 m_trueTypeExtension->ReleaseTrueTypeTable(table);
119 bool QSymbianTypeFaceExtras::getSfntTableData(uint tag, uchar *buffer, uint *length) const
122 if (symbianFontTableApiAvailable()) {
123 QSymbianTGetFontTableParam fontTableParams = { tag, 0, 0 };
124 if (m_cFont->ExtendedFunction(QSymbianKFontGetFontTable, &fontTableParams) == KErrNone) {
125 if (*length > 0 && *length < fontTableParams.iLength) {
126 result = false; // Caller did not allocate enough memory
128 *length = fontTableParams.iLength;
130 memcpy(buffer, fontTableParams.iContent, fontTableParams.iLength);
132 m_cFont->ExtendedFunction(QSymbianKFontReleaseFontTable, &fontTableParams);
137 if (!m_trueTypeExtension->HasTrueTypeTable(tag))
140 TInt error = KErrNone;
141 TInt tableByteLength;
142 TAny *table = m_trueTypeExtension->GetTrueTypeTable(error, tag, &tableByteLength);
145 if (error != KErrNone) {
147 } else if (*length > 0 && *length < tableByteLength) {
148 result = false; // Caller did not allocate enough memory
150 *length = tableByteLength;
152 memcpy(buffer, table, tableByteLength);
155 m_trueTypeExtension->ReleaseTrueTypeTable(table);
160 const uchar *QSymbianTypeFaceExtras::cmap() const
162 if (m_cmapTable.isNull()) {
163 const QByteArray cmapTable = getSfntTable(MAKE_TAG('c', 'm', 'a', 'p'));
165 const uchar *cmap = QFontEngine::getCMap(reinterpret_cast<const uchar *>
166 (cmapTable.constData()), cmapTable.size(), &m_symbolCMap, &size);
167 m_cmapTable = QByteArray(reinterpret_cast<const char *>(cmap), size);
169 return reinterpret_cast<const uchar *>(m_cmapTable.constData());
172 bool QSymbianTypeFaceExtras::isSymbolCMap() const
177 CFont *QSymbianTypeFaceExtras::fontOwner() const
182 QFixed QSymbianTypeFaceExtras::unitsPerEm() const
184 if (m_unitsPerEm.value() != 0)
186 const QByteArray head = getSfntTable(MAKE_TAG('h', 'e', 'a', 'd'));
187 const int unitsPerEmOffset = 18;
188 if (head.size() > unitsPerEmOffset + sizeof(quint16)) {
189 const uchar* tableData = reinterpret_cast<const uchar*>(head.constData());
190 const uchar* unitsPerEm = tableData + unitsPerEmOffset;
191 m_unitsPerEm = qFromBigEndian<quint16>(unitsPerEm);
193 // Bitmap font? Corrupt font?
194 // We return -1 and let the QFontEngineS60 return the pixel size.
200 bool QSymbianTypeFaceExtras::symbianFontTableApiAvailable()
202 enum FontTableApiAvailability {
207 static FontTableApiAvailability availability =
208 QSysInfo::symbianVersion() < QSysInfo::SV_SF_3 ?
209 Unavailable : Unknown;
210 if (availability == Unknown) {
211 // Actually, we should ask CFeatureDiscovery::IsFeatureSupportedL()
212 // with FfFontTable here. But since at the time of writing, the
213 // FfFontTable flag check either gave false positives or false
214 // negatives. Here comes an implicit check via CFont::ExtendedFunction.
215 QSymbianTGetFontTableParam fontTableParams = {
216 MAKE_TAG('O', 'S', '/', '2'), 0, 0 };
217 QSymbianFbsHeapLock lock(QSymbianFbsHeapLock::Unlock);
219 const TInt getFontErr = S60->screenDevice()->GetNearestFontInTwips(font, TFontSpec());
220 Q_ASSERT(getFontErr == KErrNone);
221 if (font->ExtendedFunction(QSymbianKFontGetFontTable, &fontTableParams) == KErrNone) {
222 font->ExtendedFunction(QSymbianKFontReleaseFontTable, &fontTableParams);
223 availability = Available;
225 availability = Unavailable;
227 S60->screenDevice()->ReleaseFont(font);
230 return availability == Available;
233 // duplicated from qfontengine_xyz.cpp
234 static inline unsigned int getChar(const QChar *str, int &i, const int len)
236 unsigned int uc = str[i].unicode();
237 if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) {
238 uint low = str[i+1].unicode();
239 if (low >= 0xdc00 && low < 0xe000) {
240 uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
247 extern QString qt_symbian_fontNameWithAppFontMarker(const QString &fontName); // qfontdatabase_s60.cpp
249 CFont *QFontEngineS60::fontWithSize(qreal size) const
252 const QString family = qt_symbian_fontNameWithAppFontMarker(QFontEngine::fontDef.family);
253 TFontSpec fontSpec(qt_QString2TPtrC(family), TInt(size));
254 fontSpec.iFontStyle.SetBitmapType(EAntiAliasedGlyphBitmap);
255 fontSpec.iFontStyle.SetPosture(QFontEngine::fontDef.style == QFont::StyleNormal?EPostureUpright:EPostureItalic);
256 fontSpec.iFontStyle.SetStrokeWeight(QFontEngine::fontDef.weight > QFont::Normal?EStrokeWeightBold:EStrokeWeightNormal);
257 const TInt errorCode = S60->screenDevice()->GetNearestFontToDesignHeightInPixels(result, fontSpec);
258 Q_ASSERT(result && (errorCode == 0));
262 void QFontEngineS60::setFontScale(qreal scale)
264 if (qFuzzyCompare(scale, qreal(1))) {
266 m_originalFont = fontWithSize(m_originalFontSizeInPixels);
267 m_activeFont = m_originalFont;
269 const qreal scaledFontSizeInPixels = m_originalFontSizeInPixels * scale;
271 (TInt(scaledFontSizeInPixels) != TInt(m_scaledFontSizeInPixels))) {
272 releaseFont(m_scaledFont);
273 m_scaledFontSizeInPixels = scaledFontSizeInPixels;
274 m_scaledFont = fontWithSize(m_scaledFontSizeInPixels);
276 m_activeFont = m_scaledFont;
280 void QFontEngineS60::releaseFont(CFont *&font)
283 S60->screenDevice()->ReleaseFont(font);
288 QFontEngineS60::QFontEngineS60(const QFontDef &request, const QSymbianTypeFaceExtras *extras)
291 , m_originalFontSizeInPixels((request.pixelSize >= 0)?
292 request.pixelSize:pointsToPixels(request.pointSize))
294 , m_scaledFontSizeInPixels(0)
297 QFontEngine::fontDef = request;
299 cache_cost = sizeof(QFontEngineS60);
302 QFontEngineS60::~QFontEngineS60()
304 releaseFont(m_originalFont);
305 releaseFont(m_scaledFont);
308 QFixed QFontEngineS60::emSquareSize() const
310 const QFixed unitsPerEm = m_extras->unitsPerEm();
311 return unitsPerEm.toInt() == -1 ?
312 QFixed::fromReal(m_originalFontSizeInPixels) : unitsPerEm;
315 bool QFontEngineS60::stringToCMap(const QChar *characters, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
317 if (*nglyphs < len) {
322 HB_Glyph *g = glyphs->glyphs;
323 const unsigned char* cmap = m_extras->cmap();
324 const bool isRtl = (flags & QTextEngine::RightToLeft);
325 for (int i = 0; i < len; ++i) {
326 const unsigned int uc = getChar(characters, i, len);
327 *g++ = QFontEngine::getTrueTypeGlyphIndex(cmap,
328 (isRtl && !m_extras->isSymbolCMap()) ? QChar::mirroredChar(uc) : uc);
331 glyphs->numGlyphs = g - glyphs->glyphs;
332 *nglyphs = glyphs->numGlyphs;
334 if (flags & QTextEngine::GlyphIndicesOnly)
337 recalcAdvances(glyphs, flags);
341 void QFontEngineS60::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
344 TOpenFontCharMetrics metrics;
345 const TUint8 *glyphBitmapBytes;
346 TSize glyphBitmapSize;
347 for (int i = 0; i < glyphs->numGlyphs; i++) {
348 getCharacterData(glyphs->glyphs[i], metrics, glyphBitmapBytes, glyphBitmapSize);
349 glyphs->advances_x[i] = metrics.HorizAdvance();
350 glyphs->advances_y[i] = 0;
354 #ifdef Q_SYMBIAN_HAS_GLYPHOUTLINE_API
355 static bool parseGlyphPathData(const char *dataStr, const char *dataEnd, QPainterPath &path,
356 qreal fontPixelSize, const QPointF &offset, bool hinted);
357 #endif //Q_SYMBIAN_HAS_GLYPHOUTLINE_API
359 void QFontEngineS60::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions,
360 int nglyphs, QPainterPath *path,
361 QTextItem::RenderFlags flags)
363 #ifdef Q_SYMBIAN_HAS_GLYPHOUTLINE_API
365 RGlyphOutlineIterator iterator;
366 const TInt error = iterator.Open(*m_activeFont, glyphs, nglyphs);
367 if (KErrNone != error)
369 const qreal fontSizeInPixels = qreal(m_activeFont->HeightInPixels());
372 const TUint8* outlineUint8 = iterator.Outline();
373 const char* const outlineChar = reinterpret_cast<const char*>(outlineUint8);
374 const char* const outlineEnd = outlineChar + iterator.OutlineLength();
375 parseGlyphPathData(outlineChar, outlineEnd, *path, fontSizeInPixels,
376 positions[count++].toPointF(), false);
377 } while(KErrNone == iterator.Next() && count <= nglyphs);
379 #else // Q_SYMBIAN_HAS_GLYPHOUTLINE_API
380 QFontEngine::addGlyphsToPath(glyphs, positions, nglyphs, path, flags);
381 #endif //Q_SYMBIAN_HAS_GLYPHOUTLINE_API
384 QImage QFontEngineS60::alphaMapForGlyph(glyph_t glyph)
386 // Note: On some Symbian versions (apparently <= Symbian^1), this
387 // function will return gray values 0x00, 0x10 ... 0xe0, 0xf0 due
388 // to a bug. The glyphs are nowhere perfectly opaque.
389 // This has been fixed for Symbian^3.
391 TOpenFontCharMetrics metrics;
392 const TUint8 *glyphBitmapBytes;
393 TSize glyphBitmapSize;
394 getCharacterData(glyph, metrics, glyphBitmapBytes, glyphBitmapSize);
395 QImage result(glyphBitmapBytes, glyphBitmapSize.iWidth, glyphBitmapSize.iHeight, glyphBitmapSize.iWidth, QImage::Format_Indexed8);
396 result.setColorTable(grayPalette());
400 glyph_metrics_t QFontEngineS60::boundingBox(const QGlyphLayout &glyphs)
402 if (glyphs.numGlyphs == 0)
403 return glyph_metrics_t();
406 for (int i = 0; i < glyphs.numGlyphs; ++i)
407 w += glyphs.effectiveAdvance(i);
409 return glyph_metrics_t(0, -ascent(), w - lastRightBearing(glyphs), ascent()+descent()+1, w, 0);
412 glyph_metrics_t QFontEngineS60::boundingBox_const(glyph_t glyph) const
414 TOpenFontCharMetrics metrics;
415 const TUint8 *glyphBitmapBytes;
416 TSize glyphBitmapSize;
417 getCharacterData(glyph, metrics, glyphBitmapBytes, glyphBitmapSize);
418 const glyph_metrics_t result(
419 metrics.HorizBearingX(),
420 -metrics.HorizBearingY(),
423 metrics.HorizAdvance(),
429 glyph_metrics_t QFontEngineS60::boundingBox(glyph_t glyph)
431 return boundingBox_const(glyph);
434 QFixed QFontEngineS60::ascent() const
436 // Workaround for QTBUG-8013
437 // Stroke based fonts may return an incorrect FontMaxAscent of 0.
438 const QFixed ascent = m_originalFont->FontMaxAscent();
439 return (ascent > 0) ? ascent : QFixed::fromReal(m_originalFontSizeInPixels) - descent();
442 QFixed QFontEngineS60::descent() const
444 return m_originalFont->FontMaxDescent();
447 QFixed QFontEngineS60::leading() const
452 qreal QFontEngineS60::maxCharWidth() const
454 return m_originalFont->MaxCharWidthInPixels();
457 const char *QFontEngineS60::name() const
459 return "QFontEngineS60";
462 bool QFontEngineS60::canRender(const QChar *string, int len)
464 const unsigned char *cmap = m_extras->cmap();
465 for (int i = 0; i < len; ++i) {
466 const unsigned int uc = getChar(string, i, len);
467 if (QFontEngine::getTrueTypeGlyphIndex(cmap, uc) == 0)
473 QByteArray QFontEngineS60::getSfntTable(uint tag) const
475 return m_extras->getSfntTable(tag);
478 bool QFontEngineS60::getSfntTableData(uint tag, uchar *buffer, uint *length) const
480 return m_extras->getSfntTableData(tag, buffer, length);
483 QFontEngine::Type QFontEngineS60::type() const
485 return QFontEngine::S60FontEngine;
488 void QFontEngineS60::getCharacterData(glyph_t glyph, TOpenFontCharMetrics& metrics, const TUint8*& bitmap, TSize& bitmapSize) const
490 // Setting the most significant bit tells GetCharacterData
491 // that 'code' is a Glyph ID, rather than a UTF-16 value
492 const TUint specialCode = (TUint)glyph | 0x80000000;
494 const CFont::TCharacterDataAvailability availability =
495 m_activeFont->GetCharacterData(specialCode, metrics, bitmap, bitmapSize);
496 const glyph_t fallbackGlyph = '?';
497 if (availability != CFont::EAllCharacterData) {
498 const CFont::TCharacterDataAvailability fallbackAvailability =
499 m_activeFont->GetCharacterData(fallbackGlyph, metrics, bitmap, bitmapSize);
500 Q_ASSERT(fallbackAvailability == CFont::EAllCharacterData);
504 #ifdef Q_SYMBIAN_HAS_GLYPHOUTLINE_API
505 static inline void skipSpacesAndComma(const char* &str, const char* const strEnd)
507 while (str <= strEnd && (*str == ' ' || *str == ','))
511 static bool parseGlyphPathData(const char *svgPath, const char *svgPathEnd, QPainterPath &path,
512 qreal fontPixelSize, const QPointF &offset, bool hinted)
515 QPointF p1, p2, firstSubPathPoint;
516 qreal *elementValues[] =
517 {&p1.rx(), &p1.ry(), &p2.rx(), &p2.ry()};
518 const int unitsPerEm = 2048; // See: http://en.wikipedia.org/wiki/Em_%28typography%29
519 const qreal resizeFactor = fontPixelSize / unitsPerEm;
521 while (svgPath < svgPathEnd) {
522 skipSpacesAndComma(svgPath, svgPathEnd);
523 const char pathElem = *svgPath++;
524 skipSpacesAndComma(svgPath, svgPathEnd);
526 if (pathElem != 'Z') {
528 int elementValuesCount = 0;
529 for (int i = 0; i < 4; ++i) { // 4 = size of elementValues[]
530 qreal coordinateValue = strtod(svgPath, &endStr);
531 if (svgPath == endStr)
533 if (i % 2) // Flip vertically
534 coordinateValue = -coordinateValue;
535 *elementValues[i] = coordinateValue * resizeFactor;
536 elementValuesCount++;
538 skipSpacesAndComma(svgPath, svgPathEnd);
541 if (elementValuesCount == 2)
542 p2 = firstSubPathPoint;
549 firstSubPathPoint = p1;
567 #endif // Q_SYMBIAN_HAS_GLYPHOUTLINE_API