blob: d732b5741a58a5047273e990951ae92ecaf76b9d [file] [log] [blame]
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04001/*
Behdad Esfahbod1c1233e2012-06-08 09:20:53 -04002 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -04003 *
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
Behdad Esfahbod552bf3a2012-07-11 16:44:51 -040027#define _WIN32_WINNT 0x0600
Behdad Esfahbodeb56f6a2012-08-07 21:44:25 -040028#define WIN32_LEAN_AND_MEAN
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040029
Behdad Esfahbod027857d2012-07-26 17:34:25 -040030#define HB_SHAPER uniscribe
31#include "hb-shaper-impl-private.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040032
Behdad Esfahbodb4922992011-08-05 20:34:50 -040033#include <windows.h>
34#include <usp10.h>
35
36typedef ULONG WIN_ULONG;
37
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040038#include "hb-uniscribe.h"
39
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020040#include "hb-ot-name-table.hh"
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040041#include "hb-ot-tag.h"
42
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -040043
44#ifndef HB_DEBUG_UNISCRIBE
45#define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
46#endif
47
48
49/*
50DWORD GetFontData(
51 __in HDC hdc,
52 __in DWORD dwTable,
53 __in DWORD dwOffset,
54 __out LPVOID lpvBuffer,
55 __in DWORD cbData
56);
57*/
58
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020059
Behdad Esfahbodcfe98822012-07-27 03:06:30 -040060HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, face)
61HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe, font)
Behdad Esfahbodcfe98822012-07-27 03:06:30 -040062
63
Behdad Esfahbod027857d2012-07-26 17:34:25 -040064/*
65 * shaper face data
66 */
Behdad Esfahbod71388b32011-08-24 02:09:04 +020067
Behdad Esfahbod027857d2012-07-26 17:34:25 -040068struct hb_uniscribe_shaper_face_data_t {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020069 HANDLE fh;
Behdad Esfahbod027857d2012-07-26 17:34:25 -040070};
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020071
Behdad Esfahbod027857d2012-07-26 17:34:25 -040072hb_uniscribe_shaper_face_data_t *
73_hb_uniscribe_shaper_face_data_create (hb_face_t *face)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020074{
Behdad Esfahbod027857d2012-07-26 17:34:25 -040075 hb_uniscribe_shaper_face_data_t *data = (hb_uniscribe_shaper_face_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t));
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020076 if (unlikely (!data))
Behdad Esfahbod027857d2012-07-26 17:34:25 -040077 return NULL;
Behdad Esfahboda3bd8a02011-08-24 03:22:49 +020078
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020079 hb_blob_t *blob = hb_face_reference_blob (face);
80 unsigned int blob_length;
81 const char *blob_data = hb_blob_get_data (blob, &blob_length);
82 if (unlikely (!blob_length))
83 DEBUG_MSG (UNISCRIBE, face, "Face has empty blob");
84
85 DWORD num_fonts_installed;
86 data->fh = AddFontMemResourceEx ((void *) blob_data, blob_length, 0, &num_fonts_installed);
87 hb_blob_destroy (blob);
Behdad Esfahbod027857d2012-07-26 17:34:25 -040088 if (unlikely (!data->fh)) {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020089 DEBUG_MSG (UNISCRIBE, face, "Face AddFontMemResourceEx() failed");
Behdad Esfahbod027857d2012-07-26 17:34:25 -040090 free (data);
91 return NULL;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +020092 }
93
94 return data;
95}
96
Behdad Esfahbod027857d2012-07-26 17:34:25 -040097void
98_hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t *data)
99{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400100 RemoveFontMemResourceEx (data->fh);
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400101 free (data);
102}
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200103
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400104
105/*
106 * shaper font data
107 */
108
109struct hb_uniscribe_shaper_font_data_t {
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200110 HDC hdc;
Behdad Esfahbodd6660352011-08-10 22:08:36 +0200111 LOGFONTW log_font;
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200112 HFONT hfont;
113 SCRIPT_CACHE script_cache;
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400114};
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200115
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400116static bool
117populate_log_font (LOGFONTW *lf,
118 hb_font_t *font)
119{
120 memset (lf, 0, sizeof (*lf));
121 lf->lfHeight = -font->y_scale;
122 lf->lfCharSet = DEFAULT_CHARSET;
123
124 hb_blob_t *blob = Sanitizer<name>::sanitize (hb_face_reference_table (font->face, HB_TAG ('n','a','m','e')));
125 const name *name_table = Sanitizer<name>::lock_instance (blob);
126 unsigned int len = name_table->get_name (3, 1, 0x409, 4,
127 lf->lfFaceName,
128 sizeof (lf->lfFaceName[0]) * LF_FACESIZE)
129 / sizeof (lf->lfFaceName[0]);
130 hb_blob_destroy (blob);
131
132 if (unlikely (!len)) {
133 DEBUG_MSG (UNISCRIBE, NULL, "Didn't find English name table entry");
134 return false;
135 }
136 if (unlikely (len >= LF_FACESIZE)) {
137 DEBUG_MSG (UNISCRIBE, NULL, "Font name too long");
138 return false;
139 }
140
141 for (unsigned int i = 0; i < len; i++)
142 lf->lfFaceName[i] = hb_be_uint16 (lf->lfFaceName[i]);
143 lf->lfFaceName[len] = 0;
144
145 return true;
146}
147
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400148hb_uniscribe_shaper_font_data_t *
149_hb_uniscribe_shaper_font_data_create (hb_font_t *font)
150{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400151 if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font->face))) return NULL;
152
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400153 hb_uniscribe_shaper_font_data_t *data = (hb_uniscribe_shaper_font_data_t *) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t));
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400154 if (unlikely (!data))
155 return NULL;
156
157 data->hdc = GetDC (NULL);
158
159 if (unlikely (!populate_log_font (&data->log_font, font))) {
160 DEBUG_MSG (UNISCRIBE, font, "Font populate_log_font() failed");
161 _hb_uniscribe_shaper_font_data_destroy (data);
162 return NULL;
163 }
164
165 data->hfont = CreateFontIndirectW (&data->log_font);
166 if (unlikely (!data->hfont)) {
167 DEBUG_MSG (UNISCRIBE, font, "Font CreateFontIndirectW() failed");
168 _hb_uniscribe_shaper_font_data_destroy (data);
169 return NULL;
170 }
171
172 if (!SelectObject (data->hdc, data->hfont)) {
173 DEBUG_MSG (UNISCRIBE, font, "Font SelectObject() failed");
174 _hb_uniscribe_shaper_font_data_destroy (data);
175 return NULL;
176 }
177
178 return data;
179}
180
181void
182_hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t *data)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200183{
184 if (data->hdc)
185 ReleaseDC (NULL, data->hdc);
186 if (data->hfont)
187 DeleteObject (data->hfont);
188 if (data->script_cache)
189 ScriptFreeCache (&data->script_cache);
190 free (data);
191}
192
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400193
194/*
195 * shaper shape_plan data
196 */
197
198struct hb_uniscribe_shaper_shape_plan_data_t {};
199
200hb_uniscribe_shaper_shape_plan_data_t *
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400201_hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
202 const hb_feature_t *user_features HB_UNUSED,
203 unsigned int num_user_features HB_UNUSED)
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200204{
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400205 return (hb_uniscribe_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
206}
207
208void
Behdad Esfahbod45c13832012-08-14 09:33:18 -0400209_hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t *data HB_UNUSED)
Behdad Esfahbod027857d2012-07-26 17:34:25 -0400210{
211}
212
213
214/*
215 * shaper
216 */
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200217
Behdad Esfahbodd6660352011-08-10 22:08:36 +0200218LOGFONTW *
219hb_uniscribe_font_get_logfontw (hb_font_t *font)
220{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400221 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
Behdad Esfahboda00ad602012-07-28 21:16:08 -0400222 return NULL;
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400223 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
Behdad Esfahbodd6660352011-08-10 22:08:36 +0200224 return &font_data->log_font;
225}
226
227HFONT
228hb_uniscribe_font_get_hfont (hb_font_t *font)
229{
Behdad Esfahbod713914d2012-07-30 17:54:38 -0400230 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font))) return NULL;
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400231 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
Behdad Esfahbodd6660352011-08-10 22:08:36 +0200232 return font_data->hfont;
233}
234
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200235
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400236hb_bool_t
Behdad Esfahbodbd26b4d2012-07-26 22:05:39 -0400237_hb_uniscribe_shape (hb_shape_plan_t *shape_plan,
238 hb_font_t *font,
Behdad Esfahbod6bd9b472012-04-12 14:53:53 -0400239 hb_buffer_t *buffer,
240 const hb_feature_t *features,
241 unsigned int num_features)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400242{
Behdad Esfahbodb6b7ba12012-07-27 01:26:11 -0400243 hb_face_t *face = font->face;
244 hb_uniscribe_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face);
245 hb_uniscribe_shaper_font_data_t *font_data = HB_SHAPER_DATA_GET (font);
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400246
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200247#define FAIL(...) \
248 HB_STMT_START { \
249 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400250 return false; \
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200251 } HB_STMT_END;
252
Behdad Esfahbodbf3eef52011-08-09 00:13:24 +0200253 HRESULT hr;
254
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400255retry:
256
257 unsigned int scratch_size;
258 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
259
260 /* Allocate char buffers; they all fit */
261
262#define ALLOCATE_ARRAY(Type, name, len) \
263 Type *name = (Type *) scratch; \
Behdad Esfahbod91e721e2012-07-25 19:20:34 -0400264 scratch += (len) * sizeof ((name)[0]); \
265 scratch_size -= (len) * sizeof ((name)[0]);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400266
267#define utf16_index() var1.u32
268
269 WCHAR *pchars = (WCHAR *) scratch;
270 unsigned int chars_len = 0;
271 for (unsigned int i = 0; i < buffer->len; i++) {
272 hb_codepoint_t c = buffer->info[i].codepoint;
273 buffer->info[i].utf16_index() = chars_len;
274 if (likely (c < 0x10000))
275 pchars[chars_len++] = c;
276 else if (unlikely (c >= 0x110000))
277 pchars[chars_len++] = 0xFFFD;
278 else {
279 pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10);
280 pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1));
281 }
282 }
283
284 ALLOCATE_ARRAY (WCHAR, wchars, chars_len);
285 ALLOCATE_ARRAY (WORD, log_clusters, chars_len);
286 ALLOCATE_ARRAY (SCRIPT_CHARPROP, char_props, chars_len);
287
288 /* On Windows, we don't care about alignment...*/
289 unsigned int glyphs_size = scratch_size / (sizeof (WORD) +
290 sizeof (SCRIPT_GLYPHPROP) +
291 sizeof (int) +
292 sizeof (GOFFSET) +
293 sizeof (uint32_t));
294
295 ALLOCATE_ARRAY (WORD, glyphs, glyphs_size);
296 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP, glyph_props, glyphs_size);
297 ALLOCATE_ARRAY (int, advances, glyphs_size);
298 ALLOCATE_ARRAY (GOFFSET, offsets, glyphs_size);
299 ALLOCATE_ARRAY (uint32_t, vis_clusters, glyphs_size);
300
Behdad Esfahbod91e721e2012-07-25 19:20:34 -0400301#undef ALLOCATE_ARRAY
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400302
Behdad Esfahbod87296912012-06-08 14:18:30 -0400303#define MAX_ITEMS 256
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400304
305 SCRIPT_ITEM items[MAX_ITEMS + 1];
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400306 SCRIPT_CONTROL bidi_control = {0};
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400307 SCRIPT_STATE bidi_state = {0};
Behdad Esfahbodb4922992011-08-05 20:34:50 -0400308 WIN_ULONG script_tags[MAX_ITEMS];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400309 int item_count;
310
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400311 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400312 //bidi_control.fMergeNeutralItems = true;
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400313 *(uint32_t*)&bidi_control |= 1<<24;
314
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400315 bidi_state.uBidiLevel = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1;
Behdad Esfahbod29eac8f2012-06-08 09:26:17 -0400316 bidi_state.fOverrideDirection = 1;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400317
318 hr = ScriptItemizeOpenType (wchars,
319 chars_len,
320 MAX_ITEMS,
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400321 &bidi_control,
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400322 &bidi_state,
323 items,
324 script_tags,
325 &item_count);
326 if (unlikely (FAILED (hr)))
Behdad Esfahbod2eb474a2011-08-07 00:59:38 -0400327 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400328
329#undef MAX_ITEMS
330
331 int *range_char_counts = NULL;
332 TEXTRANGE_PROPERTIES **range_properties = NULL;
333 int range_count = 0;
334 if (num_features) {
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400335 /* TODO setup ranges */
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400336 }
337
Behdad Esfahbodfcd6f532012-06-08 09:59:43 -0400338 OPENTYPE_TAG language_tag = hb_uint32_swap (hb_ot_tag_from_language (buffer->props.language));
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400339
340 unsigned int glyphs_offset = 0;
341 unsigned int glyphs_len;
Behdad Esfahbodb069c3c2012-06-08 10:10:29 -0400342 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
343 for (unsigned int j = 0; j < item_count; j++)
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400344 {
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400345 unsigned int i = backward ? item_count - 1 - j : j;
346 unsigned int chars_offset = items[i].iCharPos;
347 unsigned int item_chars_len = items[i + 1].iCharPos - chars_offset;
Behdad Esfahbod1c1233e2012-06-08 09:20:53 -0400348
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400349 retry_shape:
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400350 hr = ScriptShapeOpenType (font_data->hdc,
351 &font_data->script_cache,
352 &items[i].a,
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400353 script_tags[i],
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400354 language_tag,
355 range_char_counts,
356 range_properties,
357 range_count,
358 wchars + chars_offset,
359 item_chars_len,
360 glyphs_size - glyphs_offset,
361 /* out */
362 log_clusters + chars_offset,
363 char_props + chars_offset,
364 glyphs + glyphs_offset,
365 glyph_props + glyphs_offset,
366 (int *) &glyphs_len);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400367
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400368 if (unlikely (items[i].a.fNoGlyphIndex))
369 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
370 if (unlikely (hr == E_OUTOFMEMORY))
371 {
372 buffer->ensure (buffer->allocated * 2);
373 if (buffer->in_error)
374 FAIL ("Buffer resize failed");
375 goto retry;
376 }
377 if (unlikely (hr == USP_E_SCRIPT_NOT_IN_FONT))
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400378 {
379 if (items[i].a.eScript == SCRIPT_UNDEFINED)
380 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
381 items[i].a.eScript = SCRIPT_UNDEFINED;
382 goto retry_shape;
383 }
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400384 if (unlikely (FAILED (hr)))
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400385 {
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400386 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr);
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400387 }
388
389 for (unsigned int j = chars_offset; j < chars_offset + item_chars_len; j++)
390 log_clusters[j] += glyphs_offset;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400391
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400392 hr = ScriptPlaceOpenType (font_data->hdc,
393 &font_data->script_cache,
394 &items[i].a,
Behdad Esfahbode9c0f152012-07-20 17:05:46 -0400395 script_tags[i],
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400396 language_tag,
397 range_char_counts,
398 range_properties,
399 range_count,
400 wchars + chars_offset,
401 log_clusters + chars_offset,
402 char_props + chars_offset,
403 item_chars_len,
404 glyphs + glyphs_offset,
405 glyph_props + glyphs_offset,
406 glyphs_len,
407 /* out */
408 advances + glyphs_offset,
409 offsets + glyphs_offset,
410 NULL);
411 if (unlikely (FAILED (hr)))
412 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr);
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400413
Behdad Esfahbod0dd86f92012-06-08 10:23:03 -0400414 glyphs_offset += glyphs_len;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400415 }
416 glyphs_len = glyphs_offset;
417
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400418 /* Ok, we've got everything we need, now compose output buffer,
419 * very, *very*, carefully! */
420
421 /* Calculate visual-clusters. That's what we ship. */
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400422 for (unsigned int i = 0; i < glyphs_len; i++)
423 vis_clusters[i] = -1;
Behdad Esfahbod577326b2011-08-07 01:04:40 -0400424 for (unsigned int i = 0; i < buffer->len; i++) {
425 uint32_t *p = &vis_clusters[log_clusters[buffer->info[i].utf16_index()]];
426 *p = MIN (*p, buffer->info[i].cluster);
427 }
Behdad Esfahbodb069c3c2012-06-08 10:10:29 -0400428 if (!backward) {
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400429 for (unsigned int i = 1; i < glyphs_len; i++)
Behdad Esfahbod8e7beba2012-06-08 10:22:06 -0400430 if (vis_clusters[i] == -1)
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400431 vis_clusters[i] = vis_clusters[i - 1];
432 } else {
433 for (int i = glyphs_len - 2; i >= 0; i--)
Behdad Esfahbod8e7beba2012-06-08 10:22:06 -0400434 if (vis_clusters[i] == -1)
Behdad Esfahbod5c299342011-09-19 14:53:26 -0400435 vis_clusters[i] = vis_clusters[i + 1];
436 }
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400437
438#undef utf16_index
439
440 buffer->ensure (glyphs_len);
441 if (buffer->in_error)
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400442 FAIL ("Buffer in error");
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400443
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400444#undef FAIL
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400445
446 /* Set glyph infos */
Behdad Esfahbodd753ac72011-08-09 14:03:12 +0200447 buffer->len = 0;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400448 for (unsigned int i = 0; i < glyphs_len; i++)
449 {
Behdad Esfahbodd753ac72011-08-09 14:03:12 +0200450 hb_glyph_info_t *info = &buffer->info[buffer->len++];
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400451
452 info->codepoint = glyphs[i];
453 info->cluster = vis_clusters[i];
454
455 /* The rest is crap. Let's store position info there for now. */
456 info->mask = advances[i];
457 info->var1.u32 = offsets[i].du;
458 info->var2.u32 = offsets[i].dv;
459 }
460
461 /* Set glyph positions */
462 buffer->clear_positions ();
463 for (unsigned int i = 0; i < glyphs_len; i++)
464 {
465 hb_glyph_info_t *info = &buffer->info[i];
466 hb_glyph_position_t *pos = &buffer->pos[i];
467
468 /* TODO vertical */
469 pos->x_advance = info->mask;
470 pos->x_offset = info->var1.u32;
471 pos->y_offset = info->var2.u32;
472 }
473
474 /* Wow, done! */
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400475 return true;
Behdad Esfahbod0fbb2dc2011-08-03 19:55:04 -0400476}
477
478