hb-kbts.cc (6647B)
1 /* 2 * This is part of HarfBuzz, a text shaping library. 3 * 4 * Permission is hereby granted, without written agreement and without 5 * license or royalty fees, to use, copy, modify, and distribute this 6 * software and its documentation for any purpose, provided that the 7 * above copyright notice and the following two paragraphs appear in 8 * all copies of this software. 9 * 10 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 11 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 12 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 13 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 14 * DAMAGE. 15 * 16 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 17 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 18 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 19 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 20 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 21 * 22 * Author(s): Khaled Hosny 23 */ 24 25 #include "hb.hh" 26 27 #ifdef HAVE_KBTS 28 29 #include "hb-shaper-impl.hh" 30 31 #define KB_TEXT_SHAPE_IMPLEMENTATION 32 #define KB_TEXT_SHAPE_STATIC 33 #define KB_TEXT_SHAPE_NO_CRT 34 #define KBTS_MALLOC(a, b) hb_malloc(b) 35 #define KBTS_FREE(a, b) hb_free(b) 36 #define KBTS_MEMCPY hb_memcpy 37 #define KBTS_MEMSET hb_memset 38 39 #pragma GCC diagnostic push 40 #pragma GCC diagnostic ignored "-Wcast-align" 41 #pragma GCC diagnostic ignored "-Wunused-function" 42 #pragma GCC diagnostic ignored "-Wsign-compare" 43 #include "kb_text_shape.h" 44 #pragma GCC diagnostic pop 45 46 47 hb_kbts_face_data_t * 48 _hb_kbts_shaper_face_data_create (hb_face_t *face) 49 { 50 hb_blob_t *blob = hb_face_reference_blob (face); 51 52 unsigned int blob_length; 53 const char *blob_data = hb_blob_get_data (blob, &blob_length); 54 if (unlikely (!blob_length)) 55 { 56 DEBUG_MSG (KBTS, blob, "Empty blob"); 57 hb_blob_destroy (blob); 58 return nullptr; 59 } 60 61 kbts_font *kb_font = (kbts_font *) hb_calloc (1, sizeof (kbts_font)); 62 if (unlikely (!kb_font)) 63 { 64 hb_blob_destroy (blob); 65 return nullptr; 66 } 67 68 *kb_font = kbts_FontFromMemory((void *)blob_data, blob_length, face->index, nullptr, nullptr); 69 hb_blob_destroy (blob); 70 blob = nullptr; 71 72 if (unlikely (!kbts_FontIsValid (kb_font))) 73 { 74 DEBUG_MSG (KBTS, face, "Failed create font from data"); 75 kbts_FreeFont (kb_font); 76 hb_free (kb_font); 77 return nullptr; 78 } 79 80 return (hb_kbts_face_data_t *) kb_font; 81 } 82 83 void 84 _hb_kbts_shaper_face_data_destroy (hb_kbts_face_data_t *data) 85 { 86 kbts_font *font = (kbts_font *) data; 87 88 assert (kbts_FontIsValid (font)); 89 90 kbts_FreeFont (font); 91 hb_free (font); 92 } 93 94 hb_kbts_font_data_t * 95 _hb_kbts_shaper_font_data_create (hb_font_t *font) 96 { 97 return (hb_kbts_font_data_t *) HB_SHAPER_DATA_SUCCEEDED; 98 } 99 100 void 101 _hb_kbts_shaper_font_data_destroy (hb_kbts_font_data_t *data) 102 { 103 } 104 105 hb_bool_t 106 _hb_kbts_shape (hb_shape_plan_t *shape_plan, 107 hb_font_t *font, 108 hb_buffer_t *buffer, 109 const hb_feature_t *features, 110 unsigned int num_features) 111 { 112 hb_face_t *face = font->face; 113 kbts_font *kb_font = (kbts_font *) (const void *) face->data.kbts; 114 115 kbts_direction kb_direction; 116 switch (buffer->props.direction) 117 { 118 case HB_DIRECTION_LTR: kb_direction = KBTS_DIRECTION_LTR; break; 119 case HB_DIRECTION_RTL: kb_direction = KBTS_DIRECTION_RTL; break; 120 case HB_DIRECTION_TTB: 121 case HB_DIRECTION_BTT: 122 DEBUG_MSG (KBTS, face, "Vertical direction is not supported"); 123 return false; 124 default: 125 case HB_DIRECTION_INVALID: 126 DEBUG_MSG (KBTS, face, "Invalid direction"); 127 return false; 128 } 129 130 kbts_glyph_storage kb_glyph_storage; 131 if (unlikely (!kbts_InitializeGlyphStorage (&kb_glyph_storage, nullptr, nullptr))) 132 return false; 133 134 kbts_script kb_script = KBTS_SCRIPT_DONT_KNOW; 135 kbts_language kb_language = KBTS_LANGUAGE_DEFAULT; 136 { 137 hb_tag_t scripts[HB_OT_MAX_TAGS_PER_SCRIPT]; 138 hb_tag_t language; 139 unsigned int script_count = ARRAY_LENGTH (scripts); 140 unsigned int language_count = 1; 141 142 hb_ot_tags_from_script_and_language (buffer->props.script, buffer->props.language, 143 &script_count, scripts, 144 &language_count, &language); 145 146 for (unsigned int i = 0; i < script_count && scripts[i] != HB_TAG_NONE; ++i) 147 { 148 kb_script = kbts_ScriptTagToScript (hb_uint32_swap (scripts[i])); 149 if (kb_script != KBTS_SCRIPT_DONT_KNOW) 150 break; 151 } 152 153 if (language_count) 154 kb_language = (kbts_language) hb_uint32_swap (language); 155 } 156 157 for (size_t i = 0; i < buffer->len; ++i) 158 { 159 kbts_glyph_config *kb_config = nullptr; 160 if (num_features) 161 { 162 hb_vector_t<kbts_feature_override> kb_features; 163 for (unsigned int j = 0; j < num_features; ++j) 164 { 165 if (hb_in_range<size_t> (i, features[j].start, features[j].end)) 166 kb_features.push<kbts_feature_override>({ hb_uint32_swap (features[j].tag), (int)features[j].value }); 167 } 168 if (kb_features) 169 kb_config = kbts_CreateGlyphConfig (kb_features.arrayZ, kb_features.length, nullptr, nullptr); 170 } 171 kbts_PushGlyph (&kb_glyph_storage, kb_font, buffer->info[i].codepoint, kb_config, buffer->info[i].cluster); 172 } 173 174 uint32_t glyph_count = 0; 175 kbts_glyph *kb_glyph = nullptr; 176 kbts_glyph_iterator kb_output; 177 hb_glyph_info_t *info; 178 hb_glyph_position_t *pos; 179 180 kbts_shape_config *kb_shape_config = kbts_CreateShapeConfig (kb_font, kb_script, kb_language, nullptr, nullptr); 181 hb_bool_t res = kbts_ShapeDirect (kb_shape_config, &kb_glyph_storage, kb_direction, 182 nullptr, nullptr, &kb_output) == KBTS_SHAPE_ERROR_NONE; 183 if (unlikely (!res)) 184 goto done; 185 186 for (auto it = kb_output; kbts_GlyphIteratorNext (&it, &kb_glyph); ) 187 glyph_count += 1; 188 189 hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_GLYPHS); 190 hb_buffer_set_length (buffer, glyph_count); 191 192 buffer->clear_positions (); 193 194 info = buffer->info; 195 pos = buffer->pos; 196 197 for (auto it = kb_output; kbts_GlyphIteratorNext (&it, &kb_glyph); info++, pos++) 198 { 199 info->codepoint = kb_glyph->Id; 200 info->cluster = kb_glyph->UserIdOrCodepointIndex; 201 pos->x_advance = font->em_scalef_x (kb_glyph->AdvanceX); 202 pos->y_advance = font->em_scalef_y (kb_glyph->AdvanceY); 203 pos->x_offset = font->em_scalef_x (kb_glyph->OffsetX); 204 pos->y_offset = font->em_scalef_y (kb_glyph->OffsetY); 205 } 206 207 done: 208 kbts_DestroyShapeConfig (kb_shape_config); 209 while (kbts_GlyphIteratorNext (&kb_output, &kb_glyph)) 210 kbts_DestroyGlyphConfig (kb_glyph->Config); 211 kbts_FreeAllGlyphs (&kb_glyph_storage); 212 213 buffer->clear_glyph_flags (); 214 buffer->unsafe_to_break (); 215 216 return res; 217 } 218 219 #endif