Skip to content

Commit f8e5653

Browse files
committed
fix: Revert "feat: Use TTFLoader in useFont for non-JSON files (#2095)"
This reverts commit 61d3bc5.
1 parent 396156c commit f8e5653

File tree

6 files changed

+15
-53
lines changed

6 files changed

+15
-53
lines changed
-259 KB
Binary file not shown.

.storybook/stories/Text3D.stories.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ export default {
1616
</Setup>
1717
),
1818
],
19-
args: {
20-
bevelEnabled: true,
21-
bevelSize: 0.05,
22-
},
2319
} satisfies Meta<typeof Text3D>
2420

2521
type Story = StoryObj<typeof Text3D>
@@ -40,15 +36,9 @@ function Text3DScene(props: React.ComponentProps<typeof Text3D>) {
4036
export const Text3DSt = {
4137
args: {
4238
font: '/fonts/helvetiker_regular.typeface.json',
39+
bevelEnabled: true,
40+
bevelSize: 0.05,
4341
},
4442
render: (args) => <Text3DScene {...args} />,
4543
name: 'Default',
4644
} satisfies Story
47-
48-
export const Text3DTtfSt = {
49-
args: {
50-
font: '/fonts/lemon-round.ttf',
51-
},
52-
render: (args) => <Text3DScene {...args} />,
53-
name: 'TTF',
54-
} satisfies Story

docs/abstractions/text3d.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ Render 3D text using ThreeJS's `TextGeometry`.
1515

1616
Text3D will suspend while loading the font data. Text3D requires fonts in JSON format generated through [typeface.json](http://gero3.github.io/facetype.js), either as a path to a JSON file or a JSON object. If you face display issues try checking "Reverse font direction" in the typeface tool.
1717

18-
Alternatively, the path can point to a font file of a type supported by [opentype.js](https://github.com/opentypejs/opentype.js) (for example OTF or TTF), in which case the conversion to the JSON format will be done automatically at load time.
19-
2018
```jsx
2119
<Text3D font={fontUrl} {...textOptions}>
2220
Hello world!

docs/loaders/use-font.mdx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@ title: useFont
33
sourcecode: src/core/useFont.tsx
44
---
55

6-
Uses `THREE.FontLoader` to load a font and returns a `THREE.Font` object. It also accepts a JSON object as a parameter. You can use this to preload or share a font across multiple components.
6+
Uses THREE.FontLoader to load a font and returns a `THREE.Font` object. It also accepts a JSON object as a parameter. You can use this to preload or share a font across multiple components.
77

88
```jsx
99
const font = useFont('/fonts/helvetiker_regular.typeface.json')
10-
return <Text3D font={font.data} />
10+
return <Text3D font={font} />
1111
```
1212

1313
In order to preload you do this:
1414

1515
```jsx
1616
useFont.preload('/fonts/helvetiker_regular.typeface.json')
1717
```
18-
19-
If the response from the URL is not a JSON, `THREE.TTFLoader` is used to try parsing the response as a standard font file.
20-
However, keep in mind that the on-the-fly conversion to the JSON format will impact the loading time.
21-
22-
```jsx
23-
const font = useFont('/fonts/helvetiker_regular.ttf')
24-
return <Text3D font={font.data} />
25-
```

src/core/Text3D.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as React from 'react'
22
import * as THREE from 'three'
33
import { extend, MeshProps, Node } from '@react-three/fiber'
44
import { useMemo } from 'react'
5-
import { mergeVertices, TextGeometry, TextGeometryParameters } from 'three-stdlib'
6-
import { useFont } from './useFont'
5+
import { suspend } from 'suspend-react'
6+
import { mergeVertices, TextGeometry, TextGeometryParameters, FontLoader } from 'three-stdlib'
7+
import { useFont, FontData } from './useFont'
78
import { ForwardRefComponent } from '../helpers/ts-utils'
89

910
declare global {
@@ -15,7 +16,7 @@ declare global {
1516
}
1617

1718
type Text3DProps = {
18-
font: Parameters<typeof useFont>[0]
19+
font: FontData | string
1920
bevelSegments?: number
2021
smooth?: number
2122
} & Omit<TextGeometryParameters, 'font'> &

src/core/useFont.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FontLoader, TTFLoader } from 'three-stdlib'
1+
import { FontLoader } from 'three-stdlib'
22
import { suspend, preload, clear } from 'suspend-react'
33

44
export type Glyph = {
@@ -22,7 +22,10 @@ export type FontData = {
2222
type FontInput = string | FontData
2323

2424
let fontLoader: FontLoader | null = null
25-
let ttfLoader: TTFLoader | null = null
25+
26+
async function loadFontData(font: FontInput): Promise<FontData> {
27+
return typeof font === 'string' ? await (await fetch(font)).json() : font
28+
}
2629

2730
function parseFontData(fontData: FontData) {
2831
if (!fontLoader) {
@@ -31,31 +34,9 @@ function parseFontData(fontData: FontData) {
3134
return fontLoader.parse(fontData)
3235
}
3336

34-
function parseTtfArrayBuffer(ttfData: ArrayBuffer) {
35-
if (!ttfLoader) {
36-
ttfLoader = new TTFLoader()
37-
}
38-
return ttfLoader.parse(ttfData) as FontData
39-
}
40-
41-
async function loadFontData(font: FontInput) {
42-
if (typeof font === 'string') {
43-
const res = await fetch(font)
44-
45-
if (res.headers.get('Content-Type')?.includes('application/json')) {
46-
return (await res.json()) as FontData
47-
} else {
48-
const arrayBuffer = await res.arrayBuffer()
49-
return parseTtfArrayBuffer(arrayBuffer)
50-
}
51-
} else {
52-
return font
53-
}
54-
}
55-
5637
async function loader(font: FontInput) {
57-
const fontData = await loadFontData(font)
58-
return parseFontData(fontData)
38+
const data = await loadFontData(font)
39+
return parseFontData(data)
5940
}
6041

6142
export function useFont(font: FontInput) {

0 commit comments

Comments
 (0)