gfx/harfbuzz/src/hb-old.cc

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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 old
michael@0 28 #define hb_old_shaper_face_data_t HB_FaceRec_
michael@0 29 #define hb_old_shaper_font_data_t HB_Font_
michael@0 30 #include "hb-shaper-impl-private.hh"
michael@0 31
michael@0 32 #include <harfbuzz.h>
michael@0 33
michael@0 34
michael@0 35 #ifndef HB_DEBUG_OLD
michael@0 36 #define HB_DEBUG_OLD (HB_DEBUG+0)
michael@0 37 #endif
michael@0 38
michael@0 39
michael@0 40 static HB_Script
michael@0 41 hb_old_script_from_script (hb_script_t script)
michael@0 42 {
michael@0 43 switch ((hb_tag_t) script)
michael@0 44 {
michael@0 45 default:
michael@0 46 case HB_SCRIPT_COMMON: return HB_Script_Common;
michael@0 47 case HB_SCRIPT_GREEK: return HB_Script_Greek;
michael@0 48 case HB_SCRIPT_CYRILLIC: return HB_Script_Cyrillic;
michael@0 49 case HB_SCRIPT_ARMENIAN: return HB_Script_Armenian;
michael@0 50 case HB_SCRIPT_HEBREW: return HB_Script_Hebrew;
michael@0 51 case HB_SCRIPT_ARABIC: return HB_Script_Arabic;
michael@0 52 case HB_SCRIPT_SYRIAC: return HB_Script_Syriac;
michael@0 53 case HB_SCRIPT_THAANA: return HB_Script_Thaana;
michael@0 54 case HB_SCRIPT_DEVANAGARI: return HB_Script_Devanagari;
michael@0 55 case HB_SCRIPT_BENGALI: return HB_Script_Bengali;
michael@0 56 case HB_SCRIPT_GURMUKHI: return HB_Script_Gurmukhi;
michael@0 57 case HB_SCRIPT_GUJARATI: return HB_Script_Gujarati;
michael@0 58 case HB_SCRIPT_ORIYA: return HB_Script_Oriya;
michael@0 59 case HB_SCRIPT_TAMIL: return HB_Script_Tamil;
michael@0 60 case HB_SCRIPT_TELUGU: return HB_Script_Telugu;
michael@0 61 case HB_SCRIPT_KANNADA: return HB_Script_Kannada;
michael@0 62 case HB_SCRIPT_MALAYALAM: return HB_Script_Malayalam;
michael@0 63 case HB_SCRIPT_SINHALA: return HB_Script_Sinhala;
michael@0 64 case HB_SCRIPT_THAI: return HB_Script_Thai;
michael@0 65 case HB_SCRIPT_LAO: return HB_Script_Lao;
michael@0 66 case HB_SCRIPT_TIBETAN: return HB_Script_Tibetan;
michael@0 67 case HB_SCRIPT_MYANMAR: return HB_Script_Myanmar;
michael@0 68 case HB_SCRIPT_GEORGIAN: return HB_Script_Georgian;
michael@0 69 case HB_SCRIPT_HANGUL: return HB_Script_Hangul;
michael@0 70 case HB_SCRIPT_OGHAM: return HB_Script_Ogham;
michael@0 71 case HB_SCRIPT_RUNIC: return HB_Script_Runic;
michael@0 72 case HB_SCRIPT_KHMER: return HB_Script_Khmer;
michael@0 73 case HB_SCRIPT_NKO: return HB_Script_Nko;
michael@0 74 case HB_SCRIPT_INHERITED: return HB_Script_Inherited;
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78
michael@0 79 static HB_Bool
michael@0 80 hb_old_convertStringToGlyphIndices (HB_Font old_font,
michael@0 81 const HB_UChar16 *string,
michael@0 82 hb_uint32 length,
michael@0 83 HB_Glyph *glyphs,
michael@0 84 hb_uint32 *numGlyphs,
michael@0 85 HB_Bool rightToLeft)
michael@0 86 {
michael@0 87 hb_font_t *font = (hb_font_t *) old_font->userData;
michael@0 88
michael@0 89 for (unsigned int i = 0; i < length; i++)
michael@0 90 {
michael@0 91 hb_codepoint_t u;
michael@0 92
michael@0 93 /* XXX Handle UTF-16. Ugh */
michael@0 94 u = string[i];
michael@0 95
michael@0 96 if (rightToLeft)
michael@0 97 u = hb_unicode_funcs_get_default ()->mirroring (u);
michael@0 98
michael@0 99 font->get_glyph (u, 0, &u); /* TODO Variation selectors */
michael@0 100
michael@0 101 glyphs[i] = u;
michael@0 102 }
michael@0 103 *numGlyphs = length; /* XXX */
michael@0 104
michael@0 105 return true;
michael@0 106 }
michael@0 107
michael@0 108 static void
michael@0 109 hb_old_getGlyphAdvances (HB_Font old_font,
michael@0 110 const HB_Glyph *glyphs,
michael@0 111 hb_uint32 numGlyphs,
michael@0 112 HB_Fixed *advances,
michael@0 113 int flags /*HB_ShaperFlag*/ HB_UNUSED)
michael@0 114 {
michael@0 115 hb_font_t *font = (hb_font_t *) old_font->userData;
michael@0 116
michael@0 117 for (unsigned int i = 0; i < numGlyphs; i++)
michael@0 118 advances[i] = font->get_glyph_h_advance (glyphs[i]);
michael@0 119 }
michael@0 120
michael@0 121 static HB_Bool
michael@0 122 hb_old_canRender (HB_Font old_font,
michael@0 123 const HB_UChar16 *string,
michael@0 124 hb_uint32 length)
michael@0 125 {
michael@0 126 return true; /* TODO */
michael@0 127 }
michael@0 128
michael@0 129 static HB_Error
michael@0 130 hb_old_getPointInOutline (HB_Font old_font,
michael@0 131 HB_Glyph glyph,
michael@0 132 int flags /*HB_ShaperFlag*/,
michael@0 133 hb_uint32 point,
michael@0 134 HB_Fixed *xpos,
michael@0 135 HB_Fixed *ypos,
michael@0 136 hb_uint32 *nPoints)
michael@0 137 {
michael@0 138 return HB_Err_Ok; /* TODO */
michael@0 139 }
michael@0 140
michael@0 141 static void
michael@0 142 hb_old_getGlyphMetrics (HB_Font old_font,
michael@0 143 HB_Glyph glyph,
michael@0 144 HB_GlyphMetrics *metrics)
michael@0 145 {
michael@0 146 hb_font_t *font = (hb_font_t *) old_font->userData;
michael@0 147
michael@0 148 hb_glyph_extents_t extents;
michael@0 149
michael@0 150 font->get_glyph_extents (glyph, &extents);
michael@0 151
michael@0 152 metrics->x = extents.x_bearing;
michael@0 153 metrics->y = extents.y_bearing;
michael@0 154 metrics->width = extents.width;
michael@0 155 metrics->height = extents.height;
michael@0 156 metrics->xOffset = font->get_glyph_h_advance (glyph);
michael@0 157 metrics->yOffset = 0;
michael@0 158 }
michael@0 159
michael@0 160 static HB_Fixed
michael@0 161 hb_old_getFontMetric (HB_Font old_font,
michael@0 162 HB_FontMetric metric)
michael@0 163 {
michael@0 164 hb_font_t *font = (hb_font_t *) old_font->userData;
michael@0 165
michael@0 166 switch (metric)
michael@0 167 {
michael@0 168 case HB_FontAscent:
michael@0 169 return font->y_scale; /* XXX We don't have ascent data yet. */
michael@0 170
michael@0 171 default:
michael@0 172 return 0;
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 static const HB_FontClass hb_old_font_class = {
michael@0 177 hb_old_convertStringToGlyphIndices,
michael@0 178 hb_old_getGlyphAdvances,
michael@0 179 hb_old_canRender,
michael@0 180 hb_old_getPointInOutline,
michael@0 181 hb_old_getGlyphMetrics,
michael@0 182 hb_old_getFontMetric
michael@0 183 };
michael@0 184
michael@0 185
michael@0 186
michael@0 187 static HB_Error
michael@0 188 table_func (void *font, HB_Tag tag, HB_Byte *buffer, HB_UInt *length)
michael@0 189 {
michael@0 190 hb_face_t *face = (hb_face_t *) font;
michael@0 191 hb_blob_t *blob = face->reference_table ((hb_tag_t) tag);
michael@0 192 unsigned int capacity = *length;
michael@0 193 *length = hb_blob_get_length (blob);
michael@0 194 memcpy (buffer, hb_blob_get_data (blob, NULL), MIN (capacity, *length));
michael@0 195 hb_blob_destroy (blob);
michael@0 196 return HB_Err_Ok;
michael@0 197 }
michael@0 198
michael@0 199
michael@0 200 /*
michael@0 201 * shaper face data
michael@0 202 */
michael@0 203
michael@0 204 hb_old_shaper_face_data_t *
michael@0 205 _hb_old_shaper_face_data_create (hb_face_t *face)
michael@0 206 {
michael@0 207 return HB_NewFace (face, table_func);
michael@0 208 }
michael@0 209
michael@0 210 void
michael@0 211 _hb_old_shaper_face_data_destroy (hb_old_shaper_face_data_t *data)
michael@0 212 {
michael@0 213 HB_FreeFace (data);
michael@0 214 }
michael@0 215
michael@0 216
michael@0 217 /*
michael@0 218 * shaper font data
michael@0 219 */
michael@0 220
michael@0 221 hb_old_shaper_font_data_t *
michael@0 222 _hb_old_shaper_font_data_create (hb_font_t *font)
michael@0 223 {
michael@0 224 HB_FontRec *data = (HB_FontRec *) calloc (1, sizeof (HB_FontRec));
michael@0 225 if (unlikely (!data)) {
michael@0 226 DEBUG_MSG (OLD, font, "malloc()ing HB_Font failed");
michael@0 227 return NULL;
michael@0 228 }
michael@0 229
michael@0 230 data->klass = &hb_old_font_class;
michael@0 231 data->x_ppem = font->x_ppem;
michael@0 232 data->y_ppem = font->y_ppem;
michael@0 233 data->x_scale = font->x_scale; /* XXX */
michael@0 234 data->y_scale = font->y_scale; /* XXX */
michael@0 235 data->userData = font;
michael@0 236
michael@0 237 return data;
michael@0 238 }
michael@0 239
michael@0 240 void
michael@0 241 _hb_old_shaper_font_data_destroy (hb_old_shaper_font_data_t *data)
michael@0 242 {
michael@0 243 free (data);
michael@0 244 }
michael@0 245
michael@0 246
michael@0 247 /*
michael@0 248 * shaper shape_plan data
michael@0 249 */
michael@0 250
michael@0 251 struct hb_old_shaper_shape_plan_data_t {};
michael@0 252
michael@0 253 hb_old_shaper_shape_plan_data_t *
michael@0 254 _hb_old_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan HB_UNUSED,
michael@0 255 const hb_feature_t *user_features HB_UNUSED,
michael@0 256 unsigned int num_user_features HB_UNUSED)
michael@0 257 {
michael@0 258 return (hb_old_shaper_shape_plan_data_t *) HB_SHAPER_DATA_SUCCEEDED;
michael@0 259 }
michael@0 260
michael@0 261 void
michael@0 262 _hb_old_shaper_shape_plan_data_destroy (hb_old_shaper_shape_plan_data_t *data HB_UNUSED)
michael@0 263 {
michael@0 264 }
michael@0 265
michael@0 266
michael@0 267 /*
michael@0 268 * shaper
michael@0 269 */
michael@0 270
michael@0 271 hb_bool_t
michael@0 272 _hb_old_shape (hb_shape_plan_t *shape_plan HB_UNUSED,
michael@0 273 hb_font_t *font,
michael@0 274 hb_buffer_t *buffer,
michael@0 275 const hb_feature_t *features,
michael@0 276 unsigned int num_features)
michael@0 277 {
michael@0 278 hb_face_t *face = font->face;
michael@0 279 HB_Face old_face = HB_SHAPER_DATA_GET (face);
michael@0 280 HB_Font old_font = HB_SHAPER_DATA_GET (font);
michael@0 281
michael@0 282 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
michael@0 283
michael@0 284 retry:
michael@0 285
michael@0 286 unsigned int scratch_size;
michael@0 287 char *scratch = (char *) buffer->get_scratch_buffer (&scratch_size);
michael@0 288
michael@0 289 #define utf16_index() var1.u32
michael@0 290 HB_UChar16 *pchars = (HB_UChar16 *) scratch;
michael@0 291 unsigned int chars_len = 0;
michael@0 292 for (unsigned int i = 0; i < buffer->len; i++) {
michael@0 293 hb_codepoint_t c = buffer->info[i].codepoint;
michael@0 294 buffer->info[i].utf16_index() = chars_len;
michael@0 295 if (likely (c < 0x10000))
michael@0 296 pchars[chars_len++] = c;
michael@0 297 else if (unlikely (c >= 0x110000))
michael@0 298 pchars[chars_len++] = 0xFFFD;
michael@0 299 else {
michael@0 300 pchars[chars_len++] = 0xD800 + ((c - 0x10000) >> 10);
michael@0 301 pchars[chars_len++] = 0xDC00 + ((c - 0x10000) & ((1 << 10) - 1));
michael@0 302 }
michael@0 303 }
michael@0 304
michael@0 305
michael@0 306 #define ALLOCATE_ARRAY(Type, name, len) \
michael@0 307 name = (Type *) scratch; \
michael@0 308 scratch += (len) * sizeof ((name)[0]); \
michael@0 309 scratch_size -= (len) * sizeof ((name)[0]);
michael@0 310
michael@0 311
michael@0 312 HB_ShaperItem item = {0};
michael@0 313
michael@0 314 ALLOCATE_ARRAY (const HB_UChar16, item.string, chars_len);
michael@0 315 ALLOCATE_ARRAY (unsigned short, item.log_clusters, chars_len + 2);
michael@0 316 item.stringLength = chars_len;
michael@0 317 item.item.pos = 0;
michael@0 318 item.item.length = item.stringLength;
michael@0 319 item.item.script = hb_old_script_from_script (buffer->props.script);
michael@0 320 item.item.bidiLevel = backward ? 1 : 0;
michael@0 321
michael@0 322 item.font = old_font;
michael@0 323 item.face = old_face;
michael@0 324 item.shaperFlags = 0;
michael@0 325
michael@0 326 item.glyphIndicesPresent = false;
michael@0 327
michael@0 328 /* TODO Alignment. */
michael@0 329 unsigned int num_glyphs = scratch_size / (sizeof (HB_Glyph) +
michael@0 330 sizeof (HB_GlyphAttributes) +
michael@0 331 sizeof (HB_Fixed) +
michael@0 332 sizeof (HB_FixedPoint) +
michael@0 333 sizeof (uint32_t));
michael@0 334
michael@0 335 item.num_glyphs = num_glyphs;
michael@0 336 ALLOCATE_ARRAY (HB_Glyph, item.glyphs, num_glyphs);
michael@0 337 ALLOCATE_ARRAY (HB_GlyphAttributes, item.attributes, num_glyphs);
michael@0 338 ALLOCATE_ARRAY (HB_Fixed, item.advances, num_glyphs);
michael@0 339 ALLOCATE_ARRAY (HB_FixedPoint, item.offsets, num_glyphs);
michael@0 340 /* Apparently in some cases the offsets array will not be fully assigned to.
michael@0 341 * Clear it. */
michael@0 342 memset (item.offsets, 0, num_glyphs * sizeof (item.offsets[0]));
michael@0 343 uint32_t *vis_clusters;
michael@0 344 ALLOCATE_ARRAY (uint32_t, vis_clusters, num_glyphs);
michael@0 345
michael@0 346 #undef ALLOCATE_ARRAY
michael@0 347
michael@0 348 if (!HB_ShapeItem (&item))
michael@0 349 {
michael@0 350 if (unlikely (item.num_glyphs > num_glyphs))
michael@0 351 {
michael@0 352 buffer->ensure (buffer->allocated * 2);
michael@0 353 if (buffer->in_error)
michael@0 354 return false;
michael@0 355 goto retry;
michael@0 356 }
michael@0 357 return false;
michael@0 358 }
michael@0 359 num_glyphs = item.num_glyphs;
michael@0 360
michael@0 361 /* Ok, we've got everything we need, now compose output buffer,
michael@0 362 * very, *very*, carefully! */
michael@0 363
michael@0 364 /* Calculate visual-clusters. That's what we ship. */
michael@0 365 for (unsigned int i = 0; i < num_glyphs; i++)
michael@0 366 vis_clusters[i] = -1;
michael@0 367 for (unsigned int i = 0; i < buffer->len; i++) {
michael@0 368 uint32_t *p = &vis_clusters[item.log_clusters[buffer->info[i].utf16_index()]];
michael@0 369 *p = MIN (*p, buffer->info[i].cluster);
michael@0 370 }
michael@0 371 for (unsigned int i = 1; i < num_glyphs; i++)
michael@0 372 if (vis_clusters[i] == (uint32_t) -1)
michael@0 373 vis_clusters[i] = vis_clusters[i - 1];
michael@0 374
michael@0 375 #undef utf16_index
michael@0 376
michael@0 377 buffer->ensure (num_glyphs);
michael@0 378 if (buffer->in_error)
michael@0 379 return false;
michael@0 380
michael@0 381
michael@0 382 buffer->len = num_glyphs;
michael@0 383 hb_glyph_info_t *info = buffer->info;
michael@0 384 for (unsigned int i = 0; i < num_glyphs; i++)
michael@0 385 {
michael@0 386 info[i].codepoint = item.glyphs[i];
michael@0 387 info[i].cluster = vis_clusters[i];
michael@0 388
michael@0 389 info[i].mask = item.advances[i];
michael@0 390 info[i].var1.u32 = item.offsets[i].x;
michael@0 391 info[i].var2.u32 = item.offsets[i].y;
michael@0 392 }
michael@0 393
michael@0 394 buffer->clear_positions ();
michael@0 395
michael@0 396 for (unsigned int i = 0; i < num_glyphs; ++i) {
michael@0 397 hb_glyph_info_t *info = &buffer->info[i];
michael@0 398 hb_glyph_position_t *pos = &buffer->pos[i];
michael@0 399
michael@0 400 /* TODO vertical */
michael@0 401 pos->x_advance = info->mask;
michael@0 402 pos->x_offset = info->var1.u32;
michael@0 403 pos->y_offset = info->var2.u32;
michael@0 404 }
michael@0 405
michael@0 406 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
michael@0 407 buffer->reverse ();
michael@0 408
michael@0 409 return true;
michael@0 410 }

mercurial