From 22546710df848cb2c2d62781bfcb19c0397fff36 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 27 Dec 2023 14:10:48 +0800 Subject: [PATCH] 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 --- .../src/blitstr2/codegen/lib/blitpattern.go | 8 +++++--- .../src/blitstr2/codegen/lib/templates.go | 17 ++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/services/graphics-server/src/blitstr2/codegen/lib/blitpattern.go b/services/graphics-server/src/blitstr2/codegen/lib/blitpattern.go index ed2e8289e..52f1badd4 100644 --- a/services/graphics-server/src/blitstr2/codegen/lib/blitpattern.go +++ b/services/graphics-server/src/blitstr2/codegen/lib/blitpattern.go @@ -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 func ConvertPatternToRust(pattern BlitPattern) string { - patternStr := "" + patternStr := " " wordsPerRow := uint32(8) ceilRow := uint32(len(pattern.Words)) / wordsPerRow if uint32(len(pattern.Words))%wordsPerRow > 0 { ceilRow += 1 } for i := uint32(0); i < ceilRow; i++ { + if i != 0 { + patternStr += " " + } start := i * wordsPerRow end := min(uint32(len(pattern.Words)), (i+1)*wordsPerRow) line := pattern.Words[start:end] @@ -86,9 +89,8 @@ func ConvertPatternToRust(pattern BlitPattern) string { for _, word := range line { s = append(s, fmt.Sprintf("0x%08x", word)) } - patternStr += strings.Join(s, ", ") + "," + patternStr += strings.Join(s, ", ") + ",\n" } - patternStr += "\n" return patternStr } diff --git a/services/graphics-server/src/blitstr2/codegen/lib/templates.go b/services/graphics-server/src/blitstr2/codegen/lib/templates.go index e9bdc28e1..768cc2202 100644 --- a/services/graphics-server/src/blitstr2/codegen/lib/templates.go +++ b/services/graphics-server/src/blitstr2/codegen/lib/templates.go @@ -144,31 +144,30 @@ pub const GLYPHS: [u32; {{.GS.GlyphsLen}}] = [ /// Widths for proportional glyphs pub const WIDTHS: [u8; {{.GS.WidthsLen}}] = [ {{.GS.Widths}}]; -{{end}}` +{{end}} +` -const loaderFileTemplate = ` -// DO NOT MAKE EDITS HERE because this file is automatically generated. +const loaderFileTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated. // To make changes, see /services/graphics-server/src/blitstr2/codegen/main.go {{.Font.Legal}} //! {{.Font.Name}} Font #![allow(dead_code)] -#[link_section=".fontdata"] +#[link_section = ".fontdata"] #[no_mangle] #[used] /// Packed 16px * 16px glyph pattern data. /// 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 pub static {{.FontName}}_GLYPHS: [u32; {{.GS.GlyphsLen}}] = [ -{{.GS.Glyphs}}]; -{{if .GS.Widths}} -{{end}}` +{{.GS.Glyphs}}];{{if .GS.Widths}}{{end}} +` 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. // To make changes, see /services/graphics-server/src/blitstr2/codegen/main.go - -{{range $f := .FontDir}}pub mod {{$f.Name}}; +{{range $f := .FontDir}} +pub mod {{$f.Name}}; {{end}}` const fontmapTemplate = `// DO NOT MAKE EDITS HERE because this file is automatically generated.