10#include "private/qfontdatabase_p.h"
11#include "private/qimage_p.h"
12#include <private/qstringiterator_p.h>
13#include <qguiapplication.h>
15#include <qpa/qplatformscreen.h>
16#include <QtCore/QUuid>
17#include <QtCore/QLoggingCategory>
18#include <QtGui/QPainterPath>
24#include <qscopedvaluerollback.h>
28#include <private/qcolrpaintgraphrenderer_p.h>
35#include FT_SYNTHESIS_H
36#include FT_TRUETYPE_TABLES_H
37#include FT_TYPE1_TABLES_H
40#include FT_LCD_FILTER_H
41#include FT_MULTIPLE_MASTERS_H
43#if defined(FT_CONFIG_OPTIONS_H)
44#include FT_CONFIG_OPTIONS_H
47#if defined(FT_FONT_FORMATS_H)
48#include FT_FONT_FORMATS_H
57using namespace Qt::StringLiterals;
59#define FLOOR(x) ((x) & -64
)
60#define CEIL(x) (((x)+63
) & -64
)
61#define TRUNC(x) ((x) >> 6
)
62#define ROUND(x) (((x)+32
) & -64
)
64static bool ft_getSfntTable(
void *user_data, uint tag, uchar *buffer, uint *length)
66 FT_Face face = (FT_Face)user_data;
69 if (FT_IS_SFNT(face)) {
70 FT_ULong len = *length;
71 result = FT_Load_Sfnt_Table(face, tag, 0, buffer, &len) == FT_Err_Ok
83 QFontEngineFT::HintFull;
85 QFontEngineFT::HintNone;
116 for (
auto iter = faces.cbegin(); iter != faces.cend(); ++iter) {
117 iter.value()->cleanup();
118 if (!iter.value()->ref.deref())
123 for (
auto iter = staleFaces.cbegin(); iter != staleFaces.cend(); ++iter) {
125 if (!(*iter)->ref.deref())
130 FT_Done_FreeType(library);
136 return style1.faceFileName == style2.faceFileName && style1.styleName == style2.styleName;
141 return qHashMulti(seed, style.faceFileName, style.styleName);
151 if (!freetypeData->library) {
152 FT_Init_FreeType(&freetypeData->library);
153#if defined(FT_FONT_FORMATS_H)
155 FT_Bool no_darkening =
false;
156 FT_Property_Set(freetypeData->library,
"cff",
"no-stem-darkening", &no_darkening);
165 Q_ASSERT(freetypeData->library);
166 return freetypeData->library;
169int QFreetypeFace::fsType()
const
172 TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
174 fsType = os2->fsType;
178int QFreetypeFace::getPointInOutline(glyph_t glyph,
int flags, quint32 point, QFixed *xpos, QFixed *ypos, quint32 *nPoints)
180 if (
int error = FT_Load_Glyph(face, glyph, flags))
183 if (face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
184 return Err_Invalid_SubTable;
186 *nPoints = face->glyph->outline.n_points;
190 if (point > *nPoints)
191 return Err_Invalid_SubTable;
193 *xpos = QFixed::fromFixed(face->glyph->outline.points[point].x);
194 *ypos = QFixed::fromFixed(face->glyph->outline.points[point].y);
199bool QFreetypeFace::isScalableBitmap()
const
202 return !FT_IS_SCALABLE(face) && FT_HAS_COLOR(face);
211
212
213
214
215
216
217QFreetypeFace *QFreetypeFace::getFace(
const QFontEngine::FaceId &face_id,
218 const QByteArray &fontData)
220 if (face_id.filename.isEmpty() && fontData.isEmpty())
223 QtFreetypeData *freetypeData = qt_getFreetypeData();
226 freetypeData->staleFaces.removeIf([](QFreetypeFace *face) {
227 if (face->ref.loadRelaxed() == 1) {
235 QFreetypeFace *freetype =
nullptr;
236 auto it = freetypeData->faces.find(face_id);
237 if (it != freetypeData->faces.end()) {
240 Q_ASSERT(freetype->ref.loadRelaxed() > 0);
241 if (freetype->ref.loadRelaxed() == 1) {
248 freetypeData->faces.erase(it);
257 const auto deleter = [](QFreetypeFace *f) {
delete f; };
258 std::unique_ptr<QFreetypeFace,
decltype(deleter)> newFreetype(
new QFreetypeFace, deleter);
261 if (!face_id.filename.isEmpty()) {
262 QString fileName = QFile::decodeName(face_id.filename);
263 if (
const char *prefix =
":qmemoryfonts/"; face_id.filename.startsWith(prefix)) {
265 QByteArray idx = face_id.filename;
266 idx.remove(0, strlen(prefix));
268 newFreetype->fontData = qt_fontdata_from_index(idx.toInt(&ok));
270 newFreetype->fontData = QByteArray();
271 }
else if (!QFileInfo(fileName).isNativePath()) {
272 QFile file(fileName);
273 if (!file.open(QIODevice::ReadOnly)) {
276 newFreetype->fontData = file.readAll();
279 newFreetype->fontData = fontData;
282 FT_Int major, minor, patch;
283 FT_Library_Version(qt_getFreetype(), &major, &minor, &patch);
284 const bool goodVersion = major > 2 || (major == 2 && minor > 13) || (major == 2 && minor == 13 && patch > 2);
286 if (!newFreetype->fontData.isEmpty()) {
287 if (FT_New_Memory_Face(freetypeData->library,
288 (
const FT_Byte *)newFreetype->fontData.constData(),
289 newFreetype->fontData.size(),
299 if (FT_Reference_Face(face))
301 }
else if (!FT_HAS_MULTIPLE_MASTERS(face)
302 || FT_New_Memory_Face(freetypeData->library,
303 (
const FT_Byte *)newFreetype->fontData.constData(),
304 newFreetype->fontData.size(),
306 &tmpFace) != FT_Err_Ok) {
310 if (FT_New_Face(freetypeData->library, face_id.filename, face_id.index, &face))
317 if (FT_Reference_Face(face))
319 }
else if (!FT_HAS_MULTIPLE_MASTERS(face)
320 || FT_New_Face(freetypeData->library, face_id.filename, face_id.index, &tmpFace) != FT_Err_Ok) {
325#if (FREETYPE_MAJOR*10000
+ FREETYPE_MINOR*100
+ FREETYPE_PATCH) >= 20900
326 if (face_id.instanceIndex >= 0) {
328 <<
"Selecting named instance" << (face_id.instanceIndex)
329 <<
"in" << face_id.filename;
330 FT_Set_Named_Instance(face, face_id.instanceIndex + 1);
338 if (tmpFace !=
nullptr) {
340 if (FT_Get_MM_Var(tmpFace, &var) == FT_Err_Ok) {
341 for (FT_UInt i = 0; i < var->num_axis; ++i) {
342 FT_Var_Axis *axis = var->axis + i;
344 QFontVariableAxis fontVariableAxis;
345 if (
const auto tag = QFont::Tag::fromValue(axis->tag)) {
346 fontVariableAxis.setTag(*tag);
348 qWarning() <<
"QFreetypeFace::getFace: Invalid variable axis tag encountered"
352 fontVariableAxis.setMinimumValue(axis->minimum / 65536.0);
353 fontVariableAxis.setMaximumValue(axis->maximum / 65536.0);
354 fontVariableAxis.setDefaultValue(axis->def / 65536.0);
355 fontVariableAxis.setName(QString::fromUtf8(axis->name));
357 newFreetype->variableAxisList.append(fontVariableAxis);
360 if (!face_id.variableAxes.isEmpty()) {
361 QVarLengthArray<FT_Fixed, 16> coords(var->num_axis);
362 FT_Get_Var_Design_Coordinates(face, var->num_axis, coords.data());
363 for (qsizetype i = 0; i < newFreetype->variableAxisList.size(); ++i) {
364 const QFontVariableAxis &axis = newFreetype->variableAxisList.at(i);
365 if (axis.tag().isValid()) {
366 const auto it = face_id.variableAxes.constFind(axis.tag());
367 if (it != face_id.variableAxes.constEnd())
368 coords[i] = FT_Fixed(*it * 65536);
371 FT_Set_Var_Design_Coordinates(face, var->num_axis, coords.data());
374 FT_Done_MM_Var(qt_getFreetype(), var);
377 FT_Done_Face(tmpFace);
380 newFreetype->face = face;
381 newFreetype->mm_var =
nullptr;
382 if (FT_IS_NAMED_INSTANCE(newFreetype->face)) {
384 ftresult = FT_Get_MM_Var(face, &newFreetype->mm_var);
385 if (ftresult != FT_Err_Ok)
386 newFreetype->mm_var =
nullptr;
389 newFreetype->ref.storeRelaxed(1);
390 newFreetype->xsize = 0;
391 newFreetype->ysize = 0;
392 newFreetype->matrix.xx = 0x10000;
393 newFreetype->matrix.yy = 0x10000;
394 newFreetype->matrix.xy = 0;
395 newFreetype->matrix.yx = 0;
396 newFreetype->unicode_map =
nullptr;
397 newFreetype->symbol_map =
nullptr;
399 memset(newFreetype->cmapCache, 0,
sizeof(newFreetype->cmapCache));
401 for (
int i = 0; i < newFreetype->face->num_charmaps; ++i) {
402 FT_CharMap cm = newFreetype->face->charmaps[i];
403 switch(cm->encoding) {
404 case FT_ENCODING_UNICODE:
405 newFreetype->unicode_map = cm;
407 case FT_ENCODING_APPLE_ROMAN:
408 case FT_ENCODING_ADOBE_LATIN_1:
409 if (!newFreetype->unicode_map || newFreetype->unicode_map->encoding != FT_ENCODING_UNICODE)
410 newFreetype->unicode_map = cm;
412 case FT_ENCODING_ADOBE_CUSTOM:
413 case FT_ENCODING_MS_SYMBOL:
414 if (!newFreetype->symbol_map)
415 newFreetype->symbol_map = cm;
422 if (!FT_IS_SCALABLE(newFreetype->face) && newFreetype->face->num_fixed_sizes == 1)
423 FT_Set_Char_Size(face, newFreetype->face->available_sizes[0].x_ppem, newFreetype->face->available_sizes[0].y_ppem, 0, 0);
425 FT_Set_Charmap(newFreetype->face, newFreetype->unicode_map);
428 freetypeData->faces.insert(face_id, newFreetype.get());
430 newFreetype.release()->release(face_id);
434 freetype = newFreetype.release();
440void QFreetypeFace::cleanup()
444 FT_Done_MM_Var(qt_getFreetype(), mm_var);
450void QFreetypeFace::release(
const QFontEngine::FaceId &face_id)
453 bool deleteThis = !ref.deref();
459 if (face && ref.loadRelaxed() == 1) {
460 QtFreetypeData *freetypeData = qt_getFreetypeData();
462 freetypeData->staleFaces.removeIf([&deleteThis,
this](QFreetypeFace *face){
463 if (face->ref.loadRelaxed() == 1) {
474 for (
auto it = freetypeData->faces.constBegin();
475 it != freetypeData->faces.constEnd();
476 it = freetypeData->faces.erase(it)) {
477 if (it.value()->ref.loadRelaxed() == 1) {
478 it.value()->cleanup();
479 if (it.value() ==
this)
484 freetypeData->staleFaces.append(it.value());
488 if (freetypeData->faces.isEmpty() && freetypeData->staleFaces.isEmpty()) {
489 FT_Done_FreeType(freetypeData->library);
490 freetypeData->library =
nullptr;
500 FT_Library library = qt_getFreetype();
508 FT_Error error = FT_New_Face(library, faceFileName.toUtf8().constData(), faceIndex, &face);
509 if (error != FT_Err_Ok) {
510 qDebug() <<
"FT_New_Face failed for face index" << faceIndex <<
':' << Qt::hex << error;
514 const bool found = QLatin1StringView(face->style_name) == styleName;
515 numFaces = face->num_faces;
521 }
while (++faceIndex < numFaces);
527int QFreetypeFace::getFaceIndexByStyleName(
const QString &faceFileName,
const QString &styleName)
529 QtFreetypeData *freetypeData = qt_getFreetypeData();
532 QtFreetypeData::FaceStyle faceStyle(faceFileName, styleName);
533 int faceIndex = freetypeData->faceIndices.value(faceStyle, -1);
538 faceIndex = computeFaceIndex(faceFileName, styleName);
540 freetypeData->faceIndices.insert(faceStyle, faceIndex);
545void QFreetypeFace::computeSize(
const QFontDef &fontDef,
int *xsize,
int *ysize,
bool *outline_drawing, QFixed *scalableBitmapScaleFactor)
547 *ysize = qRound(fontDef.pixelSize * 64);
548 *xsize = *ysize * fontDef.stretch / 100;
549 *scalableBitmapScaleFactor = 1;
550 *outline_drawing =
false;
552 if (!(face->face_flags & FT_FACE_FLAG_SCALABLE)) {
554 if (!isScalableBitmap()) {
556
557
558
559 for (
int i = 1; i < face->num_fixed_sizes; i++) {
560 if (qAbs(*ysize - face->available_sizes[i].y_ppem) <
561 qAbs(*ysize - face->available_sizes[best].y_ppem) ||
562 (qAbs(*ysize - face->available_sizes[i].y_ppem) ==
563 qAbs(*ysize - face->available_sizes[best].y_ppem) &&
564 qAbs(*xsize - face->available_sizes[i].x_ppem) <
565 qAbs(*xsize - face->available_sizes[best].x_ppem))) {
571 for (
int i = 1; i < face->num_fixed_sizes; i++) {
572 if (face->available_sizes[i].y_ppem < *ysize) {
573 if (face->available_sizes[i].y_ppem > face->available_sizes[best].y_ppem)
575 }
else if (face->available_sizes[best].y_ppem < *ysize) {
577 }
else if (face->available_sizes[i].y_ppem < face->available_sizes[best].y_ppem) {
585 if (FT_Select_Size(face, best) == 0) {
586 if (isScalableBitmap())
587 *scalableBitmapScaleFactor = QFixed::fromReal((qreal)fontDef.pixelSize / face->available_sizes[best].height);
588 *xsize = face->available_sizes[best].x_ppem;
589 *ysize = face->available_sizes[best].y_ppem;
594#if defined FT_HAS_COLOR
595 if (FT_HAS_COLOR(face)) {
596 *outline_drawing =
false;
600 int maxCachedGlyphSize = QFontEngine::maxCachedGlyphSize();
601 *outline_drawing = (*xsize > (maxCachedGlyphSize << 6) || *ysize > (maxCachedGlyphSize << 6));
606QFontEngine::Properties QFreetypeFace::properties()
const
608 QFontEngine::Properties p;
609 p.postscriptName = FT_Get_Postscript_Name(face);
610 PS_FontInfoRec font_info;
611 if (FT_Get_PS_Font_Info(face, &font_info) == 0)
612 p.copyright = font_info.notice;
613 if (FT_IS_SCALABLE(face)
614#if defined(FT_HAS_COLOR)
615 && !FT_HAS_COLOR(face)
618 p.ascent = face->ascender;
619 p.descent = -face->descender;
620 p.leading = face->height - face->ascender + face->descender;
621 p.emSquare = face->units_per_EM;
622 p.boundingBox = QRectF(face->bbox.xMin, -face->bbox.yMax,
623 face->bbox.xMax - face->bbox.xMin,
624 face->bbox.yMax - face->bbox.yMin);
626 p.ascent = QFixed::fromFixed(face->size->metrics.ascender);
627 p.descent = QFixed::fromFixed(-face->size->metrics.descender);
628 p.leading = QFixed::fromFixed(face->size->metrics.height - face->size->metrics.ascender + face->size->metrics.descender);
629 p.emSquare = face->size->metrics.y_ppem;
631 p.boundingBox = QRectF(0, -p.ascent.toReal(),
632 face->size->metrics.max_advance/64, (p.ascent + p.descent).toReal() );
635 p.capHeight = p.ascent;
636 p.lineWidth = face->underline_thickness;
641bool QFreetypeFace::getSfntTable(uint tag, uchar *buffer, uint *length)
const
643 return ft_getSfntTable(face, tag, buffer, length);
647
648
649
650
651
652
653
654
655static void scaleOutline(FT_Face face, FT_GlyphSlot g, FT_Fixed x_scale, FT_Fixed y_scale)
657 x_scale = FT_MulDiv(x_scale, 1 << 10, face->units_per_EM);
658 y_scale = FT_MulDiv(y_scale, 1 << 10, face->units_per_EM);
659 FT_Vector *p = g->outline.points;
660 const FT_Vector *e = p + g->outline.n_points;
662 p->x = FT_MulFix(p->x, x_scale);
663 p->y = FT_MulFix(p->y, y_scale);
668#define GLYPH2PATH_DEBUG QT_NO_QDEBUG_MACRO
669void QFreetypeFace::addGlyphToPath(FT_Face face, FT_GlyphSlot g,
const QFixedPoint &point, QPainterPath *path, FT_Fixed x_scale, FT_Fixed y_scale)
671 const qreal factor = 1/64.;
672 scaleOutline(face, g, x_scale, y_scale);
674 QPointF cp = point.toPointF();
678 for (
int j = 0; j < g->outline.n_contours; ++j) {
679 int last_point = g->outline.contours[j];
681 QPointF start = QPointF(g->outline.points[i].x*factor, -g->outline.points[i].y*factor);
682 if (!(g->outline.tags[i] & 1)) {
683 if (!(g->outline.tags[last_point] & 1)) {
685 start = (QPointF(g->outline.points[last_point].x*factor,
686 -g->outline.points[last_point].y*factor) + start) / 2.0;
689 start = QPointF(g->outline.points[last_point].x*factor,
690 -g->outline.points[last_point].y*factor);
701 while (i < last_point) {
703 c[n] = cp + QPointF(g->outline.points[i].x*factor, -g->outline.points[i].y*factor);
704 GLYPH2PATH_DEBUG() <<
" " << i << c[n] <<
"tag =" << (
int)g->outline.tags[i]
705 <<
": on curve =" << (
bool)(g->outline.tags[i] & 1);
707 switch (g->outline.tags[i] & 3) {
712 c[3] = (c[3] + c[2])/2;
719 c[3] = (c[1] + c[2])/2;
720 c[2] = (2*c[1] + c[3])/3;
721 c[1] = (2*c[1] + c[0])/3;
734 c[2] = (2*c[1] + c[3])/3;
735 c[1] = (2*c[1] + c[0])/3;
740 path->cubicTo(c[1], c[2], c[3]);
747 path->closeSubpath();
751 c[2] = (2*c[1] + c[3])/3;
752 c[1] = (2*c[1] + c[0])/3;
755 path->cubicTo(c[1], c[2], c[3]);
761extern void qt_addBitmapToPath(qreal x0, qreal y0,
const uchar *image_data,
int bpl,
int w,
int h, QPainterPath *path);
763void QFreetypeFace::addBitmapToPath(FT_GlyphSlot slot,
const QFixedPoint &point, QPainterPath *path)
765 if (slot->format != FT_GLYPH_FORMAT_BITMAP
766 || slot->bitmap.pixel_mode != FT_PIXEL_MODE_MONO)
769 QPointF cp = point.toPointF();
770 qt_addBitmapToPath(cp.x() +
TRUNC(slot->metrics.horiBearingX), cp.y() -
TRUNC(slot->metrics.horiBearingY),
771 slot->bitmap.buffer, slot->bitmap.pitch, slot->bitmap.width, slot->bitmap.rows, path);
774static inline void convertRGBToARGB(
const uchar *src, uint *dst,
int width,
int height,
int src_pitch,
bool bgr)
776 const int offs = bgr ? -1 : 1;
777 const int w = width * 3;
780 for (
int x = 0; x < w; x += 3) {
781 uchar red = src[x + 1 - offs];
782 uchar green = src[x + 1];
783 uchar blue = src[x + 1 + offs];
784 *dd++ = (0xFFU << 24) | (red << 16) | (green << 8) | blue;
791static inline void convertRGBToARGB_V(
const uchar *src, uint *dst,
int width,
int height,
int src_pitch,
bool bgr)
793 const int offs = bgr ? -src_pitch : src_pitch;
795 for (
int x = 0; x < width; x++) {
796 uchar red = src[x + src_pitch - offs];
797 uchar green = src[x + src_pitch];
798 uchar blue = src[x + src_pitch + offs];
799 *dst++ = (0XFFU << 24) | (red << 16) | (green << 8) | blue;
807 static int type = -1;
809 if (QScreen *screen = QGuiApplication::primaryScreen())
810 type = screen->handle()->subpixelAntialiasingTypeHint();
812 return static_cast<QFontEngine::SubpixelAntialiasingType>(type);
815QFontEngineFT *QFontEngineFT::create(
const QFontDef &fontDef, FaceId faceId,
const QByteArray &fontData)
817 auto engine = std::make_unique<QFontEngineFT>(fontDef);
819 QFontEngineFT::GlyphFormat format = QFontEngineFT::Format_Mono;
820 const bool antialias = !(fontDef.styleStrategy & QFont::NoAntialias);
823 QFontEngine::SubpixelAntialiasingType subpixelType = subpixelAntialiasingTypeHint();
824 if (subpixelType == QFontEngine::Subpixel_None || (fontDef.styleStrategy & QFont::NoSubpixelAntialias)) {
825 format = QFontEngineFT::Format_A8;
826 engine->subpixelType = QFontEngine::Subpixel_None;
828 format = QFontEngineFT::Format_A32;
829 engine->subpixelType = subpixelType;
833 if (!engine->init(faceId, antialias, format, fontData) || engine->invalid()) {
834 qWarning(
"QFontEngineFT: Failed to create FreeType font engine");
838 engine->setQtDefaultHintStyle(
static_cast<QFont::HintingPreference>(fontDef.hintingPreference));
839 return engine.release();
844 FT_MM_Var *var = freetypeFace->mm_var;
845 if (var !=
nullptr && faceId.instanceIndex >= 0 && FT_UInt(faceId.instanceIndex) < var->num_namedstyles) {
846 for (FT_UInt axis = 0; axis < var->num_axis; ++axis) {
847 if (var->axis[axis].tag == QFont::Tag(
"wght").value()) {
848 return var->namedstyle[faceId.instanceIndex].coords[axis] >> 16;
852 if (
const TT_OS2 *os2 =
reinterpret_cast<
const TT_OS2 *>(FT_Get_Sfnt_Table(face, ft_sfnt_os2))) {
853 return os2->usWeightClass;
860 class QFontEngineFTRawData:
public QFontEngineFT
863 QFontEngineFTRawData(
const QFontDef &fontDef) : QFontEngineFT(fontDef)
867 void updateFamilyNameAndStyle()
869 fontDef.families = QStringList(QString::fromLatin1(freetype->face->family_name));
871 if (freetype->face->style_flags & FT_STYLE_FLAG_ITALIC)
872 fontDef.style = QFont::StyleItalic;
874 if (freetype->face->style_flags & FT_STYLE_FLAG_BOLD)
875 fontDef.weight = QFont::Bold;
877 fontDef.weight = calculateActualWeight(freetype, freetype->face, faceId());
881 const QMap<QFont::Tag,
float> &variableAxisValues,
885 faceId.filename =
"";
887 faceId.uuid = QUuid::createUuid().toByteArray();
888 faceId.variableAxes = variableAxisValues;
889 faceId.instanceIndex = instanceIndex;
891 return init(faceId,
true, Format_None, fontData);
896QFontEngineFT *QFontEngineFT::create(
const QByteArray &fontData,
898 QFont::HintingPreference hintingPreference,
899 const QMap<QFont::Tag,
float> &variableAxisValues,
903 fontDef.pixelSize = pixelSize;
904 fontDef.stretch = QFont::Unstretched;
905 fontDef.hintingPreference = hintingPreference;
906 fontDef.variableAxisValues = variableAxisValues;
908 QFontEngineFTRawData *fe =
new QFontEngineFTRawData(fontDef);
909 if (!fe->initFromData(fontData, variableAxisValues, instanceIndex)) {
914 fe->updateFamilyNameAndStyle();
915 fe->setQtDefaultHintStyle(
static_cast<QFont::HintingPreference>(fontDef.hintingPreference));
920QFontEngineFT::QFontEngineFT(
const QFontDef &fd)
921 : QFontEngine(Freetype)
928 cache_cost = 100 * 1024;
929 kerning_pairs_loaded =
false;
935 default_load_flags = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
936 default_hint_style = ftInitialDefaultHintStyle;
937 subpixelType = Subpixel_None;
938 lcdFilterType = (
int)((quintptr) FT_LCD_FILTER_DEFAULT);
939 defaultFormat = Format_None;
940 embeddedbitmap =
false;
941 const QByteArray env = qgetenv(
"QT_NO_FT_CACHE");
942 cacheEnabled = env.isEmpty() || env.toInt() == 0;
943 m_subPixelPositionCount = 4;
944 forceAutoHint =
false;
945 stemDarkeningDriver =
false;
946 verticalHintingOnly =
false;
949QFontEngineFT::~QFontEngineFT()
952 freetype->release(face_id);
955bool QFontEngineFT::init(FaceId faceId,
bool antialias, GlyphFormat format,
956 const QByteArray &fontData)
958 return init(faceId, antialias, format, QFreetypeFace::getFace(faceId, fontData));
965 FT_MM_Var *var = freetypeFace->mm_var;
966 if (var !=
nullptr && faceId.instanceIndex >= 0 && FT_UInt(faceId.instanceIndex) < var->num_namedstyles) {
967 for (FT_UInt axis = 0; axis < var->num_axis; ++axis) {
968 if (var->axis[axis].tag == QFont::Tag(
"ital").value()) {
969 return (var->namedstyle[faceId.instanceIndex].coords[axis] >> 16) == 1;
974 return (face->style_flags & FT_STYLE_FLAG_ITALIC);
977bool QFontEngineFT::init(FaceId faceId,
bool antialias, GlyphFormat format,
978 QFreetypeFace *freetypeFace)
980 freetype = freetypeFace;
986 defaultFormat = format;
987 this->antialias = antialias;
990 glyphFormat = QFontEngine::Format_Mono;
992 glyphFormat = defaultFormat;
996 symbol = freetype->symbol_map !=
nullptr;
997 PS_FontInfoRec psrec;
999 if (FT_Get_PS_Font_Info(freetype->face, &psrec) == FT_Err_Ok) {
1000 symbol = !fontDef.families.isEmpty() &&
bool(fontDef.families.constFirst().contains(
"symbol"_L1, Qt::CaseInsensitive));
1003 freetype->computeSize(fontDef, &xsize, &ysize, &defaultGlyphSet.outline_drawing, &scalableBitmapScaleFactor);
1005 FT_Face face = lockFace();
1007 if (FT_IS_SCALABLE(face)
1008#if defined(FT_HAS_COLOR)
1009 && !FT_HAS_COLOR(face)
1012 bool isItalic = calculateActualItalic(freetype, face, faceId);
1013 bool fake_oblique = (fontDef.style != QFont::StyleNormal) && !isItalic && !qEnvironmentVariableIsSet(
"QT_NO_SYNTHESIZED_ITALIC");
1016 FT_Set_Transform(face, &matrix,
nullptr);
1017 freetype->matrix = matrix;
1019 if ((fontDef.weight >= QFont::Bold) && !(face->style_flags & FT_STYLE_FLAG_BOLD) && !FT_IS_FIXED_WIDTH(face) && !qEnvironmentVariableIsSet(
"QT_NO_SYNTHESIZED_BOLD")) {
1020 FT_UShort actualWeight = calculateActualWeight(freetype, face, faceId);
1021 if (actualWeight < 700 &&
1022 (fontDef.pixelSize < 64 || qEnvironmentVariableIsSet(
"QT_NO_SYNTHESIZED_BOLD_LIMIT"))) {
1027 line_thickness = QFixed::fromFixed(FT_MulFix(face->underline_thickness, face->size->metrics.y_scale));
1028 QFixed center_position = QFixed::fromFixed(-FT_MulFix(face->underline_position, face->size->metrics.y_scale));
1029 underline_position = center_position - line_thickness / 2;
1032 int score = fontDef.weight * fontDef.pixelSize;
1033 line_thickness = score / 7000;
1035 if (line_thickness < 2 && score >= 1050)
1037 underline_position = ((line_thickness * 2) + 3) / 6;
1039 cacheEnabled =
false;
1040#if defined(FT_HAS_COLOR)
1041 if (FT_HAS_COLOR(face))
1042 glyphFormat = defaultFormat = GlyphFormat::Format_ARGB;
1045 if (line_thickness < 1)
1048 metrics = face->size->metrics;
1051
1052
1053
1054
1055
1056
1057 if (FT_IS_SCALABLE(face)) {
1058 for (
int i = 0; i < face->num_fixed_sizes; ++i) {
1059 if (xsize == face->available_sizes[i].x_ppem && ysize == face->available_sizes[i].y_ppem) {
1060 face->face_flags &= ~FT_FACE_FLAG_SCALABLE;
1062 FT_Select_Size(face, i);
1063 if (face->size->metrics.ascender + face->size->metrics.descender > 0) {
1064 FT_Pos leading = metrics.height - metrics.ascender + metrics.descender;
1065 metrics.ascender = face->size->metrics.ascender;
1066 metrics.descender = face->size->metrics.descender;
1067 if (metrics.descender > 0
1068 && QString::fromUtf8(face->family_name) ==
"Courier New"_L1) {
1069 metrics.descender *= -1;
1071 metrics.height = metrics.ascender - metrics.descender + leading;
1073 FT_Set_Char_Size(face, xsize, ysize, 0, 0);
1075 face->face_flags |= FT_FACE_FLAG_SCALABLE;
1080#if defined(FT_FONT_FORMATS_H)
1081 const char *fmt = FT_Get_Font_Format(face);
1082 if (fmt && qstrncmp(fmt,
"CFF", 4) == 0) {
1083 FT_Bool no_stem_darkening =
true;
1084 FT_Error err = FT_Property_Get(qt_getFreetype(),
"cff",
"no-stem-darkening", &no_stem_darkening);
1085 if (err == FT_Err_Ok)
1086 stemDarkeningDriver = !no_stem_darkening;
1088 stemDarkeningDriver =
false;
1092 fontDef.styleName = QString::fromUtf8(face->style_name);
1094 if (!freetype->hbFace) {
1095 faceData.user_data = face;
1096 faceData.get_font_table = ft_getSfntTable;
1097 (
void)harfbuzzFace();
1098 freetype->hbFace = std::move(face_);
1103 face_ = Holder(freetype->hbFace.get(), dont_delete);
1107 fsType = freetype->fsType();
1111void QFontEngineFT::setQtDefaultHintStyle(QFont::HintingPreference hintingPreference)
1113 switch (hintingPreference) {
1114 case QFont::PreferNoHinting:
1115 setDefaultHintStyle(HintNone);
1117 case QFont::PreferFullHinting:
1118 setDefaultHintStyle(HintFull);
1120 case QFont::PreferVerticalHinting:
1121 setDefaultHintStyle(HintLight);
1123 case QFont::PreferDefaultHinting:
1124 setDefaultHintStyle(ftInitialDefaultHintStyle);
1129void QFontEngineFT::setDefaultHintStyle(HintStyle style)
1131 default_hint_style = style;
1137#if defined(FT_FONT_FORMATS_H)
1138 FT_UInt version = 0;
1139 if (FT_Property_Get(library,
"truetype",
"interpreter-version", &version) != FT_Err_Ok)
1141 return version == 40;
1149bool QFontEngineFT::setVerticalHintingOnly()
1154 if ((default_hint_style == HintMedium || default_hint_style == HintFull)
1155 && !hasTrueTypeInterpreterV40(qt_getFreetype())) {
1158 verticalHintingOnly =
true;
1162bool QFontEngineFT::expectsGammaCorrectedBlending(QFontEngine::GlyphFormat format)
const
1165 return stemDarkeningDriver;
1168int QFontEngineFT::loadFlags(QGlyphSet *set, GlyphFormat format,
int flags,
1169 bool &hsubpixel,
int &vfactor)
const
1171 int load_flags = FT_LOAD_DEFAULT | default_load_flags;
1172 int load_target = default_hint_style == HintLight
1173 ? FT_LOAD_TARGET_LIGHT
1174 : verticalHintingOnly
1175 ? FT_LOAD_TARGET_LCD
1176 : FT_LOAD_TARGET_NORMAL;
1178 if (format == Format_Mono) {
1179 load_target = FT_LOAD_TARGET_MONO;
1180 }
else if (format == Format_A32) {
1181 if (subpixelType == Subpixel_RGB || subpixelType == Subpixel_BGR)
1183 else if (subpixelType == Subpixel_VRGB || subpixelType == Subpixel_VBGR)
1185 }
else if (format == Format_ARGB) {
1187 load_flags |= FT_LOAD_COLOR;
1191 if (set && set->outline_drawing)
1192 load_flags |= FT_LOAD_NO_BITMAP;
1194 if (default_hint_style == HintNone || (flags & DesignMetrics) || (set && set->outline_drawing))
1195 load_flags |= FT_LOAD_NO_HINTING;
1197 load_flags |= load_target;
1200 load_flags |= FT_LOAD_FORCE_AUTOHINT;
1208 return info.width > 0xFF || info.height > 0xFF || info.linearAdvance > 0x7FFF;
1217 FT_Vector_Transform(&vector, matrix);
1222 FT_Vector_Transform(&vector, matrix);
1223 if (l > vector.x) l = vector.x;
1224 if (r < vector.x) r = vector.x;
1225 if (t < vector.y) t = vector.y;
1226 if (b > vector.y) b = vector.y;
1229 FT_Vector_Transform(&vector, matrix);
1230 if (l > vector.x) l = vector.x;
1231 if (r < vector.x) r = vector.x;
1232 if (t < vector.y) t = vector.y;
1233 if (b > vector.y) b = vector.y;
1236 FT_Vector_Transform(&vector, matrix);
1237 if (l > vector.x) l = vector.x;
1238 if (r < vector.x) r = vector.x;
1239 if (t < vector.y) t = vector.y;
1240 if (b > vector.y) b = vector.y;
1247#if defined(QFONTENGINE_FT_SUPPORT_COLRV1)
1248#define FROM_FIXED_16_16(value) (value / 65536.0
)
1250static inline QTransform FTAffineToQTransform(
const FT_Affine23 &matrix)
1252 qreal m11 = FROM_FIXED_16_16(matrix.xx);
1253 qreal m21 = -FROM_FIXED_16_16(matrix.xy);
1254 qreal m12 = -FROM_FIXED_16_16(matrix.yx);
1255 qreal m22 = FROM_FIXED_16_16(matrix.yy);
1256 qreal dx = FROM_FIXED_16_16(matrix.dx);
1257 qreal dy = -FROM_FIXED_16_16(matrix.dy);
1259 return QTransform(m11, m12, m21, m22, dx, dy);
1262bool QFontEngineFT::traverseColr1(FT_OpaquePaint opaquePaint,
1263 QSet<std::pair<FT_Byte *, FT_Bool> > *loops,
1264 QColor foregroundColor,
1266 ushort paletteCount,
1267 QColrPaintGraphRenderer *paintGraphRenderer)
const
1269 FT_Face face = freetype->face;
1271 auto key = std::pair{opaquePaint.p, opaquePaint.insert_root_transform};
1272 if (loops->contains(key)) {
1273 qCWarning(lcColrv1) <<
"Cycle detected in COLRv1 graph";
1277 paintGraphRenderer->save();
1279 auto cleanup = qScopeGuard([&paintGraphRenderer, &key, &loops]() {
1281 paintGraphRenderer->restore();
1284 FT_COLR_Paint paint;
1285 if (!FT_Get_Paint(face, opaquePaint, &paint))
1288 if (paint.format == FT_COLR_PAINTFORMAT_COLR_LAYERS) {
1289 FT_OpaquePaint layerPaint;
1290 layerPaint.p =
nullptr;
1291 while (FT_Get_Paint_Layers(face, &paint.u.colr_layers.layer_iterator, &layerPaint)) {
1292 if (!traverseColr1(layerPaint, loops, foregroundColor, palette, paletteCount, paintGraphRenderer))
1295 }
else if (paint.format == FT_COLR_PAINTFORMAT_TRANSFORM
1296 || paint.format == FT_COLR_PAINTFORMAT_SCALE
1297 || paint.format == FT_COLR_PAINTFORMAT_TRANSLATE
1298 || paint.format == FT_COLR_PAINTFORMAT_ROTATE
1299 || paint.format == FT_COLR_PAINTFORMAT_SKEW) {
1302 FT_OpaquePaint nextPaint;
1303 switch (paint.format) {
1304 case FT_COLR_PAINTFORMAT_TRANSFORM:
1305 xform = FTAffineToQTransform(paint.u.transform.affine);
1306 nextPaint = paint.u.transform.paint;
1308 case FT_COLR_PAINTFORMAT_SCALE:
1310 qreal centerX = FROM_FIXED_16_16(paint.u.scale.center_x);
1311 qreal centerY = -FROM_FIXED_16_16(paint.u.scale.center_y);
1312 qreal scaleX = FROM_FIXED_16_16(paint.u.scale.scale_x);
1313 qreal scaleY = FROM_FIXED_16_16(paint.u.scale.scale_y);
1315 xform.translate(centerX, centerY);
1316 xform.scale(scaleX, scaleY);
1317 xform.translate(-centerX, -centerY);
1319 nextPaint = paint.u.scale.paint;
1322 case FT_COLR_PAINTFORMAT_ROTATE:
1324 qreal centerX = FROM_FIXED_16_16(paint.u.rotate.center_x);
1325 qreal centerY = -FROM_FIXED_16_16(paint.u.rotate.center_y);
1326 qreal angle = -FROM_FIXED_16_16(paint.u.rotate.angle) * 180.0;
1328 xform.translate(centerX, centerY);
1329 xform.rotate(angle);
1330 xform.translate(-centerX, -centerY);
1332 nextPaint = paint.u.rotate.paint;
1336 case FT_COLR_PAINTFORMAT_SKEW:
1338 qreal centerX = FROM_FIXED_16_16(paint.u.skew.center_x);
1339 qreal centerY = -FROM_FIXED_16_16(paint.u.skew.center_y);
1340 qreal angleX = FROM_FIXED_16_16(paint.u.skew.x_skew_angle) * M_PI;
1341 qreal angleY = -FROM_FIXED_16_16(paint.u.skew.y_skew_angle) * M_PI;
1343 xform.translate(centerX, centerY);
1344 xform.shear(qTan(angleX), qTan(angleY));
1345 xform.translate(-centerX, -centerY);
1347 nextPaint = paint.u.rotate.paint;
1350 case FT_COLR_PAINTFORMAT_TRANSLATE:
1352 qreal dx = FROM_FIXED_16_16(paint.u.translate.dx);
1353 qreal dy = -FROM_FIXED_16_16(paint.u.translate.dy);
1355 xform.translate(dx, dy);
1356 nextPaint = paint.u.rotate.paint;
1363 paintGraphRenderer->prependTransform(xform);
1364 if (!traverseColr1(nextPaint, loops, foregroundColor, palette, paletteCount, paintGraphRenderer))
1366 }
else if (paint.format == FT_COLR_PAINTFORMAT_LINEAR_GRADIENT
1367 || paint.format == FT_COLR_PAINTFORMAT_RADIAL_GRADIENT
1368 || paint.format == FT_COLR_PAINTFORMAT_SWEEP_GRADIENT
1369 || paint.format == FT_COLR_PAINTFORMAT_SOLID) {
1370 auto getPaletteColor = [&palette, &paletteCount, &foregroundColor](FT_UInt16 index,
1373 if (index < paletteCount) {
1374 const FT_Color &paletteColor = palette[index];
1375 color = qRgba(paletteColor.red,
1378 paletteColor.alpha);
1379 }
else if (index == 0xffff) {
1380 color = foregroundColor;
1383 if (color.isValid())
1384 color.setAlphaF(color.alphaF() * (alpha / 16384.0));
1389 auto gatherGradientStops = [&](FT_ColorStopIterator it) {
1391 ret.resize(it.num_color_stops);
1393 FT_ColorStop colorStop;
1394 while (FT_Get_Colorline_Stops(face, &colorStop, &it)) {
1395 uint index = it.current_color_stop - 1;
1396 if (qsizetype(index) < ret.size()) {
1397 QGradientStop &gradientStop = ret[index];
1398 gradientStop.first = FROM_FIXED_16_16(colorStop.stop_offset);
1399 gradientStop.second = getPaletteColor(colorStop.color.palette_index,
1400 colorStop.color.alpha);
1407 auto extendToSpread = [](FT_PaintExtend extend) {
1409 case FT_COLR_PAINT_EXTEND_REPEAT:
1410 return QGradient::RepeatSpread;
1411 case FT_COLR_PAINT_EXTEND_REFLECT:
1412 return QGradient::ReflectSpread;
1414 return QGradient::PadSpread;
1418 if (paintGraphRenderer->isRendering()) {
1419 if (paint.format == FT_COLR_PAINTFORMAT_LINEAR_GRADIENT) {
1420 const qreal p0x = FROM_FIXED_16_16(paint.u.linear_gradient.p0.x);
1421 const qreal p0y = -FROM_FIXED_16_16(paint.u.linear_gradient.p0.y);
1423 const qreal p1x = FROM_FIXED_16_16(paint.u.linear_gradient.p1.x);
1424 const qreal p1y = -FROM_FIXED_16_16(paint.u.linear_gradient.p1.y);
1426 const qreal p2x = FROM_FIXED_16_16(paint.u.linear_gradient.p2.x);
1427 const qreal p2y = -FROM_FIXED_16_16(paint.u.linear_gradient.p2.y);
1429 QPointF p0(p0x, p0y);
1430 QPointF p1(p1x, p1y);
1431 QPointF p2(p2x, p2y);
1433 const QGradient::Spread spread =
1434 extendToSpread(paint.u.linear_gradient.colorline.extend);
1435 const QGradientStops stops =
1436 gatherGradientStops(paint.u.linear_gradient.colorline.color_stop_iterator);
1437 paintGraphRenderer->setLinearGradient(p0, p1, p2, spread, stops);
1439 }
else if (paint.format == FT_COLR_PAINTFORMAT_RADIAL_GRADIENT) {
1440 const qreal c0x = FROM_FIXED_16_16(paint.u.radial_gradient.c0.x);
1441 const qreal c0y = -FROM_FIXED_16_16(paint.u.radial_gradient.c0.y);
1442 const qreal r0 = FROM_FIXED_16_16(paint.u.radial_gradient.r0);
1443 const qreal c1x = FROM_FIXED_16_16(paint.u.radial_gradient.c1.x);
1444 const qreal c1y = -FROM_FIXED_16_16(paint.u.radial_gradient.c1.y);
1445 const qreal r1 = FROM_FIXED_16_16(paint.u.radial_gradient.r1);
1447 const QPointF c0(c0x, c0y);
1448 const QPointF c1(c1x, c1y);
1449 const QGradient::Spread spread =
1450 extendToSpread(paint.u.radial_gradient.colorline.extend);
1451 const QGradientStops stops =
1452 gatherGradientStops(paint.u.radial_gradient.colorline.color_stop_iterator);
1454 paintGraphRenderer->setRadialGradient(c0, r0, c1, r1, spread, stops);
1455 }
else if (paint.format == FT_COLR_PAINTFORMAT_SWEEP_GRADIENT) {
1456 const qreal centerX = FROM_FIXED_16_16(paint.u.sweep_gradient.center.x);
1457 const qreal centerY = -FROM_FIXED_16_16(paint.u.sweep_gradient.center.y);
1458 const qreal startAngle = 180.0 * FROM_FIXED_16_16(paint.u.sweep_gradient.start_angle);
1459 const qreal endAngle = 180.0 * FROM_FIXED_16_16(paint.u.sweep_gradient.end_angle);
1461 const QPointF center(centerX, centerY);
1463 const QGradient::Spread spread = extendToSpread(paint.u.radial_gradient.colorline.extend);
1464 const QGradientStops stops = gatherGradientStops(paint.u.sweep_gradient.colorline.color_stop_iterator);
1466 paintGraphRenderer->setConicalGradient(center, startAngle, endAngle, spread, stops);
1468 }
else if (paint.format == FT_COLR_PAINTFORMAT_SOLID) {
1469 QColor color = getPaletteColor(paint.u.solid.color.palette_index,
1470 paint.u.solid.color.alpha);
1471 if (!color.isValid()) {
1472 qCWarning(lcColrv1) <<
"Invalid palette index in COLRv1 graph:"
1473 << paint.u.solid.color.palette_index;
1477 paintGraphRenderer->setSolidColor(color);
1481 paintGraphRenderer->drawCurrentPath();
1482 }
else if (paint.format == FT_COLR_PAINTFORMAT_COMPOSITE) {
1483 if (!paintGraphRenderer->isRendering()) {
1484 if (!traverseColr1(paint.u.composite.backdrop_paint,
1489 paintGraphRenderer)) {
1492 if (!traverseColr1(paint.u.composite.source_paint,
1497 paintGraphRenderer)) {
1501 QPainter::CompositionMode compositionMode = QPainter::CompositionMode_SourceOver;
1502 switch (paint.u.composite.composite_mode) {
1503 case FT_COLR_COMPOSITE_CLEAR:
1504 compositionMode = QPainter::CompositionMode_Clear;
1506 case FT_COLR_COMPOSITE_SRC:
1507 compositionMode = QPainter::CompositionMode_Source;
1509 case FT_COLR_COMPOSITE_DEST:
1510 compositionMode = QPainter::CompositionMode_Destination;
1512 case FT_COLR_COMPOSITE_SRC_OVER:
1513 compositionMode = QPainter::CompositionMode_SourceOver;
1515 case FT_COLR_COMPOSITE_DEST_OVER:
1516 compositionMode = QPainter::CompositionMode_DestinationOver;
1518 case FT_COLR_COMPOSITE_SRC_IN:
1519 compositionMode = QPainter::CompositionMode_SourceIn;
1521 case FT_COLR_COMPOSITE_DEST_IN:
1522 compositionMode = QPainter::CompositionMode_DestinationIn;
1524 case FT_COLR_COMPOSITE_SRC_OUT:
1525 compositionMode = QPainter::CompositionMode_SourceOut;
1527 case FT_COLR_COMPOSITE_DEST_OUT:
1528 compositionMode = QPainter::CompositionMode_DestinationOut;
1530 case FT_COLR_COMPOSITE_SRC_ATOP:
1531 compositionMode = QPainter::CompositionMode_SourceAtop;
1533 case FT_COLR_COMPOSITE_DEST_ATOP:
1534 compositionMode = QPainter::CompositionMode_DestinationAtop;
1536 case FT_COLR_COMPOSITE_XOR:
1537 compositionMode = QPainter::CompositionMode_Xor;
1539 case FT_COLR_COMPOSITE_PLUS:
1540 compositionMode = QPainter::CompositionMode_Plus;
1542 case FT_COLR_COMPOSITE_SCREEN:
1543 compositionMode = QPainter::CompositionMode_Screen;
1545 case FT_COLR_COMPOSITE_OVERLAY:
1546 compositionMode = QPainter::CompositionMode_Overlay;
1548 case FT_COLR_COMPOSITE_DARKEN:
1549 compositionMode = QPainter::CompositionMode_Darken;
1551 case FT_COLR_COMPOSITE_LIGHTEN:
1552 compositionMode = QPainter::CompositionMode_Lighten;
1554 case FT_COLR_COMPOSITE_COLOR_DODGE:
1555 compositionMode = QPainter::CompositionMode_ColorDodge;
1557 case FT_COLR_COMPOSITE_COLOR_BURN:
1558 compositionMode = QPainter::CompositionMode_ColorBurn;
1560 case FT_COLR_COMPOSITE_HARD_LIGHT:
1561 compositionMode = QPainter::CompositionMode_HardLight;
1563 case FT_COLR_COMPOSITE_SOFT_LIGHT:
1564 compositionMode = QPainter::CompositionMode_SoftLight;
1566 case FT_COLR_COMPOSITE_DIFFERENCE:
1567 compositionMode = QPainter::CompositionMode_Difference;
1569 case FT_COLR_COMPOSITE_EXCLUSION:
1570 compositionMode = QPainter::CompositionMode_Exclusion;
1572 case FT_COLR_COMPOSITE_MULTIPLY:
1573 compositionMode = QPainter::CompositionMode_Multiply;
1576 qCWarning(lcColrv1) <<
"Unsupported COLRv1 composition mode" << paint.u.composite.composite_mode;
1580 QColrPaintGraphRenderer compositeRenderer;
1581 compositeRenderer.setBoundingRect(paintGraphRenderer->boundingRect());
1582 compositeRenderer.beginRender(fontDef.pixelSize / face->units_per_EM,
1583 paintGraphRenderer->currentTransform());
1584 if (!traverseColr1(paint.u.composite.backdrop_paint,
1589 &compositeRenderer)) {
1593 compositeRenderer.setCompositionMode(compositionMode);
1594 if (!traverseColr1(paint.u.composite.source_paint,
1599 &compositeRenderer)) {
1602 paintGraphRenderer->drawImage(compositeRenderer.endRender());
1604 }
else if (paint.format == FT_COLR_PAINTFORMAT_GLYPH) {
1605 FT_Error error = FT_Load_Glyph(face,
1606 paint.u.glyph.glyphID,
1607 FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP | FT_LOAD_NO_SVG | FT_LOAD_IGNORE_TRANSFORM | FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT | FT_LOAD_BITMAP_METRICS_ONLY);
1609 qCWarning(lcColrv1) <<
"Failed to load glyph"
1610 << paint.u.glyph.glyphID
1611 <<
"in COLRv1 graph. Error: " << error;
1616 QFreetypeFace::addGlyphToPath(face,
1620 face->units_per_EM << 6,
1621 face->units_per_EM << 6);
1623 paintGraphRenderer->appendPath(path);
1625 if (!traverseColr1(paint.u.glyph.paint, loops, foregroundColor, palette, paletteCount, paintGraphRenderer))
1627 }
else if (paint.format == FT_COLR_PAINTFORMAT_COLR_GLYPH) {
1628 FT_OpaquePaint otherOpaquePaint;
1629 otherOpaquePaint.p =
nullptr;
1630 if (!FT_Get_Color_Glyph_Paint(face,
1631 paint.u.colr_glyph.glyphID,
1632 FT_COLOR_NO_ROOT_TRANSFORM,
1633 &otherOpaquePaint)) {
1634 qCWarning(lcColrv1) <<
"Failed to load color glyph"
1635 << paint.u.colr_glyph.glyphID
1636 <<
"in COLRv1 graph.";
1640 if (!traverseColr1(otherOpaquePaint, loops, foregroundColor, palette, paletteCount, paintGraphRenderer))
1647QFontEngineFT::Glyph *QFontEngineFT::loadColrv1Glyph(QGlyphSet *set,
1650 const QColor &foregroundColor,
1651 bool fetchMetricsOnly)
const
1653 FT_Face face = freetype->face;
1656 memset(&info, 0,
sizeof(info));
1660 FT_Load_Glyph(face, glyph, FT_LOAD_DEFAULT
1663 | FT_LOAD_BITMAP_METRICS_ONLY);
1664 info.linearAdvance =
int(face->glyph->linearHoriAdvance >> 10);
1665 info.xOff =
short(TRUNC(ROUND(face->glyph->advance.x)));
1667 FT_OpaquePaint opaquePaint;
1668 opaquePaint.p =
nullptr;
1669 if (!FT_Get_Color_Glyph_Paint(face, glyph, FT_COLOR_INCLUDE_ROOT_TRANSFORM, &opaquePaint))
1674 FT_Set_Char_Size(face, face->units_per_EM << 6, face->units_per_EM << 6, 0, 0);
1678 FT_Get_Transform(face, &matrix, &delta);
1679 QTransform originalXform(FROM_FIXED_16_16(matrix.xx), -FROM_FIXED_16_16(matrix.yx),
1680 -FROM_FIXED_16_16(matrix.xy), FROM_FIXED_16_16(matrix.yy),
1681 FROM_FIXED_16_16(delta.x), FROM_FIXED_16_16(delta.y));
1685 FT_Set_Transform(face,
nullptr,
nullptr);
1687 auto cleanup = qScopeGuard([&]() {
1689 FT_Set_Char_Size(face, xsize, ysize, 0, 0);
1690 FT_Set_Transform(face, &matrix, &delta);
1693 qCDebug(lcColrv1).noquote() <<
"================== Start collecting COLRv1 metrics for" << glyph;
1694 QRect designCoordinateBounds;
1698 if (colrv1_bounds_cache_id == glyph) {
1699 designCoordinateBounds = colrv1_bounds_cache;
1704 if (FT_Get_Color_Glyph_ClipBox(face, glyph, &clipBox)) {
1705 FT_Pos left = qMin(clipBox.bottom_left.x, qMin(clipBox.bottom_right.x, qMin(clipBox.top_left.x, clipBox.top_right.x)));
1706 FT_Pos right = qMax(clipBox.bottom_left.x, qMax(clipBox.bottom_right.x, qMax(clipBox.top_left.x, clipBox.top_right.x)));
1708 FT_Pos top = qMin(-clipBox.bottom_left.y, qMin(-clipBox.bottom_right.y, qMin(-clipBox.top_left.y, -clipBox.top_right.y)));
1709 FT_Pos bottom = qMax(-clipBox.bottom_left.y, qMax(-clipBox.bottom_right.y, qMax(-clipBox.top_left.y, -clipBox.top_right.y)));
1711 qreal scale = 1.0 / 64.0;
1712 designCoordinateBounds = QRect(QPoint(qFloor(left * scale), qFloor(top * scale)),
1713 QPoint(qCeil(right * scale), qCeil(bottom * scale)));
1716 QColrPaintGraphRenderer boundingRectCalculator;
1717 boundingRectCalculator.beginCalculateBoundingBox();
1718 QSet<std::pair<FT_Byte *, FT_Bool> > loops;
1719 if (traverseColr1(opaquePaint,
1724 &boundingRectCalculator)) {
1725 designCoordinateBounds = boundingRectCalculator.boundingRect().toAlignedRect();
1729 colrv1_bounds_cache_id = glyph;
1730 colrv1_bounds_cache = designCoordinateBounds;
1733 QTransform initialTransform;
1734 initialTransform.scale(fontDef.pixelSize / face->units_per_EM,
1735 fontDef.pixelSize / face->units_per_EM);
1736 QRect bounds = initialTransform.mapRect(designCoordinateBounds);
1737 bounds = originalXform.mapRect(bounds);
1739 info.x = bounds.left();
1740 info.y = -bounds.top();
1741 info.width = bounds.width();
1742 info.height = bounds.height();
1744 qCDebug(lcColrv1) <<
"Bounds of" << glyph <<
"==" << bounds;
1747 QImage destinationImage;
1748 if (!fetchMetricsOnly && !bounds.size().isEmpty()) {
1749 FT_Palette_Data paletteData;
1750 if (FT_Palette_Data_Get(face, &paletteData))
1753 FT_Color *palette =
nullptr;
1754 FT_Error error = FT_Palette_Select(face, 0, &palette);
1756 qWarning(
"selecting palette for COLRv1 failed, err=%x face=%p, glyph=%d",
1762 if (palette ==
nullptr)
1765 ushort paletteCount = paletteData.num_palette_entries;
1767 QColrPaintGraphRenderer paintGraphRenderer;
1768 paintGraphRenderer.setBoundingRect(bounds);
1769 paintGraphRenderer.beginRender(fontDef.pixelSize / face->units_per_EM,
1773 QSet<std::pair<FT_Byte *, FT_Bool> > loops;
1774 if (!traverseColr1(opaquePaint,
1779 &paintGraphRenderer)) {
1783 destinationImage = paintGraphRenderer.endRender();
1786 if (fetchMetricsOnly || !destinationImage.isNull()) {
1791 set->setGlyph(glyph, QFixedPoint{}, g);
1794 g->linearAdvance = info.linearAdvance;
1795 g->width = info.width;
1796 g->height = info.height;
1799 g->advance = info.xOff;
1800 g->format = Format_ARGB;
1802 if (!fetchMetricsOnly && !destinationImage.isNull()) {
1803 g->data =
new uchar[info.height * info.width * 4];
1804 memcpy(g->data, destinationImage.constBits(), info.height * info.width * 4);
1817QFontEngineFT::Glyph *QFontEngineFT::loadGlyph(QGlyphSet *set, uint glyph,
1818 const QFixedPoint &subPixelPosition,
1821 bool fetchMetricsOnly,
1822 bool disableOutlineDrawing)
const
1826 if (format == Format_None)
1827 format = defaultFormat != Format_None ? defaultFormat : Format_Mono;
1828 Q_ASSERT(format != Format_None);
1830 Glyph *g = set ? set->getGlyph(glyph, subPixelPosition) :
nullptr;
1831 if (g && g->format == format && (fetchMetricsOnly || g->data))
1834 if (!g && set && set->isGlyphMissing(glyph))
1838 FT_Face face = freetype->face;
1840 FT_Matrix matrix = freetype->matrix;
1841 bool transform = matrix.xx != 0x10000
1842 || matrix.yy != 0x10000
1845 if (obliquen && transform) {
1850 slant.xx = 0x10000L;
1852 slant.xy = obliqueSlant;
1853 slant.yy = 0x10000L;
1855 FT_Matrix_Multiply(&matrix, &slant);
1860 v.x = format == Format_Mono ? 0 : FT_Pos(subPixelPosition.x.value());
1861 v.y = format == Format_Mono ? 0 : FT_Pos(-subPixelPosition.y.value());
1862 FT_Set_Transform(face, &matrix, &v);
1864 bool hsubpixel =
false;
1866 int load_flags = loadFlags(set, format, 0, hsubpixel, vfactor);
1868 if (transform || obliquen || (format != Format_Mono && !isScalableBitmap()))
1869 load_flags |= FT_LOAD_NO_BITMAP;
1871#if (FREETYPE_MAJOR*10000
+ FREETYPE_MINOR*100
+ FREETYPE_PATCH) >= 20701
1873 if (fetchMetricsOnly)
1874 load_flags |= FT_LOAD_BITMAP_METRICS_ONLY;
1877#if defined(QFONTENGINE_FT_SUPPORT_COLRV1)
1878 if (FT_IS_SCALABLE(freetype->face)
1879 && FT_HAS_COLOR(freetype->face)
1880 && (load_flags & FT_LOAD_COLOR)) {
1882 Glyph *ret = loadColrv1Glyph(set, g, glyph, color, fetchMetricsOnly);
1890 FT_Error err = FT_Load_Glyph(face, glyph, load_flags);
1891 if (err && (load_flags & FT_LOAD_NO_BITMAP)) {
1892 load_flags &= ~FT_LOAD_NO_BITMAP;
1893 err = FT_Load_Glyph(face, glyph, load_flags);
1895 if (err == FT_Err_Too_Few_Arguments) {
1897 load_flags |= FT_LOAD_FORCE_AUTOHINT;
1898 err = FT_Load_Glyph(face, glyph, load_flags);
1899 }
else if (err == FT_Err_Execution_Too_Long) {
1902 qWarning(
"load glyph failed due to broken hinting bytecode in font, switching to auto hinting");
1903 default_load_flags |= FT_LOAD_FORCE_AUTOHINT;
1904 load_flags |= FT_LOAD_FORCE_AUTOHINT;
1905 err = FT_Load_Glyph(face, glyph, load_flags);
1907 if (err != FT_Err_Ok) {
1908 qWarning(
"load glyph failed err=%x face=%p, glyph=%d", err, face, glyph);
1910 set->setGlyphMissing(glyph);
1914 FT_GlyphSlot slot = face->glyph;
1917 FT_GlyphSlot_Embolden(slot);
1918 if (obliquen && !transform) {
1919 FT_GlyphSlot_Oblique(slot);
1930 FT_Matrix_Multiply(&m, &matrix);
1934 info.linearAdvance = slot->linearHoriAdvance >> 10;
1938 if ((set && set->outline_drawing && !disableOutlineDrawing) || fetchMetricsOnly) {
1939 int left = slot->metrics.horiBearingX;
1940 int right = slot->metrics.horiBearingX + slot->metrics.width;
1941 int top = slot->metrics.horiBearingY;
1942 int bottom = slot->metrics.horiBearingY - slot->metrics.height;
1944 if (transform && slot->format != FT_GLYPH_FORMAT_BITMAP)
1945 transformBoundingBox(&left, &top, &right, &bottom, &matrix);
1948 right =
CEIL(right);
1949 bottom =
FLOOR(bottom);
1952 info.x =
TRUNC(left);
1953 info.y =
TRUNC(top);
1954 info.width =
TRUNC(right - left);
1955 info.height =
TRUNC(top - bottom);
1959 if (areMetricsTooLarge(info))
1964 g->linearAdvance = info.linearAdvance;
1965 g->width = info.width;
1966 g->height = info.height;
1969 g->advance = info.xOff;
1973 set->setGlyph(glyph, subPixelPosition, g);
1978 if (slot->format == FT_GLYPH_FORMAT_OUTLINE) {
1979 const QTransform xform(matrix.xx / 65536.0, matrix.xy / 65536.0,
1980 matrix.yx / 65536.0, matrix.yy / 65536.0, 0, 0);
1982 FT_Outline_Get_CBox(&slot->outline, &cbox);
1983 const qreal glyphWidth = qreal((cbox.xMax - cbox.xMin) >> 6);
1984 const qreal glyphHeight = qreal((cbox.yMax - cbox.yMin) >> 6);
1985 if (glyphBoundingBoxExceedsLimit(glyphWidth, glyphHeight, effectivePixelSizeSquared(xform))) {
1987 set->setGlyphMissing(glyph);
1992 int glyph_buffer_size = 0;
1993 std::unique_ptr<uchar[]> glyph_buffer;
1994 FT_Render_Mode renderMode = (default_hint_style == HintLight) ? FT_RENDER_MODE_LIGHT : FT_RENDER_MODE_NORMAL;
1997 renderMode = FT_RENDER_MODE_MONO;
2000 if (!hsubpixel && vfactor == 1) {
2001 qWarning(
"Format_A32 requested, but subpixel layout is unknown.");
2005 renderMode = hsubpixel ? FT_RENDER_MODE_LCD : FT_RENDER_MODE_LCD_V;
2013 FT_Library_SetLcdFilter(slot->library, (FT_LcdFilter)lcdFilterType);
2015 err = FT_Render_Glyph(slot, renderMode);
2016 FT_Library_SetLcdFilter(slot->library, FT_LCD_FILTER_NONE);
2018 if (err != FT_Err_Ok) {
2019 qWarning(
"render glyph failed err=%x face=%p, glyph=%d", err, face, glyph);
2023 info.height = slot->bitmap.rows;
2024 info.width = slot->bitmap.width;
2025 info.x = slot->bitmap_left;
2026 info.y = slot->bitmap_top;
2027 if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_LCD)
2028 info.width = info.width / 3;
2029 if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V)
2030 info.height = info.height / vfactor;
2032 int pitch = (format == Format_Mono ? ((info.width + 31) & ~31) >> 3 :
2033 (format == Format_A8 ? (info.width + 3) & ~3 : info.width * 4));
2035 glyph_buffer_size = info.height * pitch;
2036 glyph_buffer.reset(
new uchar[glyph_buffer_size]);
2038 if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {
2039 uchar *src = slot->bitmap.buffer;
2040 uchar *dst = glyph_buffer.get();
2041 int h = slot->bitmap.rows;
2043 if (format == Format_Mono) {
2044 int bytes = ((info.width + 7) & ~7) >> 3;
2046 memcpy (dst, src, bytes);
2048 src += slot->bitmap.pitch;
2050 }
else if (format == Format_A8) {
2052 for (
int x = 0; x <
int{info.width}; x++)
2053 dst[x] = ((src[x >> 3] & (0x80 >> (x & 7))) ? 0xff : 0x00);
2055 src += slot->bitmap.pitch;
2059 uint *dd =
reinterpret_cast<uint *>(dst);
2060 for (
int x = 0; x <
int{info.width}; x++)
2061 dd[x] = ((src[x >> 3] & (0x80 >> (x & 7))) ? 0xffffffff : 0x00000000);
2063 src += slot->bitmap.pitch;
2066 }
else if (slot->bitmap.pixel_mode == 7 ) {
2067 Q_ASSERT(format == Format_ARGB);
2068 uchar *src = slot->bitmap.buffer;
2069 uchar *dst = glyph_buffer.get();
2070 int h = slot->bitmap.rows;
2072#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2073 const quint32 *srcPixel = (
const quint32 *)src;
2074 quint32 *dstPixel = (quint32 *)dst;
2075 for (
int x = 0; x <
static_cast<
int>(slot->bitmap.width); x++, srcPixel++, dstPixel++) {
2076 const quint32 pixel = *srcPixel;
2077 *dstPixel = qbswap(pixel);
2080 memcpy(dst, src, slot->bitmap.width * 4);
2082 dst += slot->bitmap.pitch;
2083 src += slot->bitmap.pitch;
2085 info.linearAdvance = info.xOff = slot->bitmap.width;
2086 }
else if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) {
2087 if (format == Format_A8) {
2088 uchar *src = slot->bitmap.buffer;
2089 uchar *dst = glyph_buffer.get();
2090 int h = slot->bitmap.rows;
2091 int bytes = info.width;
2093 memcpy (dst, src, bytes);
2095 src += slot->bitmap.pitch;
2097 }
else if (format == Format_ARGB) {
2098 uchar *src = slot->bitmap.buffer;
2099 quint32 *dstPixel =
reinterpret_cast<quint32 *>(glyph_buffer.get());
2100 int h = slot->bitmap.rows;
2102 for (
int x = 0; x <
static_cast<
int>(slot->bitmap.width); ++x) {
2103 uchar alpha = src[x];
2104 float alphaF = alpha / 255.0;
2105 dstPixel[x] = qRgba(qRound(alphaF * color.red()),
2106 qRound(alphaF * color.green()),
2107 qRound(alphaF * color.blue()),
2110 src += slot->bitmap.pitch;
2111 dstPixel += info.width;
2114 }
else if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_LCD) {
2115 Q_ASSERT(format == Format_A32);
2116 convertRGBToARGB(slot->bitmap.buffer, (uint *)glyph_buffer.get(), info.width, info.height, slot->bitmap.pitch, subpixelType != Subpixel_RGB);
2117 }
else if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V) {
2118 Q_ASSERT(format == Format_A32);
2119 convertRGBToARGB_V(slot->bitmap.buffer, (uint *)glyph_buffer.get(), info.width, info.height, slot->bitmap.pitch, subpixelType != Subpixel_VRGB);
2121 qWarning(
"QFontEngine: Glyph rendered in unknown pixel_mode=%d", slot->bitmap.pixel_mode);
2130 g->linearAdvance = info.linearAdvance;
2131 g->width = info.width;
2132 g->height = info.height;
2135 g->advance = info.xOff;
2138 g->data = glyph_buffer.release();
2141 set->setGlyph(glyph, subPixelPosition, g);
2146QFontEngine::FaceId QFontEngineFT::faceId()
const
2151QFontEngine::Properties QFontEngineFT::properties()
const
2153 Properties p = freetype->properties();
2154 if (p.postscriptName.isEmpty()) {
2155 p.postscriptName = QFontEngine::convertToPostscriptFontFamilyName(fontDef.family().toUtf8());
2158 return freetype->properties();
2161QFixed QFontEngineFT::emSquareSize()
const
2163 if (FT_IS_SCALABLE(freetype->face))
2164 return freetype->face->units_per_EM;
2166 return freetype->face->size->metrics.y_ppem;
2169bool QFontEngineFT::getSfntTableData(uint tag, uchar *buffer, uint *length)
const
2171 return ft_getSfntTable(freetype->face, tag, buffer, length);
2174int QFontEngineFT::synthesized()
const
2177 if ((fontDef.style != QFont::StyleNormal) && !(freetype->face->style_flags & FT_STYLE_FLAG_ITALIC))
2178 s = SynthesizedItalic;
2179 if ((fontDef.weight >= QFont::Bold) && !(freetype->face->style_flags & FT_STYLE_FLAG_BOLD))
2180 s |= SynthesizedBold;
2181 if (fontDef.stretch != 100 && FT_IS_SCALABLE(freetype->face))
2182 s |= SynthesizedStretch;
2186void QFontEngineFT::initializeHeightMetrics()
const
2188 m_ascent = QFixed::fromFixed(metrics.ascender);
2189 m_descent = QFixed::fromFixed(-metrics.descender);
2190 m_leading = QFixed::fromFixed(metrics.height - metrics.ascender + metrics.descender);
2192 QFontEngine::initializeHeightMetrics();
2194 if (scalableBitmapScaleFactor != 1) {
2195 m_ascent *= scalableBitmapScaleFactor;
2196 m_descent *= scalableBitmapScaleFactor;
2197 m_leading *= scalableBitmapScaleFactor;
2201QFixed QFontEngineFT::capHeight()
const
2203 TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(freetype->face, ft_sfnt_os2);
2204 if (os2 && os2->version >= 2) {
2206 QFixed answer = QFixed::fromFixed(FT_MulFix(os2->sCapHeight, freetype->face->size->metrics.y_scale));
2210 return calculatedCapHeight();
2213QFixed QFontEngineFT::xHeight()
const
2215 TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(freetype->face, ft_sfnt_os2);
2216 if (os2 && os2->sxHeight) {
2218 QFixed answer = QFixed(os2->sxHeight * freetype->face->size->metrics.y_ppem) / emSquareSize();
2223 return QFontEngine::xHeight();
2226QFixed QFontEngineFT::averageCharWidth()
const
2228 TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(freetype->face, ft_sfnt_os2);
2229 if (os2 && os2->xAvgCharWidth) {
2231 QFixed answer = QFixed(os2->xAvgCharWidth * freetype->face->size->metrics.x_ppem) / emSquareSize();
2236 return QFontEngine::averageCharWidth();
2239qreal QFontEngineFT::maxCharWidth()
const
2241 QFixed max_advance = QFixed::fromFixed(metrics.max_advance);
2242 if (scalableBitmapScaleFactor != 1)
2243 max_advance *= scalableBitmapScaleFactor;
2244 return max_advance.toReal();
2247QFixed QFontEngineFT::lineThickness()
const
2249 return line_thickness;
2252QFixed QFontEngineFT::underlinePosition()
const
2254 return underline_position;
2257void QFontEngineFT::doKerning(QGlyphLayout *g, QFontEngine::ShaperFlags flags)
const
2259 if (!kerning_pairs_loaded) {
2260 kerning_pairs_loaded =
true;
2262 if (freetype->face->size->metrics.x_ppem != 0) {
2263 QFixed scalingFactor = emSquareSize() / QFixed(freetype->face->size->metrics.x_ppem);
2265 const_cast<QFontEngineFT *>(
this)->loadKerningPairs(scalingFactor);
2271 if (shouldUseDesignMetrics(flags))
2272 flags |= DesignMetrics;
2274 flags &= ~DesignMetrics;
2276 QFontEngine::doKerning(g, flags);
2283 m.xx = FT_Fixed(matrix.m11() * 65536);
2284 m.xy = FT_Fixed(-matrix.m21() * 65536);
2285 m.yx = FT_Fixed(-matrix.m12() * 65536);
2286 m.yy = FT_Fixed(matrix.m22() * 65536);
2291QFontEngineFT::QGlyphSet *QFontEngineFT::TransformedGlyphSets::findSet(
const QTransform &matrix,
const QFontDef &fontDef)
2293 FT_Matrix m = QTransformToFTMatrix(matrix);
2296 for (; i < nSets; ++i) {
2297 QGlyphSet *g = sets[i];
2300 if (g->transformationMatrix.xx == m.xx
2301 && g->transformationMatrix.xy == m.xy
2302 && g->transformationMatrix.yx == m.yx
2303 && g->transformationMatrix.yy == m.yy) {
2315 moveToFront(nSets - 1);
2317 sets[0] =
new QGlyphSet;
2318 QGlyphSet *gs = sets[0];
2319 Q_ASSERT(gs !=
nullptr);
2322 gs->transformationMatrix = m;
2323 const int maxCachedSize = maxCachedGlyphSize();
2324 gs->outline_drawing = fontDef.pixelSize * fontDef.pixelSize * qAbs(matrix.determinant()) > maxCachedSize * maxCachedSize;
2329void QFontEngineFT::TransformedGlyphSets::moveToFront(
int i)
2331 QGlyphSet *g = sets[i];
2333 sets[i] = sets[i - 1];
2340QFontEngineFT::QGlyphSet *QFontEngineFT::loadGlyphSet(
const QTransform &matrix)
2342 if (matrix.type() > QTransform::TxShear || !cacheEnabled)
2346 if (!FT_IS_SCALABLE(freetype->face))
2347 return matrix.type() <= QTransform::TxTranslate ? &defaultGlyphSet :
nullptr;
2349 return transformedGlyphSets.findSet(matrix, fontDef);
2352void QFontEngineFT::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
2354 FT_Face face = lockFace(Unscaled);
2355 FT_Set_Transform(face,
nullptr,
nullptr);
2356 FT_Load_Glyph(face, glyph, FT_LOAD_NO_BITMAP);
2358 int left = face->glyph->metrics.horiBearingX;
2359 int right = face->glyph->metrics.horiBearingX + face->glyph->metrics.width;
2360 int top = face->glyph->metrics.horiBearingY;
2361 int bottom = face->glyph->metrics.horiBearingY - face->glyph->metrics.height;
2367 metrics->width = QFixed::fromFixed(right-left);
2368 metrics->height = QFixed::fromFixed(top-bottom);
2369 metrics->x = QFixed::fromFixed(left);
2370 metrics->y = QFixed::fromFixed(-top);
2371 metrics->xoff = QFixed::fromFixed(face->glyph->advance.x);
2373 if (!FT_IS_SCALABLE(freetype->face))
2374 QFreetypeFace::addBitmapToPath(face->glyph, p, path);
2376 QFreetypeFace::addGlyphToPath(face, face->glyph, p, path, face->units_per_EM << 6, face->units_per_EM << 6);
2378 FT_Set_Transform(face, &freetype->matrix,
nullptr);
2382bool QFontEngineFT::supportsTransformation(
const QTransform &transform)
const
2384 return transform.type() <= QTransform::TxRotate;
2387void QFontEngineFT::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
2389 if (!glyphs.numGlyphs)
2392 if (FT_IS_SCALABLE(freetype->face)) {
2393 QFontEngine::addOutlineToPath(x, y, glyphs, path, flags);
2395 QVarLengthArray<QFixedPoint> positions;
2396 QVarLengthArray<glyph_t> positioned_glyphs;
2398 matrix.translate(x, y);
2399 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
2401 FT_Face face = lockFace(Unscaled);
2402 for (
int gl = 0; gl < glyphs.numGlyphs; gl++) {
2403 FT_UInt glyph = positioned_glyphs[gl];
2404 FT_Load_Glyph(face, glyph, FT_LOAD_TARGET_MONO);
2405 QFreetypeFace::addBitmapToPath(face->glyph, positions[gl], path);
2411void QFontEngineFT::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions,
int numGlyphs,
2412 QPainterPath *path, QTextItem::RenderFlags)
2414 FT_Face face = lockFace(Unscaled);
2416 for (
int gl = 0; gl < numGlyphs; gl++) {
2417 FT_UInt glyph = glyphs[gl];
2419 FT_Load_Glyph(face, glyph, FT_LOAD_NO_BITMAP);
2421 FT_GlyphSlot g = face->glyph;
2422 if (g->format != FT_GLYPH_FORMAT_OUTLINE)
2425 FT_GlyphSlot_Embolden(g);
2427 FT_GlyphSlot_Oblique(g);
2428 QFreetypeFace::addGlyphToPath(face, g, positions[gl], path, xsize, ysize);
2433glyph_t QFontEngineFT::glyphIndex(uint ucs4)
const
2435 glyph_t glyph = ucs4 < QFreetypeFace::cmapCacheSize ? freetype->cmapCache[ucs4] : 0;
2437 FT_Face face = freetype->face;
2438 glyph = FT_Get_Char_Index(face, ucs4);
2442 if (ucs4 == QChar::Nbsp || ucs4 == QChar::Tabulation) {
2443 glyph = FT_Get_Char_Index(face, QChar::Space);
2444 }
else if (freetype->symbol_map) {
2451 FT_Set_Charmap(face, freetype->symbol_map);
2452 glyph = FT_Get_Char_Index(face, ucs4);
2453 FT_Set_Charmap(face, freetype->unicode_map);
2454 if (!glyph && symbol && ucs4 < 0x100)
2455 glyph = FT_Get_Char_Index(face, ucs4 + 0xf000);
2458 if (ucs4 < QFreetypeFace::cmapCacheSize)
2459 freetype->cmapCache[ucs4] = glyph;
2465QString QFontEngineFT::glyphName(glyph_t index)
const
2468 if (index >= glyph_t(glyphCount()))
2471 FT_Face face = freetype->face;
2472 if (face->face_flags & FT_FACE_FLAG_GLYPH_NAMES) {
2473 char glyphName[128] = {};
2474 if (FT_Get_Glyph_Name(face, index, glyphName,
sizeof(glyphName)) == 0)
2475 result = QString::fromUtf8(glyphName);
2478 return result.isEmpty() ? QFontEngine::glyphName(index) : result;
2481int QFontEngineFT::stringToCMap(
const QChar *str,
int len, QGlyphLayout *glyphs,
int *nglyphs,
2482 QFontEngine::ShaperFlags flags)
const
2484 Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
2485 if (*nglyphs < len) {
2490 int mappedGlyphs = 0;
2492 if (freetype->symbol_map) {
2493 FT_Face face = freetype->face;
2494 QStringIterator it(str, str + len);
2495 while (it.hasNext()) {
2496 uint uc = it.next();
2497 glyphs->glyphs[glyph_pos] = uc < QFreetypeFace::cmapCacheSize ? freetype->cmapCache[uc] : 0;
2498 if ( !glyphs->glyphs[glyph_pos] ) {
2505 glyph_t glyph = FT_Get_Char_Index(face, uc);
2508 if (!glyph && (uc == 0xa0 || uc == 0x9)) {
2510 glyph = FT_Get_Char_Index(face, uc);
2513 FT_Set_Charmap(face, freetype->symbol_map);
2514 glyph = FT_Get_Char_Index(face, uc);
2515 FT_Set_Charmap(face, freetype->unicode_map);
2516 if (!glyph && symbol && uc < 0x100)
2517 glyph = FT_Get_Char_Index(face, uc + 0xf000);
2519 glyphs->glyphs[glyph_pos] = glyph;
2520 if (uc < QFreetypeFace::cmapCacheSize)
2521 freetype->cmapCache[uc] = glyph;
2523 if (glyphs->glyphs[glyph_pos] || isIgnorableChar(uc))
2528 FT_Face face = freetype->face;
2529 QStringIterator it(str, str + len);
2530 while (it.hasNext()) {
2531 uint uc = it.next();
2532 glyphs->glyphs[glyph_pos] = uc < QFreetypeFace::cmapCacheSize ? freetype->cmapCache[uc] : 0;
2533 if (!glyphs->glyphs[glyph_pos]) {
2536 glyph_t glyph = FT_Get_Char_Index(face, uc);
2537 if (!glyph && (uc == 0xa0 || uc == 0x9)) {
2541 glyphs->glyphs[glyph_pos] = glyph;
2542 if (uc < QFreetypeFace::cmapCacheSize)
2543 freetype->cmapCache[uc] = glyph;
2546 if (glyphs->glyphs[glyph_pos] || isIgnorableChar(uc))
2552 *nglyphs = glyph_pos;
2553 glyphs->numGlyphs = glyph_pos;
2555 if (!(flags & GlyphIndicesOnly))
2556 recalcAdvances(glyphs, flags);
2558 return mappedGlyphs;
2561bool QFontEngineFT::shouldUseDesignMetrics(QFontEngine::ShaperFlags flags)
const
2563 if (!FT_IS_SCALABLE(freetype->face))
2566 return supportsHorizontalSubPixelPositions() || (flags & DesignMetrics);
2569QFixed QFontEngineFT::scaledBitmapMetrics(QFixed m)
const
2571 return m * scalableBitmapScaleFactor;
2574glyph_metrics_t QFontEngineFT::scaledBitmapMetrics(
const glyph_metrics_t &m,
const QTransform &t)
const
2577 trans.setMatrix(t.m11(), t.m12(), t.m13(),
2578 t.m21(), t.m22(), t.m23(),
2580 const qreal scaleFactor = scalableBitmapScaleFactor.toReal();
2581 trans.scale(scaleFactor, scaleFactor);
2583 QRectF rect(m.x.toReal(), m.y.toReal(), m.width.toReal(), m.height.toReal());
2584 QPointF offset(m.xoff.toReal(), m.yoff.toReal());
2586 rect = trans.mapRect(rect);
2587 offset = trans.map(offset);
2589 glyph_metrics_t metrics;
2590 metrics.x = QFixed::fromReal(rect.x());
2591 metrics.y = QFixed::fromReal(rect.y());
2592 metrics.width = QFixed::fromReal(rect.width());
2593 metrics.height = QFixed::fromReal(rect.height());
2594 metrics.xoff = QFixed::fromReal(offset.x());
2595 metrics.yoff = QFixed::fromReal(offset.y());
2599void QFontEngineFT::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2601 FT_Face face =
nullptr;
2602 bool design = shouldUseDesignMetrics(flags);
2603 for (
int i = 0; i < glyphs->numGlyphs; i++) {
2604 Glyph *g = cacheEnabled ? defaultGlyphSet.getGlyph(glyphs->glyphs[i]) :
nullptr;
2606 GlyphFormat acceptableFormat = (defaultFormat != Format_None) ? defaultFormat : Format_Mono;
2607 if (g && g->format == acceptableFormat) {
2608 glyphs->advances[i] = design ? QFixed::fromFixed(g->linearAdvance) : QFixed(g->advance);
2612 g = loadGlyph(cacheEnabled ? &defaultGlyphSet :
nullptr,
2619 glyphs->advances[i] = design ? QFixed::fromFixed(g->linearAdvance) : QFixed(g->advance);
2621 glyphs->advances[i] = design ? QFixed::fromFixed(face->glyph->linearHoriAdvance >> 10)
2622 : QFixed::fromFixed(face->glyph->metrics.horiAdvance).round();
2623 if (!cacheEnabled && g != &emptyGlyph)
2627 if (scalableBitmapScaleFactor != 1)
2628 glyphs->advances[i] *= scalableBitmapScaleFactor;
2634glyph_metrics_t QFontEngineFT::boundingBox(
const QGlyphLayout &glyphs)
2636 FT_Face face =
nullptr;
2638 glyph_metrics_t overall;
2640 if (!isScalableBitmap()) {
2641 overall.y = -ascent();
2642 overall.height = ascent() + descent();
2644 overall.y = QFixed::fromFixed(-metrics.ascender);
2645 overall.height = QFixed::fromFixed(metrics.ascender - metrics.descender);
2650 for (
int i = 0; i < glyphs.numGlyphs; i++) {
2652 if (!glyphs.advances[i] || glyphs.attributes[i].dontPrint)
2654 Glyph *g = cacheEnabled ? defaultGlyphSet.getGlyph(glyphs.glyphs[i]) :
nullptr;
2658 g = loadGlyph(cacheEnabled ? &defaultGlyphSet :
nullptr,
2666 QFixed x = overall.xoff + glyphs.offsets[i].x + g->x;
2667 QFixed y = overall.yoff + glyphs.offsets[i].y - g->y;
2668 overall.x = qMin(overall.x, x);
2669 overall.y = qMin(overall.y, y);
2670 xmax = qMax(xmax, x.ceil() + g->width);
2671 ymax = qMax(ymax, y.ceil() + g->height);
2672 if (!cacheEnabled && g != &emptyGlyph)
2675 int left =
FLOOR(face->glyph->metrics.horiBearingX);
2676 int right =
CEIL(face->glyph->metrics.horiBearingX + face->glyph->metrics.width);
2677 int top =
CEIL(face->glyph->metrics.horiBearingY);
2678 int bottom =
FLOOR(face->glyph->metrics.horiBearingY - face->glyph->metrics.height);
2680 QFixed x = overall.xoff + glyphs.offsets[i].x - (-
TRUNC(left));
2681 QFixed y = overall.yoff + glyphs.offsets[i].y -
TRUNC(top);
2682 overall.x = qMin(overall.x, x);
2683 overall.y = qMin(overall.y, y);
2684 xmax = qMax(xmax, x +
TRUNC(right - left));
2685 ymax = qMax(ymax, y +
TRUNC(top - bottom));
2687 overall.xoff += glyphs.effectiveAdvance(i);
2689 overall.height = qMax(overall.height, ymax - overall.y);
2690 overall.width = xmax - overall.x;
2695 if (isScalableBitmap())
2696 overall = scaledBitmapMetrics(overall, QTransform());
2700glyph_metrics_t QFontEngineFT::boundingBox(glyph_t glyph)
2702 FT_Face face =
nullptr;
2703 glyph_metrics_t overall;
2704 Glyph *g = cacheEnabled ? defaultGlyphSet.getGlyph(glyph) :
nullptr;
2707 g = loadGlyph(cacheEnabled ? &defaultGlyphSet :
nullptr,
2717 overall.width = g->width;
2718 overall.height = g->height;
2719 overall.xoff = g->advance;
2720 if (!cacheEnabled && g != &emptyGlyph)
2723 int left =
FLOOR(face->glyph->metrics.horiBearingX);
2724 int right =
CEIL(face->glyph->metrics.horiBearingX + face->glyph->metrics.width);
2725 int top =
CEIL(face->glyph->metrics.horiBearingY);
2726 int bottom =
FLOOR(face->glyph->metrics.horiBearingY - face->glyph->metrics.height);
2728 overall.width =
TRUNC(right-left);
2729 overall.height =
TRUNC(top-bottom);
2730 overall.x =
TRUNC(left);
2731 overall.y = -
TRUNC(top);
2732 overall.xoff =
TRUNC(
ROUND(face->glyph->advance.x));
2737 if (isScalableBitmap())
2738 overall = scaledBitmapMetrics(overall, QTransform());
2742glyph_metrics_t QFontEngineFT::boundingBox(glyph_t glyph,
const QTransform &matrix)
2744 return alphaMapBoundingBox(glyph, QFixedPoint(), matrix, QFontEngine::Format_None);
2747glyph_metrics_t QFontEngineFT::alphaMapBoundingBox(glyph_t glyph,
2748 const QFixedPoint &subPixelPosition,
2749 const QTransform &matrix,
2750 QFontEngine::GlyphFormat format)
2756 const bool needsImageTransform = !FT_IS_SCALABLE(freetype->face)
2757 && matrix.type() > QTransform::TxTranslate;
2758 if (needsImageTransform && format == QFontEngine::Format_Mono)
2759 format = QFontEngine::Format_A8;
2760 Glyph *g = loadGlyphFor(glyph, subPixelPosition, format, matrix, QColor(),
true,
true);
2762 glyph_metrics_t overall;
2766 overall.width = g->width;
2767 overall.height = g->height;
2768 overall.xoff = g->advance;
2769 if (!cacheEnabled && g != &emptyGlyph)
2772 FT_Face face = lockFace();
2773 int left =
FLOOR(face->glyph->metrics.horiBearingX);
2774 int right =
CEIL(face->glyph->metrics.horiBearingX + face->glyph->metrics.width);
2775 int top =
CEIL(face->glyph->metrics.horiBearingY);
2776 int bottom =
FLOOR(face->glyph->metrics.horiBearingY - face->glyph->metrics.height);
2778 overall.width =
TRUNC(right-left);
2779 overall.height =
TRUNC(top-bottom);
2780 overall.x =
TRUNC(left);
2781 overall.y = -
TRUNC(top);
2782 overall.xoff =
TRUNC(
ROUND(face->glyph->advance.x));
2786 if (isScalableBitmap() || needsImageTransform)
2787 overall = scaledBitmapMetrics(overall, matrix);
2793 if (glyph ==
nullptr || glyph->height == 0 || glyph->width == 0)
2796 QImage::Format format = QImage::Format_Invalid;
2797 int bytesPerLine = -1;
2798 switch (glyphFormat) {
2799 case QFontEngine::Format_Mono:
2800 format = QImage::Format_Mono;
2801 bytesPerLine = ((glyph->width + 31) & ~31) >> 3;
2803 case QFontEngine::Format_A8:
2804 format = QImage::Format_Alpha8;
2805 bytesPerLine = (glyph->width + 3) & ~3;
2807 case QFontEngine::Format_A32:
2808 format = QImage::Format_RGB32;
2809 bytesPerLine = glyph->width * 4;
2815 QImage img(
static_cast<
const uchar *>(glyph->data), glyph->width, glyph->height, bytesPerLine, format);
2816 if (format == QImage::Format_Mono)
2817 img.setColor(1, QColor(Qt::white).rgba());
2821QFontEngine::Glyph *QFontEngineFT::glyphData(glyph_t glyphIndex,
2822 const QFixedPoint &subPixelPosition,
2823 QFontEngine::GlyphFormat neededFormat,
2824 const QTransform &t)
2826 Q_ASSERT(cacheEnabled);
2829 neededFormat = Format_Mono;
2830 else if (neededFormat == Format_None && defaultFormat != Format_None)
2831 neededFormat = defaultFormat;
2832 else if (neededFormat == Format_None)
2833 neededFormat = Format_A8;
2835 Glyph *glyph = loadGlyphFor(glyphIndex, subPixelPosition, neededFormat, t, QColor());
2836 if (!glyph || !glyph->width || !glyph->height)
2844 return qFuzzyCompare(t.m11(), t.m22()) && qFuzzyCompare(t.m12(), -t.m21())
2845 && qFuzzyCompare(t.m11()*t.m22() - t.m12()*t.m21(), qreal(1.0));
2848QFontEngineFT::Glyph *QFontEngineFT::loadGlyphFor(glyph_t g,
2849 const QFixedPoint &subPixelPosition,
2851 const QTransform &t,
2853 bool fetchBoundingBox,
2854 bool disableOutlineDrawing)
2856 QGlyphSet *glyphSet = loadGlyphSet(t);
2857 if (glyphSet !=
nullptr && glyphSet->outline_drawing && !disableOutlineDrawing && !fetchBoundingBox)
2860 Glyph *glyph = glyphSet !=
nullptr ? glyphSet->getGlyph(g, subPixelPosition) :
nullptr;
2861 if (!glyph || glyph->format != format || (!fetchBoundingBox && !glyph->data)) {
2862 QScopedValueRollback<HintStyle> saved_default_hint_style(default_hint_style);
2865 const qreal scaleX = qAbs(t.m11());
2866 const qreal scaleY = qAbs(t.m22());
2867 const bool hintAtDeviceSize = default_hint_style != HintNone
2868 && format != Format_ARGB
2869 && FT_IS_SCALABLE(freetype->face)
2870 && t.type() <= QTransform::TxScale
2871 && xsize * scaleX >= 64 && ysize * scaleY >= 64;
2873 FT_Matrix m =
this->matrix;
2874 if (hintAtDeviceSize) {
2877 setFaceSize(qRound(xsize * scaleX), qRound(ysize * scaleY));
2882 m.xy = qRound(obliqueSlant * (scaleX / scaleY - 1));
2894 if (t.type() >= QTransform::TxScale && !is2dRotation(t))
2895 default_hint_style = HintNone;
2898 FT_Matrix ftMatrix = glyphSet !=
nullptr ? glyphSet->transformationMatrix : QTransformToFTMatrix(t);
2899 FT_Matrix_Multiply(&ftMatrix, &m);
2901 freetype->matrix = m;
2902 glyph = loadGlyph(glyphSet, g, subPixelPosition, color, format,
false, disableOutlineDrawing);
2909QImage QFontEngineFT::alphaMapForGlyph(glyph_t g,
const QFixedPoint &subPixelPosition)
2911 return alphaMapForGlyph(g, subPixelPosition, QTransform());
2914QImage QFontEngineFT::alphaMapForGlyph(glyph_t g,
2915 const QFixedPoint &subPixelPosition,
2916 const QTransform &t)
2918 const bool needsImageTransform = !FT_IS_SCALABLE(freetype->face)
2919 && t.type() > QTransform::TxTranslate;
2920 const GlyphFormat neededFormat = antialias || needsImageTransform ? Format_A8 : Format_Mono;
2922 Glyph *glyph = loadGlyphFor(g, subPixelPosition, neededFormat, t, QColor(),
false,
true);
2924 QImage img = alphaMapFromGlyphData(glyph, neededFormat);
2925 if (needsImageTransform)
2926 img = img.transformed(t, Qt::FastTransformation);
2930 if (!cacheEnabled && glyph != &emptyGlyph)
2936QImage QFontEngineFT::alphaRGBMapForGlyph(glyph_t g,
2937 const QFixedPoint &subPixelPosition,
2938 const QTransform &t)
2940 if (t.type() > QTransform::TxRotate)
2941 return QFontEngine::alphaRGBMapForGlyph(g, subPixelPosition, t);
2943 const bool needsImageTransform = !FT_IS_SCALABLE(freetype->face)
2944 && t.type() > QTransform::TxTranslate;
2947 const GlyphFormat neededFormat = Format_A32;
2949 Glyph *glyph = loadGlyphFor(g, subPixelPosition, neededFormat, t, QColor(),
false,
true);
2951 QImage img = alphaMapFromGlyphData(glyph, neededFormat);
2952 if (needsImageTransform)
2953 img = img.transformed(t, Qt::FastTransformation);
2957 if (!cacheEnabled && glyph != &emptyGlyph)
2963 return QFontEngine::alphaRGBMapForGlyph(g, subPixelPosition, t);
2966QImage QFontEngineFT::bitmapForGlyph(glyph_t g,
2967 const QFixedPoint &subPixelPosition,
2968 const QTransform &t,
2969 const QColor &color)
2971 Glyph *glyph = loadGlyphFor(g, subPixelPosition, defaultFormat, t, color);
2972 if (glyph ==
nullptr)
2976 if (defaultFormat == GlyphFormat::Format_ARGB)
2977 img = QImage(glyph->data, glyph->width, glyph->height, QImage::Format_ARGB32_Premultiplied).copy();
2978 else if (defaultFormat == GlyphFormat::Format_Mono)
2979 img = QImage(glyph->data, glyph->width, glyph->height, QImage::Format_Mono).copy();
2981 if (!img.isNull() && (scalableBitmapScaleFactor != 1 || (!t.isIdentity() && !isSmoothlyScalable))) {
2982 QTransform trans(t);
2983 const qreal scaleFactor = scalableBitmapScaleFactor.toReal();
2984 trans.scale(scaleFactor, scaleFactor);
2985 img = img.transformed(trans, Qt::SmoothTransformation);
2988 if (!cacheEnabled && glyph != &emptyGlyph)
2994void QFontEngineFT::removeGlyphFromCache(glyph_t glyph)
2996 defaultGlyphSet.removeGlyphFromCache(glyph, QFixedPoint());
2999int QFontEngineFT::glyphCount()
const
3002 FT_Face face = lockFace();
3004 count =
int(std::clamp(face->num_glyphs, FT_Long(0), FT_Long(INT_MAX)));
3010FT_Face QFontEngineFT::lockFace(Scaling scale)
const
3013 FT_Face face = freetype->face;
3014 if (scale == Unscaled) {
3015 if (FT_Set_Char_Size(face, face->units_per_EM << 6, face->units_per_EM << 6, 0, 0) == 0) {
3016 freetype->xsize = face->units_per_EM << 6;
3017 freetype->ysize = face->units_per_EM << 6;
3020 setFaceSize(xsize, ysize);
3022 if (freetype->matrix.xx != matrix.xx ||
3023 freetype->matrix.yy != matrix.yy ||
3024 freetype->matrix.xy != matrix.xy ||
3025 freetype->matrix.yx != matrix.yx) {
3026 freetype->matrix = matrix;
3027 FT_Set_Transform(face, &freetype->matrix,
nullptr);
3033void QFontEngineFT::setFaceSize(
int xsize,
int ysize)
const
3035 if (freetype->xsize != xsize || freetype->ysize != ysize) {
3036 FT_Set_Char_Size(freetype->face, xsize, ysize, 0, 0);
3037 freetype->xsize = xsize;
3038 freetype->ysize = ysize;
3042void QFontEngineFT::unlockFace()
const
3047FT_Face QFontEngineFT::non_locked_face()
const
3049 return freetype->face;
3053QFontEngineFT::QGlyphSet::QGlyphSet()
3054 : outline_drawing(
false)
3056 transformationMatrix.xx = 0x10000;
3057 transformationMatrix.yy = 0x10000;
3058 transformationMatrix.xy = 0;
3059 transformationMatrix.yx = 0;
3060 memset(fast_glyph_data, 0,
sizeof(fast_glyph_data));
3061 fast_glyph_count = 0;
3064QFontEngineFT::QGlyphSet::~QGlyphSet()
3069void QFontEngineFT::QGlyphSet::clear()
3071 if (fast_glyph_count > 0) {
3072 for (
int i = 0; i < 256; ++i) {
3073 if (fast_glyph_data[i]) {
3074 delete fast_glyph_data[i];
3075 fast_glyph_data[i] =
nullptr;
3078 fast_glyph_count = 0;
3080 qDeleteAll(glyph_data);
3084void QFontEngineFT::QGlyphSet::removeGlyphFromCache(glyph_t index,
3085 const QFixedPoint &subPixelPosition)
3087 if (useFastGlyphData(index, subPixelPosition)) {
3088 if (fast_glyph_data[index]) {
3089 delete fast_glyph_data[index];
3090 fast_glyph_data[index] =
nullptr;
3091 if (fast_glyph_count > 0)
3095 delete glyph_data.take(GlyphAndSubPixelPosition(index, subPixelPosition));
3099void QFontEngineFT::QGlyphSet::setGlyph(glyph_t index,
3100 const QFixedPoint &subPixelPosition,
3103 if (useFastGlyphData(index, subPixelPosition)) {
3104 if (!fast_glyph_data[index])
3106 fast_glyph_data[index] = glyph;
3108 glyph_data.insert(GlyphAndSubPixelPosition(index, subPixelPosition), glyph);
3112int QFontEngineFT::getPointInOutline(glyph_t glyph,
int flags, quint32 point, QFixed *xpos, QFixed *ypos, quint32 *nPoints)
3115 bool hsubpixel =
true;
3117 int load_flags = loadFlags(
nullptr, Format_A8, flags, hsubpixel, vfactor);
3118 int result = freetype->getPointInOutline(glyph, load_flags, point, xpos, ypos, nPoints);
3123bool QFontEngineFT::initFromFontEngine(
const QFontEngineFT *fe)
3125 if (!init(fe->faceId(), fe->antialias, fe->defaultFormat, fe->freetype))
3130 freetype->ref.ref();
3132 default_load_flags = fe->default_load_flags;
3133 default_hint_style = fe->default_hint_style;
3134 antialias = fe->antialias;
3135 transform = fe->transform;
3136 embolden = fe->embolden;
3137 obliquen = fe->obliquen;
3138 subpixelType = fe->subpixelType;
3139 lcdFilterType = fe->lcdFilterType;
3140 embeddedbitmap = fe->embeddedbitmap;
3141 verticalHintingOnly = fe->verticalHintingOnly;
3146QFontEngine *QFontEngineFT::cloneWithSize(qreal pixelSize)
const
3148 QFontDef fontDef(
this->fontDef);
3149 fontDef.pixelSize = pixelSize;
3150 QFontEngineFT *fe =
new QFontEngineFT(fontDef);
3151 if (!fe->initFromFontEngine(
this)) {
3159Qt::HANDLE QFontEngineFT::handle()
const
3161 return non_locked_face();
3164QList<QFontVariableAxis> QFontEngineFT::variableAxes()
const
3166 return freetype->variableAxes();
QHash< QFontEngine::FaceId, QFreetypeFace * > faces
QList< QFreetypeFace * > staleFaces
QHash< FaceStyle, int > faceIndices
Combined button and popup list for selecting options.
static bool hasTrueTypeInterpreterV40(FT_Library library)
static const QFontEngine::HintStyle ftInitialDefaultHintStyle
static QFontEngine::SubpixelAntialiasingType subpixelAntialiasingTypeHint()
static FT_Matrix QTransformToFTMatrix(const QTransform &matrix)
static void transformBoundingBox(int *left, int *top, int *right, int *bottom, FT_Matrix *matrix)
static void convertRGBToARGB(const uchar *src, uint *dst, int width, int height, int src_pitch, bool bgr)
QtFreetypeData * qt_getFreetypeData()
static bool calculateActualItalic(QFreetypeFace *freetypeFace, FT_Face face, QFontEngine::FaceId faceId)
static bool is2dRotation(const QTransform &t)
static QImage alphaMapFromGlyphData(QFontEngineFT::Glyph *glyph, QFontEngine::GlyphFormat glyphFormat)
static QFontEngineFT::Glyph emptyGlyph
QByteArray qt_fontdata_from_index(int)
size_t qHash(const QtFreetypeData::FaceStyle &style, size_t seed)
static bool ft_getSfntTable(void *user_data, uint tag, uchar *buffer, uint *length)
void qt_addBitmapToPath(qreal x0, qreal y0, const uchar *image_data, int bpl, int w, int h, QPainterPath *path)
static int computeFaceIndex(const QString &faceFileName, const QString &styleName)
bool operator==(const QtFreetypeData::FaceStyle &style1, const QtFreetypeData::FaceStyle &style2)
FT_Library qt_getFreetype()
static FT_UShort calculateActualWeight(QFreetypeFace *freetypeFace, FT_Face face, QFontEngine::FaceId faceId)
static void convertRGBToARGB_V(const uchar *src, uint *dst, int width, int height, int src_pitch, bool bgr)
static void scaleOutline(FT_Face face, FT_GlyphSlot g, FT_Fixed x_scale, FT_Fixed y_scale)
static const FT_Fixed obliqueSlant
static void dont_delete(void *)
static bool areMetricsTooLarge(const QFontEngineFT::GlyphInfo &info)
FaceStyle(QString faceFileName, QString styleName)