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 #define HB_SHAPER uniscribe
28 #include "hb-shaper-impl-private.hh"
34 #include "hb-uniscribe.h"
36 #include "hb-open-file-private.hh"
37 #include "hb-ot-name-table.hh"
38 #include "hb-ot-tag.h"
41 #ifndef HB_DEBUG_UNISCRIBE
42 #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
46 typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
47 const WCHAR *pwcInChars,
50 const SCRIPT_CONTROL *psControl,
51 const SCRIPT_STATE *psState,
53 OPENTYPE_TAG *pScriptTags,
57 typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
61 OPENTYPE_TAG tagScript,
62 OPENTYPE_TAG tagLangSys,
64 TEXTRANGE_PROPERTIES **rpRangeProperties,
66 const WCHAR *pwcChars,
70 SCRIPT_CHARPROP *pCharProps,
72 SCRIPT_GLYPHPROP *pOutGlyphProps,
76 typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
80 OPENTYPE_TAG tagScript,
81 OPENTYPE_TAG tagLangSys,
83 TEXTRANGE_PROPERTIES **rpRangeProperties,
85 const WCHAR *pwcChars,
87 SCRIPT_CHARPROP *pCharProps,
90 const SCRIPT_GLYPHPROP *pGlyphProps,
98 /* Fallback implementations. */
100 static HRESULT WINAPI
101 hb_ScriptItemizeOpenType(
102 const WCHAR *pwcInChars,
105 const SCRIPT_CONTROL *psControl,
106 const SCRIPT_STATE *psState,
108 OPENTYPE_TAG *pScriptTags,
113 return ScriptItemize (pwcInChars,
123 static HRESULT WINAPI
124 hb_ScriptShapeOpenType(
127 SCRIPT_ANALYSIS *psa,
128 OPENTYPE_TAG tagScript,
129 OPENTYPE_TAG tagLangSys,
131 TEXTRANGE_PROPERTIES **rpRangeProperties,
133 const WCHAR *pwcChars,
137 SCRIPT_CHARPROP *pCharProps,
139 SCRIPT_GLYPHPROP *pOutGlyphProps,
143 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
144 return ScriptShape (hdc,
156 static HRESULT WINAPI
157 hb_ScriptPlaceOpenType(
160 SCRIPT_ANALYSIS *psa,
161 OPENTYPE_TAG tagScript,
162 OPENTYPE_TAG tagLangSys,
164 TEXTRANGE_PROPERTIES **rpRangeProperties,
166 const WCHAR *pwcChars,
168 SCRIPT_CHARPROP *pCharProps,
170 const WORD *pwGlyphs,
171 const SCRIPT_GLYPHPROP *pGlyphProps,
178 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
179 return ScriptPlace (hdc,
191 struct hb_uniscribe_shaper_funcs_t {
192 SIOT ScriptItemizeOpenType;
193 SSOT ScriptShapeOpenType;
194 SPOT ScriptPlaceOpenType;
196 inline void init (void)
199 this->ScriptItemizeOpenType = NULL;
200 this->ScriptShapeOpenType = NULL;
201 this->ScriptPlaceOpenType = NULL;
203 hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
206 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
207 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
208 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
210 if (!this->ScriptItemizeOpenType ||
211 !this->ScriptShapeOpenType ||
212 !this->ScriptPlaceOpenType)
214 DEBUG_MSG (UNISCRIBE, NULL, "OpenType versions of functions not found; falling back.");
215 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
216 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
217 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
221 static hb_uniscribe_shaper_funcs_t *uniscribe_funcs;
224 free_uniscribe_funcs (void)
226 free (uniscribe_funcs);
229 static hb_uniscribe_shaper_funcs_t *
230 hb_uniscribe_shaper_get_funcs (void)
233 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
235 if (unlikely (!funcs))
237 funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
238 if (unlikely (!funcs))
243 if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, NULL, funcs)) {
249 atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */
257 struct active_feature_t {
258 OPENTYPE_FEATURE_RECORD rec;
261 static int cmp (const active_feature_t *a, const active_feature_t *b) {
262 return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
263 a->order < b->order ? -1 : a->order > b->order ? 1 :
264 a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
267 bool operator== (const active_feature_t *f) {
268 return cmp (this, f) == 0;
272 struct feature_event_t {
275 active_feature_t feature;
277 static int cmp (const feature_event_t *a, const feature_event_t *b) {
278 return a->index < b->index ? -1 : a->index > b->index ? 1 :
279 a->start < b->start ? -1 : a->start > b->start ? 1 :
280 active_feature_t::cmp (&a->feature, &b->feature);
284 struct range_record_t {
285 TEXTRANGE_PROPERTIES props;
286 unsigned int index_first; /* == start */
287 unsigned int index_last; /* == end - 1 */
290 HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face)
291 HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font)
298 struct hb_uniscribe_shaper_face_data_t {
300 hb_uniscribe_shaper_funcs_t *funcs;
301 wchar_t face_name[LF_FACESIZE];
304 /* face_name should point to a wchar_t[LF_FACESIZE] object. */
306 _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
308 /* We'll create a private name for the font from a UUID using a simple,
309 * somewhat base64-like encoding scheme */
310 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
312 UuidCreate ((UUID*) &id);
313 ASSERT_STATIC (2 + 3 * (16/2) < LF_FACESIZE);
314 unsigned int name_str_len = 0;
315 face_name[name_str_len++] = 'F';
316 face_name[name_str_len++] = '_';
317 unsigned char *p = (unsigned char *) &id;
318 for (unsigned int i = 0; i < 16; i += 2)
320 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
321 * using the bits in groups of 5,5,6 to select chars from enc.
322 * This will generate 24 characters; with the 'F_' prefix we already provided,
323 * the name will be 26 chars (plus the NUL terminator), so will always fit within
324 * face_name (LF_FACESIZE = 32). */
325 face_name[name_str_len++] = enc[p[i] >> 3];
326 face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
327 face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
329 face_name[name_str_len] = 0;
331 *plen = name_str_len;
336 _hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
338 /* Create a copy of the font data, with the 'name' table replaced by a
339 * table that names the font with our private F_* name created above.
340 * For simplicity, we just append a new 'name' table and update the
341 * sfnt directory; the original table is left in place, but unused.
343 * The new table will contain just 5 name IDs: family, style, unique,
344 * full, PS. All of them point to the same name data with our unique name.
347 blob = OT::Sanitizer<OT::OpenTypeFontFile>::sanitize (blob);
349 unsigned int length, new_length, name_str_len;
350 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
352 _hb_generate_unique_face_name (new_name, &name_str_len);
354 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
356 unsigned int name_table_length = OT::name::min_size +
357 ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
358 name_str_len * 2; /* for name data in UTF16BE form */
359 unsigned int name_table_offset = (length + 3) & ~3;
361 new_length = name_table_offset + ((name_table_length + 3) & ~3);
362 void *new_sfnt_data = calloc (1, new_length);
365 hb_blob_destroy (blob);
369 memcpy(new_sfnt_data, orig_sfnt_data, length);
371 OT::name &name = OT::StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
373 name.count.set (ARRAY_LENGTH (name_IDs));
374 name.stringOffset.set (name.get_size ());
375 for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
377 OT::NameRecord &record = name.nameRecord[i];
378 record.platformID.set (3);
379 record.encodingID.set (1);
380 record.languageID.set (0x0409u); /* English */
381 record.nameID.set (name_IDs[i]);
382 record.length.set (name_str_len * 2);
383 record.offset.set (0);
386 /* Copy string data from new_name, converting wchar_t to UTF16BE. */
387 unsigned char *p = &OT::StructAfter<unsigned char> (name);
388 for (unsigned int i = 0; i < name_str_len; i++)
390 *p++ = new_name[i] >> 8;
391 *p++ = new_name[i] & 0xff;
394 /* Adjust name table entry to point to new name table */
395 const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
396 unsigned int face_count = file.get_face_count ();
397 for (unsigned int face_index = 0; face_index < face_count; face_index++)
399 /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
400 * toe-stepping. But we don't really care. */
401 const OT::OpenTypeFontFace &face = file.get_face (face_index);
403 if (face.find_table_index (HB_OT_TAG_name, &index))
405 OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
406 record.checkSum.set_for_data (&name, name_table_length);
407 record.offset.set (name_table_offset);
408 record.length.set (name_table_length);
410 else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
412 free (new_sfnt_data);
413 hb_blob_destroy (blob);
418 /* The checkSumAdjustment field in the 'head' table is now wrong,
419 * but that doesn't actually seem to cause any problems so we don't
422 hb_blob_destroy (blob);
423 return hb_blob_create ((const char *) new_sfnt_data, new_length,
424 HB_MEMORY_MODE_WRITABLE, NULL, free);
427 hb_uniscribe_shaper_face_data_t *
428 _hb_uniscribe_shaper_face_data_create (hb_face_t *face)
430 hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
431 if (unlikely (!data))
434 data->funcs = hb_uniscribe_shaper_get_funcs ();
435 if (unlikely (!data->funcs))
441 hb_blob_t *blob = hb_face_reference_blob (face);
442 if (unlikely (!hb_blob_get_length (blob)))
443 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
445 blob = _hb_rename_font (blob, data->face_name);
446 if (unlikely (!blob))
452 DWORD num_fonts_installed;
453 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, NULL),
454 hb_blob_get_length (blob),
455 0, &num_fonts_installed);
456 if (unlikely (!data->fh))
458 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
467 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
469 RemoveFontMemResourceEx (data->fh);
478 struct hb_uniscribe_shaper_font_data_t {
482 SCRIPT_CACHE script_cache;
486 populate_log_font (LOGFONTW *lf,
489 memset (lf, 0, sizeof (*lf));
490 lf->lfHeight = -font->y_scale;
491 lf->lfCharSet = DEFAULT_CHARSET;
493 hb_face_t *face = font->face;
494 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
496 memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName));
501 hb_uniscribe_shaper_font_data_t *
502 _hb_uniscribe_shaper_font_data_create (hb_font_t *font)
504 if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL;
506 hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
507 if (unlikely (!data))
510 data->hdc = GetDC (NULL);
512 if (unlikely (!populate_log_font (&data->log_font, font))) {
513 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
514 _hb_uniscribe_shaper_font_data_destroy (data);
518 data->hfont = CreateFontIndirectW (&data->log_font);
519 if (unlikely (!data->hfont)) {
520 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
521 _hb_uniscribe_shaper_font_data_destroy (data);
525 if (!SelectObject (data->hdc, data->hfont)) {
526 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
527 _hb_uniscribe_shaper_font_data_destroy (data);
535 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
538 ReleaseDC (NULL, data->hdc);
540 DeleteObject (data->hfont);
541 if (data->script_cache)
542 ScriptFreeCache (&data->script_cache);
547 hb_uniscribe_font_get_logfontw (hb_font_t *font)
549 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
550 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
551 return &font_data->log_font;
555 hb_uniscribe_font_get_hfont (hb_font_t *font)
557 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
558 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
559 return font_data->hfont;
564 * shaper shape_plan data
567 struct hb_uniscribe_shaper_shape_plan_data_t {};
569 hb_uniscribe_shaper_shape_plan_data_t *
570 _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
571 const hb_feature_t *user_features HB_UNUSED,
572 unsigned int num_user_features HB_UNUSED)
574 return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
578 _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED)
589 _hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
592 const hb_feature_t *features,
593 unsigned int num_features)
595 hb_face_t *face = font->face;
596 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
597 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
598 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
603 hb_auto_array_t<OPENTYPE_FEATURE_RECORD> feature_records;
604 hb_auto_array_t<range_record_t> range_records;
607 /* Sort features by start/end events. */
608 hb_auto_array_t<feature_event_t> feature_events;
609 for (unsigned int i = 0; i < num_features; i++)
611 active_feature_t feature;
612 feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
613 feature.rec.lParameter = features[i].value;
616 feature_event_t *event;
618 event = feature_events.push ();
619 if (unlikely (!event))
621 event->index = features[i].start;
623 event->feature = feature;
625 event = feature_events.push ();
626 if (unlikely (!event))
628 event->index = features[i].end;
629 event->start = false;
630 event->feature = feature;
632 feature_events.qsort ();
633 /* Add a strategic final event. */
635 active_feature_t feature;
636 feature.rec.tagFeature = 0;
637 feature.rec.lParameter = 0;
638 feature.order = num_features + 1;
640 feature_event_t *event = feature_events.push ();
641 if (unlikely (!event))
643 event->index = 0; /* This value does magic. */
644 event->start = false;
645 event->feature = feature;
648 /* Scan events and save features for each range. */
649 hb_auto_array_t<active_feature_t> active_features;
650 unsigned int last_index = 0;
651 for (unsigned int i = 0; i < feature_events.len; i++)
653 feature_event_t *event = &feature_events[i];
655 if (event->index != last_index)
657 /* Save a snapshot of active features and the range. */
658 range_record_t *range = range_records.push ();
659 if (unlikely (!range))
662 unsigned int offset = feature_records.len;
664 active_features.qsort ();
665 for (unsigned int j = 0; j < active_features.len; j++)
667 if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.len - 1].tagFeature)
669 OPENTYPE_FEATURE_RECORD *feature = feature_records.push ();
670 if (unlikely (!feature))
672 *feature = active_features[j].rec;
676 /* Overrides value for existing feature. */
677 feature_records[feature_records.len - 1].lParameter = active_features[j].rec.lParameter;
681 /* Will convert to pointer after all is ready, since feature_records.array
682 * may move as we grow it. */
683 range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
684 range->props.cotfRecords = feature_records.len - offset;
685 range->index_first = last_index;
686 range->index_last = event->index - 1;
688 last_index = event->index;
692 active_feature_t *feature = active_features.push ();
693 if (unlikely (!feature))
695 *feature = event->feature;
697 active_feature_t *feature = active_features.find (&event->feature);
699 active_features.remove (feature - active_features.array);
703 if (!range_records.len) /* No active feature found. */
706 /* Fixup the pointers. */
707 for (unsigned int i = 0; i < range_records.len; i++)
709 range_record_t *range = &range_records[i];
710 range->props.potfRecords = feature_records.array + reinterpret_cast<uintptr_t> (range->props.potfRecords);
721 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
729 unsigned int scratch_size;
730 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
732 #define ALLOCATE_ARRAY(Type, name, len) \
733 Type *name = (Type *) scratch; \
735 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
736 assert (_consumed <= scratch_size); \
737 scratch += _consumed; \
738 scratch_size -= _consumed; \
741 #define utf16_index() var1.u32
743 ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
745 unsigned int chars_len = 0;
746 for (unsigned int i = 0; i < buffer->len; i++)
748 hb_codepoint_t c = buffer->info[i].codepoint;
749 buffer->info[i].utf16_index() = chars_len;
750 if (likely (c <= 0xFFFFu))
751 pchars[chars_len++] = c;
752 else if (unlikely (c > 0x10FFFFu))
753 pchars[chars_len++] = 0xFFFDu;
755 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
756 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1 << 10) - 1));
760 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
761 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
765 /* Need log_clusters to assign features. */
767 for (unsigned int i = 0; i < buffer->len; i++)
769 hb_codepoint_t c = buffer->info[i].codepoint;
770 unsigned int cluster = buffer->info[i].cluster;
771 log_clusters[chars_len++] = cluster;
772 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
773 log_clusters[chars_len++] = cluster; /* Surrogates. */
777 /* The -2 in the following is to compensate for possible
778 * alignment needed after the WORD array. sizeof(WORD) == 2. */
779 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
781 sizeof (SCRIPT_GLYPHPROP) +
786 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
787 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
788 ALLOCATE_ARRAY (int, advances, glyphs_size);
789 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
790 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
793 * We can't touch the contents of glyph_props. Our fallback
794 * implementations of Shape and Place functions use that buffer
795 * by casting it to a different type. It works because they
796 * both agree about it, but if we want to access it here we
797 * need address that issue first.
800 #undef ALLOCATE_ARRAY
802 #define MAX_ITEMS 256
804 SCRIPT_ITEM items[MAX_ITEMS + 1];
805 SCRIPT_CONTROL bidi_control = {0};
806 SCRIPT_STATE bidi_state = {0};
807 ULONG script_tags[MAX_ITEMS];
810 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
811 //bidi_control.fMergeNeutralItems = true;
812 *(uint32_t*)&bidi_control |= 1<<24;
814 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
815 bidi_state.fOverrideDirection = 1;
817 hr = funcs->ScriptItemizeOpenType (pchars,
825 if (unlikely (FAILED (hr)))
826 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
830 OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
831 hb_auto_array_t<TEXTRANGE_PROPERTIES*> range_properties;
832 hb_auto_array_t<int> range_char_counts;
834 unsigned int glyphs_offset = 0;
835 unsigned int glyphs_len;
836 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
837 for (unsigned int i = 0; i < item_count; i++)
839 unsigned int chars_offset = items[i].iCharPos;
840 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
844 range_properties.shrink (0);
845 range_char_counts.shrink (0);
847 range_record_t *last_range = &range_records[0];
849 for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
851 range_record_t *range = last_range;
852 while (log_clusters[k] < range->index_first)
854 while (log_clusters[k] > range->index_last)
856 if (!range_properties.len ||
857 &range->props != range_properties[range_properties.len - 1])
859 TEXTRANGE_PROPERTIES **props = range_properties.push ();
860 int *c = range_char_counts.push ();
861 if (unlikely (!props || !c))
863 range_properties.shrink (0);
864 range_char_counts.shrink (0);
867 *props = &range->props;
872 range_char_counts[range_char_counts.len - 1]++;
879 /* Asking for glyphs in logical order circumvents at least
880 * one bug in Uniscribe. */
881 items[i].a.fLogicalOrder = true;
884 hr = funcs->ScriptShapeOpenType (font_data->hdc,
885 &font_data->script_cache,
889 range_char_counts.array,
890 range_properties.array,
891 range_properties.len,
892 pchars + chars_offset,
894 glyphs_size - glyphs_offset,
896 log_clusters + chars_offset,
897 char_props + chars_offset,
898 glyphs + glyphs_offset,
899 glyph_props + glyphs_offset,
900 (int *) &glyphs_len);
902 if (unlikely (items[i].a.fNoGlyphIndex))
903 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
904 if (unlikely (hr == E_OUTOFMEMORY))
906 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
907 FAIL ("Buffer resize failed");
910 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
912 if (items[i].a.eScript == SCRIPT_UNDEFINED)
913 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
914 items[i].a.eScript = SCRIPT_UNDEFINED;
917 if (unlikely (FAILED (hr)))
919 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
922 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
923 log_clusters[j] += glyphs_offset;
925 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
926 &font_data->script_cache,
930 range_char_counts.array,
931 range_properties.array,
932 range_properties.len,
933 pchars + chars_offset,
934 log_clusters + chars_offset,
935 char_props + chars_offset,
937 glyphs + glyphs_offset,
938 glyph_props + glyphs_offset,
941 advances + glyphs_offset,
942 offsets + glyphs_offset,
944 if (unlikely (FAILED (hr)))
945 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
947 if (DEBUG_ENABLED (UNISCRIBE))
948 fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
951 items[i].a.fLayoutRTL,
952 items[i].a.fLogicalOrder,
953 HB_UNTAG (hb_uint32_swap (script_tags[i])));
955 glyphs_offset += glyphs_len;
957 glyphs_len = glyphs_offset;
959 /* Ok, we've got everything we need, now compose output buffer,
960 * very, *very*, carefully! */
962 /* Calculate visual-clusters. That's what we ship. */
963 for (unsigned int i = 0; i < glyphs_len; i++)
964 vis_clusters[i] = -1;
965 for (unsigned int i = 0; i < buffer->len; i++) {
966 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
967 *p = MIN (*p, buffer->info[i].cluster);
969 for (unsigned int i = 1; i < glyphs_len; i++)
970 if (vis_clusters[i] == -1)
971 vis_clusters[i] = vis_clusters[i - 1];
975 if (unlikely (!buffer->ensure (glyphs_len)))
976 FAIL ("Buffer in error");
980 /* Set glyph infos */
982 for (unsigned int i = 0; i < glyphs_len; i++)
984 hb_glyph_info_t *info = &buffer->info[buffer->len++];
986 info->codepoint = glyphs[i];
987 info->cluster = vis_clusters[i];
989 /* The rest is crap. Let's store position info there for now. */
990 info->mask = advances[i];
991 info->var1.u32 = offsets[i].du;
992 info->var2.u32 = offsets[i].dv;
995 /* Set glyph positions */
996 buffer->clear_positions ();
997 for (unsigned int i = 0; i < glyphs_len; i++)
999 hb_glyph_info_t *info = &buffer->info[i];
1000 hb_glyph_position_t *pos = &buffer->pos[i];
1003 pos->x_advance = info->mask;
1004 pos->x_offset = backward ? -info->var1.u32 : info->var1.u32;
1005 pos->y_offset = info->var2.u32;
1009 hb_buffer_reverse (buffer);