~whereswaldon/fontpreview

04c78fac6c85dd8bb327c9aff1941312a0db6272 — Chris Waldon 2 years ago c955fda main
feat: make default shaping parameters into flags

This commit allows the initial configuration for the preview to be
configured via command line flags.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 57 insertions(+), 15 deletions(-)

M main.go
M main.go => main.go +57 -15
@@ 5,6 5,8 @@ package main
// A simple Gio program. See https://gioui.org for more information.

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"


@@ 33,9 35,34 @@ type (
)

func main() {
	rtl := flag.Bool("rtl", false, "set initial text direction to right-to-left")
	script := flag.String("script", "Latin", "set initial text script (case sensitive)")
	language := flag.String("language", "EN", "set initial language (case sensitive)")
	sample := flag.String("sample", "abcdefghijklmnopqrstuvwxyz", "set preview text sample")
	font := flag.String("font", "", "path to initial previewing font; defaults to go regular")
	flag.Parse()

	if _, ok := nameToScript[*script]; !ok {
		log.Fatalf("unrecognized script name: %s", *script)
	}

	var collection = gofont.CollectionHB()

	if *font != "" {
		b, err := os.ReadFile(*font)
		if err != nil {
			log.Fatalf("failed to read font %s: %v", *font, err)
		}
		collection, err = bytesToFontCollection(b)
		if err != nil {
			log.Fatalf("failed to decode font %s: %v", *font, err)
		}

	}

	go func() {
		w := app.NewWindow()
		if err := loop(w); err != nil {
		if err := loop(w, *rtl, *script, *language, *sample, collection); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)


@@ 79,19 106,40 @@ type themeUpdate struct {
}

// newUI initializes a UI using the provided window.
func newUI(w *app.Window) *ui {
func newUI(w *app.Window, rtl bool, script, lang, sample string, fonts []text.FontFace) *ui {
	var ui ui
	ui.window = w
	ui.explorer = explorer.NewExplorer(w)
	ui.themeChan = make(chan themeUpdate, 1)
	ui.previewText = "abcdefghijklmnopqrstuvwxyz"
	ui.previewTheme = material.NewTheme(gofont.CollectionHB())
	ui.langEd.SetText("EN")
	ui.scriptEnum.Value = scriptNames[language.Latin]
	ui.previewText = sample
	ui.previewTheme = material.NewTheme(fonts)
	ui.langEd.SetText(lang)
	ui.previewTheme.Language = language.NewLanguage(lang)
	ui.scriptEnum.Value = scriptNames[nameToScript[script]]
	ui.previewTheme.Script = nameToScript[script]
	if rtl {
		ui.dirSwitch.Value = true
		ui.previewTheme.Direction = di.DirectionRTL
	}
	ui.scriptList.Axis = layout.Vertical
	return &ui
}

// bytesToFontCollection converts a slice of bytes representing font data into
// a slice of usable text.FontFace.
func bytesToFontCollection(b []byte) ([]text.FontFace, error) {
	hbFont, err := opentype.ParseHarfbuzz(b)
	if err != nil {
		return nil, fmt.Errorf("failed parsing font: %w", err)
	}
	return []text.FontFace{
		{
			Font: text.Font{},
			Face: hbFont,
		},
	}, nil
}

// chooseFont opens a local file explorer and searches for
// fonts. If it finds one, it will parse it and send an updated
// theme providing that font on the themeChan. If it fails


@@ 107,17 155,11 @@ func (ui *ui) chooseFont() {
		ui.themeChan <- themeUpdate{Err: err}
		return
	}
	hbFont, err := opentype.ParseHarfbuzz(b)
	collectionHB, err := bytesToFontCollection(b)
	if err != nil {
		ui.themeChan <- themeUpdate{Err: err}
		return
	}
	collectionHB := []text.FontFace{
		{
			Font: text.Font{},
			Face: hbFont,
		},
	}
	ui.themeChan <- themeUpdate{
		Theme: material.NewTheme(collectionHB),
	}


@@ 247,9 289,9 @@ func (ui *ui) layoutScriptPicker(gtx C, th *material.Theme) D {
	)
}

func loop(w *app.Window) error {
func loop(w *app.Window, rtl bool, script, lang, sample string, fonts []text.FontFace) error {
	th := material.NewTheme(gofont.Collection())
	ui := newUI(w)
	ui := newUI(w, rtl, script, lang, sample, fonts)
	var ops op.Ops
	for {
		select {