Created
July 4, 2021 02:51
-
-
Save lzpel/87c07e357255955bfd0539dc6fdbcf69 to your computer and use it in GitHub Desktop.
日本語OpenTypeを画像に描画
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/image/font" | |
"golang.org/x/image/font/opentype" | |
"golang.org/x/image/math/fixed" | |
"image" | |
"image/color" | |
"image/png" | |
"io/ioutil" | |
"log" | |
"os" | |
) | |
func main() { | |
const ( | |
width = 360 | |
height = 36 | |
startingDotX = 6 | |
startingDotY = 28 | |
) | |
fontdata,_:=ioutil.ReadFile("Chalk-JP.otf") | |
f, err := opentype.Parse(fontdata) | |
if err != nil { | |
log.Fatalf("Parse: %v", err) | |
} | |
face, err := opentype.NewFace(f, &opentype.FaceOptions{ | |
Size: 32, | |
DPI: 72, | |
Hinting: font.HintingNone, | |
}) | |
if err != nil { | |
log.Fatalf("NewFace: %v", err) | |
} | |
dst := image.NewGray(image.Rect(0, 0, width, height)) | |
d := font.Drawer{ | |
Dst: dst, | |
Src: image.White, | |
Face: face, | |
Dot: fixed.P(startingDotX, startingDotY), | |
} | |
fmt.Printf("The dot is at %v\n", d.Dot) | |
d.DrawString("我思う、故に") | |
fmt.Printf("The dot is at %v\n", d.Dot) | |
d.Src = image.NewUniform(color.Gray{0x7F}) | |
d.DrawString("我在り") | |
fmt.Printf("The dot is at %v\n", d.Dot) | |
pOutput, err := os.Create("./new.png") | |
if err != nil { | |
panic(err) | |
} | |
defer pOutput.Close() | |
err = png.Encode(pOutput, dst) | |
if err != nil { | |
panic(err) | |
} | |
} |
Author
lzpel
commented
Jul 4, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment