From 342d466567dd491b12d7ef05847ef3e614623422 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 6 Apr 2018 11:48:03 -0700 Subject: [PATCH] libtxt: support for locale-specific text styles (flutter/engine#4943) Fixes https://github.com/flutter/flutter/issues/12630 --- .../snapshot/fuchsia_compilation_trace.txt | 2 + engine/src/flutter/lib/ui/text.dart | 51 ++++++++++++++----- .../flutter/lib/ui/text/paragraph_builder.cc | 25 +++++++-- .../flutter/lib/ui/text/paragraph_builder.h | 9 ++-- .../third_party/txt/src/txt/paragraph.cc | 8 ++- .../txt/src/txt/paragraph_style.cc | 1 + .../third_party/txt/src/txt/paragraph_style.h | 2 + .../third_party/txt/src/txt/text_style.cc | 2 + .../third_party/txt/src/txt/text_style.h | 1 + 9 files changed, 80 insertions(+), 21 deletions(-) diff --git a/engine/src/flutter/lib/snapshot/fuchsia_compilation_trace.txt b/engine/src/flutter/lib/snapshot/fuchsia_compilation_trace.txt index ccefbcce84..49bf715cfc 100644 --- a/engine/src/flutter/lib/snapshot/fuchsia_compilation_trace.txt +++ b/engine/src/flutter/lib/snapshot/fuchsia_compilation_trace.txt @@ -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. diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index 5a456eeb23..7c42c878a9 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -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]. /// diff --git a/engine/src/flutter/lib/ui/text/paragraph_builder.cc b/engine/src/flutter/lib/ui/text/paragraph_builder.cc index 469f2ca939..44d05053ba 100644 --- a/engine/src/flutter/lib/ui/text/paragraph_builder.cc +++ b/engine/src/flutter/lib/ui/text/paragraph_builder.cc @@ -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::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::create( const std::string& fontFamily, double fontSize, double lineHeight, - const std::u16string& ellipsis) { + const std::u16string& ellipsis, + const std::string& locale) { return fxl::MakeRefCounted(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( 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. diff --git a/engine/src/flutter/lib/ui/text/paragraph_builder.h b/engine/src/flutter/lib/ui/text/paragraph_builder.h index ceb8c3a5de..37a1e9a3e7 100644 --- a/engine/src/flutter/lib/ui/text/paragraph_builder.h +++ b/engine/src/flutter/lib/ui/text/paragraph_builder.h @@ -31,7 +31,8 @@ class ParagraphBuilder : public fxl::RefCountedThreadSafe, 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, 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, const std::string& fontFamily, double fontSize, double lineHeight, - const std::u16string& ellipsis); + const std::u16string& ellipsis, + const std::string& locale); void createRenderView(); diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc index cad44199e7..aec5977179 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph.cc @@ -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, diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph_style.cc b/engine/src/flutter/third_party/txt/src/txt/paragraph_style.cc index a7941ad4e5..362f138bb3 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph_style.cc +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph_style.cc @@ -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; } diff --git a/engine/src/flutter/third_party/txt/src/txt/paragraph_style.h b/engine/src/flutter/third_party/txt/src/txt/paragraph_style.h index 8c2701768b..13d32106c1 100644 --- a/engine/src/flutter/third_party/txt/src/txt/paragraph_style.h +++ b/engine/src/flutter/third_party/txt/src/txt/paragraph_style.h @@ -53,6 +53,8 @@ class ParagraphStyle { size_t max_lines = std::numeric_limits::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). diff --git a/engine/src/flutter/third_party/txt/src/txt/text_style.cc b/engine/src/flutter/third_party/txt/src/txt/text_style.cc index 0796ad27e4..242b6f3818 100644 --- a/engine/src/flutter/third_party/txt/src/txt/text_style.cc +++ b/engine/src/flutter/third_party/txt/src/txt/text_style.cc @@ -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; } diff --git a/engine/src/flutter/third_party/txt/src/txt/text_style.h b/engine/src/flutter/third_party/txt/src/txt/text_style.h index 6e12560638..0a57cc7c68 100644 --- a/engine/src/flutter/third_party/txt/src/txt/text_style.h +++ b/engine/src/flutter/third_party/txt/src/txt/text_style.h @@ -45,6 +45,7 @@ class TextStyle { double letter_spacing = 0.0; double word_spacing = 0.0; double height = 1.0; + std::string locale; TextStyle();