libtxt: support for locale-specific text styles (flutter/engine#4943)
Fixes https://github.com/flutter/flutter/issues/12630
This commit is contained in:
@@ -1845,6 +1845,7 @@ dart:ui,TextStyle,get:_fontSize
|
||||
dart:ui,TextStyle,get:_letterSpacing
|
||||
dart:ui,TextStyle,get:_wordSpacing
|
||||
dart:ui,TextStyle,get:_height
|
||||
dart:ui,TextStyle,get:_locale
|
||||
dart:ui,FilterQuality,get:index
|
||||
dart:ui,ParagraphStyle,ParagraphStyle.
|
||||
dart:ui,ParagraphStyle,get:_encoded
|
||||
@@ -1852,6 +1853,7 @@ dart:ui,ParagraphStyle,get:_fontFamily
|
||||
dart:ui,ParagraphStyle,get:_fontSize
|
||||
dart:ui,ParagraphStyle,get:_lineHeight
|
||||
dart:ui,ParagraphStyle,get:_ellipsis
|
||||
dart:ui,ParagraphStyle,get:_locale
|
||||
dart:ui,_Jenkins,combine
|
||||
dart:ui,_Jenkins,finish
|
||||
dart:ui,_HashEnd,_HashEnd.
|
||||
|
||||
@@ -256,6 +256,7 @@ Int32List _encodeTextStyle(
|
||||
double letterSpacing,
|
||||
double wordSpacing,
|
||||
double height,
|
||||
Locale locale,
|
||||
) {
|
||||
final Int32List result = new Int32List(8);
|
||||
if (color != null) {
|
||||
@@ -306,6 +307,10 @@ Int32List _encodeTextStyle(
|
||||
result[0] |= 1 << 12;
|
||||
// Passed separately to native.
|
||||
}
|
||||
if (locale != null) {
|
||||
result[0] |= 1 << 13;
|
||||
// Passed separately to native.
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -325,6 +330,7 @@ class TextStyle {
|
||||
/// * `wordSpacing`: The amount of space (in logical pixels) to add at each sequence of white-space (i.e. between each word).
|
||||
/// * `textBaseline`: The common baseline that should be aligned between this text span and its parent text span, or, for the root text spans, with the line box.
|
||||
/// * `height`: The height of this text span, as a multiple of the font size.
|
||||
/// * `locale`: The locale used to select region-specific glyphs.
|
||||
TextStyle({
|
||||
Color color,
|
||||
TextDecoration decoration,
|
||||
@@ -338,6 +344,7 @@ class TextStyle {
|
||||
double letterSpacing,
|
||||
double wordSpacing,
|
||||
double height,
|
||||
Locale locale,
|
||||
}) : _encoded = _encodeTextStyle(
|
||||
color,
|
||||
decoration,
|
||||
@@ -351,12 +358,14 @@ class TextStyle {
|
||||
letterSpacing,
|
||||
wordSpacing,
|
||||
height,
|
||||
locale,
|
||||
),
|
||||
_fontFamily = fontFamily ?? '',
|
||||
_fontSize = fontSize,
|
||||
_letterSpacing = letterSpacing,
|
||||
_wordSpacing = wordSpacing,
|
||||
_height = height;
|
||||
_height = height,
|
||||
_locale = locale;
|
||||
|
||||
final Int32List _encoded;
|
||||
final String _fontFamily;
|
||||
@@ -364,6 +373,7 @@ class TextStyle {
|
||||
final double _letterSpacing;
|
||||
final double _wordSpacing;
|
||||
final double _height;
|
||||
final Locale _locale;
|
||||
|
||||
@override
|
||||
bool operator ==(dynamic other) {
|
||||
@@ -376,7 +386,8 @@ class TextStyle {
|
||||
_fontSize != typedOther._fontSize ||
|
||||
_letterSpacing != typedOther._letterSpacing ||
|
||||
_wordSpacing != typedOther._wordSpacing ||
|
||||
_height != typedOther._height)
|
||||
_height != typedOther._height ||
|
||||
_locale != typedOther._locale)
|
||||
return false;
|
||||
for (int index = 0; index < _encoded.length; index += 1) {
|
||||
if (_encoded[index] != typedOther._encoded[index])
|
||||
@@ -386,7 +397,7 @@ class TextStyle {
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => hashValues(hashList(_encoded), _fontFamily, _fontSize, _letterSpacing, _wordSpacing, _height);
|
||||
int get hashCode => hashValues(hashList(_encoded), _fontFamily, _fontSize, _letterSpacing, _wordSpacing, _height, _locale);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -402,7 +413,8 @@ class TextStyle {
|
||||
'fontSize: ${ _encoded[0] & 0x0200 == 0x0200 ? _fontSize : "unspecified"}, '
|
||||
'letterSpacing: ${ _encoded[0] & 0x0400 == 0x0400 ? "${_letterSpacing}x" : "unspecified"}, '
|
||||
'wordSpacing: ${ _encoded[0] & 0x0800 == 0x0800 ? "${_wordSpacing}x" : "unspecified"}, '
|
||||
'height: ${ _encoded[0] & 0x1000 == 0x1000 ? "${_height}x" : "unspecified"}'
|
||||
'height: ${ _encoded[0] & 0x1000 == 0x1000 ? "${_height}x" : "unspecified"}, '
|
||||
'locale: ${ _encoded[0] & 0x2000 == 0x2000 ? _locale : "unspecified"}'
|
||||
')';
|
||||
}
|
||||
}
|
||||
@@ -434,6 +446,7 @@ Int32List _encodeParagraphStyle(
|
||||
double fontSize,
|
||||
double lineHeight,
|
||||
String ellipsis,
|
||||
Locale locale,
|
||||
) {
|
||||
final Int32List result = new Int32List(6); // also update paragraph_builder.cc
|
||||
if (textAlign != null) {
|
||||
@@ -472,6 +485,10 @@ Int32List _encodeParagraphStyle(
|
||||
result[0] |= 1 << 9;
|
||||
// Passed separately to native.
|
||||
}
|
||||
if (locale != null) {
|
||||
result[0] |= 1 << 10;
|
||||
// Passed separately to native.
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -521,6 +538,8 @@ class ParagraphStyle {
|
||||
/// constraints are those set in the [ParagraphConstraints] object passed to
|
||||
/// the [Paragraph.layout] method. The empty string and the null value are
|
||||
/// considered equivalent and turn off this behavior.
|
||||
///
|
||||
/// * `locale`: The locale used to select region-specific glyphs.
|
||||
ParagraphStyle({
|
||||
TextAlign textAlign,
|
||||
TextDirection textDirection,
|
||||
@@ -531,6 +550,7 @@ class ParagraphStyle {
|
||||
double fontSize,
|
||||
double lineHeight,
|
||||
String ellipsis,
|
||||
Locale locale,
|
||||
}) : _encoded = _encodeParagraphStyle(
|
||||
textAlign,
|
||||
textDirection,
|
||||
@@ -541,17 +561,20 @@ class ParagraphStyle {
|
||||
fontSize,
|
||||
lineHeight,
|
||||
ellipsis,
|
||||
locale,
|
||||
),
|
||||
_fontFamily = fontFamily,
|
||||
_fontSize = fontSize,
|
||||
_lineHeight = lineHeight,
|
||||
_ellipsis = ellipsis;
|
||||
_ellipsis = ellipsis,
|
||||
_locale = locale;
|
||||
|
||||
final Int32List _encoded;
|
||||
final String _fontFamily;
|
||||
final double _fontSize;
|
||||
final double _lineHeight;
|
||||
final String _ellipsis;
|
||||
final Locale _locale;
|
||||
|
||||
@override
|
||||
bool operator ==(dynamic other) {
|
||||
@@ -563,7 +586,8 @@ class ParagraphStyle {
|
||||
if (_fontFamily != typedOther._fontFamily ||
|
||||
_fontSize != typedOther._fontSize ||
|
||||
_lineHeight != typedOther._lineHeight ||
|
||||
_ellipsis != typedOther._ellipsis)
|
||||
_ellipsis != typedOther._ellipsis ||
|
||||
_locale != typedOther._locale)
|
||||
return false;
|
||||
for (int index = 0; index < _encoded.length; index += 1) {
|
||||
if (_encoded[index] != typedOther._encoded[index])
|
||||
@@ -573,7 +597,7 @@ class ParagraphStyle {
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => hashValues(hashList(_encoded), _fontFamily, _fontSize, _lineHeight, _ellipsis);
|
||||
int get hashCode => hashValues(hashList(_encoded), _fontFamily, _fontSize, _lineHeight, _ellipsis, _locale);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -586,7 +610,8 @@ class ParagraphStyle {
|
||||
'fontFamily: ${ _encoded[0] & 0x040 == 0x040 ? _fontFamily : "unspecified"}, '
|
||||
'fontSize: ${ _encoded[0] & 0x080 == 0x080 ? _fontSize : "unspecified"}, '
|
||||
'lineHeight: ${ _encoded[0] & 0x100 == 0x100 ? "${_lineHeight}x" : "unspecified"}, '
|
||||
'ellipsis: ${ _encoded[0] & 0x200 == 0x200 ? "\"$_ellipsis\"" : "unspecified"}'
|
||||
'ellipsis: ${ _encoded[0] & 0x200 == 0x200 ? "\"$_ellipsis\"" : "unspecified"}, '
|
||||
'locale: ${ _encoded[0] & 0x400 == 0x400 ? _locale : "unspecified"}'
|
||||
')';
|
||||
}
|
||||
}
|
||||
@@ -982,14 +1007,16 @@ class Paragraph extends NativeFieldWrapperClass2 {
|
||||
class ParagraphBuilder extends NativeFieldWrapperClass2 {
|
||||
/// Creates a [ParagraphBuilder] object, which is used to create a
|
||||
/// [Paragraph].
|
||||
ParagraphBuilder(ParagraphStyle style) { _constructor(style._encoded, style._fontFamily, style._fontSize, style._lineHeight, style._ellipsis); }
|
||||
void _constructor(Int32List encoded, String fontFamily, double fontSize, double lineHeight, String ellipsis) native 'ParagraphBuilder_constructor';
|
||||
ParagraphBuilder(ParagraphStyle style) { _constructor(style._encoded, style._fontFamily, style._fontSize, style._lineHeight, style._ellipsis, _encodeLocale(style._locale)); }
|
||||
void _constructor(Int32List encoded, String fontFamily, double fontSize, double lineHeight, String ellipsis, String locale) native 'ParagraphBuilder_constructor';
|
||||
|
||||
/// Applies the given style to the added text until [pop] is called.
|
||||
///
|
||||
/// See [pop] for details.
|
||||
void pushStyle(TextStyle style) => _pushStyle(style._encoded, style._fontFamily, style._fontSize, style._letterSpacing, style._wordSpacing, style._height);
|
||||
void _pushStyle(Int32List encoded, String fontFamily, double fontSize, double letterSpacing, double wordSpacing, double height) native 'ParagraphBuilder_pushStyle';
|
||||
void pushStyle(TextStyle style) => _pushStyle(style._encoded, style._fontFamily, style._fontSize, style._letterSpacing, style._wordSpacing, style._height, _encodeLocale(style._locale));
|
||||
void _pushStyle(Int32List encoded, String fontFamily, double fontSize, double letterSpacing, double wordSpacing, double height, String locale) native 'ParagraphBuilder_pushStyle';
|
||||
|
||||
static String _encodeLocale(Locale locale) => locale?.toString() ?? '';
|
||||
|
||||
/// Ends the effect of the most recent call to [pushStyle].
|
||||
///
|
||||
|
||||
@@ -41,6 +41,7 @@ const int tsFontSizeIndex = 9;
|
||||
const int tsLetterSpacingIndex = 10;
|
||||
const int tsWordSpacingIndex = 11;
|
||||
const int tsHeightIndex = 12;
|
||||
const int tsLocaleIndex = 13;
|
||||
|
||||
const int tsColorMask = 1 << tsColorIndex;
|
||||
const int tsTextDecorationMask = 1 << tsTextDecorationIndex;
|
||||
@@ -54,6 +55,7 @@ const int tsFontSizeMask = 1 << tsFontSizeIndex;
|
||||
const int tsLetterSpacingMask = 1 << tsLetterSpacingIndex;
|
||||
const int tsWordSpacingMask = 1 << tsWordSpacingIndex;
|
||||
const int tsHeightMask = 1 << tsHeightIndex;
|
||||
const int tsLocaleMask = 1 << tsLocaleIndex;
|
||||
|
||||
// ParagraphStyle
|
||||
|
||||
@@ -66,6 +68,7 @@ const int psFontFamilyIndex = 6;
|
||||
const int psFontSizeIndex = 7;
|
||||
const int psLineHeightIndex = 8;
|
||||
const int psEllipsisIndex = 9;
|
||||
const int psLocaleIndex = 10;
|
||||
|
||||
const int psTextAlignMask = 1 << psTextAlignIndex;
|
||||
const int psTextDirectionMask = 1 << psTextDirectionIndex;
|
||||
@@ -76,6 +79,7 @@ const int psFontFamilyMask = 1 << psFontFamilyIndex;
|
||||
const int psFontSizeMask = 1 << psFontSizeIndex;
|
||||
const int psLineHeightMask = 1 << psLineHeightIndex;
|
||||
const int psEllipsisMask = 1 << psEllipsisIndex;
|
||||
const int psLocaleMask = 1 << psLocaleIndex;
|
||||
|
||||
float getComputedSizeFromSpecifiedSize(float specifiedSize) {
|
||||
if (specifiedSize < std::numeric_limits<float>::epsilon())
|
||||
@@ -191,7 +195,7 @@ FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void ParagraphBuilder::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register(
|
||||
{{"ParagraphBuilder_constructor", ParagraphBuilder_constructor, 6, true},
|
||||
{{"ParagraphBuilder_constructor", ParagraphBuilder_constructor, 7, true},
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
@@ -200,16 +204,18 @@ fxl::RefPtr<ParagraphBuilder> ParagraphBuilder::create(
|
||||
const std::string& fontFamily,
|
||||
double fontSize,
|
||||
double lineHeight,
|
||||
const std::u16string& ellipsis) {
|
||||
const std::u16string& ellipsis,
|
||||
const std::string& locale) {
|
||||
return fxl::MakeRefCounted<ParagraphBuilder>(encoded, fontFamily, fontSize,
|
||||
lineHeight, ellipsis);
|
||||
lineHeight, ellipsis, locale);
|
||||
}
|
||||
|
||||
ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded,
|
||||
const std::string& fontFamily,
|
||||
double fontSize,
|
||||
double lineHeight,
|
||||
const std::u16string& ellipsis) {
|
||||
const std::u16string& ellipsis,
|
||||
const std::string& locale) {
|
||||
if (!Settings::Get().using_blink) {
|
||||
int32_t mask = encoded[0];
|
||||
txt::ParagraphStyle style;
|
||||
@@ -242,6 +248,10 @@ ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded,
|
||||
style.ellipsis = ellipsis;
|
||||
}
|
||||
|
||||
if (mask & psLocaleMask) {
|
||||
style.locale = locale;
|
||||
}
|
||||
|
||||
m_paragraphBuilder = std::make_unique<txt::ParagraphBuilder>(
|
||||
style, blink::FontCollection::ForProcess().GetFontCollection());
|
||||
} else {
|
||||
@@ -274,7 +284,8 @@ void ParagraphBuilder::pushStyle(tonic::Int32List& encoded,
|
||||
double fontSize,
|
||||
double letterSpacing,
|
||||
double wordSpacing,
|
||||
double height) {
|
||||
double height,
|
||||
const std::string& locale) {
|
||||
FXL_DCHECK(encoded.num_elements() == 8);
|
||||
|
||||
int32_t mask = encoded[0];
|
||||
@@ -331,6 +342,10 @@ void ParagraphBuilder::pushStyle(tonic::Int32List& encoded,
|
||||
style.height = height;
|
||||
}
|
||||
|
||||
if (mask & tsLocaleMask) {
|
||||
style.locale = locale;
|
||||
}
|
||||
|
||||
m_paragraphBuilder->PushStyle(style);
|
||||
} else {
|
||||
// Blink Version.
|
||||
|
||||
@@ -31,7 +31,8 @@ class ParagraphBuilder : public fxl::RefCountedThreadSafe<ParagraphBuilder>,
|
||||
const std::string& fontFamily,
|
||||
double fontSize,
|
||||
double lineHeight,
|
||||
const std::u16string& ellipsis);
|
||||
const std::u16string& ellipsis,
|
||||
const std::string& locale);
|
||||
|
||||
~ParagraphBuilder() override;
|
||||
|
||||
@@ -40,7 +41,8 @@ class ParagraphBuilder : public fxl::RefCountedThreadSafe<ParagraphBuilder>,
|
||||
double fontSize,
|
||||
double letterSpacing,
|
||||
double wordSpacing,
|
||||
double height);
|
||||
double height,
|
||||
const std::string& locale);
|
||||
|
||||
void pop();
|
||||
|
||||
@@ -55,7 +57,8 @@ class ParagraphBuilder : public fxl::RefCountedThreadSafe<ParagraphBuilder>,
|
||||
const std::string& fontFamily,
|
||||
double fontSize,
|
||||
double lineHeight,
|
||||
const std::u16string& ellipsis);
|
||||
const std::u16string& ellipsis,
|
||||
const std::string& locale);
|
||||
|
||||
void createRenderView();
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "font_collection.h"
|
||||
#include "font_skia.h"
|
||||
#include "lib/fxl/logging.h"
|
||||
#include "minikin/FontLanguageListCache.h"
|
||||
#include "minikin/HbFontCache.h"
|
||||
#include "minikin/LayoutUtils.h"
|
||||
#include "minikin/LineBreaker.h"
|
||||
@@ -130,7 +131,12 @@ bool GetItalic(const TextStyle& style) {
|
||||
}
|
||||
|
||||
minikin::FontStyle GetMinikinFontStyle(const TextStyle& style) {
|
||||
return minikin::FontStyle(GetWeight(style), GetItalic(style));
|
||||
uint32_t language_list_id =
|
||||
style.locale.empty()
|
||||
? minikin::FontLanguageListCache::kEmptyListId
|
||||
: minikin::FontStyle::registerLanguageList(style.locale);
|
||||
return minikin::FontStyle(language_list_id, 0, GetWeight(style),
|
||||
GetItalic(style));
|
||||
}
|
||||
|
||||
void GetFontAndMinikinPaint(const TextStyle& style,
|
||||
|
||||
@@ -24,6 +24,7 @@ TextStyle ParagraphStyle::GetTextStyle() const {
|
||||
result.font_style = font_style;
|
||||
result.font_family = font_family;
|
||||
result.font_size = font_size;
|
||||
result.locale = locale;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,8 @@ class ParagraphStyle {
|
||||
size_t max_lines = std::numeric_limits<size_t>::max();
|
||||
double line_height = 1.0;
|
||||
std::u16string ellipsis;
|
||||
std::string locale;
|
||||
|
||||
// Default strategy is kBreakStrategy_Greedy. Sometimes,
|
||||
// kBreakStrategy_HighQuality will produce more desireable layouts (eg, very
|
||||
// long words are more likely to be reasonably placed).
|
||||
|
||||
@@ -47,6 +47,8 @@ bool TextStyle::equals(const TextStyle& other) const {
|
||||
return false;
|
||||
if (height != other.height)
|
||||
return false;
|
||||
if (locale != other.locale)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ class TextStyle {
|
||||
double letter_spacing = 0.0;
|
||||
double word_spacing = 0.0;
|
||||
double height = 1.0;
|
||||
std::string locale;
|
||||
|
||||
TextStyle();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user