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 static inline uint16_t hb_uint16_swap (const uint16_t v)
47 { return (v >> 8) | (v << 8); }
48 static inline uint32_t hb_uint32_swap (const uint32_t v)
49 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
52 typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
53 const WCHAR *pwcInChars,
56 const SCRIPT_CONTROL *psControl,
57 const SCRIPT_STATE *psState,
59 OPENTYPE_TAG *pScriptTags,
63 typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
67 OPENTYPE_TAG tagScript,
68 OPENTYPE_TAG tagLangSys,
70 TEXTRANGE_PROPERTIES **rpRangeProperties,
72 const WCHAR *pwcChars,
76 SCRIPT_CHARPROP *pCharProps,
78 SCRIPT_GLYPHPROP *pOutGlyphProps,
82 typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
86 OPENTYPE_TAG tagScript,
87 OPENTYPE_TAG tagLangSys,
89 TEXTRANGE_PROPERTIES **rpRangeProperties,
91 const WCHAR *pwcChars,
93 SCRIPT_CHARPROP *pCharProps,
96 const SCRIPT_GLYPHPROP *pGlyphProps,
104 /* Fallback implementations. */
106 static HRESULT WINAPI
107 hb_ScriptItemizeOpenType(
108 const WCHAR *pwcInChars,
111 const SCRIPT_CONTROL *psControl,
112 const SCRIPT_STATE *psState,
114 OPENTYPE_TAG *pScriptTags,
119 return ScriptItemize (pwcInChars,
129 static HRESULT WINAPI
130 hb_ScriptShapeOpenType(
133 SCRIPT_ANALYSIS *psa,
134 OPENTYPE_TAG tagScript,
135 OPENTYPE_TAG tagLangSys,
137 TEXTRANGE_PROPERTIES **rpRangeProperties,
139 const WCHAR *pwcChars,
143 SCRIPT_CHARPROP *pCharProps,
145 SCRIPT_GLYPHPROP *pOutGlyphProps,
149 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
150 return ScriptShape (hdc,
162 static HRESULT WINAPI
163 hb_ScriptPlaceOpenType(
166 SCRIPT_ANALYSIS *psa,
167 OPENTYPE_TAG tagScript,
168 OPENTYPE_TAG tagLangSys,
170 TEXTRANGE_PROPERTIES **rpRangeProperties,
172 const WCHAR *pwcChars,
174 SCRIPT_CHARPROP *pCharProps,
176 const WORD *pwGlyphs,
177 const SCRIPT_GLYPHPROP *pGlyphProps,
184 SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
185 return ScriptPlace (hdc,
197 struct hb_uniscribe_shaper_funcs_t {
198 SIOT ScriptItemizeOpenType;
199 SSOT ScriptShapeOpenType;
200 SPOT ScriptPlaceOpenType;
202 inline void init (void)
205 this->ScriptItemizeOpenType = NULL;
206 this->ScriptShapeOpenType = NULL;
207 this->ScriptPlaceOpenType = NULL;
209 hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
212 this->ScriptItemizeOpenType = (SIOT) GetProcAddress (hinstLib, "ScriptItemizeOpenType");
213 this->ScriptShapeOpenType = (SSOT) GetProcAddress (hinstLib, "ScriptShapeOpenType");
214 this->ScriptPlaceOpenType = (SPOT) GetProcAddress (hinstLib, "ScriptPlaceOpenType");
216 if (!this->ScriptItemizeOpenType ||
217 !this->ScriptShapeOpenType ||
218 !this->ScriptPlaceOpenType)
220 DEBUG_MSG (UNISCRIBE, NULL, "OpenType versions of functions not found; falling back.");
221 this->ScriptItemizeOpenType = hb_ScriptItemizeOpenType;
222 this->ScriptShapeOpenType = hb_ScriptShapeOpenType;
223 this->ScriptPlaceOpenType = hb_ScriptPlaceOpenType;
227 static hb_uniscribe_shaper_funcs_t *uniscribe_funcs;
230 free_uniscribe_funcs (void)
232 free (uniscribe_funcs);
235 static hb_uniscribe_shaper_funcs_t *
236 hb_uniscribe_shaper_get_funcs (void)
239 hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) hb_atomic_ptr_get (&uniscribe_funcs);
241 if (unlikely (!funcs))
243 funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
244 if (unlikely (!funcs))
249 if (!hb_atomic_ptr_cmpexch (&uniscribe_funcs, NULL, funcs)) {
255 atexit (free_uniscribe_funcs); /* First person registers atexit() callback. */
263 struct active_feature_t {
264 OPENTYPE_FEATURE_RECORD rec;
267 static int cmp (const active_feature_t *a, const active_feature_t *b) {
268 return a->rec.tagFeature < b->rec.tagFeature ? -1 : a->rec.tagFeature > b->rec.tagFeature ? 1 :
269 a->order < b->order ? -1 : a->order > b->order ? 1 :
270 a->rec.lParameter < b->rec.lParameter ? -1 : a->rec.lParameter > b->rec.lParameter ? 1 :
273 bool operator== (const active_feature_t *f) {
274 return cmp (this, f) == 0;
278 struct feature_event_t {
281 active_feature_t feature;
283 static int cmp (const feature_event_t *a, const feature_event_t *b) {
284 return a->index < b->index ? -1 : a->index > b->index ? 1 :
285 a->start < b->start ? -1 : a->start > b->start ? 1 :
286 active_feature_t::cmp (&a->feature, &b->feature);
290 struct range_record_t {
291 TEXTRANGE_PROPERTIES props;
292 unsigned int index_first; /* == start */
293 unsigned int index_last; /* == end - 1 */
296 HB_SHAPER_DATA_ENSURE_DEFINE(uniscribe, face)
297 HB_SHAPER_DATA_ENSURE_DEFINE(uniscribe, font)
304 struct hb_uniscribe_shaper_face_data_t {
306 hb_uniscribe_shaper_funcs_t *funcs;
307 wchar_t face_name[LF_FACESIZE];
310 /* face_name should point to a wchar_t[LF_FACESIZE] object. */
312 _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
314 /* We'll create a private name for the font from a UUID using a simple,
315 * somewhat base64-like encoding scheme */
316 const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
318 UuidCreate ((UUID*) &id);
319 ASSERT_STATIC (2 + 3 * (16/2) < LF_FACESIZE);
320 unsigned int name_str_len = 0;
321 face_name[name_str_len++] = 'F';
322 face_name[name_str_len++] = '_';
323 unsigned char *p = (unsigned char *) &id;
324 for (unsigned int i = 0; i < 16; i += 2)
326 /* Spread the 16 bits from two bytes of the UUID across three chars of face_name,
327 * using the bits in groups of 5,5,6 to select chars from enc.
328 * This will generate 24 characters; with the 'F_' prefix we already provided,
329 * the name will be 26 chars (plus the NUL terminator), so will always fit within
330 * face_name (LF_FACESIZE = 32). */
331 face_name[name_str_len++] = enc[p[i] >> 3];
332 face_name[name_str_len++] = enc[((p[i] << 2) | (p[i + 1] >> 6)) & 0x1f];
333 face_name[name_str_len++] = enc[p[i + 1] & 0x3f];
335 face_name[name_str_len] = 0;
337 *plen = name_str_len;
342 _hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
344 /* Create a copy of the font data, with the 'name' table replaced by a
345 * table that names the font with our private F_* name created above.
346 * For simplicity, we just append a new 'name' table and update the
347 * sfnt directory; the original table is left in place, but unused.
349 * The new table will contain just 5 name IDs: family, style, unique,
350 * full, PS. All of them point to the same name data with our unique name.
353 blob = OT::Sanitizer<OT::OpenTypeFontFile>::sanitize (blob);
355 unsigned int length, new_length, name_str_len;
356 const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
358 _hb_generate_unique_face_name (new_name, &name_str_len);
360 static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
362 unsigned int name_table_length = OT::name::min_size +
363 ARRAY_LENGTH (name_IDs) * OT::NameRecord::static_size +
364 name_str_len * 2; /* for name data in UTF16BE form */
365 unsigned int name_table_offset = (length + 3) & ~3;
367 new_length = name_table_offset + ((name_table_length + 3) & ~3);
368 void *new_sfnt_data = calloc (1, new_length);
371 hb_blob_destroy (blob);
375 memcpy(new_sfnt_data, orig_sfnt_data, length);
377 OT::name &name = OT::StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
379 name.count.set (ARRAY_LENGTH (name_IDs));
380 name.stringOffset.set (name.get_size ());
381 for (unsigned int i = 0; i < ARRAY_LENGTH (name_IDs); i++)
383 OT::NameRecord &record = name.nameRecord[i];
384 record.platformID.set (3);
385 record.encodingID.set (1);
386 record.languageID.set (0x0409u); /* English */
387 record.nameID.set (name_IDs[i]);
388 record.length.set (name_str_len * 2);
389 record.offset.set (0);
392 /* Copy string data from new_name, converting wchar_t to UTF16BE. */
393 unsigned char *p = &OT::StructAfter<unsigned char> (name);
394 for (unsigned int i = 0; i < name_str_len; i++)
396 *p++ = new_name[i] >> 8;
397 *p++ = new_name[i] & 0xff;
400 /* Adjust name table entry to point to new name table */
401 const OT::OpenTypeFontFile &file = * (OT::OpenTypeFontFile *) (new_sfnt_data);
402 unsigned int face_count = file.get_face_count ();
403 for (unsigned int face_index = 0; face_index < face_count; face_index++)
405 /* Note: doing multiple edits (ie. TTC) can be unsafe. There may be
406 * toe-stepping. But we don't really care. */
407 const OT::OpenTypeFontFace &face = file.get_face (face_index);
409 if (face.find_table_index (HB_OT_TAG_name, &index))
411 OT::TableRecord &record = const_cast<OT::TableRecord &> (face.get_table (index));
412 record.checkSum.set_for_data (&name, name_table_length);
413 record.offset.set (name_table_offset);
414 record.length.set (name_table_length);
416 else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
418 free (new_sfnt_data);
419 hb_blob_destroy (blob);
424 /* The checkSumAdjustment field in the 'head' table is now wrong,
425 * but that doesn't actually seem to cause any problems so we don't
428 hb_blob_destroy (blob);
429 return hb_blob_create ((const char *) new_sfnt_data, new_length,
430 HB_MEMORY_MODE_WRITABLE, NULL, free);
433 hb_uniscribe_shaper_face_data_t *
434 _hb_uniscribe_shaper_face_data_create (hb_face_t *face)
436 hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
437 if (unlikely (!data))
440 data->funcs = hb_uniscribe_shaper_get_funcs ();
441 if (unlikely (!data->funcs))
447 hb_blob_t *blob = hb_face_reference_blob (face);
448 if (unlikely (!hb_blob_get_length (blob)))
449 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
451 blob = _hb_rename_font (blob, data->face_name);
452 if (unlikely (!blob))
458 DWORD num_fonts_installed;
459 data->fh = AddFontMemResourceEx ((void *) hb_blob_get_data (blob, NULL),
460 hb_blob_get_length (blob),
461 0, &num_fonts_installed);
462 if (unlikely (!data->fh))
464 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
473 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
475 RemoveFontMemResourceEx (data->fh);
484 struct hb_uniscribe_shaper_font_data_t {
488 SCRIPT_CACHE script_cache;
489 double x_mult, y_mult; /* From LOGFONT space to HB space. */
493 populate_log_font (LOGFONTW *lf,
495 unsigned int font_size)
497 memset (lf, 0, sizeof (*lf));
498 lf->lfHeight = -font_size;
499 lf->lfCharSet = DEFAULT_CHARSET;
501 hb_face_t *face = font->face;
502 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
504 memcpy (lf->lfFaceName, face_data->face_name, sizeof (lf->lfFaceName));
509 hb_uniscribe_shaper_font_data_t *
510 _hb_uniscribe_shaper_font_data_create (hb_font_t *font)
512 if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL;
514 hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
515 if (unlikely (!data))
518 int font_size = font->face->get_upem (); /* Default... */
519 /* No idea if the following is even a good idea. */
521 font_size = font->y_ppem;
524 font_size = -font_size;
525 data->x_mult = (double) font->x_scale / font_size;
526 data->y_mult = (double) font->y_scale / font_size;
528 data->hdc = GetDC (NULL);
530 if (unlikely (!populate_log_font (&data->log_font, font, font_size))) {
531 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
532 _hb_uniscribe_shaper_font_data_destroy (data);
536 data->hfont = CreateFontIndirectW (&data->log_font);
537 if (unlikely (!data->hfont)) {
538 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
539 _hb_uniscribe_shaper_font_data_destroy (data);
543 if (!SelectObject (data->hdc, data->hfont)) {
544 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
545 _hb_uniscribe_shaper_font_data_destroy (data);
553 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
556 ReleaseDC (NULL, data->hdc);
558 DeleteObject (data->hfont);
559 if (data->script_cache)
560 ScriptFreeCache (&data->script_cache);
565 hb_uniscribe_font_get_logfontw (hb_font_t *font)
567 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
568 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
569 return &font_data->log_font;
573 hb_uniscribe_font_get_hfont (hb_font_t *font)
575 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
576 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
577 return font_data->hfont;
582 * shaper shape_plan data
585 struct hb_uniscribe_shaper_shape_plan_data_t {};
587 hb_uniscribe_shaper_shape_plan_data_t *
588 _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
589 const hb_feature_t *user_features HB_UNUSED,
590 unsigned int num_user_features HB_UNUSED,
591 const int *coords HB_UNUSED,
592 unsigned int num_coords HB_UNUSED)
594 return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
598 _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED)
609 _hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
612 const hb_feature_t *features,
613 unsigned int num_features)
615 hb_face_t *face = font->face;
616 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
617 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
618 hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
623 hb_auto_array_t<OPENTYPE_FEATURE_RECORD> feature_records;
624 hb_auto_array_t<range_record_t> range_records;
627 /* Sort features by start/end events. */
628 hb_auto_array_t<feature_event_t> feature_events;
629 for (unsigned int i = 0; i < num_features; i++)
631 active_feature_t feature;
632 feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
633 feature.rec.lParameter = features[i].value;
636 feature_event_t *event;
638 event = feature_events.push ();
639 if (unlikely (!event))
641 event->index = features[i].start;
643 event->feature = feature;
645 event = feature_events.push ();
646 if (unlikely (!event))
648 event->index = features[i].end;
649 event->start = false;
650 event->feature = feature;
652 feature_events.qsort ();
653 /* Add a strategic final event. */
655 active_feature_t feature;
656 feature.rec.tagFeature = 0;
657 feature.rec.lParameter = 0;
658 feature.order = num_features + 1;
660 feature_event_t *event = feature_events.push ();
661 if (unlikely (!event))
663 event->index = 0; /* This value does magic. */
664 event->start = false;
665 event->feature = feature;
668 /* Scan events and save features for each range. */
669 hb_auto_array_t<active_feature_t> active_features;
670 unsigned int last_index = 0;
671 for (unsigned int i = 0; i < feature_events.len; i++)
673 feature_event_t *event = &feature_events[i];
675 if (event->index != last_index)
677 /* Save a snapshot of active features and the range. */
678 range_record_t *range = range_records.push ();
679 if (unlikely (!range))
682 unsigned int offset = feature_records.len;
684 active_features.qsort ();
685 for (unsigned int j = 0; j < active_features.len; j++)
687 if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.len - 1].tagFeature)
689 OPENTYPE_FEATURE_RECORD *feature = feature_records.push ();
690 if (unlikely (!feature))
692 *feature = active_features[j].rec;
696 /* Overrides value for existing feature. */
697 feature_records[feature_records.len - 1].lParameter = active_features[j].rec.lParameter;
701 /* Will convert to pointer after all is ready, since feature_records.array
702 * may move as we grow it. */
703 range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
704 range->props.cotfRecords = feature_records.len - offset;
705 range->index_first = last_index;
706 range->index_last = event->index - 1;
708 last_index = event->index;
712 active_feature_t *feature = active_features.push ();
713 if (unlikely (!feature))
715 *feature = event->feature;
717 active_feature_t *feature = active_features.find (&event->feature);
719 active_features.remove (feature - active_features.array);
723 if (!range_records.len) /* No active feature found. */
726 /* Fixup the pointers. */
727 for (unsigned int i = 0; i < range_records.len; i++)
729 range_record_t *range = &range_records[i];
730 range->props.potfRecords = feature_records.array + reinterpret_cast<uintptr_t> (range->props.potfRecords);
741 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
749 unsigned int scratch_size;
750 hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
752 #define ALLOCATE_ARRAY(Type, name, len) \
753 Type *name = (Type *) scratch; \
755 unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
756 assert (_consumed <= scratch_size); \
757 scratch += _consumed; \
758 scratch_size -= _consumed; \
761 #define utf16_index() var1.u32
763 ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
765 unsigned int chars_len = 0;
766 for (unsigned int i = 0; i < buffer->len; i++)
768 hb_codepoint_t c = buffer->info[i].codepoint;
769 buffer->info[i].utf16_index() = chars_len;
770 if (likely (c <= 0xFFFFu))
771 pchars[chars_len++] = c;
772 else if (unlikely (c > 0x10FFFFu))
773 pchars[chars_len++] = 0xFFFDu;
775 pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
776 pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
780 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
781 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
785 /* Need log_clusters to assign features. */
787 for (unsigned int i = 0; i < buffer->len; i++)
789 hb_codepoint_t c = buffer->info[i].codepoint;
790 unsigned int cluster = buffer->info[i].cluster;
791 log_clusters[chars_len++] = cluster;
792 if (hb_in_range (c, 0x10000u, 0x10FFFFu))
793 log_clusters[chars_len++] = cluster; /* Surrogates. */
797 /* The -2 in the following is to compensate for possible
798 * alignment needed after the WORD array. sizeof(WORD) == 2. */
799 unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
801 sizeof (SCRIPT_GLYPHPROP) +
806 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
807 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
808 ALLOCATE_ARRAY (int, advances, glyphs_size);
809 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
810 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
813 * We can't touch the contents of glyph_props. Our fallback
814 * implementations of Shape and Place functions use that buffer
815 * by casting it to a different type. It works because they
816 * both agree about it, but if we want to access it here we
817 * need address that issue first.
820 #undef ALLOCATE_ARRAY
822 #define MAX_ITEMS 256
824 SCRIPT_ITEM items[MAX_ITEMS + 1];
825 SCRIPT_CONTROL bidi_control = {0};
826 SCRIPT_STATE bidi_state = {0};
827 ULONG script_tags[MAX_ITEMS];
830 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
831 //bidi_control.fMergeNeutralItems = true;
832 *(uint32_t*)&bidi_control |= 1u<<24;
834 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
835 bidi_state.fOverrideDirection = 1;
837 hr = funcs->ScriptItemizeOpenType (pchars,
845 if (unlikely (FAILED (hr)))
846 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
850 OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
851 hb_auto_array_t<TEXTRANGE_PROPERTIES*> range_properties;
852 hb_auto_array_t<int> range_char_counts;
854 unsigned int glyphs_offset = 0;
855 unsigned int glyphs_len;
856 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
857 for (unsigned int i = 0; i < item_count; i++)
859 unsigned int chars_offset = items[i].iCharPos;
860 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
864 range_properties.shrink (0);
865 range_char_counts.shrink (0);
867 range_record_t *last_range = &range_records[0];
869 for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
871 range_record_t *range = last_range;
872 while (log_clusters[k] < range->index_first)
874 while (log_clusters[k] > range->index_last)
876 if (!range_properties.len ||
877 &range->props != range_properties[range_properties.len - 1])
879 TEXTRANGE_PROPERTIES **props = range_properties.push ();
880 int *c = range_char_counts.push ();
881 if (unlikely (!props || !c))
883 range_properties.shrink (0);
884 range_char_counts.shrink (0);
887 *props = &range->props;
892 range_char_counts[range_char_counts.len - 1]++;
899 /* Asking for glyphs in logical order circumvents at least
900 * one bug in Uniscribe. */
901 items[i].a.fLogicalOrder = true;
904 hr = funcs->ScriptShapeOpenType (font_data->hdc,
905 &font_data->script_cache,
909 range_char_counts.array,
910 range_properties.array,
911 range_properties.len,
912 pchars + chars_offset,
914 glyphs_size - glyphs_offset,
916 log_clusters + chars_offset,
917 char_props + chars_offset,
918 glyphs + glyphs_offset,
919 glyph_props + glyphs_offset,
920 (int *) &glyphs_len);
922 if (unlikely (items[i].a.fNoGlyphIndex))
923 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
924 if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
926 if (unlikely (!buffer->ensure (buffer->allocated * 2)))
927 FAIL ("Buffer resize failed");
930 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
932 if (items[i].a.eScript == SCRIPT_UNDEFINED)
933 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
934 items[i].a.eScript = SCRIPT_UNDEFINED;
937 if (unlikely (FAILED (hr)))
939 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
942 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
943 log_clusters[j] += glyphs_offset;
945 hr = funcs->ScriptPlaceOpenType (font_data->hdc,
946 &font_data->script_cache,
950 range_char_counts.array,
951 range_properties.array,
952 range_properties.len,
953 pchars + chars_offset,
954 log_clusters + chars_offset,
955 char_props + chars_offset,
957 glyphs + glyphs_offset,
958 glyph_props + glyphs_offset,
961 advances + glyphs_offset,
962 offsets + glyphs_offset,
964 if (unlikely (FAILED (hr)))
965 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
967 if (DEBUG_ENABLED (UNISCRIBE))
968 fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
971 items[i].a.fLayoutRTL,
972 items[i].a.fLogicalOrder,
973 HB_UNTAG (hb_uint32_swap (script_tags[i])));
975 glyphs_offset += glyphs_len;
977 glyphs_len = glyphs_offset;
979 /* Ok, we've got everything we need, now compose output buffer,
980 * very, *very*, carefully! */
982 /* Calculate visual-clusters. That's what we ship. */
983 for (unsigned int i = 0; i < glyphs_len; i++)
984 vis_clusters[i] = -1;
985 for (unsigned int i = 0; i < buffer->len; i++) {
986 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
987 *p = MIN (*p, buffer->info[i].cluster);
989 for (unsigned int i = 1; i < glyphs_len; i++)
990 if (vis_clusters[i] == -1)
991 vis_clusters[i] = vis_clusters[i - 1];
995 if (unlikely (!buffer->ensure (glyphs_len)))
996 FAIL ("Buffer in error");
1000 /* Set glyph infos */
1002 for (unsigned int i = 0; i < glyphs_len; i++)
1004 hb_glyph_info_t *info = &buffer->info[buffer->len++];
1006 info->codepoint = glyphs[i];
1007 info->cluster = vis_clusters[i];
1009 /* The rest is crap. Let's store position info there for now. */
1010 info->mask = advances[i];
1011 info->var1.i32 = offsets[i].du;
1012 info->var2.i32 = offsets[i].dv;
1015 /* Set glyph positions */
1016 buffer->clear_positions ();
1017 double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
1018 for (unsigned int i = 0; i < glyphs_len; i++)
1020 hb_glyph_info_t *info = &buffer->info[i];
1021 hb_glyph_position_t *pos = &buffer->pos[i];
1024 pos->x_advance = x_mult * (int32_t) info->mask;
1025 pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
1026 pos->y_offset = y_mult * info->var2.i32;
1030 hb_buffer_reverse (buffer);