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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/fonts/shaping/text_auto_space.h"
#include <unicode/uchar.h>
#include <unicode/uscript.h>
#include "base/check.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/fonts/simple_font_data.h"
#include "third_party/blink/renderer/platform/wtf/text/utf16.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
float TextAutoSpace::GetSpacingWidth(const Font* font) {
if (const SimpleFontData* font_data = font->PrimaryFont()) {
return font_data->IdeographicInlineSize().value_or(
font_data->PlatformData().size()) /
8;
}
NOTREACHED();
}
// static
TextAutoSpace::CharType TextAutoSpace::GetTypeAndNext(const String& text,
wtf_size_t& offset) {
CHECK(!text.Is8Bit());
return GetType(CodePointAtAndNext(text.Span16(), offset));
}
// static
TextAutoSpace::CharType TextAutoSpace::GetPrevType(const String& text,
wtf_size_t offset) {
DCHECK_GT(offset, 0u);
CHECK(!text.Is8Bit());
UChar32 last_ch;
UNSAFE_TODO(U16_PREV(text.Characters16(), 0, offset, last_ch));
return GetType(last_ch);
}
// static
TextAutoSpace::CharType TextAutoSpace::GetType(UChar32 ch) {
// This logic is based on:
// https://drafts.csswg.org/css-text-4/#text-spacing-classes
const uint32_t gc_mask = U_GET_GC_MASK(ch);
static_assert(kNonHanIdeographMin <= 0x30FF && 0x30FF <= kNonHanIdeographMax);
if (ch >= kNonHanIdeographMin && ch <= 0x30FF && !(gc_mask & U_GC_P_MASK)) {
return kIdeograph;
}
static_assert(kNonHanIdeographMin <= 0x31C0 && 0x31C0 <= kNonHanIdeographMax);
if (ch >= 0x31C0 && ch <= kNonHanIdeographMax) {
return kIdeograph;
}
UErrorCode err = U_ZERO_ERROR;
const UScriptCode script = uscript_getScript(ch, &err);
DCHECK(U_SUCCESS(err));
if (U_SUCCESS(err) && script == USCRIPT_HAN) {
return kIdeograph;
}
if (gc_mask & (U_GC_L_MASK | U_GC_M_MASK | U_GC_ND_MASK)) {
const UEastAsianWidth eaw = static_cast<UEastAsianWidth>(
u_getIntPropertyValue(ch, UCHAR_EAST_ASIAN_WIDTH));
if (eaw != UEastAsianWidth::U_EA_FULLWIDTH) {
return kLetterOrNumeral;
}
}
return kOther;
}
std::ostream& operator<<(std::ostream& ostream, TextAutoSpace::CharType type) {
switch (type) {
case TextAutoSpace::kIdeograph:
return ostream << "kIdeograph";
case TextAutoSpace::kLetterOrNumeral:
return ostream << "kLetterOrNumeral";
case TextAutoSpace::kOther:
return ostream << "kOther";
}
return ostream << static_cast<int>(type);
}
} // namespace blink
|