// SPDX-License-Identifier: GPL-3.0-or-later #include "app/gdifont.h" #include #include #include namespace xpl { namespace { quint16 u16(const QByteArray &table, int at) { return at + 2 <= table.size() ? qFromBigEndian(table.constData() + at) : 0; } } // namespace GdiMetrics gdiMetrics(const QFont &font) { const QRawFont raw = QRawFont::fromFont(font); const QByteArray head = raw.fontTable("head"), os2 = raw.fontTable("OS/2"); const int upem = u16(head, 18); const int px = font.pixelSize() > 0 ? font.pixelSize() : int(std::lround(raw.pixelSize())); GdiMetrics m; // A font's own pixel heights at this size win, as GDI's do: VDMX (its // yMax / -yMin at the ppem, for a 1:1 screen). Franklin Gothic Medium at // 19 px is 18 + 6 there, GDI's tmAscent / tmDescent measured on XP, where // its OS/2 winAscent / winDescent scale to 17 + 4. const QByteArray vdmx = raw.fontTable("VDMX"); if (const int ratios = u16(vdmx, 4); vdmx.size() >= 6 && px > 0) { for (int i = 0; i < ratios; ++i) { const int at = 6 + i * 4; if (at + 4 > vdmx.size()) break; const int x = quint8(vdmx[at + 1]), from = quint8(vdmx[at + 2]), to = quint8(vdmx[at + 3]); if (!(x == 0 && from == 0 && to == 0) && !(from <= x && x <= to)) continue; // (another aspect ratio's) const int group = u16(vdmx, 6 + ratios * 4 + i * 2); const int records = u16(vdmx, group); for (int r = 0; r < records; ++r) { const int e = group + 4 + r * 6; if (u16(vdmx, e) == px) { m.ascent = qint16(u16(vdmx, e + 2)); m.descent = -qint16(u16(vdmx, e + 4)); return m; } } break; // (the ratio's group hasn't this size: as no table) } } if (upem > 0 && os2.size() >= 78) { m.ascent = int(std::lround(double(u16(os2, 74)) * px / upem)); m.descent = int(std::lround(double(u16(os2, 76)) * px / upem)); } else { m.ascent = int(std::lround(raw.ascent())); m.descent = int(std::lround(raw.descent())); } return m; } int gdiTextWidth(const QFont &font, const QString &text) { const QRawFont raw = QRawFont::fromFont(font); const QList glyphs = raw.glyphIndexesForString(text); const int px = font.pixelSize() > 0 ? font.pixelSize() : int(std::lround(raw.pixelSize())); // hdmx: version, numRecords, sizeDeviceRecord; then per size // {pixelSize, maxWidth, widths[numGlyphs]}. const QByteArray hdmx = raw.fontTable("hdmx"); if (hdmx.size() >= 8) { const int records = qint16(u16(hdmx, 2)); const int recordSize = int(qFromBigEndian(hdmx.constData() + 4)); for (int i = 0; i < records && recordSize > 2; ++i) { const int at = 8 + i * recordSize; if (at + recordSize > hdmx.size() || quint8(hdmx[at]) != px) continue; int width = 0; for (const quint32 g : glyphs) width += int(g) + 2 < recordSize ? quint8(hdmx[at + 2 + int(g)]) : 0; return width; } } const QByteArray head = raw.fontTable("head"), hhea = raw.fontTable("hhea"), hmtx = raw.fontTable("hmtx"); const int upem = u16(head, 18), metrics = u16(hhea, 34); // (A glyph with any advance is at least a pixel: GDI's Tahoma at 1 and // 2px gives "start" 1px a letter, 5 in all -- measured on XP; rounded, // most of its letters came to nothing.) int width = 0; for (const quint32 g : glyphs) { const int index = int(g) < metrics ? int(g) : metrics - 1; const int advance = u16(hmtx, index * 4); width += upem > 0 && advance > 0 ? qMax(1, int(std::lround(double(advance) * px / upem))) : 0; } return width; } bool gdiSmooths(const QFont &font, qreal scale) { const QRawFont raw = QRawFont::fromFont(font); const QByteArray gasp = raw.fontTable("gasp"); const int px = int(std::lround((font.pixelSize() > 0 ? font.pixelSize() : raw.pixelSize()) * scale)); // gasp: version, numRanges, then {rangeMaxPPEM, rangeGaspBehavior}; // GASP_DOGRAY is 0x0002. const int ranges = u16(gasp, 2); for (int i = 0; i < ranges && 4 + (i + 1) * 4 <= gasp.size(); ++i) if (px <= u16(gasp, 4 + i * 4)) return u16(gasp, 6 + i * 4) & 0x0002; return gasp.isEmpty(); // (no table: smoothed) } QFont gdiFont(QFont font, qreal scale) { font.setHintingPreference(QFont::PreferFullHinting); if (!gdiSmooths(font, scale)) font.setStyleStrategy(QFont::NoAntialias); return font; } } // namespace xpl