| 1 |
#include <QTransform> |
| 2 |
#include <QDebug> |
| 3 |
|
| 4 |
#include <hb.h> |
| 5 |
|
| 6 |
#include "harfbuzzfont.h" |
| 7 |
|
| 8 |
struct Outliner |
| 9 |
{ |
| 10 |
static void moveToFn(hb_position_t to_x, hb_position_t to_y, Outliner &outliner) |
| 11 |
{ |
| 12 |
outliner.path.moveTo(to_x, to_y); |
| 13 |
} |
| 14 |
|
| 15 |
static void lineToFn(hb_position_t to_x, hb_position_t to_y, Outliner &outliner) |
| 16 |
{ |
| 17 |
outliner.path.lineTo(to_x, to_y); |
| 18 |
} |
| 19 |
|
| 20 |
static void quadToFn(hb_position_t control_x, hb_position_t control_y, |
| 21 |
hb_position_t to_x, hb_position_t to_y, |
| 22 |
Outliner &outliner) |
| 23 |
{ |
| 24 |
outliner.path.quadTo(control_x, control_y, to_x, to_y); |
| 25 |
} |
| 26 |
|
| 27 |
static void cubicToFn(hb_position_t control1_x, hb_position_t control1_y, |
| 28 |
hb_position_t control2_x, hb_position_t control2_y, |
| 29 |
hb_position_t to_x, hb_position_t to_y, |
| 30 |
Outliner &outliner) |
| 31 |
{ |
| 32 |
outliner.path.cubicTo(control1_x, control1_y, control2_x, control2_y, to_x, to_y); |
| 33 |
} |
| 34 |
|
| 35 |
static void closePathFn(Outliner &outliner) |
| 36 |
{ |
| 37 |
outliner.path.closeSubpath(); |
| 38 |
} |
| 39 |
|
| 40 |
QPainterPath path; |
| 41 |
}; |
| 42 |
|
| 43 |
HarfBuzzFont::HarfBuzzFont() |
| 44 |
{ |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
HarfBuzzFont::~HarfBuzzFont() |
| 49 |
{ |
| 50 |
reset(); |
| 51 |
} |
| 52 |
|
| 53 |
void HarfBuzzFont::open(const QString &path, const quint32 index) |
| 54 |
{ |
| 55 |
if (isOpen()) { |
| 56 |
reset(); |
| 57 |
} |
| 58 |
|
| 59 |
const auto utf8Path = path.toUtf8(); |
| 60 |
hb_blob_t *blob = hb_blob_create_from_file(utf8Path.constData()); |
| 61 |
if (!blob) { |
| 62 |
throw tr("Failed to open a font."); |
| 63 |
} |
| 64 |
|
| 65 |
hb_face_t *face = hb_face_create(blob, index); |
| 66 |
if (!face) { |
| 67 |
throw tr("Failed to open a font."); |
| 68 |
} |
| 69 |
|
| 70 |
hb_font_t *font = hb_font_create(face); |
| 71 |
if (!font) { |
| 72 |
throw tr("Failed to open a font."); |
| 73 |
} |
| 74 |
|
| 75 |
m_blob = blob; |
| 76 |
m_face = face; |
| 77 |
m_font = font; |
| 78 |
} |
| 79 |
|
| 80 |
bool HarfBuzzFont::isOpen() const |
| 81 |
{ |
| 82 |
return m_font != nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
Glyph HarfBuzzFont::outline(const quint16 gid) const |
| 86 |
{ |
| 87 |
if (!isOpen()) { |
| 88 |
throw tr("Font is not loaded."); |
| 89 |
} |
| 90 |
|
| 91 |
Outliner outliner; |
| 92 |
|
| 93 |
hb_draw_funcs_t *funcs = hb_draw_funcs_create(); |
| 94 |
hb_draw_funcs_set_move_to_func(funcs, (hb_draw_move_to_func_t)outliner.moveToFn); |
| 95 |
hb_draw_funcs_set_line_to_func(funcs, (hb_draw_line_to_func_t)outliner.lineToFn); |
| 96 |
hb_draw_funcs_set_quadratic_to_func(funcs, (hb_draw_quadratic_to_func_t)outliner.quadToFn); |
| 97 |
hb_draw_funcs_set_cubic_to_func(funcs, (hb_draw_cubic_to_func_t)outliner.cubicToFn); |
| 98 |
hb_draw_funcs_set_close_path_func(funcs, (hb_draw_close_path_func_t)outliner.closePathFn); |
| 99 |
|
| 100 |
if (!hb_font_draw_glyph(m_font, gid, funcs, &outliner)) { |
| 101 |
throw tr("Failed to outline a glyph %1.").arg(gid); |
| 102 |
} |
| 103 |
|
| 104 |
hb_draw_funcs_destroy(funcs); |
| 105 |
|
| 106 |
hb_glyph_extents_t extents = {0, 0, 0, 0}; |
| 107 |
if (!hb_font_get_glyph_extents(m_font, gid, &extents)) { |
| 108 |
throw tr("Failed to query glyph extents."); |
| 109 |
} |
| 110 |
|
| 111 |
const QRect bbox( |
| 112 |
extents.x_bearing, |
| 113 |
-extents.y_bearing, |
| 114 |
extents.width, |
| 115 |
-extents.height |
| 116 |
); |
| 117 |
|
| 118 |
// Flip outline around x-axis. |
| 119 |
QTransform ts(1, 0, 0, -1, 0, 0); |
| 120 |
outliner.path = ts.map(outliner.path); |
| 121 |
|
| 122 |
outliner.path.setFillRule(Qt::WindingFill); |
| 123 |
|
| 124 |
return Glyph { |
| 125 |
outliner.path, |
| 126 |
bbox, |
| 127 |
}; |
| 128 |
} |
| 129 |
|
| 130 |
void HarfBuzzFont::setVariations(const QVector<Variation> &variations) |
| 131 |
{ |
| 132 |
if (!isOpen()) { |
| 133 |
throw tr("Font is not loaded."); |
| 134 |
} |
| 135 |
|
| 136 |
QVector<hb_variation_t> hbVariations; |
| 137 |
for (const auto &var : variations) { |
| 138 |
hbVariations.append({ var.tag.value, (float)var.value }); |
| 139 |
} |
| 140 |
|
| 141 |
hb_font_set_variations(m_font, hbVariations.constData(), hbVariations.size()); |
| 142 |
} |
| 143 |
|
| 144 |
void HarfBuzzFont::reset() |
| 145 |
{ |
| 146 |
if (m_blob) { |
| 147 |
hb_blob_destroy(m_blob); |
| 148 |
m_blob = nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
if (m_font) { |
| 152 |
hb_font_destroy(m_font); |
| 153 |
m_font = nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
if (m_face) { |
| 157 |
hb_face_destroy(m_face); |
| 158 |
m_face = nullptr; |
| 159 |
} |
| 160 |
} |