gfx/harfbuzz/src/hb-icu-le.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright © 2012 Google, Inc.
michael@0 3 *
michael@0 4 * This is part of HarfBuzz, a text shaping library.
michael@0 5 *
michael@0 6 * Permission is hereby granted, without written agreement and without
michael@0 7 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 8 * software and its documentation for any purpose, provided that the
michael@0 9 * above copyright notice and the following two paragraphs appear in
michael@0 10 * all copies of this software.
michael@0 11 *
michael@0 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 16 * DAMAGE.
michael@0 17 *
michael@0 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 23 *
michael@0 24 * Google Author(s): Behdad Esfahbod
michael@0 25 */
michael@0 26
michael@0 27 #define HB_SHAPER icu_le
michael@0 28 #define hb_icu_le_shaper_font_data_t PortableFontInstance
michael@0 29 #include "hb-shaper-impl-private.hh"
michael@0 30
michael@0 31 #include "hb-icu-le/PortableFontInstance.h"
michael@0 32
michael@0 33 #include "layout/loengine.h"
michael@0 34 #include "unicode/unistr.h"
michael@0 35
michael@0 36 #include "hb-icu.h"
michael@0 37
michael@0 38
michael@0 39 /*
michael@0 40 * shaper face data
michael@0 41 */
michael@0 42
michael@0 43 struct hb_icu_le_shaper_face_data_t {};
michael@0 44
michael@0 45 hb_icu_le_shaper_face_data_t *
michael@0 46 _hb_icu_le_shaper_face_data_create (hb_face_t *face HB_UNUSED)
michael@0 47 {
michael@0 48 return (hb_icu_le_shaper_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
michael@0 49 }
michael@0 50
michael@0 51 void
michael@0 52 _hb_icu_le_shaper_face_data_destroy (hb_icu_le_shaper_face_data_t *data HB_UNUSED)
michael@0 53 {
michael@0 54 }
michael@0 55
michael@0 56
michael@0 57 /*
michael@0 58 * shaper font data
michael@0 59 */
michael@0 60
michael@0 61 hb_icu_le_shaper_font_data_t *
michael@0 62 _hb_icu_le_shaper_font_data_create (hb_font_t *font)
michael@0 63 {
michael@0 64 LEErrorCode status = LE_NO_ERROR;
michael@0 65 hb_icu_le_shaper_font_data_t *data = new PortableFontInstance (font->face,
michael@0 66 font->x_scale,
michael@0 67 font->y_scale,
michael@0 68 status);
michael@0 69 if (status != LE_NO_ERROR) {
michael@0 70 delete (data);
michael@0 71 return NULL;
michael@0 72 }
michael@0 73
michael@0 74 return data;
michael@0 75 }
michael@0 76
michael@0 77 void
michael@0 78 _hb_icu_le_shaper_font_data_destroy (hb_icu_le_shaper_font_data_t *data)
michael@0 79 {
michael@0 80 delete (data);
michael@0 81 }
michael@0 82
michael@0 83
michael@0 84 /*
michael@0 85 * shaper shape_plan data
michael@0 86 */
michael@0 87
michael@0 88 struct hb_icu_le_shaper_shape_plan_data_t {};
michael@0 89
michael@0 90 hb_icu_le_shaper_shape_plan_data_t *
michael@0 91 _hb_icu_le_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
michael@0 92 const hb_feature_t *user_features,
michael@0 93 unsigned int num_user_features)
michael@0 94 {
michael@0 95 return (hb_icu_le_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
michael@0 96 }
michael@0 97
michael@0 98 void
michael@0 99 _hb_icu_le_shaper_shape_plan_data_destroy (hb_icu_le_shaper_shape_plan_data_t *data)
michael@0 100 {
michael@0 101 }
michael@0 102
michael@0 103
michael@0 104 /*
michael@0 105 * shaper
michael@0 106 */
michael@0 107
michael@0 108 hb_bool_t
michael@0 109 _hb_icu_le_shape (hb_shape_plan_t *shape_plan,
michael@0 110 hb_font_t *font,
michael@0 111 hb_buffer_t *buffer,
michael@0 112 const hb_feature_t *features,
michael@0 113 unsigned int num_features)
michael@0 114 {
michael@0 115 LEFontInstance *font_instance = HB_SHAPER_DATA_GET (font);
michael@0 116 le_int32 script_code = hb_icu_script_from_script (shape_plan->props.script);
michael@0 117 le_int32 language_code = -1 /* TODO */;
michael@0 118 le_int32 typography_flags = 3; /* Needed for ligatures and kerning */
michael@0 119 LEErrorCode status = LE_NO_ERROR;
michael@0 120 le_engine *le = le_create ((const le_font *) font_instance,
michael@0 121 script_code,
michael@0 122 language_code,
michael@0 123 typography_flags,
michael@0 124 &status);
michael@0 125 if (status != LE_NO_ERROR)
michael@0 126 { le_close (le); return false; }
michael@0 127
michael@0 128 retry:
michael@0 129
michael@0 130 unsigned int scratch_size;
michael@0 131 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
michael@0 132
michael@0 133 #define ALLOCATE_ARRAY(Type, name, len) \
michael@0 134 Type *name = (Type *) scratch; \
michael@0 135 scratch += (len) * sizeof ((name)[0]); \
michael@0 136 scratch_size -= (len) * sizeof ((name)[0]);
michael@0 137
michael@0 138 ALLOCATE_ARRAY (LEUnicode, chars, buffer->len);
michael@0 139 ALLOCATE_ARRAY (unsigned int, clusters, buffer->len);
michael@0 140
michael@0 141 /* XXX Use UTF-16 decoder! */
michael@0 142 for (unsigned int i = 0; i < buffer->len; i++) {
michael@0 143 chars[i] = buffer->info[i].codepoint;
michael@0 144 clusters[i] = buffer->info[i].cluster;
michael@0 145 }
michael@0 146
michael@0 147 unsigned int glyph_count = le_layoutChars (le,
michael@0 148 chars,
michael@0 149 0,
michael@0 150 buffer->len,
michael@0 151 buffer->len,
michael@0 152 HB_DIRECTION_IS_BACKWARD (buffer->props.direction),
michael@0 153 0., 0.,
michael@0 154 &status);
michael@0 155 if (status != LE_NO_ERROR)
michael@0 156 { le_close (le); return false; }
michael@0 157
michael@0 158 unsigned int num_glyphs = scratch_size / (sizeof (LEGlyphID) +
michael@0 159 sizeof (le_int32) +
michael@0 160 sizeof (float) * 2);
michael@0 161
michael@0 162 if (unlikely (glyph_count >= num_glyphs || glyph_count > buffer->allocated)) {
michael@0 163 buffer->ensure (buffer->allocated * 2);
michael@0 164 if (buffer->in_error)
michael@0 165 { le_close (le); return false; }
michael@0 166 goto retry;
michael@0 167 }
michael@0 168
michael@0 169 ALLOCATE_ARRAY (LEGlyphID, glyphs, glyph_count);
michael@0 170 ALLOCATE_ARRAY (le_int32, indices, glyph_count);
michael@0 171 ALLOCATE_ARRAY (float, positions, glyph_count * 2 + 2);
michael@0 172
michael@0 173 le_getGlyphs (le, glyphs, &status);
michael@0 174 le_getCharIndices (le, indices, &status);
michael@0 175 le_getGlyphPositions (le, positions, &status);
michael@0 176
michael@0 177 #undef ALLOCATE_ARRAY
michael@0 178
michael@0 179 /* Ok, we've got everything we need, now compose output buffer,
michael@0 180 * very, *very*, carefully! */
michael@0 181
michael@0 182 unsigned int j = 0;
michael@0 183 hb_glyph_info_t *info = buffer->info;
michael@0 184 for (unsigned int i = 0; i < glyph_count; i++)
michael@0 185 {
michael@0 186 if (glyphs[i] >= 0xFFFE)
michael@0 187 continue;
michael@0 188
michael@0 189 info[j].codepoint = glyphs[i];
michael@0 190 info[j].cluster = clusters[indices[i]];
michael@0 191
michael@0 192 /* icu-le doesn't seem to have separate advance values. */
michael@0 193 info[j].mask = positions[2 * i + 2] - positions[2 * i];
michael@0 194 info[j].var1.u32 = 0;
michael@0 195 info[j].var2.u32 = -positions[2 * i + 1];
michael@0 196
michael@0 197 j++;
michael@0 198 }
michael@0 199 buffer->len = j;
michael@0 200
michael@0 201 buffer->clear_positions ();
michael@0 202
michael@0 203 for (unsigned int i = 0; i < buffer->len; i++) {
michael@0 204 hb_glyph_info_t *info = &buffer->info[i];
michael@0 205 hb_glyph_position_t *pos = &buffer->pos[i];
michael@0 206
michael@0 207 /* TODO vertical */
michael@0 208 pos->x_advance = info->mask;
michael@0 209 pos->x_offset = info->var1.u32;
michael@0 210 pos->y_offset = info->var2.u32;
michael@0 211 }
michael@0 212
michael@0 213 le_close (le);
michael@0 214 return true;
michael@0 215 }

mercurial