|
1 | 1 | import 'dart:io';
|
2 | 2 | import 'dart:ui';
|
3 | 3 | import 'package:flutter/foundation.dart';
|
4 |
| -import 'package:flutter/widgets.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | + |
| 6 | +/// An app-wide [Typography] for Zulip, customized from the Material default. |
| 7 | +/// |
| 8 | +/// Include this in the app-wide [MaterialApp.theme]. |
| 9 | +/// |
| 10 | +/// We expect these text styles to be the basis of all the styles chosen by the |
| 11 | +/// Material library's widgets, such as the default styling of |
| 12 | +/// an [AppBar]'s title, of an [ElevatedButton]'s label, and so on. |
| 13 | +/// |
| 14 | +/// In all the comprised [TextStyle]s, this sets: |
| 15 | +/// |
| 16 | +/// - [TextStyle.fontFamily] to [kDefaultFontFamily], and |
| 17 | +/// - [TextStyle.fontFamilyFallback] to [kDefaultFontFamilyFallback]. |
| 18 | +/// |
| 19 | +/// Since [kDefaultFontFamily] is a variable-weight font, |
| 20 | +/// each [TextStyle] needs some processing, which this provides, |
| 21 | +/// in order to correctly specify the font weight. |
| 22 | +/// |
| 23 | +/// When building on top of these [TextStyles], callers that wish to specify |
| 24 | +/// a different font weight are still responsible for reprocessing the style |
| 25 | +/// with [weightVariableTextStyle] before passing it to a [Text]. |
| 26 | +/// (Widgets in the Material library won't do this; they aren't yet equipped |
| 27 | +/// to set font weights on variable-weight fonts. If this causes visible bugs, |
| 28 | +/// we should investigate and fix, but they should become less likely as |
| 29 | +/// we transition from Material's widgets to our own bespoke ones.) |
| 30 | +Typography zulipTypography(BuildContext context) { |
| 31 | + final typography = Theme.of(context).typography; |
| 32 | + return weightVariableTypography(context, typography) |
| 33 | + .copyWith( |
| 34 | + black: typography.black.apply( |
| 35 | + fontFamily: kDefaultFontFamily, |
| 36 | + fontFamilyFallback: kDefaultFontFamilyFallback), |
| 37 | + white: typography.white.apply( |
| 38 | + fontFamily: kDefaultFontFamily, |
| 39 | + fontFamilyFallback: kDefaultFontFamilyFallback), |
| 40 | + ); |
| 41 | +} |
5 | 42 |
|
6 | 43 | /// The [TextStyle.fontFamily] to use in most of the app.
|
7 | 44 | ///
|
@@ -134,3 +171,75 @@ FontWeight clampVariableFontWeight(double wght) {
|
134 | 171 | }
|
135 | 172 | }
|
136 | 173 | }
|
| 174 | + |
| 175 | +/// Convert an input [Typography] to one that works with "wght"-variable fonts. |
| 176 | +/// |
| 177 | +/// Processes the "geometry" [TextTheme] fields |
| 178 | +/// ([Typography.dense], [Typography.englishLike], and [Typography.tall]) |
| 179 | +/// with [weightVariableTextTheme], passing along the [BuildContext]. |
| 180 | +Typography weightVariableTypography(BuildContext context, Typography input) => |
| 181 | + input.copyWith( |
| 182 | + dense: weightVariableTextTheme(context, input.dense), |
| 183 | + englishLike: weightVariableTextTheme(context, input.englishLike), |
| 184 | + tall: weightVariableTextTheme(context, input.tall), |
| 185 | + ); |
| 186 | + |
| 187 | +/// Convert a geometry [TextTheme] to one that works with "wght"-variable fonts. |
| 188 | +/// |
| 189 | +/// A "geometry [TextTheme]" is a [TextTheme] that's meant to specify |
| 190 | +/// font weight and other parameters about shape, size, distance, etc. |
| 191 | +/// See [Typography]. |
| 192 | +/// |
| 193 | +/// This looks at each of `input`'s fields, such as [TextTheme.bodyMedium], and: |
| 194 | +/// |
| 195 | +/// If the field is non-null, calls [TextStyle.merge], passing the result of |
| 196 | +/// [weightVariableTextStyle] called with: |
| 197 | +/// - this function's [BuildContext] param, and |
| 198 | +/// - for `wght`: |
| 199 | +/// - if present, the field's [TextStyle.fontWeight] |
| 200 | +/// (via [wghtFromFontWeight]), else |
| 201 | +/// - null. |
| 202 | +/// |
| 203 | +/// If the field is null, gives null. |
| 204 | +/// |
| 205 | +/// This assumes that if a non-null field omits [TextStyle.fontWeight], then |
| 206 | +/// the normal font weight is implied, and it will be expressed in the output. |
| 207 | +TextTheme weightVariableTextTheme(BuildContext context, TextTheme input) { |
| 208 | + TextStyle? convert(TextStyle? maybeInputStyle) { |
| 209 | + if (maybeInputStyle == null) { |
| 210 | + return null; |
| 211 | + } |
| 212 | + final maybeInputFontWeight = maybeInputStyle.fontWeight; |
| 213 | + return maybeInputStyle.merge(weightVariableTextStyle(context, |
| 214 | + wght: maybeInputFontWeight != null |
| 215 | + ? wghtFromFontWeight(maybeInputFontWeight) |
| 216 | + : null)); |
| 217 | + } |
| 218 | + |
| 219 | + return TextTheme( |
| 220 | + displayLarge: convert(input.displayLarge), |
| 221 | + displayMedium: convert(input.displayMedium), |
| 222 | + displaySmall: convert(input.displaySmall), |
| 223 | + headlineLarge: convert(input.headlineLarge), |
| 224 | + headlineMedium: convert(input.headlineMedium), |
| 225 | + headlineSmall: convert(input.headlineSmall), |
| 226 | + titleLarge: convert(input.titleLarge), |
| 227 | + titleMedium: convert(input.titleMedium), |
| 228 | + titleSmall: convert(input.titleSmall), |
| 229 | + bodyLarge: convert(input.bodyLarge), |
| 230 | + bodyMedium: convert(input.bodyMedium), |
| 231 | + bodySmall: convert(input.bodySmall), |
| 232 | + labelLarge: convert(input.labelLarge), |
| 233 | + labelMedium: convert(input.labelMedium), |
| 234 | + labelSmall: convert(input.labelSmall), |
| 235 | + ); |
| 236 | +} |
| 237 | + |
| 238 | +/// A good guess at a font's "wght" value to match a given [FontWeight]. |
| 239 | +/// |
| 240 | +/// Returns [FontWeight.value] as a double. |
| 241 | +/// |
| 242 | +/// This might not be exactly where the font designer would land on their |
| 243 | +/// font's own custom-defined "wght" axis. But it's a great guess, |
| 244 | +/// at least without knowledge of the particular font. |
| 245 | +double wghtFromFontWeight(FontWeight fontWeight) => fontWeight.value.toDouble(); |
0 commit comments