text_style.dart 33.6 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui' as ui show ParagraphStyle, TextStyle, lerpDouble;
6

7 8
import 'package:flutter/foundation.dart';

9
import 'basic_types.dart';
10

11 12
const String _kDefaultDebugLabel = 'unknown';

13 14 15
const String _kColorForegroundWarning = 'Cannot provide both a color and a foreground\n'
         'The color argument is just a shorthand for "foreground: new Paint()..color = color".';

Florian Loitsch's avatar
Florian Loitsch committed
16
/// An immutable style in which paint text.
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
///
/// ## Sample code
///
/// ### Bold
///
/// Here, a single line of text in a [Text] widget is given a specific style
/// override. The style is mixed with the ambient [DefaultTextStyle] by the
/// [Text] widget.
///
/// ```dart
/// new Text(
///   'No, we need bold strokes. We need this plan.',
///   style: new TextStyle(fontWeight: FontWeight.bold),
/// )
/// ```
///
/// ### Italics
///
/// As in the previous example, the [Text] widget is given a specific style
/// override which is implicitly mixed with the ambient [DefaultTextStyle].
///
/// ```dart
/// new Text(
///   'Welcome to the present, we\'re running a real nation.',
///   style: new TextStyle(fontStyle: FontStyle.italic),
/// )
/// ```
///
45
/// ### Opacity and Color
46 47
///
/// Each line here is progressively more opaque. The base color is
48 49 50 51 52
/// [material.Colors.black], and [Color.withOpacity] is used to create a
/// derivative color with the desired opacity. The root [TextSpan] for this
/// [RichText] widget is explicitly given the ambient [DefaultTextStyle], since
/// [RichText] does not do that automatically. The inner [TextStyle] objects are
/// implicitly mixed with the parent [TextSpan]'s [TextSpan.style].
53 54 55
/// 
/// If [color] is specified, [foreground] must be null and vice versa. [color] is
/// treated as a shorthand for `new Paint()..color = color`.
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
///
/// ```dart
/// new RichText(
///   text: new TextSpan(
///     style: DefaultTextStyle.of(context).style,
///     children: <TextSpan>[
///       new TextSpan(
///         text: 'You don\'t have the votes.\n',
///         style: new TextStyle(color: Colors.black.withOpacity(0.6)),
///       ),
///       new TextSpan(
///         text: 'You don\'t have the votes!\n',
///         style: new TextStyle(color: Colors.black.withOpacity(0.8)),
///       ),
///       new TextSpan(
///         text: 'You\'re gonna need congressional approval and you don\'t have the votes!\n',
///         style: new TextStyle(color: Colors.black.withOpacity(1.0)),
///       ),
///     ],
///   ),
/// )
/// ```
///
/// ### Size
///
/// In this example, the ambient [DefaultTextStyle] is explicitly manipulated to
/// obtain a [TextStyle] that doubles the default font size.
///
/// ```dart
/// new Text(
///   'These are wise words, enterprising men quote \'em.',
///   style: DefaultTextStyle.of(context).style.apply(fontSizeFactor: 2.0),
/// )
/// ```
///
/// ### Line height
///
/// The [height] property can be used to change the line height. Here, the line
/// height is set to 100 logical pixels, so that the text is very spaced out.
///
/// ```dart
/// new Text(
///   'Don\'t act surprised, you guys, cuz I wrote \'em!',
///   style: new TextStyle(height: 100.0),
/// )
/// ```
///
/// ### Wavy red underline with black text
///
/// Styles can be combined. In this example, the misspelt word is drawn in black
/// text and underlined with a wavy red line to indicate a spelling error. (The
/// remainder is styled according to the Flutter default text styles, not the
/// ambient [DefaultTextStyle], since no explicit style is given and [RichText]
/// does not automatically use the ambient [DefaultTextStyle].)
///
/// ```dart
/// new RichText(
///   text: new TextSpan(
///     text: 'Don\'t tax the South ',
///     children: <TextSpan>[
///       new TextSpan(
///         text: 'cuz',
///         style: new TextStyle(
///           color: Colors.black,
///           decoration: TextDecoration.underline,
///           decorationColor: Colors.red,
///           decorationStyle: TextDecorationStyle.wavy,
///         ),
///       ),
///       new TextSpan(
///         text: ' we got it made in the shade',
///       ),
///     ],
///   ),
/// )
/// ```
///
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/// ### Custom Fonts
///
/// Custom fonts can be declared in the `pubspec.yaml` file as shown below:
///
///```yaml
/// flutter:
///   fonts:
///     - family: Raleway
///       fonts:
///         - asset: fonts/Raleway-Regular.ttf
///         - asset: fonts/Raleway-Medium.ttf
///           weight: 500
///         - asset: assets/fonts/Raleway-SemiBold.ttf
///           weight: 600
///      - family: Schyler
///        fonts:
///          - asset: fonts/Schyler-Regular.ttf
///          - asset: fonts/Schyler-Italic.ttf
///            style: italic
///```
///
/// The `family` property determines the name of the font, which you can use in
/// the [fontFamily] argument. The `asset` property is a path to the font file,
156
/// relative to the `pubspec.yaml` file. The `weight` property specifies the
157 158
/// weight of the glyph outlines in the file as an integer multiple of 100
/// between 100 and 900. This corresponds to the [FontWeight] class and can be
159
/// used in the [fontWeight] argument. The `style` property specifies whether the
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/// outlines in the file are `italic` or `normal`. These values correspond to
/// the [FontStyle] class and can be used in the [fontStyle] argument.
///
/// To select a custom font, create [TextStyle] using the [fontFamily]
/// argument as shown in the example below:
///
/// ```dart
/// const TextStyle(fontFamily: 'Raleway')
/// ```
///
/// To use a font family defined in a package, the [package] argument must be
/// provided. For instance, suppose the font declaration above is in the
/// `pubspec.yaml` of a package named `my_package` which the app depends on.
/// Then creating the TextStyle is done as follows:
///
/// ```dart
/// const TextStyle(fontFamily: 'Raleway', package: 'my_package')
/// ```
///
179 180
/// If the package internally uses the font it defines, it should still specify
/// the `package` argument when creating the text style as in the example above.
181
///
182 183 184 185
/// A package can also provide font files without declaring a font in its
/// `pubspec.yaml`. These files should then be in the `lib/` folder of the
/// package. The font files will not automatically be bundled in the app, instead
/// the app can use these selectively when declaring a font. Suppose a package
186
/// named `my_package` has:
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
///
/// ```
/// lib/fonts/Raleway-Medium.ttf
/// ```
///
/// Then the app can declare a font like in the example below:
///
///```yaml
/// flutter:
///   fonts:
///     - family: Raleway
///       fonts:
///         - asset: assets/fonts/Raleway-Regular.ttf
///         - asset: packages/my_package/fonts/Raleway-Medium.ttf
///           weight: 500
///```
///
/// The `lib/` is implied, so it should not be included in the asset path.
///
206
/// In this case, since the app locally defines the font, the TextStyle is
207 208 209 210 211 212
/// created without the `package` argument:
///
///```dart
/// const TextStyle(fontFamily: 'Raleway')
/// ```
///
213 214 215 216 217 218 219 220
/// See also:
///
///  * [Text], the widget for showing text in a single style.
///  * [DefaultTextStyle], the widget that specifies the default text styles for
///    [Text] widgets, configured using a [TextStyle].
///  * [RichText], the widget for showing a paragraph of mix-style text.
///  * [TextSpan], the class that wraps a [TextStyle] for the purposes of
///    passing it to a [RichText].
221
@immutable
222
class TextStyle extends Diagnosticable {
223
  /// Creates a text style.
224 225 226 227
  ///
  /// The `package` argument must be non-null if the font family is defined in a
  /// package. It is combined with the `fontFamily` argument to set the
  /// [fontFamily] property.
228
  const TextStyle({
229
    this.inherit = true,
230 231 232
    this.color,
    this.fontSize,
    this.fontWeight,
233
    this.fontStyle,
234
    this.letterSpacing,
235
    this.wordSpacing,
236
    this.textBaseline,
237
    this.height,
238
    this.locale,
239
    this.foreground,
240
    this.background,
241 242
    this.decoration,
    this.decorationColor,
243
    this.decorationStyle,
244
    this.debugLabel,
245 246
    String fontFamily,
    String package,
247
  }) : fontFamily = package == null ? fontFamily : 'packages/$package/$fontFamily',
248 249 250 251
       assert(inherit != null),
       // TODO(dnfield): once https://github.com/dart-lang/sdk/issues/33408 is finished, this can be replaced with
       // assert(color == null || foreground == null, _kColorForegroundWarning);
       assert(identical(color, null) || identical(foreground, null), _kColorForegroundWarning);
252

253

254 255 256 257 258 259
  /// Whether null values are replaced with their value in an ancestor text
  /// style (e.g., in a [TextSpan] tree).
  ///
  /// If this is false, properties that don't have explicit values will revert
  /// to the defaults: white in color, a font size of 10 pixels, in a sans-serif
  /// font face.
260 261
  final bool inherit;

262
  /// The color to use when painting the text.
263 264 265 266 267 268 269
  /// 
  /// If [foreground] is specified, this value must be null. The [color] property
  /// is shorthand for `new Paint()..color = color`.
  ///   
  /// In [merge], [apply], and [lerp], conflicts between [color] and [foreground]
  /// specification are resolved in [foreground]'s favor - i.e. if [foreground] is
  /// specified in one place, it will dominate [color] in another.
270
  final Color color;
271

272 273 274 275 276
  /// The name of the font to use when painting the text (e.g., Roboto). If the
  /// font is defined in a package, this will be prefixed with
  /// 'packages/package_name/' (e.g. 'packages/cool_fonts/Roboto'). The
  /// prefixing is done by the constructor when the `package` argument is
  /// provided.
277
  final String fontFamily;
278

279
  /// The size of glyphs (in logical pixels) to use when painting the text.
280 281 282 283
  ///
  /// During painting, the [fontSize] is multiplied by the current
  /// `textScaleFactor` to let users make it easier to read text by increasing
  /// its size.
284 285 286
  ///
  /// [getParagraphStyle] will default to 14 logical pixels if the font size
  /// isn't specified here.
287 288
  final double fontSize;

289 290 291
  // The default font size if none is specified.
  static const double _defaultFontSize = 14.0;

292
  /// The typeface thickness to use when painting the text (e.g., bold).
293
  final FontWeight fontWeight;
294

295
  /// The typeface variant to use when drawing the letters (e.g., italics).
296
  final FontStyle fontStyle;
297

298
  /// The amount of space (in logical pixels) to add between each letter.
xster's avatar
xster committed
299
  /// A negative value can be used to bring the letters closer.
300 301
  final double letterSpacing;

302 303 304
  /// The amount of space (in logical pixels) to add at each sequence of
  /// white-space (i.e. between each word). A negative value can be used to
  /// bring the words closer.
305 306
  final double wordSpacing;

307 308
  /// 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.
309
  final TextBaseline textBaseline;
310

311 312 313
  /// The height of this text span, as a multiple of the font size.
  ///
  /// If applied to the root [TextSpan], this value sets the line height, which
314 315
  /// is the minimum distance between subsequent text baselines, as multiple of
  /// the font size.
316 317
  final double height;

318
  /// The locale used to select region-specific glyphs.
319 320 321 322 323 324 325
  ///
  /// This property is rarely set. Typically the locale used to select
  /// region-specific glyphs is defined by the text widget's [BuildContext]
  /// using `Localizations.localeOf(context)`. For example [RichText] defines
  /// its locale this way. However, a rich text widget's [TextSpan]s could specify
  /// text styles with different explicit locales in order to select different
  /// region-specifc glyphs for each text span.
326 327
  final Locale locale;

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  /// The paint drawn as a foreground for the text.
  ///
  /// The value should ideally be cached and reused each time if multiple text
  /// styles are created with the same paint settings. Otherwise, each time it
  /// will appear like the style changed, which will result in unnecessary
  /// updates all the way through the framework.
  /// 
  /// If [color] is specified, this value must be null. The [color] property
  /// is shorthand for `new Paint()..color = color`.
  /// 
  /// In [merge], [apply], and [lerp], conflicts between [color] and [foreground]
  /// specification are resolved in [foreground]'s favor - i.e. if [foreground] is
  /// specified in one place, it will dominate [color] in another.
  final Paint foreground;  
  
343 344 345 346 347 348 349
  /// The paint drawn as a background for the text.
  ///
  /// The value should ideally be cached and reused each time if multiple text
  /// styles are created with the same paint settings. Otherwise, each time it
  /// will appear like the style changed, which will result in unnecessary
  /// updates all the way through the framework.
  final Paint background;
350

351
  /// The decorations to paint near the text (e.g., an underline).
352
  final TextDecoration decoration;
353

354
  /// The color in which to paint the text decorations.
355
  final Color decorationColor;
356

357
  /// The style in which to paint the text decorations (e.g., dashed).
358 359
  final TextDecorationStyle decorationStyle;

360 361 362 363 364 365 366 367 368 369 370 371 372
  /// A human-readable description of this text style.
  ///
  /// This property is maintained only in debug builds.
  ///
  /// When merging ([merge]), copying ([copyWith]), modifying using [apply], or
  /// interpolating ([lerp]), the label of the resulting style is marked with
  /// the debug labels of the original styles. This helps figuring out where a
  /// particular text style came from.
  ///
  /// This property is not considered when comparing text styles using `==` or
  /// [compareTo], and it does not affect [hashCode].
  final String debugLabel;

373 374
  /// Creates a copy of this text style but with the given fields replaced with
  /// the new values.
375 376 377
  /// 
  /// One of [color] or [foreground] must be null, and if this has [foreground]
  /// specified it will be given preference over any color parameter.
378 379 380 381 382
  TextStyle copyWith({
    Color color,
    String fontFamily,
    double fontSize,
    FontWeight fontWeight,
383
    FontStyle fontStyle,
384
    double letterSpacing,
385
    double wordSpacing,
386
    TextBaseline textBaseline,
387
    double height,
388
    Locale locale,
389
    Paint foreground,
390
    Paint background,
391
    TextDecoration decoration,
392
    Color decorationColor,
393
    TextDecorationStyle decorationStyle,
394
    String debugLabel,
395
  }) {
396
    assert(color == null || foreground == null, _kColorForegroundWarning);
397 398 399
    String newDebugLabel;
    assert(() {
      if (this.debugLabel != null)
400
        newDebugLabel = debugLabel ?? '(${this.debugLabel}).copyWith';
401 402
      return true;
    }());
403
    return new TextStyle(
404
      inherit: inherit,
405
      color: this.foreground == null && foreground == null ? color ?? this.color : null,
Ian Hickson's avatar
Ian Hickson committed
406 407 408 409 410 411 412 413
      fontFamily: fontFamily ?? this.fontFamily,
      fontSize: fontSize ?? this.fontSize,
      fontWeight: fontWeight ?? this.fontWeight,
      fontStyle: fontStyle ?? this.fontStyle,
      letterSpacing: letterSpacing ?? this.letterSpacing,
      wordSpacing: wordSpacing ?? this.wordSpacing,
      textBaseline: textBaseline ?? this.textBaseline,
      height: height ?? this.height,
414
      locale: locale ?? this.locale,
415
      foreground: foreground ?? this.foreground,
416
      background: background ?? this.background,
Ian Hickson's avatar
Ian Hickson committed
417 418
      decoration: decoration ?? this.decoration,
      decorationColor: decorationColor ?? this.decorationColor,
419
      decorationStyle: decorationStyle ?? this.decorationStyle,
420
      debugLabel: newDebugLabel,
421 422 423
    );
  }

424 425 426 427 428
  /// Creates a copy of this text style replacing or altering the specified
  /// properties.
  ///
  /// The non-numeric properties [color], [fontFamily], [decoration],
  /// [decorationColor] and [decorationStyle] are replaced with the new values.
429 430
  /// 
  /// [foreground] will be given preference over [color] if it is not null.
431 432 433
  ///
  /// The numeric properties are multiplied by the given factors and then
  /// incremented by the given deltas.
Ian Hickson's avatar
Ian Hickson committed
434
  ///
435 436
  /// For example, `style.apply(fontSizeFactor: 2.0, fontSizeDelta: 1.0)` would
  /// return a [TextStyle] whose [fontSize] is `style.fontSize * 2.0 + 1.0`.
Ian Hickson's avatar
Ian Hickson committed
437 438 439 440 441 442
  ///
  /// For the [fontWeight], the delta is applied to the [FontWeight] enum index
  /// values, so that for instance `style.apply(fontWeightDelta: -2)` when
  /// applied to a `style` whose [fontWeight] is [FontWeight.w500] will return a
  /// [TextStyle] with a [FontWeight.w300].
  ///
443
  /// The numeric arguments must not be null.
Ian Hickson's avatar
Ian Hickson committed
444 445 446
  ///
  /// If the underlying values are null, then the corresponding factors and/or
  /// deltas must not be specified.
447 448 449
  /// 
  /// If [foreground] is specified on this object, then applying [color] here
  /// will have no effect.
Ian Hickson's avatar
Ian Hickson committed
450
  TextStyle apply({
451
    Color color,
452 453 454
    TextDecoration decoration,
    Color decorationColor,
    TextDecorationStyle decorationStyle,
455
    String fontFamily,
456 457 458 459 460 461 462 463 464
    double fontSizeFactor = 1.0,
    double fontSizeDelta = 0.0,
    int fontWeightDelta = 0,
    double letterSpacingFactor = 1.0,
    double letterSpacingDelta = 0.0,
    double wordSpacingFactor = 1.0,
    double wordSpacingDelta = 0.0,
    double heightFactor = 1.0,
    double heightDelta = 0.0,
Ian Hickson's avatar
Ian Hickson committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  }) {
    assert(fontSizeFactor != null);
    assert(fontSizeDelta != null);
    assert(fontSize != null || (fontSizeFactor == 1.0 && fontSizeDelta == 0.0));
    assert(fontWeightDelta != null);
    assert(fontWeight != null || fontWeightDelta == 0.0);
    assert(letterSpacingFactor != null);
    assert(letterSpacingDelta != null);
    assert(letterSpacing != null || (letterSpacingFactor == 1.0 && letterSpacingDelta == 0.0));
    assert(wordSpacingFactor != null);
    assert(wordSpacingDelta != null);
    assert(wordSpacing != null || (wordSpacingFactor == 1.0 && wordSpacingDelta == 0.0));
    assert(heightFactor != null);
    assert(heightDelta != null);
    assert(heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0));
480 481 482 483

    String modifiedDebugLabel;
    assert(() {
      if (debugLabel != null)
484
        modifiedDebugLabel = '($debugLabel).apply';
485 486 487
      return true;
    }());

Ian Hickson's avatar
Ian Hickson committed
488 489
    return new TextStyle(
      inherit: inherit,
490
      color: foreground == null ? color ?? this.color : null,
491
      fontFamily: fontFamily ?? this.fontFamily,
Ian Hickson's avatar
Ian Hickson committed
492 493 494 495 496 497 498
      fontSize: fontSize == null ? null : fontSize * fontSizeFactor + fontSizeDelta,
      fontWeight: fontWeight == null ? null : FontWeight.values[(fontWeight.index + fontWeightDelta).clamp(0, FontWeight.values.length - 1)],
      fontStyle: fontStyle,
      letterSpacing: letterSpacing == null ? null : letterSpacing * letterSpacingFactor + letterSpacingDelta,
      wordSpacing: wordSpacing == null ? null : wordSpacing * wordSpacingFactor + wordSpacingDelta,
      textBaseline: textBaseline,
      height: height == null ? null : height * heightFactor + heightDelta,
499
      locale: locale,
500
      foreground: foreground != null ? foreground : null,
501
      background: background,
502 503 504 505
      decoration: decoration ?? this.decoration,
      decorationColor: decorationColor ?? this.decorationColor,
      decorationStyle: decorationStyle ?? this.decorationStyle,
      debugLabel: modifiedDebugLabel,
Ian Hickson's avatar
Ian Hickson committed
506 507 508
    );
  }

509 510 511 512 513 514 515 516 517 518 519 520 521 522
  /// Returns a new text style that is a combination of this style and the given
  /// [other] style.
  ///
  /// If the given [other] text style has its [TextStyle.inherit] set to true,
  /// its null properties are replaced with the non-null properties of this text
  /// style. The [other] style _inherits_ the properties of this style. Another
  /// way to think of it is that the "missing" properties of the [other] style
  /// are _filled_ by the properties of this style.
  ///
  /// If the given [other] text style has its [TextStyle.inherit] set to false,
  /// returns the given [other] style unchanged. The [other] style does not
  /// inherit properties of this style.
  ///
  /// If the given text style is null, returns this text style.
523 524 525
  /// 
  /// One of [color] or [foreground] must be null, and if this or `other` has
  /// [foreground] specified it will be given preference over any color parameter.
526
  TextStyle merge(TextStyle other) {
527 528
    if (other == null)
      return this;
529 530 531 532 533 534
    if (!other.inherit)
      return other;

    String mergedDebugLabel;
    assert(() {
      if (other.debugLabel != null || debugLabel != null)
535
        mergedDebugLabel = '(${debugLabel ?? _kDefaultDebugLabel}).merge(${other.debugLabel ?? _kDefaultDebugLabel})';
536 537 538
      return true;
    }());

539 540 541 542 543
    return copyWith(
      color: other.color,
      fontFamily: other.fontFamily,
      fontSize: other.fontSize,
      fontWeight: other.fontWeight,
544
      fontStyle: other.fontStyle,
545
      letterSpacing: other.letterSpacing,
546
      wordSpacing: other.wordSpacing,
547
      textBaseline: other.textBaseline,
548
      height: other.height,
549
      locale: other.locale,
550
      foreground: other.foreground,
551
      background: other.background,
552 553
      decoration: other.decoration,
      decorationColor: other.decorationColor,
554 555
      decorationStyle: other.decorationStyle,
      debugLabel: mergedDebugLabel,
556 557 558
    );
  }

559 560 561
  /// Interpolate between two text styles.
  ///
  /// This will not work well if the styles don't set the same fields.
562 563 564 565 566 567 568 569 570 571 572 573
  ///
  /// The `t` argument represents position on the timeline, with 0.0 meaning
  /// that the interpolation has not started, returning `a` (or something
  /// equivalent to `a`), 1.0 meaning that the interpolation has finished,
  /// returning `b` (or something equivalent to `b`), and values in between
  /// meaning that the interpolation is at the relevant point on the timeline
  /// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
  /// 1.0, so negative values and values greater than 1.0 are valid (and can
  /// easily be generated by curves such as [Curves.elasticInOut]).
  ///
  /// Values for `t` are usually obtained from an [Animation<double>], such as
  /// an [AnimationController].
574 575 576 577
  /// 
  /// If [foreground] is specified on either of `a` or `b`, both will be treated
  /// as if they have a [foreground] paint (creating a new [Paint] if necessary
  /// based on the [color] property).
578 579
  static TextStyle lerp(TextStyle a, TextStyle b, double t) {
    assert(t != null);
580 581 582 583
    assert(a == null || b == null || a.inherit == b.inherit);
    if (a == null && b == null) {
      return null;
    }
584 585 586

    String lerpDebugLabel;
    assert(() {
587
      lerpDebugLabel = 'lerp(${a?.debugLabel ?? _kDefaultDebugLabel}${t.toStringAsFixed(1)}${b?.debugLabel ?? _kDefaultDebugLabel})';
588 589 590
      return true;
    }());

591 592 593 594 595 596 597 598 599 600 601 602 603
    if (a == null) {
      return new TextStyle(
        inherit: b.inherit,
        color: Color.lerp(null, b.color, t),
        fontFamily: t < 0.5 ? null : b.fontFamily,
        fontSize: t < 0.5 ? null : b.fontSize,
        fontWeight: FontWeight.lerp(null, b.fontWeight, t),
        fontStyle: t < 0.5 ? null : b.fontStyle,
        letterSpacing: t < 0.5 ? null : b.letterSpacing,
        wordSpacing: t < 0.5 ? null : b.wordSpacing,
        textBaseline: t < 0.5 ? null : b.textBaseline,
        height: t < 0.5 ? null : b.height,
        locale: t < 0.5 ? null : b.locale,
604
        foreground: t < 0.5 ? null : b.foreground,
605
        background: t < 0.5 ? null : b.background,
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
        decoration: t < 0.5 ? null : b.decoration,
        decorationColor: Color.lerp(null, b.decorationColor, t),
        decorationStyle: t < 0.5 ? null : b.decorationStyle,
        debugLabel: lerpDebugLabel,
      );
    }

    if (b == null) {
      return new TextStyle(
        inherit: a.inherit,
        color: Color.lerp(a.color, null, t),
        fontFamily: t < 0.5 ? a.fontFamily : null,
        fontSize: t < 0.5 ? a.fontSize : null,
        fontWeight: FontWeight.lerp(a.fontWeight, null, t),
        fontStyle: t < 0.5 ? a.fontStyle : null,
        letterSpacing: t < 0.5 ? a.letterSpacing : null,
        wordSpacing: t < 0.5 ? a.wordSpacing : null,
        textBaseline: t < 0.5 ? a.textBaseline : null,
        height: t < 0.5 ? a.height : null,
        locale: t < 0.5 ? a.locale : null,
626
        foreground: t < 0.5 ? a.foreground : null,
627
        background: t < 0.5 ? a.background : null,
628 629 630 631 632 633 634
        decoration: t < 0.5 ? a.decoration : null,
        decorationColor: Color.lerp(a.decorationColor, null, t),
        decorationStyle: t < 0.5 ? a.decorationStyle : null,
        debugLabel: lerpDebugLabel,
      );
    }

635
    return new TextStyle(
636
      inherit: b.inherit,
637
      color: a.foreground == null && b.foreground == null ? Color.lerp(a.color, b.color, t) : null,
638 639 640 641 642 643 644 645
      fontFamily: t < 0.5 ? a.fontFamily : b.fontFamily,
      fontSize: ui.lerpDouble(a.fontSize ?? b.fontSize, b.fontSize ?? a.fontSize, t),
      fontWeight: FontWeight.lerp(a.fontWeight, b.fontWeight, t),
      fontStyle: t < 0.5 ? a.fontStyle : b.fontStyle,
      letterSpacing: ui.lerpDouble(a.letterSpacing ?? b.letterSpacing, b.letterSpacing ?? a.letterSpacing, t),
      wordSpacing: ui.lerpDouble(a.wordSpacing ?? b.wordSpacing, b.wordSpacing ?? a.wordSpacing, t),
      textBaseline: t < 0.5 ? a.textBaseline : b.textBaseline,
      height: ui.lerpDouble(a.height ?? b.height, b.height ?? a.height, t),
646
      locale: t < 0.5 ? a.locale : b.locale,
647 648 649 650 651
      foreground: (a.foreground != null || b.foreground != null)
        ? t < 0.5 
          ? a.foreground ?? (new Paint()..color = a.color)
          : b.foreground ?? (new Paint()..color = b.color)
        : null,
652
      background: t < 0.5 ? a.background : b.background,
653 654 655
      decoration: t < 0.5 ? a.decoration : b.decoration,
      decorationColor: Color.lerp(a.decorationColor, b.decorationColor, t),
      decorationStyle: t < 0.5 ? a.decorationStyle : b.decorationStyle,
656
      debugLabel: lerpDebugLabel,
657 658
    );
  }
659

660
  /// The style information for text runs, encoded for use by `dart:ui`.
661
  ui.TextStyle getTextStyle({ double textScaleFactor = 1.0 }) {
662 663 664 665 666 667 668
    return new ui.TextStyle(
      color: color,
      decoration: decoration,
      decorationColor: decorationColor,
      decorationStyle: decorationStyle,
      fontWeight: fontWeight,
      fontStyle: fontStyle,
669
      textBaseline: textBaseline,
670
      fontFamily: fontFamily,
671
      fontSize: fontSize == null ? null : fontSize * textScaleFactor,
672 673
      letterSpacing: letterSpacing,
      wordSpacing: wordSpacing,
674 675
      height: height,
      locale: locale,
676
      foreground: foreground,
677
      background: background,
678 679 680
    );
  }

681
  /// The style information for paragraphs, encoded for use by `dart:ui`.
682 683 684 685
  ///
  /// The `textScaleFactor` argument must not be null. If omitted, it defaults
  /// to 1.0. The other arguments may be null. The `maxLines` argument, if
  /// specified and non-null, must be greater than zero.
686 687 688
  ///
  /// If the font size on this style isn't set, it will default to 14 logical
  /// pixels.
689 690
  ui.ParagraphStyle getParagraphStyle({
      TextAlign textAlign,
Ian Hickson's avatar
Ian Hickson committed
691
      TextDirection textDirection,
692
      double textScaleFactor = 1.0,
693
      String ellipsis,
694
      int maxLines,
695
      Locale locale,
696 697 698
  }) {
    assert(textScaleFactor != null);
    assert(maxLines == null || maxLines > 0);
699
    return new ui.ParagraphStyle(
700
      textAlign: textAlign,
Ian Hickson's avatar
Ian Hickson committed
701
      textDirection: textDirection,
Adam Barth's avatar
Adam Barth committed
702 703 704
      fontWeight: fontWeight,
      fontStyle: fontStyle,
      fontFamily: fontFamily,
705
      fontSize: (fontSize ?? _defaultFontSize) * textScaleFactor,
706
      lineHeight: height,
707
      maxLines: maxLines,
708
      ellipsis: ellipsis,
709
      locale: locale,
710 711
    );
  }
712

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
  /// Describe the difference between this style and another, in terms of how
  /// much damage it will make to the rendering.
  ///
  /// See also:
  ///
  ///  * [TextSpan.compareTo], which does the same thing for entire [TextSpan]s.
  RenderComparison compareTo(TextStyle other) {
    if (identical(this, other))
      return RenderComparison.identical;
    if (inherit != other.inherit ||
        fontFamily != other.fontFamily ||
        fontSize != other.fontSize ||
        fontWeight != other.fontWeight ||
        fontStyle != other.fontStyle ||
        letterSpacing != other.letterSpacing ||
        wordSpacing != other.wordSpacing ||
        textBaseline != other.textBaseline ||
730
        height != other.height ||
731
        locale != other.locale ||
732
        foreground != other.foreground ||
733
        background != other.background)
734 735 736 737 738 739 740 741 742
      return RenderComparison.layout;
    if (color != other.color ||
        decoration != other.decoration ||
        decorationColor != other.decorationColor ||
        decorationStyle != other.decorationStyle)
      return RenderComparison.paint;
    return RenderComparison.identical;
  }

743
  @override
Hixie's avatar
Hixie committed
744
  bool operator ==(dynamic other) {
745 746
    if (identical(this, other))
      return true;
Ian Hickson's avatar
Ian Hickson committed
747
    if (other.runtimeType != runtimeType)
Hixie's avatar
Hixie committed
748 749
      return false;
    final TextStyle typedOther = other;
750 751
    return inherit == typedOther.inherit &&
           color == typedOther.color &&
Hixie's avatar
Hixie committed
752 753 754 755
           fontFamily == typedOther.fontFamily &&
           fontSize == typedOther.fontSize &&
           fontWeight == typedOther.fontWeight &&
           fontStyle == typedOther.fontStyle &&
756
           letterSpacing == typedOther.letterSpacing &&
757
           wordSpacing == typedOther.wordSpacing &&
Hixie's avatar
Hixie committed
758
           textBaseline == typedOther.textBaseline &&
759
           height == typedOther.height &&
760
           locale == typedOther.locale &&
761
           foreground == typedOther.foreground &&
762
           background == typedOther.background &&
Hixie's avatar
Hixie committed
763 764 765
           decoration == typedOther.decoration &&
           decorationColor == typedOther.decorationColor &&
           decorationStyle == typedOther.decorationStyle;
766 767
  }

768
  @override
769
  int get hashCode {
770
    return hashValues(
771
      inherit,
772 773 774 775 776 777
      color,
      fontFamily,
      fontSize,
      fontWeight,
      fontStyle,
      letterSpacing,
778
      wordSpacing,
779
      textBaseline,
780
      height,
781
      locale,
782
      foreground,
783
      background,
784 785 786 787
      decoration,
      decorationColor,
      decorationStyle
    );
788 789
  }

790
  @override
791
  String toStringShort() => '$runtimeType';
792 793

  /// Adds all properties prefixing property names with the optional `prefix`.
794
  @override
795
  void debugFillProperties(DiagnosticPropertiesBuilder properties, { String prefix = '' }) {
796
    super.debugFillProperties(properties);
797 798
    if (debugLabel != null)
      properties.add(new MessageProperty('${prefix}debugLabel', debugLabel));
799 800 801 802 803
    final List<DiagnosticsNode> styles = <DiagnosticsNode>[];
    styles.add(new DiagnosticsProperty<Color>('${prefix}color', color, defaultValue: null));
    styles.add(new StringProperty('${prefix}family', fontFamily, defaultValue: null, quoted: false));
    styles.add(new DoubleProperty('${prefix}size', fontSize, defaultValue: null));
    String weightDescription;
Hixie's avatar
Hixie committed
804 805 806
    if (fontWeight != null) {
      switch (fontWeight) {
        case FontWeight.w100:
807
          weightDescription = '100';
Hixie's avatar
Hixie committed
808 809
          break;
        case FontWeight.w200:
810
          weightDescription = '200';
Hixie's avatar
Hixie committed
811 812
          break;
        case FontWeight.w300:
813
          weightDescription = '300';
Hixie's avatar
Hixie committed
814 815
          break;
        case FontWeight.w400:
816
          weightDescription = '400';
Hixie's avatar
Hixie committed
817 818
          break;
        case FontWeight.w500:
819
          weightDescription = '500';
Hixie's avatar
Hixie committed
820 821
          break;
        case FontWeight.w600:
822
          weightDescription = '600';
Hixie's avatar
Hixie committed
823 824
          break;
        case FontWeight.w700:
825
          weightDescription = '700';
Hixie's avatar
Hixie committed
826 827
          break;
        case FontWeight.w800:
828
          weightDescription = '800';
Hixie's avatar
Hixie committed
829 830
          break;
        case FontWeight.w900:
831
          weightDescription = '900';
Hixie's avatar
Hixie committed
832 833 834
          break;
      }
    }
835 836 837 838 839 840 841 842 843 844
    // TODO(jacobr): switch this to use enumProperty which will either cause the
    // weight description to change to w600 from 600 or require existing
    // enumProperty to handle this special case.
    styles.add(new DiagnosticsProperty<FontWeight>(
      '${prefix}weight',
      fontWeight,
      description: weightDescription,
      defaultValue: null,
    ));
    styles.add(new EnumProperty<FontStyle>('${prefix}style', fontStyle, defaultValue: null));
Ian Hickson's avatar
Ian Hickson committed
845 846
    styles.add(new DoubleProperty('${prefix}letterSpacing', letterSpacing, defaultValue: null));
    styles.add(new DoubleProperty('${prefix}wordSpacing', wordSpacing, defaultValue: null));
847 848
    styles.add(new EnumProperty<TextBaseline>('${prefix}baseline', textBaseline, defaultValue: null));
    styles.add(new DoubleProperty('${prefix}height', height, unit: 'x', defaultValue: null));
849 850 851
    styles.add(new DiagnosticsProperty<Locale>('${prefix}locale', locale, defaultValue: null));
    styles.add(new DiagnosticsProperty<Paint>('${prefix}foreground', foreground, defaultValue: null));
    styles.add(new DiagnosticsProperty<Paint>('${prefix}background', background, defaultValue: null));
Hixie's avatar
Hixie committed
852
    if (decoration != null || decorationColor != null || decorationStyle != null) {
853 854
      final List<String> decorationDescription = <String>[];
      if (decorationStyle != null)
855
        decorationDescription.add(describeEnum(decorationStyle));
856 857 858

      // Hide decorationColor from the default text view as it is shown in the
      // terse decoration summary as well.
859
      styles.add(new DiagnosticsProperty<Color>('${prefix}decorationColor', decorationColor, defaultValue: null, level: DiagnosticLevel.fine));
860 861 862 863 864 865 866

      if (decorationColor != null)
        decorationDescription.add('$decorationColor');

      // Intentionally collide with the property 'decoration' added below.
      // Tools that show hidden properties could choose the first property
      // matching the name to disambiguate.
867
      styles.add(new DiagnosticsProperty<TextDecoration>('${prefix}decoration', decoration, defaultValue: null, level: DiagnosticLevel.hidden));
868 869 870 871
      if (decoration != null)
        decorationDescription.add('$decoration');
      assert(decorationDescription.isNotEmpty);
      styles.add(new MessageProperty('${prefix}decoration', decorationDescription.join(' ')));
Hixie's avatar
Hixie committed
872
    }
873

874 875
    final bool styleSpecified = styles.any((DiagnosticsNode n) => !n.isFiltered(DiagnosticLevel.info));
    properties.add(new DiagnosticsProperty<bool>('${prefix}inherit', inherit, level: (!styleSpecified && inherit) ? DiagnosticLevel.fine : DiagnosticLevel.info));
876
    styles.forEach(properties.add);
877

878 879
    if (!styleSpecified)
      properties.add(new FlagProperty('inherit', value: inherit, ifTrue: '$prefix<all styles inherited>', ifFalse: '$prefix<no style specified>'));
880 881
  }
}