blitstr2: codegen: generate rustfmt-compatible code

Generate code that can be put through rustfmt without being altered.
This removes an issue where the font tables can be rearraged by simply
running `rustfmt` on the repository.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-12-27 14:10:48 +08:00
parent 9ebf8eb2fc
commit 22546710df
2 changed files with 13 additions and 12 deletions

View file

@ -72,13 +72,16 @@ func NewBlitPattern(img image.Image, font FontSpec, cs CharSpec, dbg bool) BlitP
// Convert blit pattern to rust source code for part of an array of bytes // Convert blit pattern to rust source code for part of an array of bytes
func ConvertPatternToRust(pattern BlitPattern) string { func ConvertPatternToRust(pattern BlitPattern) string {
patternStr := "" patternStr := " "
wordsPerRow := uint32(8) wordsPerRow := uint32(8)
ceilRow := uint32(len(pattern.Words)) / wordsPerRow ceilRow := uint32(len(pattern.Words)) / wordsPerRow
if uint32(len(pattern.Words))%wordsPerRow > 0 { if uint32(len(pattern.Words))%wordsPerRow > 0 {
ceilRow += 1 ceilRow += 1
} }
for i := uint32(0); i < ceilRow; i++ { for i := uint32(0); i < ceilRow; i++ {
if i != 0 {
patternStr += " "
}
start := i * wordsPerRow start := i * wordsPerRow
end := min(uint32(len(pattern.Words)), (i+1)*wordsPerRow) end := min(uint32(len(pattern.Words)), (i+1)*wordsPerRow)
line := pattern.Words[start:end] line := pattern.Words[start:end]
@ -86,9 +89,8 @@ func ConvertPatternToRust(pattern BlitPattern) string {
for _, word := range line { for _, word := range line {
s = append(s, fmt.Sprintf("0x%08x", word)) s = append(s, fmt.Sprintf("0x%08x", word))
} }
patternStr += strings.Join(s, ", ") + "," patternStr += strings.Join(s, ", ") + ",\n"
} }
patternStr += "\n"
return patternStr return patternStr
} }

View file

@ -144,31 +144,30 @@ pub const GLYPHS: [u32; {{.GS.GlyphsLen}}] = [
/// Widths for proportional glyphs /// Widths for proportional glyphs
pub const WIDTHS: [u8; {{.GS.WidthsLen}}] = [ pub const WIDTHS: [u8; {{.GS.WidthsLen}}] = [
{{.GS.Widths}}]; {{.GS.Widths}}];
{{end}}` {{end}}
`
const loaderFileTemplate = ` const loaderFileTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated.
// DO NOT MAKE EDITS HERE because this file is automatically generated.
// To make changes, see <xous_root>/services/graphics-server/src/blitstr2/codegen/main.go // To make changes, see <xous_root>/services/graphics-server/src/blitstr2/codegen/main.go
{{.Font.Legal}} {{.Font.Legal}}
//! {{.Font.Name}} Font //! {{.Font.Name}} Font
#![allow(dead_code)] #![allow(dead_code)]
#[link_section=".fontdata"] #[link_section = ".fontdata"]
#[no_mangle] #[no_mangle]
#[used] #[used]
/// Packed 16px * 16px glyph pattern data. /// Packed 16px * 16px glyph pattern data.
/// Pixels are packed in row-major order with LSB of first pixel word /// Pixels are packed in row-major order with LSB of first pixel word
/// containing the top left pixel. Bit of 0 means clear, 1 means set /// containing the top left pixel. Bit of 0 means clear, 1 means set
pub static {{.FontName}}_GLYPHS: [u32; {{.GS.GlyphsLen}}] = [ pub static {{.FontName}}_GLYPHS: [u32; {{.GS.GlyphsLen}}] = [
{{.GS.Glyphs}}]; {{.GS.Glyphs}}];{{if .GS.Widths}}{{end}}
{{if .GS.Widths}} `
{{end}}`
const loadermodTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated. const loadermodTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated.
// The order of these modules affects the link order in the loader, which is referred to in the graphics engine. // 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 // To make changes, see <xous_root>/services/graphics-server/src/blitstr2/codegen/main.go
{{range $f := .FontDir}}
{{range $f := .FontDir}}pub mod {{$f.Name}}; pub mod {{$f.Name}};
{{end}}` {{end}}`
const fontmapTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated. const fontmapTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated.