michael@0: /* michael@0: * Copyright © 2011 Martin Hosken michael@0: * Copyright © 2011 SIL International michael@0: * Copyright © 2011,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 graphite2 michael@0: #define hb_graphite2_shaper_font_data_t gr_font michael@0: #include "hb-shaper-impl-private.hh" michael@0: michael@0: #include "hb-graphite2.h" michael@0: michael@0: #include michael@0: michael@0: michael@0: HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, face) michael@0: HB_SHAPER_DATA_ENSURE_DECLARE(graphite2, font) michael@0: michael@0: michael@0: /* michael@0: * shaper face data michael@0: */ michael@0: michael@0: typedef struct hb_graphite2_tablelist_t { michael@0: struct hb_graphite2_tablelist_t *next; michael@0: hb_blob_t *blob; michael@0: unsigned int tag; michael@0: } hb_graphite2_tablelist_t; michael@0: michael@0: struct hb_graphite2_shaper_face_data_t { michael@0: hb_face_t *face; michael@0: gr_face *grface; michael@0: hb_graphite2_tablelist_t *tlist; michael@0: }; michael@0: michael@0: static const void *hb_graphite2_get_table (const void *data, unsigned int tag, size_t *len) michael@0: { michael@0: hb_graphite2_shaper_face_data_t *face_data = (hb_graphite2_shaper_face_data_t *) data; michael@0: hb_graphite2_tablelist_t *tlist = face_data->tlist; michael@0: michael@0: hb_blob_t *blob = NULL; michael@0: michael@0: for (hb_graphite2_tablelist_t *p = tlist; p; p = p->next) michael@0: if (p->tag == tag) { michael@0: blob = p->blob; michael@0: break; michael@0: } michael@0: michael@0: if (unlikely (!blob)) michael@0: { michael@0: blob = face_data->face->reference_table (tag); michael@0: michael@0: hb_graphite2_tablelist_t *p = (hb_graphite2_tablelist_t *) calloc (1, sizeof (hb_graphite2_tablelist_t)); michael@0: if (unlikely (!p)) { michael@0: hb_blob_destroy (blob); michael@0: return NULL; michael@0: } michael@0: p->blob = blob; michael@0: p->tag = tag; michael@0: michael@0: /* TODO Not thread-safe, but fairly harmless. michael@0: * We can do the double-chcked pointer cmpexch thing here. */ michael@0: p->next = face_data->tlist; michael@0: face_data->tlist = p; michael@0: } michael@0: michael@0: unsigned int tlen; michael@0: const char *d = hb_blob_get_data (blob, &tlen); michael@0: *len = tlen; michael@0: return d; michael@0: } michael@0: michael@0: hb_graphite2_shaper_face_data_t * michael@0: _hb_graphite2_shaper_face_data_create (hb_face_t *face) michael@0: { michael@0: hb_blob_t *silf_blob = face->reference_table (HB_GRAPHITE2_TAG_SILF); michael@0: /* Umm, we just reference the table to check whether it exists. michael@0: * Maybe add better API for this? */ michael@0: if (!hb_blob_get_length (silf_blob)) michael@0: { michael@0: hb_blob_destroy (silf_blob); michael@0: return NULL; michael@0: } michael@0: hb_blob_destroy (silf_blob); michael@0: michael@0: hb_graphite2_shaper_face_data_t *data = (hb_graphite2_shaper_face_data_t *) calloc (1, sizeof (hb_graphite2_shaper_face_data_t)); michael@0: if (unlikely (!data)) michael@0: return NULL; michael@0: michael@0: data->face = face; michael@0: data->grface = gr_make_face (data, &hb_graphite2_get_table, gr_face_preloadAll); michael@0: michael@0: if (unlikely (!data->grface)) { michael@0: free (data); michael@0: return NULL; michael@0: } michael@0: michael@0: return data; michael@0: } michael@0: michael@0: void michael@0: _hb_graphite2_shaper_face_data_destroy (hb_graphite2_shaper_face_data_t *data) michael@0: { michael@0: hb_graphite2_tablelist_t *tlist = data->tlist; michael@0: michael@0: while (tlist) michael@0: { michael@0: hb_graphite2_tablelist_t *old = tlist; michael@0: hb_blob_destroy (tlist->blob); michael@0: tlist = tlist->next; michael@0: free (old); michael@0: } michael@0: michael@0: gr_face_destroy (data->grface); michael@0: michael@0: free (data); michael@0: } michael@0: michael@0: gr_face * michael@0: hb_graphite2_face_get_gr_face (hb_face_t *face) michael@0: { michael@0: if (unlikely (!hb_graphite2_shaper_face_data_ensure (face))) return NULL; michael@0: return HB_SHAPER_DATA_GET (face)->grface; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper font data michael@0: */ michael@0: michael@0: static float hb_graphite2_get_advance (const void *hb_font, unsigned short gid) michael@0: { michael@0: return ((hb_font_t *) hb_font)->get_glyph_h_advance (gid); michael@0: } michael@0: michael@0: hb_graphite2_shaper_font_data_t * michael@0: _hb_graphite2_shaper_font_data_create (hb_font_t *font) michael@0: { michael@0: if (unlikely (!hb_graphite2_shaper_face_data_ensure (font->face))) return NULL; michael@0: michael@0: hb_face_t *face = font->face; michael@0: hb_graphite2_shaper_face_data_t *face_data = HB_SHAPER_DATA_GET (face); michael@0: michael@0: return gr_make_font_with_advance_fn (font->x_scale, font, &hb_graphite2_get_advance, face_data->grface); michael@0: } michael@0: michael@0: void michael@0: _hb_graphite2_shaper_font_data_destroy (hb_graphite2_shaper_font_data_t *data) michael@0: { michael@0: gr_font_destroy (data); michael@0: } michael@0: michael@0: gr_font * michael@0: hb_graphite2_font_get_gr_font (hb_font_t *font) michael@0: { michael@0: if (unlikely (!hb_graphite2_shaper_font_data_ensure (font))) return NULL; michael@0: return HB_SHAPER_DATA_GET (font); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * shaper shape_plan data michael@0: */ michael@0: michael@0: struct hb_graphite2_shaper_shape_plan_data_t {}; michael@0: michael@0: hb_graphite2_shaper_shape_plan_data_t * michael@0: _hb_graphite2_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_graphite2_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED; michael@0: } michael@0: michael@0: void michael@0: _hb_graphite2_shaper_shape_plan_data_destroy (hb_graphite2_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: struct hb_graphite2_cluster_t { michael@0: unsigned int base_char; michael@0: unsigned int num_chars; michael@0: unsigned int base_glyph; michael@0: unsigned int num_glyphs; michael@0: }; michael@0: michael@0: hb_bool_t michael@0: _hb_graphite2_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: hb_face_t *face = font->face; michael@0: gr_face *grface = HB_SHAPER_DATA_GET (face)->grface; michael@0: gr_font *grfont = HB_SHAPER_DATA_GET (font); michael@0: michael@0: const char *lang = hb_language_to_string (hb_buffer_get_language (buffer)); michael@0: const char *lang_end = lang ? strchr (lang, '-') : NULL; michael@0: int lang_len = lang_end ? lang_end - lang : -1; michael@0: gr_feature_val *feats = gr_face_featureval_for_lang (grface, lang ? hb_tag_from_string (lang, lang_len) : 0); michael@0: michael@0: while (num_features--) michael@0: { michael@0: const gr_feature_ref *fref = gr_face_find_fref (grface, features->tag); michael@0: if (fref) michael@0: gr_fref_set_feature_value (fref, features->value, feats); michael@0: features++; michael@0: } michael@0: michael@0: gr_segment *seg = NULL; michael@0: const gr_slot *is; michael@0: unsigned int ci = 0, ic = 0; michael@0: float curradvx = 0., curradvy = 0.; michael@0: michael@0: unsigned int scratch_size; michael@0: hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); michael@0: michael@0: uint32_t *chars = (uint32_t *) scratch; michael@0: michael@0: for (unsigned int i = 0; i < buffer->len; ++i) michael@0: chars[i] = buffer->info[i].codepoint; michael@0: michael@0: hb_tag_t script_tag[2]; michael@0: hb_ot_tags_from_script (hb_buffer_get_script (buffer), &script_tag[0], &script_tag[1]); michael@0: michael@0: seg = gr_make_seg (grfont, grface, michael@0: script_tag[1] == HB_TAG_NONE ? script_tag[0] : script_tag[1], michael@0: feats, michael@0: gr_utf32, chars, buffer->len, michael@0: 2 | (hb_buffer_get_direction (buffer) == HB_DIRECTION_RTL ? 1 : 0)); michael@0: michael@0: if (unlikely (!seg)) { michael@0: if (feats) gr_featureval_destroy (feats); michael@0: return false; michael@0: } michael@0: michael@0: unsigned int glyph_count = gr_seg_n_slots (seg); michael@0: if (unlikely (!glyph_count)) { michael@0: if (feats) gr_featureval_destroy (feats); michael@0: gr_seg_destroy (seg); michael@0: return false; michael@0: } michael@0: michael@0: scratch = buffer->get_scratch_buffer (&scratch_size); michael@0: while ((DIV_CEIL (sizeof (hb_graphite2_cluster_t) * buffer->len, sizeof (*scratch)) + michael@0: DIV_CEIL (sizeof (hb_codepoint_t) * glyph_count, sizeof (*scratch))) > scratch_size) michael@0: { michael@0: buffer->ensure (buffer->allocated * 2); michael@0: if (unlikely (buffer->in_error)) { michael@0: if (feats) gr_featureval_destroy (feats); michael@0: gr_seg_destroy (seg); michael@0: return false; michael@0: } michael@0: scratch = buffer->get_scratch_buffer (&scratch_size); michael@0: } michael@0: michael@0: #define ALLOCATE_ARRAY(Type, name, len) \ michael@0: Type *name = (Type *) scratch; \ michael@0: { \ michael@0: unsigned int _consumed = DIV_CEIL ((len) * sizeof (Type), sizeof (*scratch)); \ michael@0: assert (_consumed <= scratch_size); \ michael@0: scratch += _consumed; \ michael@0: scratch_size -= _consumed; \ michael@0: } michael@0: michael@0: ALLOCATE_ARRAY (hb_graphite2_cluster_t, clusters, buffer->len); michael@0: ALLOCATE_ARRAY (hb_codepoint_t, gids, glyph_count); michael@0: michael@0: #undef ALLOCATE_ARRAY michael@0: michael@0: memset (clusters, 0, sizeof (clusters[0]) * buffer->len); michael@0: michael@0: hb_codepoint_t *pg = gids; michael@0: for (is = gr_seg_first_slot (seg), ic = 0; is; is = gr_slot_next_in_segment (is), ic++) michael@0: { michael@0: unsigned int before = gr_slot_before (is); michael@0: unsigned int after = gr_slot_after (is); michael@0: *pg = gr_slot_gid (is); michael@0: pg++; michael@0: while (clusters[ci].base_char > before && ci) michael@0: { michael@0: clusters[ci-1].num_chars += clusters[ci].num_chars; michael@0: clusters[ci-1].num_glyphs += clusters[ci].num_glyphs; michael@0: ci--; michael@0: } michael@0: michael@0: if (gr_slot_can_insert_before (is) && clusters[ci].num_chars && before >= clusters[ci].base_char + clusters[ci].num_chars) michael@0: { michael@0: hb_graphite2_cluster_t *c = clusters + ci + 1; michael@0: c->base_char = clusters[ci].base_char + clusters[ci].num_chars; michael@0: c->num_chars = before - c->base_char; michael@0: c->base_glyph = ic; michael@0: c->num_glyphs = 0; michael@0: ci++; michael@0: } michael@0: clusters[ci].num_glyphs++; michael@0: michael@0: if (clusters[ci].base_char + clusters[ci].num_chars < after + 1) michael@0: clusters[ci].num_chars = after + 1 - clusters[ci].base_char; michael@0: } michael@0: ci++; michael@0: michael@0: //buffer->clear_output (); michael@0: for (unsigned int i = 0; i < ci; ++i) michael@0: { michael@0: for (unsigned int j = 0; j < clusters[i].num_glyphs; ++j) michael@0: { michael@0: hb_glyph_info_t *info = &buffer->info[clusters[i].base_glyph + j]; michael@0: info->codepoint = gids[clusters[i].base_glyph + j]; michael@0: info->cluster = gr_cinfo_base(gr_seg_cinfo(seg, clusters[i].base_char)); michael@0: } michael@0: } michael@0: buffer->len = glyph_count; michael@0: //buffer->swap_buffers (); michael@0: michael@0: if (HB_DIRECTION_IS_BACKWARD(buffer->props.direction)) michael@0: curradvx = gr_seg_advance_X(seg); michael@0: michael@0: hb_glyph_position_t *pPos; michael@0: for (pPos = hb_buffer_get_glyph_positions (buffer, NULL), is = gr_seg_first_slot (seg); michael@0: is; pPos++, is = gr_slot_next_in_segment (is)) michael@0: { michael@0: pPos->x_offset = gr_slot_origin_X (is) - curradvx; michael@0: pPos->y_offset = gr_slot_origin_Y (is) - curradvy; michael@0: pPos->x_advance = gr_slot_advance_X (is, grface, grfont); michael@0: pPos->y_advance = gr_slot_advance_Y (is, grface, grfont); michael@0: if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) michael@0: curradvx -= pPos->x_advance; michael@0: pPos->x_offset = gr_slot_origin_X (is) - curradvx; michael@0: if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) michael@0: curradvx += pPos->x_advance; michael@0: pPos->y_offset = gr_slot_origin_Y (is) - curradvy; michael@0: curradvy += pPos->y_advance; michael@0: } michael@0: if (!HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) michael@0: pPos[-1].x_advance += gr_seg_advance_X(seg) - curradvx; michael@0: michael@0: if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) michael@0: hb_buffer_reverse_clusters (buffer); michael@0: michael@0: if (feats) gr_featureval_destroy (feats); michael@0: gr_seg_destroy (seg); michael@0: michael@0: return true; michael@0: }