michael@0: /* michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #define HB_SHAPER icu_le michael@0: #define hb_icu_le_shaper_font_data_t PortableFontInstance michael@0: #include "hb-shaper-impl-private.hh" michael@0: michael@0: #include "hb-icu-le/PortableFontInstance.h" michael@0: michael@0: #include "layout/loengine.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: #include "hb-icu.h" michael@0: michael@0: michael@0: /* michael@0: * shaper face data michael@0: */ michael@0: michael@0: struct hb_icu_le_shaper_face_data_t {}; michael@0: michael@0: hb_icu_le_shaper_face_data_t * michael@0: _hb_icu_le_shaper_face_data_create (hb_face_t *face HB_UNUSED) michael@0: { michael@0: return (hb_icu_le_shaper_face_data_t *) HB_SHAPER_DATA_SUCCEEDED; michael@0: } michael@0: michael@0: void michael@0: _hb_icu_le_shaper_face_data_destroy (hb_icu_le_shaper_face_data_t *data HB_UNUSED) michael@0: { michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper font data michael@0: */ michael@0: michael@0: hb_icu_le_shaper_font_data_t * michael@0: _hb_icu_le_shaper_font_data_create (hb_font_t *font) michael@0: { michael@0: LEErrorCode status = LE_NO_ERROR; michael@0: hb_icu_le_shaper_font_data_t *data = new PortableFontInstance (font->face, michael@0: font->x_scale, michael@0: font->y_scale, michael@0: status); michael@0: if (status != LE_NO_ERROR) { michael@0: delete (data); michael@0: return NULL; michael@0: } michael@0: michael@0: return data; michael@0: } michael@0: michael@0: void michael@0: _hb_icu_le_shaper_font_data_destroy (hb_icu_le_shaper_font_data_t *data) michael@0: { michael@0: delete (data); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper shape_plan data michael@0: */ michael@0: michael@0: struct hb_icu_le_shaper_shape_plan_data_t {}; michael@0: michael@0: hb_icu_le_shaper_shape_plan_data_t * michael@0: _hb_icu_le_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, michael@0: const hb_feature_t *user_features, michael@0: unsigned int num_user_features) michael@0: { michael@0: return (hb_icu_le_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; michael@0: } michael@0: michael@0: void michael@0: _hb_icu_le_shaper_shape_plan_data_destroy (hb_icu_le_shaper_shape_plan_data_t *data) michael@0: { michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper michael@0: */ michael@0: michael@0: hb_bool_t michael@0: _hb_icu_le_shape (hb_shape_plan_t *shape_plan, michael@0: hb_font_t *font, michael@0: hb_buffer_t *buffer, michael@0: const hb_feature_t *features, michael@0: unsigned int num_features) michael@0: { michael@0: LEFontInstance *font_instance = HB_SHAPER_DATA_GET (font); michael@0: le_int32 script_code = hb_icu_script_from_script (shape_plan->props.script); michael@0: le_int32 language_code = -1 /* TODO */; michael@0: le_int32 typography_flags = 3; /* Needed for ligatures and kerning */ michael@0: LEErrorCode status = LE_NO_ERROR; michael@0: le_engine *le = le_create ((const le_font *) font_instance, michael@0: script_code, michael@0: language_code, michael@0: typography_flags, michael@0: &status); michael@0: if (status != LE_NO_ERROR) michael@0: { le_close (le); return false; } michael@0: michael@0: retry: michael@0: michael@0: unsigned int scratch_size; michael@0: char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size); michael@0: michael@0: #define ALLOCATE_ARRAY(Type, name, len) \ michael@0: Type *name = (Type *) scratch; \ michael@0: scratch += (len) * sizeof ((name)[0]); \ michael@0: scratch_size -= (len) * sizeof ((name)[0]); michael@0: michael@0: ALLOCATE_ARRAY (LEUnicode, chars, buffer->len); michael@0: ALLOCATE_ARRAY (unsigned int, clusters, buffer->len); michael@0: michael@0: /* XXX Use UTF-16 decoder! */ michael@0: for (unsigned int i = 0; i < buffer->len; i++) { michael@0: chars[i] = buffer->info[i].codepoint; michael@0: clusters[i] = buffer->info[i].cluster; michael@0: } michael@0: michael@0: unsigned int glyph_count = le_layoutChars (le, michael@0: chars, michael@0: 0, michael@0: buffer->len, michael@0: buffer->len, michael@0: HB_DIRECTION_IS_BACKWARD (buffer->props.direction), michael@0: 0., 0., michael@0: &status); michael@0: if (status != LE_NO_ERROR) michael@0: { le_close (le); return false; } michael@0: michael@0: unsigned int num_glyphs = scratch_size / (sizeof (LEGlyphID) + michael@0: sizeof (le_int32) + michael@0: sizeof (float) * 2); michael@0: michael@0: if (unlikely (glyph_count >= num_glyphs || glyph_count > buffer->allocated)) { michael@0: buffer->ensure (buffer->allocated * 2); michael@0: if (buffer->in_error) michael@0: { le_close (le); return false; } michael@0: goto retry; michael@0: } michael@0: michael@0: ALLOCATE_ARRAY (LEGlyphID, glyphs, glyph_count); michael@0: ALLOCATE_ARRAY (le_int32, indices, glyph_count); michael@0: ALLOCATE_ARRAY (float, positions, glyph_count * 2 + 2); michael@0: michael@0: le_getGlyphs (le, glyphs, &status); michael@0: le_getCharIndices (le, indices, &status); michael@0: le_getGlyphPositions (le, positions, &status); michael@0: michael@0: #undef ALLOCATE_ARRAY michael@0: michael@0: /* Ok, we've got everything we need, now compose output buffer, michael@0: * very, *very*, carefully! */ michael@0: michael@0: unsigned int j = 0; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: for (unsigned int i = 0; i < glyph_count; i++) michael@0: { michael@0: if (glyphs[i] >= 0xFFFE) michael@0: continue; michael@0: michael@0: info[j].codepoint = glyphs[i]; michael@0: info[j].cluster = clusters[indices[i]]; michael@0: michael@0: /* icu-le doesn't seem to have separate advance values. */ michael@0: info[j].mask = positions[2 * i + 2] - positions[2 * i]; michael@0: info[j].var1.u32 = 0; michael@0: info[j].var2.u32 = -positions[2 * i + 1]; michael@0: michael@0: j++; michael@0: } michael@0: buffer->len = j; michael@0: michael@0: buffer->clear_positions (); michael@0: michael@0: for (unsigned int i = 0; i < buffer->len; i++) { michael@0: hb_glyph_info_t *info = &buffer->info[i]; michael@0: hb_glyph_position_t *pos = &buffer->pos[i]; michael@0: michael@0: /* TODO vertical */ michael@0: pos->x_advance = info->mask; michael@0: pos->x_offset = info->var1.u32; michael@0: pos->y_offset = info->var2.u32; michael@0: } michael@0: michael@0: le_close (le); michael@0: return true; michael@0: }