1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
|
/*
* Copyright (c) 2012 Google Inc. All rights reserved.
* Copyright (C) 2013 BlackBerry Limited. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_shaper.h"
#include <hb.h>
#include <unicode/uchar.h>
#include <unicode/uscript.h>
#include <algorithm>
#include <hb-cplusplus.hh>
#include <memory>
#include <utility>
#include "base/check_op.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/fonts/font_description.h"
#include "third_party/blink/renderer/platform/fonts/font_fallback_iterator.h"
#include "third_party/blink/renderer/platform/fonts/font_fallback_priority.h"
#include "third_party/blink/renderer/platform/fonts/font_variant_emoji.h"
#include "third_party/blink/renderer/platform/fonts/opentype/open_type_caps_support.h"
#include "third_party/blink/renderer/platform/fonts/shaping/case_mapping_harfbuzz_buffer_filler.h"
#include "third_party/blink/renderer/platform/fonts/shaping/font_features.h"
#include "third_party/blink/renderer/platform/fonts/shaping/han_kerning.h"
#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_run.h"
#include "third_party/blink/renderer/platform/fonts/small_caps_iterator.h"
#include "third_party/blink/renderer/platform/fonts/utf16_text_iterator.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/text/text_break_iterator.h"
#include "third_party/blink/renderer/platform/wtf/deque.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/unicode.h"
namespace blink {
namespace {
//
// This class holds an `hb_buffer_t`.
//
// To reduce constructions and destructions of `hb_buffer_t`, it returns the
// `hb_buffer_t` instance to its internal pool on the destruction. The pooled
// instances will be `hb_buffer_reset`ed and reused on future constructions.
//
class PooledHarfBuzzBuffer {
public:
PooledHarfBuzzBuffer() {
if (RuntimeEnabledFeatures::HarfBuzzBufferPoolEnabled()) {
Pool& pool = GetPool();
if (!pool.empty()) {
buffer_ = std::move(pool.back());
#if EXPENSIVE_DCHECKS_ARE_ON()
DCHECK(buffer_);
DCHECK(!pool.back());
#endif // EXPENSIVE_DCHECKS_ARE_ON()
pool.pop_back();
hb_buffer_reset(buffer_);
return;
}
}
buffer_ = hb::unique_ptr<hb_buffer_t>{hb_buffer_create()};
}
~PooledHarfBuzzBuffer() {
if (RuntimeEnabledFeatures::HarfBuzzBufferPoolEnabled()) {
Pool& pool = GetPool();
pool.push_back(std::move(buffer_));
#if EXPENSIVE_DCHECKS_ARE_ON()
DCHECK_LE(pool.size(), kInlineCapacity);
DCHECK(!buffer_);
#endif // EXPENSIVE_DCHECKS_ARE_ON()
}
}
hb_buffer_t* Get() const { return buffer_; }
const hb_buffer_t* operator->() const { return Get(); }
explicit operator hb_buffer_t*() const { return Get(); }
explicit operator bool() const { return Get(); }
private:
static constexpr wtf_size_t kInlineCapacity = 2;
using Pool = Vector<hb::unique_ptr<hb_buffer_t>, kInlineCapacity>;
static Pool& GetPool() {
DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<Pool>, pool, ());
return *pool;
}
hb::unique_ptr<hb_buffer_t> buffer_;
};
#if EXPENSIVE_DCHECKS_ARE_ON()
// Check if the ShapeResult has the specified range.
// |text| and |font| are only for logging.
void CheckShapeResultRange(const ShapeResult* result,
unsigned start,
unsigned end,
const String& text,
const Font* font) {
if (!result) {
return;
}
DCHECK_LE(start, end);
unsigned length = end - start;
if (length == result->NumCharacters() &&
(!length ||
(start == result->StartIndex() && end == result->EndIndex()))) {
return;
}
// Log font-family/size as specified.
StringBuilder log;
log.Append("Font='");
const FontDescription& font_description = font->GetFontDescription();
log.Append(font_description.Family().ToString());
log.AppendFormat("', %f", font_description.ComputedSize());
// Log the primary font with its family name in the font file.
const SimpleFontData* font_data = font->PrimaryFont();
if (font_data) {
const SkTypeface* typeface = font_data->PlatformData().Typeface();
SkString family_name;
typeface->getFamilyName(&family_name);
log.Append(", primary=");
log.Append(family_name.c_str());
}
// Log the text to shape.
log.AppendFormat(": %u-%u -> %u-%u:", start, end, result->StartIndex(),
result->EndIndex());
for (unsigned i = start; i < end; ++i) {
log.AppendFormat(" %02X", text[i]);
}
log.Append(", result=");
result->ToString(&log);
NOTREACHED() << log.ToString();
}
#endif
struct TrackEmoji {
bool is_start;
unsigned tracked_cluster_index;
bool cluster_broken;
unsigned num_broken_clusters;
unsigned num_clusters;
};
// The algorithm is relying on the following assumption: If an emoji is shaped
// correctly it will present as only one glyph. This definitely holds for
// NotoColorEmoji. So if one sequence (which HarfBuzz groups as a cluster)
// presents as multiple glyphs, it means an emoji is rendered as sequence that
// the font did not understand and did not shape into only one glyph. If it
// renders as only one glyph but that glyph is .notdef/Tofu, it also means it's
// broken. Due to the way flags work (pairs of regional indicators), broken
// flags cannot be correctly identified with this method - as each regional
// indicator will display as one emoji with Noto Color Emoji.
void IdentifyBrokenEmoji(void* context,
unsigned character_index,
Glyph glyph,
gfx::Vector2dF,
float,
bool,
CanvasRotationInVertical,
const SimpleFontData*) {
DCHECK(context);
TrackEmoji* track_emoji = reinterpret_cast<TrackEmoji*>(context);
if (character_index != track_emoji->tracked_cluster_index ||
track_emoji->is_start) {
// We have reached the next cluster and can decide for the previous cluster
// whether it was broken or not.
track_emoji->num_clusters++;
track_emoji->is_start = false;
track_emoji->tracked_cluster_index = character_index;
if (track_emoji->cluster_broken) {
track_emoji->num_broken_clusters++;
}
track_emoji->cluster_broken = glyph == 0;
} else {
// We have reached an additional glyph for the same cluster, which means the
// sequence was not identified by the font and is showing as multiple
// glyphs.
track_emoji->cluster_broken = true;
}
}
struct EmojiCorrectness {
unsigned num_clusters = 0;
unsigned num_broken_clusters = 0;
};
EmojiCorrectness ComputeBrokenEmojiPercentage(ShapeResult* shape_result,
unsigned start_index,
unsigned end_index) {
TrackEmoji track_emoji = {true, 0, false, 0, 0};
shape_result->ForEachGlyph(0.f, start_index, end_index, 0 /* index_offset */,
IdentifyBrokenEmoji, &track_emoji);
track_emoji.num_broken_clusters += track_emoji.cluster_broken ? 1 : 0;
return {track_emoji.num_clusters, track_emoji.num_broken_clusters};
}
FontFallbackPriority ApplyFontVariantEmojiOnFallbackPriority(
FontFallbackPriority curr_font_fallback_priority,
FontVariantEmoji font_variant_emoji) {
// font-variant-emoji property should not override emoji variation selectors,
// see https://www.w3.org/TR/css-fonts-4/#font-variant-emoji-prop.
if (!HasVSFallbackPriority(curr_font_fallback_priority)) {
if (font_variant_emoji == kEmojiVariantEmoji) {
return FontFallbackPriority::kEmojiEmoji;
}
if (font_variant_emoji == kTextVariantEmoji) {
return FontFallbackPriority::kText;
}
}
return curr_font_fallback_priority;
}
} // namespace
enum ReshapeQueueItemAction {
kReshapeQueueNextFont,
kReshapeQueueRange,
kReshapeQueueReset
};
struct ReshapeQueueItem {
DISALLOW_NEW();
ReshapeQueueItemAction action_;
unsigned start_index_;
unsigned num_characters_;
ReshapeQueueItem(ReshapeQueueItemAction action, unsigned start, unsigned num)
: action_(action), start_index_(start), num_characters_(num) {}
};
//
// Represents a context while shaping a range.
//
// Input-only data and objects whose pointers don't change are marked as
// `const`.
//
struct RangeContext {
STACK_ALLOCATED();
public:
RangeContext(const Font* font,
TextDirection direction,
unsigned start,
unsigned end,
ShapeOptions options = ShapeOptions())
: font(font),
text_direction(direction),
start(start),
end(end),
font_features(font->GetFontFeatures()),
options(options) {
DCHECK_GE(end, start);
}
const Font* const font;
const TextDirection text_direction;
const unsigned start;
const unsigned end;
const PooledHarfBuzzBuffer buffer;
FontFeatures font_features;
Deque<ReshapeQueueItem> reshape_queue;
const ShapeOptions options;
hb_direction_t HarfBuzzDirection(CanvasRotationInVertical canvas_rotation) {
FontOrientation orientation = font->GetFontDescription().Orientation();
hb_direction_t direction =
IsVerticalAnyUpright(orientation) &&
IsCanvasRotationInVerticalUpright(canvas_rotation)
? HB_DIRECTION_TTB
: HB_DIRECTION_LTR;
return text_direction == TextDirection::kRtl
? HB_DIRECTION_REVERSE(direction)
: direction;
}
};
struct BufferSlice {
unsigned start_character_index;
unsigned num_characters;
unsigned start_glyph_index;
unsigned num_glyphs;
};
namespace {
// A port of hb_icu_script_to_script because harfbuzz on CrOS is built
// without hb-icu. See http://crbug.com/356929
static inline hb_script_t ICUScriptToHBScript(UScriptCode script) {
if (script == USCRIPT_INVALID_CODE) [[unlikely]] {
return HB_SCRIPT_INVALID;
}
return hb_script_from_string(uscript_getShortName(script), -1);
}
inline float HarfBuzzPositionToFloat(hb_position_t value) {
return static_cast<float>(value) / (1 << 16);
}
void RoundHarfBuzzPosition(hb_position_t* value) {
if ((*value) & 0xFFFF) {
// There is a non-zero fractional part in the 16.16 value.
*value = static_cast<hb_position_t>(
round(static_cast<float>(*value) / (1 << 16)))
<< 16;
}
}
void RoundHarfBuzzBufferPositions(hb_buffer_t* buffer) {
unsigned int len;
hb_glyph_position_t* glyph_positions =
hb_buffer_get_glyph_positions(buffer, &len);
for (unsigned int i = 0; i < len; i++) {
hb_glyph_position_t* pos = &UNSAFE_TODO(glyph_positions[i]);
RoundHarfBuzzPosition(&pos->x_offset);
RoundHarfBuzzPosition(&pos->y_offset);
RoundHarfBuzzPosition(&pos->x_advance);
RoundHarfBuzzPosition(&pos->y_advance);
}
}
inline bool ShapeRange(hb_buffer_t* buffer,
const FontFeatures& font_features,
const SimpleFontData* current_font,
const UnicodeRangeSet* current_font_range_set,
UScriptCode current_run_script,
hb_direction_t direction,
hb_language_t language,
float specified_size) {
const FontPlatformData& platform_data = current_font->PlatformData();
HarfBuzzFace* face = platform_data.GetHarfBuzzFace();
if (!face) {
DLOG(ERROR) << "Could not create HarfBuzzFace from FontPlatformData.";
return false;
}
std::optional<FontFeatures> variant_features;
if (const ResolvedFontFeatures& resolved_features =
platform_data.ResolvedFeatures();
!resolved_features.empty()) {
// Insert `resolved_features` before `font_features`.
variant_features.emplace();
variant_features->reserve(resolved_features.size() + font_features.size());
for (const FontFeatureValue& feature : resolved_features) {
variant_features->push_back(FontFeatureRange{feature});
}
variant_features->AppendVector(font_features);
}
const FontFeatures& argument_features =
variant_features ? *variant_features : font_features;
hb_buffer_set_language(buffer, language);
hb_buffer_set_script(buffer, ICUScriptToHBScript(current_run_script));
hb_buffer_set_direction(buffer, direction);
hb_font_t* hb_font =
face->GetScaledFont(current_font_range_set,
HB_DIRECTION_IS_VERTICAL(direction)
? HarfBuzzFace::kPrepareForVerticalLayout
: HarfBuzzFace::kNoVerticalLayout,
specified_size);
hb_shape(hb_font, buffer,
FontFeatureRange::ToHarfBuzzData(argument_features.data()),
argument_features.size());
if (!face->ShouldSubpixelPosition()) {
RoundHarfBuzzBufferPositions(buffer);
}
return true;
}
BufferSlice ComputeSlice(RangeContext* range_data,
const ReshapeQueueItem& current_queue_item,
const hb_glyph_info_t* glyph_info,
unsigned num_glyphs,
unsigned old_glyph_index,
unsigned new_glyph_index) {
// Compute the range indices of consecutive shaped or .notdef glyphs.
// Cluster information for RTL runs becomes reversed, e.g. glyph 0
// has cluster index 5 in a run of 6 characters.
BufferSlice result;
result.start_glyph_index = old_glyph_index;
result.num_glyphs = new_glyph_index - old_glyph_index;
if (HB_DIRECTION_IS_FORWARD(
hb_buffer_get_direction(range_data->buffer.Get()))) {
result.start_character_index =
UNSAFE_TODO(glyph_info[old_glyph_index].cluster);
if (new_glyph_index == num_glyphs) {
// Clamp the end offsets of the queue item to the offsets representing
// the shaping window.
unsigned shape_end =
std::min(range_data->end, current_queue_item.start_index_ +
current_queue_item.num_characters_);
result.num_characters = shape_end - result.start_character_index;
} else {
result.num_characters = UNSAFE_TODO(glyph_info[new_glyph_index].cluster -
result.start_character_index);
}
} else {
// Direction Backwards
result.start_character_index =
UNSAFE_TODO(glyph_info[new_glyph_index - 1].cluster);
if (old_glyph_index == 0) {
// Clamp the end offsets of the queue item to the offsets representing
// the shaping window.
unsigned shape_end =
std::min(range_data->end, current_queue_item.start_index_ +
current_queue_item.num_characters_);
result.num_characters = shape_end - result.start_character_index;
} else {
result.num_characters =
UNSAFE_TODO(glyph_info[old_glyph_index - 1].cluster -
glyph_info[new_glyph_index - 1].cluster);
}
}
return result;
}
inline bool IsLastFontToShape(
HarfBuzzShaper::FallbackFontStage fallback_stage) {
return fallback_stage == HarfBuzzShaper::kLast ||
fallback_stage == HarfBuzzShaper::kLastIgnoreVS;
}
inline bool StageNeedsQueueReset(
HarfBuzzShaper::FallbackFontStage fallback_stage) {
return fallback_stage == HarfBuzzShaper::kLastWithVS;
}
HarfBuzzShaper::FallbackFontStage ChangeStageToLast(
HarfBuzzShaper::FallbackFontStage fallback_stage) {
switch (fallback_stage) {
case HarfBuzzShaper::kIntermediate:
return HarfBuzzShaper::kLast;
case HarfBuzzShaper::kIntermediateWithVS:
return HarfBuzzShaper::kLastWithVS;
case HarfBuzzShaper::kIntermediateIgnoreVS:
return HarfBuzzShaper::kLastIgnoreVS;
default:
return fallback_stage;
}
}
HarfBuzzShaper::FallbackFontStage ChangeStageToVS(
HarfBuzzShaper::FallbackFontStage fallback_stage) {
switch (fallback_stage) {
case HarfBuzzShaper::kIntermediate:
return HarfBuzzShaper::kIntermediateWithVS;
case HarfBuzzShaper::kLast:
return HarfBuzzShaper::kLastWithVS;
case HarfBuzzShaper::kIntermediateWithVS:
case HarfBuzzShaper::kLastWithVS:
return fallback_stage;
default:
// We should not call this function on the second fallback pass.
NOTREACHED();
}
}
void QueueCharacters(RangeContext* range_data,
const SimpleFontData* current_font,
bool& font_cycle_queued,
const BufferSlice& slice,
HarfBuzzShaper::FallbackFontStage font_stage) {
if (!font_cycle_queued) {
if (StageNeedsQueueReset(font_stage)) {
range_data->reshape_queue.push_back(
ReshapeQueueItem(kReshapeQueueReset, 0, 0));
} else {
range_data->reshape_queue.push_back(
ReshapeQueueItem(kReshapeQueueNextFont, 0, 0));
}
font_cycle_queued = true;
}
DCHECK(slice.num_characters);
range_data->reshape_queue.push_back(ReshapeQueueItem(
kReshapeQueueRange, slice.start_character_index, slice.num_characters));
}
CanvasRotationInVertical CanvasRotationForRun(
FontOrientation font_orientation,
OrientationIterator::RenderOrientation render_orientation,
const FontDescription& font_description) {
if (font_orientation == FontOrientation::kVerticalUpright) {
return font_description.IsSyntheticOblique()
? CanvasRotationInVertical::kRotateCanvasUprightOblique
: CanvasRotationInVertical::kRotateCanvasUpright;
}
if (font_orientation == FontOrientation::kVerticalMixed) {
if (render_orientation == OrientationIterator::kOrientationKeep) {
return font_description.IsSyntheticOblique()
? CanvasRotationInVertical::kRotateCanvasUprightOblique
: CanvasRotationInVertical::kRotateCanvasUpright;
}
return font_description.IsSyntheticOblique()
? CanvasRotationInVertical::kOblique
: CanvasRotationInVertical::kRegular;
}
return CanvasRotationInVertical::kRegular;
}
} // namespace
inline void HarfBuzzShaper::CheckTextLen(unsigned start,
unsigned length) const {
CHECK_LE(start, text_.length());
CHECK_LE(length, text_.length() - start);
}
inline void HarfBuzzShaper::CheckTextEnd(unsigned start, unsigned end) const {
CHECK_LE(start, end);
CHECK_LE(start, text_.length());
CHECK_LE(end, text_.length());
}
void HarfBuzzShaper::CommitGlyphs(RangeContext* range_data,
const SimpleFontData* current_font,
UScriptCode current_run_script,
CanvasRotationInVertical canvas_rotation,
FallbackFontStage fallback_stage,
const BufferSlice& slice,
ShapeResult* shape_result) const {
hb_direction_t direction = range_data->HarfBuzzDirection(canvas_rotation);
hb_script_t script = ICUScriptToHBScript(current_run_script);
// Here we need to specify glyph positions.
BufferSlice next_slice;
unsigned run_start_index = slice.start_character_index;
for (const BufferSlice* current_slice = &slice;;) {
auto* run = MakeGarbageCollected<ShapeResultRun>(
current_font, direction, canvas_rotation, script, run_start_index,
current_slice->num_glyphs, current_slice->num_characters);
unsigned next_start_glyph;
shape_result->InsertRun(run, current_slice->start_glyph_index,
current_slice->num_glyphs, &next_start_glyph,
range_data->buffer.Get());
DCHECK_GE(current_slice->start_glyph_index + current_slice->num_glyphs,
next_start_glyph);
unsigned next_num_glyphs =
current_slice->num_glyphs -
(next_start_glyph - current_slice->start_glyph_index);
if (!next_num_glyphs) {
break;
}
// If the slice exceeds the limit a RunInfo can store, create another
// RunInfo for the rest of the slice.
DCHECK_GT(current_slice->num_characters, run->num_characters_);
next_slice = {current_slice->start_character_index + run->num_characters_,
current_slice->num_characters - run->num_characters_,
next_start_glyph, next_num_glyphs};
current_slice = &next_slice;
// The |InsertRun| has truncated the right end. In LTR, advance the
// |run_start_index| because the end characters are truncated. In RTL, keep
// the same |run_start_index| because the start characters are truncated.
if (HB_DIRECTION_IS_FORWARD(direction)) {
run_start_index = next_slice.start_character_index;
}
}
if (IsLastFontToShape(fallback_stage)) {
range_data->font->ReportNotDefGlyph();
}
}
void HarfBuzzShaper::ExtractShapeResults(
RangeContext* range_data,
bool& font_cycle_queued,
const ReshapeQueueItem& current_queue_item,
const SimpleFontData* current_font,
UScriptCode current_run_script,
CanvasRotationInVertical canvas_rotation,
FallbackFontStage& fallback_stage,
ShapeResult* shape_result) const {
enum ClusterResult { kShaped, kNotDef, kUnknown };
ClusterResult current_cluster_result = kUnknown;
ClusterResult previous_cluster_result = kUnknown;
unsigned previous_cluster = 0;
unsigned current_cluster = 0;
// Find first notdef glyph in buffer.
unsigned num_glyphs = hb_buffer_get_length(range_data->buffer.Get());
hb_glyph_info_t* glyph_info =
hb_buffer_get_glyph_infos(range_data->buffer.Get(), nullptr);
unsigned last_change_glyph_index = 0;
unsigned previous_cluster_start_glyph_index = 0;
if (!num_glyphs) {
return;
}
const Glyph space_glyph = current_font->SpaceGlyph();
for (unsigned glyph_index = 0; glyph_index < num_glyphs; ++glyph_index) {
// We proceed by full clusters and determine a shaping result - either
// kShaped or kNotDef for each cluster.
const hb_glyph_info_t& glyph = UNSAFE_TODO(glyph_info[glyph_index]);
previous_cluster = current_cluster;
current_cluster = glyph.cluster;
const hb_codepoint_t glyph_id = glyph.codepoint;
ClusterResult glyph_result;
if (glyph_id == 0) {
// Glyph 0 must be assigned to a .notdef glyph.
// https://docs.microsoft.com/en-us/typography/opentype/spec/recom#glyph-0-the-notdef-glyph
glyph_result = kNotDef;
} else if (glyph_id == space_glyph && !IsLastFontToShape(fallback_stage) &&
text_[current_cluster] == kIdeographicSpaceCharacter) {
// HarfBuzz synthesizes U+3000 IDEOGRAPHIC SPACE using the space glyph.
// This is not desired for run-splitting, applying features, and for
// computing `line-height`. crbug.com/1193282
// We revisit when HarfBuzz decides how to solve this more generally.
// https://github.com/harfbuzz/harfbuzz/issues/2889
glyph_result = kNotDef;
} else if (glyph_id == kUnmatchedVSGlyphId) {
fallback_stage = ChangeStageToVS(fallback_stage);
glyph_result = kNotDef;
} else {
glyph_result = kShaped;
}
if (current_cluster != previous_cluster) {
// We are transitioning to a new cluster (whose shaping result state we
// have not looked at yet). This means the cluster we just looked at is
// completely analysed and we can determine whether it was fully shaped
// and whether that means a state change to the cluster before that one.
if ((previous_cluster_result != current_cluster_result) &&
previous_cluster_result != kUnknown) {
BufferSlice slice = ComputeSlice(
range_data, current_queue_item, glyph_info, num_glyphs,
last_change_glyph_index, previous_cluster_start_glyph_index);
// If the most recent cluster is shaped and there is a state change,
// it means the previous ones were unshaped, so we queue them, unless
// we're using the last resort font.
if (current_cluster_result == kShaped &&
!IsLastFontToShape(fallback_stage)) {
QueueCharacters(range_data, current_font, font_cycle_queued, slice,
fallback_stage);
} else {
// If the most recent cluster is unshaped and there is a state
// change, it means the previous one(s) were shaped, so we commit
// the glyphs. We also commit when we've reached the last resort
// font.
CommitGlyphs(range_data, current_font, current_run_script,
canvas_rotation, fallback_stage, slice, shape_result);
}
last_change_glyph_index = previous_cluster_start_glyph_index;
}
// No state change happened, continue.
previous_cluster_result = current_cluster_result;
previous_cluster_start_glyph_index = glyph_index;
// Reset current cluster result.
current_cluster_result = glyph_result;
} else {
// Update and merge current cluster result.
current_cluster_result =
glyph_result == kShaped && (current_cluster_result == kShaped ||
current_cluster_result == kUnknown)
? kShaped
: kNotDef;
}
}
// End of the run.
if (current_cluster_result != previous_cluster_result &&
previous_cluster_result != kUnknown &&
!IsLastFontToShape(fallback_stage)) {
// The last cluster in the run still had shaping status different from
// the cluster(s) before it, we need to submit one shaped and one
// unshaped segment.
if (current_cluster_result == kShaped) {
BufferSlice slice = ComputeSlice(
range_data, current_queue_item, glyph_info, num_glyphs,
last_change_glyph_index, previous_cluster_start_glyph_index);
QueueCharacters(range_data, current_font, font_cycle_queued, slice,
fallback_stage);
slice =
ComputeSlice(range_data, current_queue_item, glyph_info, num_glyphs,
previous_cluster_start_glyph_index, num_glyphs);
CommitGlyphs(range_data, current_font, current_run_script,
canvas_rotation, fallback_stage, slice, shape_result);
} else {
BufferSlice slice = ComputeSlice(
range_data, current_queue_item, glyph_info, num_glyphs,
last_change_glyph_index, previous_cluster_start_glyph_index);
CommitGlyphs(range_data, current_font, current_run_script,
canvas_rotation, fallback_stage, slice, shape_result);
slice =
ComputeSlice(range_data, current_queue_item, glyph_info, num_glyphs,
previous_cluster_start_glyph_index, num_glyphs);
QueueCharacters(range_data, current_font, font_cycle_queued, slice,
fallback_stage);
}
} else {
// There hasn't been a state change for the last cluster, so we can just
// either commit or queue what we have up until here.
BufferSlice slice =
ComputeSlice(range_data, current_queue_item, glyph_info, num_glyphs,
last_change_glyph_index, num_glyphs);
if (current_cluster_result == kNotDef &&
!IsLastFontToShape(fallback_stage)) {
QueueCharacters(range_data, current_font, font_cycle_queued, slice,
fallback_stage);
} else {
CommitGlyphs(range_data, current_font, current_run_script,
canvas_rotation, fallback_stage, slice, shape_result);
}
}
}
bool HarfBuzzShaper::CollectFallbackHintChars(
const Deque<ReshapeQueueItem>& reshape_queue,
bool needs_hint_list,
HintCharList& hint) const {
if (reshape_queue.empty()) {
return false;
}
// Clear without releasing the capacity to avoid reallocations.
hint.resize(0);
size_t num_chars_added = 0;
for (auto it = reshape_queue.begin(); it != reshape_queue.end(); ++it) {
if (it->action_ == kReshapeQueueNextFont) {
break;
}
CheckTextLen(it->start_index_, it->num_characters_);
if (text_.Is8Bit()) {
for (unsigned i = 0; i < it->num_characters_; i++) {
const UChar hint_char = text_[it->start_index_ + i];
hint.push_back(hint_char);
num_chars_added++;
// Determine if we can take a shortcut and not fill the hint list
// further: We can do that if we do not need a hint list, and we have
// managed to find a character with a definite script since
// FontFallbackIterator needs a character with a determined script to
// perform meaningful system fallback.
if (!needs_hint_list && Character::HasDefiniteScript(hint_char)) {
return true;
}
}
continue;
}
// !text_.Is8Bit()...
UChar32 hint_char;
UTF16TextIterator iterator(
text_.Span16().subspan(it->start_index_, it->num_characters_));
while (iterator.Consume(hint_char)) {
hint.push_back(hint_char);
num_chars_added++;
// Determine if we can take a shortcut and not fill the hint list
// further: We can do that if we do not need a hint list, and we have
// managed to find a character with a definite script since
// FontFallbackIterator needs a character with a determined script to
// perform meaningful system fallback.
if (!needs_hint_list && Character::HasDefiniteScript(hint_char)) {
return true;
}
iterator.Advance();
}
}
return num_chars_added > 0;
}
namespace {
void SplitUntilNextCaseChange(
const String& text,
Deque<blink::ReshapeQueueItem>* queue,
blink::ReshapeQueueItem& current_queue_item,
SmallCapsIterator::SmallCapsBehavior& small_caps_behavior) {
// TODO(layout-dev): Add support for latin-1 to SmallCapsIterator.
base::span<const UChar> normalized_buffer;
std::optional<String> utf16_text;
if (text.Is8Bit()) {
utf16_text.emplace(text);
utf16_text->Ensure16Bit();
normalized_buffer = utf16_text->Span16();
} else {
normalized_buffer = text.Span16();
}
unsigned num_characters_until_case_change = 0;
SmallCapsIterator small_caps_iterator(normalized_buffer.subspan(
current_queue_item.start_index_, current_queue_item.num_characters_));
small_caps_iterator.Consume(&num_characters_until_case_change,
&small_caps_behavior);
if (num_characters_until_case_change > 0 &&
num_characters_until_case_change < current_queue_item.num_characters_) {
queue->push_front(blink::ReshapeQueueItem(
blink::ReshapeQueueItemAction::kReshapeQueueRange,
current_queue_item.start_index_ + num_characters_until_case_change,
current_queue_item.num_characters_ - num_characters_until_case_change));
current_queue_item.num_characters_ = num_characters_until_case_change;
}
}
class CapsFeatureSettingsScopedOverlay final {
STACK_ALLOCATED();
public:
CapsFeatureSettingsScopedOverlay(FontFeatures*,
FontDescription::FontVariantCaps);
CapsFeatureSettingsScopedOverlay() = delete;
~CapsFeatureSettingsScopedOverlay();
private:
void OverlayCapsFeatures(FontDescription::FontVariantCaps);
void PrependCounting(const FontFeatureRange&);
FontFeatures* features_;
wtf_size_t count_features_;
};
CapsFeatureSettingsScopedOverlay::CapsFeatureSettingsScopedOverlay(
FontFeatures* features,
FontDescription::FontVariantCaps variant_caps)
: features_(features), count_features_(0) {
OverlayCapsFeatures(variant_caps);
}
void CapsFeatureSettingsScopedOverlay::OverlayCapsFeatures(
FontDescription::FontVariantCaps variant_caps) {
static constexpr FontFeatureRange smcp{{{'s', 'm', 'c', 'p'}, 1}};
static constexpr FontFeatureRange pcap{{{'p', 'c', 'a', 'p'}, 1}};
static constexpr FontFeatureRange c2sc{{{'c', '2', 's', 'c'}, 1}};
static constexpr FontFeatureRange c2pc{{{'c', '2', 'p', 'c'}, 1}};
static constexpr FontFeatureRange unic{{{'u', 'n', 'i', 'c'}, 1}};
static constexpr FontFeatureRange titl{{{'t', 'i', 't', 'l'}, 1}};
if (variant_caps == FontDescription::kSmallCaps ||
variant_caps == FontDescription::kAllSmallCaps) {
PrependCounting(smcp);
if (variant_caps == FontDescription::kAllSmallCaps) {
PrependCounting(c2sc);
}
}
if (variant_caps == FontDescription::kPetiteCaps ||
variant_caps == FontDescription::kAllPetiteCaps) {
PrependCounting(pcap);
if (variant_caps == FontDescription::kAllPetiteCaps) {
PrependCounting(c2pc);
}
}
if (variant_caps == FontDescription::kUnicase) {
PrependCounting(unic);
}
if (variant_caps == FontDescription::kTitlingCaps) {
PrependCounting(titl);
}
}
void CapsFeatureSettingsScopedOverlay::PrependCounting(
const FontFeatureRange& feature) {
features_->push_front(feature);
count_features_++;
}
CapsFeatureSettingsScopedOverlay::~CapsFeatureSettingsScopedOverlay() {
features_->EraseAt(0, count_features_);
}
} // namespace
void HarfBuzzShaper::ShapeSegment(
RangeContext* range_data,
const RunSegmenter::RunSegmenterRange& segment,
ShapeResult* result) const {
DCHECK(result);
DCHECK(range_data->buffer);
const Font* font = range_data->font;
const FontDescription& font_description = font->GetFontDescription();
const LayoutLocale& locale = font_description.LocaleOrDefault();
const hb_language_t language = locale.HarfbuzzLanguage();
bool needs_caps_handling =
font_description.VariantCaps() != FontDescription::kCapsNormal;
OpenTypeCapsSupport caps_support;
FontFallbackIterator fallback_iterator(
font->CreateFontFallbackIterator(ApplyFontVariantEmojiOnFallbackPriority(
segment.font_fallback_priority, font_description.VariantEmoji())));
range_data->reshape_queue.push_back(
ReshapeQueueItem(kReshapeQueueNextFont, 0, 0));
range_data->reshape_queue.push_back(ReshapeQueueItem(
kReshapeQueueRange, segment.start, segment.end - segment.start));
bool font_cycle_queued = false;
HintCharList fallback_chars_hint;
// Reserve sufficient capacity to avoid multiple reallocations, only when a
// full hint list is needed.
if (fallback_iterator.NeedsHintList()) {
fallback_chars_hint.ReserveInitialCapacity(range_data->end -
range_data->start);
}
FontDataForRangeSet* current_font_data_for_range_set = nullptr;
FallbackFontStage fallback_stage = kIntermediate;
// Variation selector mode should be always set to default at the
// beginning of the segment shaping run.
DCHECK(HarfBuzzFace::GetVariationSelectorMode() ==
kUseSpecifiedVariationSelector);
if (font_description.VariantEmoji() != kNormalVariantEmoji) {
HarfBuzzFace::SetVariationSelectorMode(
GetVariationSelectorModeFromFontVariantEmoji(
font_description.VariantEmoji()));
}
while (!range_data->reshape_queue.empty()) {
ReshapeQueueItem current_queue_item = range_data->reshape_queue.TakeFirst();
if (current_queue_item.action_ != kReshapeQueueRange) {
if (current_queue_item.action_ == kReshapeQueueReset) {
// We reached last font in the list, some of the variation sequences
// are not shaped yet and there is a fonts in the list that has glyphs
// for the base codepoint of unshaped variation sequences, so we need to
// restart the fallback queue and set the variation selector mode to
// `kIgnoreVariationSelector`.
DCHECK_EQ(fallback_stage, kLastWithVS);
fallback_iterator.Reset();
fallback_stage = kIntermediateIgnoreVS;
HarfBuzzFace::SetVariationSelectorMode(kIgnoreVariationSelector);
}
if (!CollectFallbackHintChars(range_data->reshape_queue,
fallback_iterator.NeedsHintList(),
fallback_chars_hint)) {
// Give up shaping since we cannot retrieve a font fallback
// font without a hintlist.
range_data->reshape_queue.clear();
break;
}
current_font_data_for_range_set =
fallback_iterator.Next(fallback_chars_hint);
if (!current_font_data_for_range_set->FontData()) {
DCHECK(range_data->reshape_queue.empty());
break;
}
font_cycle_queued = false;
continue;
}
if (!fallback_iterator.HasNext()) {
fallback_stage = ChangeStageToLast(fallback_stage);
}
const SimpleFontData* font_data =
current_font_data_for_range_set->FontData();
SmallCapsIterator::SmallCapsBehavior small_caps_behavior =
SmallCapsIterator::kSmallCapsSameCase;
if (needs_caps_handling) {
caps_support =
OpenTypeCapsSupport(font_data->PlatformData().GetHarfBuzzFace(),
font_description.VariantCaps(),
font_description.GetFontSynthesisSmallCaps(),
ICUScriptToHBScript(segment.script));
if (caps_support.NeedsRunCaseSplitting()) {
SplitUntilNextCaseChange(text_, &range_data->reshape_queue,
current_queue_item, small_caps_behavior);
// Skip queue items generated by SplitUntilNextCaseChange that do not
// contribute to the shape result if the range_data restricts shaping to
// a substring.
if (range_data->start >= current_queue_item.start_index_ +
current_queue_item.num_characters_ ||
range_data->end <= current_queue_item.start_index_) {
continue;
}
}
}
DCHECK(current_queue_item.num_characters_);
const SimpleFontData* adjusted_font = font_data;
// Clamp the start and end offsets of the queue item to the offsets
// representing the shaping window.
const unsigned shape_start =
std::max(range_data->start, current_queue_item.start_index_);
const unsigned shape_end =
std::min(range_data->end, current_queue_item.start_index_ +
current_queue_item.num_characters_);
DCHECK_GT(shape_end, shape_start);
CheckTextEnd(shape_start, shape_end);
CaseMapIntend case_map_intend = CaseMapIntend::kKeepSameCase;
if (needs_caps_handling) {
case_map_intend = caps_support.NeedsCaseChange(small_caps_behavior);
if (caps_support.NeedsSyntheticFont(small_caps_behavior)) {
adjusted_font = font_data->SmallCapsFontData(font_description);
}
}
CaseMappingHarfBuzzBufferFiller(
case_map_intend, font_description.LocaleOrDefault(),
range_data->buffer.Get(), text_, shape_start, shape_end - shape_start);
CanvasRotationInVertical canvas_rotation =
CanvasRotationForRun(adjusted_font->PlatformData().Orientation(),
segment.render_orientation, font_description);
CapsFeatureSettingsScopedOverlay caps_overlay(
&range_data->font_features,
caps_support.FontFeatureToUse(small_caps_behavior));
hb_direction_t direction = range_data->HarfBuzzDirection(canvas_rotation);
HanKerning han_kerning(
text_, shape_start, shape_end, *adjusted_font, font_description,
{.is_horizontal = HB_DIRECTION_IS_HORIZONTAL(direction),
.is_line_start = range_data->options.is_line_start &&
range_data->start == shape_start,
.apply_start = range_data->options.han_kerning_start &&
range_data->start == shape_start,
.apply_end = range_data->options.han_kerning_end &&
range_data->end == shape_end},
&range_data->font_features);
if (!ShapeRange(range_data->buffer.Get(), range_data->font_features,
adjusted_font, current_font_data_for_range_set->Ranges(),
segment.script, direction, language,
font_description.SpecifiedSize())) {
DLOG(ERROR) << "Shaping range failed.";
}
ExtractShapeResults(range_data, font_cycle_queued, current_queue_item,
adjusted_font, segment.script, canvas_rotation,
fallback_stage, result);
if (!han_kerning.UnsafeToBreakBefore().empty()) [[unlikely]] {
result->AddUnsafeToBreak(han_kerning.UnsafeToBreakBefore());
}
hb_buffer_reset(range_data->buffer.Get());
}
// Set variation selector mode to the default state.
HarfBuzzFace::SetVariationSelectorMode(kUseSpecifiedVariationSelector);
if (IsEmojiPresentationEmoji(segment.font_fallback_priority)) {
EmojiCorrectness emoji_correctness =
ComputeBrokenEmojiPercentage(result, segment.start, segment.end);
if (emoji_metrics_reporter_for_testing_) {
emoji_metrics_reporter_for_testing_.Run(
emoji_correctness.num_clusters,
emoji_correctness.num_broken_clusters);
} else {
range_data->font->ReportEmojiSegmentGlyphCoverage(
emoji_correctness.num_clusters,
emoji_correctness.num_broken_clusters);
}
}
}
ShapeResult* HarfBuzzShaper::Shape(const Font* font,
TextDirection direction,
unsigned start,
unsigned end) const {
DCHECK_GE(end, start);
DCHECK_LE(end, text_.length());
const unsigned length = end - start;
ShapeResult* result =
MakeGarbageCollected<ShapeResult>(start, length, direction);
RangeContext range_data(font, direction, start, end);
if (text_.Is8Bit()) {
// 8-bit text is guaranteed to be horizontal latin-1.
RunSegmenter::RunSegmenterRange segment_range = {
start, end, USCRIPT_LATIN, OrientationIterator::kOrientationKeep,
FontFallbackPriority::kText};
ShapeSegment(&range_data, segment_range, result);
} else {
// Run segmentation needs to operate on the entire string, regardless of the
// shaping window (defined by the start and end parameters).
DCHECK(!text_.Is8Bit());
RunSegmenter run_segmenter(text_.Span16(),
font->GetFontDescription().Orientation());
RunSegmenter::RunSegmenterRange segment_range;
while (run_segmenter.Consume(&segment_range)) {
// Only shape segments overlapping with the range indicated by start and
// end. Not only those strictly within.
if (start < segment_range.end && end > segment_range.start) {
ShapeSegment(&range_data, segment_range, result);
}
// Break if beyond the requested range. Because RunSegmenter is
// incremental, further ranges are not needed. This also allows reusing
// the segmenter state for next incremental calls.
if (segment_range.end >= end) {
break;
}
}
}
#if EXPENSIVE_DCHECKS_ARE_ON()
CheckShapeResultRange(result, start, end, text_, font);
#endif
return result;
}
ShapeResult* HarfBuzzShaper::Shape(
const Font* font,
TextDirection direction,
unsigned start,
unsigned end,
const Vector<RunSegmenter::RunSegmenterRange>& ranges,
ShapeOptions options) const {
DCHECK_GE(end, start);
DCHECK_LE(end, text_.length());
DCHECK_GT(ranges.size(), 0u);
DCHECK_EQ(start, ranges[0].start);
DCHECK_EQ(end, ranges[ranges.size() - 1].end);
const unsigned length = end - start;
ShapeResult* result =
MakeGarbageCollected<ShapeResult>(start, length, direction);
RangeContext range_data(font, direction, start, end, options);
for (const RunSegmenter::RunSegmenterRange& segmented_range : ranges) {
DCHECK_GE(segmented_range.end, segmented_range.start);
DCHECK_GE(segmented_range.start, start);
DCHECK_LE(segmented_range.end, end);
ShapeSegment(&range_data, segmented_range, result);
}
#if EXPENSIVE_DCHECKS_ARE_ON()
CheckShapeResultRange(result, start, end, text_, font);
#endif
return result;
}
ShapeResult* HarfBuzzShaper::Shape(
const Font* font,
TextDirection direction,
unsigned start,
unsigned end,
const RunSegmenter::RunSegmenterRange pre_segmented,
ShapeOptions options) const {
DCHECK_GE(end, start);
DCHECK_LE(end, text_.length());
DCHECK_GE(start, pre_segmented.start);
DCHECK_LE(end, pre_segmented.end);
const unsigned length = end - start;
ShapeResult* result =
MakeGarbageCollected<ShapeResult>(start, length, direction);
RangeContext range_data(font, direction, start, end, options);
ShapeSegment(&range_data, pre_segmented, result);
#if EXPENSIVE_DCHECKS_ARE_ON()
CheckShapeResultRange(result, start, end, text_, font);
#endif
return result;
}
ShapeResult* HarfBuzzShaper::Shape(const Font* font,
TextDirection direction) const {
return Shape(font, direction, 0, text_.length());
}
void HarfBuzzShaper::GetGlyphData(const SimpleFontData& font_data,
const LayoutLocale& locale,
UScriptCode script,
bool is_horizontal,
GlyphDataList& glyphs) {
PooledHarfBuzzBuffer pooled_hb_buffer;
hb_buffer_t* hb_buffer = pooled_hb_buffer.Get();
hb_buffer_set_language(hb_buffer, locale.HarfbuzzLanguage());
hb_buffer_set_script(hb_buffer, ICUScriptToHBScript(script));
hb_buffer_set_direction(hb_buffer,
is_horizontal ? HB_DIRECTION_LTR : HB_DIRECTION_TTB);
CHECK(!text_.Is8Bit());
static_assert(sizeof(uint16_t) == sizeof(UChar));
auto span = text_.SpanUint16();
hb_buffer_add_utf16(hb_buffer, span.data(), span.size(), 0, text_.length());
const FontPlatformData& platform_data = font_data.PlatformData();
HarfBuzzFace* const hb_face = platform_data.GetHarfBuzzFace();
DCHECK(hb_face);
hb_font_t* const hb_font = hb_face->GetScaledFont(
nullptr,
is_horizontal ? HarfBuzzFace::kNoVerticalLayout
: HarfBuzzFace::kPrepareForVerticalLayout,
platform_data.size());
DCHECK(hb_font);
hb_shape(hb_font, hb_buffer, nullptr, 0);
// Create `GlyphDataList` from `hb_buffer`.
unsigned num_glyphs;
hb_glyph_info_t* glyph_info =
hb_buffer_get_glyph_infos(hb_buffer, &num_glyphs);
hb_glyph_position_t* glyph_position =
hb_buffer_get_glyph_positions(hb_buffer, nullptr);
glyphs.reserve(num_glyphs);
for (; num_glyphs;
--num_glyphs, UNSAFE_TODO(++glyph_info), UNSAFE_TODO(++glyph_position)) {
glyphs.push_back(GlyphData{
.cluster = glyph_info->cluster,
.glyph = static_cast<Glyph>(glyph_info->codepoint),
.advance = {HarfBuzzPositionToFloat(glyph_position->x_advance),
-HarfBuzzPositionToFloat(glyph_position->y_advance)},
.offset = {HarfBuzzPositionToFloat(glyph_position->x_offset),
-HarfBuzzPositionToFloat(glyph_position->y_offset)}});
}
}
} // namespace blink
|