Skip to content

Commit 99c1e70

Browse files
committed
Added updated font generation documentation
1 parent cc67d56 commit 99c1e70

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

docs/en/manuals/font.md

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ Fonts added to your project are automatically converted into a texture format th
1616
- Bitmap
1717
- Distance field
1818

19-
::: sidenote
20-
It is possible to [generate font glyphs at runtime](/extension-fontgen) from a bundled TrueType font instead of generating and including a font texture in the application bundle. This approach can greatly reduce the download size and runtime memory consumption of a Defold game.
21-
:::
22-
23-
2419
## Creating a font
2520

2621
To create a font for use in Defold, create a new Font file by selecting <kbd>File ▸ New...</kbd> from the menu, then select <kbd>Font</kbd>. You can also <kbd>right click</kbd> a location in the *Assets* browser and select <kbd>New... ▸ Font</kbd>.
@@ -97,7 +92,7 @@ space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E
9792
*Cache Width/Height*
9893
: Constrains the size of the glyph cache bitmap. When the engine renders text, it looks up the glyph from the cache bitmap. If it does not exist there, it will be added to the cache before rendering. If the cache bitmap is too small to contain all the glyphs the engine is asked to render, an error is signalled (`ERROR:RENDER: Out of available cache cells! Consider increasing cache_width or cache_height for the font.`).
9994

100-
If set to 0 the cache size is set automatically.
95+
If set to 0 the cache size is set automatically, and will grow to 2048x4096 max.
10196

10297
## Distance field fonts
10398

@@ -167,3 +162,79 @@ For example - to generate a gradient in a shader fragment, simply write:
167162
`float horizontal_gradient = fract(var_texcoord0.y / texture_size_recip.w);`
168163

169164
For more information about shader uniforms, see the [Shader manual](/manuals/shader).
165+
166+
## Runtime generation
167+
168+
It is possible to use runtime generation for SDF type fonts, when using TrueType (.ttf) fonts.
169+
This approach can greatly reduce the download size and runtime memory consumption of a Defold game.
170+
The small downside is a very small delay for each glyph generated at runtime.
171+
172+
Enable the feature by setting `font.runtime_generation` in game.project.
173+
174+
::: sidenote
175+
This feature is currently experimental, but with the intention to be used as the default workflow in the future.
176+
:::
177+
178+
::: important
179+
This setting affects all .ttf fonts in the project.
180+
:::
181+
182+
### Prewarming glyph cache
183+
184+
In order to make the runtime fonts easier to use, they support prewarming of the glyph cache.
185+
This means the font will generate the glyphs listed in *Characters* in the font.
186+
187+
::: sidenote
188+
If `All Chars` is selected, there will be no prewarming as it defeats the purpose of not having to generate load all glyphs at the same time.
189+
:::
190+
191+
### Font Scripting
192+
193+
For runtime fonts, it's possible to add or removed sub fonts.
194+
This is useful when a large font has been split up into multiple for different character sets (e.g. CJK)
195+
196+
::: important
197+
Adding a subfont doesn't automatically load or render all the glyphs.
198+
:::
199+
200+
```lua
201+
-- Add the range A-Z to the .fontc
202+
local font_hash = hash("/assets/fonts/roboto.fontc")
203+
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
204+
local codepoint_min = 0x00000041 -- A
205+
local codepoint_max = 0x0000005A -- Z
206+
font.add_source(font_hash, ttf_hash, codepoint_min, codepoint_max)
207+
```
208+
209+
```lua
210+
-- Remove the associated ttf resource
211+
local font_hash = hash("/assets/fonts/roboto.fontc")
212+
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
213+
font.remove_source(font_hash, ttf_hash)
214+
```
215+
216+
To load the glyphs to the font, you will need to call the `font.add_glyphs()`.
217+
It is an asynchronous operation, and once it's done, it's safe to progress to show any message containing the glyphs.
218+
219+
```lua
220+
local function add_glyph_callback(self, id, result, errmsg)
221+
if not result then
222+
print("Request " .. id .." finished with error:", errmsg)
223+
else
224+
msg.post(some_url, "show_dialog")
225+
end
226+
end
227+
228+
-- Load glyphs into the font
229+
local font_hash = hash("/assets/fonts/roboto.fontc")
230+
local glyphs = "Some text to be shown!" -- for optimal performance, make this a list of unique glyphs
231+
local request_id = font.add_glyphs(font_hash, ttf_hash, add_glyph_callback)
232+
```
233+
234+
And, once the characters aren't needed anymore, you can discard that memory:
235+
```lua
236+
-- Remove the associated ttf resource
237+
local font_hash = hash("/assets/fonts/roboto.fontc")
238+
font.remove_glyphs(font_hash, "All the characters in the set")
239+
```
240+

0 commit comments

Comments
 (0)