2 * Copyright © 2011,2012,2013 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #include "hb-private.hh"
28 #include "hb-debug.hh"
29 #define HB_SHAPER uniscribe
30 #include "hb-shaper-impl-private.hh"
36 #include "hb-uniscribe.h"
38 #include "hb-open-file-private.hh"
39 #include "hb-ot-name-table.hh"
40 #include "hb-ot-tag.h"
43 static inline uint16_t hb_uint16_swap (const uint16_t v)
44 { return (v >> 8) | (v << 8); }
45 static inline uint32_t hb_uint32_swap (const uint32_t v)
46 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
49 typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
50 const WCHAR *pwcInChars,
53 const SCRIPT_CONTROL *psControl,
54 const SCRIPT_STATE *psState,
56 OPENTYPE_TAG *pScriptTags,
60 typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
64 OPENTYPE_TAG tagScript,
65 OPENTYPE_TAG tagLangSys,
67 TEXTRANGE_PROPERTIES **rpRangeProperties,
69 const WCHAR *pwcChars,
73 SCRIPT_CHARPROP *pCharProps,
75 SCRIPT_GLYPHPROP *pOutGlyphProps,
79 typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
83 OPENTYPE_TAG tagScript,
84 OPENTYPE_TAG tagLangSys,
86 TEXTRANGE_PROPERTIES **rpRangeProperties,
88 const WCHAR *pwcChars,
90 SCRIPT_CHARPROP *pCharProps,
93 const SCRIPT_GLYPHPROP *pGlyphProps,
101 /* Fallback implementations. */
103 static HRESULT WINAPI
104 hb_ScriptItemizeOpenType(
105 const WCHAR *pwcInChars,
108 const SCRIPT_CONTROL *psControl,
109 const SCRIPT_STATE *psState,
111 OPENTYPE_TAG *pScriptTags,
116 return ScriptItemize (pwcInChars,
126 static HRESULT WINAPI
127 hb_ScriptShapeOpenType(
130 SCRIPT_ANALYSIS *psa,
131 OPENTYPE_TAG tagScript,
132 OPENTYPE_TAG tagLangSys,
134 TEXTRANGE_PROPERTIES **rpRangeProperties,
136 const WCHAR *pwcChars,
140 SCRIPT_CHARPROP *pCharProps,
142 SCRIPT_GLYPHPROP *pOutGlyphProps,
146 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
147 return ScriptShape (hdc,
159 static HRESULT WINAPI
160 hb_ScriptPlaceOpenType(
163 SCRIPT_ANALYSIS *psa,
164 OPENTYPE_TAG tagScript,
165 OPENTYPE_TAG tagLangSys,
167 TEXTRANGE_PROPERTIES **rpRangeProperties,
169 const WCHAR *pwcChars,
171 SCRIPT_CHARPROP *pCharProps,
173 const WORD *pwGlyphs,
174 const SCRIPT_GLYPHPROP *pGlyphProps,
181 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
182 return ScriptPlace (hdc,
194 struct hb_uniscribe_shaper_funcs_t {
195 SIOT ScriptItemizeOpenType;
196 SSOT ScriptShapeOpenType;
197 SPOT ScriptPlaceOpenType;
199 inline void init (void)
202 this->ScriptItemizeOpenType = nullptr;
203 this->ScriptShapeOpenType = nullptr;
204 this->ScriptPlaceOpenType = nullptr;
206 hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
209 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
210 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
211 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
213 if (!this->ScriptItemizeOpenType ||
214 !this->ScriptShapeOpenType ||
215 !this->ScriptPlaceOpenType)
217 DEBUG_MSG (UNISCRIBE, nullptr, "OpenType versions of functions not found; falling back.");
218 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
219 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
220 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
224 static hb_uniscribe_shaper_funcs_t *uniscribe_funcs;
227 free_uniscribe_funcs (void)
229 free (uniscribe_funcs);
232 static hb_uniscribe_shaper_funcs_t *
233 hb_uniscribe_shaper_get_funcs (void)
236 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
238 if (unlikely (!funcs))
240 funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
241 if (unlikely (!funcs))
246 if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, nullptr, funcs)) {
252 atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */
260 struct active_feature_t {
261 OPENTYPE_FEATURE_RECORD rec;
264 static int cmp (const void *pa, const void *pb) {
265 const active_feature_t *a = (const active_feature_t *) pa;
266 const active_feature_t *b = (const active_feature_t *) pb;
267 return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
268 a->order < b->order ? -1 : a->order > b->order ? 1 :
269 a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
272 bool operator== (const active_feature_t *f) {
273 return cmp (this, f) == 0;
277 struct feature_event_t {
280 active_feature_t feature;
282 static int cmp (const void *pa, const void *pb) {
283 const feature_event_t *a = (const feature_event_t *) pa;
284 const feature_event_t *b = (const feature_event_t *) pb;
285 return a->index < b->index ? -1 : a->index > b->index ? 1 :
286 a->start < b->start ? -1 : a->start > b->start ? 1 :
287 active_feature_t::cmp (&a->feature, &b->feature);
291 struct range_record_t {
292 TEXTRANGE_PROPERTIES props;
293 unsigned int index_first; /* == start */
294 unsigned int index_last; /* == end - 1 */
297 HB_SHAPER_DATA_ENSURE_DEFINE(uniscribe, face)
298 HB_SHAPER_DATA_ENSURE_DEFINE(uniscribe, font)
305 struct hb_uniscribe_shaper_face_data_t {
307 hb_uniscribe_shaper_funcs_t *funcs;
308 wchar_t face_name[LF_FACESIZE];
311 /* face_name should point to a wchar_t[LF_FACESIZE] object. */
313 _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
315 /* We'll create a private name for the font from a UUID using a simple,
316 * somewhat base64-like encoding scheme */
317 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
319 UuidCreate ((UUID*) &id);
320 static_assert ((2 + 3 * (16/2) < LF_FACESIZE), "");
321 unsigned int name_str_len = 0;
322 face_name[name_str_len++] = 'F';
323 face_name[name_str_len++] = '_';
324 unsigned char *p = (unsigned char *) &id;
325 for (unsigned int i = 0; i < 16; i += 2)
327 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
328 * using the bits in groups of 5,5,6 to select chars from enc.
329 * This will generate 24 characters; with the 'F_' prefix we already provided,
330 * the name will be 26 chars (plus the NUL terminator), so will always fit within
331 * face_name (LF_FACESIZE = 32). */
332 face_name[name_str_len++] = enc[p[i] >> 3];
333 face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
334 face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
336 face_name[name_str_len] = 0;
338 *plen = name_str_len;
343 _hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
345 /* Create a copy of the font data, with the 'name' table replaced by a
346 * table that names the font with our private F_* name created above.
347 * For simplicity, we just append a new 'name' table and update the
348 * sfnt directory; the original table is left in place, but unused.
350 * The new table will contain just 5 name IDs: family, style, unique,
351 * full, PS. All of them point to the same name data with our unique name.
354 blob = OT::Sanitizer<OT::OpenTypeFontFile>().sanitize (blob);
356 unsigned int length, new_length, name_str_len;
357 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
359 _hb_generate_unique_face_name (new_name, &name_str_len);
361 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
363 unsigned int name_table_length = OT::name::min_size +
364 ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
365 name_str_len * 2; /* for name data in UTF16BE form */
366 unsigned int name_table_offset = (length + 3) & ~3;
368 new_length = name_table_offset + ((name_table_length + 3) & ~3);
369 void *new_sfnt_data = calloc (1, new_length);
372 hb_blob_destroy (blob);
376 memcpy(new_sfnt_data, orig_sfnt_data, length);
378 OT::name &name = OT::StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
380 name.count.set (ARRAY_LENGTH (name_IDs));
381 name.stringOffset.set (name.get_size ());
382 for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
384 OT::NameRecord &record = name.nameRecord[i];
385 record.platformID.set (3);
386 record.encodingID.set (1);
387 record.languageID.set (0x0409u); /* English */
388 record.nameID.set (name_IDs[i]);
389 record.length.set (name_str_len * 2);
390 record.offset.set (0);
393 /* Copy string data from new_name, converting wchar_t to UTF16BE. */
394 unsigned char *p = &OT::StructAfter<unsigned char> (name);
395 for (unsigned int i = 0; i < name_str_len; i++)
397 *p++ = new_name[i] >> 8;
398 *p++ = new_name[i] & 0xff;
401 /* Adjust name table entry to point to new name table */
402 const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
403 unsigned int face_count = file.get_face_count ();
404 for (unsigned int face_index = 0; face_index < face_count; face_index++)
406 /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
407 * toe-stepping. But we don't really care. */
408 const OT::OpenTypeFontFace &face = file.get_face (face_index);
410 if (face.find_table_index (HB_OT_TAG_name, &index))
412 OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
413 record.checkSum.set_for_data (&name, name_table_length);
414 record.offset.set (name_table_offset);
415 record.length.set (name_table_length);
417 else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
419 free (new_sfnt_data);
420 hb_blob_destroy (blob);
425 /* The checkSumAdjustment field in the 'head' table is now wrong,
426 * but that doesn't actually seem to cause any problems so we don't
429 hb_blob_destroy (blob);
430 return hb_blob_create ((const char *) new_sfnt_data, new_length,
431 HB_MEMORY_MODE_WRITABLE, nullptr, free);
434 hb_uniscribe_shaper_face_data_t *
435 _hb_uniscribe_shaper_face_data_create (hb_face_t *face)
437 hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
438 if (unlikely (!data))
441 data->funcs = hb_uniscribe_shaper_get_funcs ();
442 if (unlikely (!data->funcs))
448 hb_blob_t *blob = hb_face_reference_blob (face);
449 if (unlikely (!hb_blob_get_length (blob)))
450 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
452 blob = _hb_rename_font (blob, data->face_name);
453 if (unlikely (!blob))
459 DWORD num_fonts_installed;
460 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, nullptr),
461 hb_blob_get_length (blob),
462 0, &num_fonts_installed);
463 if (unlikely (!data->fh))
465 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
474 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
476 RemoveFontMemResourceEx (data->fh);
485 struct hb_uniscribe_shaper_font_data_t {
489 SCRIPT_CACHE script_cache;
490 double x_mult, y_mult; /* From LOGFONT space to HB space. */
494 populate_log_font (LOGFONTW *lf,
496 unsigned int font_size)
498 memset (lf, 0, sizeof (*lf));
499 lf->lfHeight = - (int) font_size;
500 lf->lfCharSet = DEFAULT_CHARSET;
502 hb_face_t *face = font->face;
503 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
505 memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName));
510 hb_uniscribe_shaper_font_data_t *
511 _hb_uniscribe_shaper_font_data_create (hb_font_t *font)
513 if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return nullptr;
515 hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
516 if (unlikely (!data))
519 int font_size = font->face->get_upem (); /* Default... */
520 /* No idea if the following is even a good idea. */
522 font_size = font->y_ppem;
525 font_size = -font_size;
526 data->x_mult = (double) font->x_scale / font_size;
527 data->y_mult = (double) font->y_scale / font_size;
529 data->hdc = GetDC (nullptr);
531 if (unlikely (!populate_log_font (&data->log_font, font, font_size))) {
532 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
533 _hb_uniscribe_shaper_font_data_destroy (data);
537 data->hfont = CreateFontIndirectW (&data->log_font);
538 if (unlikely (!data->hfont)) {
539 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
540 _hb_uniscribe_shaper_font_data_destroy (data);
544 if (!SelectObject (data->hdc, data->hfont)) {
545 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
546 _hb_uniscribe_shaper_font_data_destroy (data);
554 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
557 ReleaseDC (nullptr, data->hdc);
559 DeleteObject (data->hfont);
560 if (data->script_cache)
561 ScriptFreeCache (&data->script_cache);
566 hb_uniscribe_font_get_logfontw (hb_font_t *font)
568 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return nullptr;
569 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
570 return &font_data->log_font;
574 hb_uniscribe_font_get_hfont (hb_font_t *font)
576 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return nullptr;
577 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
578 return font_data->hfont;
583 * shaper shape_plan data
586 struct hb_uniscribe_shaper_shape_plan_data_t {};
588 hb_uniscribe_shaper_shape_plan_data_t *
589 _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
590 const hb_feature_t *user_features HB_UNUSED,
591 unsigned int num_user_features HB_UNUSED,
592 const int *coords HB_UNUSED,
593 unsigned int num_coords HB_UNUSED)
595 return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
599 _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED)
610 _hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
613 const hb_feature_t *features,
614 unsigned int num_features)
616 hb_face_t *face = font->face;
617 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
618 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
619 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
624 hb_auto_array_t<OPENTYPE_FEATURE_RECORD> feature_records;
625 hb_auto_array_t<range_record_t> range_records;
628 /* Sort features by start/end events. */
629 hb_auto_array_t<feature_event_t> feature_events;
630 for (unsigned int i = 0; i < num_features; i++)
632 active_feature_t feature;
633 feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
634 feature.rec.lParameter = features[i].value;
637 feature_event_t *event;
639 event = feature_events.push ();
640 if (unlikely (!event))
642 event->index = features[i].start;
644 event->feature = feature;
646 event = feature_events.push ();
647 if (unlikely (!event))
649 event->index = features[i].end;
650 event->start = false;
651 event->feature = feature;
653 feature_events.qsort ();
654 /* Add a strategic final event. */
656 active_feature_t feature;
657 feature.rec.tagFeature = 0;
658 feature.rec.lParameter = 0;
659 feature.order = num_features + 1;
661 feature_event_t *event = feature_events.push ();
662 if (unlikely (!event))
664 event->index = 0; /* This value does magic. */
665 event->start = false;
666 event->feature = feature;
669 /* Scan events and save features for each range. */
670 hb_auto_array_t<active_feature_t> active_features;
671 unsigned int last_index = 0;
672 for (unsigned int i = 0; i < feature_events.len; i++)
674 feature_event_t *event = &feature_events[i];
676 if (event->index != last_index)
678 /* Save a snapshot of active features and the range. */
679 range_record_t *range = range_records.push ();
680 if (unlikely (!range))
683 unsigned int offset = feature_records.len;
685 active_features.qsort ();
686 for (unsigned int j = 0; j < active_features.len; j++)
688 if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.len - 1].tagFeature)
690 OPENTYPE_FEATURE_RECORD *feature = feature_records.push ();
691 if (unlikely (!feature))
693 *feature = active_features[j].rec;
697 /* Overrides value for existing feature. */
698 feature_records[feature_records.len - 1].lParameter = active_features[j].rec.lParameter;
702 /* Will convert to pointer after all is ready, since feature_records.array
703 * may move as we grow it. */
704 range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
705 range->props.cotfRecords = feature_records.len - offset;
706 range->index_first = last_index;
707 range->index_last = event->index - 1;
709 last_index = event->index;
713 active_feature_t *feature = active_features.push ();
714 if (unlikely (!feature))
716 *feature = event->feature;
718 active_feature_t *feature = active_features.find (&event->feature);
720 active_features.remove (feature - active_features.array);
724 if (!range_records.len) /* No active feature found. */
727 /* Fixup the pointers. */
728 for (unsigned int i = 0; i < range_records.len; i++)
730 range_record_t *range = &range_records[i];
731 range->props.potfRecords = feature_records.array + reinterpret_cast<uintptr_t> (range->props.potfRecords);
742 DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \
750 unsigned int scratch_size;
751 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
753 #define ALLOCATE_ARRAY(Type, name, len) \
754 Type *name = (Type *) scratch; \
756 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
757 assert (_consumed <= scratch_size); \
758 scratch += _consumed; \
759 scratch_size -= _consumed; \
762 #define utf16_index() var1.u32
764 ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
766 unsigned int chars_len = 0;
767 for (unsigned int i = 0; i < buffer->len; i++)
769 hb_codepoint_t c = buffer->info[i].codepoint;
770 buffer->info[i].utf16_index() = chars_len;
771 if (likely (c <= 0xFFFFu))
772 pchars[chars_len++] = c;
773 else if (unlikely (c > 0x10FFFFu))
774 pchars[chars_len++] = 0xFFFDu;
776 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
777 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
781 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
782 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
786 /* Need log_clusters to assign features. */
788 for (unsigned int i = 0; i < buffer->len; i++)
790 hb_codepoint_t c = buffer->info[i].codepoint;
791 unsigned int cluster = buffer->info[i].cluster;
792 log_clusters[chars_len++] = cluster;
793 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
794 log_clusters[chars_len++] = cluster; /* Surrogates. */
798 /* The -2 in the following is to compensate for possible
799 * alignment needed after the WORD array. sizeof(WORD) == 2. */
800 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
802 sizeof (SCRIPT_GLYPHPROP) +
807 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
808 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
809 ALLOCATE_ARRAY (int, advances, glyphs_size);
810 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
811 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
814 * We can't touch the contents of glyph_props. Our fallback
815 * implementations of Shape and Place functions use that buffer
816 * by casting it to a different type. It works because they
817 * both agree about it, but if we want to access it here we
818 * need address that issue first.
821 #undef ALLOCATE_ARRAY
823 #define MAX_ITEMS 256
825 SCRIPT_ITEM items[MAX_ITEMS + 1];
826 SCRIPT_CONTROL bidi_control = {0};
827 SCRIPT_STATE bidi_state = {0};
828 ULONG script_tags[MAX_ITEMS];
831 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
832 //bidi_control.fMergeNeutralItems = true;
833 *(uint32_t*)&bidi_control |= 1u<<24;
835 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
836 bidi_state.fOverrideDirection = 1;
838 hr = funcs->ScriptItemizeOpenType (pchars,
846 if (unlikely (FAILED (hr)))
847 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
851 OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
852 hb_auto_array_t<TEXTRANGE_PROPERTIES*> range_properties;
853 hb_auto_array_t<int> range_char_counts;
855 unsigned int glyphs_offset = 0;
856 unsigned int glyphs_len;
857 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
858 for (int i = 0; i < item_count; i++)
860 unsigned int chars_offset = items[i].iCharPos;
861 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
865 range_properties.shrink (0);
866 range_char_counts.shrink (0);
868 range_record_t *last_range = &range_records[0];
870 for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
872 range_record_t *range = last_range;
873 while (log_clusters[k] < range->index_first)
875 while (log_clusters[k] > range->index_last)
877 if (!range_properties.len ||
878 &range->props != range_properties[range_properties.len - 1])
880 TEXTRANGE_PROPERTIES **props = range_properties.push ();
881 int *c = range_char_counts.push ();
882 if (unlikely (!props || !c))
884 range_properties.shrink (0);
885 range_char_counts.shrink (0);
888 *props = &range->props;
893 range_char_counts[range_char_counts.len - 1]++;
900 /* Asking for glyphs in logical order circumvents at least
901 * one bug in Uniscribe. */
902 items[i].a.fLogicalOrder = true;
905 hr = funcs->ScriptShapeOpenType (font_data->hdc,
906 &font_data->script_cache,
910 range_char_counts.array,
911 range_properties.array,
912 range_properties.len,
913 pchars + chars_offset,
915 glyphs_size - glyphs_offset,
917 log_clusters + chars_offset,
918 char_props + chars_offset,
919 glyphs + glyphs_offset,
920 glyph_props + glyphs_offset,
921 (int *) &glyphs_len);
923 if (unlikely (items[i].a.fNoGlyphIndex))
924 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
925 if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
927 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
928 FAIL ("Buffer resize failed");
931 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
933 if (items[i].a.eScript == SCRIPT_UNDEFINED)
934 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
935 items[i].a.eScript = SCRIPT_UNDEFINED;
938 if (unlikely (FAILED (hr)))
940 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
943 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
944 log_clusters[j] += glyphs_offset;
946 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
947 &font_data->script_cache,
951 range_char_counts.array,
952 range_properties.array,
953 range_properties.len,
954 pchars + chars_offset,
955 log_clusters + chars_offset,
956 char_props + chars_offset,
958 glyphs + glyphs_offset,
959 glyph_props + glyphs_offset,
962 advances + glyphs_offset,
963 offsets + glyphs_offset,
965 if (unlikely (FAILED (hr)))
966 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
968 if (DEBUG_ENABLED (UNISCRIBE))
969 fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
972 items[i].a.fLayoutRTL,
973 items[i].a.fLogicalOrder,
974 HB_UNTAG (hb_uint32_swap (script_tags[i])));
976 glyphs_offset += glyphs_len;
978 glyphs_len = glyphs_offset;
980 /* Ok, we've got everything we need, now compose output buffer,
981 * very, *very*, carefully! */
983 /* Calculate visual-clusters. That's what we ship. */
984 for (unsigned int i = 0; i < glyphs_len; i++)
985 vis_clusters[i] = -1;
986 for (unsigned int i = 0; i < buffer->len; i++) {
987 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
988 *p = MIN (*p, buffer->info[i].cluster);
990 for (unsigned int i = 1; i < glyphs_len; i++)
991 if (vis_clusters[i] == -1)
992 vis_clusters[i] = vis_clusters[i - 1];
996 if (unlikely (!buffer->ensure (glyphs_len)))
997 FAIL ("Buffer in error");
1001 /* Set glyph infos */
1003 for (unsigned int i = 0; i < glyphs_len; i++)
1005 hb_glyph_info_t *info = &buffer->info[buffer->len++];
1007 info->codepoint = glyphs[i];
1008 info->cluster = vis_clusters[i];
1010 /* The rest is crap. Let's store position info there for now. */
1011 info->mask = advances[i];
1012 info->var1.i32 = offsets[i].du;
1013 info->var2.i32 = offsets[i].dv;
1016 /* Set glyph positions */
1017 buffer->clear_positions ();
1018 double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
1019 for (unsigned int i = 0; i < glyphs_len; i++)
1021 hb_glyph_info_t *info = &buffer->info[i];
1022 hb_glyph_position_t *pos = &buffer->pos[i];
1025 pos->x_advance = x_mult * (int32_t) info->mask;
1026 pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
1027 pos->y_offset = y_mult * info->var2.i32;
1031 hb_buffer_reverse (buffer);
1033 buffer->unsafe_to_break_all ();