add target hooks to font generation (part I)

a "small" memory target is created, which maps only a subset
of fonts to memory for targets that have extremely limited amounts
of on-chip storage.

the main thing I'm not happy about with this change is that the
origin offset of the font table is hard-coded into the template,
but also, I'm not quite sure if there's a better way to do this
that doesn't make things unnecessarily complicated. The origin
offset *is* target specific, and the templates already
reference targets specifically in the config lists...so if this
were to be made more generic, the path you'd end up on is something
like parsing link.x files out of various configurations which seems
like an extreme amount of work for a constant that rarely changes.

So, it's a footgun, but one where mending the foot is several times
less effort than putting a safety on the footgun.
This commit is contained in:
bunnie 2024-01-28 02:11:09 +08:00
parent 9f8cfde417
commit 334495aaec
5 changed files with 57 additions and 25 deletions

View file

@ -21,7 +21,8 @@
"indexType": "txt-row-major",
"glyphTrim": "CJK",
"rustout": "../fonts/emoji.rs",
"loaderout": "../../../../../loader/src/fonts/emoji.rs"
"loaderout": "../../../../../loader/src/fonts/emoji.rs",
"small": true
},
{
"name": "zh",
@ -35,7 +36,8 @@
"indexType": "txt-row-major",
"glyphTrim": "CJK",
"rustout": "../fonts/zh.rs",
"loaderout": "../../../../../loader/src/fonts/zh.rs"
"loaderout": "../../../../../loader/src/fonts/zh.rs",
"small": false
},
{
"name": "ja",
@ -49,7 +51,8 @@
"indexType": "txt-row-major",
"glyphTrim": "CJK",
"rustout": "../fonts/ja.rs",
"loaderout": "../../../../../loader/src/fonts/ja.rs"
"loaderout": "../../../../../loader/src/fonts/ja.rs",
"small": false
},
{
"name": "kr",
@ -63,7 +66,8 @@
"indexType": "txt-row-major",
"glyphTrim": "CJK",
"rustout": "../fonts/kr.rs",
"loaderout": "../../../../../loader/src/fonts/kr.rs"
"loaderout": "../../../../../loader/src/fonts/kr.rs",
"small": false
},
{
"name": "Bold",
@ -77,7 +81,8 @@
"indexType": "json-grid-coord",
"glyphTrim": "proportional",
"rustout": "../fonts/bold.rs",
"loaderout": "../../../../../loader/src/fonts/bold.rs"
"loaderout": "../../../../../loader/src/fonts/bold.rs",
"small": true
},
{
"name": "Mono",
@ -91,7 +96,8 @@
"indexType": "json-grid-coord",
"glyphTrim": "monospace",
"rustout": "../fonts/mono.rs",
"loaderout": "../../../../../loader/src/fonts/mono.rs"
"loaderout": "../../../../../loader/src/fonts/mono.rs",
"small": true
},
{
"name": "Regular",
@ -105,7 +111,8 @@
"indexType": "json-grid-coord",
"glyphTrim": "proportional",
"rustout": "../fonts/regular.rs",
"loaderout": "../../../../../loader/src/fonts/regular.rs"
"loaderout": "../../../../../loader/src/fonts/regular.rs",
"small": false
},
{
"name": "Tall",
@ -119,7 +126,8 @@
"indexType": "json-grid-coord",
"glyphTrim": "proportional",
"rustout": "../fonts/tall.rs",
"loaderout": "../../../../../loader/src/fonts/tall.rs"
"loaderout": "../../../../../loader/src/fonts/tall.rs",
"small": true
},
{
"name": "Small",
@ -133,7 +141,8 @@
"indexType": "json-grid-coord",
"glyphTrim": "proportional",
"rustout": "../fonts/small.rs",
"loaderout": "../../../../../loader/src/fonts/small.rs"
"loaderout": "../../../../../loader/src/fonts/small.rs",
"small": false
}
]
}
}

View file

@ -32,6 +32,7 @@ type ConfigGlyphSet struct {
GlyphTrim string
RustOut string
LoaderOut string
Small bool
}
// Holds data parsed from a json index file
@ -71,6 +72,7 @@ func (c Config) Fonts() []FontSpec {
gs.codepointMap(),
gs.RustOut, gs.GlyphTrim,
gs.LoaderOut,
gs.Small,
}
list = append(list, fs)
}

View file

@ -16,6 +16,7 @@ type FontSpec struct {
RustOut string // Where should the generated rust source code go?
GlyphTrim string // How should bitmap glyphs be trimmed (proportional or CJK)?
LoaderOut string // Path to the split of the glyph data into the loader, to reduce the RAM load
Small bool // Is this font part of the "small" configuration set
}
// Look up trim limits based on row & column in glyph grid

View file

@ -35,8 +35,8 @@ func RenderLoaderFileTemplate(f FontSpec, gs GlyphSet) string {
context := loaderFileTemplateContext{f, gs, fname}
return renderTemplate(loaderFileTemplate, "loaderfile", context)
}
func RenderFontmapTemplate(fd []FontMap) string {
context := fontmapTemplateContext{fd}
func RenderFontmapTemplate(fd []FontMap, small_fd []FontMap) string {
context := fontmapTemplateContext{fd, small_fd}
return renderTemplate(fontmapTemplate, "fontmap", context)
}
func RenderLoadermodTemplate(fd []FontSummary) string {
@ -67,7 +67,8 @@ type loaderFileTemplateContext struct {
// Holds data for rendering fontmapTemplate
type fontmapTemplateContext struct {
FontDir []FontMap
FontDir []FontMap
SmallFontDir []FontMap
}
type loadermodTemplateContext struct {
FontDir []FontSummary
@ -120,12 +121,12 @@ pub const MAX_HEIGHT: u8 = {{.Font.Size}};
pub const CODEPOINTS: [u32; {{.GS.CodepointsLen}}] = [
{{.GS.Codepoints}}];
#[cfg(any(feature="precursor", feature="renode"))]
#[cfg(any(feature="precursor", feature="renode", feature="cramium-soc"))]
pub(crate) static GLYPH_LOCATION: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);
pub(crate) const GLYPH_LEN: usize = {{.GS.GlyphsLen}};
pub(crate) fn glyphs() -> &'static [u32] {
#[cfg(any(feature="precursor", feature="renode"))]
#[cfg(any(feature="precursor", feature="renode", feature="cramium-soc"))]
unsafe {
let data: *const u32 = core::mem::transmute(GLYPH_LOCATION.load(core::sync::atomic::Ordering::SeqCst));
core::slice::from_raw_parts(data, GLYPH_LEN)
@ -177,5 +178,15 @@ const fontmapTemplate = `#![cfg_attr(rustfmt, rustfmt_skip)]
// The order of these modules affects the link order in the loader, which is referred to in the graphics engine.
// To make changes, see <xous_root>/services/graphics-server/src/blitstr2/codegen/main.go
#![allow(dead_code)]
{{range $f := .FontDir}}pub const {{$f.Name}}: usize = 0x{{$f.Len}};
{{end}}`
#[cfg(not(feature = "cramium-soc"))]
pub const FONT_BASE: usize = 0x2053_0000;
#[cfg(feature = "cramium-soc")]
pub const FONT_BASE: usize = 0x6001_0000;
{{range $f := .FontDir}}#[cfg(not(feature = "cramium-soc"))]
pub const {{$f.Name}}: usize = 0x{{$f.Len}};
{{end}}
{{range $fs := .SmallFontDir}}#[cfg(feature = "cramium-soc")]
pub const {{$fs.Name}}: usize = 0x{{$fs.Len}};
{{end}}
`

View file

@ -14,9 +14,6 @@ import (
"strings"
)
// Location of the font base in the loader
const font_base = 0x2053_0000
// Command line switch to confirm intent of writing output files
const confirm = "--write"
@ -47,11 +44,9 @@ func codegen() {
fsList := conf.Fonts()
var fdir []FontSummary
var offsets []FontMap
var small_offsets []FontMap
var cur_address int = 0
offsets = append(offsets, lib.FontMap{
Name: "FONT_BASE",
Len: fmt.Sprintf("%x", font_base),
})
var small_cur_address int = 0
for _, f := range fsList {
// Find all the glyphs and pack them into a list of blit pattern objects
pl := patternListFromSpriteSheet(f)
@ -75,18 +70,32 @@ func codegen() {
Len: fmt.Sprintf("%x", cur_address),
}
offsets = append(offsets, offset)
if f.Small {
small_offsets = append(small_offsets, offset)
}
length := lib.FontMap{
Name: strings.ToUpper(f.Name) + "_LEN",
Len: fmt.Sprintf("%x", gs.GlyphsLen*4),
}
offsets = append(offsets, length)
if f.Small {
small_offsets = append(small_offsets, length)
}
cur_address = cur_address + gs.GlyphsLen*4
if f.Small {
small_cur_address = small_cur_address + gs.GlyphsLen*4
}
}
total := lib.FontMap{
Name: "FONT_TOTAL_LEN",
Len: fmt.Sprintf("%x", cur_address),
}
offsets = append(offsets, total)
small_total := lib.FontMap{
Name: "FONT_TOTAL_LEN",
Len: fmt.Sprintf("%x", small_cur_address),
}
small_offsets = append(small_offsets, small_total)
for _, fs := range fdir {
fmt.Println(fs)
}
@ -94,7 +103,7 @@ func codegen() {
fmt.Println("Writing to", conf.GetLoaderMod())
ioutil.WriteFile(conf.GetLoaderMod(), []byte(loadermod), 0644)
fontmap := RenderFontmapTemplate(offsets)
fontmap := RenderFontmapTemplate(offsets, small_offsets)
fmt.Println("Writing to", conf.GetFontMap())
ioutil.WriteFile(conf.GetFontMap(), []byte(fontmap), 0644)
}