diff --git a/.storybook/public/fonts/lemon-round.ttf b/.storybook/public/fonts/lemon-round.ttf deleted file mode 100644 index fe5a98e08..000000000 Binary files a/.storybook/public/fonts/lemon-round.ttf and /dev/null differ diff --git a/.storybook/stories/Text3D.stories.tsx b/.storybook/stories/Text3D.stories.tsx index e0e2d7645..d7b2543c1 100644 --- a/.storybook/stories/Text3D.stories.tsx +++ b/.storybook/stories/Text3D.stories.tsx @@ -16,10 +16,6 @@ export default { ), ], - args: { - bevelEnabled: true, - bevelSize: 0.05, - }, } satisfies Meta type Story = StoryObj @@ -40,15 +36,9 @@ function Text3DScene(props: React.ComponentProps) { export const Text3DSt = { args: { font: '/fonts/helvetiker_regular.typeface.json', + bevelEnabled: true, + bevelSize: 0.05, }, render: (args) => , name: 'Default', } satisfies Story - -export const Text3DTtfSt = { - args: { - font: '/fonts/lemon-round.ttf', - }, - render: (args) => , - name: 'TTF', -} satisfies Story diff --git a/docs/abstractions/text3d.mdx b/docs/abstractions/text3d.mdx index dc03fcc39..2e2a18116 100644 --- a/docs/abstractions/text3d.mdx +++ b/docs/abstractions/text3d.mdx @@ -15,8 +15,6 @@ Render 3D text using ThreeJS's `TextGeometry`. 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. -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. - ```jsx Hello world! diff --git a/docs/loaders/use-font.mdx b/docs/loaders/use-font.mdx index a983beaed..2d45a1cd8 100644 --- a/docs/loaders/use-font.mdx +++ b/docs/loaders/use-font.mdx @@ -3,11 +3,11 @@ title: useFont sourcecode: src/core/useFont.tsx --- -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. +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. ```jsx const font = useFont('/fonts/helvetiker_regular.typeface.json') -return +return ``` In order to preload you do this: @@ -15,11 +15,3 @@ In order to preload you do this: ```jsx useFont.preload('/fonts/helvetiker_regular.typeface.json') ``` - -If the response from the URL is not a JSON, `THREE.TTFLoader` is used to try parsing the response as a standard font file. -However, keep in mind that the on-the-fly conversion to the JSON format will impact the loading time. - -```jsx -const font = useFont('/fonts/helvetiker_regular.ttf') -return -``` diff --git a/src/core/Text3D.tsx b/src/core/Text3D.tsx index 777654bb7..1aa809b9a 100644 --- a/src/core/Text3D.tsx +++ b/src/core/Text3D.tsx @@ -2,8 +2,9 @@ import * as React from 'react' import * as THREE from 'three' import { extend, MeshProps, Node } from '@react-three/fiber' import { useMemo } from 'react' -import { mergeVertices, TextGeometry, TextGeometryParameters } from 'three-stdlib' -import { useFont } from './useFont' +import { suspend } from 'suspend-react' +import { mergeVertices, TextGeometry, TextGeometryParameters, FontLoader } from 'three-stdlib' +import { useFont, FontData } from './useFont' import { ForwardRefComponent } from '../helpers/ts-utils' declare global { @@ -15,7 +16,7 @@ declare global { } type Text3DProps = { - font: Parameters[0] + font: FontData | string bevelSegments?: number smooth?: number } & Omit & diff --git a/src/core/useFont.tsx b/src/core/useFont.tsx index 4d1e373af..5e5200afc 100644 --- a/src/core/useFont.tsx +++ b/src/core/useFont.tsx @@ -1,4 +1,4 @@ -import { FontLoader, TTFLoader } from 'three-stdlib' +import { FontLoader } from 'three-stdlib' import { suspend, preload, clear } from 'suspend-react' export type Glyph = { @@ -22,7 +22,10 @@ export type FontData = { type FontInput = string | FontData let fontLoader: FontLoader | null = null -let ttfLoader: TTFLoader | null = null + +async function loadFontData(font: FontInput): Promise { + return typeof font === 'string' ? await (await fetch(font)).json() : font +} function parseFontData(fontData: FontData) { if (!fontLoader) { @@ -31,31 +34,9 @@ function parseFontData(fontData: FontData) { return fontLoader.parse(fontData) } -function parseTtfArrayBuffer(ttfData: ArrayBuffer) { - if (!ttfLoader) { - ttfLoader = new TTFLoader() - } - return ttfLoader.parse(ttfData) as FontData -} - -async function loadFontData(font: FontInput) { - if (typeof font === 'string') { - const res = await fetch(font) - - if (res.headers.get('Content-Type')?.includes('application/json')) { - return (await res.json()) as FontData - } else { - const arrayBuffer = await res.arrayBuffer() - return parseTtfArrayBuffer(arrayBuffer) - } - } else { - return font - } -} - async function loader(font: FontInput) { - const fontData = await loadFontData(font) - return parseFontData(fontData) + const data = await loadFontData(font) + return parseFontData(data) } export function useFont(font: FontInput) {