hb-uniscribe.cc revision cfe9882610489e1b917e09a74dfbf6bbba2e4a57
1/*
2 * Copyright © 2011,2012  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#define _WIN32_WINNT 0x0600
28
29#define HB_SHAPER uniscribe
30#include "hb-shaper-impl-private.hh"
31
32#include <windows.h>
33#include <usp10.h>
34
35typedef ULONG WIN_ULONG;
36
37#include "hb-uniscribe.h"
38
39#include "hb-ot-name-table.hh"
40#include "hb-ot-tag.h"
41
42
43#ifndef HB_DEBUG_UNISCRIBE
44#define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
45#endif
46
47
48/*
49DWORD GetFontData(
50  __in   HDC hdc,
51  __in   DWORD dwTable,
52  __in   DWORD dwOffset,
53  __out  LPVOID lpvBuffer,
54  __in   DWORD cbData
55);
56*/
57
58
59HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face)
60HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font)
61hb_bool_t
62hb_uniscribe_font_ensure (hb_font_t *font)
63{
64  hb_face_t *face = font->face;
65  return hb_uniscribe_shaper_face_data_ensure (face) &&
66         hb_uniscribe_shaper_font_data_ensure (font);
67}
68
69
70/*
71 * shaper face data
72 */
73
74struct hb_uniscribe_shaper_face_data_t {
75  HANDLE fh;
76};
77
78hb_uniscribe_shaper_face_data_t *
79_hb_uniscribe_shaper_face_data_create (hb_face_t *face)
80{
81  hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
82  if (unlikely (!data))
83    return NULL;
84
85  hb_blob_t *blob = hb_face_reference_blob (face);
86  unsigned int blob_length;
87  const char *blob_data = hb_blob_get_data (blob, &blob_length);
88  if (unlikely (!blob_length))
89    DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
90
91  DWORD num_fonts_installed;
92  data->fh = AddFontMemResourceEx ((void *) blob_data, blob_length, 0, &num_fonts_installed);
93  hb_blob_destroy (blob);
94  if (unlikely (!data->fh)) {
95    DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
96    free (data);
97    return NULL;
98  }
99
100  return data;
101}
102
103void
104_hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
105{
106  if (data->fh)
107    RemoveFontMemResourceEx (data->fh);
108  free (data);
109}
110
111
112/*
113 * shaper font data
114 */
115
116struct hb_uniscribe_shaper_font_data_t {
117  HDC hdc;
118  LOGFONTW log_font;
119  HFONT hfont;
120  SCRIPT_CACHE script_cache;
121};
122
123static bool
124populate_log_font (LOGFONTW  *lf,
125		   hb_font_t *font)
126{
127  memset (lf, 0, sizeof (*lf));
128  lf->lfHeight = -font->y_scale;
129  lf->lfCharSet = DEFAULT_CHARSET;
130
131  hb_blob_t *blob = Sanitizer<name>::sanitize (hb_face_reference_table (font->face, HB_TAG ('n','a','m','e')));
132  const name *name_table = Sanitizer<name>::lock_instance (blob);
133  unsigned int len = name_table->get_name (3, 1, 0x409, 4,
134					   lf->lfFaceName,
135					   sizeof (lf->lfFaceName[0]) * LF_FACESIZE)
136					  / sizeof (lf->lfFaceName[0]);
137  hb_blob_destroy (blob);
138
139  if (unlikely (!len)) {
140    DEBUG_MSG (UNISCRIBE, NULL, "Didn't find English name table entry");
141    return false;
142  }
143  if (unlikely (len >= LF_FACESIZE)) {
144    DEBUG_MSG (UNISCRIBE, NULL, "Font name too long");
145    return false;
146  }
147
148  for (unsigned int i = 0; i < len; i++)
149    lf->lfFaceName[i] = hb_be_uint16 (lf->lfFaceName[i]);
150  lf->lfFaceName[len] = 0;
151
152  return true;
153}
154
155hb_uniscribe_shaper_font_data_t *
156_hb_uniscribe_shaper_font_data_create (hb_font_t *font)
157{
158  hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
159  if (unlikely (!data))
160    return NULL;
161
162  data->hdc = GetDC (NULL);
163
164  if (unlikely (!populate_log_font (&data->log_font, font))) {
165    DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
166    _hb_uniscribe_shaper_font_data_destroy (data);
167    return NULL;
168  }
169
170  data->hfont = CreateFontIndirectW (&data->log_font);
171  if (unlikely (!data->hfont)) {
172    DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
173    _hb_uniscribe_shaper_font_data_destroy (data);
174     return NULL;
175  }
176
177  if (!SelectObject (data->hdc, data->hfont)) {
178    DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
179    _hb_uniscribe_shaper_font_data_destroy (data);
180     return NULL;
181  }
182
183  return data;
184}
185
186void
187_hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
188{
189  if (data->hdc)
190    ReleaseDC (NULL, data->hdc);
191  if (data->hfont)
192    DeleteObject (data->hfont);
193  if (data->script_cache)
194    ScriptFreeCache (&data->script_cache);
195  free (data);
196}
197
198
199/*
200 * shaper shape_plan data
201 */
202
203struct hb_uniscribe_shaper_shape_plan_data_t {};
204
205hb_uniscribe_shaper_shape_plan_data_t *
206_hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t    *shape_plan,
207					     const hb_feature_t *user_features,
208					     unsigned int        num_user_features)
209{
210  return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
211}
212
213void
214_hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data)
215{
216}
217
218
219/*
220 * shaper
221 */
222
223LOGFONTW *
224hb_uniscribe_font_get_logfontw (hb_font_t *font)
225{
226  hb_uniscribe_shaper_font_data_t *font_data =  HB_SHAPER_DATA_GET (font);
227  return &font_data->log_font;
228}
229
230HFONT
231hb_uniscribe_font_get_hfont (hb_font_t *font)
232{
233  hb_uniscribe_shaper_font_data_t *font_data =  HB_SHAPER_DATA_GET (font);
234  return font_data->hfont;
235}
236
237
238hb_bool_t
239_hb_uniscribe_shape (hb_shape_plan_t    *shape_plan,
240		     hb_font_t          *font,
241		     hb_buffer_t        *buffer,
242		     const hb_feature_t *features,
243		     unsigned int        num_features)
244{
245  hb_face_t *face = font->face;
246  hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
247  hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
248
249#define FAIL(...) \
250  HB_STMT_START { \
251    DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
252    return false; \
253  } HB_STMT_END;
254
255  HRESULT hr;
256
257retry:
258
259  unsigned int scratch_size;
260  char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
261
262  /* Allocate char buffers; they all fit */
263
264#define ALLOCATE_ARRAY(Type, name, len) \
265  Type *name = (Type *) scratch; \
266  scratch += (len) * sizeof ((name)[0]); \
267  scratch_size -= (len) * sizeof ((name)[0]);
268
269#define utf16_index() var1.u32
270
271  WCHAR *pchars = (WCHAR *) scratch;
272  unsigned int chars_len = 0;
273  for (unsigned int i = 0; i < buffer->len; i++) {
274    hb_codepoint_t c = buffer->info[i].codepoint;
275    buffer->info[i].utf16_index() = chars_len;
276    if (likely (c < 0x10000))
277      pchars[chars_len++] = c;
278    else if (unlikely (c >= 0x110000))
279      pchars[chars_len++] = 0xFFFD;
280    else {
281      pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10);
282      pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1));
283    }
284  }
285
286  ALLOCATE_ARRAY (WCHAR, wchars, chars_len);
287  ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
288  ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
289
290  /* On Windows, we don't care about alignment...*/
291  unsigned int glyphs_size = scratch_size / (sizeof (WORD) +
292					     sizeof (SCRIPT_GLYPHPROP) +
293					     sizeof (int) +
294					     sizeof (GOFFSET) +
295					     sizeof (uint32_t));
296
297  ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
298  ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
299  ALLOCATE_ARRAY (int, advances, glyphs_size);
300  ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
301  ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
302
303#undef ALLOCATE_ARRAY
304
305#define MAX_ITEMS 256
306
307  SCRIPT_ITEM items[MAX_ITEMS + 1];
308  SCRIPT_CONTROL bidi_control = {0};
309  SCRIPT_STATE bidi_state = {0};
310  WIN_ULONG script_tags[MAX_ITEMS];
311  int item_count;
312
313  /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
314  //bidi_control.fMergeNeutralItems = true;
315  *(uint32_t*)&bidi_control |= 1<<24;
316
317  bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
318  bidi_state.fOverrideDirection = 1;
319
320  hr = ScriptItemizeOpenType (wchars,
321			      chars_len,
322			      MAX_ITEMS,
323			      &bidi_control,
324			      &bidi_state,
325			      items,
326			      script_tags,
327			      &item_count);
328  if (unlikely (FAILED (hr)))
329    FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
330
331#undef MAX_ITEMS
332
333  int *range_char_counts = NULL;
334  TEXTRANGE_PROPERTIES **range_properties = NULL;
335  int range_count = 0;
336  if (num_features) {
337    /* TODO setup ranges */
338  }
339
340  OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
341
342  unsigned int glyphs_offset = 0;
343  unsigned int glyphs_len;
344  bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
345  for (unsigned int j = 0; j < item_count; j++)
346  {
347    unsigned int i = backward ? item_count - 1 - j : j;
348    unsigned int chars_offset = items[i].iCharPos;
349    unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
350
351  retry_shape:
352    hr = ScriptShapeOpenType (font_data->hdc,
353			      &font_data->script_cache,
354			      &items[i].a,
355			      script_tags[i],
356			      language_tag,
357			      range_char_counts,
358			      range_properties,
359			      range_count,
360			      wchars + chars_offset,
361			      item_chars_len,
362			      glyphs_size - glyphs_offset,
363			      /* out */
364			      log_clusters + chars_offset,
365			      char_props + chars_offset,
366			      glyphs + glyphs_offset,
367			      glyph_props + glyphs_offset,
368			      (int *) &glyphs_len);
369
370    if (unlikely (items[i].a.fNoGlyphIndex))
371      FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
372    if (unlikely (hr == E_OUTOFMEMORY))
373    {
374      buffer->ensure (buffer->allocated * 2);
375      if (buffer->in_error)
376	FAIL ("Buffer resize failed");
377      goto retry;
378    }
379    if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
380    {
381      if (items[i].a.eScript == SCRIPT_UNDEFINED)
382	FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
383      items[i].a.eScript = SCRIPT_UNDEFINED;
384      goto retry_shape;
385    }
386    if (unlikely (FAILED (hr)))
387    {
388      FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
389    }
390
391    for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
392      log_clusters[j] += glyphs_offset;
393
394    hr = ScriptPlaceOpenType (font_data->hdc,
395			      &font_data->script_cache,
396			      &items[i].a,
397			      script_tags[i],
398			      language_tag,
399			      range_char_counts,
400			      range_properties,
401			      range_count,
402			      wchars + chars_offset,
403			      log_clusters + chars_offset,
404			      char_props + chars_offset,
405			      item_chars_len,
406			      glyphs + glyphs_offset,
407			      glyph_props + glyphs_offset,
408			      glyphs_len,
409			      /* out */
410			      advances + glyphs_offset,
411			      offsets + glyphs_offset,
412			      NULL);
413    if (unlikely (FAILED (hr)))
414      FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
415
416    glyphs_offset += glyphs_len;
417  }
418  glyphs_len = glyphs_offset;
419
420  /* Ok, we've got everything we need, now compose output buffer,
421   * very, *very*, carefully! */
422
423  /* Calculate visual-clusters.  That's what we ship. */
424  for (unsigned int i = 0; i < glyphs_len; i++)
425    vis_clusters[i] = -1;
426  for (unsigned int i = 0; i < buffer->len; i++) {
427    uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
428    *p = MIN (*p, buffer->info[i].cluster);
429  }
430  if (!backward) {
431    for (unsigned int i = 1; i < glyphs_len; i++)
432      if (vis_clusters[i] == -1)
433	vis_clusters[i] = vis_clusters[i - 1];
434  } else {
435    for (int i = glyphs_len - 2; i >= 0; i--)
436      if (vis_clusters[i] == -1)
437	vis_clusters[i] = vis_clusters[i + 1];
438  }
439
440#undef utf16_index
441
442  buffer->ensure (glyphs_len);
443  if (buffer->in_error)
444    FAIL ("Buffer in error");
445
446#undef FAIL
447
448  /* Set glyph infos */
449  buffer->len = 0;
450  for (unsigned int i = 0; i < glyphs_len; i++)
451  {
452    hb_glyph_info_t *info = &buffer->info[buffer->len++];
453
454    info->codepoint = glyphs[i];
455    info->cluster = vis_clusters[i];
456
457    /* The rest is crap.  Let's store position info there for now. */
458    info->mask = advances[i];
459    info->var1.u32 = offsets[i].du;
460    info->var2.u32 = offsets[i].dv;
461  }
462
463  /* Set glyph positions */
464  buffer->clear_positions ();
465  for (unsigned int i = 0; i < glyphs_len; i++)
466  {
467    hb_glyph_info_t *info = &buffer->info[i];
468    hb_glyph_position_t *pos = &buffer->pos[i];
469
470    /* TODO vertical */
471    pos->x_advance = info->mask;
472    pos->x_offset = info->var1.u32;
473    pos->y_offset = info->var2.u32;
474  }
475
476  /* Wow, done! */
477  return true;
478}
479
480
481