|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'template.dart'; |
| 6 | + |
| 7 | +class TypographyTemplate extends TokenTemplate { |
| 8 | + const TypographyTemplate(String fileName, Map<String, dynamic> tokens) : super(fileName, tokens); |
| 9 | + |
| 10 | + @override |
| 11 | + String generate() => ''' |
| 12 | +// Generated version ${tokens["version"]} |
| 13 | +class _M3Typography { |
| 14 | + _M3Typography._(); |
| 15 | +
|
| 16 | + ${_textTheme('englishLike', 'alphabetic')} |
| 17 | +
|
| 18 | + ${_textTheme('dense', 'ideographic')} |
| 19 | +
|
| 20 | + ${_textTheme('tall', 'alphabetic')} |
| 21 | +} |
| 22 | +'''; |
| 23 | + |
| 24 | + String _textTheme(String name, String baseline) { |
| 25 | + final StringBuffer theme = StringBuffer('static const TextTheme $name = TextTheme(\n'); |
| 26 | + theme.writeln(' displayLarge: ${_textStyleDef('md.sys.typescale.display-large', '$name displayLarge 2021', baseline)},'); |
| 27 | + theme.writeln(' displayMedium: ${_textStyleDef('md.sys.typescale.display-medium', '$name displayMedium 2021', baseline)},'); |
| 28 | + theme.writeln(' displaySmall: ${_textStyleDef('md.sys.typescale.display-small', '$name displaySmall 2021', baseline)},'); |
| 29 | + theme.writeln(' headlineLarge: ${_textStyleDef('md.sys.typescale.headline-large', '$name headlineLarge 2021', baseline)},'); |
| 30 | + theme.writeln(' headlineMedium: ${_textStyleDef('md.sys.typescale.headline-medium', '$name headlineMedium 2021', baseline)},'); |
| 31 | + theme.writeln(' headlineSmall: ${_textStyleDef('md.sys.typescale.headline-small', '$name headlineSmall 2021', baseline)},'); |
| 32 | + theme.writeln(' titleLarge: ${_textStyleDef('md.sys.typescale.title-large', '$name titleLarge 2021', baseline)},'); |
| 33 | + theme.writeln(' titleMedium: ${_textStyleDef('md.sys.typescale.title-medium', '$name titleMedium 2021', baseline)},'); |
| 34 | + theme.writeln(' titleSmall: ${_textStyleDef('md.sys.typescale.title-small', '$name titleSmall 2021', baseline)},'); |
| 35 | + theme.writeln(' labelLarge: ${_textStyleDef('md.sys.typescale.label-large', '$name labelLarge 2021', baseline)},'); |
| 36 | + theme.writeln(' labelMedium: ${_textStyleDef('md.sys.typescale.label-medium', '$name labelMedium 2021', baseline)},'); |
| 37 | + theme.writeln(' labelSmall: ${_textStyleDef('md.sys.typescale.label-small', '$name labelSmall 2021', baseline)},'); |
| 38 | + theme.writeln(' bodyLarge: ${_textStyleDef('md.sys.typescale.body-large', '$name bodyLarge 2021', baseline)},'); |
| 39 | + theme.writeln(' bodyMedium: ${_textStyleDef('md.sys.typescale.body-medium', '$name bodyMedium 2021', baseline)},'); |
| 40 | + theme.writeln(' bodySmall: ${_textStyleDef('md.sys.typescale.body-small', '$name bodySmall 2021', baseline)},'); |
| 41 | + theme.write(' );'); |
| 42 | + return theme.toString(); |
| 43 | + } |
| 44 | + |
| 45 | + String _textStyleDef(String tokenName, String debugLabel, String baseline) { |
| 46 | + final StringBuffer style = StringBuffer("TextStyle(debugLabel: '$debugLabel'"); |
| 47 | + style.write(', inherit: false'); |
| 48 | + style.write(', fontSize: ${_fontSize(tokenName)}'); |
| 49 | + style.write(', fontWeight: ${_fontWeight(tokenName)}'); |
| 50 | + style.write(', letterSpacing: ${_fontSpacing(tokenName)}'); |
| 51 | + style.write(', height: ${_fontHeight(tokenName)}'); |
| 52 | + style.write(', textBaseline: TextBaseline.$baseline'); |
| 53 | + style.write(', leadingDistribution: TextLeadingDistribution.even'); |
| 54 | + style.write(')'); |
| 55 | + return style.toString(); |
| 56 | + } |
| 57 | + |
| 58 | + String _fontSize(String textStyleTokenName) { |
| 59 | + return tokens['$textStyleTokenName.size']!.toString(); |
| 60 | + } |
| 61 | + |
| 62 | + String _fontWeight(String textStyleTokenName) { |
| 63 | + final String weightValue = tokens[tokens['$textStyleTokenName.weight']!]!.toString(); |
| 64 | + return 'FontWeight.w$weightValue'; |
| 65 | + } |
| 66 | + |
| 67 | + String _fontSpacing(String textStyleTokenName) { |
| 68 | + return tokens['$textStyleTokenName.tracking']!.toString(); |
| 69 | + } |
| 70 | + |
| 71 | + String _fontHeight(String textStyleTokenName) { |
| 72 | + final double size = tokens['$textStyleTokenName.size']! as double; |
| 73 | + final double lineHeight = tokens['$textStyleTokenName.line-height']! as double; |
| 74 | + return (lineHeight / size).toStringAsFixed(2); |
| 75 | + } |
| 76 | +} |
0 commit comments