Add hb_gdi_face_create API
[platform/upstream/libHarfBuzzSharp.git] / src / hb-uniscribe.cc
1 /*
2  * Copyright © 2011,2012,2013  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
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.
11  *
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
16  * DAMAGE.
17  *
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.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #include "hb.hh"
28
29 #ifdef HAVE_UNISCRIBE
30
31 #ifdef HB_NO_OT_TAG
32 #error "Cannot compile 'uniscribe' shaper with HB_NO_OT_TAG."
33 #endif
34
35 #include "hb-shaper-impl.hh"
36
37 #include <windows.h>
38 #include <usp10.h>
39 #include <rpc.h>
40
41 #ifndef E_NOT_SUFFICIENT_BUFFER
42 #define E_NOT_SUFFICIENT_BUFFER HRESULT_FROM_WIN32 (ERROR_INSUFFICIENT_BUFFER)
43 #endif
44
45 #include "hb-uniscribe.h"
46
47 #include "hb-open-file.hh"
48 #include "hb-ot-name-table.hh"
49 #include "hb-ot-layout.h"
50
51
52 /**
53  * SECTION:hb-uniscribe
54  * @title: hb-uniscribe
55  * @short_description: Windows integration
56  * @include: hb-uniscribe.h
57  *
58  * Functions for using HarfBuzz with the Windows fonts.
59  **/
60
61 typedef HRESULT (WINAPI *SIOT) /*ScriptItemizeOpenType*/(
62   const WCHAR *pwcInChars,
63   int cInChars,
64   int cMaxItems,
65   const SCRIPT_CONTROL *psControl,
66   const SCRIPT_STATE *psState,
67   SCRIPT_ITEM *pItems,
68   OPENTYPE_TAG *pScriptTags,
69   int *pcItems
70 );
71
72 typedef HRESULT (WINAPI *SSOT) /*ScriptShapeOpenType*/(
73   HDC hdc,
74   SCRIPT_CACHE *psc,
75   SCRIPT_ANALYSIS *psa,
76   OPENTYPE_TAG tagScript,
77   OPENTYPE_TAG tagLangSys,
78   int *rcRangeChars,
79   TEXTRANGE_PROPERTIES **rpRangeProperties,
80   int cRanges,
81   const WCHAR *pwcChars,
82   int cChars,
83   int cMaxGlyphs,
84   WORD *pwLogClust,
85   SCRIPT_CHARPROP *pCharProps,
86   WORD *pwOutGlyphs,
87   SCRIPT_GLYPHPROP *pOutGlyphProps,
88   int *pcGlyphs
89 );
90
91 typedef HRESULT (WINAPI *SPOT) /*ScriptPlaceOpenType*/(
92   HDC hdc,
93   SCRIPT_CACHE *psc,
94   SCRIPT_ANALYSIS *psa,
95   OPENTYPE_TAG tagScript,
96   OPENTYPE_TAG tagLangSys,
97   int *rcRangeChars,
98   TEXTRANGE_PROPERTIES **rpRangeProperties,
99   int cRanges,
100   const WCHAR *pwcChars,
101   WORD *pwLogClust,
102   SCRIPT_CHARPROP *pCharProps,
103   int cChars,
104   const WORD *pwGlyphs,
105   const SCRIPT_GLYPHPROP *pGlyphProps,
106   int cGlyphs,
107   int *piAdvance,
108   GOFFSET *pGoffset,
109   ABC *pABC
110 );
111
112
113 /* Fallback implementations. */
114
115 static HRESULT WINAPI
116 hb_ScriptItemizeOpenType(
117   const WCHAR *pwcInChars,
118   int cInChars,
119   int cMaxItems,
120   const SCRIPT_CONTROL *psControl,
121   const SCRIPT_STATE *psState,
122   SCRIPT_ITEM *pItems,
123   OPENTYPE_TAG *pScriptTags,
124   int *pcItems
125 )
126 {
127 {
128   return ScriptItemize (pwcInChars,
129                         cInChars,
130                         cMaxItems,
131                         psControl,
132                         psState,
133                         pItems,
134                         pcItems);
135 }
136 }
137
138 static HRESULT WINAPI
139 hb_ScriptShapeOpenType(
140   HDC hdc,
141   SCRIPT_CACHE *psc,
142   SCRIPT_ANALYSIS *psa,
143   OPENTYPE_TAG tagScript,
144   OPENTYPE_TAG tagLangSys,
145   int *rcRangeChars,
146   TEXTRANGE_PROPERTIES **rpRangeProperties,
147   int cRanges,
148   const WCHAR *pwcChars,
149   int cChars,
150   int cMaxGlyphs,
151   WORD *pwLogClust,
152   SCRIPT_CHARPROP *pCharProps,
153   WORD *pwOutGlyphs,
154   SCRIPT_GLYPHPROP *pOutGlyphProps,
155   int *pcGlyphs
156 )
157 {
158   SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pOutGlyphProps;
159   return ScriptShape (hdc,
160                       psc,
161                       pwcChars,
162                       cChars,
163                       cMaxGlyphs,
164                       psa,
165                       pwOutGlyphs,
166                       pwLogClust,
167                       psva,
168                       pcGlyphs);
169 }
170
171 static HRESULT WINAPI
172 hb_ScriptPlaceOpenType(
173   HDC hdc,
174   SCRIPT_CACHE *psc,
175   SCRIPT_ANALYSIS *psa,
176   OPENTYPE_TAG tagScript,
177   OPENTYPE_TAG tagLangSys,
178   int *rcRangeChars,
179   TEXTRANGE_PROPERTIES **rpRangeProperties,
180   int cRanges,
181   const WCHAR *pwcChars,
182   WORD *pwLogClust,
183   SCRIPT_CHARPROP *pCharProps,
184   int cChars,
185   const WORD *pwGlyphs,
186   const SCRIPT_GLYPHPROP *pGlyphProps,
187   int cGlyphs,
188   int *piAdvance,
189   GOFFSET *pGoffset,
190   ABC *pABC
191 )
192 {
193   SCRIPT_VISATTR *psva = (SCRIPT_VISATTR *) pGlyphProps;
194   return ScriptPlace (hdc,
195                       psc,
196                       pwGlyphs,
197                       cGlyphs,
198                       psva,
199                       psa,
200                       piAdvance,
201                       pGoffset,
202                       pABC);
203 }
204
205
206 struct hb_uniscribe_shaper_funcs_t
207 {
208   SIOT ScriptItemizeOpenType;
209   SSOT ScriptShapeOpenType;
210   SPOT ScriptPlaceOpenType;
211
212   void init ()
213   {
214     HMODULE hinstLib;
215     this->ScriptItemizeOpenType = nullptr;
216     this->ScriptShapeOpenType   = nullptr;
217     this->ScriptPlaceOpenType   = nullptr;
218
219     hinstLib = GetModuleHandle (TEXT ("usp10.dll"));
220     if (hinstLib)
221     {
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
228     }
229     if (!this->ScriptItemizeOpenType ||
230         !this->ScriptShapeOpenType   ||
231         !this->ScriptPlaceOpenType)
232     {
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;
237     }
238   }
239 };
240
241 #if HB_USE_ATEXIT
242 static void free_static_uniscribe_shaper_funcs ();
243 #endif
244
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>
247 {
248   static hb_uniscribe_shaper_funcs_t *create ()
249   {
250     hb_uniscribe_shaper_funcs_t *funcs = (hb_uniscribe_shaper_funcs_t *) calloc (1, sizeof (hb_uniscribe_shaper_funcs_t));
251     if (unlikely (!funcs))
252       return nullptr;
253
254     funcs->init ();
255
256 #if HB_USE_ATEXIT
257     atexit (free_static_uniscribe_shaper_funcs);
258 #endif
259
260     return funcs;
261   }
262   static void destroy (hb_uniscribe_shaper_funcs_t *p)
263   {
264     free ((void *) p);
265   }
266   static hb_uniscribe_shaper_funcs_t *get_null ()
267   {
268     return nullptr;
269   }
270 } static_uniscribe_shaper_funcs;
271
272 #if HB_USE_ATEXIT
273 static
274 void free_static_uniscribe_shaper_funcs ()
275 {
276   static_uniscribe_shaper_funcs.free_instance ();
277 }
278 #endif
279
280 static hb_uniscribe_shaper_funcs_t *
281 hb_uniscribe_shaper_get_funcs ()
282 {
283   return static_uniscribe_shaper_funcs.get_unconst ();
284 }
285
286
287 struct active_feature_t {
288   OPENTYPE_FEATURE_RECORD rec;
289   unsigned int order;
290
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 :
297            0;
298   }
299   bool operator== (const active_feature_t *f)
300   { return cmp (this, f) == 0; }
301 };
302
303 struct feature_event_t {
304   unsigned int index;
305   bool start;
306   active_feature_t feature;
307
308   HB_INTERNAL static int cmp (const void *pa, const void *pb)
309   {
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);
315   }
316 };
317
318 struct range_record_t {
319   TEXTRANGE_PROPERTIES props;
320   unsigned int index_first; /* == start */
321   unsigned int index_last;  /* == end - 1 */
322 };
323
324
325 /*
326  * shaper face data
327  */
328
329 struct hb_uniscribe_face_data_t {
330   HANDLE fh;
331   hb_uniscribe_shaper_funcs_t *funcs;
332   wchar_t face_name[LF_FACESIZE];
333 };
334
335 /* face_name should point to a wchar_t[LF_FACESIZE] object. */
336 static void
337 _hb_generate_unique_face_name (wchar_t *face_name, unsigned int *plen)
338 {
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+-";
342   UUID id;
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)
350   {
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];
359   }
360   face_name[name_str_len] = 0;
361   if (plen)
362     *plen = name_str_len;
363 }
364
365 /* Destroys blob. */
366 static hb_blob_t *
367 _hb_rename_font (hb_blob_t *blob, wchar_t *new_name)
368 {
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.
373    *
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.
376    */
377
378   blob = hb_sanitize_context_t ().sanitize_blob<OT::OpenTypeFontFile> (blob);
379
380   unsigned int length, new_length, name_str_len;
381   const char *orig_sfnt_data = hb_blob_get_data (blob, &length);
382
383   _hb_generate_unique_face_name (new_name, &name_str_len);
384
385   static const uint16_t name_IDs[] = { 1, 2, 3, 4, 6 };
386
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;
392
393   new_length = name_table_offset + padded_name_table_length;
394   void *new_sfnt_data = calloc (1, new_length);
395   if (!new_sfnt_data)
396   {
397     hb_blob_destroy (blob);
398     return nullptr;
399   }
400
401   memcpy(new_sfnt_data, orig_sfnt_data, length);
402
403   OT::name &name = StructAtOffset<OT::name> (new_sfnt_data, name_table_offset);
404   name.format = 0;
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++)
408   {
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;
415     record.offset = 0;
416   }
417
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++)
421   {
422     *p++ = new_name[i] >> 8;
423     *p++ = new_name[i] & 0xff;
424   }
425
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++)
430   {
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);
434     unsigned int index;
435     if (face.find_table_index (HB_OT_TAG_name, &index))
436     {
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;
441     }
442     else if (face_index == 0) /* Fail if first face doesn't have 'name' table. */
443     {
444       free (new_sfnt_data);
445       hb_blob_destroy (blob);
446       return nullptr;
447     }
448   }
449
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
452    * bother. */
453
454   hb_blob_destroy (blob);
455   return hb_blob_create ((const char *) new_sfnt_data, new_length,
456                          HB_MEMORY_MODE_WRITABLE, nullptr, free);
457 }
458
459 hb_uniscribe_face_data_t *
460 _hb_uniscribe_shaper_face_data_create (hb_face_t *face)
461 {
462   hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
463   if (unlikely (!data))
464     return nullptr;
465
466   data->funcs = hb_uniscribe_shaper_get_funcs ();
467   if (unlikely (!data->funcs))
468   {
469     free (data);
470     return nullptr;
471   }
472
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");
476
477   blob = _hb_rename_font (blob, data->face_name);
478   if (unlikely (!blob))
479   {
480     free (data);
481     return nullptr;
482   }
483
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))
489   {
490     DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
491     free (data);
492     return nullptr;
493   }
494
495   return data;
496 }
497
498 void
499 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_face_data_t *data)
500 {
501   RemoveFontMemResourceEx (data->fh);
502   free (data);
503 }
504
505
506 /*
507  * shaper font data
508  */
509
510 struct hb_uniscribe_font_data_t
511 {
512   HDC hdc;
513   mutable LOGFONTW log_font;
514   HFONT hfont;
515   mutable SCRIPT_CACHE script_cache;
516   double x_mult, y_mult; /* From LOGFONT space to HB space. */
517 };
518
519 static bool
520 populate_log_font (LOGFONTW  *lf,
521                    hb_font_t *font,
522                    unsigned int font_size)
523 {
524   memset (lf, 0, sizeof (*lf));
525   lf->lfHeight = - (int) font_size;
526   lf->lfCharSet = DEFAULT_CHARSET;
527
528   memcpy (lf->lfFaceName, font->face->data.uniscribe->face_name, sizeof (lf->lfFaceName));
529
530   return true;
531 }
532
533 hb_uniscribe_font_data_t *
534 _hb_uniscribe_shaper_font_data_create (hb_font_t *font)
535 {
536   hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
537   if (unlikely (!data))
538     return nullptr;
539
540   int font_size = font->face->get_upem (); /* Default... */
541   /* No idea if the following is even a good idea. */
542   if (font->y_ppem)
543     font_size = font->y_ppem;
544
545   if (font_size < 0)
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;
549
550   data->hdc = GetDC (nullptr);
551
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);
555     return nullptr;
556   }
557
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);
562      return nullptr;
563   }
564
565   if (!SelectObject (data->hdc, data->hfont)) {
566     DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
567     _hb_uniscribe_shaper_font_data_destroy (data);
568      return nullptr;
569   }
570
571   return data;
572 }
573
574 void
575 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_font_data_t *data)
576 {
577   if (data->hdc)
578     ReleaseDC (nullptr, data->hdc);
579   if (data->hfont)
580     DeleteObject (data->hfont);
581   if (data->script_cache)
582     ScriptFreeCache (&data->script_cache);
583   free (data);
584 }
585
586 LOGFONTW *
587 hb_uniscribe_font_get_logfontw (hb_font_t *font)
588 {
589   const hb_uniscribe_font_data_t *data =  font->data.uniscribe;
590   return data ? &data->log_font : nullptr;
591 }
592
593 HFONT
594 hb_uniscribe_font_get_hfont (hb_font_t *font)
595 {
596   const hb_uniscribe_font_data_t *data =  font->data.uniscribe;
597   return data ? data->hfont : nullptr;
598 }
599
600
601 /*
602  * shaper
603  */
604
605
606 hb_bool_t
607 _hb_uniscribe_shape (hb_shape_plan_t    *shape_plan,
608                      hb_font_t          *font,
609                      hb_buffer_t        *buffer,
610                      const hb_feature_t *features,
611                      unsigned int        num_features)
612 {
613   hb_face_t *face = font->face;
614   const hb_uniscribe_face_data_t *face_data = face->data.uniscribe;
615   const hb_uniscribe_font_data_t *font_data = font->data.uniscribe;
616   hb_uniscribe_shaper_funcs_t *funcs = face_data->funcs;
617
618   /*
619    * Set up features.
620    */
621   hb_vector_t<OPENTYPE_FEATURE_RECORD> feature_records;
622   hb_vector_t<range_record_t> range_records;
623   if (num_features)
624   {
625     /* Sort features by start/end events. */
626     hb_vector_t<feature_event_t> feature_events;
627     for (unsigned int i = 0; i < num_features; i++)
628     {
629       active_feature_t feature;
630       feature.rec.tagFeature = hb_uint32_swap (features[i].tag);
631       feature.rec.lParameter = features[i].value;
632       feature.order = i;
633
634       feature_event_t *event;
635
636       event = feature_events.push ();
637       event->index = features[i].start;
638       event->start = true;
639       event->feature = feature;
640
641       event = feature_events.push ();
642       event->index = features[i].end;
643       event->start = false;
644       event->feature = feature;
645     }
646     feature_events.qsort ();
647     /* Add a strategic final event. */
648     {
649       active_feature_t feature;
650       feature.rec.tagFeature = 0;
651       feature.rec.lParameter = 0;
652       feature.order = num_features + 1;
653
654       feature_event_t *event = feature_events.push ();
655       event->index = 0; /* This value does magic. */
656       event->start = false;
657       event->feature = feature;
658     }
659
660     /* Scan events and save features for each range. */
661     hb_vector_t<active_feature_t> active_features;
662     unsigned int last_index = 0;
663     for (unsigned int i = 0; i < feature_events.length; i++)
664     {
665       feature_event_t *event = &feature_events[i];
666
667       if (event->index != last_index)
668       {
669         /* Save a snapshot of active features and the range. */
670         range_record_t *range = range_records.push ();
671
672         unsigned int offset = feature_records.length;
673
674         active_features.qsort ();
675         for (unsigned int j = 0; j < active_features.length; j++)
676         {
677           if (!j || active_features[j].rec.tagFeature != feature_records[feature_records.length - 1].tagFeature)
678           {
679             feature_records.push (active_features[j].rec);
680           }
681           else
682           {
683             /* Overrides value for existing feature. */
684             feature_records[feature_records.length - 1].lParameter = active_features[j].rec.lParameter;
685           }
686         }
687
688         /* Will convert to pointer after all is ready, since feature_records.array
689          * may move as we grow it. */
690         range->props.potfRecords = reinterpret_cast<OPENTYPE_FEATURE_RECORD *> (offset);
691         range->props.cotfRecords = feature_records.length - offset;
692         range->index_first = last_index;
693         range->index_last  = event->index - 1;
694
695         last_index = event->index;
696       }
697
698       if (event->start)
699       {
700         active_features.push (event->feature);
701       }
702       else
703       {
704         active_feature_t *feature = active_features.find (&event->feature);
705         if (feature)
706           active_features.remove (feature - active_features.arrayZ);
707       }
708     }
709
710     if (!range_records.length) /* No active feature found. */
711       num_features = 0;
712
713     /* Fixup the pointers. */
714     for (unsigned int i = 0; i < range_records.length; i++)
715     {
716       range_record_t *range = &range_records[i];
717       range->props.potfRecords = (OPENTYPE_FEATURE_RECORD *) feature_records + reinterpret_cast<uintptr_t> (range->props.potfRecords);
718     }
719   }
720
721 #define FAIL(...) \
722   HB_STMT_START { \
723     DEBUG_MSG (UNISCRIBE, nullptr, __VA_ARGS__); \
724     return false; \
725   } HB_STMT_END
726
727   HRESULT hr;
728
729 retry:
730
731   unsigned int scratch_size;
732   hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size);
733
734 #define ALLOCATE_ARRAY(Type, name, len) \
735   Type *name = (Type *) scratch; \
736   do { \
737     unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \
738     assert (_consumed <= scratch_size); \
739     scratch += _consumed; \
740     scratch_size -= _consumed; \
741   } while (0)
742
743 #define utf16_index() var1.u32
744
745   ALLOCATE_ARRAY (WCHAR, pchars, buffer->len * 2);
746
747   unsigned int chars_len = 0;
748   for (unsigned int i = 0; i < buffer->len; i++)
749   {
750     hb_codepoint_t c = buffer->info[i].codepoint;
751     buffer->info[i].utf16_index() = chars_len;
752     if (likely (c <= 0xFFFFu))
753       pchars[chars_len++] = c;
754     else if (unlikely (c > 0x10FFFFu))
755       pchars[chars_len++] = 0xFFFDu;
756     else {
757       pchars[chars_len++] = 0xD800u + ((c - 0x10000u) >> 10);
758       pchars[chars_len++] = 0xDC00u + ((c - 0x10000u) & ((1u << 10) - 1));
759     }
760   }
761
762   ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
763   ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
764
765   if (num_features)
766   {
767     /* Need log_clusters to assign features. */
768     chars_len = 0;
769     for (unsigned int i = 0; i < buffer->len; i++)
770     {
771       hb_codepoint_t c = buffer->info[i].codepoint;
772       unsigned int cluster = buffer->info[i].cluster;
773       log_clusters[chars_len++] = cluster;
774       if (hb_in_range (c, 0x10000u, 0x10FFFFu))
775         log_clusters[chars_len++] = cluster; /* Surrogates. */
776     }
777   }
778
779   /* The -2 in the following is to compensate for possible
780    * alignment needed after the WORD array.  sizeof(WORD) == 2. */
781   unsigned int glyphs_size = (scratch_size * sizeof (int) - 2)
782                            / (sizeof (WORD) +
783                               sizeof (SCRIPT_GLYPHPROP) +
784                               sizeof (int) +
785                               sizeof (GOFFSET) +
786                               sizeof (uint32_t));
787
788   ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
789   ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
790   ALLOCATE_ARRAY (int, advances, glyphs_size);
791   ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
792   ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
793
794   /* Note:
795    * We can't touch the contents of glyph_props.  Our fallback
796    * implementations of Shape and Place functions use that buffer
797    * by casting it to a different type.  It works because they
798    * both agree about it, but if we want to access it here we
799    * need address that issue first.
800    */
801
802 #undef ALLOCATE_ARRAY
803
804 #define MAX_ITEMS 256
805
806   SCRIPT_ITEM items[MAX_ITEMS + 1];
807   SCRIPT_CONTROL bidi_control = {0};
808   SCRIPT_STATE bidi_state = {0};
809   ULONG script_tags[MAX_ITEMS];
810   int item_count;
811
812   /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
813   //bidi_control.fMergeNeutralItems = true;
814   *(uint32_t*)&bidi_control |= 1u<<24;
815
816   bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
817   bidi_state.fOverrideDirection = 1;
818
819   hr = funcs->ScriptItemizeOpenType (pchars,
820                                      chars_len,
821                                      MAX_ITEMS,
822                                      &bidi_control,
823                                      &bidi_state,
824                                      items,
825                                      script_tags,
826                                      &item_count);
827   if (unlikely (FAILED (hr)))
828     FAIL ("ScriptItemizeOpenType() failed: 0x%08lx", hr);
829
830 #undef MAX_ITEMS
831
832   hb_tag_t lang_tag;
833   unsigned int lang_count = 1;
834   hb_ot_tags_from_script_and_language (buffer->props.script,
835                                        buffer->props.language,
836                                        nullptr, nullptr,
837                                        &lang_count, &lang_tag);
838   OPENTYPE_TAG language_tag = hb_uint32_swap (lang_count ? lang_tag : HB_TAG_NONE);
839   hb_vector_t<TEXTRANGE_PROPERTIES*> range_properties;
840   hb_vector_t<int> range_char_counts;
841
842   unsigned int glyphs_offset = 0;
843   unsigned int glyphs_len;
844   bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
845   for (int i = 0; i < item_count; i++)
846   {
847     unsigned int chars_offset = items[i].iCharPos;
848     unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
849
850     if (num_features)
851     {
852       range_properties.shrink (0);
853       range_char_counts.shrink (0);
854
855       range_record_t *last_range = &range_records[0];
856
857       for (unsigned int k = chars_offset; k < chars_offset + item_chars_len; k++)
858       {
859         range_record_t *range = last_range;
860         while (log_clusters[k] < range->index_first)
861           range--;
862         while (log_clusters[k] > range->index_last)
863           range++;
864         if (!range_properties.length ||
865             &range->props != range_properties[range_properties.length - 1])
866         {
867           TEXTRANGE_PROPERTIES **props = range_properties.push ();
868           int *c = range_char_counts.push ();
869           if (unlikely (!props || !c))
870           {
871             range_properties.shrink (0);
872             range_char_counts.shrink (0);
873             break;
874           }
875           *props = &range->props;
876           *c = 1;
877         }
878         else
879         {
880           range_char_counts[range_char_counts.length - 1]++;
881         }
882
883         last_range = range;
884       }
885     }
886
887     /* Asking for glyphs in logical order circumvents at least
888      * one bug in Uniscribe. */
889     items[i].a.fLogicalOrder = true;
890
891   retry_shape:
892     hr = funcs->ScriptShapeOpenType (font_data->hdc,
893                                      &font_data->script_cache,
894                                      &items[i].a,
895                                      script_tags[i],
896                                      language_tag,
897                                      range_char_counts.arrayZ,
898                                      range_properties.arrayZ,
899                                      range_properties.length,
900                                      pchars + chars_offset,
901                                      item_chars_len,
902                                      glyphs_size - glyphs_offset,
903                                      /* out */
904                                      log_clusters + chars_offset,
905                                      char_props + chars_offset,
906                                      glyphs + glyphs_offset,
907                                      glyph_props + glyphs_offset,
908                                      (int *) &glyphs_len);
909
910     if (unlikely (items[i].a.fNoGlyphIndex))
911       FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
912     if (unlikely (hr == E_OUTOFMEMORY || hr == E_NOT_SUFFICIENT_BUFFER))
913     {
914       if (unlikely (!buffer->ensure (buffer->allocated * 2)))
915         FAIL ("Buffer resize failed");
916       goto retry;
917     }
918     if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
919     {
920       if (items[i].a.eScript == SCRIPT_UNDEFINED)
921         FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
922       items[i].a.eScript = SCRIPT_UNDEFINED;
923       goto retry_shape;
924     }
925     if (unlikely (FAILED (hr)))
926     {
927       FAIL ("ScriptShapeOpenType() failed: 0x%08lx", hr);
928     }
929
930     for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
931       log_clusters[j] += glyphs_offset;
932
933     hr = funcs->ScriptPlaceOpenType (font_data->hdc,
934                                      &font_data->script_cache,
935                                      &items[i].a,
936                                      script_tags[i],
937                                      language_tag,
938                                      range_char_counts.arrayZ,
939                                      range_properties.arrayZ,
940                                      range_properties.length,
941                                      pchars + chars_offset,
942                                      log_clusters + chars_offset,
943                                      char_props + chars_offset,
944                                      item_chars_len,
945                                      glyphs + glyphs_offset,
946                                      glyph_props + glyphs_offset,
947                                      glyphs_len,
948                                      /* out */
949                                      advances + glyphs_offset,
950                                      offsets + glyphs_offset,
951                                      nullptr);
952     if (unlikely (FAILED (hr)))
953       FAIL ("ScriptPlaceOpenType() failed: 0x%08lx", hr);
954
955     if (DEBUG_ENABLED (UNISCRIBE))
956       fprintf (stderr, "Item %d RTL %d LayoutRTL %d LogicalOrder %d ScriptTag %c%c%c%c\n",
957                i,
958                items[i].a.fRTL,
959                items[i].a.fLayoutRTL,
960                items[i].a.fLogicalOrder,
961                HB_UNTAG (hb_uint32_swap (script_tags[i])));
962
963     glyphs_offset += glyphs_len;
964   }
965   glyphs_len = glyphs_offset;
966
967   /* Ok, we've got everything we need, now compose output buffer,
968    * very, *very*, carefully! */
969
970   /* Calculate visual-clusters.  That's what we ship. */
971   for (unsigned int i = 0; i < glyphs_len; i++)
972     vis_clusters[i] = (uint32_t) -1;
973   for (unsigned int i = 0; i < buffer->len; i++) {
974     uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
975     *p = hb_min (*p, buffer->info[i].cluster);
976   }
977   for (unsigned int i = 1; i < glyphs_len; i++)
978     if (vis_clusters[i] == (uint32_t) -1)
979       vis_clusters[i] = vis_clusters[i - 1];
980
981 #undef utf16_index
982
983   if (unlikely (!buffer->ensure (glyphs_len)))
984     FAIL ("Buffer in error");
985
986 #undef FAIL
987
988   /* Set glyph infos */
989   buffer->len = 0;
990   for (unsigned int i = 0; i < glyphs_len; i++)
991   {
992     hb_glyph_info_t *info = &buffer->info[buffer->len++];
993
994     info->codepoint = glyphs[i];
995     info->cluster = vis_clusters[i];
996
997     /* The rest is crap.  Let's store position info there for now. */
998     info->mask = advances[i];
999     info->var1.i32 = offsets[i].du;
1000     info->var2.i32 = offsets[i].dv;
1001   }
1002
1003   /* Set glyph positions */
1004   buffer->clear_positions ();
1005   double x_mult = font_data->x_mult, y_mult = font_data->y_mult;
1006   for (unsigned int i = 0; i < glyphs_len; i++)
1007   {
1008     hb_glyph_info_t *info = &buffer->info[i];
1009     hb_glyph_position_t *pos = &buffer->pos[i];
1010
1011     /* TODO vertical */
1012     pos->x_advance = x_mult * (int32_t) info->mask;
1013     pos->x_offset = x_mult * (backward ? -info->var1.i32 : info->var1.i32);
1014     pos->y_offset = y_mult * info->var2.i32;
1015   }
1016
1017   if (backward)
1018     hb_buffer_reverse (buffer);
1019
1020   buffer->unsafe_to_break_all ();
1021
1022   /* Wow, done! */
1023   return true;
1024 }
1025
1026
1027 #endif