WIP: load custom fonts

Amolith created

Change summary

main.go | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 130 insertions(+), 14 deletions(-)

Detailed changes

main.go 🔗

@@ -1,13 +1,13 @@
 package main
 
 import (
-	_ "embed"
+	"embed"
 	"fmt"
 	"os"
 	"strings"
 
 	"gioui.org/app"
-	"gioui.org/font/gofont"
+	"gioui.org/font/opentype"
 	"gioui.org/io/system"
 	"gioui.org/layout"
 	"gioui.org/op"
@@ -29,8 +29,8 @@ var (
 	flagSiteTitleSize *int    = flag.IntP("sitetitlesize", "s", 10, "Size of font for site title")
 )
 
-//go:embed font.otf
-var fontEmbed []byte
+//go:embed fonts
+var fontEmbed embed.FS
 
 func main() {
 	flag.Parse()
@@ -48,26 +48,49 @@ func main() {
 	dateEdited := getGitDate(*flagInput)
 
 	// TODO: Render information to image
-	fmt.Println(postTitle, postSubtitle, postDate, postReadTime, siteTitle, dateEdited)
+	fmt.Printf(`Title:          %s
+Subtitle:       %s
+Read Time:      %d minutes
+Date Published: %s
+Date Edited:    %s
+Site Title:     %s
+`, postTitle, postSubtitle, postReadTime, postDate, dateEdited, siteTitle)
 
-	// fontFace, err := opentype.Parse(fontEmbed)
-	// if err != nil {
-	// 	fmt.Println("Error: Could not parse font")
-	// 	fmt.Println(err)
-	// 	os.Exit(1)
-	// }
+	collection := fontCollection()
 
 	go func() {
 		w := app.NewWindow(app.Title("p2c"), app.Size(unit.Dp(1200), unit.Dp(630)))
 		var ops op.Ops
-		th := material.NewTheme(gofont.Collection())
+		th := material.NewTheme(collection)
 		for e := range w.Events() {
 			switch e := e.(type) {
 			case system.FrameEvent:
 				gtx := layout.NewContext(&ops, e)
-				title := material.H3(th, postTitle)
-				title.Alignment = text.Middle
+
+				title := material.LabelStyle{
+					Font:      text.Font{Typeface: "Primary font", Variant: "", Style: text.Regular, Weight: text.Bold},
+					Alignment: text.Middle,
+					Text:      postTitle,
+					TextSize:  unit.Sp(float32(*flagPostTitleSize)),
+				}
 				title.Layout(gtx)
+
+				rTime := material.Body1(th, fmt.Sprint("Reading Time: ", postReadTime))
+				rTime.Alignment = text.Middle
+				rTime.Layout(gtx)
+
+				// pDate := material.Body1(th, fmt.Sprint("Published: ", postDate))
+				// pDate.Alignment = text.Middle
+				// pDate.Layout(gtx)
+
+				// eDate := material.Body1(th, fmt.Sprint("Last Edited: ", dateEdited))
+				// eDate.Alignment = text.Middle
+				// eDate.Layout(gtx)
+
+				// sTitle := material.Body1(th, siteTitle)
+				// sTitle.Alignment = text.Middle
+				// sTitle.Layout(gtx)
+
 				e.Frame(gtx.Ops)
 			}
 		}
@@ -76,6 +99,99 @@ func main() {
 	app.Main()
 }
 
+func fontCollection() []text.FontFace {
+	regularFaceBytes, err := fontEmbed.ReadFile("fonts/regular.otf")
+	if err != nil {
+		fmt.Println("Error: Could not read regular font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+	regularFace, err := opentype.Parse(regularFaceBytes)
+	if err != nil {
+		fmt.Println("Error: Could not parse regular font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	boldFaceBytes, err := fontEmbed.ReadFile("fonts/bold.otf")
+	if err != nil {
+		fmt.Println("Error: Could not read bold font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+	boldFace, err := opentype.Parse(boldFaceBytes)
+	if err != nil {
+		fmt.Println("Error: Could not parse bold font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	italicFaceBytes, err := fontEmbed.ReadFile("fonts/italic.otf")
+	if err != nil {
+		fmt.Println("Error: Could not read italic font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+	italicFace, err := opentype.Parse(italicFaceBytes)
+	if err != nil {
+		fmt.Println("Error: Could not parse italic font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	boldItalicFaceBytes, err := fontEmbed.ReadFile("fonts/bold-italic.otf")
+	if err != nil {
+		fmt.Println("Error: Could not read bold italic font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+	boldItalicFace, err := opentype.Parse(boldItalicFaceBytes)
+	if err != nil {
+		fmt.Println("Error: Could not parse bold italic font")
+		fmt.Println(err)
+		os.Exit(1)
+	}
+
+	return []text.FontFace{
+		{
+			Font: text.Font{
+				Typeface: "Primary font",
+				Variant:  "",
+				Style:    text.Regular,
+				Weight:   text.Normal,
+			},
+			Face: regularFace,
+		},
+		{
+			Font: text.Font{
+				Typeface: "Primary font",
+				Variant:  "",
+				Style:    text.Regular,
+				Weight:   text.Bold,
+			},
+			Face: boldFace,
+		},
+		{
+			Font: text.Font{
+				Typeface: "Primary font",
+				Variant:  "",
+				Style:    text.Italic,
+				Weight:   text.Normal,
+			},
+			Face: italicFace,
+		},
+		{
+			Font: text.Font{
+				Typeface: "Primary font",
+				Variant:  "",
+				Style:    text.Italic,
+				Weight:   text.Bold,
+			},
+			Face: boldItalicFace,
+		},
+	}
+}
+
 // Print help message
 func help() {
 	fmt.Println("\nUsage: p2c [options]")