1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2013 Konstantin Ritt
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qharfbuzzng_p.h"
6
7#include <qstring.h>
8
9#include <private/qstringiterator_p.h>
10
11#include "qfontengine_p.h"
12
13QT_BEGIN_NAMESPACE
14
15// Unicode routines
16
17static const hb_script_t _qtscript_to_hbscript[] = {
18 HB_SCRIPT_UNKNOWN,
19 HB_SCRIPT_INHERITED,
20 HB_SCRIPT_COMMON,
21
22 HB_SCRIPT_LATIN,
23 HB_SCRIPT_GREEK,
24 HB_SCRIPT_CYRILLIC,
25 HB_SCRIPT_ARMENIAN,
26 HB_SCRIPT_HEBREW,
27 HB_SCRIPT_ARABIC,
28 HB_SCRIPT_SYRIAC,
29 HB_SCRIPT_THAANA,
30 HB_SCRIPT_DEVANAGARI,
31 HB_SCRIPT_BENGALI,
32 HB_SCRIPT_GURMUKHI,
33 HB_SCRIPT_GUJARATI,
34 HB_SCRIPT_ORIYA,
35 HB_SCRIPT_TAMIL,
36 HB_SCRIPT_TELUGU,
37 HB_SCRIPT_KANNADA,
38 HB_SCRIPT_MALAYALAM,
39 HB_SCRIPT_SINHALA,
40 HB_SCRIPT_THAI,
41 HB_SCRIPT_LAO,
42 HB_SCRIPT_TIBETAN,
43 HB_SCRIPT_MYANMAR,
44 HB_SCRIPT_GEORGIAN,
45 HB_SCRIPT_HANGUL,
46 HB_SCRIPT_ETHIOPIC,
47 HB_SCRIPT_CHEROKEE,
48 HB_SCRIPT_CANADIAN_SYLLABICS,
49 HB_SCRIPT_OGHAM,
50 HB_SCRIPT_RUNIC,
51 HB_SCRIPT_KHMER,
52 HB_SCRIPT_MONGOLIAN,
53 HB_SCRIPT_HIRAGANA,
54 HB_SCRIPT_KATAKANA,
55 HB_SCRIPT_BOPOMOFO,
56 HB_SCRIPT_HAN,
57 HB_SCRIPT_YI,
58 HB_SCRIPT_OLD_ITALIC,
59 HB_SCRIPT_GOTHIC,
60 HB_SCRIPT_DESERET,
61 HB_SCRIPT_TAGALOG,
62 HB_SCRIPT_HANUNOO,
63 HB_SCRIPT_BUHID,
64 HB_SCRIPT_TAGBANWA,
65 HB_SCRIPT_COPTIC,
66
67 // Unicode 4.0 additions
68 HB_SCRIPT_LIMBU,
69 HB_SCRIPT_TAI_LE,
70 HB_SCRIPT_LINEAR_B,
71 HB_SCRIPT_UGARITIC,
72 HB_SCRIPT_SHAVIAN,
73 HB_SCRIPT_OSMANYA,
74 HB_SCRIPT_CYPRIOT,
75 HB_SCRIPT_BRAILLE,
76
77 // Unicode 4.1 additions
78 HB_SCRIPT_BUGINESE,
79 HB_SCRIPT_NEW_TAI_LUE,
80 HB_SCRIPT_GLAGOLITIC,
81 HB_SCRIPT_TIFINAGH,
82 HB_SCRIPT_SYLOTI_NAGRI,
83 HB_SCRIPT_OLD_PERSIAN,
84 HB_SCRIPT_KHAROSHTHI,
85
86 // Unicode 5.0 additions
87 HB_SCRIPT_BALINESE,
88 HB_SCRIPT_CUNEIFORM,
89 HB_SCRIPT_PHOENICIAN,
90 HB_SCRIPT_PHAGS_PA,
91 HB_SCRIPT_NKO,
92
93 // Unicode 5.1 additions
94 HB_SCRIPT_SUNDANESE,
95 HB_SCRIPT_LEPCHA,
96 HB_SCRIPT_OL_CHIKI,
97 HB_SCRIPT_VAI,
98 HB_SCRIPT_SAURASHTRA,
99 HB_SCRIPT_KAYAH_LI,
100 HB_SCRIPT_REJANG,
101 HB_SCRIPT_LYCIAN,
102 HB_SCRIPT_CARIAN,
103 HB_SCRIPT_LYDIAN,
104 HB_SCRIPT_CHAM,
105
106 // Unicode 5.2 additions
107 HB_SCRIPT_TAI_THAM,
108 HB_SCRIPT_TAI_VIET,
109 HB_SCRIPT_AVESTAN,
110 HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
111 HB_SCRIPT_SAMARITAN,
112 HB_SCRIPT_LISU,
113 HB_SCRIPT_BAMUM,
114 HB_SCRIPT_JAVANESE,
115 HB_SCRIPT_MEETEI_MAYEK,
116 HB_SCRIPT_IMPERIAL_ARAMAIC,
117 HB_SCRIPT_OLD_SOUTH_ARABIAN,
118 HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
119 HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
120 HB_SCRIPT_OLD_TURKIC,
121 HB_SCRIPT_KAITHI,
122
123 // Unicode 6.0 additions
124 HB_SCRIPT_BATAK,
125 HB_SCRIPT_BRAHMI,
126 HB_SCRIPT_MANDAIC,
127
128 // Unicode 6.1 additions
129 HB_SCRIPT_CHAKMA,
130 HB_SCRIPT_MEROITIC_CURSIVE,
131 HB_SCRIPT_MEROITIC_HIEROGLYPHS,
132 HB_SCRIPT_MIAO,
133 HB_SCRIPT_SHARADA,
134 HB_SCRIPT_SORA_SOMPENG,
135 HB_SCRIPT_TAKRI,
136
137 // Unicode 7.0 additions
138 HB_SCRIPT_CAUCASIAN_ALBANIAN,
139 HB_SCRIPT_BASSA_VAH,
140 HB_SCRIPT_DUPLOYAN,
141 HB_SCRIPT_ELBASAN,
142 HB_SCRIPT_GRANTHA,
143 HB_SCRIPT_PAHAWH_HMONG,
144 HB_SCRIPT_KHOJKI,
145 HB_SCRIPT_LINEAR_A,
146 HB_SCRIPT_MAHAJANI,
147 HB_SCRIPT_MANICHAEAN,
148 HB_SCRIPT_MENDE_KIKAKUI,
149 HB_SCRIPT_MODI,
150 HB_SCRIPT_MRO,
151 HB_SCRIPT_OLD_NORTH_ARABIAN,
152 HB_SCRIPT_NABATAEAN,
153 HB_SCRIPT_PALMYRENE,
154 HB_SCRIPT_PAU_CIN_HAU,
155 HB_SCRIPT_OLD_PERMIC,
156 HB_SCRIPT_PSALTER_PAHLAVI,
157 HB_SCRIPT_SIDDHAM,
158 HB_SCRIPT_KHUDAWADI,
159 HB_SCRIPT_TIRHUTA,
160 HB_SCRIPT_WARANG_CITI,
161
162 // Unicode 8.0 additions
163 HB_SCRIPT_AHOM,
164 HB_SCRIPT_ANATOLIAN_HIEROGLYPHS,
165 HB_SCRIPT_HATRAN,
166 HB_SCRIPT_MULTANI,
167 HB_SCRIPT_OLD_HUNGARIAN,
168 HB_SCRIPT_SIGNWRITING,
169
170 // Unicode 9.0 additions
171 HB_SCRIPT_ADLAM,
172 HB_SCRIPT_BHAIKSUKI,
173 HB_SCRIPT_MARCHEN,
174 HB_SCRIPT_NEWA,
175 HB_SCRIPT_OSAGE,
176 HB_SCRIPT_TANGUT,
177
178 // Unicode 10.0 additions
179 HB_SCRIPT_MASARAM_GONDI,
180 HB_SCRIPT_NUSHU,
181 HB_SCRIPT_SOYOMBO,
182 HB_SCRIPT_ZANABAZAR_SQUARE,
183
184 // Unicode 11.0 additions
185 HB_SCRIPT_DOGRA,
186 HB_SCRIPT_GUNJALA_GONDI,
187 HB_SCRIPT_HANIFI_ROHINGYA,
188 HB_SCRIPT_MAKASAR,
189 HB_SCRIPT_MEDEFAIDRIN,
190 HB_SCRIPT_OLD_SOGDIAN,
191 HB_SCRIPT_SOGDIAN,
192
193 // Unicode 12.0 additions
194 HB_SCRIPT_ELYMAIC,
195 HB_SCRIPT_NANDINAGARI,
196 HB_SCRIPT_NYIAKENG_PUACHUE_HMONG,
197 HB_SCRIPT_WANCHO,
198
199 // Unicode 13.0 additions (not present in harfbuzz-ng 2.6.6 and earlier)
200#if !HB_VERSION_ATLEAST(2, 6, 7)
201 hb_script_t(HB_TAG('C', 'h', 'r', 's')), // Script_Chorasmian
202 hb_script_t(HB_TAG('D', 'i', 'a', 'k')), // Script_DivesAkuru
203 hb_script_t(HB_TAG('K', 'i', 't', 's')), // Script_KhitanSmallScript
204 hb_script_t(HB_TAG('Y', 'e', 'z', 'i')), // Script_Yezidi
205#else
206 HB_SCRIPT_CHORASMIAN,
207 HB_SCRIPT_DIVES_AKURU,
208 HB_SCRIPT_KHITAN_SMALL_SCRIPT,
209 HB_SCRIPT_YEZIDI,
210#endif
211 // Unicode 14.0 additions (not present in harfbuzz-ng 2.9.1 and earlier)
212#if !HB_VERSION_ATLEAST(3, 0, 0)
213 hb_script_t(HB_TAG('C','p','m','n')), // Script_CyproMinoan
214 hb_script_t(HB_TAG('O','u','g','r')), // Script_OldUyghur
215 hb_script_t(HB_TAG('T','n','s','a')), // Script_Tangsa
216 hb_script_t(HB_TAG('T','o','t','o')), // Script_Toto
217 hb_script_t(HB_TAG('V','i','t','h')), // Script_Vithkuqi
218#else
219 HB_SCRIPT_CYPRO_MINOAN,
220 HB_SCRIPT_OLD_UYGHUR,
221 HB_SCRIPT_TANGSA,
222 HB_SCRIPT_TOTO,
223 HB_SCRIPT_VITHKUQI,
224#endif
225 // Unicode 15.0 additions (not present in harfbuzz-ng 5.1.0 and earlier)
226#if !HB_VERSION_ATLEAST(5, 2, 0)
227 hb_script_t(HB_TAG('K','a','w','i')), // Script_Kawi
228 hb_script_t(HB_TAG('N','a','g','m')), // Script_NagMundari
229#else
230 HB_SCRIPT_KAWI,
231 HB_SCRIPT_NAG_MUNDARI,
232#endif
233};
234static_assert(QChar::ScriptCount == sizeof(_qtscript_to_hbscript) / sizeof(_qtscript_to_hbscript[0]));
235
236hb_script_t hb_qt_script_to_script(QChar::Script script)
237{
238 return _qtscript_to_hbscript[script];
239}
240
241QChar::Script hb_qt_script_from_script(hb_script_t script)
242{
243 uint i = QChar::ScriptCount - 1;
244 while (i > QChar::Script_Unknown && _qtscript_to_hbscript[i] != script)
245 --i;
246 return QChar::Script(i);
247}
248
249
250static hb_unicode_combining_class_t
251_hb_qt_unicode_combining_class(hb_unicode_funcs_t * /*ufuncs*/,
252 hb_codepoint_t unicode,
253 void * /*user_data*/)
254{
255 return hb_unicode_combining_class_t(QChar::combiningClass(ucs4: unicode));
256}
257
258static const hb_unicode_general_category_t _qtcategory_to_hbcategory[] = {
259 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK, // Mn
260 HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK, // Mc
261 HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK, // Me
262
263 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER, // Nd
264 HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER, // Nl
265 HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER, // No
266
267 HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR, // Zs
268 HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR, // Zl
269 HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR, // Zp
270
271 HB_UNICODE_GENERAL_CATEGORY_CONTROL, // Cc
272 HB_UNICODE_GENERAL_CATEGORY_FORMAT, // Cf
273 HB_UNICODE_GENERAL_CATEGORY_SURROGATE, // Cs
274 HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE, // Co
275 HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED, // Cn
276
277 HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER, // Lu
278 HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER, // Ll
279 HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER, // Lt
280 HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER, // Lm
281 HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER, // Lo
282
283 HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION, // Pc
284 HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION, // Pd
285 HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION, // Ps
286 HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION, // Pe
287 HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION, // Pi
288 HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION, // Pf
289 HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION, // Po
290
291 HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL, // Sm
292 HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL, // Sc
293 HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL, // Sk
294 HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL // So
295};
296
297static hb_unicode_general_category_t
298_hb_qt_unicode_general_category(hb_unicode_funcs_t * /*ufuncs*/,
299 hb_codepoint_t unicode,
300 void * /*user_data*/)
301{
302 return _qtcategory_to_hbcategory[QChar::category(ucs4: unicode)];
303}
304
305static hb_codepoint_t
306_hb_qt_unicode_mirroring(hb_unicode_funcs_t * /*ufuncs*/,
307 hb_codepoint_t unicode,
308 void * /*user_data*/)
309{
310 return QChar::mirroredChar(ucs4: unicode);
311}
312
313static hb_script_t
314_hb_qt_unicode_script(hb_unicode_funcs_t * /*ufuncs*/,
315 hb_codepoint_t unicode,
316 void * /*user_data*/)
317{
318 return _qtscript_to_hbscript[QChar::script(ucs4: unicode)];
319}
320
321static hb_bool_t
322_hb_qt_unicode_compose(hb_unicode_funcs_t * /*ufuncs*/,
323 hb_codepoint_t a, hb_codepoint_t b,
324 hb_codepoint_t *ab,
325 void * /*user_data*/)
326{
327 // ### optimize
328 QString s;
329 s.reserve(asize: 4);
330 s += QChar::fromUcs4(c: a);
331 s += QChar::fromUcs4(c: b);
332 QString normalized = s.normalized(mode: QString::NormalizationForm_C);
333
334 QStringIterator it(normalized);
335 Q_ASSERT(it.hasNext()); // size>0
336 *ab = it.next();
337
338 return !it.hasNext(); // size==1
339}
340
341static hb_bool_t
342_hb_qt_unicode_decompose(hb_unicode_funcs_t * /*ufuncs*/,
343 hb_codepoint_t ab,
344 hb_codepoint_t *a, hb_codepoint_t *b,
345 void * /*user_data*/)
346{
347 // ### optimize
348 if (QChar::decompositionTag(ucs4: ab) != QChar::Canonical) // !NFD
349 return false;
350
351 QString normalized = QChar::decomposition(ucs4: ab);
352 if (normalized.isEmpty())
353 return false;
354
355 QStringIterator it(normalized);
356 Q_ASSERT(it.hasNext()); // size>0
357 *a = it.next();
358
359 if (!it.hasNext()) { // size==1
360 *b = 0;
361 return *a != ab;
362 }
363
364 // size>1
365 *b = it.next();
366 if (!it.hasNext()) { // size==2
367 // Here's the ugly part: if ab decomposes to a single character and
368 // that character decomposes again, we have to detect that and undo
369 // the second part :-(
370 const QString recomposed = normalized.normalized(mode: QString::NormalizationForm_C);
371 QStringIterator jt(recomposed);
372 Q_ASSERT(jt.hasNext()); // size>0
373 const hb_codepoint_t c = jt.next();
374 if (c != *a && c != ab) {
375 *a = c;
376 *b = 0;
377 }
378 return true;
379 }
380
381 // size>2
382 // If decomposed to more than two characters, take the last one,
383 // and recompose the rest to get the first component
384 do {
385 *b = it.next();
386 } while (it.hasNext());
387 normalized.chop(n: QChar::requiresSurrogates(ucs4: *b) ? 2 : 1);
388 const QString recomposed = normalized.normalized(mode: QString::NormalizationForm_C);
389 QStringIterator jt(recomposed);
390 Q_ASSERT(jt.hasNext()); // size>0
391 // We expect that recomposed has exactly one character now
392 *a = jt.next();
393 return true;
394}
395
396
397struct _hb_unicode_funcs_t {
398 _hb_unicode_funcs_t()
399 {
400 funcs = hb_unicode_funcs_create(NULL);
401 hb_unicode_funcs_set_combining_class_func(ufuncs: funcs, func: _hb_qt_unicode_combining_class, NULL, NULL);
402 hb_unicode_funcs_set_general_category_func(ufuncs: funcs, func: _hb_qt_unicode_general_category, NULL, NULL);
403 hb_unicode_funcs_set_mirroring_func(ufuncs: funcs, func: _hb_qt_unicode_mirroring, NULL, NULL);
404 hb_unicode_funcs_set_script_func(ufuncs: funcs, func: _hb_qt_unicode_script, NULL, NULL);
405 hb_unicode_funcs_set_compose_func(ufuncs: funcs, func: _hb_qt_unicode_compose, NULL, NULL);
406 hb_unicode_funcs_set_decompose_func(ufuncs: funcs, func: _hb_qt_unicode_decompose, NULL, NULL);
407 }
408 ~_hb_unicode_funcs_t()
409 {
410 hb_unicode_funcs_destroy(ufuncs: funcs);
411 }
412
413 hb_unicode_funcs_t *funcs;
414};
415
416Q_GLOBAL_STATIC(_hb_unicode_funcs_t, qt_ufuncs)
417
418hb_unicode_funcs_t *hb_qt_get_unicode_funcs()
419{
420 return qt_ufuncs()->funcs;
421}
422
423
424// Font routines
425
426static hb_bool_t
427_hb_qt_get_font_h_extents(hb_font_t * /*font*/, void *font_data,
428 hb_font_extents_t *metrics,
429 void * /*user_data*/)
430{
431 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
432 Q_ASSERT(fe);
433
434 metrics->ascender = fe->ascent().value();
435 metrics->descender = fe->descent().value();
436 metrics->line_gap = fe->leading().value();
437
438 return true;
439}
440
441static hb_bool_t
442_hb_qt_font_get_nominal_glyph(hb_font_t * /*font*/, void *font_data,
443 hb_codepoint_t unicode,
444 hb_codepoint_t *glyph,
445 void * /*user_data*/)
446{
447 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
448 Q_ASSERT(fe);
449
450 *glyph = fe->glyphIndex(ucs4: unicode);
451
452 return *glyph != 0;
453}
454
455static hb_bool_t
456_hb_qt_font_get_variation_glyph(hb_font_t * /*font*/, void *font_data,
457 hb_codepoint_t unicode, hb_codepoint_t /*variation_selector*/,
458 hb_codepoint_t *glyph,
459 void * /*user_data*/)
460{
461 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
462 Q_ASSERT(fe);
463
464 // ### TODO add support for variation selectors
465 *glyph = fe->glyphIndex(ucs4: unicode);
466
467 return *glyph != 0;
468}
469
470static hb_position_t
471_hb_qt_font_get_glyph_h_advance(hb_font_t *font, void *font_data,
472 hb_codepoint_t glyph,
473 void * /*user_data*/)
474{
475 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
476 Q_ASSERT(fe);
477
478 QFixed advance;
479
480 QGlyphLayout g;
481 g.numGlyphs = 1;
482 g.glyphs = &glyph;
483 g.advances = &advance;
484
485 fe->recalcAdvances(&g, QFontEngine::ShaperFlags(hb_qt_font_get_use_design_metrics(font)));
486
487 return advance.value();
488}
489
490static hb_position_t
491_hb_qt_font_get_glyph_h_kerning(hb_font_t *font, void *font_data,
492 hb_codepoint_t first_glyph, hb_codepoint_t second_glyph,
493 void * /*user_data*/)
494{
495 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
496 Q_ASSERT(fe);
497
498 glyph_t glyphs[2] = { first_glyph, second_glyph };
499 QFixed advance;
500
501 QGlyphLayout g;
502 g.numGlyphs = 2;
503 g.glyphs = glyphs;
504 g.advances = &advance;
505
506 fe->doKerning(&g, QFontEngine::ShaperFlags(hb_qt_font_get_use_design_metrics(font)));
507
508 return advance.value();
509}
510
511static hb_bool_t
512_hb_qt_font_get_glyph_extents(hb_font_t * /*font*/, void *font_data,
513 hb_codepoint_t glyph,
514 hb_glyph_extents_t *extents,
515 void * /*user_data*/)
516{
517 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
518 Q_ASSERT(fe);
519
520 glyph_metrics_t gm = fe->boundingBox(glyph);
521
522 extents->x_bearing = gm.x.value();
523 extents->y_bearing = gm.y.value();
524 extents->width = gm.width.value();
525 extents->height = gm.height.value();
526
527 return true;
528}
529
530static hb_bool_t
531_hb_qt_font_get_glyph_contour_point(hb_font_t * /*font*/, void *font_data,
532 hb_codepoint_t glyph,
533 unsigned int point_index, hb_position_t *x, hb_position_t *y,
534 void * /*user_data*/)
535{
536 QFontEngine *fe = static_cast<QFontEngine *>(font_data);
537 Q_ASSERT(fe);
538
539 QFixed xpos, ypos;
540 quint32 numPoints = 1;
541 if (Q_LIKELY(fe->getPointInOutline(glyph, 0, point_index, &xpos, &ypos, &numPoints) == 0)) {
542 *x = xpos.value();
543 *y = ypos.value();
544 return true;
545 }
546
547 *x = *y = 0;
548 return false;
549}
550
551
552static hb_user_data_key_t _useDesignMetricsKey;
553
554void hb_qt_font_set_use_design_metrics(hb_font_t *font, uint value)
555{
556 hb_font_set_user_data(font, key: &_useDesignMetricsKey, data: (void *)quintptr(value), NULL, replace: true);
557}
558
559uint hb_qt_font_get_use_design_metrics(hb_font_t *font)
560{
561 return quintptr(hb_font_get_user_data(font, key: &_useDesignMetricsKey));
562}
563
564
565struct _hb_qt_font_funcs_t {
566 _hb_qt_font_funcs_t()
567 {
568 funcs = hb_font_funcs_create();
569
570 hb_font_funcs_set_font_h_extents_func(ffuncs: funcs, func: _hb_qt_get_font_h_extents, NULL, NULL);
571 hb_font_funcs_set_nominal_glyph_func(ffuncs: funcs, func: _hb_qt_font_get_nominal_glyph, NULL, NULL);
572 hb_font_funcs_set_variation_glyph_func(ffuncs: funcs, func: _hb_qt_font_get_variation_glyph, NULL, NULL);
573 hb_font_funcs_set_glyph_h_advance_func(ffuncs: funcs, func: _hb_qt_font_get_glyph_h_advance, NULL, NULL);
574 hb_font_funcs_set_glyph_h_kerning_func(ffuncs: funcs, func: _hb_qt_font_get_glyph_h_kerning, NULL, NULL);
575 hb_font_funcs_set_glyph_extents_func(ffuncs: funcs, func: _hb_qt_font_get_glyph_extents, NULL, NULL);
576 hb_font_funcs_set_glyph_contour_point_func(ffuncs: funcs, func: _hb_qt_font_get_glyph_contour_point, NULL, NULL);
577
578 hb_font_funcs_make_immutable(ffuncs: funcs);
579 }
580 ~_hb_qt_font_funcs_t()
581 {
582 hb_font_funcs_destroy(ffuncs: funcs);
583 }
584
585 hb_font_funcs_t *funcs;
586};
587
588Q_GLOBAL_STATIC(_hb_qt_font_funcs_t, qt_ffuncs)
589
590
591static hb_blob_t *
592_hb_qt_reference_table(hb_face_t * /*face*/, hb_tag_t tag, void *user_data)
593{
594 QFontEngine::FaceData *data = static_cast<QFontEngine::FaceData *>(user_data);
595 Q_ASSERT(data);
596
597 qt_get_font_table_func_t get_font_table = data->get_font_table;
598 Q_ASSERT(get_font_table);
599
600 uint length = 0;
601 if (Q_UNLIKELY(!get_font_table(data->user_data, tag, 0, &length)))
602 return hb_blob_get_empty();
603
604 char *buffer = static_cast<char *>(malloc(size: length));
605 if (q_check_ptr(p: buffer) == nullptr)
606 return nullptr;
607
608 if (Q_UNLIKELY(!get_font_table(data->user_data, tag, reinterpret_cast<uchar *>(buffer), &length)))
609 return nullptr;
610
611 return hb_blob_create(data: const_cast<const char *>(buffer), length,
612 mode: HB_MEMORY_MODE_WRITABLE,
613 user_data: buffer, destroy: free);
614}
615
616static inline hb_face_t *
617_hb_qt_face_create(QFontEngine *fe)
618{
619 QFontEngine::FaceData *data = static_cast<QFontEngine::FaceData *>(malloc(size: sizeof(QFontEngine::FaceData)));
620 Q_CHECK_PTR(data);
621 data->user_data = fe->faceData.user_data;
622 data->get_font_table = fe->faceData.get_font_table;
623
624 hb_face_t *face = hb_face_create_for_tables(reference_table_func: _hb_qt_reference_table, user_data: (void *)data, destroy: free);
625
626 hb_face_set_index(face, index: fe->faceId().index);
627 hb_face_set_upem(face, upem: fe->emSquareSize().truncate());
628
629 return face;
630}
631
632static void
633_hb_qt_face_release(void *user_data)
634{
635 hb_face_destroy(face: static_cast<hb_face_t *>(user_data));
636}
637
638hb_face_t *hb_qt_face_get_for_engine(QFontEngine *fe)
639{
640 Q_ASSERT(fe && fe->type() != QFontEngine::Multi);
641
642 if (Q_UNLIKELY(!fe->face_))
643 fe->face_ = QFontEngine::Holder(_hb_qt_face_create(fe), _hb_qt_face_release);
644
645 return static_cast<hb_face_t *>(fe->face_.get());
646}
647
648
649static inline hb_font_t *
650_hb_qt_font_create(QFontEngine *fe)
651{
652 hb_face_t *face = hb_qt_face_get_for_engine(fe);
653
654 hb_font_t *font = hb_font_create(face);
655
656 const qreal y_ppem = fe->fontDef.pixelSize;
657 const qreal x_ppem = (fe->fontDef.pixelSize * fe->fontDef.stretch) / 100.0;
658
659 hb_font_set_funcs(font, klass: qt_ffuncs()->funcs, font_data: fe, destroy: nullptr);
660 hb_font_set_scale(font, x_scale: QFixed::fromReal(r: x_ppem).value(), y_scale: -QFixed::fromReal(r: y_ppem).value());
661 hb_font_set_ppem(font, x_ppem: int(x_ppem), y_ppem: int(y_ppem));
662
663 hb_font_set_ptem(font, ptem: fe->fontDef.pointSize);
664
665 return font;
666}
667
668static void
669_hb_qt_font_release(void *user_data)
670{
671 hb_font_destroy(font: static_cast<hb_font_t *>(user_data));
672}
673
674hb_font_t *hb_qt_font_get_for_engine(QFontEngine *fe)
675{
676 Q_ASSERT(fe && fe->type() != QFontEngine::Multi);
677
678 if (Q_UNLIKELY(!fe->font_))
679 fe->font_ = QFontEngine::Holder(_hb_qt_font_create(fe), _hb_qt_font_release);
680
681 return static_cast<hb_font_t *>(fe->font_.get());
682}
683
684QT_END_NAMESPACE
685

source code of qtbase/src/gui/text/qharfbuzzng.cpp