@hozo/typography 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/dist/fonts-expo.d.ts +27 -0
- package/dist/fonts-expo.d.ts.map +1 -0
- package/dist/fonts-expo.js +63 -0
- package/dist/fonts-expo.js.map +1 -0
- package/dist/fonts-native.d.ts +28 -0
- package/dist/fonts-native.d.ts.map +1 -0
- package/dist/fonts-native.js +47 -0
- package/dist/fonts-native.js.map +1 -0
- package/dist/fonts.d.ts +62 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +172 -0
- package/dist/fonts.js.map +1 -0
- package/dist/generated/ruby.d.ts +2 -0
- package/dist/generated/ruby.d.ts.map +1 -0
- package/dist/generated/ruby.js +2 -0
- package/dist/generated/ruby.js.map +1 -0
- package/dist/generated/ruby.native.d.ts +2 -0
- package/dist/generated/ruby.native.d.ts.map +1 -0
- package/dist/generated/ruby.native.js +2 -0
- package/dist/generated/ruby.native.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.d.ts +82 -0
- package/dist/index.native.d.ts.map +1 -0
- package/dist/index.native.js +136 -0
- package/dist/index.native.js.map +1 -0
- package/dist/ruby.native.d.ts +43 -0
- package/dist/ruby.native.d.ts.map +1 -0
- package/dist/ruby.native.js +50 -0
- package/dist/ruby.native.js.map +1 -0
- package/dist/text-size.d.ts +35 -0
- package/dist/text-size.d.ts.map +1 -0
- package/dist/text-size.js +40 -0
- package/dist/text-size.js.map +1 -0
- package/package.json +93 -0
- package/src/fonts-expo.ts +90 -0
- package/src/fonts-native.ts +85 -0
- package/src/fonts.ts +240 -0
- package/src/generated/ruby.native.ts +6 -0
- package/src/generated/ruby.ts +1 -0
- package/src/index.native.tsx +240 -0
- package/src/index.tsx +152 -0
- package/src/ruby.native.tsx +91 -0
- package/src/text-size.ts +41 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineFonts,
|
|
3
|
+
type FontFamilyDefinition,
|
|
4
|
+
type FontManifest,
|
|
5
|
+
type FontStyle,
|
|
6
|
+
type FontWeight,
|
|
7
|
+
} from './fonts.ts'
|
|
8
|
+
|
|
9
|
+
export interface ExpoAndroidFontDefinition {
|
|
10
|
+
path: string
|
|
11
|
+
weight: number
|
|
12
|
+
style?: 'normal' | 'italic'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ExpoAndroidFontFamily {
|
|
16
|
+
fontFamily: string
|
|
17
|
+
fontDefinitions: ExpoAndroidFontDefinition[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The public option shape accepted by Expo's `expo-font` config plugin. */
|
|
21
|
+
export interface ExpoFontPluginOptions {
|
|
22
|
+
ios?: { fonts: string[] }
|
|
23
|
+
android?: { fonts: ExpoAndroidFontFamily[] }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isExternal(definition: FontFamilyDefinition, platform: 'ios' | 'android'): boolean {
|
|
27
|
+
return definition.external === true || definition.external?.includes(platform) === true
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function androidWeight(weight: FontWeight | undefined, id: string): number {
|
|
31
|
+
if (weight === undefined || weight === 'normal') return 400
|
|
32
|
+
if (weight === 'bold') return 700
|
|
33
|
+
if (typeof weight === 'number') return weight
|
|
34
|
+
throw new TypeError(
|
|
35
|
+
`Font ${JSON.stringify(id)} uses variable weight ${JSON.stringify(weight)}; Expo Android XML needs one static face per weight`,
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function androidStyle(style: FontStyle | undefined, id: string): 'normal' | 'italic' {
|
|
40
|
+
if (style === undefined || style === 'normal') return 'normal'
|
|
41
|
+
if (style === 'italic') return 'italic'
|
|
42
|
+
throw new TypeError(
|
|
43
|
+
`Font ${JSON.stringify(id)} uses oblique style; Expo Android XML supports normal or italic`,
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Derives options for Expo's official `expo-font` config plugin.
|
|
49
|
+
*
|
|
50
|
+
* This function does not import Expo. Keep `expo-font` in the application and
|
|
51
|
+
* pass the returned value as its plugin options from `app.config.ts`.
|
|
52
|
+
*/
|
|
53
|
+
export function createExpoFontOptions(manifest: FontManifest): ExpoFontPluginOptions {
|
|
54
|
+
defineFonts(manifest)
|
|
55
|
+
const ios = new Set<string>()
|
|
56
|
+
const android = new Map<string, Map<string, ExpoAndroidFontDefinition>>()
|
|
57
|
+
|
|
58
|
+
for (const [id, definition] of Object.entries(manifest)) {
|
|
59
|
+
for (const face of definition.faces ?? []) {
|
|
60
|
+
if (!isExternal(definition, 'ios') && face.sources.ios !== undefined) {
|
|
61
|
+
ios.add(face.sources.ios)
|
|
62
|
+
}
|
|
63
|
+
if (isExternal(definition, 'android') || face.sources.android === undefined) continue
|
|
64
|
+
|
|
65
|
+
const family = definition.nativeFamily?.android ?? definition.family
|
|
66
|
+
const font = {
|
|
67
|
+
path: face.sources.android,
|
|
68
|
+
weight: androidWeight(face.weight, id),
|
|
69
|
+
style: androidStyle(face.style, id),
|
|
70
|
+
} satisfies ExpoAndroidFontDefinition
|
|
71
|
+
const definitions = android.get(family) ?? new Map<string, ExpoAndroidFontDefinition>()
|
|
72
|
+
definitions.set(JSON.stringify(font), font)
|
|
73
|
+
android.set(family, definitions)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
...(ios.size === 0 ? {} : { ios: { fonts: [...ios] } }),
|
|
79
|
+
...(android.size === 0
|
|
80
|
+
? {}
|
|
81
|
+
: {
|
|
82
|
+
android: {
|
|
83
|
+
fonts: [...android].map(([fontFamily, definitions]) => ({
|
|
84
|
+
fontFamily,
|
|
85
|
+
fontDefinitions: [...definitions.values()],
|
|
86
|
+
})),
|
|
87
|
+
},
|
|
88
|
+
}),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineFonts,
|
|
3
|
+
type FontManifest,
|
|
4
|
+
type FontStyle,
|
|
5
|
+
type FontWeight,
|
|
6
|
+
fontFamily,
|
|
7
|
+
} from './fonts.js'
|
|
8
|
+
|
|
9
|
+
export type NativeFontPlatform = 'ios' | 'android'
|
|
10
|
+
|
|
11
|
+
export interface NativeFontFacePlan {
|
|
12
|
+
/** Project-relative path that the native project must register before React renders. */
|
|
13
|
+
path: string
|
|
14
|
+
/** The descriptor authored in the shared manifest, with aliases canonicalized. */
|
|
15
|
+
weight: number | `${number} ${number}`
|
|
16
|
+
style: FontStyle
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface NativeFontFamilyPlan {
|
|
20
|
+
/** Stable manifest key, useful to generators and validation tooling. */
|
|
21
|
+
id: string
|
|
22
|
+
/** The value to use as React Native's `fontFamily`. */
|
|
23
|
+
family: string
|
|
24
|
+
faces: NativeFontFacePlan[]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface NativeFontPlan {
|
|
28
|
+
platform: NativeFontPlatform
|
|
29
|
+
/** Deduplicated files that must be registered by the host project. */
|
|
30
|
+
files: string[]
|
|
31
|
+
families: NativeFontFamilyPlan[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function canonicalWeight(weight: FontWeight | undefined): number | `${number} ${number}` {
|
|
35
|
+
if (weight === undefined || weight === 'normal') return 400
|
|
36
|
+
if (weight === 'bold') return 700
|
|
37
|
+
return weight
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isExternal(
|
|
41
|
+
external: true | readonly ('web' | NativeFontPlatform)[] | undefined,
|
|
42
|
+
platform: NativeFontPlatform,
|
|
43
|
+
): boolean {
|
|
44
|
+
return external === true || external?.includes(platform) === true
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Produces a deterministic description of the assets a bare React Native host must register.
|
|
49
|
+
* It deliberately performs no filesystem or native-project mutation.
|
|
50
|
+
*/
|
|
51
|
+
export function createNativeFontPlan(
|
|
52
|
+
manifest: FontManifest,
|
|
53
|
+
platform: NativeFontPlatform,
|
|
54
|
+
): NativeFontPlan {
|
|
55
|
+
defineFonts(manifest)
|
|
56
|
+
const files = new Set<string>()
|
|
57
|
+
const families: NativeFontFamilyPlan[] = []
|
|
58
|
+
|
|
59
|
+
for (const [id, definition] of Object.entries(manifest)) {
|
|
60
|
+
if (isExternal(definition.external, platform)) continue
|
|
61
|
+
|
|
62
|
+
const seenFaces = new Set<string>()
|
|
63
|
+
const faces: NativeFontFacePlan[] = []
|
|
64
|
+
for (const face of definition.faces ?? []) {
|
|
65
|
+
const path = face.sources[platform]
|
|
66
|
+
if (path === undefined) continue
|
|
67
|
+
const planned = {
|
|
68
|
+
path: path.trim(),
|
|
69
|
+
weight: canonicalWeight(face.weight),
|
|
70
|
+
style: face.style ?? 'normal',
|
|
71
|
+
} satisfies NativeFontFacePlan
|
|
72
|
+
const key = JSON.stringify(planned)
|
|
73
|
+
if (seenFaces.has(key)) continue
|
|
74
|
+
seenFaces.add(key)
|
|
75
|
+
files.add(planned.path)
|
|
76
|
+
faces.push(planned)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (faces.length > 0) {
|
|
80
|
+
families.push({ id, family: fontFamily(manifest, id, platform)!, faces })
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return { platform, files: [...files], families }
|
|
85
|
+
}
|
package/src/fonts.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
export type FontPlatform = 'web' | 'ios' | 'android'
|
|
2
|
+
export type FontStyle = 'normal' | 'italic' | 'oblique'
|
|
3
|
+
export type FontDisplay = 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
|
|
4
|
+
export type FontFormat = 'woff2' | 'woff' | 'truetype' | 'opentype'
|
|
5
|
+
export type FontWeight = number | 'normal' | 'bold' | `${number} ${number}`
|
|
6
|
+
|
|
7
|
+
export interface WebFontSource {
|
|
8
|
+
/** A URL already resolved by the host bundler, or one valid from the emitted stylesheet. */
|
|
9
|
+
url: string
|
|
10
|
+
format?: FontFormat
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface FontFaceSources {
|
|
14
|
+
web?: readonly WebFontSource[]
|
|
15
|
+
/** Project-relative file path used by Expo or a bare iOS project. */
|
|
16
|
+
ios?: string
|
|
17
|
+
/** Project-relative file path used by Expo or a bare Android project. */
|
|
18
|
+
android?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface FontFaceDefinition {
|
|
22
|
+
sources: FontFaceSources
|
|
23
|
+
weight?: FontWeight
|
|
24
|
+
style?: FontStyle
|
|
25
|
+
display?: FontDisplay
|
|
26
|
+
unicodeRange?: string | readonly string[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FontFamilyDefinition {
|
|
30
|
+
/** CSS family name. It is also the Native fallback when no platform name is supplied. */
|
|
31
|
+
family: string
|
|
32
|
+
/** Native family/PostScript names when they differ from the logical Web family. */
|
|
33
|
+
nativeFamily?: { ios?: string; android?: string }
|
|
34
|
+
faces?: readonly FontFaceDefinition[]
|
|
35
|
+
/** Platforms whose font availability is owned by the host, such as next/font or linked assets. */
|
|
36
|
+
external?: true | readonly FontPlatform[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type FontManifest = Readonly<Record<string, FontFamilyDefinition>>
|
|
40
|
+
|
|
41
|
+
export interface FontAvailability {
|
|
42
|
+
families: Array<{
|
|
43
|
+
id: string
|
|
44
|
+
names: Record<FontPlatform, string>
|
|
45
|
+
external: FontPlatform[]
|
|
46
|
+
faces: Array<{
|
|
47
|
+
platforms: FontPlatform[]
|
|
48
|
+
weightFrom: number
|
|
49
|
+
weightTo: number
|
|
50
|
+
style: FontStyle
|
|
51
|
+
}>
|
|
52
|
+
}>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function externalPlatforms(value: FontFamilyDefinition['external']): Set<FontPlatform> {
|
|
56
|
+
return new Set(value === true ? ['web', 'ios', 'android'] : (value ?? []))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function printable(value: string, label: string): string {
|
|
60
|
+
const trimmed = value.trim()
|
|
61
|
+
const hasControlCharacter = [...trimmed].some((character) => {
|
|
62
|
+
const code = character.charCodeAt(0)
|
|
63
|
+
return code < 32 || code === 127
|
|
64
|
+
})
|
|
65
|
+
if (trimmed.length === 0 || hasControlCharacter) {
|
|
66
|
+
throw new TypeError(`${label} must be a non-empty printable string`)
|
|
67
|
+
}
|
|
68
|
+
return trimmed
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function normalizedWeight(weight: FontWeight | undefined): string {
|
|
72
|
+
if (weight === undefined) return '400'
|
|
73
|
+
if (weight === 'normal') return '400'
|
|
74
|
+
if (weight === 'bold') return '700'
|
|
75
|
+
if (typeof weight === 'number') {
|
|
76
|
+
if (!Number.isInteger(weight) || weight < 1 || weight > 1000) {
|
|
77
|
+
throw new TypeError(`Font weight ${weight} must be an integer from 1 through 1000`)
|
|
78
|
+
}
|
|
79
|
+
return String(weight)
|
|
80
|
+
}
|
|
81
|
+
const match = /^(\d+) (\d+)$/.exec(weight)
|
|
82
|
+
if (!match) throw new TypeError(`Invalid variable font weight range ${JSON.stringify(weight)}`)
|
|
83
|
+
const from = Number(match[1])
|
|
84
|
+
const to = Number(match[2])
|
|
85
|
+
if (from < 1 || to > 1000 || from >= to) {
|
|
86
|
+
throw new TypeError(
|
|
87
|
+
`Variable font weight range ${JSON.stringify(weight)} must increase within 1..1000`,
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
return `${from} ${to}`
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function ranges(value: FontFaceDefinition['unicodeRange']): string[] {
|
|
94
|
+
const values = typeof value === 'string' ? [value] : (value ?? [])
|
|
95
|
+
return values.map((range) => {
|
|
96
|
+
const trimmed = range.trim()
|
|
97
|
+
if (!/^U\+[0-9A-F?]+(?:-[0-9A-F]+)?$/i.test(trimmed)) {
|
|
98
|
+
throw new TypeError(`Invalid Unicode range ${JSON.stringify(range)}`)
|
|
99
|
+
}
|
|
100
|
+
return trimmed.toUpperCase()
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function validateFace(id: string, face: FontFaceDefinition, external: Set<FontPlatform>): void {
|
|
105
|
+
normalizedWeight(face.weight)
|
|
106
|
+
const sources = face.sources
|
|
107
|
+
const web = sources.web ?? []
|
|
108
|
+
if (external.has('web') && web.length > 0) {
|
|
109
|
+
throw new TypeError(
|
|
110
|
+
`Font ${JSON.stringify(id)} is externally managed on Web but also has Web sources`,
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
if (external.has('ios') && sources.ios !== undefined) {
|
|
114
|
+
throw new TypeError(
|
|
115
|
+
`Font ${JSON.stringify(id)} is externally managed on iOS but also has an iOS source`,
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
if (external.has('android') && sources.android !== undefined) {
|
|
119
|
+
throw new TypeError(
|
|
120
|
+
`Font ${JSON.stringify(id)} is externally managed on Android but also has an Android source`,
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
for (const source of web) printable(source.url, `Web source for font ${JSON.stringify(id)}`)
|
|
124
|
+
if (sources.ios !== undefined) printable(sources.ios, `iOS source for font ${JSON.stringify(id)}`)
|
|
125
|
+
if (sources.android !== undefined) {
|
|
126
|
+
printable(sources.android, `Android source for font ${JSON.stringify(id)}`)
|
|
127
|
+
}
|
|
128
|
+
ranges(face.unicodeRange)
|
|
129
|
+
if (web.length === 0 && sources.ios === undefined && sources.android === undefined) {
|
|
130
|
+
throw new TypeError(`Font ${JSON.stringify(id)} has a face with no source`)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Validates a font manifest while preserving its exact logical-family keys for inference.
|
|
136
|
+
* Calling it does not register or load anything.
|
|
137
|
+
*/
|
|
138
|
+
export function defineFonts<const T extends FontManifest>(manifest: T): T {
|
|
139
|
+
if (Object.keys(manifest).length === 0) throw new TypeError('A font manifest cannot be empty')
|
|
140
|
+
for (const [id, definition] of Object.entries(manifest)) {
|
|
141
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(id)) {
|
|
142
|
+
throw new TypeError(`Invalid logical font identifier ${JSON.stringify(id)}`)
|
|
143
|
+
}
|
|
144
|
+
printable(definition.family, `Family name for font ${JSON.stringify(id)}`)
|
|
145
|
+
if (definition.nativeFamily?.ios !== undefined) {
|
|
146
|
+
printable(definition.nativeFamily.ios, `iOS family for font ${JSON.stringify(id)}`)
|
|
147
|
+
}
|
|
148
|
+
if (definition.nativeFamily?.android !== undefined) {
|
|
149
|
+
printable(definition.nativeFamily.android, `Android family for font ${JSON.stringify(id)}`)
|
|
150
|
+
}
|
|
151
|
+
const external = externalPlatforms(definition.external)
|
|
152
|
+
if ((definition.faces?.length ?? 0) === 0 && external.size === 0) {
|
|
153
|
+
throw new TypeError(
|
|
154
|
+
`Font ${JSON.stringify(id)} needs a face or an explicitly external platform`,
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
for (const face of definition.faces ?? []) validateFace(id, face, external)
|
|
158
|
+
}
|
|
159
|
+
return manifest
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Resolves one logical family to the name authored in a platform style. */
|
|
163
|
+
export function fontFamily(
|
|
164
|
+
manifest: FontManifest,
|
|
165
|
+
id: string,
|
|
166
|
+
platform: FontPlatform,
|
|
167
|
+
): string | undefined {
|
|
168
|
+
const definition = manifest[id]
|
|
169
|
+
if (!definition) return undefined
|
|
170
|
+
if (platform === 'web') return definition.family
|
|
171
|
+
return definition.nativeFamily?.[platform] ?? definition.family
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function weightRange(weight: FontWeight | undefined): [number, number] {
|
|
175
|
+
const normalized = normalizedWeight(weight)
|
|
176
|
+
const [from, to = from] = normalized.split(' ').map(Number)
|
|
177
|
+
return [from!, to!]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Creates the small serializable registry used by Hozo's conservative compiler diagnostics. */
|
|
181
|
+
export function createFontAvailability(manifest: FontManifest): FontAvailability {
|
|
182
|
+
defineFonts(manifest)
|
|
183
|
+
return {
|
|
184
|
+
families: Object.entries(manifest).map(([id, definition]) => ({
|
|
185
|
+
id,
|
|
186
|
+
names: {
|
|
187
|
+
web: definition.family,
|
|
188
|
+
ios: definition.nativeFamily?.ios ?? definition.family,
|
|
189
|
+
android: definition.nativeFamily?.android ?? definition.family,
|
|
190
|
+
},
|
|
191
|
+
external: [...externalPlatforms(definition.external)],
|
|
192
|
+
faces: (definition.faces ?? []).map((face) => {
|
|
193
|
+
const [weightFrom, weightTo] = weightRange(face.weight)
|
|
194
|
+
return {
|
|
195
|
+
platforms: (['web', 'ios', 'android'] as const).filter((platform) =>
|
|
196
|
+
platform === 'web'
|
|
197
|
+
? (face.sources.web?.length ?? 0) > 0
|
|
198
|
+
: face.sources[platform] !== undefined,
|
|
199
|
+
),
|
|
200
|
+
weightFrom,
|
|
201
|
+
weightTo,
|
|
202
|
+
style: face.style ?? 'normal',
|
|
203
|
+
}
|
|
204
|
+
}),
|
|
205
|
+
})),
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function quoted(value: string): string {
|
|
210
|
+
return `"${value.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Creates deterministic, deduplicated `@font-face` CSS for Web-managed faces. */
|
|
214
|
+
export function createFontFaceCss(manifest: FontManifest): string {
|
|
215
|
+
defineFonts(manifest)
|
|
216
|
+
const rules = new Set<string>()
|
|
217
|
+
for (const definition of Object.values(manifest)) {
|
|
218
|
+
if (externalPlatforms(definition.external).has('web')) continue
|
|
219
|
+
for (const face of definition.faces ?? []) {
|
|
220
|
+
const sources = face.sources.web ?? []
|
|
221
|
+
if (sources.length === 0) continue
|
|
222
|
+
const src = sources
|
|
223
|
+
.map(
|
|
224
|
+
({ url, format }) => `url(${quoted(url)})${format ? ` format(${quoted(format)})` : ''}`,
|
|
225
|
+
)
|
|
226
|
+
.join(', ')
|
|
227
|
+
const descriptors = [
|
|
228
|
+
` font-family: ${quoted(definition.family)};`,
|
|
229
|
+
` src: ${src};`,
|
|
230
|
+
` font-style: ${face.style ?? 'normal'};`,
|
|
231
|
+
` font-weight: ${normalizedWeight(face.weight)};`,
|
|
232
|
+
` font-display: ${face.display ?? 'swap'};`,
|
|
233
|
+
]
|
|
234
|
+
const unicode = ranges(face.unicodeRange)
|
|
235
|
+
if (unicode.length > 0) descriptors.push(` unicode-range: ${unicode.join(', ')};`)
|
|
236
|
+
rules.add(`@font-face {\n${descriptors.join('\n')}\n}`)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return rules.size === 0 ? '' : `${[...rules].join('\n\n')}\n`
|
|
240
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Ruby as HozoRuby, RubyText as HozoRubyText } from '../index.tsx'
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { HozoTextSizeContext } from '@hozo/engine'
|
|
2
|
+
import { hozoPreflight } from '@hozo/engine/project'
|
|
3
|
+
import { Link, Text } from '@hozo/primitives'
|
|
4
|
+
import React, { type ComponentProps, type ReactNode, useContext } from 'react'
|
|
5
|
+
// The components rather than their names. These files used to render
|
|
6
|
+
// `React.createElement('View')`, and React Native resolves a string tag
|
|
7
|
+
// through its view config registry, where the registered names are
|
|
8
|
+
// `RCTView` and `RCTText` -- never `View` or `Text`. Every one of these
|
|
9
|
+
// would have thrown "View config getter callback for component `View`
|
|
10
|
+
// must be a function" on the first render. Nothing caught it because
|
|
11
|
+
// nothing imported these files: the tests next to them render the Web
|
|
12
|
+
// half through `react-dom/server`.
|
|
13
|
+
import type { AccessibilityRole, StyleProp, TextStyle } from 'react-native'
|
|
14
|
+
import { HozoRuby, HozoRubyText } from './ruby.native.tsx'
|
|
15
|
+
import { BARE_TEXT_SIZE_RATIOS, TEXT_SIZE_RATIOS } from './text-size.ts'
|
|
16
|
+
|
|
17
|
+
export { BARE_TEXT_SIZE_RATIOS, TEXT_SIZE_RATIOS } from './text-size.ts'
|
|
18
|
+
export { Link, Text }
|
|
19
|
+
|
|
20
|
+
export interface TypographyNativeProps {
|
|
21
|
+
/**
|
|
22
|
+
* Tailwind classes, read by the compiler and gone by runtime.
|
|
23
|
+
*
|
|
24
|
+
* The Web half of this file has always declared it; this half never
|
|
25
|
+
* did, and nothing noticed because the `react-native` export condition
|
|
26
|
+
* carried no `types`, so a React Native app was type-checked against
|
|
27
|
+
* the Web declarations instead. Uncompiled, the prop is simply ignored
|
|
28
|
+
* here -- which is what the fallback components are for.
|
|
29
|
+
*/
|
|
30
|
+
className?: string
|
|
31
|
+
children?: ReactNode
|
|
32
|
+
/**
|
|
33
|
+
* React Native's own types rather than `any` and `string`.
|
|
34
|
+
*
|
|
35
|
+
* The Web half of this file says `CSSProperties`. This half agreed with
|
|
36
|
+
* anything, so a Web-only style reached a native `Text` in silence --
|
|
37
|
+
* the same erasure `@hozo/tailwind-conformance` exists to catch one
|
|
38
|
+
* layer down.
|
|
39
|
+
*/
|
|
40
|
+
style?: StyleProp<TextStyle>
|
|
41
|
+
testID?: string
|
|
42
|
+
nativeID?: string
|
|
43
|
+
accessibilityLabel?: string
|
|
44
|
+
accessibilityHint?: string
|
|
45
|
+
accessibilityRole?: AccessibilityRole
|
|
46
|
+
accessible?: boolean
|
|
47
|
+
numberOfLines?: number
|
|
48
|
+
ellipsizeMode?: 'head' | 'middle' | 'tail' | 'clip'
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// The public concept keeps one name across platform entry points even
|
|
52
|
+
// though its style field uses React Native's own type here.
|
|
53
|
+
export type TypographyUniversalProps = TypographyNativeProps
|
|
54
|
+
|
|
55
|
+
export type TextProps = TypographyNativeProps
|
|
56
|
+
export type SemanticTextProps = TypographyNativeProps
|
|
57
|
+
|
|
58
|
+
export interface HeadingProps extends TypographyNativeProps {
|
|
59
|
+
level?: 1 | 2 | 3 | 4 | 5 | 6
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Native declaration for the Link value re-exported from primitives. */
|
|
63
|
+
export interface LinkProps extends TypographyNativeProps {
|
|
64
|
+
href: string
|
|
65
|
+
external?: boolean
|
|
66
|
+
replace?: boolean
|
|
67
|
+
prefetch?: boolean
|
|
68
|
+
target?: string
|
|
69
|
+
rel?: string
|
|
70
|
+
download?: boolean | string
|
|
71
|
+
onPress?: (event: { defaultPrevented?: boolean }) => void
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The size the text around this is drawn at.
|
|
76
|
+
*
|
|
77
|
+
* `@hozo/engine`'s, not one of this package's own. The compiler emits
|
|
78
|
+
* `HozoTextSize` for an element whose size it could not read, and that
|
|
79
|
+
* publishes into this context -- so a compiled ancestor and one of the
|
|
80
|
+
* components below, rendered uncompiled, agree about the base. Two
|
|
81
|
+
* contexts would have been two answers.
|
|
82
|
+
*
|
|
83
|
+
* React Native offers nothing to build this on. Its `fontSize` is a
|
|
84
|
+
* number of points with no relative unit, a nested `Text` inherits
|
|
85
|
+
* visually but cannot read what it inherited, and the only ancestor
|
|
86
|
+
* context the platform exposes -- `TextAncestorContext` -- is a boolean
|
|
87
|
+
* saying whether there is a `Text` above at all.
|
|
88
|
+
*
|
|
89
|
+
* The default is React Native's own, and the constants this replaced --
|
|
90
|
+
* 11, 11, 12 -- are exactly the ratios applied to it.
|
|
91
|
+
*/
|
|
92
|
+
const TextSize = HozoTextSizeContext
|
|
93
|
+
const NativeText = Text as unknown as React.ComponentType<TypographyNativeProps>
|
|
94
|
+
|
|
95
|
+
function relative(ratio: number, base: number) {
|
|
96
|
+
return Math.round(base * ratio)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function Paragraph(props: SemanticTextProps) {
|
|
100
|
+
return <NativeText {...props} />
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function Heading({
|
|
104
|
+
level = 1,
|
|
105
|
+
accessibilityRole = 'header',
|
|
106
|
+
style,
|
|
107
|
+
...props
|
|
108
|
+
}: HeadingProps) {
|
|
109
|
+
// Preflight resets headings to inherited size and weight. Without it,
|
|
110
|
+
// reproduce the browser's heading defaults that Native does not have.
|
|
111
|
+
const base = useContext(TextSize)
|
|
112
|
+
const ratio = BARE_TEXT_SIZE_RATIOS.heading[Math.min(6, Math.max(1, level)) - 1] as number
|
|
113
|
+
const defaultStyle = hozoPreflight
|
|
114
|
+
? undefined
|
|
115
|
+
: { fontSize: relative(ratio, base), fontWeight: 'bold' as const }
|
|
116
|
+
return (
|
|
117
|
+
<NativeText accessibilityRole={accessibilityRole} style={[defaultStyle, style]} {...props} />
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function Strong({ style, ...props }: TypographyNativeProps) {
|
|
122
|
+
return <NativeText style={[{ fontWeight: 'bold' }, style]} {...props} />
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function Emphasis({ style, ...props }: TypographyNativeProps) {
|
|
126
|
+
return <NativeText style={[{ fontStyle: 'italic' }, style]} {...props} />
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function Underline({ style, ...props }: TypographyNativeProps) {
|
|
130
|
+
return <NativeText style={[{ textDecorationLine: 'underline' }, style]} {...props} />
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function Strikethrough({ style, ...props }: TypographyNativeProps) {
|
|
134
|
+
return <NativeText style={[{ textDecorationLine: 'line-through' }, style]} {...props} />
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const Del = Strikethrough
|
|
138
|
+
|
|
139
|
+
export function Sub({ style, ...props }: TypographyNativeProps) {
|
|
140
|
+
const base = useContext(TextSize)
|
|
141
|
+
const ratios = hozoPreflight ? TEXT_SIZE_RATIOS : BARE_TEXT_SIZE_RATIOS
|
|
142
|
+
return <NativeText style={[{ fontSize: relative(ratios.sub, base) }, style]} {...props} />
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function Sup({ style, ...props }: TypographyNativeProps) {
|
|
146
|
+
const base = useContext(TextSize)
|
|
147
|
+
const ratios = hozoPreflight ? TEXT_SIZE_RATIOS : BARE_TEXT_SIZE_RATIOS
|
|
148
|
+
return <NativeText style={[{ fontSize: relative(ratios.sup, base) }, style]} {...props} />
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function Code({ style, ...props }: TypographyNativeProps) {
|
|
152
|
+
return <NativeText style={[{ fontFamily: 'monospace' }, style]} {...props} />
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function Small({ style, ...props }: TypographyNativeProps) {
|
|
156
|
+
const base = useContext(TextSize)
|
|
157
|
+
const ratios = hozoPreflight ? TEXT_SIZE_RATIOS : BARE_TEXT_SIZE_RATIOS
|
|
158
|
+
return (
|
|
159
|
+
<NativeText
|
|
160
|
+
style={[{ fontSize: relative(ratios.small, base), opacity: 0.8 }, style]}
|
|
161
|
+
{...props}
|
|
162
|
+
/>
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function Mark({ style, ...props }: TypographyNativeProps) {
|
|
167
|
+
return <NativeText style={[{ backgroundColor: '#fef08a' }, style]} {...props} />
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Recursively replaces normal spaces with Unicode non-breaking spaces (\u00A0) */
|
|
171
|
+
function replaceSpacesWithNbsp(node: ReactNode): ReactNode {
|
|
172
|
+
if (typeof node === 'string') {
|
|
173
|
+
return node.replace(/ /g, '\u00A0')
|
|
174
|
+
}
|
|
175
|
+
if (Array.isArray(node)) {
|
|
176
|
+
return React.Children.map(node, replaceSpacesWithNbsp)
|
|
177
|
+
}
|
|
178
|
+
if (React.isValidElement(node)) {
|
|
179
|
+
const element = node as React.ReactElement<{ children?: ReactNode }>
|
|
180
|
+
if (element.props?.children) {
|
|
181
|
+
return React.cloneElement(element, {
|
|
182
|
+
children: replaceSpacesWithNbsp(element.props.children),
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return node
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function NoBreak({ children, ...props }: TypographyNativeProps) {
|
|
190
|
+
return <NativeText {...props}>{replaceSpacesWithNbsp(children)}</NativeText>
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* A Native style handed over as if it were a Web one -- a resolver's
|
|
195
|
+
* limit rather than a claim about the value.
|
|
196
|
+
*
|
|
197
|
+
* A package resolves `@hozo/engine`'s types through the Web entry
|
|
198
|
+
* whichever platform it is building for, because `exports` carries a
|
|
199
|
+
* single `types` and this package compiles both halves in one `tsc` run.
|
|
200
|
+
* So these are seen with `style?: CSSProperties` here while the module
|
|
201
|
+
* Metro loads takes `StyleProp<TextStyle>`. `@hozo/semantics` has the
|
|
202
|
+
* same line for the same reason.
|
|
203
|
+
*/
|
|
204
|
+
const asWeb = (style: TypographyNativeProps['style']) =>
|
|
205
|
+
style as unknown as ComponentProps<typeof HozoRuby>['style']
|
|
206
|
+
/**
|
|
207
|
+
* Ruby, sharing the pair the Native backend emits.
|
|
208
|
+
*
|
|
209
|
+
* React Native flattens nested `Text` into one accessibility node, so a
|
|
210
|
+
* screen reader read the base and then the reading: 「漢字かんじ」. The
|
|
211
|
+
* `accessible={false}` that used to be on the annotation could not stop
|
|
212
|
+
* it -- the flattening happens above, and the parent's label is built
|
|
213
|
+
* from the text it contains whatever the children say about themselves.
|
|
214
|
+
*/
|
|
215
|
+
export function Ruby({ children, accessibilityLabel, style, ...props }: TypographyNativeProps) {
|
|
216
|
+
return (
|
|
217
|
+
<HozoRuby accessibilityLabel={accessibilityLabel} style={asWeb(style)} {...props}>
|
|
218
|
+
{children}
|
|
219
|
+
</HozoRuby>
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function RubyText({ style, children, ...props }: TypographyNativeProps) {
|
|
224
|
+
// Half the size of the text it annotates, against the size `Text`
|
|
225
|
+
// published; `fontSize: '0.65em'` was here and React Native ignored it.
|
|
226
|
+
const base = useContext(TextSize)
|
|
227
|
+
return (
|
|
228
|
+
<HozoRubyText
|
|
229
|
+
style={asWeb([{ fontSize: relative(TEXT_SIZE_RATIOS.rubyText, base), opacity: 0.85 }, style])}
|
|
230
|
+
{...props}
|
|
231
|
+
>
|
|
232
|
+
{children}
|
|
233
|
+
</HozoRubyText>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
// Both spellings, the way `TermList` reads both. The member keeps the
|
|
237
|
+
// full name rather than shortening to `Text`: a flat export has to be
|
|
238
|
+
// readable on its own, and a `<Text>` that meant the annotation would
|
|
239
|
+
// not be. The compiler recognises the pair in `hozo_parser`.
|
|
240
|
+
Ruby.RubyText = RubyText
|