blob: a0020cacfc074ab840686b3043d104d5a32852e1 [file] [log] [blame]
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001/*
2 * Copyright © 2011 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 0x0500
28
29#include "hb-private.hh"
30
Behdad Esfahbodb4922992011-08-05 20:34:50 -040031#include <windows.h>
32#include <usp10.h>
33
34typedef ULONG WIN_ULONG;
35
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040036#include "hb-uniscribe.h"
37
Behdad Esfahbodb4922992011-08-05 20:34:50 -040038#include "hb-ot-name-private.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040039#include "hb-ot-tag.h"
40
41#include "hb-font-private.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040042#include "hb-buffer-private.hh"
43
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040044
45
46#ifndef HB_DEBUG_UNISCRIBE
47#define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
48#endif
49
50
51/*
52DWORD GetFontData(
53 __in HDC hdc,
54 __in DWORD dwTable,
55 __in DWORD dwOffset,
56 __out LPVOID lpvBuffer,
57 __in DWORD cbData
58);
59*/
60
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040061static bool
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040062populate_log_font (LOGFONTW *lf,
63 HDC hdc,
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040064 hb_font_t *font)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040065{
66 memset (lf, 0, sizeof (*lf));
67 int dpi = GetDeviceCaps (hdc, LOGPIXELSY);
Behdad Esfahbod3fd2b5b2011-08-06 22:59:54 -040068 lf->lfHeight = -font->y_scale;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040069
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040070 hb_blob_t *blob = Sanitizer<name>::sanitize (hb_face_reference_table (font->face, HB_TAG ('n','a','m','e')));
71 const name *name_table = Sanitizer<name>::lock_instance (blob);
72 unsigned int len = name_table->get_name (3, 1, 0x409, 4,
73 lf->lfFaceName,
74 sizeof (lf->lfFaceName[0]) * LF_FACESIZE)
75 / sizeof (lf->lfFaceName[0]);
Behdad Esfahbod826e2272011-08-07 03:53:42 -040076 hb_blob_destroy (blob);
77
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040078 if (unlikely (!len)) {
79 DEBUG_MSG (UNISCRIBE, NULL, "Didn't find English name table entry");
80 return FALSE;
81 }
82 if (unlikely (len >= LF_FACESIZE)) {
83 DEBUG_MSG (UNISCRIBE, NULL, "Font name too long");
84 return FALSE;
85 }
Behdad Esfahbode9c71fa2011-08-07 00:00:27 -040086
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040087 for (unsigned int i = 0; i < len; i++)
88 lf->lfFaceName[i] = hb_be_uint16 (lf->lfFaceName[i]);
89 lf->lfFaceName[len] = 0;
Behdad Esfahbode9c71fa2011-08-07 00:00:27 -040090
Behdad Esfahbod892eb2e2011-08-06 22:06:52 -040091 return TRUE;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040092}
93
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020094
Behdad Esfahbod255f1762011-08-09 08:35:07 +020095static struct hb_uniscribe_face_data_t {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020096 HANDLE fh;
97} _hb_uniscribe_face_data_nil = {0};
98
99static hb_user_data_key_t uniscribe_face_data_key;
100
101static void
102_hb_uniscribe_face_data_destroy (hb_uniscribe_face_data_t *data)
103{
104 if (data->fh)
105 RemoveFontMemResourceEx (data->fh);
106 free (data);
107}
108
109static hb_uniscribe_face_data_t *
110_hb_uniscribe_face_get_data (hb_face_t *face)
111{
112 hb_uniscribe_face_data_t *data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_face_data_key);
113 if (likely (data)) return data;
114
115 data = (hb_uniscribe_face_data_t *) calloc (1, sizeof (hb_uniscribe_face_data_t));
116 if (unlikely (!data))
117 return &_hb_uniscribe_face_data_nil;
118
119 hb_blob_t *blob = hb_face_reference_blob (face);
120 unsigned int blob_length;
121 const char *blob_data = hb_blob_get_data (blob, &blob_length);
122 if (unlikely (!blob_length))
123 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
124
125 DWORD num_fonts_installed;
126 data->fh = AddFontMemResourceEx ((void *) blob_data, blob_length, 0, &num_fonts_installed);
127 hb_blob_destroy (blob);
128 if (unlikely (!data->fh))
129 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
130
131 if (unlikely (!hb_face_set_user_data (face, &uniscribe_face_data_key, data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200132 (hb_destroy_func_t) _hb_uniscribe_face_data_destroy,
133 FALSE)))
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200134 {
135 _hb_uniscribe_face_data_destroy (data);
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200136 data = (hb_uniscribe_face_data_t *) hb_face_get_user_data (face, &uniscribe_face_data_key);
137 if (data)
138 return data;
139 else
140 return &_hb_uniscribe_face_data_nil;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200141 }
142
143 return data;
144}
145
146
147static struct hb_uniscribe_font_data_t {
148 HDC hdc;
149 HFONT hfont;
150 SCRIPT_CACHE script_cache;
151} _hb_uniscribe_font_data_nil = {NULL, NULL, NULL};
152
153static hb_user_data_key_t uniscribe_font_data_key;
154
155static void
156_hb_uniscribe_font_data_destroy (hb_uniscribe_font_data_t *data)
157{
158 if (data->hdc)
159 ReleaseDC (NULL, data->hdc);
160 if (data->hfont)
161 DeleteObject (data->hfont);
162 if (data->script_cache)
163 ScriptFreeCache (&data->script_cache);
164 free (data);
165}
166
167static hb_uniscribe_font_data_t *
168_hb_uniscribe_font_get_data (hb_font_t *font)
169{
170 hb_uniscribe_font_data_t *data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_font_data_key);
171 if (likely (data)) return data;
172
173 data = (hb_uniscribe_font_data_t *) calloc (1, sizeof (hb_uniscribe_font_data_t));
174 if (unlikely (!data))
175 return &_hb_uniscribe_font_data_nil;
176
177 data->hdc = GetDC (NULL);
178
179 LOGFONTW log_font;
180 if (unlikely (!populate_log_font (&log_font, data->hdc, font)))
181 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
182 else {
183 data->hfont = CreateFontIndirectW (&log_font);
184 if (unlikely (!data->hfont))
185 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
186 if (!SelectObject (data->hdc, data->hfont))
187 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
188 }
189
190 if (unlikely (!hb_font_set_user_data (font, &uniscribe_font_data_key, data,
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200191 (hb_destroy_func_t) _hb_uniscribe_font_data_destroy,
192 FALSE)))
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200193 {
194 _hb_uniscribe_font_data_destroy (data);
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200195 data = (hb_uniscribe_font_data_t *) hb_font_get_user_data (font, &uniscribe_font_data_key);
196 if (data)
197 return data;
198 else
199 return &_hb_uniscribe_font_data_nil;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200200 }
201
202 return data;
203}
204
205
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400206hb_bool_t
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400207hb_uniscribe_shape (hb_font_t *font,
208 hb_buffer_t *buffer,
209 const hb_feature_t *features,
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400210 unsigned int num_features,
211 const char *shaper_options)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400212{
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400213 buffer->guess_properties ();
214
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200215#define FAIL(...) \
216 HB_STMT_START { \
217 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
218 return FALSE; \
219 } HB_STMT_END;
220
221 hb_uniscribe_face_data_t *face_data = _hb_uniscribe_face_get_data (font->face);
Behdad Esfahboda9057eb2011-08-09 00:47:55 +0200222 if (unlikely (!face_data->fh))
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200223 FAIL ("Couldn't get face data");
224
225 hb_uniscribe_font_data_t *font_data = _hb_uniscribe_font_get_data (font);
Behdad Esfahboda9057eb2011-08-09 00:47:55 +0200226 if (unlikely (!font_data->hfont))
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200227 FAIL ("Couldn't get font font");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400228
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400229 if (unlikely (!buffer->len))
230 return TRUE;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400231
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200232 HRESULT hr;
233
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400234retry:
235
236 unsigned int scratch_size;
237 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
238
239 /* Allocate char buffers; they all fit */
240
241#define ALLOCATE_ARRAY(Type, name, len) \
242 Type *name = (Type *) scratch; \
243 scratch += len * sizeof (name[0]); \
244 scratch_size -= len * sizeof (name[0]);
245
246#define utf16_index() var1.u32
247
248 WCHAR *pchars = (WCHAR *) scratch;
249 unsigned int chars_len = 0;
250 for (unsigned int i = 0; i < buffer->len; i++) {
251 hb_codepoint_t c = buffer->info[i].codepoint;
252 buffer->info[i].utf16_index() = chars_len;
253 if (likely (c < 0x10000))
254 pchars[chars_len++] = c;
255 else if (unlikely (c >= 0x110000))
256 pchars[chars_len++] = 0xFFFD;
257 else {
258 pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10);
259 pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1));
260 }
261 }
262
263 ALLOCATE_ARRAY (WCHAR, wchars, chars_len);
264 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
265 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
266
267 /* On Windows, we don't care about alignment...*/
268 unsigned int glyphs_size = scratch_size / (sizeof (WORD) +
269 sizeof (SCRIPT_GLYPHPROP) +
270 sizeof (int) +
271 sizeof (GOFFSET) +
272 sizeof (uint32_t));
273
274 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
275 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
276 ALLOCATE_ARRAY (int, advances, glyphs_size);
277 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
278 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
279
280
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400281#define MAX_ITEMS 10
282
283 SCRIPT_ITEM items[MAX_ITEMS + 1];
284 SCRIPT_STATE bidi_state = {0};
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400285 WIN_ULONG script_tags[MAX_ITEMS];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400286 int item_count;
287
288 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
289 bidi_state.fOverrideDirection = 1;
290
291 hr = ScriptItemizeOpenType (wchars,
292 chars_len,
293 MAX_ITEMS,
294 NULL,
295 &bidi_state,
296 items,
297 script_tags,
298 &item_count);
299 if (unlikely (FAILED (hr)))
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400300 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400301
302#undef MAX_ITEMS
303
304 int *range_char_counts = NULL;
305 TEXTRANGE_PROPERTIES **range_properties = NULL;
306 int range_count = 0;
307 if (num_features) {
308 /* XXX setup ranges */
309 }
310
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400311 OPENTYPE_TAG language_tag = hb_ot_tag_from_language (buffer->props.language);
312
313 unsigned int glyphs_offset = 0;
314 unsigned int glyphs_len;
315 for (unsigned int i = 0; i < item_count; i++)
316 {
317 unsigned int chars_offset = items[i].iCharPos;
318 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
319 OPENTYPE_TAG script_tag = script_tags[i]; /* XXX buffer->props.script */
320
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200321 hr = ScriptShapeOpenType (font_data->hdc,
322 &font_data->script_cache,
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400323 &items[i].a,
324 script_tag,
325 language_tag,
326 range_char_counts,
327 range_properties,
328 range_count,
329 wchars + chars_offset,
330 item_chars_len,
331 glyphs_size - glyphs_offset,
332 /* out */
333 log_clusters + chars_offset,
334 char_props + chars_offset,
335 glyphs + glyphs_offset,
336 glyph_props + glyphs_offset,
337 (int *) &glyphs_len);
338
339 if (unlikely (items[i].a.fNoGlyphIndex))
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400340 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400341 if (unlikely (hr == E_OUTOFMEMORY))
342 {
343 buffer->ensure (buffer->allocated * 2);
344 if (buffer->in_error)
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400345 FAIL ("Buffer resize failed");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400346 goto retry;
347 }
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400348 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
349 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400350 if (unlikely (FAILED (hr)))
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400351 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400352
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200353 hr = ScriptPlaceOpenType (font_data->hdc,
354 &font_data->script_cache,
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400355 &items[i].a,
356 script_tag,
357 language_tag,
358 range_char_counts,
359 range_properties,
360 range_count,
361 wchars + chars_offset,
362 log_clusters + chars_offset,
363 char_props + chars_offset,
364 item_chars_len,
365 glyphs + glyphs_offset,
366 glyph_props + glyphs_offset,
367 glyphs_len,
368 /* out */
369 advances + glyphs_offset,
370 offsets + glyphs_offset,
371 NULL);
372 if (unlikely (FAILED (hr)))
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400373 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400374
375 glyphs_offset += glyphs_len;
376 }
377 glyphs_len = glyphs_offset;
378
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400379 /* Ok, we've got everything we need, now compose output buffer,
380 * very, *very*, carefully! */
381
382 /* Calculate visual-clusters. That's what we ship. */
383 for (unsigned int i = 0; i < buffer->len; i++)
Behdad Esfahbod577326b2011-08-07 01:04:40 -0400384 vis_clusters[i] = 0;
385 for (unsigned int i = 0; i < buffer->len; i++) {
386 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
387 *p = MIN (*p, buffer->info[i].cluster);
388 }
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400389 for (unsigned int i = 1; i < glyphs_len; i++)
390 if (!glyph_props[i].sva.fClusterStart)
391 vis_clusters[i] = vis_clusters[i - 1];
392
393#undef utf16_index
394
395 buffer->ensure (glyphs_len);
396 if (buffer->in_error)
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400397 FAIL ("Buffer in error");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400398
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400399#undef FAIL
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400400
401 /* Set glyph infos */
402 for (unsigned int i = 0; i < glyphs_len; i++)
403 {
404 hb_glyph_info_t *info = &buffer->info[i];
405
406 info->codepoint = glyphs[i];
407 info->cluster = vis_clusters[i];
408
409 /* The rest is crap. Let's store position info there for now. */
410 info->mask = advances[i];
411 info->var1.u32 = offsets[i].du;
412 info->var2.u32 = offsets[i].dv;
413 }
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400414 buffer->len = glyphs_len;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400415
416 /* Set glyph positions */
417 buffer->clear_positions ();
418 for (unsigned int i = 0; i < glyphs_len; i++)
419 {
420 hb_glyph_info_t *info = &buffer->info[i];
421 hb_glyph_position_t *pos = &buffer->pos[i];
422
423 /* TODO vertical */
424 pos->x_advance = info->mask;
425 pos->x_offset = info->var1.u32;
426 pos->y_offset = info->var2.u32;
427 }
428
429 /* Wow, done! */
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400430 return TRUE;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400431}
432
433