Unverified Commit c3ce892c authored by Jason Simmons's avatar Jason Simmons Committed by GitHub

Add support for locale-specific text styles (#16430)

See https://github.com/flutter/flutter/issues/12630 and
https://github.com/flutter/flutter/issues/16408
parent c43134a7
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle; import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle, Locale;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
...@@ -48,6 +48,7 @@ class TextPainter { ...@@ -48,6 +48,7 @@ class TextPainter {
double textScaleFactor: 1.0, double textScaleFactor: 1.0,
int maxLines, int maxLines,
String ellipsis, String ellipsis,
ui.Locale locale,
}) : assert(text == null || text.debugAssertIsValid()), }) : assert(text == null || text.debugAssertIsValid()),
assert(textAlign != null), assert(textAlign != null),
assert(textScaleFactor != null), assert(textScaleFactor != null),
...@@ -57,7 +58,8 @@ class TextPainter { ...@@ -57,7 +58,8 @@ class TextPainter {
_textDirection = textDirection, _textDirection = textDirection,
_textScaleFactor = textScaleFactor, _textScaleFactor = textScaleFactor,
_maxLines = maxLines, _maxLines = maxLines,
_ellipsis = ellipsis; _ellipsis = ellipsis,
_locale = locale;
ui.Paragraph _paragraph; ui.Paragraph _paragraph;
bool _needsLayout = true; bool _needsLayout = true;
...@@ -167,6 +169,17 @@ class TextPainter { ...@@ -167,6 +169,17 @@ class TextPainter {
_needsLayout = true; _needsLayout = true;
} }
/// The locale used to select region-specific glyphs.
ui.Locale get locale => _locale;
ui.Locale _locale;
set locale(ui.Locale value) {
if (_locale == value)
return;
_locale = value;
_paragraph = null;
_needsLayout = true;
}
/// An optional maximum number of lines for the text to span, wrapping if /// An optional maximum number of lines for the text to span, wrapping if
/// necessary. /// necessary.
/// ///
...@@ -199,11 +212,13 @@ class TextPainter { ...@@ -199,11 +212,13 @@ class TextPainter {
textScaleFactor: textScaleFactor, textScaleFactor: textScaleFactor,
maxLines: _maxLines, maxLines: _maxLines,
ellipsis: _ellipsis, ellipsis: _ellipsis,
locale: _locale,
) ?? new ui.ParagraphStyle( ) ?? new ui.ParagraphStyle(
textAlign: textAlign, textAlign: textAlign,
textDirection: textDirection ?? defaultTextDirection, textDirection: textDirection ?? defaultTextDirection,
maxLines: maxLines, maxLines: maxLines,
ellipsis: ellipsis, ellipsis: ellipsis,
locale: locale,
); );
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:ui' as ui show ParagraphStyle, TextStyle, lerpDouble; import 'dart:ui' as ui show ParagraphStyle, TextStyle, lerpDouble, Locale;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
...@@ -229,6 +229,7 @@ class TextStyle extends Diagnosticable { ...@@ -229,6 +229,7 @@ class TextStyle extends Diagnosticable {
this.wordSpacing, this.wordSpacing,
this.textBaseline, this.textBaseline,
this.height, this.height,
this.locale,
this.decoration, this.decoration,
this.decorationColor, this.decorationColor,
this.decorationStyle, this.decorationStyle,
...@@ -296,6 +297,9 @@ class TextStyle extends Diagnosticable { ...@@ -296,6 +297,9 @@ class TextStyle extends Diagnosticable {
/// the font size. /// the font size.
final double height; final double height;
/// The locale used to select region-specific glyphs.
final ui.Locale locale;
/// The decorations to paint near the text (e.g., an underline). /// The decorations to paint near the text (e.g., an underline).
final TextDecoration decoration; final TextDecoration decoration;
...@@ -330,6 +334,7 @@ class TextStyle extends Diagnosticable { ...@@ -330,6 +334,7 @@ class TextStyle extends Diagnosticable {
double wordSpacing, double wordSpacing,
TextBaseline textBaseline, TextBaseline textBaseline,
double height, double height,
ui.Locale locale,
TextDecoration decoration, TextDecoration decoration,
Color decorationColor, Color decorationColor,
TextDecorationStyle decorationStyle, TextDecorationStyle decorationStyle,
...@@ -352,6 +357,7 @@ class TextStyle extends Diagnosticable { ...@@ -352,6 +357,7 @@ class TextStyle extends Diagnosticable {
wordSpacing: wordSpacing ?? this.wordSpacing, wordSpacing: wordSpacing ?? this.wordSpacing,
textBaseline: textBaseline ?? this.textBaseline, textBaseline: textBaseline ?? this.textBaseline,
height: height ?? this.height, height: height ?? this.height,
locale: locale ?? this.locale,
decoration: decoration ?? this.decoration, decoration: decoration ?? this.decoration,
decorationColor: decorationColor ?? this.decorationColor, decorationColor: decorationColor ?? this.decorationColor,
decorationStyle: decorationStyle ?? this.decorationStyle, decorationStyle: decorationStyle ?? this.decorationStyle,
...@@ -429,6 +435,7 @@ class TextStyle extends Diagnosticable { ...@@ -429,6 +435,7 @@ class TextStyle extends Diagnosticable {
wordSpacing: wordSpacing == null ? null : wordSpacing * wordSpacingFactor + wordSpacingDelta, wordSpacing: wordSpacing == null ? null : wordSpacing * wordSpacingFactor + wordSpacingDelta,
textBaseline: textBaseline, textBaseline: textBaseline,
height: height == null ? null : height * heightFactor + heightDelta, height: height == null ? null : height * heightFactor + heightDelta,
locale: locale,
decoration: decoration ?? this.decoration, decoration: decoration ?? this.decoration,
decorationColor: decorationColor ?? this.decorationColor, decorationColor: decorationColor ?? this.decorationColor,
decorationStyle: decorationStyle ?? this.decorationStyle, decorationStyle: decorationStyle ?? this.decorationStyle,
...@@ -473,6 +480,7 @@ class TextStyle extends Diagnosticable { ...@@ -473,6 +480,7 @@ class TextStyle extends Diagnosticable {
wordSpacing: other.wordSpacing, wordSpacing: other.wordSpacing,
textBaseline: other.textBaseline, textBaseline: other.textBaseline,
height: other.height, height: other.height,
locale: other.locale,
decoration: other.decoration, decoration: other.decoration,
decorationColor: other.decorationColor, decorationColor: other.decorationColor,
decorationStyle: other.decorationStyle, decorationStyle: other.decorationStyle,
...@@ -518,6 +526,7 @@ class TextStyle extends Diagnosticable { ...@@ -518,6 +526,7 @@ class TextStyle extends Diagnosticable {
wordSpacing: ui.lerpDouble(a.wordSpacing ?? b.wordSpacing, b.wordSpacing ?? a.wordSpacing, t), wordSpacing: ui.lerpDouble(a.wordSpacing ?? b.wordSpacing, b.wordSpacing ?? a.wordSpacing, t),
textBaseline: t < 0.5 ? a.textBaseline : b.textBaseline, textBaseline: t < 0.5 ? a.textBaseline : b.textBaseline,
height: ui.lerpDouble(a.height ?? b.height, b.height ?? a.height, t), height: ui.lerpDouble(a.height ?? b.height, b.height ?? a.height, t),
locale: t < 0.5 ? a.locale : b.locale,
decoration: t < 0.5 ? a.decoration : b.decoration, decoration: t < 0.5 ? a.decoration : b.decoration,
decorationColor: Color.lerp(a.decorationColor, b.decorationColor, t), decorationColor: Color.lerp(a.decorationColor, b.decorationColor, t),
decorationStyle: t < 0.5 ? a.decorationStyle : b.decorationStyle, decorationStyle: t < 0.5 ? a.decorationStyle : b.decorationStyle,
...@@ -539,7 +548,8 @@ class TextStyle extends Diagnosticable { ...@@ -539,7 +548,8 @@ class TextStyle extends Diagnosticable {
fontSize: fontSize == null ? null : fontSize * textScaleFactor, fontSize: fontSize == null ? null : fontSize * textScaleFactor,
letterSpacing: letterSpacing, letterSpacing: letterSpacing,
wordSpacing: wordSpacing, wordSpacing: wordSpacing,
height: height height: height,
locale: locale,
); );
} }
...@@ -557,6 +567,7 @@ class TextStyle extends Diagnosticable { ...@@ -557,6 +567,7 @@ class TextStyle extends Diagnosticable {
double textScaleFactor: 1.0, double textScaleFactor: 1.0,
String ellipsis, String ellipsis,
int maxLines, int maxLines,
ui.Locale locale,
}) { }) {
assert(textScaleFactor != null); assert(textScaleFactor != null);
assert(maxLines == null || maxLines > 0); assert(maxLines == null || maxLines > 0);
...@@ -570,6 +581,7 @@ class TextStyle extends Diagnosticable { ...@@ -570,6 +581,7 @@ class TextStyle extends Diagnosticable {
lineHeight: height, lineHeight: height,
maxLines: maxLines, maxLines: maxLines,
ellipsis: ellipsis, ellipsis: ellipsis,
locale: locale,
); );
} }
...@@ -590,7 +602,8 @@ class TextStyle extends Diagnosticable { ...@@ -590,7 +602,8 @@ class TextStyle extends Diagnosticable {
letterSpacing != other.letterSpacing || letterSpacing != other.letterSpacing ||
wordSpacing != other.wordSpacing || wordSpacing != other.wordSpacing ||
textBaseline != other.textBaseline || textBaseline != other.textBaseline ||
height != other.height) height != other.height ||
locale != other.locale)
return RenderComparison.layout; return RenderComparison.layout;
if (color != other.color || if (color != other.color ||
decoration != other.decoration || decoration != other.decoration ||
...@@ -617,6 +630,7 @@ class TextStyle extends Diagnosticable { ...@@ -617,6 +630,7 @@ class TextStyle extends Diagnosticable {
wordSpacing == typedOther.wordSpacing && wordSpacing == typedOther.wordSpacing &&
textBaseline == typedOther.textBaseline && textBaseline == typedOther.textBaseline &&
height == typedOther.height && height == typedOther.height &&
locale == typedOther.locale &&
decoration == typedOther.decoration && decoration == typedOther.decoration &&
decorationColor == typedOther.decorationColor && decorationColor == typedOther.decorationColor &&
decorationStyle == typedOther.decorationStyle; decorationStyle == typedOther.decorationStyle;
...@@ -635,6 +649,7 @@ class TextStyle extends Diagnosticable { ...@@ -635,6 +649,7 @@ class TextStyle extends Diagnosticable {
wordSpacing, wordSpacing,
textBaseline, textBaseline,
height, height,
locale,
decoration, decoration,
decorationColor, decorationColor,
decorationStyle decorationStyle
...@@ -700,6 +715,7 @@ class TextStyle extends Diagnosticable { ...@@ -700,6 +715,7 @@ class TextStyle extends Diagnosticable {
styles.add(new DoubleProperty('${prefix}wordSpacing', wordSpacing, defaultValue: null)); styles.add(new DoubleProperty('${prefix}wordSpacing', wordSpacing, defaultValue: null));
styles.add(new EnumProperty<TextBaseline>('${prefix}baseline', textBaseline, defaultValue: null)); styles.add(new EnumProperty<TextBaseline>('${prefix}baseline', textBaseline, defaultValue: null));
styles.add(new DoubleProperty('${prefix}height', height, unit: 'x', defaultValue: null)); styles.add(new DoubleProperty('${prefix}height', height, unit: 'x', defaultValue: null));
styles.add(new StringProperty('${prefix}locale', locale?.toString(), defaultValue: null, quoted: false));
if (decoration != null || decorationColor != null || decorationStyle != null) { if (decoration != null || decorationColor != null || decorationStyle != null) {
final List<String> decorationDescription = <String>[]; final List<String> decorationDescription = <String>[];
if (decorationStyle != null) if (decorationStyle != null)
......
...@@ -462,6 +462,7 @@ class _TextStyleProxy implements TextStyle { ...@@ -462,6 +462,7 @@ class _TextStyleProxy implements TextStyle {
@override FontStyle get fontStyle => _delegate.fontStyle; @override FontStyle get fontStyle => _delegate.fontStyle;
@override FontWeight get fontWeight => _delegate.fontWeight; @override FontWeight get fontWeight => _delegate.fontWeight;
@override double get height => _delegate.height; @override double get height => _delegate.height;
@override ui.Locale get locale => _delegate.locale;
@override bool get inherit => _delegate.inherit; @override bool get inherit => _delegate.inherit;
@override double get letterSpacing => _delegate.letterSpacing; @override double get letterSpacing => _delegate.letterSpacing;
@override TextBaseline get textBaseline => _delegate.textBaseline; @override TextBaseline get textBaseline => _delegate.textBaseline;
...@@ -488,7 +489,7 @@ class _TextStyleProxy implements TextStyle { ...@@ -488,7 +489,7 @@ class _TextStyleProxy implements TextStyle {
} }
@override @override
TextStyle copyWith({Color color, String fontFamily, double fontSize, FontWeight fontWeight, FontStyle fontStyle, double letterSpacing, double wordSpacing, TextBaseline textBaseline, double height, TextDecoration decoration, Color decorationColor, TextDecorationStyle decorationStyle, String debugLabel}) { TextStyle copyWith({Color color, String fontFamily, double fontSize, FontWeight fontWeight, FontStyle fontStyle, double letterSpacing, double wordSpacing, TextBaseline textBaseline, double height, ui.Locale locale, TextDecoration decoration, Color decorationColor, TextDecorationStyle decorationStyle, String debugLabel}) {
throw new UnimplementedError(); throw new UnimplementedError();
} }
...@@ -498,7 +499,7 @@ class _TextStyleProxy implements TextStyle { ...@@ -498,7 +499,7 @@ class _TextStyleProxy implements TextStyle {
} }
@override @override
ui.ParagraphStyle getParagraphStyle({TextAlign textAlign, TextDirection textDirection, double textScaleFactor: 1.0, String ellipsis, int maxLines}) { ui.ParagraphStyle getParagraphStyle({TextAlign textAlign, TextDirection textDirection, double textScaleFactor: 1.0, String ellipsis, int maxLines, ui.Locale locale}) {
throw new UnimplementedError(); throw new UnimplementedError();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment