Last active
January 20, 2017 11:53
-
-
Save misodengaku/20c65360dd440a98031a4f3df591a1ae to your computer and use it in GitHub Desktop.
美咲フォント(misaki_gothic.png, misaki_4x8_jisx0201.png)を切り出す
This file contains hidden or 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" | |
| "image" | |
| "image/png" | |
| "io/ioutil" | |
| "os" | |
| "strings" | |
| "golang.org/x/text/encoding/japanese" | |
| "golang.org/x/text/transform" | |
| ) | |
| // サイズを7x7, 3x7にする | |
| // 一部の記号以外には影響なし | |
| const Use7x7Font bool = true | |
| // ファイル書き出しを有効にする | |
| const FileSave bool = false | |
| // 切り出す対象の文字 | |
| var convertStr = "、aあいうアイウアイウ亜火不" | |
| func main() { | |
| file, err := os.Open("misaki_gothic.png") | |
| if err != nil { | |
| fmt.Println(err) | |
| file.Close() | |
| return | |
| } | |
| jisx0208Img, _, err := image.Decode(file) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| file.Close() | |
| file, err = os.Open("misaki_4x8_jisx0201.png") | |
| if err != nil { | |
| file.Close() | |
| fmt.Println(err) | |
| return | |
| } | |
| jisx0201Img, _, err := image.Decode(file) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| file.Close() | |
| // sjisStr, _ := | |
| for _, v := range convertStr { | |
| c, _ := utf8ToSjis(string(v)) | |
| kuRaw := c[0] | |
| ten := byte(0) | |
| if kuRaw >= 0x81 && kuRaw <= 0x9f { | |
| ten = c[1] | |
| ku := (kuRaw - 0x80) * 2 | |
| if ten < 0x9f { | |
| ku-- | |
| ten = ten - 0x40 | |
| } else { | |
| ten = ten - 0x9f | |
| } | |
| fmt.Printf("%s,\t%02d区,%02x,%02x\r\n", string(v), ku, kuRaw, ten) | |
| xSrc := int(ten) * 8 | |
| xDst := (int(ten) + 1) * 8 | |
| ySrc := (int(ku) - 1) * 8 | |
| yDst := int(ku) * 8 | |
| if Use7x7Font { | |
| xDst-- | |
| yDst-- | |
| } | |
| fmt.Printf("%dx%d - %dx%d\r\n", xSrc, ySrc, xDst, yDst) | |
| rect := image.Rect(xSrc, ySrc, xDst, yDst) | |
| fontImg := jisx0208Img.(interface { | |
| SubImage(r image.Rectangle) image.Image | |
| }).SubImage(rect) | |
| if FileSave { | |
| outputFile, err := os.Create(string(v) + ".png") | |
| if nil != err { | |
| fmt.Println(err) | |
| } | |
| png.Encode(outputFile, fontImg) | |
| outputFile.Close() | |
| } | |
| } else if kuRaw < 0x80 || kuRaw >= 0xa0 { | |
| kuH := int(kuRaw) >> 4 & 0x0f | |
| kuL := int(kuRaw) & 0x0f | |
| ySrc := kuH * 8 | |
| yDst := (kuH + 1) * 8 | |
| xSrc := kuL * 4 | |
| xDst := (kuL + 1) * 4 | |
| if Use7x7Font { | |
| xDst-- | |
| yDst-- | |
| } | |
| fmt.Printf("%s,\t%02x\r\n", string(v), kuRaw) | |
| fmt.Printf("%dx%d - %dx%d\r\n", xSrc, ySrc, xDst, yDst) | |
| rect := image.Rect(xSrc, ySrc, xDst, yDst) | |
| fontImg := jisx0201Img.(interface { | |
| SubImage(r image.Rectangle) image.Image | |
| }).SubImage(rect) | |
| if FileSave { | |
| outputFile, err := os.Create(string(v) + ".png") | |
| if nil != err { | |
| fmt.Println(err) | |
| } | |
| png.Encode(outputFile, fontImg) | |
| outputFile.Close() | |
| } | |
| } else { | |
| fmt.Printf("%s,\t%02x\r\n", string(v), kuRaw) | |
| } | |
| } | |
| } | |
| // UTF-8 から ShiftJIS | |
| func utf8ToSjis(str string) (string, error) { | |
| ret, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(str), japanese.ShiftJIS.NewEncoder())) | |
| if err != nil { | |
| return "", err | |
| } | |
| return string(ret), err | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment