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

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