Debian Codegraph @

texk/web2c/xetexdir/XeTeXLayoutInterface.cpp

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009-2012 by Jonathan Kew
5 Copyright (c) 2012-2019 by Khaled Hosny
6
7 SIL Author(s): Jonathan Kew
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
24 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the name of the copyright holders
29 shall not be used in advertising or otherwise to promote the sale,
30 use or other dealings in this Software without prior written
31 authorization from the copyright holders.
32 \****************************************************************************/
33
34 #include <w2c/config.h>
35
36 #include <unicode/platform.h> // We need this first
37 #include <unicode/ubidi.h>
38 #include <unicode/utext.h>
39
40 #include <graphite2/Font.h>
41 #include <graphite2/Segment.h>
42 #include <hb.h>
43 #include <hb-graphite2.h>
44 #if !HB_VERSION_ATLEAST(2,5,0)
45 #include <hb-icu.h>
46 #endif
47 #include <hb-ot.h>
48
49 #include "XeTeX_web.h"
50
51 #include "XeTeXLayoutInterface.h"
52 #include "XeTeXFontInst.h"
53 #ifdef XETEX_MAC
54 #include "XeTeXFontInst_Mac.h"
55 #endif
56 #include "XeTeXFontMgr.h"
57
58 struct XeTeXLayoutEngine_rec
59 {
60 XeTeXFontInst* font;
61 PlatformFontRef fontRef;
62 hb_tag_t script;
63 hb_language_t language;
64 hb_feature_t* features;
65 char** ShaperList; // the requested shapers
66 char* shaper; // the actually used shaper
67 int nFeatures;
68 uint32_t rgbValue;
69 float extend;
70 float slant;
71 float embolden;
72 hb_buffer_t* hbBuffer;
73 };
74
75 /*******************************************************************/
76 /* Glyph bounding box cache to speed up \XeTeXuseglyphmetrics mode */
77 /*******************************************************************/
78 #include <map>
79
80 // key is combined value representing (font_id << 16) + glyph
81 // value is glyph bounding box in TeX points
82 static std::map<uint32_t,GlyphBBox> sGlyphBoxes;
83
84 int
85 getCachedGlyphBBox(uint16_t fontID, uint16_t glyphID, GlyphBBox* bbox)
86 {
87 uint32_t key = ((uint32_t)fontID << 16) + glyphID;
88 std::map<uint32_t,GlyphBBox>::const_iterator i = sGlyphBoxes.find(key);
89 if (i == sGlyphBoxes.end()) {
90 return 0;
91 }
92 *bbox = i->second;
93 return 1;
94 }
95
96 void
97 cacheGlyphBBox(uint16_t fontID, uint16_t glyphID, const GlyphBBox* bbox)
98 {
99 uint32_t key = ((uint32_t)fontID << 16) + glyphID;
100 sGlyphBoxes[key] = *bbox;
101 }
102 /*******************************************************************/
103
104 void
105 terminatefontmanager()
106 {
107 XeTeXFontMgr::Terminate();
108 }
109
110 XeTeXFont
111 createFont(PlatformFontRef fontRef, Fixed pointSize)
112 {
113 int status = 0;
114 #ifdef XETEX_MAC
115 XeTeXFontInst* font = new XeTeXFontInst_Mac(fontRef, Fix2D(pointSize), status);
116 #else
117 FcChar8* pathname = 0;
118 FcPatternGetString(fontRef, FC_FILE, 0, &pathname);
119 int index;
120 FcPatternGetInteger(fontRef, FC_INDEX, 0, &index);
121 XeTeXFontInst* font = new XeTeXFontInst((const char*)pathname, index, Fix2D(pointSize), status);
122 #endif
123 if (status != 0) {
124 delete font;
125 return NULL;
126 }
127 return (XeTeXFont)font;
128 }
129
130 XeTeXFont
131 createFontFromFile(const char* filename, int index, Fixed pointSize)
132 {
133 int status = 0;
134 XeTeXFontInst* font = new XeTeXFontInst(filename, index, Fix2D(pointSize), status);
135 if (status != 0) {
136 delete font;
137 return NULL;
138 }
139 return (XeTeXFont)font;
140 }
141
142 void
143 setFontLayoutDir(XeTeXFont font, int vertical)
144 {
145 ((XeTeXFontInst*)font)->setLayoutDirVertical(vertical != 0);
146 }
147
148 PlatformFontRef
149 findFontByName(const char* name, char* var, double size)
150 {
151 return XeTeXFontMgr::GetFontManager()->findFont(name, var, size);
152 }
153
154 char
155 getReqEngine()
156 {
157 return XeTeXFontMgr::GetFontManager()->getReqEngine();
158 }
159
160 void
161 setReqEngine(char reqEngine)
162 {
163 XeTeXFontMgr::GetFontManager()->setReqEngine(reqEngine);
164 }
165
166 const char*
167 getFullName(PlatformFontRef fontRef)
168 {
169 return XeTeXFontMgr::GetFontManager()->getFullName(fontRef);
170 }
171
172 double
173 getDesignSize(XeTeXFont font)
174 {
175 return XeTeXFontMgr::GetFontManager()->getDesignSize(font);
176 }
177
178 char*
179 getFontFilename(XeTeXLayoutEngine engine, uint32_t* index)
180 {
181 return xstrdup(engine->font->getFilename(index));
182 }
183
184 PlatformFontRef
185 getFontRef(XeTeXLayoutEngine engine)
186 {
187 return engine->fontRef;
188 }
189
190 void
191 deleteFont(XeTeXFont font)
192 {
193 delete (XeTeXFontInst*)font;
194 }
195
196 void*
197 getFontTablePtr(XeTeXFont font, uint32_t tableTag)
198 {
199 return const_cast<void*>(((XeTeXFontInst*)font)->getFontTable(tableTag));
200 }
201
202 Fixed
203 getSlant(XeTeXFont font)
204 {
205 float italAngle = ((XeTeXFontInst*)font)->getItalicAngle();
206 return D2Fix(tan(-italAngle * M_PI / 180.0));
207 }
208
209 static unsigned int
210 getLargerScriptListTable(XeTeXFont font, hb_tag_t** scriptList)
211 {
212 unsigned int rval = 0;
213
214 hb_face_t* face = hb_font_get_face(((XeTeXFontInst*)font)->getHbFont());
215
216 hb_tag_t* scriptListSub = NULL;
217 hb_tag_t* scriptListPos = NULL;
218
219 unsigned int scriptCountSub = hb_ot_layout_table_get_script_tags(face, HB_OT_TAG_GSUB, 0, NULL, NULL);
220 scriptListSub = (hb_tag_t*) xcalloc(scriptCountSub, sizeof(hb_tag_t*));
221 hb_ot_layout_table_get_script_tags(face, HB_OT_TAG_GSUB, 0, &scriptCountSub, scriptListSub);
222
223 unsigned int scriptCountPos = hb_ot_layout_table_get_script_tags(face, HB_OT_TAG_GPOS, 0, NULL, NULL);
224 scriptListPos = (hb_tag_t*) xcalloc(scriptCountPos, sizeof(hb_tag_t*));
225 hb_ot_layout_table_get_script_tags(face, HB_OT_TAG_GSUB, 0, &scriptCountPos, scriptListPos);
226
227 if (scriptCountSub > scriptCountPos) {
228 if (scriptList != NULL)
229 *scriptList = scriptListSub;
230 rval = scriptCountSub;
231 } else {
232 if (scriptList != NULL)
233 *scriptList = scriptListPos;
234 rval = scriptCountPos;
235 }
236
237 return rval;
238 }
239
240 unsigned int
241 countScripts(XeTeXFont font)
242 {
243 return getLargerScriptListTable(font, NULL);
244 }
245
246 hb_tag_t
247 getIndScript(XeTeXFont font, unsigned int index)
248 {
249 hb_tag_t rval = 0;
250
251 hb_tag_t* scriptList;
252
253 unsigned int scriptCount = getLargerScriptListTable(font, &scriptList);
254 if (scriptList != NULL) {
255 if (index < scriptCount)
256 rval = scriptList[index];
257 }
258
259 return rval;
260 }
261
262 unsigned int
263 countLanguages(XeTeXFont font, hb_tag_t script)
264 {
265 unsigned int rval = 0;
266
267 hb_face_t* face = hb_font_get_face(((XeTeXFontInst*)font)->getHbFont());
268 hb_tag_t* scriptList;
269
270 unsigned int scriptCount = getLargerScriptListTable(font, &scriptList);
271 if (scriptList != NULL) {
272 for (int i = 0; i < scriptCount; i++) {
273 if (scriptList[i] == script) {
274 rval += hb_ot_layout_script_get_language_tags (face, HB_OT_TAG_GSUB, i, 0, NULL, NULL);
275 rval += hb_ot_layout_script_get_language_tags (face, HB_OT_TAG_GPOS, i, 0, NULL, NULL);
276 break;
277 }
278 }
279 }
280
281 return rval;
282 }
283
284 hb_tag_t
285 getIndLanguage(XeTeXFont font, hb_tag_t script, unsigned int index)
286 {
287 hb_tag_t rval = 0;
288
289 hb_face_t* face = hb_font_get_face(((XeTeXFontInst*)font)->getHbFont());
290 hb_tag_t* scriptList;
291
292 unsigned int scriptCount = getLargerScriptListTable(font, &scriptList);
293 if (scriptList != NULL) {
294 for (int i = 0; i < scriptCount; i++) {
295 if (scriptList[i] == script) {
296 unsigned int langCount;
297 hb_tag_t* langList;
298
299 langCount = hb_ot_layout_script_get_language_tags(face, HB_OT_TAG_GSUB, i, 0, NULL, NULL);
300 langList = (hb_tag_t*) xcalloc(langCount, sizeof(hb_tag_t*));
301 hb_ot_layout_script_get_language_tags(face, HB_OT_TAG_GSUB, i, 0, &langCount, langList);
302
303 if (index < langCount) {
304 rval = langList[index];
305 break;
306 }
307
308 free(langList);
309
310 langCount = hb_ot_layout_script_get_language_tags(face, HB_OT_TAG_GPOS, i, 0, NULL, NULL);
311 langList = (hb_tag_t*) xcalloc(langCount, sizeof(hb_tag_t*));
312 hb_ot_layout_script_get_language_tags(face, HB_OT_TAG_GPOS, i, 0, &langCount, langList);
313
314 if (index < langCount) {
315 rval = langList[index];
316 break;
317 }
318
319 free(langList);
320 }
321 }
322 }
323
324 return rval;
325 }
326
327 unsigned int
328 countFeatures(XeTeXFont font, hb_tag_t script, hb_tag_t language)
329 {
330 unsigned int rval = 0;
331
332 hb_face_t* face = hb_font_get_face(((XeTeXFontInst*)font)->getHbFont());
333
334 for (int i = 0; i < 2; ++i) {
335 unsigned int scriptIndex, langIndex = 0;
336 hb_tag_t tableTag = i == 0 ? HB_OT_TAG_GSUB : HB_OT_TAG_GPOS;
337 if (hb_ot_layout_table_find_script(face, tableTag, script, &scriptIndex)) {
338 if (hb_ot_layout_script_find_language(face, tableTag, scriptIndex, language, &langIndex) || language == 0) {
339 rval += hb_ot_layout_language_get_feature_tags(face, tableTag, scriptIndex, langIndex, 0, NULL, NULL);
340 }
341 }
342 }
343
344 return rval;
345 }
346
347 hb_tag_t
348 getIndFeature(XeTeXFont font, hb_tag_t script, hb_tag_t language, unsigned int index)
349 {
350 hb_tag_t rval = 0;
351
352 hb_face_t* face = hb_font_get_face(((XeTeXFontInst*)font)->getHbFont());
353
354 for (int i = 0; i < 2; ++i) {
355 unsigned int scriptIndex, langIndex = 0;
356 hb_tag_t tableTag = i == 0 ? HB_OT_TAG_GSUB : HB_OT_TAG_GPOS;
357 if (hb_ot_layout_table_find_script(face, tableTag, script, &scriptIndex)) {
358 if (hb_ot_layout_script_find_language(face, tableTag, scriptIndex, language, &langIndex) || language == 0) {
359 unsigned int featCount = hb_ot_layout_language_get_feature_tags(face, tableTag, scriptIndex, langIndex, 0, NULL, NULL);
360 hb_tag_t* featList = (hb_tag_t*) xcalloc(featCount, sizeof(hb_tag_t*));
361 hb_ot_layout_language_get_feature_tags(face, tableTag, scriptIndex, langIndex, 0, &featCount, featList);
362
363 if (index < featCount) {
364 rval = featList[index];
365 break;
366 }
367
368 index -= featCount;
369 }
370 }
371 }
372
373 return rval;
374 }
375
376 uint32_t
377 countGraphiteFeatures(XeTeXLayoutEngine engine)
378 {
379 uint32_t rval = 0;
380
381 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
382 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
383
384 if (grFace != NULL)
385 rval = gr_face_n_fref(grFace);
386
387 return rval;
388 }
389
390 uint32_t
391 getGraphiteFeatureCode(XeTeXLayoutEngine engine, uint32_t index)
392 {
393 uint32_t rval = 0;
394
395 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
396 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
397
398 if (grFace != NULL) {
399 const gr_feature_ref* feature = gr_face_fref(grFace, index);
400 rval = gr_fref_id(feature);
401 }
402
403 return rval;
404 }
405
406 uint32_t
407 countGraphiteFeatureSettings(XeTeXLayoutEngine engine, uint32_t featureID)
408 {
409 uint32_t rval = 0;
410
411 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
412 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
413
414 if (grFace != NULL) {
415 const gr_feature_ref* feature = gr_face_find_fref(grFace, featureID);
416 rval = gr_fref_n_values(feature);
417 }
418
419 return rval;
420 }
421
422 uint32_t
423 getGraphiteFeatureSettingCode(XeTeXLayoutEngine engine, uint32_t featureID, uint32_t index)
424 {
425 uint32_t rval = 0;
426
427 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
428 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
429
430 if (grFace != NULL) {
431 const gr_feature_ref* feature = gr_face_find_fref(grFace, featureID);
432 rval = gr_fref_value(feature, index);
433 }
434
435 return rval;
436 }
437
438 #define tag_from_lang(x) hb_tag_from_string(hb_language_to_string(x), strlen(hb_language_to_string(x)))
439
440 uint32_t
441 getGraphiteFeatureDefaultSetting(XeTeXLayoutEngine engine, uint32_t featureID)
442 {
443 uint32_t rval = 0;
444
445 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
446 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
447
448 if (grFace != NULL) {
449 const gr_feature_ref* feature = gr_face_find_fref(grFace, featureID);
450 gr_feature_val *featureValues = gr_face_featureval_for_lang (grFace, tag_from_lang(engine->language));
451
452 rval = gr_fref_feature_value(feature, featureValues);
453 }
454
455 return rval;
456 }
457
458 char *
459 getGraphiteFeatureLabel(XeTeXLayoutEngine engine, uint32_t featureID)
460 {
461 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
462 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
463
464 if (grFace != NULL) {
465 const gr_feature_ref* feature = gr_face_find_fref(grFace, featureID);
466 uint32_t len = 0;
467 uint16_t langID = 0x409;
468
469 return (char *) gr_fref_label(feature, &langID, gr_utf8, &len);
470 }
471
472 return NULL;
473 }
474
475 char *
476 getGraphiteFeatureSettingLabel(XeTeXLayoutEngine engine, uint32_t featureID, uint32_t settingID)
477 {
478 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
479 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
480
481 if (grFace != NULL) {
482 const gr_feature_ref* feature = gr_face_find_fref(grFace, featureID);
483 for (int i = 0; i < gr_fref_n_values(feature); i++) {
484 if (settingID == gr_fref_value(feature, i)) {
485 uint32_t len = 0;
486 uint16_t langID = 0x409;
487
488 return (char *) gr_fref_value_label(feature, i, &langID, gr_utf8, &len);
489 }
490 }
491 }
492
493 return NULL;
494 }
495
496 bool
497 findGraphiteFeature(XeTeXLayoutEngine engine, const char* s, const char* e, hb_tag_t* f, int* v)
498 /* s...e is a "feature=setting" string; look for this in the font */
499 {
500 *f = 0;
501 *v = 0;
502 while (*s == ' ' || *s == '\t')
503 ++s;
504 const char* cp = s;
505 while (cp < e && *cp != '=')
506 ++cp;
507
508 *f = findGraphiteFeatureNamed(engine, s, cp - s);
509 if (*f == -1)
510 return false;
511
512 ++cp;
513 while (cp < e && (*cp == ' ' || *cp == '\t'))
514 ++cp;
515
516 if (cp == e)
517 /* no setting was specified */
518 return false;
519
520 *v = findGraphiteFeatureSettingNamed(engine, *f, cp, e - cp);
521 if (*v == -1)
522 return false;
523
524 return true;
525 }
526
527 long
528 findGraphiteFeatureNamed(XeTeXLayoutEngine engine, const char* name, int namelength)
529 {
530 long rval = -1;
531
532 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
533 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
534
535 if (grFace != NULL) {
536 for (int i = 0; i < gr_face_n_fref(grFace); i++) {
537 const gr_feature_ref* feature = gr_face_fref(grFace, i);
538 uint32_t len = 0;
539 uint16_t langID = 0x409;
540
541 // the first call is to get the length of the string
542 gr_fref_label(feature, &langID, gr_utf8, &len);
543 char* label = (char*) xmalloc(len);
544 label = (char*) gr_fref_label(feature, &langID, gr_utf8, &len);
545
546 if (strncmp(label, name, namelength) == 0) {
547 rval = gr_fref_id(feature);
548 gr_label_destroy(label);
549 break;
550 }
551
552 gr_label_destroy(label);
553 }
554 }
555
556 return rval;
557 }
558
559 long
560 findGraphiteFeatureSettingNamed(XeTeXLayoutEngine engine, uint32_t id, const char* name, int namelength)
561 {
562 long rval = -1;
563
564 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
565 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
566
567 if (grFace != NULL) {
568 const gr_feature_ref* feature = gr_face_find_fref(grFace, id);
569 for (int i = 0; i < gr_fref_n_values(feature); i++) {
570 uint32_t len = 0;
571 uint16_t langID = 0x409;
572
573 // the first call is to get the length of the string
574 gr_fref_value_label(feature, i, &langID, gr_utf8, &len);
575 char* label = (char*) xmalloc(len);
576 label = (char*) gr_fref_value_label(feature, i, &langID, gr_utf8, &len);
577
578 if (strncmp(label, name, namelength) == 0) {
579 rval = gr_fref_value(feature, i);
580 gr_label_destroy(label);
581 break;
582 }
583
584 gr_label_destroy(label);
585 }
586 }
587
588 return rval;
589 }
590
591 float
592 getGlyphWidth(XeTeXFont font, uint32_t gid)
593 {
594 return ((XeTeXFontInst*)font)->getGlyphWidth(gid);
595 }
596
597 unsigned int
598 countGlyphs(XeTeXFont font)
599 {
600 return ((XeTeXFontInst*)font)->getNumGlyphs();
601 }
602
603 XeTeXFont
604 getFont(XeTeXLayoutEngine engine)
605 {
606 return (XeTeXFont)(engine->font);
607 }
608
609 float
610 getExtendFactor(XeTeXLayoutEngine engine)
611 {
612 return engine->extend;
613 }
614
615 float
616 getSlantFactor(XeTeXLayoutEngine engine)
617 {
618 return engine->slant;
619 }
620
621 float
622 getEmboldenFactor(XeTeXLayoutEngine engine)
623 {
624 return engine->embolden;
625 }
626
627 XeTeXLayoutEngine
628 createLayoutEngine(PlatformFontRef fontRef, XeTeXFont font, hb_tag_t script, char *language,
629 hb_feature_t* features, int nFeatures, char **shapers, uint32_t rgbValue,
630 float extend, float slant, float embolden)
631 {
632 XeTeXLayoutEngine result = new XeTeXLayoutEngine_rec;
633 result->fontRef = fontRef;
634 result->font = (XeTeXFontInst*)font;
635 result->script = script;
636 result->features = features;
637 result->ShaperList = shapers;
638 result->shaper = NULL;
639 result->nFeatures = nFeatures;
640 result->rgbValue = rgbValue;
641 result->extend = extend;
642 result->slant = slant;
643 result->embolden = embolden;
644 result->hbBuffer = hb_buffer_create();
645
646 // For Graphite fonts treat the language as BCP 47 tag, for OpenType we
647 // treat it as a OT language tag for backward compatibility with pre-0.9999
648 // XeTeX.
649 if (getReqEngine() == 'G')
650 result->language = hb_language_from_string(language, -1);
651 else
652 result->language = hb_ot_tag_to_language(hb_tag_from_string(language, -1));
653
654 free(language);
655
656 return result;
657 }
658
659 void
660 deleteLayoutEngine(XeTeXLayoutEngine engine)
661 {
662 hb_buffer_destroy(engine->hbBuffer);
663 delete engine->font;
664 free(engine->shaper);
665 }
666
667 #if !HB_VERSION_ATLEAST(2,5,0)
668 static unsigned int
669 _decompose_compat(hb_unicode_funcs_t* ufuncs,
670 hb_codepoint_t u,
671 hb_codepoint_t* decomposed,
672 void* user_data)
673 {
674 return 0;
675 }
676
677 static hb_unicode_funcs_t*
678 _get_unicode_funcs(void)
679 {
680 static hb_unicode_funcs_t* ufuncs = hb_unicode_funcs_create(hb_icu_get_unicode_funcs());
681 hb_unicode_funcs_set_decompose_compatibility_func(ufuncs, _decompose_compat, NULL, NULL);
682 return ufuncs;
683 }
684 #endif
685
686 int
687 layoutChars(XeTeXLayoutEngine engine, uint16_t chars[], int32_t offset, int32_t count, int32_t max,
688 bool rightToLeft)
689 {
690 bool res;
691 hb_script_t script = HB_SCRIPT_INVALID;
692 hb_direction_t direction = HB_DIRECTION_LTR;
693 hb_segment_properties_t segment_props;
694 hb_shape_plan_t *shape_plan;
695 hb_font_t* hbFont = engine->font->getHbFont();
696 hb_face_t* hbFace = hb_font_get_face(hbFont);
697
698 if (engine->font->getLayoutDirVertical())
699 direction = HB_DIRECTION_TTB;
700 else if (rightToLeft)
701 direction = HB_DIRECTION_RTL;
702
703 script = hb_ot_tag_to_script (engine->script);
704
705 hb_buffer_reset(engine->hbBuffer);
706
707 #if !HB_VERSION_ATLEAST(2,5,0)
708 static hb_unicode_funcs_t* hbUnicodeFuncs = NULL;
709 if (hbUnicodeFuncs == NULL)
710 hbUnicodeFuncs = _get_unicode_funcs();
711 hb_buffer_set_unicode_funcs(engine->hbBuffer, hbUnicodeFuncs);
712 #endif
713
714 hb_buffer_add_utf16(engine->hbBuffer, chars, max, offset, count);
715 hb_buffer_set_direction(engine->hbBuffer, direction);
716 hb_buffer_set_script(engine->hbBuffer, script);
717 hb_buffer_set_language(engine->hbBuffer, engine->language);
718
719 hb_buffer_guess_segment_properties(engine->hbBuffer);
720 hb_buffer_get_segment_properties(engine->hbBuffer, &segment_props);
721
722 if (engine->ShaperList == NULL) {
723 // HarfBuzz gives graphite2 shaper a priority, so that for hybrid
724 // Graphite/OpenType fonts, Graphite will be used. However, pre-0.9999
725 // XeTeX preferred OpenType over Graphite, so we are doing the same
726 // here for sake of backward compatibility. Since "ot" shaper never
727 // fails, we set the shaper list to just include it.
728 engine->ShaperList = (char**) xcalloc(2, sizeof(char*));
729 engine->ShaperList[0] = (char*) "ot";
730 engine->ShaperList[1] = NULL;
731 }
732
733 shape_plan = hb_shape_plan_create_cached(hbFace, &segment_props, engine->features, engine->nFeatures, engine->ShaperList);
734 res = hb_shape_plan_execute(shape_plan, hbFont, engine->hbBuffer, engine->features, engine->nFeatures);
735
736 if (res) {
737 engine->shaper = strdup(hb_shape_plan_get_shaper(shape_plan));
738 hb_buffer_set_content_type(engine->hbBuffer, HB_BUFFER_CONTENT_TYPE_GLYPHS);
739 } else {
740 // all selected shapers failed, retrying with default
741 // we don't use _cached here as the cached plain will always fail.
742 hb_shape_plan_destroy(shape_plan);
743 shape_plan = hb_shape_plan_create(hbFace, &segment_props, engine->features, engine->nFeatures, NULL);
744 res = hb_shape_plan_execute(shape_plan, hbFont, engine->hbBuffer, engine->features, engine->nFeatures);
745
746 if (res) {
747 engine->shaper = strdup(hb_shape_plan_get_shaper(shape_plan));
748 hb_buffer_set_content_type(engine->hbBuffer, HB_BUFFER_CONTENT_TYPE_GLYPHS);
749 } else {
750 fprintf(stderr, "\nERROR: all shapers failed\n");
751 exit(3);
752 }
753 }
754
755 hb_shape_plan_destroy(shape_plan);
756
757 int glyphCount = hb_buffer_get_length(engine->hbBuffer);
758
759 #ifdef DEBUG
760 char buf[1024];
761 unsigned int consumed;
762
763 printf ("shaper: %s\n", engine->shaper);
764
765 hb_buffer_serialize_flags_t flags = HB_BUFFER_SERIALIZE_FLAGS_DEFAULT;
766 hb_buffer_serialize_format_t format = HB_BUFFER_SERIALIZE_FORMAT_JSON;
767
768 hb_buffer_serialize_glyphs (engine->hbBuffer, 0, glyphCount, buf, sizeof(buf), &consumed, hbFont, format, flags);
769 if (consumed)
770 printf ("buffer glyphs: %s\n", buf);
771 #endif
772
773 return glyphCount;
774 }
775
776 void
777 getGlyphs(XeTeXLayoutEngine engine, uint32_t glyphs[])
778 {
779 int glyphCount = hb_buffer_get_length(engine->hbBuffer);
780 hb_glyph_info_t *hbGlyphs = hb_buffer_get_glyph_infos(engine->hbBuffer, NULL);
781
782 for (int i = 0; i < glyphCount; i++)
783 glyphs[i] = hbGlyphs[i].codepoint;
784 }
785
786 void
787 getGlyphAdvances(XeTeXLayoutEngine engine, float advances[])
788 {
789 int glyphCount = hb_buffer_get_length(engine->hbBuffer);
790 hb_glyph_position_t *hbPositions = hb_buffer_get_glyph_positions(engine->hbBuffer, NULL);
791
792 for (int i = 0; i < glyphCount; i++) {
793 if (engine->font->getLayoutDirVertical())
794 advances[i] = engine->font->unitsToPoints(hbPositions[i].y_advance);
795 else
796 advances[i] = engine->font->unitsToPoints(hbPositions[i].x_advance);
797 }
798 }
799
800 void
801 getGlyphPositions(XeTeXLayoutEngine engine, FloatPoint positions[])
802 {
803 int glyphCount = hb_buffer_get_length(engine->hbBuffer);
804 hb_glyph_position_t *hbPositions = hb_buffer_get_glyph_positions(engine->hbBuffer, NULL);
805
806 float x = 0, y = 0;
807
808 if (engine->font->getLayoutDirVertical()) {
809 for (int i = 0; i < glyphCount; i++) {
810 positions[i].x = -engine->font->unitsToPoints(x + hbPositions[i].y_offset); /* negative is forwards */
811 positions[i].y = engine->font->unitsToPoints(y - hbPositions[i].x_offset);
812 x += hbPositions[i].y_advance;
813 y += hbPositions[i].x_advance;
814 }
815 positions[glyphCount].x = -engine->font->unitsToPoints(x);
816 positions[glyphCount].y = engine->font->unitsToPoints(y);
817 } else {
818 for (int i = 0; i < glyphCount; i++) {
819 positions[i].x = engine->font->unitsToPoints(x + hbPositions[i].x_offset);
820 positions[i].y = -engine->font->unitsToPoints(y + hbPositions[i].y_offset); /* negative is upwards */
821 x += hbPositions[i].x_advance;
822 y += hbPositions[i].y_advance;
823 }
824 positions[glyphCount].x = engine->font->unitsToPoints(x);
825 positions[glyphCount].y = -engine->font->unitsToPoints(y);
826 }
827
828 if (engine->extend != 1.0 || engine->slant != 0.0)
829 for (int i = 0; i <= glyphCount; ++i)
830 positions[i].x = positions[i].x * engine->extend - positions[i].y * engine->slant;
831 }
832
833 float
834 getPointSize(XeTeXLayoutEngine engine)
835 {
836 return engine->font->getPointSize();
837 }
838
839 void
840 getAscentAndDescent(XeTeXLayoutEngine engine, float* ascent, float* descent)
841 {
842 *ascent = engine->font->getAscent();
843 *descent = engine->font->getDescent();
844 }
845
846 void
847 getCapAndXHeight(XeTeXLayoutEngine engine, float* capheight, float* xheight)
848 {
849 *capheight = engine->font->getCapHeight();
850 *xheight = engine->font->getXHeight();
851 }
852
853 int
854 getDefaultDirection(XeTeXLayoutEngine engine)
855 {
856 hb_script_t script = hb_buffer_get_script(engine->hbBuffer);
857 if (hb_script_get_horizontal_direction (script) == HB_DIRECTION_RTL)
858 return UBIDI_DEFAULT_RTL;
859 else
860 return UBIDI_DEFAULT_LTR;
861 }
862
863 uint32_t
864 getRgbValue(XeTeXLayoutEngine engine)
865 {
866 return engine->rgbValue;
867 }
868
869 void
870 getGlyphBounds(XeTeXLayoutEngine engine, uint32_t glyphID, GlyphBBox* bbox)
871 {
872 engine->font->getGlyphBounds(glyphID, bbox);
873 if (engine->extend != 0.0) {
874 bbox->xMin *= engine->extend;
875 bbox->xMax *= engine->extend;
876 }
877 }
878
879 float
880 getGlyphWidthFromEngine(XeTeXLayoutEngine engine, uint32_t glyphID)
881 {
882 return engine->extend * engine->font->getGlyphWidth(glyphID);
883 }
884
885 void
886 getGlyphHeightDepth(XeTeXLayoutEngine engine, uint32_t glyphID, float* height, float* depth)
887 {
888 engine->font->getGlyphHeightDepth(glyphID, height, depth);
889 }
890
891 void
892 getGlyphSidebearings(XeTeXLayoutEngine engine, uint32_t glyphID, float* lsb, float* rsb)
893 {
894 engine->font->getGlyphSidebearings(glyphID, lsb, rsb);
895 if (engine->extend != 0.0) {
896 *lsb *= engine->extend;
897 *rsb *= engine->extend;
898 }
899 }
900
901 float
902 getGlyphItalCorr(XeTeXLayoutEngine engine, uint32_t glyphID)
903 {
904 return engine->extend * engine->font->getGlyphItalCorr(glyphID);
905 }
906
907 uint32_t
908 mapCharToGlyph(XeTeXLayoutEngine engine, uint32_t charCode)
909 {
910 return engine->font->mapCharToGlyph(charCode);
911 }
912
913 int
914 getFontCharRange(XeTeXLayoutEngine engine, int reqFirst)
915 {
916 if (reqFirst)
917 return engine->font->getFirstCharCode();
918 else
919 return engine->font->getLastCharCode();
920 }
921
922 const char*
923 getGlyphName(XeTeXFont font, uint16_t gid, int* len)
924 {
925 return ((XeTeXFontInst*)font)->getGlyphName(gid, *len);
926 }
927
928 int
929 mapGlyphToIndex(XeTeXLayoutEngine engine, const char* glyphName)
930 {
931 return engine->font->mapGlyphToIndex(glyphName);
932 }
933
934 static gr_segment* grSegment = NULL;
935 static const gr_slot* grPrevSlot = NULL;
936 static int grTextLen;
937
938 bool
939 initGraphiteBreaking(XeTeXLayoutEngine engine, const uint16_t* txtPtr, int txtLen)
940 {
941 hb_face_t* hbFace = hb_font_get_face(engine->font->getHbFont());
942 gr_face* grFace = hb_graphite2_face_get_gr_face(hbFace);
943 gr_font* grFont = hb_graphite2_font_get_gr_font(engine->font->getHbFont());
944 if (grFace != NULL && grFont != NULL) {
945 if (grSegment != NULL) {
946 gr_seg_destroy(grSegment);
947 grSegment = NULL;
948 grPrevSlot = NULL;
949 }
950
951 gr_feature_val *grFeatureValues = gr_face_featureval_for_lang (grFace, tag_from_lang(engine->language));
952
953 int nFeatures = engine->nFeatures;
954 hb_feature_t *features = engine->features;
955 while (nFeatures--) {
956 const gr_feature_ref *fref = gr_face_find_fref (grFace, features->tag);
957 if (fref)
958 gr_fref_set_feature_value (fref, features->value, grFeatureValues);
959 features++;
960 }
961
962 grSegment = gr_make_seg(grFont, grFace, engine->script, grFeatureValues, gr_utf16, txtPtr, txtLen, 0);
963 grPrevSlot = gr_seg_first_slot(grSegment);
964 grTextLen = txtLen;
965
966 return true;
967 }
968
969 return false;
970 }
971
972 int
973 findNextGraphiteBreak(void)
974 {
975 int ret = -1;
976
977 if (grSegment != NULL) {
978 if (grPrevSlot && grPrevSlot != gr_seg_last_slot(grSegment)) {
979 for (const gr_slot* s = gr_slot_next_in_segment(grPrevSlot); s != NULL; s = gr_slot_next_in_segment(s)) {
980 const gr_char_info* ci = NULL;
981 int bw;
982
983 ci = gr_seg_cinfo(grSegment, gr_slot_index(s));
984 bw = gr_cinfo_break_weight(ci);
985 if (bw < gr_breakNone && bw >= gr_breakBeforeWord) {
986 grPrevSlot = s;
987 ret = gr_cinfo_base(ci);
988 } else if (bw > gr_breakNone && bw <= gr_breakWord) {
989 grPrevSlot = gr_slot_next_in_segment(s);
990 ret = gr_cinfo_base(ci) + 1;
991 }
992
993 if (ret != -1)
994 break;
995 }
996
997 if (ret == -1) {
998 grPrevSlot = gr_seg_last_slot(grSegment);
999 ret = grTextLen;
1000 }
1001 }
1002 }
1003
1004 return ret;
1005 }
1006
1007 bool
1008 usingGraphite(XeTeXLayoutEngine engine)
1009 {
1010 if (engine->shaper != NULL && (strcmp("graphite2", engine->shaper) == 0))
1011 return true;
1012 else
1013 return false;
1014 }
1015
1016 bool
1017 usingOpenType(XeTeXLayoutEngine engine)
1018 {
1019 if (engine->shaper == NULL || (strcmp("ot", engine->shaper) == 0))
1020 return true;
1021 else
1022 return false;
1023 }
1024
1025 bool
1026 isOpenTypeMathFont(XeTeXLayoutEngine engine)
1027 {
1028 return hb_ot_math_has_data(hb_font_get_face(engine->font->getHbFont()));
1029 }