| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "modules/skshaper/include/SkShaper_harfbuzz.h" |
| 9 |
|
| 10 |
#include "include/core/SkData.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkFontArguments.h" |
| 13 |
#include "include/core/SkFontMetrics.h" |
| 14 |
#include "include/core/SkFontMgr.h" |
| 15 |
#include "include/core/SkFontTypes.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRect.h" |
| 19 |
#include "include/core/SkRefCnt.h" |
| 20 |
#include "include/core/SkScalar.h" |
| 21 |
#include "include/core/SkSpan.h" |
| 22 |
#include "include/core/SkStream.h" |
| 23 |
#include "include/core/SkString.h" |
| 24 |
#include "include/core/SkTypeface.h" |
| 25 |
#include "include/core/SkTypes.h" |
| 26 |
#include "include/private/base/SkDebug.h" |
| 27 |
#include "include/private/base/SkMalloc.h" |
| 28 |
#include "include/private/base/SkMutex.h" |
| 29 |
#include "include/private/base/SkTArray.h" |
| 30 |
#include "include/private/base/SkTemplates.h" |
| 31 |
#include "include/private/base/SkTo.h" |
| 32 |
#include "include/private/base/SkTypeTraits.h" |
| 33 |
#include "modules/skshaper/include/SkShaper.h" |
| 34 |
#include "modules/skunicode/include/SkUnicode.h" |
| 35 |
#include "src/base/SkTDPQueue.h" |
| 36 |
#include "src/base/SkUTF.h" |
| 37 |
#include "src/core/SkLRUCache.h" |
| 38 |
|
| 39 |
#if !defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 40 |
#include "modules/skshaper/include/SkShaper_skunicode.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <hb-ot.h> |
| 44 |
#include <hb.h> |
| 45 |
|
| 46 |
#include <cstdint> |
| 47 |
#include <cstring> |
| 48 |
#include <memory> |
| 49 |
#include <type_traits> |
| 50 |
#include <utility> |
| 51 |
|
| 52 |
using namespace skia_private; |
| 53 |
|
| 54 |
// HB_FEATURE_GLOBAL_START and HB_FEATURE_GLOBAL_END were not added until HarfBuzz 2.0 |
| 55 |
// They would have always worked, they just hadn't been named yet. |
| 56 |
#if !defined(HB_FEATURE_GLOBAL_START) |
| 57 |
# define HB_FEATURE_GLOBAL_START 0 |
| 58 |
#endif |
| 59 |
#if !defined(HB_FEATURE_GLOBAL_END) |
| 60 |
# define HB_FEATURE_GLOBAL_END ((unsigned int) -1) |
| 61 |
#endif |
| 62 |
|
| 63 |
namespace { |
| 64 |
using HBBlob = std::unique_ptr<hb_blob_t , SkFunctionObject<hb_blob_destroy> >; |
| 65 |
using HBFace = std::unique_ptr<hb_face_t , SkFunctionObject<hb_face_destroy> >; |
| 66 |
using HBFont = std::unique_ptr<hb_font_t , SkFunctionObject<hb_font_destroy> >; |
| 67 |
using HBBuffer = std::unique_ptr<hb_buffer_t, SkFunctionObject<hb_buffer_destroy>>; |
| 68 |
|
| 69 |
using SkUnicodeBreak = std::unique_ptr<SkBreakIterator>; |
| 70 |
|
| 71 |
hb_position_t skhb_position(SkScalar value) { |
| 72 |
// Treat HarfBuzz hb_position_t as 16.16 fixed-point. |
| 73 |
constexpr int kHbPosition1 = 1 << 16; |
| 74 |
return SkScalarRoundToInt(value * kHbPosition1); |
| 75 |
} |
| 76 |
|
| 77 |
hb_bool_t skhb_glyph(hb_font_t* hb_font, |
| 78 |
void* font_data, |
| 79 |
hb_codepoint_t unicode, |
| 80 |
hb_codepoint_t variation_selector, |
| 81 |
hb_codepoint_t* glyph, |
| 82 |
void* user_data) { |
| 83 |
SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
| 84 |
|
| 85 |
*glyph = font.unicharToGlyph(unicode); |
| 86 |
return *glyph != 0; |
| 87 |
} |
| 88 |
|
| 89 |
hb_bool_t skhb_nominal_glyph(hb_font_t* hb_font, |
| 90 |
void* font_data, |
| 91 |
hb_codepoint_t unicode, |
| 92 |
hb_codepoint_t* glyph, |
| 93 |
void* user_data) { |
| 94 |
return skhb_glyph(hb_font, font_data, unicode, 0, glyph, user_data); |
| 95 |
} |
| 96 |
|
| 97 |
unsigned skhb_nominal_glyphs(hb_font_t *hb_font, void *font_data, |
| 98 |
unsigned int count, |
| 99 |
const hb_codepoint_t *unicodes, |
| 100 |
unsigned int unicode_stride, |
| 101 |
hb_codepoint_t *glyphs, |
| 102 |
unsigned int glyph_stride, |
| 103 |
void *user_data) { |
| 104 |
SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
| 105 |
|
| 106 |
// Batch call textToGlyphs since entry cost is not cheap. |
| 107 |
// Copy requred because textToGlyphs is dense and hb is strided. |
| 108 |
AutoSTMalloc<256, SkUnichar> unicode(count); |
| 109 |
for (unsigned i = 0; i < count; i++) { |
| 110 |
unicode[i] = *unicodes; |
| 111 |
unicodes = SkTAddOffset<const hb_codepoint_t>(unicodes, unicode_stride); |
| 112 |
} |
| 113 |
AutoSTMalloc<256, SkGlyphID> glyph(count); |
| 114 |
font.textToGlyphs(unicode.get(), count * sizeof(SkUnichar), SkTextEncoding::kUTF32, |
| 115 |
{glyph.get(), count}); |
| 116 |
|
| 117 |
// Copy the results back to the sparse array. |
| 118 |
unsigned int done; |
| 119 |
for (done = 0; done < count && glyph[done] != 0; done++) { |
| 120 |
*glyphs = glyph[done]; |
| 121 |
glyphs = SkTAddOffset<hb_codepoint_t>(glyphs, glyph_stride); |
| 122 |
} |
| 123 |
// return 'done' to allow HarfBuzz to synthesize with NFC and spaces, return 'count' to avoid |
| 124 |
return done; |
| 125 |
} |
| 126 |
|
| 127 |
hb_position_t skhb_glyph_h_advance(hb_font_t* hb_font, |
| 128 |
void* font_data, |
| 129 |
hb_codepoint_t hbGlyph, |
| 130 |
void* user_data) { |
| 131 |
SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
| 132 |
|
| 133 |
SkScalar advance = font.getWidth(SkTo<SkGlyphID>(hbGlyph)); |
| 134 |
|
| 135 |
if (!font.isSubpixel()) { |
| 136 |
advance = SkScalarRoundToInt(advance); |
| 137 |
} |
| 138 |
return skhb_position(advance); |
| 139 |
} |
| 140 |
|
| 141 |
void skhb_glyph_h_advances(hb_font_t* hb_font, |
| 142 |
void* font_data, |
| 143 |
unsigned count, |
| 144 |
const hb_codepoint_t* glyphs, |
| 145 |
unsigned int glyph_stride, |
| 146 |
hb_position_t* advances, |
| 147 |
unsigned int advance_stride, |
| 148 |
void* user_data) { |
| 149 |
SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
| 150 |
|
| 151 |
// Batch call getWidths since entry cost is not cheap. |
| 152 |
// Copy requred because getWidths is dense and hb is strided. |
| 153 |
AutoSTMalloc<256, SkGlyphID> glyph(count); |
| 154 |
for (unsigned i = 0; i < count; i++) { |
| 155 |
glyph[i] = *glyphs; |
| 156 |
glyphs = SkTAddOffset<const hb_codepoint_t>(glyphs, glyph_stride); |
| 157 |
} |
| 158 |
AutoSTMalloc<256, SkScalar> advance(count); |
| 159 |
font.getWidths({glyph.data(), count}, {advance.data(), count}); |
| 160 |
|
| 161 |
if (!font.isSubpixel()) { |
| 162 |
for (unsigned i = 0; i < count; i++) { |
| 163 |
advance[i] = SkScalarRoundToInt(advance[i]); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// Copy the results back to the sparse array. |
| 168 |
for (unsigned i = 0; i < count; i++) { |
| 169 |
*advances = skhb_position(advance[i]); |
| 170 |
advances = SkTAddOffset<hb_position_t>(advances, advance_stride); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// HarfBuzz callback to retrieve glyph extents, mainly used by HarfBuzz for |
| 175 |
// fallback mark positioning, i.e. the situation when the font does not have |
| 176 |
// mark anchors or other mark positioning rules, but instead HarfBuzz is |
| 177 |
// supposed to heuristically place combining marks around base glyphs. HarfBuzz |
| 178 |
// does this by measuring "ink boxes" of glyphs, and placing them according to |
| 179 |
// Unicode mark classes. Above, below, centered or left or right, etc. |
| 180 |
hb_bool_t skhb_glyph_extents(hb_font_t* hb_font, |
| 181 |
void* font_data, |
| 182 |
hb_codepoint_t hbGlyph, |
| 183 |
hb_glyph_extents_t* extents, |
| 184 |
void* user_data) { |
| 185 |
SkFont& font = *reinterpret_cast<SkFont*>(font_data); |
| 186 |
SkASSERT(extents); |
| 187 |
|
| 188 |
SkRect sk_bounds; |
| 189 |
SkGlyphID skGlyph = SkTo<SkGlyphID>(hbGlyph); |
| 190 |
|
| 191 |
font.getBounds({&skGlyph, 1}, {&sk_bounds, 1}, nullptr); |
| 192 |
if (!font.isSubpixel()) { |
| 193 |
sk_bounds.set(sk_bounds.roundOut()); |
| 194 |
} |
| 195 |
|
| 196 |
// Skia is y-down but HarfBuzz is y-up. |
| 197 |
extents->x_bearing = skhb_position(sk_bounds.fLeft); |
| 198 |
extents->y_bearing = skhb_position(-sk_bounds.fTop); |
| 199 |
extents->width = skhb_position(sk_bounds.width()); |
| 200 |
extents->height = skhb_position(-sk_bounds.height()); |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
#define SK_HB_VERSION_CHECK(x, y, z) \ |
| 205 |
(HB_VERSION_MAJOR > (x)) || \ |
| 206 |
(HB_VERSION_MAJOR == (x) && HB_VERSION_MINOR > (y)) || \ |
| 207 |
(HB_VERSION_MAJOR == (x) && HB_VERSION_MINOR == (y) && HB_VERSION_MICRO >= (z)) |
| 208 |
|
| 209 |
hb_font_funcs_t* skhb_get_font_funcs() { |
| 210 |
static hb_font_funcs_t* const funcs = []{ |
| 211 |
// HarfBuzz will use the default (parent) implementation if they aren't set. |
| 212 |
hb_font_funcs_t* const funcs = hb_font_funcs_create(); |
| 213 |
hb_font_funcs_set_variation_glyph_func(funcs, skhb_glyph, nullptr, nullptr); |
| 214 |
hb_font_funcs_set_nominal_glyph_func(funcs, skhb_nominal_glyph, nullptr, nullptr); |
| 215 |
#if SK_HB_VERSION_CHECK(2, 0, 0) |
| 216 |
hb_font_funcs_set_nominal_glyphs_func(funcs, skhb_nominal_glyphs, nullptr, nullptr); |
| 217 |
#else |
| 218 |
sk_ignore_unused_variable(skhb_nominal_glyphs); |
| 219 |
#endif |
| 220 |
hb_font_funcs_set_glyph_h_advance_func(funcs, skhb_glyph_h_advance, nullptr, nullptr); |
| 221 |
#if SK_HB_VERSION_CHECK(1, 8, 6) |
| 222 |
hb_font_funcs_set_glyph_h_advances_func(funcs, skhb_glyph_h_advances, nullptr, nullptr); |
| 223 |
#else |
| 224 |
sk_ignore_unused_variable(skhb_glyph_h_advances); |
| 225 |
#endif |
| 226 |
hb_font_funcs_set_glyph_extents_func(funcs, skhb_glyph_extents, nullptr, nullptr); |
| 227 |
hb_font_funcs_make_immutable(funcs); |
| 228 |
return funcs; |
| 229 |
}(); |
| 230 |
SkASSERT(funcs); |
| 231 |
return funcs; |
| 232 |
} |
| 233 |
|
| 234 |
hb_blob_t* skhb_get_table(hb_face_t* face, hb_tag_t tag, void* user_data) { |
| 235 |
SkTypeface& typeface = *reinterpret_cast<SkTypeface*>(user_data); |
| 236 |
|
| 237 |
auto data = typeface.copyTableData(tag); |
| 238 |
if (!data) { |
| 239 |
return nullptr; |
| 240 |
} |
| 241 |
SkData* rawData = data.release(); |
| 242 |
return hb_blob_create(reinterpret_cast<char*>(rawData->writable_data()), rawData->size(), |
| 243 |
HB_MEMORY_MODE_READONLY, rawData, [](void* ctx) { |
| 244 |
SkSafeUnref(((SkData*)ctx)); |
| 245 |
}); |
| 246 |
} |
| 247 |
|
| 248 |
HBBlob stream_to_blob(std::unique_ptr<SkStreamAsset> asset) { |
| 249 |
size_t size = asset->getLength(); |
| 250 |
HBBlob blob; |
| 251 |
if (const void* base = asset->getMemoryBase()) { |
| 252 |
blob.reset(hb_blob_create((const char*)base, SkToUInt(size), |
| 253 |
HB_MEMORY_MODE_READONLY, asset.release(), |
| 254 |
[](void* p) { delete (SkStreamAsset*)p; })); |
| 255 |
} else { |
| 256 |
// SkDebugf("Extra SkStreamAsset copy\n"); |
| 257 |
void* ptr = size ? sk_malloc_throw(size) : nullptr; |
| 258 |
asset->read(ptr, size); |
| 259 |
blob.reset(hb_blob_create((char*)ptr, SkToUInt(size), |
| 260 |
HB_MEMORY_MODE_READONLY, ptr, sk_free)); |
| 261 |
} |
| 262 |
SkASSERT(blob); |
| 263 |
hb_blob_make_immutable(blob.get()); |
| 264 |
return blob; |
| 265 |
} |
| 266 |
|
| 267 |
SkDEBUGCODE(static hb_user_data_key_t gDataIdKey;) |
| 268 |
|
| 269 |
HBFace create_hb_face(const SkTypeface& typeface) { |
| 270 |
int index = 0; |
| 271 |
std::unique_ptr<SkStreamAsset> typefaceAsset = typeface.openExistingStream(&index); |
| 272 |
HBFace face; |
| 273 |
if (typefaceAsset && typefaceAsset->getMemoryBase()) { |
| 274 |
HBBlob blob(stream_to_blob(std::move(typefaceAsset))); |
| 275 |
// hb_face_create always succeeds. Check that the format is minimally recognized first. |
| 276 |
// hb_face_create_for_tables may still create a working hb_face. |
| 277 |
// See https://github.com/harfbuzz/harfbuzz/issues/248 . |
| 278 |
unsigned int num_hb_faces = hb_face_count(blob.get()); |
| 279 |
if (0 < num_hb_faces && (unsigned)index < num_hb_faces) { |
| 280 |
face.reset(hb_face_create(blob.get(), (unsigned)index)); |
| 281 |
// Check the number of glyphs as a basic sanitization step. |
| 282 |
if (face && hb_face_get_glyph_count(face.get()) == 0) { |
| 283 |
face.reset(); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
if (!face) { |
| 288 |
face.reset(hb_face_create_for_tables( |
| 289 |
skhb_get_table, |
| 290 |
const_cast<SkTypeface*>(SkRef(&typeface)), |
| 291 |
[](void* user_data){ SkSafeUnref(reinterpret_cast<SkTypeface*>(user_data)); })); |
| 292 |
hb_face_set_index(face.get(), (unsigned)index); |
| 293 |
} |
| 294 |
SkASSERT(face); |
| 295 |
if (!face) { |
| 296 |
return nullptr; |
| 297 |
} |
| 298 |
hb_face_set_upem(face.get(), typeface.getUnitsPerEm()); |
| 299 |
|
| 300 |
SkDEBUGCODE( |
| 301 |
hb_face_set_user_data(face.get(), &gDataIdKey, const_cast<SkTypeface*>(&typeface), |
| 302 |
nullptr, false); |
| 303 |
) |
| 304 |
|
| 305 |
return face; |
| 306 |
} |
| 307 |
|
| 308 |
HBFont create_typeface_hb_font(const SkTypeface& typeface) { |
| 309 |
HBFace face(create_hb_face(typeface)); |
| 310 |
if (!face) { |
| 311 |
return nullptr; |
| 312 |
} |
| 313 |
|
| 314 |
HBFont otFont(hb_font_create(face.get())); |
| 315 |
SkASSERT(otFont); |
| 316 |
if (!otFont) { |
| 317 |
return nullptr; |
| 318 |
} |
| 319 |
hb_ot_font_set_funcs(otFont.get()); |
| 320 |
int axis_count = typeface.getVariationDesignPosition({}); |
| 321 |
if (axis_count > 0) { |
| 322 |
AutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate> axis_values(axis_count); |
| 323 |
if (typeface.getVariationDesignPosition({axis_values.get(), |
| 324 |
(size_t)axis_count}) == axis_count) { |
| 325 |
hb_font_set_variations(otFont.get(), |
| 326 |
reinterpret_cast<hb_variation_t*>(axis_values.get()), |
| 327 |
axis_count); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return otFont; |
| 332 |
} |
| 333 |
|
| 334 |
HBFont create_sub_hb_font(const SkFont& font, const HBFont& typefaceFont) { |
| 335 |
SkDEBUGCODE( |
| 336 |
hb_face_t* face = hb_font_get_face(typefaceFont.get()); |
| 337 |
void* dataId = hb_face_get_user_data(face, &gDataIdKey); |
| 338 |
SkASSERT(dataId == font.getTypeface()); |
| 339 |
) |
| 340 |
|
| 341 |
// Creating a sub font means that non-available functions |
| 342 |
// are found from the parent. |
| 343 |
HBFont skFont(hb_font_create_sub_font(typefaceFont.get())); |
| 344 |
hb_font_set_funcs(skFont.get(), skhb_get_font_funcs(), |
| 345 |
reinterpret_cast<void *>(new SkFont(font)), |
| 346 |
[](void* user_data){ delete reinterpret_cast<SkFont*>(user_data); }); |
| 347 |
int scale = skhb_position(font.getSize()); |
| 348 |
hb_font_set_scale(skFont.get(), scale, scale); |
| 349 |
|
| 350 |
return skFont; |
| 351 |
} |
| 352 |
|
| 353 |
/** Replaces invalid utf-8 sequences with REPLACEMENT CHARACTER U+FFFD. */ |
| 354 |
static inline SkUnichar utf8_next(const char** ptr, const char* end) { |
| 355 |
SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 356 |
return val < 0 ? 0xFFFD : val; |
| 357 |
} |
| 358 |
|
| 359 |
class SkUnicodeHbScriptRunIterator final: public SkShaper::ScriptRunIterator { |
| 360 |
public: |
| 361 |
SkUnicodeHbScriptRunIterator(const char* utf8, |
| 362 |
size_t utf8Bytes, |
| 363 |
hb_script_t defaultScript) |
| 364 |
: fCurrent(utf8) |
| 365 |
, fBegin(utf8) |
| 366 |
, fEnd(fCurrent + utf8Bytes) |
| 367 |
, fCurrentScript(defaultScript) {} |
| 368 |
hb_script_t hb_script_for_unichar(SkUnichar u) { |
| 369 |
return hb_unicode_script(hb_unicode_funcs_get_default(), u); |
| 370 |
} |
| 371 |
void consume() override { |
| 372 |
SkASSERT(fCurrent < fEnd); |
| 373 |
SkUnichar u = utf8_next(&fCurrent, fEnd); |
| 374 |
fCurrentScript = hb_script_for_unichar(u); |
| 375 |
while (fCurrent < fEnd) { |
| 376 |
const char* prev = fCurrent; |
| 377 |
u = utf8_next(&fCurrent, fEnd); |
| 378 |
const hb_script_t script = hb_script_for_unichar(u); |
| 379 |
if (script != fCurrentScript) { |
| 380 |
if (fCurrentScript == HB_SCRIPT_INHERITED || fCurrentScript == HB_SCRIPT_COMMON) { |
| 381 |
fCurrentScript = script; |
| 382 |
} else if (script == HB_SCRIPT_INHERITED || script == HB_SCRIPT_COMMON) { |
| 383 |
continue; |
| 384 |
} else { |
| 385 |
fCurrent = prev; |
| 386 |
break; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
if (fCurrentScript == HB_SCRIPT_INHERITED) { |
| 391 |
fCurrentScript = HB_SCRIPT_COMMON; |
| 392 |
} |
| 393 |
} |
| 394 |
size_t endOfCurrentRun() const override { |
| 395 |
return fCurrent - fBegin; |
| 396 |
} |
| 397 |
bool atEnd() const override { |
| 398 |
return fCurrent == fEnd; |
| 399 |
} |
| 400 |
|
| 401 |
SkFourByteTag currentScript() const override { |
| 402 |
return SkSetFourByteTag(HB_UNTAG(fCurrentScript)); |
| 403 |
} |
| 404 |
private: |
| 405 |
char const * fCurrent; |
| 406 |
char const * const fBegin; |
| 407 |
char const * const fEnd; |
| 408 |
hb_script_t fCurrentScript; |
| 409 |
}; |
| 410 |
|
| 411 |
class RunIteratorQueue { |
| 412 |
public: |
| 413 |
void insert(SkShaper::RunIterator* runIterator, int priority) { |
| 414 |
fEntries.insert({runIterator, priority}); |
| 415 |
} |
| 416 |
|
| 417 |
bool advanceRuns() { |
| 418 |
const SkShaper::RunIterator* leastRun = fEntries.peek().runIterator; |
| 419 |
if (leastRun->atEnd()) { |
| 420 |
SkASSERT(this->allRunsAreAtEnd()); |
| 421 |
return false; |
| 422 |
} |
| 423 |
const size_t leastEnd = leastRun->endOfCurrentRun(); |
| 424 |
SkShaper::RunIterator* currentRun = nullptr; |
| 425 |
SkDEBUGCODE(size_t previousEndOfCurrentRun); |
| 426 |
while ((currentRun = fEntries.peek().runIterator)->endOfCurrentRun() <= leastEnd) { |
| 427 |
int priority = fEntries.peek().priority; |
| 428 |
fEntries.pop(); |
| 429 |
SkDEBUGCODE(previousEndOfCurrentRun = currentRun->endOfCurrentRun()); |
| 430 |
currentRun->consume(); |
| 431 |
SkASSERT(previousEndOfCurrentRun < currentRun->endOfCurrentRun()); |
| 432 |
fEntries.insert({currentRun, priority}); |
| 433 |
} |
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
size_t endOfCurrentRun() const { |
| 438 |
return fEntries.peek().runIterator->endOfCurrentRun(); |
| 439 |
} |
| 440 |
|
| 441 |
private: |
| 442 |
bool allRunsAreAtEnd() const { |
| 443 |
for (int i = 0; i < fEntries.count(); ++i) { |
| 444 |
if (!fEntries.at(i).runIterator->atEnd()) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
} |
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
struct Entry { |
| 452 |
SkShaper::RunIterator* runIterator; |
| 453 |
int priority; |
| 454 |
}; |
| 455 |
static bool CompareEntry(Entry const& a, Entry const& b) { |
| 456 |
size_t aEnd = a.runIterator->endOfCurrentRun(); |
| 457 |
size_t bEnd = b.runIterator->endOfCurrentRun(); |
| 458 |
return aEnd < bEnd || (aEnd == bEnd && a.priority < b.priority); |
| 459 |
} |
| 460 |
SkTDPQueue<Entry, CompareEntry> fEntries; |
| 461 |
}; |
| 462 |
|
| 463 |
struct ShapedGlyph { |
| 464 |
SkGlyphID fID; |
| 465 |
uint32_t fCluster; |
| 466 |
SkPoint fOffset; |
| 467 |
SkVector fAdvance; |
| 468 |
bool fMayLineBreakBefore; |
| 469 |
bool fMustLineBreakBefore; |
| 470 |
bool fHasVisual; |
| 471 |
bool fGraphemeBreakBefore; |
| 472 |
bool fUnsafeToBreak; |
| 473 |
}; |
| 474 |
struct ShapedRun { |
| 475 |
ShapedRun(SkShaper::RunHandler::Range utf8Range, |
| 476 |
const SkFont& font, |
| 477 |
SkBidiIterator::Level level, |
| 478 |
SkFourByteTag script, |
| 479 |
const char* language, |
| 480 |
std::unique_ptr<ShapedGlyph[]> glyphs, size_t numGlyphs, |
| 481 |
SkVector advance = {0, 0}) |
| 482 |
: fUtf8Range(utf8Range), fFont(font), fLevel(level), fScript(script), fLanguage(language) |
| 483 |
, fGlyphs(std::move(glyphs)), fNumGlyphs(numGlyphs), fAdvance(advance) |
| 484 |
{} |
| 485 |
|
| 486 |
SkShaper::RunHandler::Range fUtf8Range; |
| 487 |
SkFont fFont; |
| 488 |
SkBidiIterator::Level fLevel; |
| 489 |
SkFourByteTag fScript; |
| 490 |
const char* fLanguage; |
| 491 |
std::unique_ptr<ShapedGlyph[]> fGlyphs; |
| 492 |
size_t fNumGlyphs; |
| 493 |
SkVector fAdvance; |
| 494 |
|
| 495 |
static_assert(::sk_is_trivially_relocatable<decltype(fUtf8Range)>::value); |
| 496 |
static_assert(::sk_is_trivially_relocatable<decltype(fFont)>::value); |
| 497 |
static_assert(::sk_is_trivially_relocatable<decltype(fLevel)>::value); |
| 498 |
static_assert(::sk_is_trivially_relocatable<decltype(fGlyphs)>::value); |
| 499 |
static_assert(::sk_is_trivially_relocatable<decltype(fAdvance)>::value); |
| 500 |
|
| 501 |
using sk_is_trivially_relocatable = std::true_type; |
| 502 |
}; |
| 503 |
struct ShapedLine { |
| 504 |
TArray<ShapedRun> runs; |
| 505 |
SkVector fAdvance = { 0, 0 }; |
| 506 |
}; |
| 507 |
|
| 508 |
constexpr bool is_LTR(SkBidiIterator::Level level) { |
| 509 |
return (level & 1) == 0; |
| 510 |
} |
| 511 |
|
| 512 |
void append(SkShaper::RunHandler* handler, const SkShaper::RunHandler::RunInfo& runInfo, |
| 513 |
const ShapedRun& run, size_t startGlyphIndex, size_t endGlyphIndex) { |
| 514 |
SkASSERT(startGlyphIndex <= endGlyphIndex); |
| 515 |
const size_t glyphLen = endGlyphIndex - startGlyphIndex; |
| 516 |
|
| 517 |
const auto buffer = handler->runBuffer(runInfo); |
| 518 |
SkASSERT(buffer.glyphs); |
| 519 |
SkASSERT(buffer.positions); |
| 520 |
|
| 521 |
SkVector advance = {0,0}; |
| 522 |
for (size_t i = 0; i < glyphLen; i++) { |
| 523 |
// Glyphs are in logical order, but output ltr since PDF readers seem to expect that. |
| 524 |
const ShapedGlyph& glyph = run.fGlyphs[is_LTR(run.fLevel) ? startGlyphIndex + i |
| 525 |
: endGlyphIndex - 1 - i]; |
| 526 |
buffer.glyphs[i] = glyph.fID; |
| 527 |
if (buffer.offsets) { |
| 528 |
buffer.positions[i] = advance + buffer.point; |
| 529 |
buffer.offsets[i] = glyph.fOffset; |
| 530 |
} else { |
| 531 |
buffer.positions[i] = advance + buffer.point + glyph.fOffset; |
| 532 |
} |
| 533 |
if (buffer.clusters) { |
| 534 |
buffer.clusters[i] = glyph.fCluster; |
| 535 |
} |
| 536 |
advance += glyph.fAdvance; |
| 537 |
} |
| 538 |
handler->commitRunBuffer(runInfo); |
| 539 |
} |
| 540 |
|
| 541 |
void emit(SkUnicode* unicode, const ShapedLine& line, SkShaper::RunHandler* handler) { |
| 542 |
// Reorder the runs and glyphs per line and write them out. |
| 543 |
handler->beginLine(); |
| 544 |
|
| 545 |
int numRuns = line.runs.size(); |
| 546 |
AutoSTMalloc<4, SkBidiIterator::Level> runLevels(numRuns); |
| 547 |
for (int i = 0; i < numRuns; ++i) { |
| 548 |
runLevels[i] = line.runs[i].fLevel; |
| 549 |
} |
| 550 |
AutoSTMalloc<4, int32_t> logicalFromVisual(numRuns); |
| 551 |
unicode->reorderVisual(runLevels, numRuns, logicalFromVisual); |
| 552 |
|
| 553 |
for (int i = 0; i < numRuns; ++i) { |
| 554 |
int logicalIndex = logicalFromVisual[i]; |
| 555 |
|
| 556 |
const auto& run = line.runs[logicalIndex]; |
| 557 |
const SkShaper::RunHandler::RunInfo info = { |
| 558 |
run.fFont, |
| 559 |
run.fLevel, |
| 560 |
run.fScript, |
| 561 |
run.fLanguage, |
| 562 |
run.fAdvance, |
| 563 |
run.fNumGlyphs, |
| 564 |
run.fUtf8Range |
| 565 |
}; |
| 566 |
handler->runInfo(info); |
| 567 |
} |
| 568 |
handler->commitRunInfo(); |
| 569 |
for (int i = 0; i < numRuns; ++i) { |
| 570 |
int logicalIndex = logicalFromVisual[i]; |
| 571 |
|
| 572 |
const auto& run = line.runs[logicalIndex]; |
| 573 |
const SkShaper::RunHandler::RunInfo info = { |
| 574 |
run.fFont, |
| 575 |
run.fLevel, |
| 576 |
run.fScript, |
| 577 |
run.fLanguage, |
| 578 |
run.fAdvance, |
| 579 |
run.fNumGlyphs, |
| 580 |
run.fUtf8Range |
| 581 |
}; |
| 582 |
append(handler, info, run, 0, run.fNumGlyphs); |
| 583 |
} |
| 584 |
|
| 585 |
handler->commitLine(); |
| 586 |
} |
| 587 |
|
| 588 |
struct ShapedRunGlyphIterator { |
| 589 |
ShapedRunGlyphIterator(const TArray<ShapedRun>& origRuns) |
| 590 |
: fRuns(&origRuns), fRunIndex(0), fGlyphIndex(0) |
| 591 |
{ } |
| 592 |
|
| 593 |
ShapedRunGlyphIterator(const ShapedRunGlyphIterator& that) = default; |
| 594 |
ShapedRunGlyphIterator& operator=(const ShapedRunGlyphIterator& that) = default; |
| 595 |
bool operator==(const ShapedRunGlyphIterator& that) const { |
| 596 |
return fRuns == that.fRuns && |
| 597 |
fRunIndex == that.fRunIndex && |
| 598 |
fGlyphIndex == that.fGlyphIndex; |
| 599 |
} |
| 600 |
bool operator!=(const ShapedRunGlyphIterator& that) const { |
| 601 |
return fRuns != that.fRuns || |
| 602 |
fRunIndex != that.fRunIndex || |
| 603 |
fGlyphIndex != that.fGlyphIndex; |
| 604 |
} |
| 605 |
|
| 606 |
ShapedGlyph* next() { |
| 607 |
const TArray<ShapedRun>& runs = *fRuns; |
| 608 |
SkASSERT(fRunIndex < runs.size()); |
| 609 |
SkASSERT(fGlyphIndex < runs[fRunIndex].fNumGlyphs); |
| 610 |
|
| 611 |
++fGlyphIndex; |
| 612 |
if (fGlyphIndex == runs[fRunIndex].fNumGlyphs) { |
| 613 |
fGlyphIndex = 0; |
| 614 |
++fRunIndex; |
| 615 |
if (fRunIndex >= runs.size()) { |
| 616 |
return nullptr; |
| 617 |
} |
| 618 |
} |
| 619 |
return &runs[fRunIndex].fGlyphs[fGlyphIndex]; |
| 620 |
} |
| 621 |
|
| 622 |
ShapedGlyph* current() { |
| 623 |
const TArray<ShapedRun>& runs = *fRuns; |
| 624 |
if (fRunIndex >= runs.size()) { |
| 625 |
return nullptr; |
| 626 |
} |
| 627 |
return &runs[fRunIndex].fGlyphs[fGlyphIndex]; |
| 628 |
} |
| 629 |
|
| 630 |
const TArray<ShapedRun>* fRuns; |
| 631 |
int fRunIndex; |
| 632 |
size_t fGlyphIndex; |
| 633 |
}; |
| 634 |
|
| 635 |
class ShaperHarfBuzz : public SkShaper { |
| 636 |
public: |
| 637 |
ShaperHarfBuzz(sk_sp<SkUnicode>, |
| 638 |
HBBuffer, |
| 639 |
sk_sp<SkFontMgr>); |
| 640 |
|
| 641 |
protected: |
| 642 |
sk_sp<SkUnicode> fUnicode; |
| 643 |
|
| 644 |
ShapedRun shape(const char* utf8, size_t utf8Bytes, |
| 645 |
const char* utf8Start, |
| 646 |
const char* utf8End, |
| 647 |
const BiDiRunIterator&, |
| 648 |
const LanguageRunIterator&, |
| 649 |
const ScriptRunIterator&, |
| 650 |
const FontRunIterator&, |
| 651 |
const Feature*, size_t featuresSize) const; |
| 652 |
private: |
| 653 |
const sk_sp<SkFontMgr> fFontMgr; // for fallback |
| 654 |
HBBuffer fBuffer; |
| 655 |
hb_language_t fUndefinedLanguage; |
| 656 |
|
| 657 |
#if !defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 658 |
void shape(const char* utf8, size_t utf8Bytes, |
| 659 |
const SkFont&, |
| 660 |
bool leftToRight, |
| 661 |
SkScalar width, |
| 662 |
RunHandler*) const override; |
| 663 |
|
| 664 |
void shape(const char* utf8Text, size_t textBytes, |
| 665 |
FontRunIterator&, |
| 666 |
BiDiRunIterator&, |
| 667 |
ScriptRunIterator&, |
| 668 |
LanguageRunIterator&, |
| 669 |
SkScalar width, |
| 670 |
RunHandler*) const override; |
| 671 |
#endif |
| 672 |
|
| 673 |
void shape(const char* utf8Text, size_t textBytes, |
| 674 |
FontRunIterator&, |
| 675 |
BiDiRunIterator&, |
| 676 |
ScriptRunIterator&, |
| 677 |
LanguageRunIterator&, |
| 678 |
const Feature*, size_t featuresSize, |
| 679 |
SkScalar width, |
| 680 |
RunHandler*) const override; |
| 681 |
|
| 682 |
virtual void wrap(char const * utf8, size_t utf8Bytes, |
| 683 |
const BiDiRunIterator&, |
| 684 |
const LanguageRunIterator&, |
| 685 |
const ScriptRunIterator&, |
| 686 |
const FontRunIterator&, |
| 687 |
RunIteratorQueue& runSegmenter, |
| 688 |
const Feature*, size_t featuresSize, |
| 689 |
SkScalar width, |
| 690 |
RunHandler*) const = 0; |
| 691 |
}; |
| 692 |
|
| 693 |
class ShaperDrivenWrapper : public ShaperHarfBuzz { |
| 694 |
public: |
| 695 |
using ShaperHarfBuzz::ShaperHarfBuzz; |
| 696 |
private: |
| 697 |
void wrap(char const * utf8, size_t utf8Bytes, |
| 698 |
const BiDiRunIterator&, |
| 699 |
const LanguageRunIterator&, |
| 700 |
const ScriptRunIterator&, |
| 701 |
const FontRunIterator&, |
| 702 |
RunIteratorQueue& runSegmenter, |
| 703 |
const Feature*, size_t featuresSize, |
| 704 |
SkScalar width, |
| 705 |
RunHandler*) const override; |
| 706 |
}; |
| 707 |
|
| 708 |
class ShapeThenWrap : public ShaperHarfBuzz { |
| 709 |
public: |
| 710 |
using ShaperHarfBuzz::ShaperHarfBuzz; |
| 711 |
private: |
| 712 |
void wrap(char const * utf8, size_t utf8Bytes, |
| 713 |
const BiDiRunIterator&, |
| 714 |
const LanguageRunIterator&, |
| 715 |
const ScriptRunIterator&, |
| 716 |
const FontRunIterator&, |
| 717 |
RunIteratorQueue& runSegmenter, |
| 718 |
const Feature*, size_t featuresSize, |
| 719 |
SkScalar width, |
| 720 |
RunHandler*) const override; |
| 721 |
}; |
| 722 |
|
| 723 |
class ShapeDontWrapOrReorder : public ShaperHarfBuzz { |
| 724 |
public: |
| 725 |
using ShaperHarfBuzz::ShaperHarfBuzz; |
| 726 |
private: |
| 727 |
void wrap(char const * utf8, size_t utf8Bytes, |
| 728 |
const BiDiRunIterator&, |
| 729 |
const LanguageRunIterator&, |
| 730 |
const ScriptRunIterator&, |
| 731 |
const FontRunIterator&, |
| 732 |
RunIteratorQueue& runSegmenter, |
| 733 |
const Feature*, size_t featuresSize, |
| 734 |
SkScalar width, |
| 735 |
RunHandler*) const override; |
| 736 |
}; |
| 737 |
|
| 738 |
ShaperHarfBuzz::ShaperHarfBuzz(sk_sp<SkUnicode> unicode, |
| 739 |
HBBuffer buffer, |
| 740 |
sk_sp<SkFontMgr> fallback) |
| 741 |
: fUnicode(unicode) |
| 742 |
, fFontMgr(fallback ? std::move(fallback) : SkFontMgr::RefEmpty()) |
| 743 |
, fBuffer(std::move(buffer)) |
| 744 |
, fUndefinedLanguage(hb_language_from_string("und", -1)) { |
| 745 |
#if defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 746 |
SkASSERT(fUnicode); |
| 747 |
#endif |
| 748 |
} |
| 749 |
|
| 750 |
#if !defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 751 |
void ShaperHarfBuzz::shape(const char* utf8, |
| 752 |
size_t utf8Bytes, |
| 753 |
const SkFont& srcFont, |
| 754 |
bool leftToRight, |
| 755 |
SkScalar width, |
| 756 |
RunHandler* handler) const { |
| 757 |
SkBidiIterator::Level defaultLevel = leftToRight ? SkBidiIterator::kLTR : SkBidiIterator::kRTL; |
| 758 |
std::unique_ptr<BiDiRunIterator> bidi( |
| 759 |
SkShapers::unicode::BidiRunIterator(fUnicode, utf8, utf8Bytes, defaultLevel)); |
| 760 |
|
| 761 |
if (!bidi) { |
| 762 |
return; |
| 763 |
} |
| 764 |
|
| 765 |
std::unique_ptr<LanguageRunIterator> language(MakeStdLanguageRunIterator(utf8, utf8Bytes)); |
| 766 |
if (!language) { |
| 767 |
return; |
| 768 |
} |
| 769 |
|
| 770 |
std::unique_ptr<ScriptRunIterator> script(SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes)); |
| 771 |
if (!script) { |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
std::unique_ptr<FontRunIterator> font( |
| 776 |
MakeFontMgrRunIterator(utf8, utf8Bytes, srcFont, fFontMgr)); |
| 777 |
if (!font) { |
| 778 |
return; |
| 779 |
} |
| 780 |
|
| 781 |
this->shape(utf8, utf8Bytes, *font, *bidi, *script, *language, width, handler); |
| 782 |
} |
| 783 |
|
| 784 |
void ShaperHarfBuzz::shape(const char* utf8, |
| 785 |
size_t utf8Bytes, |
| 786 |
FontRunIterator& font, |
| 787 |
BiDiRunIterator& bidi, |
| 788 |
ScriptRunIterator& script, |
| 789 |
LanguageRunIterator& language, |
| 790 |
SkScalar width, |
| 791 |
RunHandler* handler) const { |
| 792 |
this->shape(utf8, utf8Bytes, font, bidi, script, language, nullptr, 0, width, handler); |
| 793 |
} |
| 794 |
#endif // !defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 795 |
|
| 796 |
void ShaperHarfBuzz::shape(const char* utf8, |
| 797 |
size_t utf8Bytes, |
| 798 |
FontRunIterator& font, |
| 799 |
BiDiRunIterator& bidi, |
| 800 |
ScriptRunIterator& script, |
| 801 |
LanguageRunIterator& language, |
| 802 |
const Feature* features, |
| 803 |
size_t featuresSize, |
| 804 |
SkScalar width, |
| 805 |
RunHandler* handler) const { |
| 806 |
SkASSERT(handler); |
| 807 |
RunIteratorQueue runSegmenter; |
| 808 |
runSegmenter.insert(&font, 3); // The font iterator is always run last in case of tie. |
| 809 |
runSegmenter.insert(&bidi, 2); |
| 810 |
runSegmenter.insert(&script, 1); |
| 811 |
runSegmenter.insert(&language, 0); |
| 812 |
|
| 813 |
this->wrap(utf8, utf8Bytes, bidi, language, script, font, runSegmenter, |
| 814 |
features, featuresSize, width, handler); |
| 815 |
} |
| 816 |
|
| 817 |
void ShaperDrivenWrapper::wrap(char const * const utf8, size_t utf8Bytes, |
| 818 |
const BiDiRunIterator& bidi, |
| 819 |
const LanguageRunIterator& language, |
| 820 |
const ScriptRunIterator& script, |
| 821 |
const FontRunIterator& font, |
| 822 |
RunIteratorQueue& runSegmenter, |
| 823 |
const Feature* features, size_t featuresSize, |
| 824 |
SkScalar width, |
| 825 |
RunHandler* handler) const |
| 826 |
{ |
| 827 |
ShapedLine line; |
| 828 |
|
| 829 |
const char* utf8Start = nullptr; |
| 830 |
const char* utf8End = utf8; |
| 831 |
SkUnicodeBreak lineBreakIterator; |
| 832 |
SkString currentLanguage; |
| 833 |
while (runSegmenter.advanceRuns()) { // For each item |
| 834 |
utf8Start = utf8End; |
| 835 |
utf8End = utf8 + runSegmenter.endOfCurrentRun(); |
| 836 |
|
| 837 |
ShapedRun model(RunHandler::Range(), SkFont(), 0, 0, {}, nullptr, 0); |
| 838 |
bool modelNeedsRegenerated = true; |
| 839 |
int modelGlyphOffset = 0; |
| 840 |
|
| 841 |
struct TextProps { |
| 842 |
int glyphLen = 0; |
| 843 |
SkVector advance = {0, 0}; |
| 844 |
}; |
| 845 |
// map from character position to [safe to break, glyph position, advance] |
| 846 |
std::unique_ptr<TextProps[]> modelText; |
| 847 |
int modelTextOffset = 0; |
| 848 |
SkVector modelAdvanceOffset = {0, 0}; |
| 849 |
|
| 850 |
while (utf8Start < utf8End) { // While there are still code points left in this item |
| 851 |
size_t utf8runLength = utf8End - utf8Start; |
| 852 |
if (modelNeedsRegenerated) { |
| 853 |
model = shape(utf8, utf8Bytes, |
| 854 |
utf8Start, utf8End, |
| 855 |
bidi, language, script, font, |
| 856 |
features, featuresSize); |
| 857 |
modelGlyphOffset = 0; |
| 858 |
|
| 859 |
SkVector advance = {0, 0}; |
| 860 |
modelText = std::make_unique<TextProps[]>(utf8runLength + 1); |
| 861 |
size_t modelStartCluster = utf8Start - utf8; |
| 862 |
size_t previousCluster = 0; |
| 863 |
for (size_t i = 0; i < model.fNumGlyphs; ++i) { |
| 864 |
SkASSERT(modelStartCluster <= model.fGlyphs[i].fCluster); |
| 865 |
SkASSERT( model.fGlyphs[i].fCluster < (size_t)(utf8End - utf8)); |
| 866 |
if (!model.fGlyphs[i].fUnsafeToBreak) { |
| 867 |
// Store up to the first glyph in the cluster. |
| 868 |
size_t currentCluster = model.fGlyphs[i].fCluster - modelStartCluster; |
| 869 |
if (previousCluster != currentCluster) { |
| 870 |
previousCluster = currentCluster; |
| 871 |
modelText[currentCluster].glyphLen = i; |
| 872 |
modelText[currentCluster].advance = advance; |
| 873 |
} |
| 874 |
} |
| 875 |
advance += model.fGlyphs[i].fAdvance; |
| 876 |
} |
| 877 |
// Assume it is always safe to break after the end of an item |
| 878 |
modelText[utf8runLength].glyphLen = model.fNumGlyphs; |
| 879 |
modelText[utf8runLength].advance = model.fAdvance; |
| 880 |
modelTextOffset = 0; |
| 881 |
modelAdvanceOffset = {0, 0}; |
| 882 |
modelNeedsRegenerated = false; |
| 883 |
} |
| 884 |
|
| 885 |
// TODO: break iterator per item, but just reset position if needed? |
| 886 |
// Maybe break iterator with model? |
| 887 |
if (!lineBreakIterator || !currentLanguage.equals(language.currentLanguage())) { |
| 888 |
currentLanguage = language.currentLanguage(); |
| 889 |
lineBreakIterator = fUnicode->makeBreakIterator(currentLanguage.c_str(), |
| 890 |
SkUnicode::BreakType::kLines); |
| 891 |
if (!lineBreakIterator) { |
| 892 |
return; |
| 893 |
} |
| 894 |
} |
| 895 |
if (!lineBreakIterator->setText(utf8Start, utf8runLength)) { |
| 896 |
return; |
| 897 |
} |
| 898 |
SkBreakIterator& breakIterator = *lineBreakIterator; |
| 899 |
|
| 900 |
ShapedRun best(RunHandler::Range(), SkFont(), 0, 0, {}, nullptr, 0, |
| 901 |
{ SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity }); |
| 902 |
bool bestIsInvalid = true; |
| 903 |
bool bestUsesModelForGlyphs = false; |
| 904 |
SkScalar widthLeft = width - line.fAdvance.fX; |
| 905 |
|
| 906 |
for (int32_t breakIteratorCurrent = breakIterator.next(); |
| 907 |
!breakIterator.isDone(); |
| 908 |
breakIteratorCurrent = breakIterator.next()) |
| 909 |
{ |
| 910 |
// TODO: if past a safe to break, future safe to break will be at least as long |
| 911 |
|
| 912 |
// TODO: adjust breakIteratorCurrent by ignorable whitespace |
| 913 |
bool candidateUsesModelForGlyphs = false; |
| 914 |
ShapedRun candidate = [&](const TextProps& props){ |
| 915 |
if (props.glyphLen) { |
| 916 |
candidateUsesModelForGlyphs = true; |
| 917 |
return ShapedRun(RunHandler::Range(utf8Start - utf8, breakIteratorCurrent), |
| 918 |
font.currentFont(), bidi.currentLevel(), |
| 919 |
script.currentScript(), language.currentLanguage(), |
| 920 |
std::unique_ptr<ShapedGlyph[]>(), |
| 921 |
props.glyphLen - modelGlyphOffset, |
| 922 |
props.advance - modelAdvanceOffset); |
| 923 |
} else { |
| 924 |
return shape(utf8, utf8Bytes, |
| 925 |
utf8Start, utf8Start + breakIteratorCurrent, |
| 926 |
bidi, language, script, font, |
| 927 |
features, featuresSize); |
| 928 |
} |
| 929 |
}(modelText[breakIteratorCurrent + modelTextOffset]); |
| 930 |
auto score = [widthLeft](const ShapedRun& run) -> SkScalar { |
| 931 |
if (run.fAdvance.fX < widthLeft) { |
| 932 |
return run.fUtf8Range.size(); |
| 933 |
} else { |
| 934 |
return widthLeft - run.fAdvance.fX; |
| 935 |
} |
| 936 |
}; |
| 937 |
if (bestIsInvalid || score(best) < score(candidate)) { |
| 938 |
best = std::move(candidate); |
| 939 |
bestIsInvalid = false; |
| 940 |
bestUsesModelForGlyphs = candidateUsesModelForGlyphs; |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
// If nothing fit (best score is negative) and the line is not empty |
| 945 |
if (width < line.fAdvance.fX + best.fAdvance.fX && !line.runs.empty()) { |
| 946 |
emit(fUnicode.get(), line, handler); |
| 947 |
line.runs.clear(); |
| 948 |
line.fAdvance = {0, 0}; |
| 949 |
} else { |
| 950 |
if (bestUsesModelForGlyphs) { |
| 951 |
best.fGlyphs = std::make_unique<ShapedGlyph[]>(best.fNumGlyphs); |
| 952 |
memcpy(best.fGlyphs.get(), model.fGlyphs.get() + modelGlyphOffset, |
| 953 |
best.fNumGlyphs * sizeof(ShapedGlyph)); |
| 954 |
modelGlyphOffset += best.fNumGlyphs; |
| 955 |
modelTextOffset += best.fUtf8Range.size(); |
| 956 |
modelAdvanceOffset += best.fAdvance; |
| 957 |
} else { |
| 958 |
modelNeedsRegenerated = true; |
| 959 |
} |
| 960 |
utf8Start += best.fUtf8Range.size(); |
| 961 |
line.fAdvance += best.fAdvance; |
| 962 |
line.runs.emplace_back(std::move(best)); |
| 963 |
|
| 964 |
// If item broken, emit line (prevent remainder from accidentally fitting) |
| 965 |
if (utf8Start != utf8End) { |
| 966 |
emit(fUnicode.get(), line, handler); |
| 967 |
line.runs.clear(); |
| 968 |
line.fAdvance = {0, 0}; |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
} |
| 973 |
emit(fUnicode.get(), line, handler); |
| 974 |
} |
| 975 |
|
| 976 |
void ShapeThenWrap::wrap(char const * const utf8, size_t utf8Bytes, |
| 977 |
const BiDiRunIterator& bidi, |
| 978 |
const LanguageRunIterator& language, |
| 979 |
const ScriptRunIterator& script, |
| 980 |
const FontRunIterator& font, |
| 981 |
RunIteratorQueue& runSegmenter, |
| 982 |
const Feature* features, size_t featuresSize, |
| 983 |
SkScalar width, |
| 984 |
RunHandler* handler) const |
| 985 |
{ |
| 986 |
TArray<ShapedRun> runs; |
| 987 |
{ |
| 988 |
SkString currentLanguage; |
| 989 |
SkUnicodeBreak lineBreakIterator; |
| 990 |
SkUnicodeBreak graphemeBreakIterator; |
| 991 |
bool needIteratorInit = true; |
| 992 |
const char* utf8Start = nullptr; |
| 993 |
const char* utf8End = utf8; |
| 994 |
while (runSegmenter.advanceRuns()) { |
| 995 |
utf8Start = utf8End; |
| 996 |
utf8End = utf8 + runSegmenter.endOfCurrentRun(); |
| 997 |
|
| 998 |
runs.emplace_back(shape(utf8, utf8Bytes, |
| 999 |
utf8Start, utf8End, |
| 1000 |
bidi, language, script, font, |
| 1001 |
features, featuresSize)); |
| 1002 |
ShapedRun& run = runs.back(); |
| 1003 |
|
| 1004 |
if (needIteratorInit || !currentLanguage.equals(language.currentLanguage())) { |
| 1005 |
currentLanguage = language.currentLanguage(); |
| 1006 |
lineBreakIterator = fUnicode->makeBreakIterator(currentLanguage.c_str(), |
| 1007 |
SkUnicode::BreakType::kLines); |
| 1008 |
if (!lineBreakIterator) { |
| 1009 |
return; |
| 1010 |
} |
| 1011 |
graphemeBreakIterator = fUnicode->makeBreakIterator(currentLanguage.c_str(), |
| 1012 |
SkUnicode::BreakType::kGraphemes); |
| 1013 |
if (!graphemeBreakIterator) { |
| 1014 |
return; |
| 1015 |
} |
| 1016 |
needIteratorInit = false; |
| 1017 |
} |
| 1018 |
size_t utf8runLength = utf8End - utf8Start; |
| 1019 |
if (!lineBreakIterator->setText(utf8Start, utf8runLength)) { |
| 1020 |
return; |
| 1021 |
} |
| 1022 |
if (!graphemeBreakIterator->setText(utf8Start, utf8runLength)) { |
| 1023 |
return; |
| 1024 |
} |
| 1025 |
|
| 1026 |
uint32_t previousCluster = 0xFFFFFFFF; |
| 1027 |
for (size_t i = 0; i < run.fNumGlyphs; ++i) { |
| 1028 |
ShapedGlyph& glyph = run.fGlyphs[i]; |
| 1029 |
int32_t glyphCluster = glyph.fCluster; |
| 1030 |
|
| 1031 |
int32_t lineBreakIteratorCurrent = lineBreakIterator->current(); |
| 1032 |
while (!lineBreakIterator->isDone() && lineBreakIteratorCurrent < glyphCluster) |
| 1033 |
{ |
| 1034 |
lineBreakIteratorCurrent = lineBreakIterator->next(); |
| 1035 |
} |
| 1036 |
glyph.fMayLineBreakBefore = glyph.fCluster != previousCluster && |
| 1037 |
lineBreakIteratorCurrent == glyphCluster; |
| 1038 |
|
| 1039 |
int32_t graphemeBreakIteratorCurrent = graphemeBreakIterator->current(); |
| 1040 |
while (!graphemeBreakIterator->isDone() && graphemeBreakIteratorCurrent < glyphCluster) |
| 1041 |
{ |
| 1042 |
graphemeBreakIteratorCurrent = graphemeBreakIterator->next(); |
| 1043 |
} |
| 1044 |
glyph.fGraphemeBreakBefore = glyph.fCluster != previousCluster && |
| 1045 |
graphemeBreakIteratorCurrent == glyphCluster; |
| 1046 |
|
| 1047 |
previousCluster = glyph.fCluster; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
// Iterate over the glyphs in logical order to find potential line lengths. |
| 1053 |
{ |
| 1054 |
/** The position of the beginning of the line. */ |
| 1055 |
ShapedRunGlyphIterator beginning(runs); |
| 1056 |
|
| 1057 |
/** The position of the candidate line break. */ |
| 1058 |
ShapedRunGlyphIterator candidateLineBreak(runs); |
| 1059 |
SkScalar candidateLineBreakWidth = 0; |
| 1060 |
|
| 1061 |
/** The position of the candidate grapheme break. */ |
| 1062 |
ShapedRunGlyphIterator candidateGraphemeBreak(runs); |
| 1063 |
SkScalar candidateGraphemeBreakWidth = 0; |
| 1064 |
|
| 1065 |
/** The position of the current location. */ |
| 1066 |
ShapedRunGlyphIterator current(runs); |
| 1067 |
SkScalar currentWidth = 0; |
| 1068 |
while (ShapedGlyph* glyph = current.current()) { |
| 1069 |
// 'Break' at graphemes until a line boundary, then only at line boundaries. |
| 1070 |
// Only break at graphemes if no line boundary is valid. |
| 1071 |
if (current != beginning) { |
| 1072 |
if (glyph->fGraphemeBreakBefore || glyph->fMayLineBreakBefore) { |
| 1073 |
// TODO: preserve line breaks <= grapheme breaks |
| 1074 |
// and prevent line breaks inside graphemes |
| 1075 |
candidateGraphemeBreak = current; |
| 1076 |
candidateGraphemeBreakWidth = currentWidth; |
| 1077 |
if (glyph->fMayLineBreakBefore) { |
| 1078 |
candidateLineBreak = current; |
| 1079 |
candidateLineBreakWidth = currentWidth; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
SkScalar glyphWidth = glyph->fAdvance.fX; |
| 1085 |
// Break when overwidth, the glyph has a visual representation, and some space is used. |
| 1086 |
if (width < currentWidth + glyphWidth && glyph->fHasVisual && candidateGraphemeBreakWidth > 0){ |
| 1087 |
if (candidateLineBreak != beginning) { |
| 1088 |
beginning = candidateLineBreak; |
| 1089 |
currentWidth -= candidateLineBreakWidth; |
| 1090 |
candidateGraphemeBreakWidth -= candidateLineBreakWidth; |
| 1091 |
candidateLineBreakWidth = 0; |
| 1092 |
} else if (candidateGraphemeBreak != beginning) { |
| 1093 |
beginning = candidateGraphemeBreak; |
| 1094 |
candidateLineBreak = beginning; |
| 1095 |
currentWidth -= candidateGraphemeBreakWidth; |
| 1096 |
candidateGraphemeBreakWidth = 0; |
| 1097 |
candidateLineBreakWidth = 0; |
| 1098 |
} else { |
| 1099 |
SK_ABORT(""); |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (width < currentWidth) { |
| 1103 |
if (width < candidateGraphemeBreakWidth) { |
| 1104 |
candidateGraphemeBreak = candidateLineBreak; |
| 1105 |
candidateGraphemeBreakWidth = candidateLineBreakWidth; |
| 1106 |
} |
| 1107 |
current = candidateGraphemeBreak; |
| 1108 |
currentWidth = candidateGraphemeBreakWidth; |
| 1109 |
} |
| 1110 |
|
| 1111 |
glyph = beginning.current(); |
| 1112 |
if (glyph) { |
| 1113 |
glyph->fMustLineBreakBefore = true; |
| 1114 |
} |
| 1115 |
|
| 1116 |
} else { |
| 1117 |
current.next(); |
| 1118 |
currentWidth += glyphWidth; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
// Reorder the runs and glyphs per line and write them out. |
| 1124 |
{ |
| 1125 |
ShapedRunGlyphIterator previousBreak(runs); |
| 1126 |
ShapedRunGlyphIterator glyphIterator(runs); |
| 1127 |
int previousRunIndex = -1; |
| 1128 |
while (glyphIterator.current()) { |
| 1129 |
const ShapedRunGlyphIterator current = glyphIterator; |
| 1130 |
ShapedGlyph* nextGlyph = glyphIterator.next(); |
| 1131 |
|
| 1132 |
if (previousRunIndex != current.fRunIndex) { |
| 1133 |
SkFontMetrics metrics; |
| 1134 |
runs[current.fRunIndex].fFont.getMetrics(&metrics); |
| 1135 |
previousRunIndex = current.fRunIndex; |
| 1136 |
} |
| 1137 |
|
| 1138 |
// Nothing can be written until the baseline is known. |
| 1139 |
if (!(nextGlyph == nullptr || nextGlyph->fMustLineBreakBefore)) { |
| 1140 |
continue; |
| 1141 |
} |
| 1142 |
|
| 1143 |
int numRuns = current.fRunIndex - previousBreak.fRunIndex + 1; |
| 1144 |
AutoSTMalloc<4, SkBidiIterator::Level> runLevels(numRuns); |
| 1145 |
for (int i = 0; i < numRuns; ++i) { |
| 1146 |
runLevels[i] = runs[previousBreak.fRunIndex + i].fLevel; |
| 1147 |
} |
| 1148 |
AutoSTMalloc<4, int32_t> logicalFromVisual(numRuns); |
| 1149 |
fUnicode->reorderVisual(runLevels, numRuns, logicalFromVisual); |
| 1150 |
|
| 1151 |
// step through the runs in reverse visual order and the glyphs in reverse logical order |
| 1152 |
// until a visible glyph is found and force them to the end of the visual line. |
| 1153 |
|
| 1154 |
handler->beginLine(); |
| 1155 |
|
| 1156 |
struct SubRun { const ShapedRun& run; size_t startGlyphIndex; size_t endGlyphIndex; }; |
| 1157 |
auto makeSubRun = [&runs, &previousBreak, ¤t, &logicalFromVisual](size_t visualIndex){ |
| 1158 |
int logicalIndex = previousBreak.fRunIndex + logicalFromVisual[visualIndex]; |
| 1159 |
const auto& run = runs[logicalIndex]; |
| 1160 |
size_t startGlyphIndex = (logicalIndex == previousBreak.fRunIndex) |
| 1161 |
? previousBreak.fGlyphIndex |
| 1162 |
: 0; |
| 1163 |
size_t endGlyphIndex = (logicalIndex == current.fRunIndex) |
| 1164 |
? current.fGlyphIndex + 1 |
| 1165 |
: run.fNumGlyphs; |
| 1166 |
return SubRun{ run, startGlyphIndex, endGlyphIndex }; |
| 1167 |
}; |
| 1168 |
auto makeRunInfo = [](const SubRun& sub) { |
| 1169 |
uint32_t startUtf8 = sub.run.fGlyphs[sub.startGlyphIndex].fCluster; |
| 1170 |
uint32_t endUtf8 = (sub.endGlyphIndex < sub.run.fNumGlyphs) |
| 1171 |
? sub.run.fGlyphs[sub.endGlyphIndex].fCluster |
| 1172 |
: sub.run.fUtf8Range.end(); |
| 1173 |
|
| 1174 |
SkVector advance = SkVector::Make(0, 0); |
| 1175 |
for (size_t i = sub.startGlyphIndex; i < sub.endGlyphIndex; ++i) { |
| 1176 |
advance += sub.run.fGlyphs[i].fAdvance; |
| 1177 |
} |
| 1178 |
|
| 1179 |
return RunHandler::RunInfo{ |
| 1180 |
sub.run.fFont, |
| 1181 |
sub.run.fLevel, |
| 1182 |
sub.run.fScript, |
| 1183 |
sub.run.fLanguage, |
| 1184 |
advance, |
| 1185 |
sub.endGlyphIndex - sub.startGlyphIndex, |
| 1186 |
RunHandler::Range(startUtf8, endUtf8 - startUtf8) |
| 1187 |
}; |
| 1188 |
}; |
| 1189 |
|
| 1190 |
for (int i = 0; i < numRuns; ++i) { |
| 1191 |
handler->runInfo(makeRunInfo(makeSubRun(i))); |
| 1192 |
} |
| 1193 |
handler->commitRunInfo(); |
| 1194 |
for (int i = 0; i < numRuns; ++i) { |
| 1195 |
SubRun sub = makeSubRun(i); |
| 1196 |
append(handler, makeRunInfo(sub), sub.run, sub.startGlyphIndex, sub.endGlyphIndex); |
| 1197 |
} |
| 1198 |
|
| 1199 |
handler->commitLine(); |
| 1200 |
|
| 1201 |
previousRunIndex = -1; |
| 1202 |
previousBreak = glyphIterator; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
void ShapeDontWrapOrReorder::wrap(char const * const utf8, size_t utf8Bytes, |
| 1208 |
const BiDiRunIterator& bidi, |
| 1209 |
const LanguageRunIterator& language, |
| 1210 |
const ScriptRunIterator& script, |
| 1211 |
const FontRunIterator& font, |
| 1212 |
RunIteratorQueue& runSegmenter, |
| 1213 |
const Feature* features, size_t featuresSize, |
| 1214 |
SkScalar width, |
| 1215 |
RunHandler* handler) const |
| 1216 |
{ |
| 1217 |
sk_ignore_unused_variable(width); |
| 1218 |
TArray<ShapedRun> runs; |
| 1219 |
|
| 1220 |
const char* utf8Start = nullptr; |
| 1221 |
const char* utf8End = utf8; |
| 1222 |
while (runSegmenter.advanceRuns()) { |
| 1223 |
utf8Start = utf8End; |
| 1224 |
utf8End = utf8 + runSegmenter.endOfCurrentRun(); |
| 1225 |
|
| 1226 |
runs.emplace_back(shape(utf8, utf8Bytes, |
| 1227 |
utf8Start, utf8End, |
| 1228 |
bidi, language, script, font, |
| 1229 |
features, featuresSize)); |
| 1230 |
} |
| 1231 |
|
| 1232 |
handler->beginLine(); |
| 1233 |
for (const auto& run : runs) { |
| 1234 |
const RunHandler::RunInfo info = { |
| 1235 |
run.fFont, |
| 1236 |
run.fLevel, |
| 1237 |
run.fScript, |
| 1238 |
run.fLanguage, |
| 1239 |
run.fAdvance, |
| 1240 |
run.fNumGlyphs, |
| 1241 |
run.fUtf8Range |
| 1242 |
}; |
| 1243 |
handler->runInfo(info); |
| 1244 |
} |
| 1245 |
handler->commitRunInfo(); |
| 1246 |
for (const auto& run : runs) { |
| 1247 |
const RunHandler::RunInfo info = { |
| 1248 |
run.fFont, |
| 1249 |
run.fLevel, |
| 1250 |
run.fScript, |
| 1251 |
run.fLanguage, |
| 1252 |
run.fAdvance, |
| 1253 |
run.fNumGlyphs, |
| 1254 |
run.fUtf8Range |
| 1255 |
}; |
| 1256 |
append(handler, info, run, 0, run.fNumGlyphs); |
| 1257 |
} |
| 1258 |
handler->commitLine(); |
| 1259 |
} |
| 1260 |
|
| 1261 |
class HBLockedFaceCache { |
| 1262 |
public: |
| 1263 |
HBLockedFaceCache(SkLRUCache<SkTypefaceID, HBFont>& lruCache, SkMutex& mutex) |
| 1264 |
: fLRUCache(lruCache), fMutex(mutex) |
| 1265 |
{ |
| 1266 |
fMutex.acquire(); |
| 1267 |
} |
| 1268 |
HBLockedFaceCache(const HBLockedFaceCache&) = delete; |
| 1269 |
HBLockedFaceCache& operator=(const HBLockedFaceCache&) = delete; |
| 1270 |
HBLockedFaceCache& operator=(HBLockedFaceCache&&) = delete; |
| 1271 |
|
| 1272 |
~HBLockedFaceCache() { |
| 1273 |
fMutex.release(); |
| 1274 |
} |
| 1275 |
|
| 1276 |
HBFont* find(SkTypefaceID fontId) { |
| 1277 |
return fLRUCache.find(fontId); |
| 1278 |
} |
| 1279 |
HBFont* insert(SkTypefaceID fontId, HBFont hbFont) { |
| 1280 |
return fLRUCache.insert(fontId, std::move(hbFont)); |
| 1281 |
} |
| 1282 |
void reset() { |
| 1283 |
fLRUCache.reset(); |
| 1284 |
} |
| 1285 |
private: |
| 1286 |
SkLRUCache<SkTypefaceID, HBFont>& fLRUCache; |
| 1287 |
SkMutex& fMutex; |
| 1288 |
}; |
| 1289 |
static HBLockedFaceCache get_hbFace_cache() { |
| 1290 |
static SkMutex gHBFaceCacheMutex; |
| 1291 |
static SkLRUCache<SkTypefaceID, HBFont> gHBFaceCache(100); |
| 1292 |
return HBLockedFaceCache(gHBFaceCache, gHBFaceCacheMutex); |
| 1293 |
} |
| 1294 |
|
| 1295 |
ShapedRun ShaperHarfBuzz::shape(char const * const utf8, |
| 1296 |
size_t const utf8Bytes, |
| 1297 |
char const * const utf8Start, |
| 1298 |
char const * const utf8End, |
| 1299 |
const BiDiRunIterator& bidi, |
| 1300 |
const LanguageRunIterator& language, |
| 1301 |
const ScriptRunIterator& script, |
| 1302 |
const FontRunIterator& font, |
| 1303 |
Feature const * const features, size_t const featuresSize) const |
| 1304 |
{ |
| 1305 |
size_t utf8runLength = utf8End - utf8Start; |
| 1306 |
ShapedRun run(RunHandler::Range(utf8Start - utf8, utf8runLength), |
| 1307 |
font.currentFont(), bidi.currentLevel(), |
| 1308 |
script.currentScript(), language.currentLanguage(), |
| 1309 |
nullptr, 0); |
| 1310 |
|
| 1311 |
hb_buffer_t* buffer = fBuffer.get(); |
| 1312 |
SkAutoTCallVProc<hb_buffer_t, hb_buffer_clear_contents> autoClearBuffer(buffer); |
| 1313 |
hb_buffer_set_content_type(buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); |
| 1314 |
hb_buffer_set_cluster_level(buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS); |
| 1315 |
|
| 1316 |
// Documentation for HB_BUFFER_FLAG_BOT/EOT at 763e5466c0a03a7c27020e1e2598e488612529a7. |
| 1317 |
// Currently BOT forces a dotted circle when first codepoint is a mark; EOT has no effect. |
| 1318 |
// Avoid adding dotted circle, re-evaluate if BOT/EOT change. See https://skbug.com/40040947. |
| 1319 |
// hb_buffer_set_flags(buffer, HB_BUFFER_FLAG_BOT | HB_BUFFER_FLAG_EOT); |
| 1320 |
|
| 1321 |
// Add precontext. |
| 1322 |
hb_buffer_add_utf8(buffer, utf8, utf8Start - utf8, utf8Start - utf8, 0); |
| 1323 |
|
| 1324 |
// Populate the hb_buffer directly with utf8 cluster indexes. |
| 1325 |
const char* utf8Current = utf8Start; |
| 1326 |
while (utf8Current < utf8End) { |
| 1327 |
unsigned int cluster = utf8Current - utf8; |
| 1328 |
hb_codepoint_t u = utf8_next(&utf8Current, utf8End); |
| 1329 |
hb_buffer_add(buffer, u, cluster); |
| 1330 |
} |
| 1331 |
|
| 1332 |
// Add postcontext. |
| 1333 |
hb_buffer_add_utf8(buffer, utf8Current, utf8 + utf8Bytes - utf8Current, 0, 0); |
| 1334 |
|
| 1335 |
hb_direction_t direction = is_LTR(bidi.currentLevel()) ? HB_DIRECTION_LTR:HB_DIRECTION_RTL; |
| 1336 |
hb_buffer_set_direction(buffer, direction); |
| 1337 |
hb_buffer_set_script(buffer, hb_script_from_iso15924_tag((hb_tag_t)script.currentScript())); |
| 1338 |
// Buffers with HB_LANGUAGE_INVALID race since hb_language_get_default is not thread safe. |
| 1339 |
// The user must provide a language, but may provide data hb_language_from_string cannot use. |
| 1340 |
// Use "und" for the undefined language in this case (RFC5646 4.1 5). |
| 1341 |
hb_language_t hbLanguage = hb_language_from_string(language.currentLanguage(), -1); |
| 1342 |
if (hbLanguage == HB_LANGUAGE_INVALID) { |
| 1343 |
hbLanguage = fUndefinedLanguage; |
| 1344 |
} |
| 1345 |
hb_buffer_set_language(buffer, hbLanguage); |
| 1346 |
hb_buffer_guess_segment_properties(buffer); |
| 1347 |
|
| 1348 |
// TODO: better cache HBFace (data) / hbfont (typeface) |
| 1349 |
// An HBFace is expensive (it sanitizes the bits). |
| 1350 |
// An HBFont is fairly inexpensive. |
| 1351 |
// An HBFace is actually tied to the data, not the typeface. |
| 1352 |
// The size of 100 here is completely arbitrary and used to match libtxt. |
| 1353 |
HBFont hbFont; |
| 1354 |
{ |
| 1355 |
HBLockedFaceCache cache = get_hbFace_cache(); |
| 1356 |
SkTypefaceID dataId = font.currentFont().getTypeface()->uniqueID(); |
| 1357 |
HBFont* typefaceFontCached = cache.find(dataId); |
| 1358 |
if (!typefaceFontCached) { |
| 1359 |
HBFont typefaceFont(create_typeface_hb_font(*font.currentFont().getTypeface())); |
| 1360 |
typefaceFontCached = cache.insert(dataId, std::move(typefaceFont)); |
| 1361 |
} |
| 1362 |
hbFont = create_sub_hb_font(font.currentFont(), *typefaceFontCached); |
| 1363 |
} |
| 1364 |
if (!hbFont) { |
| 1365 |
return run; |
| 1366 |
} |
| 1367 |
|
| 1368 |
STArray<32, hb_feature_t> hbFeatures; |
| 1369 |
for (const auto& feature : SkSpan(features, featuresSize)) { |
| 1370 |
if (feature.end < SkTo<size_t>(utf8Start - utf8) || |
| 1371 |
SkTo<size_t>(utf8End - utf8) <= feature.start) |
| 1372 |
{ |
| 1373 |
continue; |
| 1374 |
} |
| 1375 |
if (feature.start <= SkTo<size_t>(utf8Start - utf8) && |
| 1376 |
SkTo<size_t>(utf8End - utf8) <= feature.end) |
| 1377 |
{ |
| 1378 |
hbFeatures.push_back({ (hb_tag_t)feature.tag, feature.value, |
| 1379 |
HB_FEATURE_GLOBAL_START, HB_FEATURE_GLOBAL_END}); |
| 1380 |
} else { |
| 1381 |
hbFeatures.push_back({ (hb_tag_t)feature.tag, feature.value, |
| 1382 |
SkTo<unsigned>(feature.start), SkTo<unsigned>(feature.end)}); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
hb_shape(hbFont.get(), buffer, hbFeatures.data(), hbFeatures.size()); |
| 1387 |
unsigned len = hb_buffer_get_length(buffer); |
| 1388 |
if (len == 0) { |
| 1389 |
return run; |
| 1390 |
} |
| 1391 |
|
| 1392 |
if (direction == HB_DIRECTION_RTL) { |
| 1393 |
// Put the clusters back in logical order. |
| 1394 |
// Note that the advances remain ltr. |
| 1395 |
hb_buffer_reverse(buffer); |
| 1396 |
} |
| 1397 |
hb_glyph_info_t* info = hb_buffer_get_glyph_infos(buffer, nullptr); |
| 1398 |
hb_glyph_position_t* pos = hb_buffer_get_glyph_positions(buffer, nullptr); |
| 1399 |
|
| 1400 |
run = ShapedRun(RunHandler::Range(utf8Start - utf8, utf8runLength), |
| 1401 |
font.currentFont(), bidi.currentLevel(), |
| 1402 |
script.currentScript(), language.currentLanguage(), |
| 1403 |
std::unique_ptr<ShapedGlyph[]>(new ShapedGlyph[len]), len); |
| 1404 |
|
| 1405 |
// Undo skhb_position with (1.0/(1<<16)) and scale as needed. |
| 1406 |
AutoSTArray<32, SkGlyphID> glyphIDs(len); |
| 1407 |
for (unsigned i = 0; i < len; i++) { |
| 1408 |
glyphIDs[i] = info[i].codepoint; |
| 1409 |
} |
| 1410 |
AutoSTArray<32, SkRect> glyphBounds(len); |
| 1411 |
SkPaint p; |
| 1412 |
run.fFont.getBounds(glyphIDs, glyphBounds, &p); |
| 1413 |
|
| 1414 |
double SkScalarFromHBPosX = +(1.52587890625e-5) * run.fFont.getScaleX(); |
| 1415 |
double SkScalarFromHBPosY = -(1.52587890625e-5); // HarfBuzz y-up, Skia y-down |
| 1416 |
SkVector runAdvance = { 0, 0 }; |
| 1417 |
for (unsigned i = 0; i < len; i++) { |
| 1418 |
ShapedGlyph& glyph = run.fGlyphs[i]; |
| 1419 |
glyph.fID = info[i].codepoint; |
| 1420 |
glyph.fCluster = info[i].cluster; |
| 1421 |
glyph.fOffset.fX = pos[i].x_offset * SkScalarFromHBPosX; |
| 1422 |
glyph.fOffset.fY = pos[i].y_offset * SkScalarFromHBPosY; |
| 1423 |
glyph.fAdvance.fX = pos[i].x_advance * SkScalarFromHBPosX; |
| 1424 |
glyph.fAdvance.fY = pos[i].y_advance * SkScalarFromHBPosY; |
| 1425 |
|
| 1426 |
glyph.fHasVisual = !glyphBounds[i].isEmpty(); //!font->currentTypeface()->glyphBoundsAreZero(glyph.fID); |
| 1427 |
#if SK_HB_VERSION_CHECK(1, 5, 0) |
| 1428 |
glyph.fUnsafeToBreak = info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK; |
| 1429 |
#else |
| 1430 |
glyph.fUnsafeToBreak = false; |
| 1431 |
#endif |
| 1432 |
glyph.fMustLineBreakBefore = false; |
| 1433 |
|
| 1434 |
runAdvance += glyph.fAdvance; |
| 1435 |
} |
| 1436 |
run.fAdvance = runAdvance; |
| 1437 |
|
| 1438 |
return run; |
| 1439 |
} |
| 1440 |
} // namespace |
| 1441 |
|
| 1442 |
#if !defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 1443 |
|
| 1444 |
#if defined(SK_UNICODE_ICU_IMPLEMENTATION) |
| 1445 |
#include "modules/skunicode/include/SkUnicode_icu.h" |
| 1446 |
#endif |
| 1447 |
|
| 1448 |
#if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION) |
| 1449 |
#include "modules/skunicode/include/SkUnicode_libgrapheme.h" |
| 1450 |
#endif |
| 1451 |
|
| 1452 |
#if defined(SK_UNICODE_ICU4X_IMPLEMENTATION) |
| 1453 |
#include "modules/skunicode/include/SkUnicode_icu4x.h" |
| 1454 |
#endif |
| 1455 |
|
| 1456 |
static sk_sp<SkUnicode> get_unicode() { |
| 1457 |
#if defined(SK_UNICODE_ICU_IMPLEMENTATION) |
| 1458 |
if (auto unicode = SkUnicodes::ICU::Make()) { |
| 1459 |
return unicode; |
| 1460 |
} |
| 1461 |
#endif // defined(SK_UNICODE_ICU_IMPLEMENTATION) |
| 1462 |
#if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION) |
| 1463 |
if (auto unicode = SkUnicodes::Libgrapheme::Make()) { |
| 1464 |
return unicode; |
| 1465 |
} |
| 1466 |
#endif |
| 1467 |
#if defined(SK_UNICODE_ICU4X_IMPLEMENTATION) |
| 1468 |
if (auto unicode = SkUnicodes::ICU4X::Make()) { |
| 1469 |
return unicode; |
| 1470 |
} |
| 1471 |
#endif |
| 1472 |
return nullptr; |
| 1473 |
} |
| 1474 |
|
| 1475 |
std::unique_ptr<SkShaper::ScriptRunIterator> |
| 1476 |
SkShaper::MakeHbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes) { |
| 1477 |
return SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes); |
| 1478 |
} |
| 1479 |
|
| 1480 |
std::unique_ptr<SkShaper::ScriptRunIterator> |
| 1481 |
SkShaper::MakeSkUnicodeHbScriptRunIterator(const char* utf8, size_t utf8Bytes) { |
| 1482 |
return SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes); |
| 1483 |
} |
| 1484 |
|
| 1485 |
std::unique_ptr<SkShaper::ScriptRunIterator> SkShaper::MakeSkUnicodeHbScriptRunIterator( |
| 1486 |
const char* utf8, size_t utf8Bytes, SkFourByteTag script) { |
| 1487 |
return SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes, script); |
| 1488 |
} |
| 1489 |
|
| 1490 |
std::unique_ptr<SkShaper> SkShaper::MakeShaperDrivenWrapper(sk_sp<SkFontMgr> fontmgr) { |
| 1491 |
return SkShapers::HB::ShaperDrivenWrapper(get_unicode(), fontmgr); |
| 1492 |
} |
| 1493 |
|
| 1494 |
std::unique_ptr<SkShaper> SkShaper::MakeShapeThenWrap(sk_sp<SkFontMgr> fontmgr) { |
| 1495 |
return SkShapers::HB::ShapeThenWrap(get_unicode(), fontmgr); |
| 1496 |
} |
| 1497 |
|
| 1498 |
void SkShaper::PurgeHarfBuzzCache() { SkShapers::HB::PurgeCaches(); } |
| 1499 |
#endif // !defined(SK_DISABLE_LEGACY_SKSHAPER_FUNCTIONS) |
| 1500 |
|
| 1501 |
namespace SkShapers::HB { |
| 1502 |
std::unique_ptr<SkShaper> ShaperDrivenWrapper(sk_sp<SkUnicode> unicode, |
| 1503 |
sk_sp<SkFontMgr> fallback) { |
| 1504 |
if (!unicode) { |
| 1505 |
return nullptr; |
| 1506 |
} |
| 1507 |
HBBuffer buffer(hb_buffer_create()); |
| 1508 |
if (!buffer) { |
| 1509 |
SkDEBUGF("Could not create hb_buffer"); |
| 1510 |
return nullptr; |
| 1511 |
} |
| 1512 |
return std::make_unique<::ShaperDrivenWrapper>( |
| 1513 |
unicode, std::move(buffer), std::move(fallback)); |
| 1514 |
} |
| 1515 |
|
| 1516 |
std::unique_ptr<SkShaper> ShapeThenWrap(sk_sp<SkUnicode> unicode, |
| 1517 |
sk_sp<SkFontMgr> fallback) { |
| 1518 |
if (!unicode) { |
| 1519 |
return nullptr; |
| 1520 |
} |
| 1521 |
HBBuffer buffer(hb_buffer_create()); |
| 1522 |
if (!buffer) { |
| 1523 |
SkDEBUGF("Could not create hb_buffer"); |
| 1524 |
return nullptr; |
| 1525 |
} |
| 1526 |
return std::make_unique<::ShapeThenWrap>( |
| 1527 |
unicode, std::move(buffer), std::move(fallback)); |
| 1528 |
} |
| 1529 |
|
| 1530 |
std::unique_ptr<SkShaper> ShapeDontWrapOrReorder(sk_sp<SkUnicode> unicode, |
| 1531 |
sk_sp<SkFontMgr> fallback) { |
| 1532 |
if (!unicode) { |
| 1533 |
return nullptr; |
| 1534 |
} |
| 1535 |
HBBuffer buffer(hb_buffer_create()); |
| 1536 |
if (!buffer) { |
| 1537 |
SkDEBUGF("Could not create hb_buffer"); |
| 1538 |
return nullptr; |
| 1539 |
} |
| 1540 |
return std::make_unique<::ShapeDontWrapOrReorder>( |
| 1541 |
unicode, std::move(buffer), std::move(fallback)); |
| 1542 |
} |
| 1543 |
|
| 1544 |
std::unique_ptr<SkShaper::ScriptRunIterator> ScriptRunIterator(const char* utf8, size_t utf8Bytes) { |
| 1545 |
return std::make_unique<SkUnicodeHbScriptRunIterator>(utf8, utf8Bytes, HB_SCRIPT_UNKNOWN); |
| 1546 |
} |
| 1547 |
std::unique_ptr<SkShaper::ScriptRunIterator> ScriptRunIterator(const char* utf8, |
| 1548 |
size_t utf8Bytes, |
| 1549 |
SkFourByteTag script) { |
| 1550 |
return std::make_unique<SkUnicodeHbScriptRunIterator>( |
| 1551 |
utf8, utf8Bytes, hb_script_from_iso15924_tag((hb_tag_t)script)); |
| 1552 |
} |
| 1553 |
|
| 1554 |
void PurgeCaches() { |
| 1555 |
HBLockedFaceCache cache = get_hbFace_cache(); |
| 1556 |
cache.reset(); |
| 1557 |
} |
| 1558 |
} // namespace SkShapers::HB |