Skip to content

Commit

Permalink
Support jpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
CaffeeLake committed Oct 7, 2024
1 parent c6482fc commit 995c508
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports: ./imports
exports: ./exports
imagetype: png
unicodestart: 0x000021
unicodeend: 0x00007E
captions: true
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module font2png

go 1.22
go 1.23

require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
gopkg.in/yaml.v3 v3.0.1
)

require golang.org/x/image v0.18.0 // indirect
require golang.org/x/image v0.21.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"image"
"image/draw"
"image/jpeg"
"image/png"
"log"
"os"
Expand All @@ -25,6 +26,7 @@ func main() {
type ConfigTypes struct {
Imports string `yaml:"imports"`
Exports string `yaml:"exports"`
ImageType string `yaml:"imagetype"`
UnicodeStart int32 `yaml:"unicodestart"`
UnicodeEnd int32 `yaml:"unicodeend"`
Captions bool `yaml:"captions"`
Expand Down Expand Up @@ -69,7 +71,7 @@ func cmd() {
}

img := image.NewRGBA(image.Rect(0, 0, 512, 512))
draw.Draw(img, img.Bounds(), image.Transparent, image.Point{}, draw.Src)
draw.Draw(img, img.Bounds(), image.White, image.Point{}, draw.Src)

c := freetype.NewContext()
c.SetDPI(72)
Expand All @@ -84,7 +86,7 @@ func cmd() {
log.Fatalln(err)
}

filename := fmt.Sprintf("%s/%s/u%06x.png", config.Exports, fontname, r)
filename := fmt.Sprintf("%s/%s/u%06x.%s", config.Exports, fontname, r, config.ImageType)
dir := filepath.Dir(filename)
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatalln(err)
Expand All @@ -93,9 +95,17 @@ func cmd() {
if err != nil {
log.Fatalln(err)
}
if err := png.Encode(f, img); err != nil {
log.Fatalln(err)

if config.ImageType == "png" {
if err := png.Encode(f, img); err != nil {
log.Fatalln(err)
}
} else if config.ImageType == "jpg" || config.ImageType == "jpeg" {
if err := jpeg.Encode(f, img, &jpeg.Options{Quality: 100}); err != nil {
log.Fatalln(err)
}
}

f.Close()

if config.Captions {
Expand Down

0 comments on commit 995c508

Please sign in to comment.