2 * Copyright © 2011,2012 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 _WIN32_WINNT 0x0600
28 #define WIN32_LEAN_AND_MEAN
30 #define HB_SHAPER uniscribe
31 #include "hb-shaper-impl-private.hh"
36 #include "hb-uniscribe.h"
38 #include "hb-ot-name-table.hh"
39 #include "hb-ot-tag.h"
42 #ifndef HB_DEBUG_UNISCRIBE
43 #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
52 __out LPVOID lpvBuffer,
58 HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe
, face
)
59 HB_SHAPER_DATA_ENSURE_DECLARE(uniscribe
, font
)
66 struct hb_uniscribe_shaper_face_data_t
{
70 hb_uniscribe_shaper_face_data_t
*
71 _hb_uniscribe_shaper_face_data_create (hb_face_t
*face
)
73 hb_uniscribe_shaper_face_data_t
*data
= (hb_uniscribe_shaper_face_data_t
*) calloc (1, sizeof (hb_uniscribe_shaper_face_data_t
));
77 hb_blob_t
*blob
= hb_face_reference_blob (face
);
78 unsigned int blob_length
;
79 const char *blob_data
= hb_blob_get_data (blob
, &blob_length
);
80 if (unlikely (!blob_length
))
81 DEBUG_MSG (UNISCRIBE
, face
, "Face has empty blob");
83 DWORD num_fonts_installed
;
84 data
->fh
= AddFontMemResourceEx ((void *) blob_data
, blob_length
, 0, &num_fonts_installed
);
85 hb_blob_destroy (blob
);
86 if (unlikely (!data
->fh
)) {
87 DEBUG_MSG (UNISCRIBE
, face
, "Face AddFontMemResourceEx() failed");
96 _hb_uniscribe_shaper_face_data_destroy (hb_uniscribe_shaper_face_data_t
*data
)
98 RemoveFontMemResourceEx (data
->fh
);
107 struct hb_uniscribe_shaper_font_data_t
{
111 SCRIPT_CACHE script_cache
;
115 populate_log_font (LOGFONTW
*lf
,
118 memset (lf
, 0, sizeof (*lf
));
119 lf
->lfHeight
= -font
->y_scale
;
120 lf
->lfCharSet
= DEFAULT_CHARSET
;
122 hb_blob_t
*blob
= OT::Sanitizer
<OT::name
>::sanitize (hb_face_reference_table (font
->face
, HB_TAG ('n','a','m','e')));
123 const OT::name
*name_table
= OT::Sanitizer
<OT::name
>::lock_instance (blob
);
124 unsigned int len
= name_table
->get_name (3, 1, 0x409, 4,
126 sizeof (lf
->lfFaceName
[0]) * LF_FACESIZE
)
127 / sizeof (lf
->lfFaceName
[0]);
128 hb_blob_destroy (blob
);
130 if (unlikely (!len
)) {
131 DEBUG_MSG (UNISCRIBE
, NULL
, "Didn't find English name table entry");
134 if (unlikely (len
>= LF_FACESIZE
)) {
135 DEBUG_MSG (UNISCRIBE
, NULL
, "Font name too long");
139 for (unsigned int i
= 0; i
< len
; i
++)
140 lf
->lfFaceName
[i
] = hb_be_uint16 (lf
->lfFaceName
[i
]);
141 lf
->lfFaceName
[len
] = 0;
146 hb_uniscribe_shaper_font_data_t
*
147 _hb_uniscribe_shaper_font_data_create (hb_font_t
*font
)
149 if (unlikely (!hb_uniscribe_shaper_face_data_ensure (font
->face
))) return NULL
;
151 hb_uniscribe_shaper_font_data_t
*data
= (hb_uniscribe_shaper_font_data_t
*) calloc (1, sizeof (hb_uniscribe_shaper_font_data_t
));
152 if (unlikely (!data
))
155 data
->hdc
= GetDC (NULL
);
157 if (unlikely (!populate_log_font (&data
->log_font
, font
))) {
158 DEBUG_MSG (UNISCRIBE
, font
, "Font populate_log_font() failed");
159 _hb_uniscribe_shaper_font_data_destroy (data
);
163 data
->hfont
= CreateFontIndirectW (&data
->log_font
);
164 if (unlikely (!data
->hfont
)) {
165 DEBUG_MSG (UNISCRIBE
, font
, "Font CreateFontIndirectW() failed");
166 _hb_uniscribe_shaper_font_data_destroy (data
);
170 if (!SelectObject (data
->hdc
, data
->hfont
)) {
171 DEBUG_MSG (UNISCRIBE
, font
, "Font SelectObject() failed");
172 _hb_uniscribe_shaper_font_data_destroy (data
);
180 _hb_uniscribe_shaper_font_data_destroy (hb_uniscribe_shaper_font_data_t
*data
)
183 ReleaseDC (NULL
, data
->hdc
);
185 DeleteObject (data
->hfont
);
186 if (data
->script_cache
)
187 ScriptFreeCache (&data
->script_cache
);
192 hb_uniscribe_font_get_logfontw (hb_font_t
*font
)
194 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font
))) return NULL
;
195 hb_uniscribe_shaper_font_data_t
*font_data
= HB_SHAPER_DATA_GET (font
);
196 return &font_data
->log_font
;
200 hb_uniscribe_font_get_hfont (hb_font_t
*font
)
202 if (unlikely (!hb_uniscribe_shaper_font_data_ensure (font
))) return NULL
;
203 hb_uniscribe_shaper_font_data_t
*font_data
= HB_SHAPER_DATA_GET (font
);
204 return font_data
->hfont
;
209 * shaper shape_plan data
212 struct hb_uniscribe_shaper_shape_plan_data_t
{};
214 hb_uniscribe_shaper_shape_plan_data_t
*
215 _hb_uniscribe_shaper_shape_plan_data_create (hb_shape_plan_t
*shape_plan HB_UNUSED
,
216 const hb_feature_t
*user_features HB_UNUSED
,
217 unsigned int num_user_features HB_UNUSED
)
219 return (hb_uniscribe_shaper_shape_plan_data_t
*) HB_SHAPER_DATA_SUCCEEDED
;
223 _hb_uniscribe_shaper_shape_plan_data_destroy (hb_uniscribe_shaper_shape_plan_data_t
*data HB_UNUSED
)
234 _hb_uniscribe_shape (hb_shape_plan_t
*shape_plan
,
237 const hb_feature_t
*features
,
238 unsigned int num_features
)
240 hb_face_t
*face
= font
->face
;
241 hb_uniscribe_shaper_face_data_t
*face_data
= HB_SHAPER_DATA_GET (face
);
242 hb_uniscribe_shaper_font_data_t
*font_data
= HB_SHAPER_DATA_GET (font
);
246 DEBUG_MSG (UNISCRIBE, NULL, __VA_ARGS__); \
254 unsigned int scratch_size
;
255 char *scratch
= (char *) buffer
->get_scratch_buffer (&scratch_size
);
257 /* Allocate char buffers; they all fit */
259 #define ALLOCATE_ARRAY(Type, name, len) \
260 Type *name = (Type *) scratch; \
261 scratch += (len) * sizeof ((name)[0]); \
262 scratch_size -= (len) * sizeof ((name)[0]);
264 #define utf16_index() var1.u32
266 WCHAR
*pchars
= (WCHAR
*) scratch
;
267 unsigned int chars_len
= 0;
268 for (unsigned int i
= 0; i
< buffer
->len
; i
++) {
269 hb_codepoint_t c
= buffer
->info
[i
].codepoint
;
270 buffer
->info
[i
].utf16_index() = chars_len
;
271 if (likely (c
< 0x10000))
272 pchars
[chars_len
++] = c
;
273 else if (unlikely (c
>= 0x110000))
274 pchars
[chars_len
++] = 0xFFFD;
276 pchars
[chars_len
++] = 0xD800 + ((c
- 0x10000) >> 10);
277 pchars
[chars_len
++] = 0xDC00 + ((c
- 0x10000) & ((1 << 10) - 1));
281 ALLOCATE_ARRAY (WCHAR
, wchars
, chars_len
);
282 ALLOCATE_ARRAY (WORD
, log_clusters
, chars_len
);
283 ALLOCATE_ARRAY (SCRIPT_CHARPROP
, char_props
, chars_len
);
285 /* On Windows, we don't care about alignment...*/
286 unsigned int glyphs_size
= scratch_size
/ (sizeof (WORD
) +
287 sizeof (SCRIPT_GLYPHPROP
) +
292 ALLOCATE_ARRAY (WORD
, glyphs
, glyphs_size
);
293 ALLOCATE_ARRAY (SCRIPT_GLYPHPROP
, glyph_props
, glyphs_size
);
294 ALLOCATE_ARRAY (int, advances
, glyphs_size
);
295 ALLOCATE_ARRAY (GOFFSET
, offsets
, glyphs_size
);
296 ALLOCATE_ARRAY (uint32_t, vis_clusters
, glyphs_size
);
298 #undef ALLOCATE_ARRAY
300 #define MAX_ITEMS 256
302 SCRIPT_ITEM items
[MAX_ITEMS
+ 1];
303 SCRIPT_CONTROL bidi_control
= {0};
304 SCRIPT_STATE bidi_state
= {0};
305 ULONG script_tags
[MAX_ITEMS
];
308 /* MinGW32 doesn't define fMergeNeutralItems, so we bruteforce */
309 //bidi_control.fMergeNeutralItems = true;
310 *(uint32_t*)&bidi_control
|= 1<<24;
312 bidi_state
.uBidiLevel
= HB_DIRECTION_IS_FORWARD (buffer
->props
.direction
) ? 0 : 1;
313 bidi_state
.fOverrideDirection
= 1;
315 hr
= ScriptItemizeOpenType (wchars
,
323 if (unlikely (FAILED (hr
)))
324 FAIL ("ScriptItemizeOpenType() failed: 0x%08xL", hr
);
328 int *range_char_counts
= NULL
;
329 TEXTRANGE_PROPERTIES
**range_properties
= NULL
;
332 /* TODO setup ranges */
335 OPENTYPE_TAG language_tag
= hb_uint32_swap (hb_ot_tag_from_language (buffer
->props
.language
));
337 unsigned int glyphs_offset
= 0;
338 unsigned int glyphs_len
;
339 bool backward
= HB_DIRECTION_IS_BACKWARD (buffer
->props
.direction
);
340 for (unsigned int j
= 0; j
< item_count
; j
++)
342 unsigned int i
= backward
? item_count
- 1 - j
: j
;
343 unsigned int chars_offset
= items
[i
].iCharPos
;
344 unsigned int item_chars_len
= items
[i
+ 1].iCharPos
- chars_offset
;
347 hr
= ScriptShapeOpenType (font_data
->hdc
,
348 &font_data
->script_cache
,
355 wchars
+ chars_offset
,
357 glyphs_size
- glyphs_offset
,
359 log_clusters
+ chars_offset
,
360 char_props
+ chars_offset
,
361 glyphs
+ glyphs_offset
,
362 glyph_props
+ glyphs_offset
,
363 (int *) &glyphs_len
);
365 if (unlikely (items
[i
].a
.fNoGlyphIndex
))
366 FAIL ("ScriptShapeOpenType() set fNoGlyphIndex");
367 if (unlikely (hr
== E_OUTOFMEMORY
))
369 buffer
->ensure (buffer
->allocated
* 2);
370 if (buffer
->in_error
)
371 FAIL ("Buffer resize failed");
374 if (unlikely (hr
== USP_E_SCRIPT_NOT_IN_FONT
))
376 if (items
[i
].a
.eScript
== SCRIPT_UNDEFINED
)
377 FAIL ("ScriptShapeOpenType() failed: Font doesn't support script");
378 items
[i
].a
.eScript
= SCRIPT_UNDEFINED
;
381 if (unlikely (FAILED (hr
)))
383 FAIL ("ScriptShapeOpenType() failed: 0x%08xL", hr
);
386 for (unsigned int j
= chars_offset
; j
< chars_offset
+ item_chars_len
; j
++)
387 log_clusters
[j
] += glyphs_offset
;
389 hr
= ScriptPlaceOpenType (font_data
->hdc
,
390 &font_data
->script_cache
,
397 wchars
+ chars_offset
,
398 log_clusters
+ chars_offset
,
399 char_props
+ chars_offset
,
401 glyphs
+ glyphs_offset
,
402 glyph_props
+ glyphs_offset
,
405 advances
+ glyphs_offset
,
406 offsets
+ glyphs_offset
,
408 if (unlikely (FAILED (hr
)))
409 FAIL ("ScriptPlaceOpenType() failed: 0x%08xL", hr
);
411 glyphs_offset
+= glyphs_len
;
413 glyphs_len
= glyphs_offset
;
415 /* Ok, we've got everything we need, now compose output buffer,
416 * very, *very*, carefully! */
418 /* Calculate visual-clusters. That's what we ship. */
419 for (unsigned int i
= 0; i
< glyphs_len
; i
++)
420 vis_clusters
[i
] = -1;
421 for (unsigned int i
= 0; i
< buffer
->len
; i
++) {
422 uint32_t *p
= &vis_clusters
[log_clusters
[buffer
->info
[i
].utf16_index()]];
423 *p
= MIN (*p
, buffer
->info
[i
].cluster
);
426 for (unsigned int i
= 1; i
< glyphs_len
; i
++)
427 if (vis_clusters
[i
] == -1)
428 vis_clusters
[i
] = vis_clusters
[i
- 1];
430 for (int i
= glyphs_len
- 2; i
>= 0; i
--)
431 if (vis_clusters
[i
] == -1)
432 vis_clusters
[i
] = vis_clusters
[i
+ 1];
437 buffer
->ensure (glyphs_len
);
438 if (buffer
->in_error
)
439 FAIL ("Buffer in error");
443 /* Set glyph infos */
445 for (unsigned int i
= 0; i
< glyphs_len
; i
++)
447 hb_glyph_info_t
*info
= &buffer
->info
[buffer
->len
++];
449 info
->codepoint
= glyphs
[i
];
450 info
->cluster
= vis_clusters
[i
];
452 /* The rest is crap. Let's store position info there for now. */
453 info
->mask
= advances
[i
];
454 info
->var1
.u32
= offsets
[i
].du
;
455 info
->var2
.u32
= offsets
[i
].dv
;
458 /* Set glyph positions */
459 buffer
->clear_positions ();
460 for (unsigned int i
= 0; i
< glyphs_len
; i
++)
462 hb_glyph_info_t
*info
= &buffer
->info
[i
];
463 hb_glyph_position_t
*pos
= &buffer
->pos
[i
];
466 pos
->x_advance
= info
->mask
;
467 pos
->x_offset
= info
->var1
.u32
;
468 pos
->y_offset
= info
->var2
.u32
;