Last active
July 4, 2021 04:03
-
-
Save lzpel/8b8635d402b26c6d81ecb981dcd5d797 to your computer and use it in GitHub Desktop.
ネコと和解せよ
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 ( | |
"golang.org/x/image/draw" | |
"golang.org/x/image/font" | |
"golang.org/x/image/font/opentype" | |
"golang.org/x/image/math/fixed" | |
"image" | |
"image/color" | |
"image/jpeg" | |
"io/ioutil" | |
"log" | |
"os" | |
) | |
const ( | |
width = 360 | |
height = 72 | |
startingDotX = 6 | |
startingDotY = 28 | |
) | |
func drawMultiline(dest draw.Image, face font.Face, input string){ | |
d := font.Drawer{ | |
Dst: dest, | |
Src: image.White, | |
Face: face, | |
Dot: fixed.P(startingDotX, startingDotY), | |
} | |
for _, c := range input { | |
if c=='\n'{ | |
d.Dot.X=startingDotX<<6 | |
d.Dot.Y+=32<<6 | |
}else if c=='\t'{ | |
d.Src = image.NewUniform(color.RGBA{R:255,G:241,B:0,A:0xff}) | |
}else{ | |
d.DrawString(string(c)) | |
} | |
} | |
} | |
func main() { | |
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.NewRGBA(image.Rect(0, 0, width, height)) | |
drawMultiline(dst,face,"ネコと和解せよ\n\tイエス・キリスト") | |
pOutput, err := os.Create("./new.jpg") | |
if err != nil { | |
panic(err) | |
} | |
defer pOutput.Close() | |
err = jpeg.Encode(pOutput, dst, &jpeg.Options{ | |
Quality:80, | |
}) | |
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