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
28 #include "hb-shaper-impl.hh"
34 #include "hb-uniscribe.h"
36 #include "hb-open-file.hh"
37 #include "hb-ot-name-table.hh"
38 #include "hb-ot-layout.h"
42 * SECTION:hb-uniscribe
43 * @title: hb-uniscribe
44 * @short_description: Windows integration
45 * @include: hb-uniscribe.h
47 * Functions for using HarfBuzz with the Windows fonts.
51 static inline uint16_t hb_uint16_swap (const uint16_t v)
52 { return (v >> 8) | (v << 8); }
53 static inline uint32_t hb_uint32_swap (const uint32_t v)
54 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
57 typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
58 const WCHAR *pwcInChars,
61 const SCRIPT_CONTROL *psControl,
62 const SCRIPT_STATE *psState,
64 OPENTYPE_TAG *pScriptTags,
68 typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
72 OPENTYPE_TAG tagScript,
73 OPENTYPE_TAG tagLangSys,
75 TEXTRANGE_PROPERTIES **rpRangeProperties,
77 const WCHAR *pwcChars,
81 SCRIPT_CHARPROP *pCharProps,
83 SCRIPT_GLYPHPROP *pOutGlyphProps,
87 typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
91 OPENTYPE_TAG tagScript,
92 OPENTYPE_TAG tagLangSys,
94 TEXTRANGE_PROPERTIES **rpRangeProperties,
96 const WCHAR *pwcChars,
98 SCRIPT_CHARPROP *pCharProps,
100 const WORD *pwGlyphs,
101 const SCRIPT_GLYPHPROP *pGlyphProps,
109 /* Fallback implementations. */
111 static HRESULT WINAPI
112 hb_ScriptItemizeOpenType(
113 const WCHAR *pwcInChars,
116 const SCRIPT_CONTROL *psControl,
117 const SCRIPT_STATE *psState,
119 OPENTYPE_TAG *pScriptTags,
124 return ScriptItemize (pwcInChars,
134 static HRESULT WINAPI
135 hb_ScriptShapeOpenType(
138 SCRIPT_ANALYSIS *psa,
139 OPENTYPE_TAG tagScript,
140 OPENTYPE_TAG tagLangSys,
142 TEXTRANGE_PROPERTIES **rpRangeProperties,
144 const WCHAR *pwcChars,
148 SCRIPT_CHARPROP *pCharProps,
150 SCRIPT_GLYPHPROP *pOutGlyphProps,
154 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
155 return ScriptShape (hdc,
167 static HRESULT WINAPI
168 hb_ScriptPlaceOpenType(
171 SCRIPT_ANALYSIS *psa,
172 OPENTYPE_TAG tagScript,
173 OPENTYPE_TAG tagLangSys,
175 TEXTRANGE_PROPERTIES **rpRangeProperties,
177 const WCHAR *pwcChars,
179 SCRIPT_CHARPROP *pCharProps,
181 const WORD *pwGlyphs,
182 const SCRIPT_GLYPHPROP *pGlyphProps,
189 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
190 return ScriptPlace (hdc,
202 struct hb_uniscribe_shaper_funcs_t
204 SIOT ScriptItemizeOpenType;
205 SSOT ScriptShapeOpenType;
206 SPOT ScriptPlaceOpenType;
211 this->ScriptItemizeOpenType = nullptr;
212 this->ScriptShapeOpenType = nullptr;
213 this->ScriptPlaceOpenType = nullptr;
215 hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
218 #pragma GCC diagnostic push
219 #pragma GCC diagnostic ignored "-Wcast-function-type"
220 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
221 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
222 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
223 #pragma GCC diagnostic pop
225 if (!this->ScriptItemizeOpenType ||
226 !this->ScriptShapeOpenType ||
227 !this->ScriptPlaceOpenType)
229 DEBUG_MSG (UNISCRIBE, nullptr, "OpenType versions of functions not found; falling back.");
230 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
231 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
232 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
238 static void free_static_uniscribe_shaper_funcs ();
240 static struct hb_uniscribe_shaper_funcs_lazy_loader_t : hb_lazy_loader_t<hb_uniscribe_shaper_funcs_t,
241 hb_uniscribe_shaper_funcs_lazy_loader_t>
243 static hb_uniscribe_shaper_funcs_t *create ()
245 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
246 if (unlikely (!funcs))
252 atexit (free_static_uniscribe_shaper_funcs);
257 static void destroy (hb_uniscribe_shaper_funcs_t *p)
261 static hb_uniscribe_shaper_funcs_t *get_null ()
265 } static_uniscribe_shaper_funcs;
269 void free_static_uniscribe_shaper_funcs ()
271 static_uniscribe_shaper_funcs.free_instance ();
275 static hb_uniscribe_shaper_funcs_t *
276 hb_uniscribe_shaper_get_funcs ()
278 return static_uniscribe_shaper_funcs.get_unconst ();
282 struct active_feature_t {
283 OPENTYPE_FEATURE_RECORD rec;
286 static int cmp (const void *pa, const void *pb) {
287 const active_feature_t *a = (const active_feature_t *) pa;
288 const active_feature_t *b = (const active_feature_t *) pb;
289 return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
290 a->order < b->order ? -1 : a->order > b->order ? 1 :
291 a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
294 bool operator== (const active_feature_t *f)
295 { return cmp (this, f) == 0; }
298 struct feature_event_t {
301 active_feature_t feature;
303 static int cmp (const void *pa, const void *pb)
305 const feature_event_t *a = (const feature_event_t *) pa;
306 const feature_event_t *b = (const feature_event_t *) pb;
307 return a->index < b->index ? -1 : a->index > b->index ? 1 :
308 a->start < b->start ? -1 : a->start > b->start ? 1 :
309 active_feature_t::cmp (&a->feature, &b->feature);
313 struct range_record_t {
314 TEXTRANGE_PROPERTIES props;
315 unsigned int index_first; /* == start */
316 unsigned int index_last; /* == end - 1 */
324 struct hb_uniscribe_face_data_t {
326 hb_uniscribe_shaper_funcs_t *funcs;
327 wchar_t face_name[LF_FACESIZE];
330 /* face_name should point to a wchar_t[LF_FACESIZE] object. */
332 _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
334 /* We'll create a private name for the font from a UUID using a simple,
335 * somewhat base64-like encoding scheme */
336 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
338 UuidCreate ((UUID*) &id);
339 static_assert ((2 + 3 * (16/2) < LF_FACESIZE), "");
340 unsigned int name_str_len = 0;
341 face_name[name_str_len++] = 'F';
342 face_name[name_str_len++] = '_';
343 unsigned char *p = (unsigned char *) &id;
344 for (unsigned int i = 0; i < 16; i += 2)
346 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
347 * using the bits in groups of 5,5,6 to select chars from enc.
348 * This will generate 24 characters; with the 'F_' prefix we already provided,
349 * the name will be 26 chars (plus the NUL terminator), so will always fit within
350 * face_name (LF_FACESIZE = 32). */
351 face_name[name_str_len++] = enc[p[i] >> 3];
352 face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
353 face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
355 face_name[name_str_len] = 0;
357 *plen = name_str_len;
362 _hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
364 /* Create a copy of the font data, with the 'name' table replaced by a
365 * table that names the font with our private F_* name created above.
366 * For simplicity, we just append a new 'name' table and update the
367 * sfnt directory; the original table is left in place, but unused.
369 * The new table will contain just 5 name IDs: family, style, unique,
370 * full, PS. All of them point to the same name data with our unique name.
373 blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (blob);
375 unsigned int length, new_length, name_str_len;
376 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
378 _hb_generate_unique_face_name (new_name, &name_str_len);
380 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
382 unsigned int name_table_length = OT::name::min_size +
383 ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
384 name_str_len * 2; /* for name data in UTF16BE form */
385 unsigned int padded_name_table_length = ((name_table_length + 3) & ~3);
386 unsigned int name_table_offset = (length + 3) & ~3;
388 new_length = name_table_offset + padded_name_table_length;
389 void *new_sfnt_data = calloc (1, new_length);
392 hb_blob_destroy (blob);
396 memcpy(new_sfnt_data, orig_sfnt_data, length);
398 OT::name &name = StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
400 name.count.set (ARRAY_LENGTH (name_IDs));
401 name.stringOffset.set (name.get_size ());
402 for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
404 OT::NameRecord &record = name.nameRecordZ[i];
405 record.platformID.set (3);
406 record.encodingID.set (1);
407 record.languageID.set (0x0409u); /* English */
408 record.nameID.set (name_IDs[i]);
409 record.length.set (name_str_len * 2);
410 record.offset.set (0);
413 /* Copy string data from new_name, converting wchar_t to UTF16BE. */
414 unsigned char *p = &StructAfter<unsigned char> (name);
415 for (unsigned int i = 0; i < name_str_len; i++)
417 *p++ = new_name[i] >> 8;
418 *p++ = new_name[i] & 0xff;
421 /* Adjust name table entry to point to new name table */
422 const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
423 unsigned int face_count = file.get_face_count ();
424 for (unsigned int face_index = 0; face_index < face_count; face_index++)
426 /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
427 * toe-stepping. But we don't really care. */
428 const OT::OpenTypeFontFace &face = file.get_face (face_index);
430 if (face.find_table_index (HB_OT_TAG_name, &index))
432 OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
433 record.checkSum.set_for_data (&name, padded_name_table_length);
434 record.offset.set (name_table_offset);
435 record.length.set (name_table_length);
437 else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
439 free (new_sfnt_data);
440 hb_blob_destroy (blob);
445 /* The checkSumAdjustment field in the 'head' table is now wrong,
446 * but that doesn't actually seem to cause any problems so we don't
449 hb_blob_destroy (blob);
450 return hb_blob_create ((const char *) new_sfnt_data, new_length,
451 HB_MEMORY_MODE_WRITABLE, nullptr, free);
454 hb_uniscribe_face_data_t *
455 _hb_uniscribe_shaper_face_data_create (hb_face_t *face)
457 hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
458 if (unlikely (!data))
461 data->funcs = hb_uniscribe_shaper_get_funcs ();
462 if (unlikely (!data->funcs))
468 hb_blob_t *blob = hb_face_reference_blob (face);
469 if (unlikely (!hb_blob_get_length (blob)))
470 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
472 blob = _hb_rename_font (blob, data->face_name);
473 if (unlikely (!blob))
479 DWORD num_fonts_installed;
480 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, nullptr),
481 hb_blob_get_length (blob),
482 0, &num_fonts_installed);
483 if (unlikely (!data->fh))
485 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
494 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_face_data_t *data)
496 RemoveFontMemResourceEx (data->fh);
505 struct hb_uniscribe_font_data_t
508 mutable LOGFONTW log_font;
510 mutable SCRIPT_CACHE script_cache;
511 double x_mult, y_mult; /* From LOGFONT space to HB space. */
515 populate_log_font (LOGFONTW *lf,
517 unsigned int font_size)
519 memset (lf, 0, sizeof (*lf));
520 lf->lfHeight = - (int) font_size;
521 lf->lfCharSet = DEFAULT_CHARSET;
523 memcpy (lf->lfFaceName, font->face->data.uniscribe->face_name, sizeof (lf->lfFaceName));
528 hb_uniscribe_font_data_t *
529 _hb_uniscribe_shaper_font_data_create (hb_font_t *font)
531 hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
532 if (unlikely (!data))
535 int font_size = font->face->get_upem (); /* Default... */
536 /* No idea if the following is even a good idea. */
538 font_size = font->y_ppem;
541 font_size = -font_size;
542 data->x_mult = (double) font->x_scale / font_size;
543 data->y_mult = (double) font->y_scale / font_size;
545 data->hdc = GetDC (nullptr);
547 if (unlikely (!populate_log_font (&data->log_font, font, font_size))) {
548 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
549 _hb_uniscribe_shaper_font_data_destroy (data);
553 data->hfont = CreateFontIndirectW (&data->log_font);
554 if (unlikely (!data->hfont)) {
555 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
556 _hb_uniscribe_shaper_font_data_destroy (data);
560 if (!SelectObject (data->hdc, data->hfont)) {
561 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
562 _hb_uniscribe_shaper_font_data_destroy (data);
570 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_font_data_t *data)
573 ReleaseDC (nullptr, data->hdc);
575 DeleteObject (data->hfont);
576 if (data->script_cache)
577 ScriptFreeCache (&data->script_cache);
582 hb_uniscribe_font_get_logfontw (hb_font_t *font)
584 const hb_uniscribe_font_data_t *data = font->data.uniscribe;
585 return data ? &data->log_font : nullptr;
589 hb_uniscribe_font_get_hfont (hb_font_t *font)
591 const hb_uniscribe_font_data_t *data = font->data.uniscribe;
592 return data ? data->hfont : nullptr;
602 _hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
605 const hb_feature_t *features,
606 unsigned int num_features)
608 hb_face_t *face = font->face;
609 const hb_uniscribe_face_data_t *face_data = face->data.uniscribe;
610 const hb_uniscribe_font_data_t *font_data = font->data.uniscribe;
611 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
616 hb_vector_t<OPENTYPE_FEATURE_RECORD> feature_records;
617 hb_vector_t<range_record_t> range_records;
620 /* Sort features by start/end events. */
621 hb_vector_t<feature_event_t> feature_events;
622 for (unsigned int i = 0; i < num_features; i++)
624 active_feature_t feature;
625 feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
626 feature.rec.lParameter = features[i].value;
629 feature_event_t *event;
631 event = feature_events.push ();
632 event->index = features[i].start;
634 event->feature = feature;
636 event = feature_events.push ();
637 event->index = features[i].end;
638 event->start = false;
639 event->feature = feature;
641 feature_events.qsort ();
642 /* Add a strategic final event. */
644 active_feature_t feature;
645 feature.rec.tagFeature = 0;
646 feature.rec.lParameter = 0;
647 feature.order = num_features + 1;
649 feature_event_t *event = feature_events.push ();
650 event->index = 0; /* This value does magic. */
651 event->start = false;
652 event->feature = feature;
655 /* Scan events and save features for each range. */
656 hb_vector_t<active_feature_t> active_features;
657 unsigned int last_index = 0;
658 for (unsigned int i = 0; i < feature_events.length; i++)
660 feature_event_t *event = &feature_events[i];
662 if (event->index != last_index)
664 /* Save a snapshot of active features and the range. */
665 range_record_t *range = range_records.push ();
667 unsigned int offset = feature_records.length;
669 active_features.qsort ();
670 for (unsigned int j = 0; j < active_features.length; j++)
672 if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.length - 1].tagFeature)
674 feature_records.push (active_features[j].rec);
678 /* Overrides value for existing feature. */
679 feature_records[feature_records.length - 1].lParameter = active_features[j].rec.lParameter;
683 /* Will convert to pointer after all is ready, since feature_records.array
684 * may move as we grow it. */
685 range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
686 range->props.cotfRecords = feature_records.length - offset;
687 range->index_first = last_index;
688 range->index_last = event->index - 1;
690 last_index = event->index;
695 active_features.push (event->feature);
699 active_feature_t *feature = active_features.find (&event->feature);
701 active_features.remove (feature - active_features.arrayZ ());
705 if (!range_records.length) /* No active feature found. */
708 /* Fixup the pointers. */
709 for (unsigned int i = 0; i < range_records.length; i++)
711 range_record_t *range = &range_records[i];
712 range->props.potfRecords = (OPENTYPE_FEATURE_RECORD *) feature_records + reinterpret_cast<uintptr_t> (range->props.potfRecords);
718 DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \
726 unsigned int scratch_size;
727 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
729 #define ALLOCATE_ARRAY(Type, name, len) \
730 Type *name = (Type *) scratch; \
732 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
733 assert (_consumed <= scratch_size); \
734 scratch += _consumed; \
735 scratch_size -= _consumed; \
738 #define utf16_index() var1.u32
740 ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
742 unsigned int chars_len = 0;
743 for (unsigned int i = 0; i < buffer->len; i++)
745 hb_codepoint_t c = buffer->info[i].codepoint;
746 buffer->info[i].utf16_index() = chars_len;
747 if (likely (c <= 0xFFFFu))
748 pchars[chars_len++] = c;
749 else if (unlikely (c > 0x10FFFFu))
750 pchars[chars_len++] = 0xFFFDu;
752 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
753 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
757 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
758 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
762 /* Need log_clusters to assign features. */
764 for (unsigned int i = 0; i < buffer->len; i++)
766 hb_codepoint_t c = buffer->info[i].codepoint;
767 unsigned int cluster = buffer->info[i].cluster;
768 log_clusters[chars_len++] = cluster;
769 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
770 log_clusters[chars_len++] = cluster; /* Surrogates. */
774 /* The -2 in the following is to compensate for possible
775 * alignment needed after the WORD array. sizeof(WORD) == 2. */
776 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
778 sizeof (SCRIPT_GLYPHPROP) +
783 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
784 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
785 ALLOCATE_ARRAY (int, advances, glyphs_size);
786 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
787 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
790 * We can't touch the contents of glyph_props. Our fallback
791 * implementations of Shape and Place functions use that buffer
792 * by casting it to a different type. It works because they
793 * both agree about it, but if we want to access it here we
794 * need address that issue first.
797 #undef ALLOCATE_ARRAY
799 #define MAX_ITEMS 256
801 SCRIPT_ITEM items[MAX_ITEMS + 1];
802 SCRIPT_CONTROL bidi_control = {0};
803 SCRIPT_STATE bidi_state = {0};
804 ULONG script_tags[MAX_ITEMS];
807 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
808 //bidi_control.fMergeNeutralItems = true;
809 *(uint32_t*)&bidi_control |= 1u<<24;
811 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
812 bidi_state.fOverrideDirection = 1;
814 hr = funcs->ScriptItemizeOpenType (pchars,
822 if (unlikely (FAILED (hr)))
823 FAIL ("ScriptItemizeOpenType() failed: 0x%08lx", hr);
828 unsigned int lang_count = 1;
829 hb_ot_tags_from_script_and_language (buffer->props.script,
830 buffer->props.language,
832 &lang_count, &lang_tag);
833 OPENTYPE_TAG language_tag = hb_uint32_swap (lang_count ? lang_tag : HB_TAG_NONE);
834 hb_vector_t<TEXTRANGE_PROPERTIES*> range_properties;
835 hb_vector_t<int> range_char_counts;
837 unsigned int glyphs_offset = 0;
838 unsigned int glyphs_len;
839 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
840 for (int i = 0; i < item_count; i++)
842 unsigned int chars_offset = items[i].iCharPos;
843 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
847 range_properties.shrink (0);
848 range_char_counts.shrink (0);
850 range_record_t *last_range = &range_records[0];
852 for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
854 range_record_t *range = last_range;
855 while (log_clusters[k] < range->index_first)
857 while (log_clusters[k] > range->index_last)
859 if (!range_properties.length ||
860 &range->props != range_properties[range_properties.length - 1])
862 TEXTRANGE_PROPERTIES **props = range_properties.push ();
863 int *c = range_char_counts.push ();
864 if (unlikely (!props || !c))
866 range_properties.shrink (0);
867 range_char_counts.shrink (0);
870 *props = &range->props;
875 range_char_counts[range_char_counts.length - 1]++;
882 /* Asking for glyphs in logical order circumvents at least
883 * one bug in Uniscribe. */
884 items[i].a.fLogicalOrder = true;
887 hr = funcs->ScriptShapeOpenType (font_data->hdc,
888 &font_data->script_cache,
892 range_char_counts.arrayZ (),
893 range_properties.arrayZ (),
894 range_properties.length,
895 pchars + chars_offset,
897 glyphs_size - glyphs_offset,
899 log_clusters + chars_offset,
900 char_props + chars_offset,
901 glyphs + glyphs_offset,
902 glyph_props + glyphs_offset,
903 (int *) &glyphs_len);
905 if (unlikely (items[i].a.fNoGlyphIndex))
906 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
907 if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
909 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
910 FAIL ("Buffer resize failed");
913 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
915 if (items[i].a.eScript == SCRIPT_UNDEFINED)
916 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
917 items[i].a.eScript = SCRIPT_UNDEFINED;
920 if (unlikely (FAILED (hr)))
922 FAIL ("ScriptShapeOpenType() failed: 0x%08lx", hr);
925 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
926 log_clusters[j] += glyphs_offset;
928 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
929 &font_data->script_cache,
933 range_char_counts.arrayZ (),
934 range_properties.arrayZ (),
935 range_properties.length,
936 pchars + chars_offset,
937 log_clusters + chars_offset,
938 char_props + chars_offset,
940 glyphs + glyphs_offset,
941 glyph_props + glyphs_offset,
944 advances + glyphs_offset,
945 offsets + glyphs_offset,
947 if (unlikely (FAILED (hr)))
948 FAIL ("ScriptPlaceOpenType() failed: 0x%08lx", hr);
950 if (DEBUG_ENABLED (UNISCRIBE))
951 fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
954 items[i].a.fLayoutRTL,
955 items[i].a.fLogicalOrder,
956 HB_UNTAG (hb_uint32_swap (script_tags[i])));
958 glyphs_offset += glyphs_len;
960 glyphs_len = glyphs_offset;
962 /* Ok, we've got everything we need, now compose output buffer,
963 * very, *very*, carefully! */
965 /* Calculate visual-clusters. That's what we ship. */
966 for (unsigned int i = 0; i < glyphs_len; i++)
967 vis_clusters[i] = (uint32_t) -1;
968 for (unsigned int i = 0; i < buffer->len; i++) {
969 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
970 *p = MIN (*p, buffer->info[i].cluster);
972 for (unsigned int i = 1; i < glyphs_len; i++)
973 if (vis_clusters[i] == (uint32_t) -1)
974 vis_clusters[i] = vis_clusters[i - 1];
978 if (unlikely (!buffer->ensure (glyphs_len)))
979 FAIL ("Buffer in error");
983 /* Set glyph infos */
985 for (unsigned int i = 0; i < glyphs_len; i++)
987 hb_glyph_info_t *info = &buffer->info[buffer->len++];
989 info->codepoint = glyphs[i];
990 info->cluster = vis_clusters[i];
992 /* The rest is crap. Let's store position info there for now. */
993 info->mask = advances[i];
994 info->var1.i32 = offsets[i].du;
995 info->var2.i32 = offsets[i].dv;
998 /* Set glyph positions */
999 buffer->clear_positions ();
1000 double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
1001 for (unsigned int i = 0; i < glyphs_len; i++)
1003 hb_glyph_info_t *info = &buffer->info[i];
1004 hb_glyph_position_t *pos = &buffer->pos[i];
1007 pos->x_advance = x_mult * (int32_t) info->mask;
1008 pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
1009 pos->y_offset = y_mult * info->var2.i32;
1013 hb_buffer_reverse (buffer);
1015 buffer->unsafe_to_break_all ();