-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add SVG font rendering support (dead end)
I'm abandoning this as the SVG emoji font I wanted to load contains one huge 14MB SVG, with a 3MB defs block, which is referenced by all glyphs. Glyphs are stored as groups an id attribute specifying the glyph name. SVG fonts seem to support glyphs as separate SVG files, but the emoji font has a lot of reuse for different variants (skin tone, etc), which I assume is why they use one massive SVG file. Due to this organisation, it's not really feasible to read SVG emoji glyphs on the Pico (at least for someone who values their time). Caching offsets in the SVG to build smaller SVGs dynamically would probably also run into memory limitations as the 11,300 def nodes in this font have 88KB of ID strings (ignoring null bytes) that would need an additional 44KB of pointers to index - so almost half the Pico's memory just to index this one font. The combined length of any one glyph plus the defs it uses is only around 24KB, so if the SVGs were organised for low-memory reads instead of element reuse, it wouldn't be a problem to render them using lunasvg as I intended (assuming lunasvg also keeps its memory use under control).
- Loading branch information
Showing
5 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "svg.hh" | ||
|
||
#include <lunasvg.h> | ||
|
||
#ifndef FT_CONFIG_OPTION_SVG | ||
#error "SVG support is disabled in this build of FreeType" | ||
#endif | ||
|
||
FT_Error lunasvg_port_init(FT_Pointer *state) | ||
{ | ||
printf("lunasvg_port_init\n"); | ||
return FT_Err_Ok; | ||
} | ||
|
||
void lunasvg_port_free(FT_Pointer *state) | ||
{ | ||
printf("lunasvg_port_free\n"); | ||
} | ||
|
||
FT_Error lunasvg_port_render(FT_GlyphSlot slot, FT_Pointer *state) | ||
{ | ||
// Note these are pixel sizes, not points | ||
const int width = slot->bitmap.width; | ||
const int height = slot->bitmap.rows; | ||
const int stride = slot->bitmap.pitch; | ||
|
||
printf("Size: %d x %d x %d\n", width, height); | ||
printf("Buffer: %lu\n", slot->bitmap.buffer); | ||
|
||
// auto document = lunasvg::Document::loadFromData(); | ||
// auto bitmap = document->renderToBitmap(width, height); | ||
|
||
// const double rootWidth = document->width(); | ||
// const double rootHeight = document->height(); | ||
|
||
// // LunaSVG uses ARGB, so this is probably wrong for FreeType | ||
// lunasvg::Bitmap bitmap((uint8_t*) slot->bitmap.buffer, width, height, stride); | ||
// lunasvg::Matrix matrix(width / rootWidth, 0, 0, height / rootHeight, 0, 0); | ||
|
||
// bitmap.clear(0); | ||
// document->render(bitmap, matrix); | ||
|
||
// slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA; | ||
// slot->bitmap.num_grays = 256; | ||
// slot->format = FT_GLYPH_FORMAT_BITMAP; | ||
|
||
printf("lunasvg_port_render\n"); | ||
|
||
return FT_Err_Ok; | ||
} | ||
|
||
FT_Error lunasvg_port_preset_slot(FT_GlyphSlot slot, FT_Bool cache, FT_Pointer *state) | ||
{ | ||
// Example: https://gitlab.freedesktop.org/freetype/freetype-demos/-/blob/master/src/rsvg-port.c | ||
// API docs: https://freetype.org/freetype2/docs/reference/ft2-svg_fonts.html | ||
|
||
FT_SVG_Document document = (FT_SVG_Document) slot->other; | ||
FT_Size_Metrics metrics = document->metrics; | ||
|
||
// document->svg_document; | ||
// document->svg_document_length; | ||
|
||
printf("lunasvg_port_preset_slot with SVG size %lu\n", document->svg_document_length); | ||
|
||
return FT_Err_Ok; | ||
} | ||
|
||
SVG_RendererHooks lunasvg_hooks = { | ||
(SVG_Lib_Init_Func) lunasvg_port_init, | ||
(SVG_Lib_Free_Func) lunasvg_port_free, | ||
(SVG_Lib_Render_Func) lunasvg_port_render, | ||
(SVG_Lib_Preset_Slot_Func) lunasvg_port_preset_slot | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <ft2build.h> | ||
#include <freetype/otsvg.h> | ||
#include <freetype/freetype.h> | ||
|
||
FT_Error lunasvg_port_init(FT_Pointer *state); | ||
void lunasvg_port_free(FT_Pointer *state); | ||
FT_Error lunasvg_port_render(FT_GlyphSlot slot, FT_Pointer *state); | ||
FT_Error lunasvg_port_preset_slot(FT_GlyphSlot slot, FT_Bool cache, FT_Pointer *state); | ||
|
||
extern SVG_RendererHooks lunasvg_hooks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters