From 4b0c8127073d91d15b36a1428fd23b4cf702cae9 Mon Sep 17 00:00:00 2001 From: Ruslan Lekhman Date: Fri, 12 Aug 2022 19:41:57 -0600 Subject: [PATCH] feat(core): make font style, weight, scale params optional (#9993) Change font style, weight, scale constructor parameters to optional. Export `FontStyle` and `FontWeight`. --- packages/core/ui/index.ts | 2 +- packages/core/ui/styling/font-common.ts | 9 ++++++++- packages/core/ui/styling/font.android.ts | 8 ++++---- packages/core/ui/styling/font.d.ts | 2 +- packages/core/ui/styling/font.ios.ts | 6 ++---- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/core/ui/index.ts b/packages/core/ui/index.ts index 3424c7735..f2fa035e5 100644 --- a/packages/core/ui/index.ts +++ b/packages/core/ui/index.ts @@ -59,7 +59,7 @@ export { Background } from './styling/background'; export type { CacheMode } from './styling/background'; export { parseCSSShadow } from './styling/css-shadow'; export { animationTimingFunctionConverter, timeConverter } from './styling/converters'; -export { Font } from './styling/font'; +export { Font, FontStyle, FontWeight } from './styling/font'; export { Style } from './styling/style'; export type { CommonLayoutParams } from './styling/style'; export * from './styling/style-properties'; diff --git a/packages/core/ui/styling/font-common.ts b/packages/core/ui/styling/font-common.ts index 782cfb013..9d10e275c 100644 --- a/packages/core/ui/styling/font-common.ts +++ b/packages/core/ui/styling/font-common.ts @@ -6,6 +6,9 @@ export * from './font-interfaces'; export abstract class Font implements FontDefinition { public static default = undefined; + public readonly fontStyle: FontStyleType; + public readonly fontWeight: FontWeightType; + public readonly fontScale: number; get isItalic(): boolean { return this.fontStyle === FontStyle.ITALIC; @@ -15,7 +18,11 @@ export abstract class Font implements FontDefinition { return this.fontWeight === FontWeight.SEMI_BOLD || this.fontWeight === FontWeight.BOLD || this.fontWeight === '700' || this.fontWeight === FontWeight.EXTRA_BOLD || this.fontWeight === FontWeight.BLACK; } - protected constructor(public readonly fontFamily: string, public readonly fontSize: number, public readonly fontStyle: FontStyleType, public readonly fontWeight: FontWeightType, public readonly fontScale: number) {} + protected constructor(public readonly fontFamily: string, public readonly fontSize: number, fontStyle?: FontStyleType, fontWeight?: FontWeightType, fontScale?: number) { + this.fontStyle = fontStyle ?? FontStyle.NORMAL; + this.fontWeight = fontWeight ?? FontWeight.NORMAL; + this.fontScale = fontScale ?? 1; + } public abstract getAndroidTypeface(): any; /* android.graphics.Typeface */ public abstract getUIFont(defaultFont: any /* UIFont */): any; /* UIFont */ diff --git a/packages/core/ui/styling/font.android.ts b/packages/core/ui/styling/font.android.ts index d67b7764a..c10d4ec8c 100644 --- a/packages/core/ui/styling/font.android.ts +++ b/packages/core/ui/styling/font.android.ts @@ -1,4 +1,4 @@ -import { Font as FontBase, parseFontFamily, genericFontFamilies, FontWeight, FontWeightType } from './font-common'; +import { Font as FontBase, parseFontFamily, genericFontFamilies, FontStyleType, FontWeight, FontWeightType } from './font-common'; import { Trace } from '../../trace'; import * as application from '../../application'; import * as fs from '../../file-system'; @@ -10,11 +10,11 @@ const typefaceCache = new Map(); let appAssets: android.content.res.AssetManager; export class Font extends FontBase { - public static default = new Font(undefined, undefined, 'normal', 'normal'); + public static default = new Font(undefined, undefined); private _typeface: android.graphics.Typeface; - constructor(family: string, size: number, style: 'normal' | 'italic', weight: FontWeightType) { + constructor(family: string, size: number, style?: FontStyleType, weight?: FontWeightType) { super(family, size, style, weight, 1); } @@ -22,7 +22,7 @@ export class Font extends FontBase { return new Font(family, this.fontSize, this.fontStyle, this.fontWeight); } - public withFontStyle(style: 'normal' | 'italic'): Font { + public withFontStyle(style: FontStyleType): Font { return new Font(this.fontFamily, this.fontSize, style, this.fontWeight); } diff --git a/packages/core/ui/styling/font.d.ts b/packages/core/ui/styling/font.d.ts index 5e1715685..4d6650e6b 100644 --- a/packages/core/ui/styling/font.d.ts +++ b/packages/core/ui/styling/font.d.ts @@ -10,7 +10,7 @@ public isBold: boolean; public isItalic: boolean; - constructor(family: string, size: number, style: FontStyle, weight: FontWeight); + constructor(family: string, size: number, style?: FontStyle, weight?: FontWeight, scale?: number); public getAndroidTypeface(): any /* android.graphics.Typeface */; public getUIFont(defaultFont: any /* UIFont */): any /* UIFont */; diff --git a/packages/core/ui/styling/font.ios.ts b/packages/core/ui/styling/font.ios.ts index aa7535804..b8e163bfd 100644 --- a/packages/core/ui/styling/font.ios.ts +++ b/packages/core/ui/styling/font.ios.ts @@ -38,11 +38,9 @@ function getUIFontCached(fontDescriptor: FontDescriptor) { } export class Font extends FontBase { - public static default = new Font(undefined, undefined, FontStyle.NORMAL, FontWeight.NORMAL, 1); + public static default = new Font(undefined, undefined); - private _uiFont: UIFont; - - constructor(family: string, size: number, style: FontStyleType, weight: FontWeightType, scale: number) { + constructor(family: string, size: number, style?: FontStyleType, weight?: FontWeightType, scale?: number) { super(family, size, style, weight, scale); }