Skip to content

Commit 522e4c5

Browse files
committed
feat: filter the available fonts by the current language script
1 parent ff3c914 commit 522e4c5

File tree

5 files changed

+57
-21
lines changed

5 files changed

+57
-21
lines changed

packages/keybr-textinput-ui/lib/TypingSettings.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useKeyboard } from "@keybr/keyboard";
1+
import { keyboardProps, useKeyboard } from "@keybr/keyboard";
22
import { Tasks } from "@keybr/lang";
33
import { Settings, useSettings } from "@keybr/settings";
44
import {
@@ -82,10 +82,7 @@ function ExampleText() {
8282
return (
8383
<div className={styles.exampleText}>
8484
<AnimatedText
85-
settings={{
86-
...toTextDisplaySettings(settings),
87-
language: keyboard.layout.language,
88-
}}
85+
settings={toTextDisplaySettings(settings)}
8986
text={keyboard.getExampleText()}
9087
/>
9188
</div>
@@ -189,18 +186,20 @@ function SpaceSkipsWordsProp() {
189186

190187
function FontProp() {
191188
const { settings, updateSettings } = useSettings();
189+
const fonts = Font.select(settings.get(keyboardProps.layout).language);
190+
const font = Font.find(fonts, settings.get(textDisplayProps.font));
192191
return (
193192
<FieldList>
194193
<Field size={10}>
195194
<FormattedMessage id="settings.font.label" defaultMessage="Font:" />
196195
</Field>
197196
<Field>
198197
<OptionList
199-
options={Font.ALL.map((item) => ({
198+
options={fonts.map((item) => ({
200199
value: item.id,
201200
name: <span style={item.cssProperties}>{item.name}</span>,
202201
}))}
203-
value={settings.get(textDisplayProps.font).id}
202+
value={font.id}
204203
onSelect={(id) => {
205204
updateSettings(
206205
settings.set(textDisplayProps.font, Font.ALL.get(id)),
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { test } from "node:test";
2+
import { Language } from "@keybr/keyboard";
3+
import { assert } from "chai";
4+
import { Font } from "./font.ts";
5+
6+
test("select fonts for each language", () => {
7+
for (const language of Language.ALL) {
8+
const fonts = Font.select(language);
9+
assert.isTrue(fonts.size > 0);
10+
for (const font of Font.ALL) {
11+
assert.isNotNull(Font.find(fonts, font));
12+
}
13+
}
14+
});

packages/keybr-textinput/lib/font.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { type Language } from "@keybr/keyboard";
12
import { Enum, type EnumItem } from "@keybr/lang";
2-
import { type FontFace, FONTS_FACES, UBUNTU_MONO } from "@keybr/themes";
3+
import {
4+
type FontFace,
5+
FONTS_FACES,
6+
type Script,
7+
UBUNTU_MONO,
8+
} from "@keybr/themes";
39
import { type CSSProperties } from "react";
410

511
export class Font implements EnumItem {
@@ -13,13 +19,25 @@ export class Font implements EnumItem {
1319
);
1420
}
1521

22+
static select(language: Language) {
23+
return new Enum(
24+
...Font.ALL.filter((font) => font.scripts.includes(language.script)),
25+
);
26+
}
27+
28+
static find(fonts: Enum<Font>, font: Font) {
29+
return fonts.has(font) ? font : fonts.at(0);
30+
}
31+
1632
readonly id: string;
1733
readonly name: string;
34+
readonly scripts: readonly Script[];
1835
readonly cssProperties: CSSProperties;
1936

2037
private constructor(fontFace: FontFace) {
2138
this.id = `${fontFace.family}-${fontFace.weight}-${fontFace.style}`;
2239
this.name = fontFace.name;
40+
this.scripts = fontFace.scripts;
2341
this.cssProperties = Object.freeze({ ...fontFace.cssProperties });
2442
Object.freeze(this);
2543
}

packages/keybr-textinput/lib/settings.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ export type TextInputSettings = {
3030
readonly spaceSkipsWords: boolean;
3131
};
3232

33-
export const textInputSettings: TextInputSettings = {
33+
export const textInputSettings = {
3434
stopOnError: true,
3535
forgiveErrors: true,
3636
spaceSkipsWords: true,
37-
};
37+
} as const satisfies TextInputSettings;
3838

3939
export const textInputProps = {
4040
stopOnError: booleanProp("textInput.stopOnError", true),
@@ -76,13 +76,13 @@ export enum WhitespaceStyle {
7676
Bullet = 3,
7777
}
7878

79-
export const textDisplaySettings: TextDisplaySettings = {
79+
export const textDisplaySettings = {
8080
font: Font.default,
8181
caretShapeStyle: CaretShapeStyle.Underline,
8282
caretMovementStyle: CaretMovementStyle.Smooth,
8383
whitespaceStyle: WhitespaceStyle.Bullet,
8484
language: Language.EN,
85-
};
85+
} as const satisfies TextDisplaySettings;
8686

8787
export const textDisplayProps = {
8888
font: itemProp("textDisplay.font", Font.ALL, Font.default),
@@ -104,11 +104,16 @@ export const textDisplayProps = {
104104
} as const;
105105

106106
export function toTextDisplaySettings(settings: Settings): TextDisplaySettings {
107+
const font = settings.get(textDisplayProps.font);
108+
const caretShapeStyle = settings.get(textDisplayProps.caretShapeStyle);
109+
const caretMovementStyle = settings.get(textDisplayProps.caretMovementStyle);
110+
const whitespaceStyle = settings.get(textDisplayProps.whitespaceStyle);
111+
const language = settings.get(keyboardProps.layout).language;
107112
return {
108-
font: settings.get(textDisplayProps.font),
109-
caretShapeStyle: settings.get(textDisplayProps.caretShapeStyle),
110-
caretMovementStyle: settings.get(textDisplayProps.caretMovementStyle),
111-
whitespaceStyle: settings.get(textDisplayProps.whitespaceStyle),
112-
language: settings.get(keyboardProps.layout).language,
113+
font: Font.find(Font.select(language), font),
114+
caretShapeStyle,
115+
caretMovementStyle,
116+
whitespaceStyle,
117+
language,
113118
};
114119
}

packages/keybr-themes/lib/fonts/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ export const FONTS_FACES: readonly FontFace[] = [
115115
NUNITO_I,
116116
NUNITO_B,
117117
NUNITO_BI,
118-
OPEN_DYSLEXIC,
119-
OPEN_DYSLEXIC_I,
120-
OPEN_DYSLEXIC_B,
121-
OPEN_DYSLEXIC_BI,
122118
OPEN_SANS,
123119
OPEN_SANS_I,
124120
OPEN_SANS_B,
@@ -167,6 +163,10 @@ export const FONTS_FACES: readonly FontFace[] = [
167163
CURSIVE_I,
168164
CURSIVE_B,
169165
CURSIVE_BI,
166+
OPEN_DYSLEXIC,
167+
OPEN_DYSLEXIC_I,
168+
OPEN_DYSLEXIC_B,
169+
OPEN_DYSLEXIC_BI,
170170
];
171171

172172
function fontName(family: string, weight: FontWeight, style: FontStyle): string {

0 commit comments

Comments
 (0)