Qt 4.8
qabstractfontengine_qws.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 #include "qabstractfontengine_p.h"
44 
45 #include <private/qtextengine_p.h>
46 #include <private/qpaintengine_raster_p.h>
47 
48 #include <qmath.h>
49 
51 
53 {
54 public:
56  : pixelSize(0), weight(QFont::Normal), style(QFont::StyleNormal)
57  {}
58 
61  int weight;
64 };
65 
88 {
90 }
91 
98 {
100  d->family = family;
101 }
102 
107  : d(new QFontEngineInfoPrivate(*other.d))
108 {
109 }
110 
116 {
117  *d = *other.d;
118  return *this;
119 }
120 
125 {
126  delete d;
127 }
128 
135 {
136  d->family = family;
137 }
138 
140 {
141  return d->family;
142 }
143 
155 {
156  d->pixelSize = size;
157 }
158 
160 {
161  return d->pixelSize;
162 }
163 
175 {
176  d->weight = weight;
177 }
178 
180 {
181  return d->weight;
182 }
183 
190 {
191  d->style = style;
192 }
193 
195 {
196  return d->style;
197 }
198 
210 {
211  return d->writingSystems;
212 }
213 
215 {
217 }
218 
220 {
222 
223  QString foundry;
224 };
225 
253  : QObject(*new QFontEnginePluginPrivate, parent)
254 {
256  d->foundry = foundry;
257 }
258 
263 {
264 }
265 
271 {
272  Q_D(const QFontEnginePlugin);
273  return QStringList(d->foundry);
274 }
275 
294 {
296 public:
297 };
298 
299 //The <classname> class is|provides|contains|specifies...
472  : QObject(*new QAbstractFontEnginePrivate, parent)
473 {
474 }
475 
480 {
481 }
482 
540 bool QAbstractFontEngine::renderGlyph(uint glyph, int depth, int bytesPerLine, int height, uchar *buffer)
541 {
542  Q_UNUSED(glyph)
543  Q_UNUSED(depth)
544  Q_UNUSED(bytesPerLine)
545  Q_UNUSED(height)
546  Q_UNUSED(buffer)
547  qWarning("QAbstractFontEngine: renderGlyph is not implemented in font plugin!");
548  return false;
549 }
550 
556 {
557  Q_UNUSED(glyphs)
558  Q_UNUSED(numGlyphs)
559  Q_UNUSED(positions)
560  Q_UNUSED(path)
561  qWarning("QAbstractFontEngine: addGlyphOutlinesToPath is not implemented in font plugin!");
562 }
563 
564 /*
565 bool QAbstractFontEngine::supportsExtension(Extension extension) const
566 {
567  Q_UNUSED(extension)
568  return false;
569 }
570 
571 QVariant QAbstractFontEngine::extension(Extension extension, const QVariant &argument)
572 {
573  Q_UNUSED(argument)
574  Q_UNUSED(extension)
575  return QVariant();
576 }
577 */
578 
580  : engine(customEngine)
581 {
582  fontDef = def;
584 }
585 
587 {
588  delete engine;
589 }
590 
591 bool QProxyFontEngine::stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
592 {
593  if (*nglyphs < len) {
594  *nglyphs = len;
595  return false;
596  }
597 
598  QVarLengthArray<uint> glyphIndicies(*nglyphs);
599  if (!engine->convertStringToGlyphIndices(str, len, glyphIndicies.data(), nglyphs, QAbstractFontEngine::TextShapingFlags(int(flags))))
600  return false;
601 
602  // ### use memcopy instead
603  for (int i = 0; i < *nglyphs; ++i) {
604  glyphs->glyphs[i] = glyphIndicies[i];
605  }
606  glyphs->numGlyphs = *nglyphs;
607 
608  recalcAdvances(glyphs, flags);
609  return true;
610 }
611 
612 void QProxyFontEngine::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
613 {
614  const int nglyphs = glyphs->numGlyphs;
615 
617  engine->getGlyphAdvances(glyphs->glyphs, nglyphs, advances.data(), QAbstractFontEngine::TextShapingFlags(int(flags)));
618 
619 
620  // ### use memcopy instead
621  for (int i = 0; i < nglyphs; ++i) {
622  glyphs->advances_x[i] = QFixed::fromFixed(advances[i]);
623  glyphs->advances_y[i] = 0;
624  }
625 }
626 
627 
629 {
630  glyph_metrics_t gm = fe->boundingBox(glyph);
631  int glyph_x = qFloor(gm.x.toReal());
632  int glyph_y = qFloor(gm.y.toReal());
633  int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
634  int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
635 
636  if (glyph_width <= 0 || glyph_height <= 0)
637  return QImage();
638  QFixedPoint pt;
639  pt.x = 0;
640  pt.y = -glyph_y; // the baseline
641  QPainterPath path;
642  QImage im(glyph_width + qAbs(glyph_x) + 4, glyph_height, QImage::Format_ARGB32_Premultiplied);
643  im.fill(Qt::transparent);
644  QPainter p(&im);
646  fe->addGlyphsToPath(&glyph, &pt, 1, &path, 0);
647  p.setPen(Qt::NoPen);
648  p.setBrush(Qt::black);
649  p.drawPath(path);
650  p.end();
651 
652  QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
653  QVector<QRgb> colors(256);
654  for (int i=0; i<256; ++i)
655  colors[i] = qRgba(0, 0, 0, i);
656  indexed.setColorTable(colors);
657 
658  for (int y=0; y<im.height(); ++y) {
659  uchar *dst = (uchar *) indexed.scanLine(y);
660  uint *src = (uint *) im.scanLine(y);
661  for (int x=0; x<im.width(); ++x)
662  dst[x] = qAlpha(src[x]);
663  }
664 
665  return indexed;
666 }
667 
668 
670 {
672  return alphaMapFromPath(this, glyph);
673 
675  if (metrics.width <= 0 || metrics.height <= 0)
676  return QImage();
677 
678  QImage img(metrics.width >> 6, metrics.height >> 6, QImage::Format_Indexed8);
679 
680  // ### we should have QImage::Format_GrayScale8
681  static QVector<QRgb> colorMap;
682  if (colorMap.isEmpty()) {
683  colorMap.resize(256);
684  for (int i=0; i<256; ++i)
685  colorMap[i] = qRgba(0, 0, 0, i);
686  }
687 
688  img.setColorTable(colorMap);
689 
690  engine->renderGlyph(glyph, /*depth*/8, img.bytesPerLine(), img.height(), img.bits());
691 
692  return img;
693 }
694 
695 void QProxyFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs, QPainterPath *path, QTextItem::RenderFlags flags)
696 {
698  engine->addGlyphOutlinesToPath(glyphs, nglyphs, reinterpret_cast<QAbstractFontEngine::FixedPoint *>(positions), path);
699  else
700  QFontEngine::addGlyphsToPath(glyphs, positions, nglyphs, path, flags);
701 }
702 
704 {
705  if (glyphs.numGlyphs == 0)
706  return glyph_metrics_t();
707 
708  QFixed w = 0;
709  for (int i = 0; i < glyphs.numGlyphs; ++i)
710  w += glyphs.effectiveAdvance(i);
711 
712  return glyph_metrics_t(0, -ascent(), w, ascent() + descent(), w, 0);
713 }
714 
716 {
717  glyph_metrics_t m;
718 
720  m.x = QFixed::fromFixed(metrics.x);
721  m.y = QFixed::fromFixed(metrics.y);
722  m.width = QFixed::fromFixed(metrics.width);
723  m.height = QFixed::fromFixed(metrics.height);
724  m.xoff = QFixed::fromFixed(metrics.advance);
725 
726  return m;
727 }
728 
730 {
732 }
733 
735 {
737 }
738 
740 {
742 }
743 
745 {
747 }
748 
750 {
752 }
753 
755 {
757 }
758 
760 {
762 }
763 
765 {
767 }
768 
770 {
772 }
773 
775 {
777 }
778 
780 {
782 }
783 
784 bool QProxyFontEngine::canRender(const QChar *string, int len)
785 {
786  QVarLengthArray<uint> glyphs(len);
787  int numGlyphs = len;
788 
789  if (!engine->convertStringToGlyphIndices(string, len, glyphs.data(), &numGlyphs, /*flags*/0))
790  return false;
791 
792  for (int i = 0; i < numGlyphs; ++i)
793  if (!glyphs[i])
794  return false;
795 
796  return true;
797 }
798 
799 void QProxyFontEngine::draw(QPaintEngine *p, qreal _x, qreal _y, const QTextItemInt &si)
800 {
801  QPaintEngineState *pState = p->state;
802  QRasterPaintEngine *paintEngine = static_cast<QRasterPaintEngine*>(p);
803 
804  QTransform matrix = pState->transform();
805  matrix.translate(_x, _y);
806  QFixed x = QFixed::fromReal(matrix.dx());
807  QFixed y = QFixed::fromReal(matrix.dy());
808 
811  getGlyphPositions(si.glyphs, matrix, si.flags, glyphs, positions);
812  if (glyphs.size() == 0)
813  return;
814 
815  for(int i = 0; i < glyphs.size(); i++) {
816  QImage glyph = alphaMapForGlyph(glyphs[i]);
817  if (glyph.isNull())
818  continue;
819 
820  if (glyph.format() != QImage::Format_Indexed8
821  && glyph.format() != QImage::Format_Mono)
822  continue;
823 
825 
826  int depth = glyph.format() == QImage::Format_Mono ? 1 : 8;
827  paintEngine->alphaPenBlt(glyph.bits(), glyph.bytesPerLine(), depth,
828  qRound(positions[i].x + QFixed::fromFixed(metrics.x)),
829  qRound(positions[i].y + QFixed::fromFixed(metrics.y)),
830  glyph.width(), glyph.height());
831  }
832 }
833 
834 /*
835  * This is only called when we use the proxy fontengine directly (without sharing the rendered
836  * glyphs). So we prefer outline rendering over rendering of unshared glyphs. That decision is
837  * done in qfontdatabase_qws.cpp by looking at the ShareGlyphsHint and the pixel size of the font.
838  */
840 {
842  return false;
843 
845  return !outlineHint.isValid() || outlineHint.toBool();
846 }
847 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
double d
Definition: qnumeric_p.h:62
qreal dy() const
Returns the vertical translation factor.
Definition: qtransform.h:277
void drawPath(const QPainterPath &path)
Draws the given painter path using the current pen for outline and the current brush for filling...
Definition: qpainter.cpp:3502
QFixed * advances_y
static QFixed fromFixed(int fixed)
Definition: qfixed_p.h:71
virtual int glyphCount() const
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
virtual QStringList keys() const
Returns a list of foundries the font engine plugin provides.
QFixed * advances_x
int qCeil(qreal v)
Definition: qmath.h:63
void setColorTable(const QVector< QRgb > colors)
Sets the color table used to translate color indexes to QRgb values, to the specified colors...
Definition: qimage.cpp:1744
QProxyFontEngine(QAbstractFontEngine *engine, const QFontDef &def)
Q_GUI_EXPORT_INLINE int qAlpha(QRgb rgb)
Definition: qrgb.h:66
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
HB_Glyph * glyphs
QPaintEngineState * state
Definition: qpaintengine.h:239
int qFloor(qreal v)
Definition: qmath.h:73
~QFontEngineInfo()
Destroys this QFontEngineInfo object.
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition: qimage.cpp:1542
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition: qimage.cpp:2032
RenderFlags flags
QTransform transform() const
Returns the matrix in the current paint engine state.
Definition: qpainter.cpp:9377
bool toBool() const
Returns the variant as a bool if the variant has type() Bool.
Definition: qvariant.cpp:2691
QFontEngineInfo & operator=(const QFontEngineInfo &other)
Assigns other to this font engine info object, and returns a reference to this.
int bytesPerLine() const
Returns the number of bytes per image scanline.
Definition: qimage.cpp:1812
virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs, QPainterPath *path, QTextItem::RenderFlags flags)
QList< QFontDatabase::WritingSystem > writingSystems
virtual QFixed descent() const
QTransform & translate(qreal dx, qreal dy)
Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to t...
Definition: qtransform.cpp:417
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QImage alphaMapForGlyph(glyph_t)
virtual QFixed leading() const
Fixed advance
The advance of the glyph.
static QFixed fromReal(qreal r)
Definition: qfixed_p.h:70
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Format format() const
Returns the format of the image.
Definition: qimage.cpp:2305
QString family() const
the family name of the font
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
void setStyle(QFont::Style style)
virtual QVariant fontProperty(FontProperty property) const =0
Implemented in subclasses to return the value of the font attribute property.
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
The QFontEnginePlugin class is the base class for font engine factory plugins in Qt for Embedded Linu...
QGlyphLayout glyphs
void setWeight(int weight)
unsigned char uchar
Definition: qglobal.h:994
virtual QFixed lineThickness() const
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint...
Definition: qpainter.cpp:7620
QAbstractFontEngine::FixedPoint defines a point in the place using 26.6 fixed point precision...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_GUI_EXPORT_INLINE QRgb qRgba(int r, int g, int b, int a)
Definition: qrgb.h:72
QFixed y
Definition: qfixed_p.h:191
virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs)
QList< QFontDatabase::WritingSystem > writingSystems() const
An empty list means that any writing system is supported.
virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const
The QFontEngineInfo class describes a specific font provided by a font engine plugin.
static const QCssKnownValue positions[NumKnownPositionModes - 1]
Definition: qcssparser.cpp:329
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags, QVarLengthArray< glyph_t > &glyphs_out, QVarLengthArray< QFixedPoint > &positions)
virtual Capabilities capabilities() const =0
Implemented in subclasses to specify the font engine&#39;s capabilities.
Q_CORE_EXPORT void qWarning(const char *,...)
Internal QTextItem.
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
unsigned int uint
Definition: qglobal.h:996
QAbstractFontEngine(QObject *parent=0)
Constructs a new QAbstractFontEngine with the given parent.
void setPixelSize(qreal size)
virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs)=0
QFontEngineInfo()
Constructs a new empty QFontEngineInfo.
virtual GlyphMetrics glyphMetrics(uint glyph) const =0
Implemented in subclass to return the metrics for glyph.
void setFamily(const QString &name)
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:90
Style
This enum describes the different styles of glyphs that are used to display text. ...
Definition: qfont.h:111
virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs, QPainterPath *path, QTextItem::RenderFlags flags)
void setWritingSystems(const QList< QFontDatabase::WritingSystem > &writingSystems)
virtual bool convertStringToGlyphIndices(const QChar *string, int length, uint *glyphs, int *numGlyphs, TextShapingFlags flags) const =0
Implemented in subclasses to convert the characters specified by string and length to glyph indicies...
QFixed x
Definition: qfixed_p.h:190
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
QFont::Style style() const
the style of the font
Fixed height
The height of the glyph.
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
virtual qreal maxCharWidth() const
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QAbstractFontEngine::GlyphMetrics defines the metrics of a single glyph.
QFontEngineInfoPrivate * d
void alphaPenBlt(const void *src, int bpl, int depth, int rx, int ry, int w, int h)
void setBrush(const QBrush &brush)
Sets the painter&#39;s brush to the given brush.
Definition: qpainter.cpp:4171
~QFontEnginePlugin()
Destroys this font engine plugin.
QAbstractFontEngine * engine
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
Fixed width
The width of the glyph.
qreal toReal() const
Definition: qfixed_p.h:77
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
qreal dx() const
Returns the horizontal translation factor.
Definition: qtransform.h:273
virtual bool canRender(const QChar *string, int len)
QFixed effectiveAdvance(int item) const
qreal pixelSize() const
A pixel size of 0 represents a freely scalable font.
QAbstractFontEngine::Capabilities engineCapabilities
virtual QFixed averageCharWidth() const
The QPaintEngineState class provides information about the active paint engine&#39;s current state...
Definition: qpaintengine.h:289
int weight() const
The value should be from the QFont::Weight enumeration.
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
static QImage alphaMapFromPath(QFontEngine *fe, glyph_t glyph)
The QRasterPaintEngine class enables hardware acceleration of painting operations in Qt for Embedded ...
virtual qreal minLeftBearing() const
QFontDef fontDef
~QAbstractFontEngine()
Destroys this QAbstractFontEngine object.
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
virtual void getGlyphAdvances(const uint *glyphs, int numGlyphs, Fixed *advances, TextShapingFlags flags) const =0
Implemented in subclasses to retrieve the advances of the array specified by glyphs and numGlyphs...
virtual qreal minRightBearing() const
virtual bool renderGlyph(uint glyph, int depth, int bytesPerLine, int height, uchar *buffer)
Implemented in subclasses to render the specified glyph into a buffer with the given depth ...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
virtual QFixed underlinePosition() const
virtual void addGlyphOutlinesToPath(uint *glyphs, int numGlyphs, FixedPoint *positions, QPainterPath *path)
Implemented in subclasses to add the outline of the glyphs specified by glyphs and numGlyphs at the s...
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const
unsigned int glyph_t
The QAbstractFontEngine class is the base class for font engine plugins in Qt for Embedded Linux...
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
Fixed x
The horizontal offset from the origin.
virtual QFixed ascent() const
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886
bool end()
Ends painting.
Definition: qpainter.cpp:1929
Fixed y
The vertical offset from the origin (baseline).
int size() const
static qreal toReal(Register *reg, int type, bool *ok=0)
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
virtual QFixed xHeight() const