Skip to content

Commit 3852093

Browse files
Site changes [skip-ci]
1 parent dfb6b1a commit 3852093

File tree

3 files changed

+67367
-66681
lines changed

3 files changed

+67367
-66681
lines changed

llms-full.txt

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4899,11 +4899,6 @@ Fonts added to your project are automatically converted into a texture format th
48994899
- Bitmap
49004900
- Distance field
49014901

4902-
<div class='sidenote' markdown='1'>
4903-
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.
4904-
</div>
4905-
4906-
49074902
## Creating a font
49084903

49094904
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>.
@@ -4980,7 +4975,7 @@ space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E
49804975
*Cache Width/Height*
49814976
: 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.`).
49824977

4983-
If set to 0 the cache size is set automatically.
4978+
If set to 0 the cache size is set automatically, and will grow to 2048x4096 max.
49844979

49854980
## Distance field fonts
49864981

@@ -5051,6 +5046,81 @@ For example - to generate a gradient in a shader fragment, simply write:
50515046

50525047
For more information about shader uniforms, see the [Shader manual](#manuals:shader).
50535048

5049+
## Runtime generation
5050+
5051+
It is possible to use runtime generation for SDF type fonts, when using TrueType (.ttf) fonts.
5052+
This approach can greatly reduce the download size and runtime memory consumption of a Defold game.
5053+
The small downside is a very small delay for each glyph generated at runtime.
5054+
5055+
Enable the feature by setting `font.runtime_generation` in game.project.
5056+
5057+
<div class='sidenote' markdown='1'>
5058+
This feature is currently experimental, but with the intention to be used as the default workflow in the future.
5059+
</div>
5060+
5061+
<div class='important' markdown='1'>
5062+
This setting affects all .ttf fonts in the project.
5063+
</div>
5064+
5065+
### Prewarming glyph cache
5066+
5067+
In order to make the runtime fonts easier to use, they support prewarming of the glyph cache.
5068+
This means the font will generate the glyphs listed in *Characters* in the font.
5069+
5070+
<div class='sidenote' markdown='1'>
5071+
If `All Chars` is selected, there will be no prewarming as it defeats the purpose of not having to generate all glyphs at the same time.
5072+
</div>
5073+
5074+
### Font Scripting
5075+
5076+
For runtime fonts, it's possible to add or removed sub fonts.
5077+
This is useful when a large font has been split up into multiple files for different character sets (e.g. CJK)
5078+
5079+
<div class='important' markdown='1'>
5080+
Adding a subfont doesn't automatically load or render all the glyphs.
5081+
</div>
5082+
5083+
```lua
5084+
-- Add the range A-Z to the .fontc
5085+
local font_hash = hash("/assets/fonts/roboto.fontc")
5086+
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
5087+
local codepoint_min = 0x00000041 -- A
5088+
local codepoint_max = 0x0000005A -- Z
5089+
font.add_source(font_hash, ttf_hash, codepoint_min, codepoint_max)
5090+
```
5091+
5092+
```lua
5093+
-- Remove the associated ttf resource
5094+
local font_hash = hash("/assets/fonts/roboto.fontc")
5095+
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
5096+
font.remove_source(font_hash, ttf_hash)
5097+
```
5098+
5099+
To load the glyphs to the font, you will need to call the `font.add_glyphs()`.
5100+
It is an asynchronous operation, and once it's done, it's safe to progress to show any message containing the glyphs.
5101+
5102+
```lua
5103+
local function add_glyph_callback(self, id, result, errmsg)
5104+
if not result then
5105+
print("Request " .. id .." finished with error:", errmsg)
5106+
else
5107+
msg.post(some_url, "show_dialog")
5108+
end
5109+
end
5110+
5111+
-- Load glyphs into the font
5112+
local font_hash = hash("/assets/fonts/roboto.fontc")
5113+
local glyphs = "Some text to be shown!" -- for optimal performance, make this a list of unique glyphs
5114+
local request_id = font.add_glyphs(font_hash, ttf_hash, add_glyph_callback)
5115+
```
5116+
5117+
And, once the characters aren't needed anymore, you can discard that memory:
5118+
```lua
5119+
-- Remove the associated ttf resource
5120+
local font_hash = hash("/assets/fonts/roboto.fontc")
5121+
font.remove_glyphs(font_hash, "All the characters in the set")
5122+
```
5123+
50545124
<!-- /manuals/resource -->
50555125

50565126
# Resource management {#manuals:resource}

manuals/font.md

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ toc:
1212
- Bitmap BMFonts
1313
- Artifacts and best practices
1414
- Font Cache
15+
- Runtime generation
16+
- Prewarming glyph cache
17+
- Font Scripting
1518
---
1619

1720
# Font files
@@ -27,11 +30,6 @@ Fonts added to your project are automatically converted into a texture format th
2730
- Bitmap
2831
- Distance field
2932

30-
<div class='sidenote' markdown='1'>
31-
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.
32-
</div>
33-
34-
3533
## Creating a font
3634

3735
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>.
@@ -108,7 +106,7 @@ space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E
108106
*Cache Width/Height*
109107
: 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.`).
110108

111-
If set to 0 the cache size is set automatically.
109+
If set to 0 the cache size is set automatically, and will grow to 2048x4096 max.
112110

113111
## Distance field fonts
114112

@@ -177,4 +175,79 @@ For example - to generate a gradient in a shader fragment, simply write:
177175

178176
`float horizontal_gradient = fract(var_texcoord0.y / texture_size_recip.w);`
179177

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

0 commit comments

Comments
 (0)