Qt 4.8
qrawfont.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 
42 #include "qglobal.h"
43 
44 #if !defined(QT_NO_RAWFONT)
45 
46 #include "qrawfont.h"
47 #include "qrawfont_p.h"
48 
49 #include <QtCore/qendian.h>
50 
52 
135  : d(new QRawFontPrivate)
136 {
137 }
138 
149  : d(new QRawFontPrivate)
150 {
151  loadFromFile(fileName, pixelSize, hintingPreference);
152 }
153 
164  : d(new QRawFontPrivate)
165 {
166  loadFromData(fontData, pixelSize, hintingPreference);
167 }
168 
173 {
174  d = other.d;
175 }
176 
181 {
182 }
183 
188 {
189  d = other.d;
190  return *this;
191 }
192 
196 bool QRawFont::isValid() const
197 {
198  return d->isValid();
199 }
200 
213 {
214  QFile file(fileName);
215  if (file.open(QIODevice::ReadOnly))
217 }
218 
228 void QRawFont::loadFromData(const QByteArray &fontData,
231 {
232  d.detach();
233  d->cleanUp();
236  d->platformLoadFromData(fontData, pixelSize, hintingPreference);
237 }
238 
253  const QTransform &transform) const
254 {
255  if (!d->isValid())
256  return QImage();
257 
258  if (antialiasingType == SubPixelAntialiasing)
259  return d->fontEngine->alphaRGBMapForGlyph(glyphIndex, QFixed(), 0, transform);
260 
261  return d->fontEngine->alphaMapForGlyph(glyphIndex, QFixed(), transform);
262 }
263 
273 {
274  if (!d->isValid())
275  return QPainterPath();
276 
278  QPainterPath path;
279  d->fontEngine->addGlyphsToPath(&glyphIndex, &position, 1, &path, 0);
280  return path;
281 }
282 
286 bool QRawFont::operator==(const QRawFont &other) const
287 {
288  return d->fontEngine == other.d->fontEngine;
289 }
290 
306 {
307  return d->isValid() ? d->fontEngine->ascent().toReal() : 0.0;
308 }
309 
316 {
317  return d->isValid() ? d->fontEngine->descent().toReal() : 0.0;
318 }
319 
326 {
327  return d->isValid() ? d->fontEngine->xHeight().toReal() : 0.0;
328 }
329 
336 {
337  return d->isValid() ? d->fontEngine->leading().toReal() : 0.0;
338 }
339 
346 {
347  return d->isValid() ? d->fontEngine->averageCharWidth().toReal() : 0.0;
348 }
349 
356 {
357  return d->isValid() ? d->fontEngine->maxCharWidth() : 0.0;
358 }
359 
368 {
369  return d->isValid() ? d->fontEngine->fontDef.pixelSize : 0.0;
370 }
371 
381 {
382  return d->isValid() ? d->fontEngine->emSquareSize().toReal() : 0.0;
383 }
384 
389 {
390  return d->isValid() ? d->fontEngine->fontDef.family : QString();
391 }
392 
399 {
400  return d->isValid() ? d->fontEngine->fontDef.styleName : QString();
401 }
402 
409 {
411 }
412 
418 int QRawFont::weight() const
419 {
420  return d->isValid() ? int(d->fontEngine->fontDef.weight) : -1;
421 }
422 
437 {
438  if (!d->isValid())
439  return QVector<quint32>();
440 
441  int nglyphs = text.size();
442  QVarLengthGlyphLayoutArray glyphs(nglyphs);
443  if (!glyphIndexesForChars(text.data(), text.size(), glyphs.glyphs, &nglyphs)) {
444  glyphs.resize(nglyphs);
445  if (!glyphIndexesForChars(text.data(), text.size(), glyphs.glyphs, &nglyphs)) {
446  Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice");
447  return QVector<quint32>();
448  }
449  }
450 
451  QVector<quint32> glyphIndexes;
452  for (int i=0; i<nglyphs; ++i)
453  glyphIndexes.append(glyphs.glyphs[i]);
454 
455  return glyphIndexes;
456 }
457 
469 bool QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
470 {
471  if (!d->isValid())
472  return false;
473 
474  QGlyphLayout glyphs;
475  glyphs.glyphs = glyphIndexes;
476  return d->fontEngine->stringToCMap(chars, numChars, &glyphs, numGlyphs, QTextEngine::GlyphIndicesOnly);
477 }
478 
487 {
488  if (!d->isValid())
489  return QVector<QPointF>();
490 
491  int numGlyphs = glyphIndexes.size();
492  QVarLengthGlyphLayoutArray glyphs(numGlyphs);
493  qMemCopy(glyphs.glyphs, glyphIndexes.data(), numGlyphs * sizeof(quint32));
494 
495  d->fontEngine->recalcAdvances(&glyphs, 0);
496 
497  QVector<QPointF> advances;
498  for (int i=0; i<numGlyphs; ++i)
499  advances.append(QPointF(glyphs.advances_x[i].toReal(), glyphs.advances_y[i].toReal()));
500 
501  return advances;
502 }
503 
513 bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const
514 {
515  if (!d->isValid())
516  return false;
517 
518  QGlyphLayout glyphs;
519  glyphs.glyphs = const_cast<HB_Glyph *>(glyphIndexes);
520  glyphs.numGlyphs = numGlyphs;
521  QVarLengthArray<QFixed> advances_x(numGlyphs);
522  QVarLengthArray<QFixed> advances_y(numGlyphs);
523  glyphs.advances_x = advances_x.data();
524  glyphs.advances_y = advances_y.data();
525 
526  d->fontEngine->recalcAdvances(&glyphs, 0);
527 
528  for (int i=0; i<numGlyphs; ++i)
529  advances[i] = QPointF(glyphs.advances_x[i].toReal(), glyphs.advances_y[i].toReal());
530 
531  return true;
532 }
533 
540 {
542 }
543 
550 QByteArray QRawFont::fontTable(const char *tagName) const
551 {
552  if (!d->isValid())
553  return QByteArray();
554 
555  const quint32 *tagId = reinterpret_cast<const quint32 *>(tagName);
556  return d->fontEngine->getSfntTable(qToBigEndian(*tagId));
557 }
558 
559 // From qfontdatabase.cpp
561 
574 {
575  if (d->isValid()) {
576  QByteArray os2Table = fontTable("OS/2");
577  if (os2Table.size() > 86) {
578  char *data = os2Table.data();
579  quint32 *bigEndianUnicodeRanges = reinterpret_cast<quint32 *>(data + 42);
580  quint32 *bigEndianCodepageRanges = reinterpret_cast<quint32 *>(data + 78);
581 
582  quint32 unicodeRanges[4];
583  quint32 codepageRanges[2];
584 
585  for (int i=0; i<4; ++i) {
586  if (i < 2)
587  codepageRanges[i] = qFromBigEndian(bigEndianCodepageRanges[i]);
588  unicodeRanges[i] = qFromBigEndian(bigEndianUnicodeRanges[i]);
589  }
590 
591  return qt_determine_writing_systems_from_truetype_bits(unicodeRanges, codepageRanges);
592  }
593  }
594 
596 }
597 
603 bool QRawFont::supportsCharacter(QChar character) const
604 {
605  return d->isValid() && d->fontEngine->canRender(&character, 1);
606 }
607 
618 {
619  QChar str[2];
620  int len;
621  if (!QChar::requiresSurrogates(ucs4)) {
622  str[0] = QChar(ucs4);
623  len = 1;
624  } else {
625  str[0] = QChar(QChar::highSurrogate(ucs4));
626  str[1] = QChar(QChar::lowSurrogate(ucs4));
627  len = 2;
628  }
629 
630  return d->isValid() && d->fontEngine->canRender(str, len);
631 }
632 
633 // qfontdatabase.cpp
635 
641 {
642  QRawFont rawFont;
643 #if defined(Q_WS_MAC)
645  layout.beginLayout();
646  QTextLine line = layout.createLine();
647  layout.endLayout();
648  QList<QGlyphRun> list = layout.glyphRuns();
649  if (list.size()) {
650  // Pick the one matches the family name we originally requested,
651  // if none of them match, just pick the first one
652  for (int i = 0; i < list.size(); i++) {
653  rawFont = list.at(i).rawFont();
654  if (rawFont.familyName() == font.family())
655  return rawFont;
656  }
657  return list.at(0).rawFont();
658  }
659 #else
660  QFontPrivate *font_d = QFontPrivate::get(font);
661  int script = qt_script_for_writing_system(writingSystem);
662  QFontEngine *fe = font_d->engineForScript(script);
663 
664  if (fe != 0 && fe->type() == QFontEngine::Multi) {
665  QFontEngineMulti *multiEngine = static_cast<QFontEngineMulti *>(fe);
666  fe = multiEngine->engine(0);
667  if (fe == 0) {
668  multiEngine->loadEngine(0);
669  fe = multiEngine->engine(0);
670  }
671  }
672 
673  if (fe != 0) {
674  rawFont.d.data()->fontEngine = fe;
675  rawFont.d.data()->fontEngine->ref.ref();
676  rawFont.d.data()->hintingPreference = font.hintingPreference();
677  }
678 #endif
679  return rawFont;
680 }
681 
686 {
687  if (d->fontEngine == 0)
688  return;
689 
690  d.detach();
691  QFontEngine *oldFontEngine = d->fontEngine;
692 
693  d->fontEngine = d->fontEngine->cloneWithSize(pixelSize);
694  if (d->fontEngine != 0)
695  d->fontEngine->ref.ref();
696 
697  oldFontEngine->ref.deref();
698  if (oldFontEngine->cache_count == 0 && oldFontEngine->ref == 0)
699  delete oldFontEngine;
700 }
701 
706 {
707  platformCleanUp();
708  if (fontEngine != 0) {
709  fontEngine->ref.deref();
710  if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
711  delete fontEngine;
712  fontEngine = 0;
713  }
715 }
716 
718 
719 #endif // QT_NO_RAWFONT
QAtomicInt ref
double d
Definition: qnumeric_p.h:62
QRawFont & operator=(const QRawFont &other)
Assigns other to this QRawFont.
Definition: qrawfont.cpp:187
virtual QFixed averageCharWidth() const
HintingPreference
This enum describes the different levels of hinting that can be applied to glyphs to improve legibili...
Definition: qfont.h:96
QFixed * advances_y
double qreal
Definition: qglobal.h:1193
virtual QImage alphaMapForGlyph(glyph_t)
qreal averageCharWidth() const
Returns the average character width of this QRawFont in pixel units.
Definition: qrawfont.cpp:345
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
AntialiasingType
This enum represents the different ways a glyph can be rasterized in the function alphaMapForGlyph()...
Definition: qrawfont.h:66
QVector< quint32 > glyphIndexesForString(const QString &text) const
Converts the string of unicode points given by text to glyph indexes using the CMAP table in the unde...
Definition: qrawfont.cpp:436
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QFixed * advances_x
HintingPreference hintingPreference() const
Returns the currently preferred hinting level for glyphs rendered with this font. ...
Definition: qfont.cpp:1088
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
HB_Glyph * glyphs
bool isValid() const
Returns true if the QRawFont is valid and false otherwise.
Definition: qrawfont.cpp:196
qreal descent() const
Returns the descent of this QRawFont in pixel units.
Definition: qrawfont.cpp:315
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void loadFromData(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
Replaces the current QRawFont with the font contained in the supplied fontData for the size (in pixel...
Definition: qrawfont.cpp:228
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QTextLine class represents a line of text inside a QTextLayout.
Definition: qtextlayout.h:197
qreal pixelSize() const
Returns the pixel size set for this QRawFont.
Definition: qrawfont.cpp:367
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
T qFromBigEndian(const uchar *src)
QVector< QPointF > advancesForGlyphIndexes(const QVector< quint32 > &glyphIndexes) const
Returns the QRawFont&#39;s advances for each of the glyphIndexes in pixel units.
Definition: qrawfont.cpp:486
QList< QFontDatabase::WritingSystem > supportedWritingSystems() const
Returns a list of writing systems supported by the font according to designer supplied information in...
Definition: qrawfont.cpp:573
qreal leading() const
Returns the leading of this QRawFont in pixel units.
Definition: qrawfont.cpp:335
static QFontPrivate * get(const QFont &font)
Definition: qfont_p.h:202
bool ref()
Atomically increments the value of this QAtomicInt.
qreal unitsPerEm() const
Returns the number of design units define the width and height of the em square for this QRawFont...
Definition: qrawfont.cpp:380
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags flags) const =0
QFont::Style style() const
Returns the style of this QRawFont.
Definition: qrawfont.cpp:408
virtual Type type() const =0
virtual QFontEngine * cloneWithSize(qreal) const
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem)
virtual QImage alphaRGBMapForGlyph(glyph_t, QFixed subPixelPosition, int margin, const QTransform &t)
static ushort highSurrogate(uint ucs4)
Returns the high surrogate value of a ucs4 code point.
Definition: qchar.h:303
static ushort lowSurrogate(uint ucs4)
Returns the low surrogate value of a ucs4 code point.
Definition: qchar.h:306
bool supportsCharacter(quint32 ucs4) const
Returns true if the font has a glyph that corresponds to the UCS-4 encoded character ucs4...
Definition: qrawfont.cpp:617
virtual qreal maxCharWidth() const =0
QByteArray fontTable(const char *tagName) const
Retrieves the sfnt table named tagName from the underlying physical font, or an empty byte array if n...
Definition: qrawfont.cpp:550
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool requiresSurrogates(uint ucs4)
Returns true if the UCS-4-encoded character specified by ucs4 can be split into the high and low part...
Definition: qchar.h:294
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
void platformLoadFromData(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
virtual bool canRender(const QChar *string, int len)=0
QRawFont()
Constructs an invalid QRawFont.
Definition: qrawfont.cpp:134
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
bool deref()
Atomically decrements the value of this QAtomicInt.
virtual QFixed ascent() const =0
const char * layout
virtual QFixed descent() const =0
T qToBigEndian(T source)
Definition: qendian.h:337
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
QList< QFontDatabase::WritingSystem > qt_determine_writing_systems_from_truetype_bits(quint32 unicodeRange[4], quint32 codePageRange[2])
uint weight
Definition: qfont_p.h:95
~QRawFont()
Destroys the QRawFont.
Definition: qrawfont.cpp:180
qreal ascent() const
Returns the ascent of this QRawFont in pixel units.
Definition: qrawfont.cpp:305
QRawFont rawFont() const
Returns the font selected for this QGlyphRun object.
Definition: qglyphrun.cpp:180
Style
This enum describes the different styles of glyphs that are used to display text. ...
Definition: qfont.h:111
QList< QGlyphRun > glyphRuns() const
Returns the glyph indexes and positions for all glyphs in this QTextLayout.
static QRawFont fromFont(const QFont &font, QFontDatabase::WritingSystem writingSystem=QFontDatabase::Any)
Fetches the physical representation based on a font query.
Definition: qrawfont.cpp:640
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
qreal pixelSize
Definition: qfont_p.h:90
static QString writingSystemSample(WritingSystem writingSystem)
Returns a string with sample characters from writingSystem.
virtual QFixed xHeight() const
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
qreal xHeight() const
Returns the xHeight of this QRawFont in pixel units.
Definition: qrawfont.cpp:325
virtual void loadEngine(int at)=0
virtual void recalcAdvances(QGlyphLayout *, QTextEngine::ShaperFlags) const
void * qMemCopy(void *dest, const void *src, size_t n)
Definition: qglobal.cpp:2508
virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs, QPainterPath *path, QTextItem::RenderFlags flags)
void resize(int totalGlyphs)
void setPixelSize(qreal pixelSize)
Sets the pixel size with which this font should be rendered to pixelSize.
Definition: qrawfont.cpp:685
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QTextLayout class is used to lay out and render text.
Definition: qtextlayout.h:105
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QRawFont class provides access to a single physical instance of a font.
Definition: qrawfont.h:63
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
QTextLine createLine()
Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise ret...
QString family() const
Returns the requested font family name, i.e.
Definition: qfont.cpp:906
bool isValid() const
Definition: qrawfont_p.h:97
uint style
Definition: qfont_p.h:97
QString styleName
Definition: qfont_p.h:83
unsigned int quint32
Definition: qglobal.h:938
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QFont::HintingPreference hintingPreference
Definition: qrawfont_p.h:112
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:148
void loadFromFile(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference)
Replaces the current QRawFont with the contents of the file referenced by fileName for the size (in p...
Definition: qrawfont.cpp:210
QString familyName() const
Returns the family name of this QRawFont.
Definition: qrawfont.cpp:388
virtual QFixed emSquareSize() const
qreal toReal() const
Definition: qfixed_p.h:77
bool glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
Converts a string of unicode points to glyph indexes using the CMAP table in the underlying font...
Definition: qrawfont.cpp:469
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QFontEngine * fontEngine
Definition: qrawfont_p.h:111
T * data()
Returns a pointer to the data stored in the vector.
Definition: qvector.h:152
qreal maxCharWidth() const
Returns the width of the widest character in the font.
Definition: qrawfont.cpp:355
QThread * thread
Definition: qrawfont_p.h:113
QFont::HintingPreference hintingPreference() const
Returns the hinting preference used to construct this QRawFont.
Definition: qrawfont.cpp:539
void endLayout()
Ends the layout process.
QFontDef fontDef
int weight() const
Returns the weight of this QRawFont.
Definition: qrawfont.cpp:418
QString family
Definition: qfont_p.h:82
QString styleName() const
Returns the style name of this QRawFont.
Definition: qrawfont.cpp:398
bool operator==(const QRawFont &other) const
Returns true if this QRawFont is equal to other.
Definition: qrawfont.cpp:286
static QString fileName(const QString &fileUrl)
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QImage alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType=SubPixelAntialiasing, const QTransform &transform=QTransform()) const
This function returns a rasterized image of the glyph at the given glyphIndex in the underlying font...
Definition: qrawfont.cpp:252
QExplicitlySharedDataPointer< QRawFontPrivate > d
Definition: qrawfont.h:138
void beginLayout()
Begins the layout process.
QPainterPath pathForGlyph(quint32 glyphIndex) const
This function returns the shape of the glyph at a given glyphIndex in the underlying font if the QRaw...
Definition: qrawfont.cpp:272
QFontEngine * engine(int at) const
#define text
Definition: qobjectdefs.h:80
virtual QFixed leading() const =0
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
QByteArray getSfntTable(uint) const
#define Q_FUNC_INFO
Definition: qglobal.h:1871