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 old michael@0: #define hb_old_shaper_face_data_t HB_FaceRec_ michael@0: #define hb_old_shaper_font_data_t HB_Font_ michael@0: #include "hb-shaper-impl-private.hh" michael@0: michael@0: #include michael@0: michael@0: michael@0: #ifndef HB_DEBUG_OLD michael@0: #define HB_DEBUG_OLD (HB_DEBUG+0) michael@0: #endif michael@0: michael@0: michael@0: static HB_Script michael@0: hb_old_script_from_script (hb_script_t script) michael@0: { michael@0: switch ((hb_tag_t) script) michael@0: { michael@0: default: michael@0: case HB_SCRIPT_COMMON: return HB_Script_Common; michael@0: case HB_SCRIPT_GREEK: return HB_Script_Greek; michael@0: case HB_SCRIPT_CYRILLIC: return HB_Script_Cyrillic; michael@0: case HB_SCRIPT_ARMENIAN: return HB_Script_Armenian; michael@0: case HB_SCRIPT_HEBREW: return HB_Script_Hebrew; michael@0: case HB_SCRIPT_ARABIC: return HB_Script_Arabic; michael@0: case HB_SCRIPT_SYRIAC: return HB_Script_Syriac; michael@0: case HB_SCRIPT_THAANA: return HB_Script_Thaana; michael@0: case HB_SCRIPT_DEVANAGARI: return HB_Script_Devanagari; michael@0: case HB_SCRIPT_BENGALI: return HB_Script_Bengali; michael@0: case HB_SCRIPT_GURMUKHI: return HB_Script_Gurmukhi; michael@0: case HB_SCRIPT_GUJARATI: return HB_Script_Gujarati; michael@0: case HB_SCRIPT_ORIYA: return HB_Script_Oriya; michael@0: case HB_SCRIPT_TAMIL: return HB_Script_Tamil; michael@0: case HB_SCRIPT_TELUGU: return HB_Script_Telugu; michael@0: case HB_SCRIPT_KANNADA: return HB_Script_Kannada; michael@0: case HB_SCRIPT_MALAYALAM: return HB_Script_Malayalam; michael@0: case HB_SCRIPT_SINHALA: return HB_Script_Sinhala; michael@0: case HB_SCRIPT_THAI: return HB_Script_Thai; michael@0: case HB_SCRIPT_LAO: return HB_Script_Lao; michael@0: case HB_SCRIPT_TIBETAN: return HB_Script_Tibetan; michael@0: case HB_SCRIPT_MYANMAR: return HB_Script_Myanmar; michael@0: case HB_SCRIPT_GEORGIAN: return HB_Script_Georgian; michael@0: case HB_SCRIPT_HANGUL: return HB_Script_Hangul; michael@0: case HB_SCRIPT_OGHAM: return HB_Script_Ogham; michael@0: case HB_SCRIPT_RUNIC: return HB_Script_Runic; michael@0: case HB_SCRIPT_KHMER: return HB_Script_Khmer; michael@0: case HB_SCRIPT_NKO: return HB_Script_Nko; michael@0: case HB_SCRIPT_INHERITED: return HB_Script_Inherited; michael@0: } michael@0: } michael@0: michael@0: michael@0: static HB_Bool michael@0: hb_old_convertStringToGlyphIndices (HB_Font old_font, michael@0: const HB_UChar16 *string, michael@0: hb_uint32 length, michael@0: HB_Glyph *glyphs, michael@0: hb_uint32 *numGlyphs, michael@0: HB_Bool rightToLeft) michael@0: { michael@0: hb_font_t *font = (hb_font_t *) old_font->userData; michael@0: michael@0: for (unsigned int i = 0; i < length; i++) michael@0: { michael@0: hb_codepoint_t u; michael@0: michael@0: /* XXX Handle UTF-16. Ugh */ michael@0: u = string[i]; michael@0: michael@0: if (rightToLeft) michael@0: u = hb_unicode_funcs_get_default ()->mirroring (u); michael@0: michael@0: font->get_glyph (u, 0, &u); /* TODO Variation selectors */ michael@0: michael@0: glyphs[i] = u; michael@0: } michael@0: *numGlyphs = length; /* XXX */ michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static void michael@0: hb_old_getGlyphAdvances (HB_Font old_font, michael@0: const HB_Glyph *glyphs, michael@0: hb_uint32 numGlyphs, michael@0: HB_Fixed *advances, michael@0: int flags /*HB_ShaperFlag*/ HB_UNUSED) michael@0: { michael@0: hb_font_t *font = (hb_font_t *) old_font->userData; michael@0: michael@0: for (unsigned int i = 0; i < numGlyphs; i++) michael@0: advances[i] = font->get_glyph_h_advance (glyphs[i]); michael@0: } michael@0: michael@0: static HB_Bool michael@0: hb_old_canRender (HB_Font old_font, michael@0: const HB_UChar16 *string, michael@0: hb_uint32 length) michael@0: { michael@0: return true; /* TODO */ michael@0: } michael@0: michael@0: static HB_Error michael@0: hb_old_getPointInOutline (HB_Font old_font, michael@0: HB_Glyph glyph, michael@0: int flags /*HB_ShaperFlag*/, michael@0: hb_uint32 point, michael@0: HB_Fixed *xpos, michael@0: HB_Fixed *ypos, michael@0: hb_uint32 *nPoints) michael@0: { michael@0: return HB_Err_Ok; /* TODO */ michael@0: } michael@0: michael@0: static void michael@0: hb_old_getGlyphMetrics (HB_Font old_font, michael@0: HB_Glyph glyph, michael@0: HB_GlyphMetrics *metrics) michael@0: { michael@0: hb_font_t *font = (hb_font_t *) old_font->userData; michael@0: michael@0: hb_glyph_extents_t extents; michael@0: michael@0: font->get_glyph_extents (glyph, &extents); michael@0: michael@0: metrics->x = extents.x_bearing; michael@0: metrics->y = extents.y_bearing; michael@0: metrics->width = extents.width; michael@0: metrics->height = extents.height; michael@0: metrics->xOffset = font->get_glyph_h_advance (glyph); michael@0: metrics->yOffset = 0; michael@0: } michael@0: michael@0: static HB_Fixed michael@0: hb_old_getFontMetric (HB_Font old_font, michael@0: HB_FontMetric metric) michael@0: { michael@0: hb_font_t *font = (hb_font_t *) old_font->userData; michael@0: michael@0: switch (metric) michael@0: { michael@0: case HB_FontAscent: michael@0: return font->y_scale; /* XXX We don't have ascent data yet. */ michael@0: michael@0: default: michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: static const HB_FontClass hb_old_font_class = { michael@0: hb_old_convertStringToGlyphIndices, michael@0: hb_old_getGlyphAdvances, michael@0: hb_old_canRender, michael@0: hb_old_getPointInOutline, michael@0: hb_old_getGlyphMetrics, michael@0: hb_old_getFontMetric michael@0: }; michael@0: michael@0: michael@0: michael@0: static HB_Error michael@0: table_func (void *font, HB_Tag tag, HB_Byte *buffer, HB_UInt *length) michael@0: { michael@0: hb_face_t *face = (hb_face_t *) font; michael@0: hb_blob_t *blob = face->reference_table ((hb_tag_t) tag); michael@0: unsigned int capacity = *length; michael@0: *length = hb_blob_get_length (blob); michael@0: memcpy (buffer, hb_blob_get_data (blob, NULL), MIN (capacity, *length)); michael@0: hb_blob_destroy (blob); michael@0: return HB_Err_Ok; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper face data michael@0: */ michael@0: michael@0: hb_old_shaper_face_data_t * michael@0: _hb_old_shaper_face_data_create (hb_face_t *face) michael@0: { michael@0: return HB_NewFace (face, table_func); michael@0: } michael@0: michael@0: void michael@0: _hb_old_shaper_face_data_destroy (hb_old_shaper_face_data_t *data) michael@0: { michael@0: HB_FreeFace (data); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper font data michael@0: */ michael@0: michael@0: hb_old_shaper_font_data_t * michael@0: _hb_old_shaper_font_data_create (hb_font_t *font) michael@0: { michael@0: HB_FontRec *data = (HB_FontRec *) calloc (1, sizeof (HB_FontRec)); michael@0: if (unlikely (!data)) { michael@0: DEBUG_MSG (OLD, font, "malloc()ing HB_Font failed"); michael@0: return NULL; michael@0: } michael@0: michael@0: data->klass = &hb_old_font_class; michael@0: data->x_ppem = font->x_ppem; michael@0: data->y_ppem = font->y_ppem; michael@0: data->x_scale = font->x_scale; /* XXX */ michael@0: data->y_scale = font->y_scale; /* XXX */ michael@0: data->userData = font; michael@0: michael@0: return data; michael@0: } michael@0: michael@0: void michael@0: _hb_old_shaper_font_data_destroy (hb_old_shaper_font_data_t *data) michael@0: { michael@0: free (data); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper shape_plan data michael@0: */ michael@0: michael@0: struct hb_old_shaper_shape_plan_data_t {}; michael@0: michael@0: hb_old_shaper_shape_plan_data_t * michael@0: _hb_old_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED, michael@0: const hb_feature_t *user_features HB_UNUSED, michael@0: unsigned int num_user_features HB_UNUSED) michael@0: { michael@0: return (hb_old_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; michael@0: } michael@0: michael@0: void michael@0: _hb_old_shaper_shape_plan_data_destroy (hb_old_shaper_shape_plan_data_t *data HB_UNUSED) 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_old_shape (hb_shape_plan_t *shape_plan HB_UNUSED, 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: hb_face_t *face = font->face; michael@0: HB_Face old_face = HB_SHAPER_DATA_GET (face); michael@0: HB_Font old_font = HB_SHAPER_DATA_GET (font); michael@0: michael@0: bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); 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 utf16_index() var1.u32 michael@0: HB_UChar16 *pchars = (HB_UChar16 *) scratch; michael@0: unsigned int chars_len = 0; michael@0: for (unsigned int i = 0; i < buffer->len; i++) { michael@0: hb_codepoint_t c = buffer->info[i].codepoint; michael@0: buffer->info[i].utf16_index() = chars_len; michael@0: if (likely (c < 0x10000)) michael@0: pchars[chars_len++] = c; michael@0: else if (unlikely (c >= 0x110000)) michael@0: pchars[chars_len++] = 0xFFFD; michael@0: else { michael@0: pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10); michael@0: pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1)); michael@0: } michael@0: } michael@0: michael@0: michael@0: #define ALLOCATE_ARRAY(Type, name, len) \ michael@0: name = (Type *) scratch; \ michael@0: scratch += (len) * sizeof ((name)[0]); \ michael@0: scratch_size -= (len) * sizeof ((name)[0]); michael@0: michael@0: michael@0: HB_ShaperItem item = {0}; michael@0: michael@0: ALLOCATE_ARRAY (const HB_UChar16, item.string, chars_len); michael@0: ALLOCATE_ARRAY (unsigned short, item.log_clusters, chars_len + 2); michael@0: item.stringLength = chars_len; michael@0: item.item.pos = 0; michael@0: item.item.length = item.stringLength; michael@0: item.item.script = hb_old_script_from_script (buffer->props.script); michael@0: item.item.bidiLevel = backward ? 1 : 0; michael@0: michael@0: item.font = old_font; michael@0: item.face = old_face; michael@0: item.shaperFlags = 0; michael@0: michael@0: item.glyphIndicesPresent = false; michael@0: michael@0: /* TODO Alignment. */ michael@0: unsigned int num_glyphs = scratch_size / (sizeof (HB_Glyph) + michael@0: sizeof (HB_GlyphAttributes) + michael@0: sizeof (HB_Fixed) + michael@0: sizeof (HB_FixedPoint) + michael@0: sizeof (uint32_t)); michael@0: michael@0: item.num_glyphs = num_glyphs; michael@0: ALLOCATE_ARRAY (HB_Glyph, item.glyphs, num_glyphs); michael@0: ALLOCATE_ARRAY (HB_GlyphAttributes, item.attributes, num_glyphs); michael@0: ALLOCATE_ARRAY (HB_Fixed, item.advances, num_glyphs); michael@0: ALLOCATE_ARRAY (HB_FixedPoint, item.offsets, num_glyphs); michael@0: /* Apparently in some cases the offsets array will not be fully assigned to. michael@0: * Clear it. */ michael@0: memset (item.offsets, 0, num_glyphs * sizeof (item.offsets[0])); michael@0: uint32_t *vis_clusters; michael@0: ALLOCATE_ARRAY (uint32_t, vis_clusters, num_glyphs); michael@0: michael@0: #undef ALLOCATE_ARRAY michael@0: michael@0: if (!HB_ShapeItem (&item)) michael@0: { michael@0: if (unlikely (item.num_glyphs > num_glyphs)) michael@0: { michael@0: buffer->ensure (buffer->allocated * 2); michael@0: if (buffer->in_error) michael@0: return false; michael@0: goto retry; michael@0: } michael@0: return false; michael@0: } michael@0: num_glyphs = item.num_glyphs; michael@0: michael@0: /* Ok, we've got everything we need, now compose output buffer, michael@0: * very, *very*, carefully! */ michael@0: michael@0: /* Calculate visual-clusters. That's what we ship. */ michael@0: for (unsigned int i = 0; i < num_glyphs; i++) michael@0: vis_clusters[i] = -1; michael@0: for (unsigned int i = 0; i < buffer->len; i++) { michael@0: uint32_t *p = &vis_clusters[item.log_clusters[buffer->info[i].utf16_index()]]; michael@0: *p = MIN (*p, buffer->info[i].cluster); michael@0: } michael@0: for (unsigned int i = 1; i < num_glyphs; i++) michael@0: if (vis_clusters[i] == (uint32_t) -1) michael@0: vis_clusters[i] = vis_clusters[i - 1]; michael@0: michael@0: #undef utf16_index michael@0: michael@0: buffer->ensure (num_glyphs); michael@0: if (buffer->in_error) michael@0: return false; michael@0: michael@0: michael@0: buffer->len = num_glyphs; michael@0: hb_glyph_info_t *info = buffer->info; michael@0: for (unsigned int i = 0; i < num_glyphs; i++) michael@0: { michael@0: info[i].codepoint = item.glyphs[i]; michael@0: info[i].cluster = vis_clusters[i]; michael@0: michael@0: info[i].mask = item.advances[i]; michael@0: info[i].var1.u32 = item.offsets[i].x; michael@0: info[i].var2.u32 = item.offsets[i].y; michael@0: } michael@0: michael@0: buffer->clear_positions (); michael@0: michael@0: for (unsigned int i = 0; i < num_glyphs; ++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: if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) michael@0: buffer->reverse (); michael@0: michael@0: return true; michael@0: }