Compile when -directwrite is passed to configure
[profile/ivi/qtbase.git] / src / gui / text / qfontenginedirectwrite.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QT_NO_DIRECTWRITE
43
44 #include "qfontenginedirectwrite_p.h"
45
46 #include <qendian.h>
47 #include <dwrite.h>
48 #include <private/qnativeimage_p.h>
49
50 #include <d2d1.h>
51
52 QT_BEGIN_NAMESPACE
53
54 // Convert from design units to logical pixels
55 #define DESIGN_TO_LOGICAL(DESIGN_UNIT_VALUE) \
56     QFixed::fromReal((qreal(DESIGN_UNIT_VALUE) / qreal(m_unitsPerEm)) * fontDef.pixelSize)
57
58 namespace {
59
60     class GeometrySink: public IDWriteGeometrySink
61     {
62     public:
63         GeometrySink(QPainterPath *path) : m_path(path), m_refCount(0)
64         {
65             Q_ASSERT(m_path != 0);
66         }
67
68         IFACEMETHOD_(void, AddBeziers)(const D2D1_BEZIER_SEGMENT *beziers, UINT bezierCount);
69         IFACEMETHOD_(void, AddLines)(const D2D1_POINT_2F *points, UINT pointCount);
70         IFACEMETHOD_(void, BeginFigure)(D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin);
71         IFACEMETHOD(Close)();
72         IFACEMETHOD_(void, EndFigure)(D2D1_FIGURE_END figureEnd);
73         IFACEMETHOD_(void, SetFillMode)(D2D1_FILL_MODE fillMode);
74         IFACEMETHOD_(void, SetSegmentFlags)(D2D1_PATH_SEGMENT vertexFlags);
75
76         IFACEMETHOD_(unsigned long, AddRef)();
77         IFACEMETHOD_(unsigned long, Release)();
78         IFACEMETHOD(QueryInterface)(IID const &riid, void **ppvObject);
79
80     private:
81         inline static QPointF fromD2D1_POINT_2F(const D2D1_POINT_2F &inp)
82         {
83             return QPointF(inp.x, inp.y);
84         }
85
86         unsigned long m_refCount;
87         QPointF m_startPoint;
88         QPainterPath *m_path;
89     };
90
91     void GeometrySink::AddBeziers(const D2D1_BEZIER_SEGMENT *beziers,
92                                   UINT bezierCount)
93     {
94         for (uint i=0; i<bezierCount; ++i) {
95             QPointF c1 = fromD2D1_POINT_2F(beziers[i].point1);
96             QPointF c2 = fromD2D1_POINT_2F(beziers[i].point2);
97             QPointF p2 = fromD2D1_POINT_2F(beziers[i].point3);
98
99             m_path->cubicTo(c1, c2, p2);
100         }
101     }
102
103     void GeometrySink::AddLines(const D2D1_POINT_2F *points, UINT pointsCount)
104     {
105         for (uint i=0; i<pointsCount; ++i)
106             m_path->lineTo(fromD2D1_POINT_2F(points[i]));
107     }
108
109     void GeometrySink::BeginFigure(D2D1_POINT_2F startPoint,
110                                    D2D1_FIGURE_BEGIN /*figureBegin*/)
111     {
112         m_startPoint = fromD2D1_POINT_2F(startPoint);
113         m_path->moveTo(m_startPoint);
114     }
115
116     IFACEMETHODIMP GeometrySink::Close()
117     {
118         return E_NOTIMPL;
119     }
120
121     void GeometrySink::EndFigure(D2D1_FIGURE_END figureEnd)
122     {
123         if (figureEnd == D2D1_FIGURE_END_CLOSED)
124             m_path->closeSubpath();
125     }
126
127     void GeometrySink::SetFillMode(D2D1_FILL_MODE fillMode)
128     {
129         m_path->setFillRule(fillMode == D2D1_FILL_MODE_ALTERNATE
130                             ? Qt::OddEvenFill
131                             : Qt::WindingFill);
132     }
133
134     void GeometrySink::SetSegmentFlags(D2D1_PATH_SEGMENT /*vertexFlags*/)
135     {
136         /* Not implemented */
137     }
138
139     IFACEMETHODIMP_(unsigned long) GeometrySink::AddRef()
140     {
141         return InterlockedIncrement(&m_refCount);
142     }
143
144     IFACEMETHODIMP_(unsigned long) GeometrySink::Release()
145     {
146         unsigned long newCount = InterlockedDecrement(&m_refCount);
147         if (newCount == 0)
148         {
149             delete this;
150             return 0;
151         }
152
153         return newCount;
154     }
155
156     IFACEMETHODIMP GeometrySink::QueryInterface(IID const &riid, void **ppvObject)
157     {
158         if (__uuidof(IDWriteGeometrySink) == riid) {
159             *ppvObject = this;
160         } else if (__uuidof(IUnknown) == riid) {
161             *ppvObject = this;
162         } else {
163             *ppvObject = NULL;
164             return E_FAIL;
165         }
166
167         AddRef();
168         return S_OK;
169     }
170
171 }
172
173 QFontEngineDirectWrite::QFontEngineDirectWrite(IDWriteFactory *directWriteFactory,
174                                                IDWriteFontFace *directWriteFontFace,
175                                                qreal pixelSize)
176     : m_directWriteFontFace(directWriteFontFace)
177     , m_directWriteFactory(directWriteFactory)
178     , m_directWriteBitmapRenderTarget(0)
179     , m_lineThickness(-1)
180     , m_unitsPerEm(-1)
181     , m_ascent(-1)
182     , m_descent(-1)
183     , m_xHeight(-1)
184     , m_lineGap(-1)
185 {
186     m_directWriteFactory->AddRef();
187     m_directWriteFontFace->AddRef();
188
189     fontDef.pixelSize = pixelSize;
190     collectMetrics();
191 }
192
193 QFontEngineDirectWrite::~QFontEngineDirectWrite()
194 {
195     m_directWriteFactory->Release();
196     m_directWriteFontFace->Release();
197
198     if (m_directWriteBitmapRenderTarget != 0)
199         m_directWriteBitmapRenderTarget->Release();
200 }
201
202 void QFontEngineDirectWrite::collectMetrics()
203 {
204     if (m_directWriteFontFace != 0) {
205         DWRITE_FONT_METRICS metrics;
206
207         m_directWriteFontFace->GetMetrics(&metrics);
208         m_unitsPerEm = metrics.designUnitsPerEm;
209
210         m_lineThickness = DESIGN_TO_LOGICAL(metrics.underlineThickness);
211         m_ascent = DESIGN_TO_LOGICAL(metrics.ascent);
212         m_descent = DESIGN_TO_LOGICAL(metrics.descent);
213         m_xHeight = DESIGN_TO_LOGICAL(metrics.xHeight);
214         m_lineGap = DESIGN_TO_LOGICAL(metrics.lineGap);
215     }
216 }
217
218 QFixed QFontEngineDirectWrite::lineThickness() const
219 {
220     if (m_lineThickness > 0)
221         return m_lineThickness;
222     else
223         return QFontEngine::lineThickness();
224 }
225
226 bool QFontEngineDirectWrite::getSfntTableData(uint tag, uchar *buffer, uint *length) const
227 {
228     bool ret = false;
229
230     if (m_directWriteFontFace) {
231         DWORD t = qbswap<quint32>(tag);
232
233         const void *tableData = 0;
234         void *tableContext = 0;
235         UINT32 tableSize;
236         BOOL exists;
237         HRESULT hr = m_directWriteFontFace->TryGetFontTable(
238                     t, &tableData, &tableSize, &tableContext, &exists
239                     );
240
241         if (SUCCEEDED(hr)) {
242             if (exists) {
243                 if (!buffer) {
244                     *length = tableSize;
245                     ret = true;
246                 } else if (*length >= tableSize) {
247                     memcpy(buffer, tableData, tableSize);
248                     ret = true;
249                 }
250             }
251             m_directWriteFontFace->ReleaseFontTable(tableContext);
252         } else {
253             qErrnoWarning("QFontEngineDirectWrite::getSfntTableData: TryGetFontTable failed");
254         }
255     }
256
257     return ret;
258 }
259
260 QFixed QFontEngineDirectWrite::emSquareSize() const
261 {
262     if (m_unitsPerEm > 0)
263         return m_unitsPerEm;
264     else
265         return QFontEngine::emSquareSize();
266 }
267
268 inline unsigned int getChar(const QChar *str, int &i, const int len)
269 {
270     uint ucs4 = str[i].unicode();
271     if (str[i].isHighSurrogate() && i < len-1 && str[i+1].isLowSurrogate()) {
272         ++i;
273         ucs4 = QChar::surrogateToUcs4( ucs4, str[i].unicode());
274     }
275     return ucs4;
276 }
277
278 bool QFontEngineDirectWrite::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs,
279                                           int *nglyphs, QTextEngine::ShaperFlags flags) const
280 {
281     if (m_directWriteFontFace != 0) {
282         QVarLengthArray<UINT32> codePoints(len);
283         for (int i=0; i<len; ++i) {
284             codePoints[i] = getChar(str, i, len);
285             if (flags & QTextEngine::RightToLeft)
286                 codePoints[i] = QChar::mirroredChar(codePoints[i]);
287         }
288
289         QVarLengthArray<UINT16> glyphIndices(len);
290         HRESULT hr = m_directWriteFontFace->GetGlyphIndicesW(codePoints.data(),
291                                                              len,
292                                                              glyphIndices.data());
293
294         if (SUCCEEDED(hr)) {
295             for (int i=0; i<len; ++i)
296                 glyphs->glyphs[i] = glyphIndices[i];
297
298             *nglyphs = len;
299
300             if (!(flags & QTextEngine::GlyphIndicesOnly))
301                 recalcAdvances(glyphs, 0);
302
303             return true;
304         } else {
305             qErrnoWarning("QFontEngineDirectWrite::stringToCMap: GetGlyphIndicesW failed");
306         }
307     }
308
309     return false;
310 }
311
312 void QFontEngineDirectWrite::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
313 {
314     if (m_directWriteFontFace == 0)
315         return;
316
317     QVarLengthArray<UINT16> glyphIndices(glyphs->numGlyphs);
318
319     // ### Caching?
320     for(int i=0; i<glyphs->numGlyphs; i++)
321         glyphIndices[i] = UINT16(glyphs->glyphs[i]);
322
323     QVarLengthArray<DWRITE_GLYPH_METRICS> glyphMetrics(glyphIndices.size());
324     HRESULT hr = m_directWriteFontFace->GetDesignGlyphMetrics(glyphIndices.data(),
325                                                               glyphIndices.size(),
326                                                               glyphMetrics.data());
327     if (SUCCEEDED(hr)) {
328         for (int i=0; i<glyphs->numGlyphs; ++i) {
329             glyphs->advances_x[i] = DESIGN_TO_LOGICAL(glyphMetrics[i].advanceWidth);
330             if (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
331                 glyphs->advances_x[i] = glyphs->advances_x[i].round();
332             glyphs->advances_y[i] = 0;
333         }
334     } else {
335         qErrnoWarning("QFontEngineDirectWrite::recalcAdvances: GetDesignGlyphMetrics failed");
336     }
337 }
338
339 void QFontEngineDirectWrite::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
340                                              QPainterPath *path, QTextItem::RenderFlags flags)
341 {
342     if (m_directWriteFontFace == 0)
343         return;
344
345     QVarLengthArray<UINT16> glyphIndices(nglyphs);
346     QVarLengthArray<DWRITE_GLYPH_OFFSET> glyphOffsets(nglyphs);
347     QVarLengthArray<FLOAT> glyphAdvances(nglyphs);
348
349     for (int i=0; i<nglyphs; ++i) {
350         glyphIndices[i] = glyphs[i];
351         glyphOffsets[i].advanceOffset  = positions[i].x.toReal();
352         glyphOffsets[i].ascenderOffset = -positions[i].y.toReal();
353         glyphAdvances[i] = 0.0;
354     }
355
356     GeometrySink geometrySink(path);
357     HRESULT hr = m_directWriteFontFace->GetGlyphRunOutline(
358                 fontDef.pixelSize,
359                 glyphIndices.data(),
360                 glyphAdvances.data(),
361                 glyphOffsets.data(),
362                 nglyphs,
363                 false,
364                 flags & QTextItem::RightToLeft,
365                 &geometrySink
366                 );
367
368     if (FAILED(hr))
369         qErrnoWarning("QFontEngineDirectWrite::addGlyphsToPath: GetGlyphRunOutline failed");
370 }
371
372 glyph_metrics_t QFontEngineDirectWrite::boundingBox(const QGlyphLayout &glyphs)
373 {
374     if (glyphs.numGlyphs == 0)
375         return glyph_metrics_t();
376
377     bool round = fontDef.styleStrategy & QFont::ForceIntegerMetrics;
378
379     QFixed w = 0;
380     for (int i = 0; i < glyphs.numGlyphs; ++i) {
381         w += round ? glyphs.effectiveAdvance(i).round() : glyphs.effectiveAdvance(i);
382
383     }
384
385     return glyph_metrics_t(0, -m_ascent, w - lastRightBearing(glyphs), m_ascent + m_descent, w, 0);
386 }
387
388 glyph_metrics_t QFontEngineDirectWrite::alphaMapBoundingBox(glyph_t glyph, QFixed subPixelPosition,
389                                                             const QTransform &matrix,
390                                                             GlyphFormat /*format*/)
391 {
392     glyph_metrics_t bbox = QFontEngine::boundingBox(glyph, matrix); // To get transformed advance
393
394     UINT16 glyphIndex = glyph;
395     FLOAT glyphAdvance = 0;
396
397     DWRITE_GLYPH_OFFSET glyphOffset;
398     glyphOffset.advanceOffset = 0;
399     glyphOffset.ascenderOffset = 0;
400
401     DWRITE_GLYPH_RUN glyphRun;
402     glyphRun.fontFace = m_directWriteFontFace;
403     glyphRun.fontEmSize = fontDef.pixelSize;
404     glyphRun.glyphCount = 1;
405     glyphRun.glyphIndices = &glyphIndex;
406     glyphRun.glyphAdvances = &glyphAdvance;
407     glyphRun.isSideways = false;
408     glyphRun.bidiLevel = 0;
409     glyphRun.glyphOffsets = &glyphOffset;
410
411     DWRITE_MATRIX transform;
412     transform.dx = subPixelPosition.toReal();
413     transform.dy = 0;
414     transform.m11 = matrix.m11();
415     transform.m12 = matrix.m12();
416     transform.m21 = matrix.m21();
417     transform.m22 = matrix.m22();
418
419     IDWriteGlyphRunAnalysis *glyphAnalysis = NULL;
420     HRESULT hr = m_directWriteFactory->CreateGlyphRunAnalysis(
421                 &glyphRun,
422                 1.0f,
423                 &transform,
424                 DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL_SYMMETRIC,
425                 DWRITE_MEASURING_MODE_NATURAL,
426                 0.0, 0.0,
427                 &glyphAnalysis
428                 );
429
430     if (SUCCEEDED(hr)) {
431         RECT rect;
432         glyphAnalysis->GetAlphaTextureBounds(DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
433         glyphAnalysis->Release();
434
435         return glyph_metrics_t(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
436                                bbox.xoff, bbox.yoff);
437     } else {
438         return glyph_metrics_t();
439     }
440 }
441
442 glyph_metrics_t QFontEngineDirectWrite::boundingBox(glyph_t g)
443 {
444     if (m_directWriteFontFace == 0)
445         return glyph_metrics_t();
446
447     UINT16 glyphIndex = g;
448
449     DWRITE_GLYPH_METRICS glyphMetrics;
450     HRESULT hr = m_directWriteFontFace->GetDesignGlyphMetrics(&glyphIndex, 1, &glyphMetrics);
451     if (SUCCEEDED(hr)) {
452         QFixed advanceWidth = DESIGN_TO_LOGICAL(glyphMetrics.advanceWidth);
453         QFixed leftSideBearing = DESIGN_TO_LOGICAL(glyphMetrics.leftSideBearing);
454         QFixed rightSideBearing = DESIGN_TO_LOGICAL(glyphMetrics.rightSideBearing);
455         QFixed advanceHeight = DESIGN_TO_LOGICAL(glyphMetrics.advanceHeight);
456         QFixed verticalOriginY = DESIGN_TO_LOGICAL(glyphMetrics.verticalOriginY);
457
458         if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
459             advanceWidth = advanceWidth.round();
460             advanceHeight = advanceHeight.round();
461         }
462
463         QFixed width = advanceWidth - leftSideBearing - rightSideBearing;
464
465         return glyph_metrics_t(-leftSideBearing, -verticalOriginY,
466                                width, m_ascent + m_descent,
467                                advanceWidth, advanceHeight);
468     } else {
469         qErrnoWarning("QFontEngineDirectWrite::boundingBox: GetDesignGlyphMetrics failed");
470     }
471
472     return glyph_metrics_t();
473 }
474
475 QFixed QFontEngineDirectWrite::ascent() const
476 {
477     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
478             ? m_ascent.round()
479             : m_ascent;
480 }
481
482 QFixed QFontEngineDirectWrite::descent() const
483 {
484     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
485            ? (m_descent).round()
486            : (m_descent);
487 }
488
489 QFixed QFontEngineDirectWrite::leading() const
490 {
491     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
492            ? m_lineGap.round()
493            : m_lineGap;
494 }
495
496 QFixed QFontEngineDirectWrite::xHeight() const
497 {
498     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
499            ? m_xHeight.round()
500            : m_xHeight;
501 }
502
503 qreal QFontEngineDirectWrite::maxCharWidth() const
504 {
505     // ###
506     return 0;
507 }
508
509 extern const uint *qt_pow_gamma();
510
511 QImage QFontEngineDirectWrite::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
512                                                 const QTransform &xform)
513 {
514     QImage im = imageForGlyph(glyph, subPixelPosition, 0, xform);
515
516     QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
517     QVector<QRgb> colors(256);
518     for (int i=0; i<256; ++i)
519         colors[i] = qRgba(0, 0, 0, i);
520     indexed.setColorTable(colors);
521
522     uint *gamma = qt_pow_gamma();
523     for (int y=0; y<im.height(); ++y) {
524         uint *src = (uint*) im.scanLine(y);
525         uchar *dst = indexed.scanLine(y);
526         for (int x=0; x<im.width(); ++x) {
527             uint gray = qGray(0xffffffff - *src);
528             *dst = 255 - (gamma ? gamma[gray] * 255. / 2047. : gray);
529             ++dst;
530             ++src;
531         }
532     }
533
534     return indexed;
535 }
536
537 bool QFontEngineDirectWrite::supportsSubPixelPositions() const
538 {
539     return true;
540 }
541
542 QImage QFontEngineDirectWrite::imageForGlyph(glyph_t t,
543                                              QFixed subPixelPosition,
544                                              int margin,
545                                              const QTransform &xform)
546 {
547     UINT16 glyphIndex = t;
548     FLOAT glyphAdvance = 0;
549
550     DWRITE_GLYPH_OFFSET glyphOffset;
551     glyphOffset.advanceOffset = 0;
552     glyphOffset.ascenderOffset = 0;
553
554     DWRITE_GLYPH_RUN glyphRun;
555     glyphRun.fontFace = m_directWriteFontFace;
556     glyphRun.fontEmSize = fontDef.pixelSize;
557     glyphRun.glyphCount = 1;
558     glyphRun.glyphIndices = &glyphIndex;
559     glyphRun.glyphAdvances = &glyphAdvance;
560     glyphRun.isSideways = false;
561     glyphRun.bidiLevel = 0;
562     glyphRun.glyphOffsets = &glyphOffset;
563
564     DWRITE_MATRIX transform;
565     transform.dx = subPixelPosition.toReal();
566     transform.dy = 0;
567     transform.m11 = xform.m11();
568     transform.m12 = xform.m12();
569     transform.m21 = xform.m21();
570     transform.m22 = xform.m22();
571
572     IDWriteGlyphRunAnalysis *glyphAnalysis = NULL;
573     HRESULT hr = m_directWriteFactory->CreateGlyphRunAnalysis(
574                 &glyphRun,
575                 1.0f,
576                 &transform,
577                 DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL_SYMMETRIC,
578                 DWRITE_MEASURING_MODE_NATURAL,
579                 0.0, 0.0,
580                 &glyphAnalysis
581                 );
582
583     if (SUCCEEDED(hr)) {
584         RECT rect;
585         glyphAnalysis->GetAlphaTextureBounds(DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
586
587         rect.left -= margin;
588         rect.top -= margin;
589         rect.right += margin;
590         rect.bottom += margin;
591
592         int width = rect.right - rect.left;
593         int height = rect.bottom - rect.top;
594
595         int size = width * height * 3;
596         if (size > 0) {
597             BYTE *alphaValues = new BYTE[size];
598             memset(alphaValues, size, 0);
599
600             hr = glyphAnalysis->CreateAlphaTexture(DWRITE_TEXTURE_CLEARTYPE_3x1,
601                                                    &rect,
602                                                    alphaValues,
603                                                    size);
604
605             if (SUCCEEDED(hr)) {
606                 QImage img(width, height, QImage::Format_RGB32);
607                 img.fill(0xffffffff);
608
609                 for (int y=0; y<height; ++y) {
610                     uint *dest = reinterpret_cast<uint *>(img.scanLine(y));
611                     BYTE *src = alphaValues + width * 3 * y;
612
613                     for (int x=0; x<width; ++x) {
614                         dest[x] = *(src) << 16
615                                 | *(src + 1) << 8
616                                 | *(src + 2);
617
618                         src += 3;
619                     }
620                 }
621
622                 delete[] alphaValues;
623                 glyphAnalysis->Release();
624
625                 return img;
626             } else {
627                 delete[] alphaValues;
628                 glyphAnalysis->Release();
629
630                 qErrnoWarning("QFontEngineDirectWrite::imageForGlyph: CreateAlphaTexture failed");
631             }
632         }
633     } else {
634         qErrnoWarning("QFontEngineDirectWrite::imageForGlyph: CreateGlyphRunAnalysis failed");
635     }
636
637     return QImage();
638 }
639
640 QImage QFontEngineDirectWrite::alphaRGBMapForGlyph(glyph_t t,
641                                                    QFixed subPixelPosition,
642                                                    const QTransform &xform)
643 {
644     QImage mask = imageForGlyph(t, subPixelPosition,
645                                 glyphMargin(QFontEngineGlyphCache::Raster_RGBMask),
646                                 xform);
647     return mask.depth() == 32
648            ? mask
649            : mask.convertToFormat(QImage::Format_RGB32);
650 }
651
652 const char *QFontEngineDirectWrite::name() const
653 {
654     return 0;
655 }
656
657 bool QFontEngineDirectWrite::canRender(const QChar *string, int len)
658 {
659     QVarLengthArray<UINT32> codePoints(len);
660     int actualLength = 0;
661     for (int i=0; i<len; ++i, actualLength++)
662         codePoints[actualLength] = getChar(string, i, len);
663
664     QVarLengthArray<UINT16> glyphIndices(actualLength);
665     HRESULT hr = m_directWriteFontFace->GetGlyphIndices(codePoints.data(), actualLength,
666                                                         glyphIndices.data());
667     if (FAILED(hr)) {
668         qErrnoWarning(hr, "QFontEngineDirectWrite::canRender: GetGlyphIndices failed");
669         return false;
670     } else {
671         for (int i=0; i<glyphIndices.size(); ++i) {
672             if (glyphIndices.at(i) == 0)
673                 return false;
674         }
675
676         return true;
677     }
678 }
679
680 QFontEngine::Type QFontEngineDirectWrite::type() const
681 {
682     return QFontEngine::DirectWrite;
683 }
684
685 QFontEngine *QFontEngineDirectWrite::cloneWithSize(qreal pixelSize) const
686 {
687     QFontEngine *fontEngine = new QFontEngineDirectWrite(m_directWriteFactory, m_directWriteFontFace,
688                                                          pixelSize);
689
690     fontEngine->fontDef = fontDef;
691     fontEngine->fontDef.pixelSize = pixelSize;
692
693     return fontEngine;
694 }
695
696 QT_END_NAMESPACE
697
698 #endif // QT_NO_DIRECTWRITE