Fix uses of qRound on non-floating-point types.
[profile/ivi/qtbase.git] / src / gui / text / qfontenginedirectwrite.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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     if (m_directWriteFontFace) {
229         DWORD t = qbswap<quint32>(tag);
230
231         const void *tableData = 0;
232         void *tableContext = 0;
233         UINT32 tableSize;
234         BOOL exists;
235         HRESULT hr = m_directWriteFontFace->TryGetFontTable(
236                     t, &tableData, &tableSize, &tableContext, &exists
237                     );
238
239         if (SUCCEEDED(hr)) {
240             if (!exists)
241                 return false;
242
243             if (buffer == 0) {
244                 *length = tableSize;
245                 return true;
246             } else if (*length < tableSize) {
247                 return false;
248             }
249
250             qMemCopy(buffer, tableData, tableSize);
251             m_directWriteFontFace->ReleaseFontTable(tableContext);
252
253             return true;
254         } else {
255             qErrnoWarning("QFontEngineDirectWrite::getSfntTableData: TryGetFontTable failed");
256         }
257     }
258
259     return false;
260 }
261
262 QFixed QFontEngineDirectWrite::emSquareSize() const
263 {
264     if (m_unitsPerEm > 0)
265         return m_unitsPerEm;
266     else
267         return QFontEngine::emSquareSize();
268 }
269
270 inline unsigned int getChar(const QChar *str, int &i, const int len)
271 {
272     unsigned int uc = str[i].unicode();
273     if (uc >= 0xd800 && uc < 0xdc00 && i < len-1) {
274         uint low = str[i+1].unicode();
275        if (low >= 0xdc00 && low < 0xe000) {
276             uc = (uc - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
277             ++i;
278         }
279     }
280     return uc;
281 }
282
283 bool QFontEngineDirectWrite::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs,
284                                           int *nglyphs, QTextEngine::ShaperFlags flags) const
285 {
286     if (m_directWriteFontFace != 0) {
287         QVarLengthArray<UINT32> codePoints(len);
288         for (int i=0; i<len; ++i) {
289             codePoints[i] = getChar(str, i, len);
290             if (flags & QTextEngine::RightToLeft)
291                 codePoints[i] = QChar::mirroredChar(codePoints[i]);
292         }
293
294         QVarLengthArray<UINT16> glyphIndices(len);
295         HRESULT hr = m_directWriteFontFace->GetGlyphIndicesW(codePoints.data(),
296                                                              len,
297                                                              glyphIndices.data());
298
299         if (SUCCEEDED(hr)) {
300             for (int i=0; i<len; ++i)
301                 glyphs->glyphs[i] = glyphIndices[i];
302
303             *nglyphs = len;
304
305             if (!(flags & QTextEngine::GlyphIndicesOnly))
306                 recalcAdvances(glyphs, 0);
307
308             return true;
309         } else {
310             qErrnoWarning("QFontEngineDirectWrite::stringToCMap: GetGlyphIndicesW failed");
311         }
312     }
313
314     return false;
315 }
316
317 void QFontEngineDirectWrite::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
318 {
319     if (m_directWriteFontFace == 0)
320         return;
321
322     QVarLengthArray<UINT16> glyphIndices(glyphs->numGlyphs);
323
324     // ### Caching?
325     for(int i=0; i<glyphs->numGlyphs; i++)
326         glyphIndices[i] = UINT16(glyphs->glyphs[i]);
327
328     QVarLengthArray<DWRITE_GLYPH_METRICS> glyphMetrics(glyphIndices.size());
329     HRESULT hr = m_directWriteFontFace->GetDesignGlyphMetrics(glyphIndices.data(),
330                                                               glyphIndices.size(),
331                                                               glyphMetrics.data());
332     if (SUCCEEDED(hr)) {
333         for (int i=0; i<glyphs->numGlyphs; ++i) {
334             glyphs->advances_x[i] = DESIGN_TO_LOGICAL(glyphMetrics[i].advanceWidth);
335             if (fontDef.styleStrategy & QFont::ForceIntegerMetrics)
336                 glyphs->advances_x[i] = glyphs->advances_x[i].round();
337             glyphs->advances_y[i] = 0;
338         }
339     } else {
340         qErrnoWarning("QFontEngineDirectWrite::recalcAdvances: GetDesignGlyphMetrics failed");
341     }
342 }
343
344 void QFontEngineDirectWrite::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs,
345                                              QPainterPath *path, QTextItem::RenderFlags flags)
346 {
347     if (m_directWriteFontFace == 0)
348         return;
349
350     QVarLengthArray<UINT16> glyphIndices(nglyphs);
351     QVarLengthArray<DWRITE_GLYPH_OFFSET> glyphOffsets(nglyphs);
352     QVarLengthArray<FLOAT> glyphAdvances(nglyphs);
353
354     for (int i=0; i<nglyphs; ++i) {
355         glyphIndices[i] = glyphs[i];
356         glyphOffsets[i].advanceOffset  = positions[i].x.toReal();
357         glyphOffsets[i].ascenderOffset = -positions[i].y.toReal();
358         glyphAdvances[i] = 0.0;
359     }
360
361     GeometrySink geometrySink(path);
362     HRESULT hr = m_directWriteFontFace->GetGlyphRunOutline(
363                 fontDef.pixelSize,
364                 glyphIndices.data(),
365                 glyphAdvances.data(),
366                 glyphOffsets.data(),
367                 nglyphs,
368                 false,
369                 flags & QTextItem::RightToLeft,
370                 &geometrySink
371                 );
372
373     if (FAILED(hr))
374         qErrnoWarning("QFontEngineDirectWrite::addGlyphsToPath: GetGlyphRunOutline failed");
375 }
376
377 glyph_metrics_t QFontEngineDirectWrite::boundingBox(const QGlyphLayout &glyphs)
378 {
379     if (glyphs.numGlyphs == 0)
380         return glyph_metrics_t();
381
382     bool round = fontDef.styleStrategy & QFont::ForceIntegerMetrics;
383
384     QFixed w = 0;
385     for (int i = 0; i < glyphs.numGlyphs; ++i) {
386         w += round ? glyphs.effectiveAdvance(i).round() : glyphs.effectiveAdvance(i);
387
388     }
389
390     return glyph_metrics_t(0, -m_ascent, w - lastRightBearing(glyphs), m_ascent + m_descent, w, 0);
391 }
392
393 glyph_metrics_t QFontEngineDirectWrite::alphaMapBoundingBox(glyph_t glyph, QFixed subPixelPosition,
394                                                             const QTransform &matrix,
395                                                             GlyphFormat /*format*/)
396 {
397     glyph_metrics_t bbox = QFontEngine::boundingBox(glyph, matrix); // To get transformed advance
398
399     UINT16 glyphIndex = glyph;
400     FLOAT glyphAdvance = 0;
401
402     DWRITE_GLYPH_OFFSET glyphOffset;
403     glyphOffset.advanceOffset = 0;
404     glyphOffset.ascenderOffset = 0;
405
406     DWRITE_GLYPH_RUN glyphRun;
407     glyphRun.fontFace = m_directWriteFontFace;
408     glyphRun.fontEmSize = fontDef.pixelSize;
409     glyphRun.glyphCount = 1;
410     glyphRun.glyphIndices = &glyphIndex;
411     glyphRun.glyphAdvances = &glyphAdvance;
412     glyphRun.isSideways = false;
413     glyphRun.bidiLevel = 0;
414     glyphRun.glyphOffsets = &glyphOffset;
415
416     DWRITE_MATRIX transform;
417     transform.dx = subPixelPosition.toReal();
418     transform.dy = 0;
419     transform.m11 = matrix.m11();
420     transform.m12 = matrix.m12();
421     transform.m21 = matrix.m21();
422     transform.m22 = matrix.m22();
423
424     IDWriteGlyphRunAnalysis *glyphAnalysis = NULL;
425     HRESULT hr = m_directWriteFactory->CreateGlyphRunAnalysis(
426                 &glyphRun,
427                 1.0f,
428                 &transform,
429                 DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL_SYMMETRIC,
430                 DWRITE_MEASURING_MODE_NATURAL,
431                 0.0, 0.0,
432                 &glyphAnalysis
433                 );
434
435     if (SUCCEEDED(hr)) {
436         RECT rect;
437         glyphAnalysis->GetAlphaTextureBounds(DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
438         glyphAnalysis->Release();
439
440         return glyph_metrics_t(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
441                                bbox.xoff, bbox.yoff);
442     } else {
443         return glyph_metrics_t();
444     }
445 }
446
447 glyph_metrics_t QFontEngineDirectWrite::boundingBox(glyph_t g)
448 {
449     if (m_directWriteFontFace == 0)
450         return glyph_metrics_t();
451
452     UINT16 glyphIndex = g;
453
454     DWRITE_GLYPH_METRICS glyphMetrics;
455     HRESULT hr = m_directWriteFontFace->GetDesignGlyphMetrics(&glyphIndex, 1, &glyphMetrics);
456     if (SUCCEEDED(hr)) {
457         QFixed advanceWidth = DESIGN_TO_LOGICAL(glyphMetrics.advanceWidth);
458         QFixed leftSideBearing = DESIGN_TO_LOGICAL(glyphMetrics.leftSideBearing);
459         QFixed rightSideBearing = DESIGN_TO_LOGICAL(glyphMetrics.rightSideBearing);
460         QFixed advanceHeight = DESIGN_TO_LOGICAL(glyphMetrics.advanceHeight);
461         QFixed verticalOriginY = DESIGN_TO_LOGICAL(glyphMetrics.verticalOriginY);
462
463         if (fontDef.styleStrategy & QFont::ForceIntegerMetrics) {
464             advanceWidth = advanceWidth.round();
465             advanceHeight = advanceHeight.round();
466         }
467
468         QFixed width = advanceWidth - leftSideBearing - rightSideBearing;
469
470         return glyph_metrics_t(-leftSideBearing, -verticalOriginY,
471                                width, m_ascent + m_descent,
472                                advanceWidth, advanceHeight);
473     } else {
474         qErrnoWarning("QFontEngineDirectWrite::boundingBox: GetDesignGlyphMetrics failed");
475     }
476
477     return glyph_metrics_t();
478 }
479
480 QFixed QFontEngineDirectWrite::ascent() const
481 {
482     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
483             ? m_ascent.round()
484             : m_ascent;
485 }
486
487 QFixed QFontEngineDirectWrite::descent() const
488 {
489     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
490            ? (m_descent - 1).round()
491            : (m_descent - 1);
492 }
493
494 QFixed QFontEngineDirectWrite::leading() const
495 {
496     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
497            ? m_lineGap.round()
498            : m_lineGap;
499 }
500
501 QFixed QFontEngineDirectWrite::xHeight() const
502 {
503     return fontDef.styleStrategy & QFont::ForceIntegerMetrics
504            ? m_xHeight.round()
505            : m_xHeight;
506 }
507
508 qreal QFontEngineDirectWrite::maxCharWidth() const
509 {
510     // ###
511     return 0;
512 }
513
514 extern uint qt_pow_gamma[256];
515
516 QImage QFontEngineDirectWrite::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition,
517                                                 const QTransform &xform)
518 {
519     QImage im = imageForGlyph(glyph, subPixelPosition, 0, xform);
520
521     QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
522     QVector<QRgb> colors(256);
523     for (int i=0; i<256; ++i)
524         colors[i] = qRgba(0, 0, 0, i);
525     indexed.setColorTable(colors);
526
527     for (int y=0; y<im.height(); ++y) {
528         uint *src = (uint*) im.scanLine(y);
529         uchar *dst = indexed.scanLine(y);
530         for (int x=0; x<im.width(); ++x) {
531             *dst = 255 - (qt_pow_gamma[qGray(0xffffffff - *src)] * 255. / 2047.);
532             ++dst;
533             ++src;
534         }
535     }
536
537     return indexed;
538 }
539
540 bool QFontEngineDirectWrite::supportsSubPixelPositions() const
541 {
542     return true;
543 }
544
545 QImage QFontEngineDirectWrite::imageForGlyph(glyph_t t,
546                                              QFixed subPixelPosition,
547                                              int margin,
548                                              const QTransform &xform)
549 {
550     UINT16 glyphIndex = t;
551     FLOAT glyphAdvance = 0;
552
553     DWRITE_GLYPH_OFFSET glyphOffset;
554     glyphOffset.advanceOffset = 0;
555     glyphOffset.ascenderOffset = 0;
556
557     DWRITE_GLYPH_RUN glyphRun;
558     glyphRun.fontFace = m_directWriteFontFace;
559     glyphRun.fontEmSize = fontDef.pixelSize;
560     glyphRun.glyphCount = 1;
561     glyphRun.glyphIndices = &glyphIndex;
562     glyphRun.glyphAdvances = &glyphAdvance;
563     glyphRun.isSideways = false;
564     glyphRun.bidiLevel = 0;
565     glyphRun.glyphOffsets = &glyphOffset;
566
567     DWRITE_MATRIX transform;
568     transform.dx = subPixelPosition.toReal();
569     transform.dy = 0;
570     transform.m11 = xform.m11();
571     transform.m12 = xform.m12();
572     transform.m21 = xform.m21();
573     transform.m22 = xform.m22();
574
575     IDWriteGlyphRunAnalysis *glyphAnalysis = NULL;
576     HRESULT hr = m_directWriteFactory->CreateGlyphRunAnalysis(
577                 &glyphRun,
578                 1.0f,
579                 &transform,
580                 DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL_SYMMETRIC,
581                 DWRITE_MEASURING_MODE_NATURAL,
582                 0.0, 0.0,
583                 &glyphAnalysis
584                 );
585
586     if (SUCCEEDED(hr)) {
587         RECT rect;
588         glyphAnalysis->GetAlphaTextureBounds(DWRITE_TEXTURE_CLEARTYPE_3x1, &rect);
589
590         rect.left -= margin;
591         rect.top -= margin;
592         rect.right += margin;
593         rect.bottom += margin;
594
595         int width = rect.right - rect.left;
596         int height = rect.bottom - rect.top;
597
598         int size = width * height * 3;
599         if (size > 0) {
600             BYTE *alphaValues = new BYTE[size];
601             qMemSet(alphaValues, size, 0);
602
603             hr = glyphAnalysis->CreateAlphaTexture(DWRITE_TEXTURE_CLEARTYPE_3x1,
604                                                    &rect,
605                                                    alphaValues,
606                                                    size);
607
608             if (SUCCEEDED(hr)) {
609                 QImage img(width, height, QImage::Format_RGB32);
610                 img.fill(0xffffffff);
611
612                 for (int y=0; y<height; ++y) {
613                     uint *dest = reinterpret_cast<uint *>(img.scanLine(y));
614                     BYTE *src = alphaValues + width * 3 * y;
615
616                     for (int x=0; x<width; ++x) {
617                         dest[x] = *(src) << 16
618                                 | *(src + 1) << 8
619                                 | *(src + 2);
620
621                         src += 3;
622                     }
623                 }
624
625                 delete[] alphaValues;
626                 glyphAnalysis->Release();
627
628                 return img;
629             } else {
630                 delete[] alphaValues;
631                 glyphAnalysis->Release();
632
633                 qErrnoWarning("QFontEngineDirectWrite::imageForGlyph: CreateAlphaTexture failed");
634             }
635         }
636     } else {
637         qErrnoWarning("QFontEngineDirectWrite::imageForGlyph: CreateGlyphRunAnalysis failed");
638     }
639
640     return QImage();
641 }
642
643 QImage QFontEngineDirectWrite::alphaRGBMapForGlyph(glyph_t t,
644                                                    QFixed subPixelPosition,
645                                                    int margin,
646                                                    const QTransform &xform)
647 {
648     QImage mask = imageForGlyph(t, subPixelPosition, margin, xform);
649     return mask.depth() == 32
650            ? mask
651            : mask.convertToFormat(QImage::Format_RGB32);
652 }
653
654 const char *QFontEngineDirectWrite::name() const
655 {
656     return 0;
657 }
658
659 bool QFontEngineDirectWrite::canRender(const QChar *string, int len)
660 {
661     QVarLengthArray<UINT32> codePoints(len);
662     int actualLength = 0;
663     for (int i=0; i<len; ++i, actualLength++)
664         codePoints[actualLength] = getChar(string, i, len);
665
666     QVarLengthArray<UINT16> glyphIndices(actualLength);
667     HRESULT hr = m_directWriteFontFace->GetGlyphIndices(codePoints.data(), actualLength,
668                                                         glyphIndices.data());
669     if (FAILED(hr)) {
670         qErrnoWarning(hr, "QFontEngineDirectWrite::canRender: GetGlyphIndices failed");
671         return false;
672     } else {
673         for (int i=0; i<glyphIndices.size(); ++i) {
674             if (glyphIndices.at(i) == 0)
675                 return false;
676         }
677
678         return true;
679     }
680 }
681
682 QFontEngine::Type QFontEngineDirectWrite::type() const
683 {
684     return QFontEngine::DirectWrite;
685 }
686
687 QFontEngine *QFontEngineDirectWrite::cloneWithSize(qreal pixelSize) const
688 {
689     QFontEngine *fontEngine = new QFontEngineDirectWrite(m_directWriteFactory, m_directWriteFontFace,
690                                                          pixelSize);
691
692     fontEngine->fontDef = fontDef;
693     fontEngine->fontDef.pixelSize = pixelSize;
694
695     return fontEngine;
696 }
697
698 QT_END_NAMESPACE
699
700 #endif // QT_NO_DIRECTWRITE