Skip to content

Commit

Permalink
Remove font regeneration
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Lewis <[email protected]>
  • Loading branch information
gmlewis committed Dec 1, 2023
1 parent 0e11376 commit 0917562
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
40 changes: 30 additions & 10 deletions cmd/font2lua/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"unicode/utf8"
Expand All @@ -28,8 +29,12 @@ import (
"golang.org/x/exp/maps"
)

const (
maxMluaUnicode = 8204
)

var (
debug = flag.Bool("debug", false, "Turn on debugging info")
debug = flag.String("debug", "", "Turn on debugging info for specific glyph ('all' for all glyphs)")

outTemp = template.Must(template.New("out").Funcs(funcMap).Parse(luaTemplate))
funcMap = template.FuncMap{
Expand All @@ -45,7 +50,7 @@ func main() {
flag.Parse()

for _, arg := range flag.Args() {
logf("Processing file %q ...", arg)
logf("all", "Processing file %q ...", arg)

fontData := &webfont.FontData{}
if buf, err := os.ReadFile(arg); err != nil {
Expand Down Expand Up @@ -130,10 +135,12 @@ func (p *processor) ProcessGlyph(r rune, g *webfont.Glyph) {
if g.GerberLP != nil {
glyph.gerberLP = *g.GerberLP
}
if glyph.gerberLP != "" {
logf("glyph.gerberLP=%q", glyph.gerberLP)
glyph.regenerateFace()
}
/*
if glyph.gerberLP != "" {
logf(glyph.unicode, "r=%q, unicode=%q, glyph.gerberLP=%q", r, glyph.unicode, glyph.gerberLP)
glyph.regenerateFace()
}
*/
}

func (p *processor) addCmd(glyph *glyphT, cmd string, x, y float64, absCmd string) {
Expand Down Expand Up @@ -171,7 +178,7 @@ func mySprintf(fmtStr string, coords ...float64) string {

func (p *processor) MoveTo(g *webfont.Glyph, cmd string, x, y float64) {
glyph := p.current
logf("p.MoveTo(g,%q,%v,%v)", cmd, x+glyph.xmin, y+glyph.ymin)
logf(glyph.unicode, "p.MoveTo(g,%q,%v,%v)", cmd, x+glyph.xmin, y+glyph.ymin)
absCmd := mySprintf("M%v %v", x+glyph.xmin, y+glyph.ymin)
p.addCmd(glyph, cmd, x, y, absCmd)
}
Expand Down Expand Up @@ -239,7 +246,7 @@ func (g *glyphT) regenerateFace() {
g.findCutPoints()

for i, face := range g.faces {
logf("face[%v] new absCmd:\n%v", i, strings.Join(face.absCmds, ""))
logf(g.unicode, "face[%v] new absCmd:\n%v", i, strings.Join(face.absCmds, ""))
}

var d strings.Builder
Expand Down Expand Up @@ -292,7 +299,20 @@ func writeFont(fontData *webfont.FontData) {
}
}

charOrHex := fmt.Sprintf("%q", unicode)
if strings.HasPrefix(charOrHex, `"\u`) {
v, err := strconv.ParseInt(charOrHex[3:len(charOrHex)-1], 16, 64)
if err != nil {
log.Fatalf("unable to parse hex '%v': %v", charOrHex, err)
}
if v >= maxMluaUnicode {
continue // too large for mlua (not for gopher-lua coincidentally)
}
charOrHex = fmt.Sprintf(`"\%v"`, v)
}

lines = append(lines, fmt.Sprintf("%v={", luaChar))
lines = append(lines, fmt.Sprintf(` char=%v,`, charOrHex))
lines = append(lines, fmt.Sprintf(" horiz_adv_x=%v,", glyph.horizAdvX))
// lines = append(lines, fmt.Sprintf(` gerber_lp="%v",`, glyph.gerberLP))
lines = append(lines, fmt.Sprintf(` d="%v",`, glyph.d))
Expand All @@ -319,8 +339,8 @@ func writeFont(fontData *webfont.FontData) {
}
}

func logf(fmt string, args ...any) {
if *debug {
func logf(unicode, fmt string, args ...any) {
if *debug == "all" || *debug == unicode {
log.Printf(fmt, args...)
}
}
Expand Down
3 changes: 3 additions & 0 deletions gen-lua-fonts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -ex
# -*- compile-command: "./gen-fonts.sh"; -*-
go run cmd/font2lua/*.go fonts/*/*.svg
14 changes: 9 additions & 5 deletions webfont/webfont.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"unicode/utf8"
)

const (
maxMluaUnicode = 8204
)

// Processor is an interface used to process glyphs.
type Processor interface {
// NewGlyph is called before the processing of a new glyph.
Expand Down Expand Up @@ -50,7 +54,7 @@ func ParseNeededGlyphs(fontData *FontData, message string, processor Processor)

// Fix UTF8 rune errors and de-duplicate identical code points.
dedup := map[rune]*Glyph{}
var dst rune = 0xfbf0
var dst rune = maxMluaUnicode - 1
for _, g := range fontData.Font.Glyphs {
if g.Unicode == nil {
continue
Expand All @@ -64,19 +68,19 @@ func ParseNeededGlyphs(fontData *FontData, message string, processor Processor)
}
if _, ok := dedup[r]; ok {
if dst == 0xfeff { // BOM - disallowed in Go source.
dst++
dst--
}
for {
if _, ok := dedup[dst]; !ok {
break
}
dst++
dst--
}
log.Printf("WARNING: unicode %+q found multiple times in font. Moving code point to %+q", r, dst)
// log.Printf("WARNING: unicode %+q found multiple times in font. Moving code point to %+q", r, dst)
rs := fmt.Sprintf("%c", dst)
g.Unicode = &rs
dedup[dst] = g
dst++
dst--
continue
}
rs := fmt.Sprintf("%c", r)
Expand Down

0 comments on commit 0917562

Please sign in to comment.