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
32 #error "Cannot compile 'uniscribe' shaper with HB_NO_OT_TAG."
35 #include "hb-shaper-impl.hh"
41 #ifndef E_NOT_SUFFICIENT_BUFFER
42 #define E_NOT_SUFFICIENT_BUFFER HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)
45 #include "hb-uniscribe.h"
47 #include "hb-open-file.hh"
48 #include "hb-ot-name-table.hh"
49 #include "hb-ot-layout.h"
53 * SECTION:hb-uniscribe
54 * @title: hb-uniscribe
55 * @short_description: Windows integration
56 * @include: hb-uniscribe.h
58 * Functions for using HarfBuzz with Windows fonts.
61 typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
62 const WCHAR *pwcInChars,
65 const SCRIPT_CONTROL *psControl,
66 const SCRIPT_STATE *psState,
68 OPENTYPE_TAG *pScriptTags,
72 typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
76 OPENTYPE_TAG tagScript,
77 OPENTYPE_TAG tagLangSys,
79 TEXTRANGE_PROPERTIES **rpRangeProperties,
81 const WCHAR *pwcChars,
85 SCRIPT_CHARPROP *pCharProps,
87 SCRIPT_GLYPHPROP *pOutGlyphProps,
91 typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
95 OPENTYPE_TAG tagScript,
96 OPENTYPE_TAG tagLangSys,
98 TEXTRANGE_PROPERTIES **rpRangeProperties,
100 const WCHAR *pwcChars,
102 SCRIPT_CHARPROP *pCharProps,
104 const WORD *pwGlyphs,
105 const SCRIPT_GLYPHPROP *pGlyphProps,
113 /* Fallback implementations. */
115 static HRESULT WINAPI
116 hb_ScriptItemizeOpenType(
117 const WCHAR *pwcInChars,
120 const SCRIPT_CONTROL *psControl,
121 const SCRIPT_STATE *psState,
123 OPENTYPE_TAG *pScriptTags,
128 return ScriptItemize (pwcInChars,
138 static HRESULT WINAPI
139 hb_ScriptShapeOpenType(
142 SCRIPT_ANALYSIS *psa,
143 OPENTYPE_TAG tagScript,
144 OPENTYPE_TAG tagLangSys,
146 TEXTRANGE_PROPERTIES **rpRangeProperties,
148 const WCHAR *pwcChars,
152 SCRIPT_CHARPROP *pCharProps,
154 SCRIPT_GLYPHPROP *pOutGlyphProps,
158 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
159 return ScriptShape (hdc,
171 static HRESULT WINAPI
172 hb_ScriptPlaceOpenType(
175 SCRIPT_ANALYSIS *psa,
176 OPENTYPE_TAG tagScript,
177 OPENTYPE_TAG tagLangSys,
179 TEXTRANGE_PROPERTIES **rpRangeProperties,
181 const WCHAR *pwcChars,
183 SCRIPT_CHARPROP *pCharProps,
185 const WORD *pwGlyphs,
186 const SCRIPT_GLYPHPROP *pGlyphProps,
193 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
194 return ScriptPlace (hdc,
206 struct hb_uniscribe_shaper_funcs_t
208 SIOT ScriptItemizeOpenType;
209 SSOT ScriptShapeOpenType;
210 SPOT ScriptPlaceOpenType;
215 this->ScriptItemizeOpenType = nullptr;
216 this->ScriptShapeOpenType = nullptr;
217 this->ScriptPlaceOpenType = nullptr;
219 hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
222 #pragma GCC diagnostic push
223 #pragma GCC diagnostic ignored "-Wcast-function-type"
224 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
225 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
226 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
227 #pragma GCC diagnostic pop
229 if (!this->ScriptItemizeOpenType ||
230 !this->ScriptShapeOpenType ||
231 !this->ScriptPlaceOpenType)
233 DEBUG_MSG (UNISCRIBE, nullptr, "OpenType versions of functions not found; falling back.");
234 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
235 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
236 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
242 static void free_static_uniscribe_shaper_funcs ();
245 static struct hb_uniscribe_shaper_funcs_lazy_loader_t : hb_lazy_loader_t<hb_uniscribe_shaper_funcs_t,
246 hb_uniscribe_shaper_funcs_lazy_loader_t>
248 static hb_uniscribe_shaper_funcs_t *create ()
250 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
251 if (unlikely (!funcs))
257 atexit (free_static_uniscribe_shaper_funcs);
262 static void destroy (hb_uniscribe_shaper_funcs_t *p)
266 static hb_uniscribe_shaper_funcs_t *get_null ()
270 } static_uniscribe_shaper_funcs;
274 void free_static_uniscribe_shaper_funcs ()
276 static_uniscribe_shaper_funcs.free_instance ();
280 static hb_uniscribe_shaper_funcs_t *
281 hb_uniscribe_shaper_get_funcs ()
283 return static_uniscribe_shaper_funcs.get_unconst ();
287 struct active_feature_t {
288 OPENTYPE_FEATURE_RECORD rec;
291 HB_INTERNAL static int cmp (const void *pa, const void *pb) {
292 const active_feature_t *a = (const active_feature_t *) pa;
293 const active_feature_t *b = (const active_feature_t *) pb;
294 return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
295 a->order < b->order ? -1 : a->order > b->order ? 1 :
296 a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
299 bool operator== (const active_feature_t *f)
300 { return cmp (this, f) == 0; }
303 struct feature_event_t {
306 active_feature_t feature;
308 HB_INTERNAL static int cmp (const void *pa, const void *pb)
310 const feature_event_t *a = (const feature_event_t *) pa;
311 const feature_event_t *b = (const feature_event_t *) pb;
312 return a->index < b->index ? -1 : a->index > b->index ? 1 :
313 a->start < b->start ? -1 : a->start > b->start ? 1 :
314 active_feature_t::cmp (&a->feature, &b->feature);
318 struct range_record_t {
319 TEXTRANGE_PROPERTIES props;
320 unsigned int index_first; /* == start */
321 unsigned int index_last; /* == end - 1 */
329 struct hb_uniscribe_face_data_t {
331 hb_uniscribe_shaper_funcs_t *funcs;
332 wchar_t face_name[LF_FACESIZE];
335 /* face_name should point to a wchar_t[LF_FACESIZE] object. */
337 _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
339 /* We'll create a private name for the font from a UUID using a simple,
340 * somewhat base64-like encoding scheme */
341 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
343 UuidCreate ((UUID*) &id);
344 static_assert ((2 + 3 * (16/2) < LF_FACESIZE), "");
345 unsigned int name_str_len = 0;
346 face_name[name_str_len++] = 'F';
347 face_name[name_str_len++] = '_';
348 unsigned char *p = (unsigned char *) &id;
349 for (unsigned int i = 0; i < 16; i += 2)
351 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
352 * using the bits in groups of 5,5,6 to select chars from enc.
353 * This will generate 24 characters; with the 'F_' prefix we already provided,
354 * the name will be 26 chars (plus the NUL terminator), so will always fit within
355 * face_name (LF_FACESIZE = 32). */
356 face_name[name_str_len++] = enc[p[i] >> 3];
357 face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
358 face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
360 face_name[name_str_len] = 0;
362 *plen = name_str_len;
367 _hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
369 /* Create a copy of the font data, with the 'name' table replaced by a
370 * table that names the font with our private F_* name created above.
371 * For simplicity, we just append a new 'name' table and update the
372 * sfnt directory; the original table is left in place, but unused.
374 * The new table will contain just 5 name IDs: family, style, unique,
375 * full, PS. All of them point to the same name data with our unique name.
378 blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (blob);
380 unsigned int length, new_length, name_str_len;
381 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
383 _hb_generate_unique_face_name (new_name, &name_str_len);
385 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
387 unsigned int name_table_length = OT::name::min_size +
388 ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
389 name_str_len * 2; /* for name data in UTF16BE form */
390 unsigned int padded_name_table_length = ((name_table_length + 3) & ~3);
391 unsigned int name_table_offset = (length + 3) & ~3;
393 new_length = name_table_offset + padded_name_table_length;
394 void *new_sfnt_data = calloc (1, new_length);
397 hb_blob_destroy (blob);
401 memcpy(new_sfnt_data, orig_sfnt_data, length);
403 OT::name &name = StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
405 name.count = ARRAY_LENGTH (name_IDs);
406 name.stringOffset = name.get_size ();
407 for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
409 OT::NameRecord &record = name.nameRecordZ[i];
410 record.platformID = 3;
411 record.encodingID = 1;
412 record.languageID = 0x0409u; /* English */
413 record.nameID = name_IDs[i];
414 record.length = name_str_len * 2;
418 /* Copy string data from new_name, converting wchar_t to UTF16BE. */
419 unsigned char *p = &StructAfter<unsigned char> (name);
420 for (unsigned int i = 0; i < name_str_len; i++)
422 *p++ = new_name[i] >> 8;
423 *p++ = new_name[i] & 0xff;
426 /* Adjust name table entry to point to new name table */
427 const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
428 unsigned int face_count = file.get_face_count ();
429 for (unsigned int face_index = 0; face_index < face_count; face_index++)
431 /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
432 * toe-stepping. But we don't really care. */
433 const OT::OpenTypeFontFace &face = file.get_face (face_index);
435 if (face.find_table_index (HB_OT_TAG_name, &index))
437 OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
438 record.checkSum.set_for_data (&name, padded_name_table_length);
439 record.offset = name_table_offset;
440 record.length = name_table_length;
442 else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
444 free (new_sfnt_data);
445 hb_blob_destroy (blob);
450 /* The checkSumAdjustment field in the 'head' table is now wrong,
451 * but that doesn't actually seem to cause any problems so we don't
454 hb_blob_destroy (blob);
455 return hb_blob_create ((const char *) new_sfnt_data, new_length,
456 HB_MEMORY_MODE_WRITABLE, new_sfnt_data, free);
459 hb_uniscribe_face_data_t *
460 _hb_uniscribe_shaper_face_data_create (hb_face_t *face)
462 hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
463 if (unlikely (!data))
466 data->funcs = hb_uniscribe_shaper_get_funcs ();
467 if (unlikely (!data->funcs))
473 hb_blob_t *blob = hb_face_reference_blob (face);
474 if (unlikely (!hb_blob_get_length (blob)))
475 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
477 blob = _hb_rename_font (blob, data->face_name);
478 if (unlikely (!blob))
484 DWORD num_fonts_installed;
485 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, nullptr),
486 hb_blob_get_length (blob),
487 0, &num_fonts_installed);
488 if (unlikely (!data->fh))
490 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
499 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_face_data_t *data)
501 RemoveFontMemResourceEx (data->fh);
510 struct hb_uniscribe_font_data_t
513 mutable LOGFONTW log_font;
515 mutable SCRIPT_CACHE script_cache;
516 double x_mult, y_mult; /* From LOGFONT space to HB space. */
520 populate_log_font (LOGFONTW *lf,
522 unsigned int font_size)
524 memset (lf, 0, sizeof (*lf));
525 lf->lfHeight = - (int) font_size;
526 lf->lfCharSet = DEFAULT_CHARSET;
528 memcpy (lf->lfFaceName, font->face->data.uniscribe->face_name, sizeof (lf->lfFaceName));
533 hb_uniscribe_font_data_t *
534 _hb_uniscribe_shaper_font_data_create (hb_font_t *font)
536 hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
537 if (unlikely (!data))
540 int font_size = font->face->get_upem (); /* Default... */
541 /* No idea if the following is even a good idea. */
543 font_size = font->y_ppem;
546 font_size = -font_size;
547 data->x_mult = (double) font->x_scale / font_size;
548 data->y_mult = (double) font->y_scale / font_size;
550 data->hdc = GetDC (nullptr);
552 if (unlikely (!populate_log_font (&data->log_font, font, font_size))) {
553 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
554 _hb_uniscribe_shaper_font_data_destroy (data);
558 data->hfont = CreateFontIndirectW (&data->log_font);
559 if (unlikely (!data->hfont)) {
560 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
561 _hb_uniscribe_shaper_font_data_destroy (data);
565 if (!SelectObject (data->hdc, data->hfont)) {
566 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
567 _hb_uniscribe_shaper_font_data_destroy (data);
575 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_font_data_t *data)
578 ReleaseDC (nullptr, data->hdc);
580 DeleteObject (data->hfont);
581 if (data->script_cache)
582 ScriptFreeCache (&data->script_cache);
587 * hb_uniscribe_font_get_logfontw:
588 * @font: The #hb_font_t to work upon
590 * Fetches the LOGFONTW structure that corresponds to the
591 * specified #hb_font_t font.
593 * Return value: a pointer to the LOGFONTW retrieved
597 hb_uniscribe_font_get_logfontw (hb_font_t *font)
599 const hb_uniscribe_font_data_t *data = font->data.uniscribe;
600 return data ? &data->log_font : nullptr;
604 * hb_uniscribe_font_get_hfont:
605 * @font: The #hb_font_t to work upon
607 * Fetches the HFONT handle that corresponds to the
608 * specified #hb_font_t font.
610 * Return value: the HFONT retreieved
614 hb_uniscribe_font_get_hfont (hb_font_t *font)
616 const hb_uniscribe_font_data_t *data = font->data.uniscribe;
617 return data ? data->hfont : nullptr;
627 _hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
630 const hb_feature_t *features,
631 unsigned int num_features)
633 hb_face_t *face = font->face;
634 const hb_uniscribe_face_data_t *face_data = face->data.uniscribe;
635 const hb_uniscribe_font_data_t *font_data = font->data.uniscribe;
636 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
641 hb_vector_t<OPENTYPE_FEATURE_RECORD> feature_records;
642 hb_vector_t<range_record_t> range_records;
645 /* Sort features by start/end events. */
646 hb_vector_t<feature_event_t> feature_events;
647 for (unsigned int i = 0; i < num_features; i++)
649 active_feature_t feature;
650 feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
651 feature.rec.lParameter = features[i].value;
654 feature_event_t *event;
656 event = feature_events.push ();
657 event->index = features[i].start;
659 event->feature = feature;
661 event = feature_events.push ();
662 event->index = features[i].end;
663 event->start = false;
664 event->feature = feature;
666 feature_events.qsort ();
667 /* Add a strategic final event. */
669 active_feature_t feature;
670 feature.rec.tagFeature = 0;
671 feature.rec.lParameter = 0;
672 feature.order = num_features + 1;
674 feature_event_t *event = feature_events.push ();
675 event->index = 0; /* This value does magic. */
676 event->start = false;
677 event->feature = feature;
680 /* Scan events and save features for each range. */
681 hb_vector_t<active_feature_t> active_features;
682 unsigned int last_index = 0;
683 for (unsigned int i = 0; i < feature_events.length; i++)
685 feature_event_t *event = &feature_events[i];
687 if (event->index != last_index)
689 /* Save a snapshot of active features and the range. */
690 range_record_t *range = range_records.push ();
692 unsigned int offset = feature_records.length;
694 active_features.qsort ();
695 for (unsigned int j = 0; j < active_features.length; j++)
697 if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.length - 1].tagFeature)
699 feature_records.push (active_features[j].rec);
703 /* Overrides value for existing feature. */
704 feature_records[feature_records.length - 1].lParameter = active_features[j].rec.lParameter;
708 /* Will convert to pointer after all is ready, since feature_records.array
709 * may move as we grow it. */
710 range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
711 range->props.cotfRecords = feature_records.length - offset;
712 range->index_first = last_index;
713 range->index_last = event->index - 1;
715 last_index = event->index;
720 active_features.push (event->feature);
724 active_feature_t *feature = active_features.find (&event->feature);
726 active_features.remove (feature - active_features.arrayZ);
730 if (!range_records.length) /* No active feature found. */
733 /* Fixup the pointers. */
734 for (unsigned int i = 0; i < range_records.length; i++)
736 range_record_t *range = &range_records[i];
737 range->props.potfRecords = (OPENTYPE_FEATURE_RECORD *) feature_records + reinterpret_cast<uintptr_t> (range->props.potfRecords);
743 DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \
751 unsigned int scratch_size;
752 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
754 #define ALLOCATE_ARRAY(Type, name, len) \
755 Type *name = (Type *) scratch; \
757 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
758 assert (_consumed <= scratch_size); \
759 scratch += _consumed; \
760 scratch_size -= _consumed; \
763 #define utf16_index() var1.u32
765 ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
767 unsigned int chars_len = 0;
768 for (unsigned int i = 0; i < buffer->len; i++)
770 hb_codepoint_t c = buffer->info[i].codepoint;
771 buffer->info[i].utf16_index() = chars_len;
772 if (likely (c <= 0xFFFFu))
773 pchars[chars_len++] = c;
774 else if (unlikely (c > 0x10FFFFu))
775 pchars[chars_len++] = 0xFFFDu;
777 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
778 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
782 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
783 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
787 /* Need log_clusters to assign features. */
789 for (unsigned int i = 0; i < buffer->len; i++)
791 hb_codepoint_t c = buffer->info[i].codepoint;
792 unsigned int cluster = buffer->info[i].cluster;
793 log_clusters[chars_len++] = cluster;
794 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
795 log_clusters[chars_len++] = cluster; /* Surrogates. */
799 /* The -2 in the following is to compensate for possible
800 * alignment needed after the WORD array. sizeof(WORD) == 2. */
801 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
803 sizeof (SCRIPT_GLYPHPROP) +
808 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
809 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
810 ALLOCATE_ARRAY (int, advances, glyphs_size);
811 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
812 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
815 * We can't touch the contents of glyph_props. Our fallback
816 * implementations of Shape and Place functions use that buffer
817 * by casting it to a different type. It works because they
818 * both agree about it, but if we want to access it here we
819 * need address that issue first.
822 #undef ALLOCATE_ARRAY
824 #define MAX_ITEMS 256
826 SCRIPT_ITEM items[MAX_ITEMS + 1];
827 SCRIPT_CONTROL bidi_control = {0};
828 SCRIPT_STATE bidi_state = {0};
829 ULONG script_tags[MAX_ITEMS];
832 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
833 //bidi_control.fMergeNeutralItems = true;
834 *(uint32_t*)&bidi_control |= 1u<<24;
836 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
837 bidi_state.fOverrideDirection = 1;
839 hr = funcs->ScriptItemizeOpenType (pchars,
847 if (unlikely (FAILED (hr)))
848 FAIL ("ScriptItemizeOpenType() failed: 0x%08lx", hr);
853 unsigned int lang_count = 1;
854 hb_ot_tags_from_script_and_language (buffer->props.script,
855 buffer->props.language,
857 &lang_count, &lang_tag);
858 OPENTYPE_TAG language_tag = hb_uint32_swap (lang_count ? lang_tag : HB_TAG_NONE);
859 hb_vector_t<TEXTRANGE_PROPERTIES*> range_properties;
860 hb_vector_t<int> range_char_counts;
862 unsigned int glyphs_offset = 0;
863 unsigned int glyphs_len;
864 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
865 for (int i = 0; i < item_count; i++)
867 unsigned int chars_offset = items[i].iCharPos;
868 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
872 range_properties.shrink (0);
873 range_char_counts.shrink (0);
875 range_record_t *last_range = &range_records[0];
877 for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
879 range_record_t *range = last_range;
880 while (log_clusters[k] < range->index_first)
882 while (log_clusters[k] > range->index_last)
884 if (!range_properties.length ||
885 &range->props != range_properties[range_properties.length - 1])
887 TEXTRANGE_PROPERTIES **props = range_properties.push ();
888 int *c = range_char_counts.push ();
889 if (unlikely (!props || !c))
891 range_properties.shrink (0);
892 range_char_counts.shrink (0);
895 *props = &range->props;
900 range_char_counts[range_char_counts.length - 1]++;
907 /* Asking for glyphs in logical order circumvents at least
908 * one bug in Uniscribe. */
909 items[i].a.fLogicalOrder = true;
912 hr = funcs->ScriptShapeOpenType (font_data->hdc,
913 &font_data->script_cache,
917 range_char_counts.arrayZ,
918 range_properties.arrayZ,
919 range_properties.length,
920 pchars + chars_offset,
922 glyphs_size - glyphs_offset,
924 log_clusters + chars_offset,
925 char_props + chars_offset,
926 glyphs + glyphs_offset,
927 glyph_props + glyphs_offset,
928 (int *) &glyphs_len);
930 if (unlikely (items[i].a.fNoGlyphIndex))
931 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
932 if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
934 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
935 FAIL ("Buffer resize failed");
938 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
940 if (items[i].a.eScript == SCRIPT_UNDEFINED)
941 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
942 items[i].a.eScript = SCRIPT_UNDEFINED;
945 if (unlikely (FAILED (hr)))
947 FAIL ("ScriptShapeOpenType() failed: 0x%08lx", hr);
950 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
951 log_clusters[j] += glyphs_offset;
953 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
954 &font_data->script_cache,
958 range_char_counts.arrayZ,
959 range_properties.arrayZ,
960 range_properties.length,
961 pchars + chars_offset,
962 log_clusters + chars_offset,
963 char_props + chars_offset,
965 glyphs + glyphs_offset,
966 glyph_props + glyphs_offset,
969 advances + glyphs_offset,
970 offsets + glyphs_offset,
972 if (unlikely (FAILED (hr)))
973 FAIL ("ScriptPlaceOpenType() failed: 0x%08lx", hr);
975 if (DEBUG_ENABLED (UNISCRIBE))
976 fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
979 items[i].a.fLayoutRTL,
980 items[i].a.fLogicalOrder,
981 HB_UNTAG (hb_uint32_swap (script_tags[i])));
983 glyphs_offset += glyphs_len;
985 glyphs_len = glyphs_offset;
987 /* Ok, we've got everything we need, now compose output buffer,
988 * very, *very*, carefully! */
990 /* Calculate visual-clusters. That's what we ship. */
991 for (unsigned int i = 0; i < glyphs_len; i++)
992 vis_clusters[i] = (uint32_t) -1;
993 for (unsigned int i = 0; i < buffer->len; i++) {
994 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
995 *p = hb_min (*p, buffer->info[i].cluster);
997 for (unsigned int i = 1; i < glyphs_len; i++)
998 if (vis_clusters[i] == (uint32_t) -1)
999 vis_clusters[i] = vis_clusters[i - 1];
1003 if (unlikely (!buffer->ensure (glyphs_len)))
1004 FAIL ("Buffer in error");
1008 /* Set glyph infos */
1010 for (unsigned int i = 0; i < glyphs_len; i++)
1012 hb_glyph_info_t *info = &buffer->info[buffer->len++];
1014 info->codepoint = glyphs[i];
1015 info->cluster = vis_clusters[i];
1017 /* The rest is crap. Let's store position info there for now. */
1018 info->mask = advances[i];
1019 info->var1.i32 = offsets[i].du;
1020 info->var2.i32 = offsets[i].dv;
1023 /* Set glyph positions */
1024 buffer->clear_positions ();
1025 double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
1026 for (unsigned int i = 0; i < glyphs_len; i++)
1028 hb_glyph_info_t *info = &buffer->info[i];
1029 hb_glyph_position_t *pos = &buffer->pos[i];
1032 pos->x_advance = x_mult * (int32_t) info->mask;
1033 pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
1034 pos->y_offset = y_mult * info->var2.i32;
1038 hb_buffer_reverse (buffer);
1040 buffer->unsafe_to_break_all ();