| 1 | // fontgen is a commandline tool for generating pixel fonts supported by pixfont. |
| 2 | // First is to create an image of your pixel font in your favorite graphics |
| 3 | // program with your set of supported characters. Ex: |
| 4 | // |
| 5 | // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 |
| 6 | // |
| 7 | // Ensure that there is a solid color background, single-color font pixels (i.e. |
| 8 | // no anti-aliasing), and that a column of pixels separate each character to |
| 9 | // ensure best results. Then simply run: |
| 10 | // |
| 11 | // ./fontgen -img mypixelfont.png -o myfont |
| 12 | // |
| 13 | // Add myfont.go to your project, then just use Font.DrawString(...) to add |
| 14 | // text to your image! |
| 15 | // |
| 16 | package mainscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/ |
| 17 | |
| 18 | import ( |
| 19 | "flagscip-go gomod github.com/golang/go/src go1.13 flag/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 21 | "go/formatscip-go gomod github.com/golang/go/src go1.13 `go/format`/" |
| 22 | "imagescip-go gomod github.com/golang/go/src go1.13 image/" |
| 23 | "image/colorscip-go gomod github.com/golang/go/src go1.13 `image/color`/" |
| 24 | _ "image/gifscip-go gomod github.com/golang/go/src go1.13 `image/gif`/" |
| 25 | _ "image/jpegscip-go gomod github.com/golang/go/src go1.13 `image/jpeg`/" |
| 26 | _ "image/pngscip-go gomod github.com/golang/go/src go1.13 `image/png`/" |
| 27 | "io/ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/" |
| 28 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 29 | "regexpscip-go gomod github.com/golang/go/src go1.13 regexp/" |
| 30 | "sortscip-go gomod github.com/golang/go/src go1.13 sort/" |
| 31 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 32 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.13 `unicode/utf8`/" |
| 33 | |
| 34 | "github.com/pbnjay/pixfontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/" |
| 35 | ) |
| 36 | |
| 37 | var ( |
| 38 | imageNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/imageName. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Stringscip-go gomod github.com/golang/go/src go1.13 flag/String().("img", "", "image file to extract pixel font from") |
| 39 | startYscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startY. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Intscip-go gomod github.com/golang/go/src go1.13 flag/Int().("y", 0, "starting Y position") |
| 40 | heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Intscip-go gomod github.com/golang/go/src go1.13 flag/Int().("h", 0, "chop height") |
| 41 | startXscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startX. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Intscip-go gomod github.com/golang/go/src go1.13 flag/Int().("x", 0, "starting X position") |
| 42 | widthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/width. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Intscip-go gomod github.com/golang/go/src go1.13 flag/Int().("w", 0, "chop width") |
| 43 | alphabetscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/alphabet. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Stringscip-go gomod github.com/golang/go/src go1.13 flag/String().("a", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "alphabet to extract") |
| 44 | varWidthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/varWidth. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Boolscip-go gomod github.com/golang/go/src go1.13 flag/Bool().("v", false, "produce variable width font") |
| 45 | |
| 46 | textNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/textName. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Stringscip-go gomod github.com/golang/go/src go1.13 flag/String().("txt", "", "text file to extract pixel font from") |
| 47 | outNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/outName. = flagscip-go gomod github.com/golang/go/src go1.13 flag/.Stringscip-go gomod github.com/golang/go/src go1.13 flag/String().("o", "", "package name to create (becomes <myfont>.go)") |
| 48 | ) |
| 49 | |
| 50 | // packFont takes a mostly textual representation of a pixel font and |
| 51 | // packs it into a tight uint32 representation, returning that representation |
| 52 | // plus a "mapping" from character code to encoded position. |
| 53 | func packFontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/packFont().(w, h int, d map[rune]map[int]string) ([]uint32, map[rune]uint16) { |
| 54 | cm := make(map[rune]uint16) |
| 55 | |
| 56 | // Sort the glyph list so the representation is stable across different invocations |
| 57 | // of fontgen. |
| 58 | chs := make([]int, 0, len(d)) |
| 59 | for ch, _ := range d { |
| 60 | chs = append(chs, int(ch)) |
| 61 | } |
| 62 | sortscip-go gomod github.com/golang/go/src go1.13 sort/.IntSlicescip-go gomod github.com/golang/go/src go1.13 sort/IntSlice#(chs).Sortscip-go gomod github.com/golang/go/src go1.13 sort/IntSlice#Sort().() |
| 63 | |
| 64 | // convert from simple character encoding to packed bitfield |
| 65 | // NB fonts should be at most 32 pixels wide to fit in the uint32 |
| 66 | // (height is limited to uint8 255) |
| 67 | // |
| 68 | // This packed representation stores 1-4 glyphs in a single uint32 (per line). |
| 69 | // For efficiency, each glyph must be 8-bit aligned. Glyphs are stored "backwards" |
| 70 | // (leftmost pixel in LSB). |
| 71 | // Glyphs that will not fit in their entirety will be pushed to the next uint32. |
| 72 | // |
| 73 | // For example: |
| 74 | // An 8-pixel font can store 4 glyphs using one uint32 per line. |
| 75 | // A 9-pixel font can only store 2, because 9-bit values must be |
| 76 | // byte-aligned. |
| 77 | // A 17-pixel font can only store 1, because it is impossible to |
| 78 | // align two 17-bit values (totalling 34 bits) in 32. |
| 79 | // |
| 80 | // Lines are stored in consecutive uint32s. |
| 81 | // |
| 82 | // 24 16 8 0 |
| 83 | // | | | | |
| 84 | // 0 DDDD CCC BBBB A == 0b00001111000011100000111100000100 == 0x0f0e0f04 |
| 85 | // 1 D D C C B B A A == 0b00010001000100010001000100001010 == 0x1111110a |
| 86 | // 2 D D C BBBB A A == 0b00010001000000010000111100010001 == 0x11010f11 |
| 87 | // 3 D D C C B B AAAAA == 0b00010001000100010001000100011111 == 0x1111111f |
| 88 | // 4 DDDD CCC BBBB A A == 0b00001111000011100000111100010001 == 0x0f0e0f11 |
| 89 | // 5 EEEEE == 0b00000000000000000000000000011111 == 0x0000001f |
| 90 | // 6 E == 0b00000000000000000000000000000001 == 0x00000001 |
| 91 | // 7 EEEE == 0b00000000000000000000000000001111 == 0x0000000f |
| 92 | // 8 E == 0b00000000000000000000000000000001 == 0x00000001 |
| 93 | // 9 EEEEE == 0b00000000000000000000000000011111 == 0x0000001f |
| 94 | |
| 95 | u8PerCh := ((w - 1) >> 3) + 1 // 0-8 take up 1 byte, 9-16 take up 2, 17-24 take up 3, 24+ take up 4 |
| 96 | chPerU32 := 4 / u8PerCh // we can fit 4, 2 or 1 glyphs per u32 |
| 97 | spacing := 4 / chPerU32 // we must skip 1, 2, or 4 8-bit units between each glyph start |
| 98 | |
| 99 | costPerLine := (len(d) + chPerU32 - 1) / chPerU32 // #of whole u32 per horizontal line in font |
| 100 | costTotal := h * costPerLine // #of whole u32s required for the whole font |
| 101 | |
| 102 | encoded := make([]uint32, costTotal) |
| 103 | |
| 104 | // i8 tracks the number of 8-bit units we've skipped |
| 105 | var i8 int |
| 106 | for _, c := range chs { |
| 107 | matrix := d[rune(c)] |
| 108 | |
| 109 | i32 := (i8 >> 2) * h // i32 is the index into encoded for the u32 for this char |
| 110 | dist := i8 & 0b11 // how many u8 units into the u32 we're offset |
| 111 | cm[rune(c)] = uint16((i32 << 2) | dist) |
| 112 | |
| 113 | for y := 0; y < h; y++ { |
| 114 | line := encoded[i32+y] |
| 115 | var b uint32 = 1 << uint(8*dist) |
| 116 | |
| 117 | if ld, hasLine := matrix[y]; hasLine { |
| 118 | for x := 0; x < w; x++ { |
| 119 | if len(ld) > x && ld[x] == 'X' { |
| 120 | line |= b |
| 121 | } |
| 122 | b <<= 1 |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | encoded[i32+y] = line |
| 127 | } |
| 128 | |
| 129 | i8 += spacing |
| 130 | } |
| 131 | |
| 132 | return encoded, cm |
| 133 | } |
| 134 | |
| 135 | func generatePixFontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/generatePixFont().(name string, w, h int, v bool, d map[rune]map[int]string) { |
| 136 | template := ` |
| 137 | package %s |
| 138 | |
| 139 | import "github.com/pbnjay/pixfont" |
| 140 | |
| 141 | var Font *pixfont.PixFont |
| 142 | |
| 143 | func init() { |
| 144 | charMap := %#v |
| 145 | data := %#v |
| 146 | Font = pixfont.NewPixFont(%d, %d, charMap, data) |
| 147 | Font.SetVariableWidth(%t) |
| 148 | } |
| 149 | ` |
| 150 | |
| 151 | encoded, cm := packFontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/packFont().(w, h, d) |
| 152 | |
| 153 | fnt := pixfontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/.NewPixFontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/NewPixFont().(uint8(w), uint8(h), cm, encoded) |
| 154 | fnt.SetVariableWidthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/PixFont#SetVariableWidth().(v) |
| 155 | |
| 156 | f, err := osscip-go gomod github.com/golang/go/src go1.13 os/.OpenFilescip-go gomod github.com/golang/go/src go1.13 os/OpenFile().(name+".go", osscip-go gomod github.com/golang/go/src go1.13 os/.O_CREATEscip-go gomod github.com/golang/go/src go1.13 os/O_CREATE.|osscip-go gomod github.com/golang/go/src go1.13 os/.O_RDWRscip-go gomod github.com/golang/go/src go1.13 os/O_RDWR., 0644) |
| 157 | if err != nil { |
| 158 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., err.Error()) |
| 159 | return |
| 160 | } |
| 161 | |
| 162 | // draw a comment header using the new font |
| 163 | sd := &pixfontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/.StringDrawablescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/StringDrawable#{} |
| 164 | fnt.DrawStringscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/PixFont#DrawString().(sd, 0, 0, name, nil) |
| 165 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(f, sd.PrefixStringscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont`/StringDrawable#PrefixString().("// ")) |
| 166 | |
| 167 | // create the code from the template and go fmt it |
| 168 | code := fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().(template, name, cm, encoded, w, h, v) |
| 169 | bcode, _ := formatscip-go gomod github.com/golang/go/src go1.13 `go/format`/.Sourcescip-go gomod github.com/golang/go/src go1.13 `go/format`/Source().([]byte(code)) |
| 170 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(f, string(bcode)) |
| 171 | |
| 172 | f.Closescip-go gomod github.com/golang/go/src go1.13 os/File#Close().() |
| 173 | } |
| 174 | |
| 175 | func processImagescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/processImage().(filename string) (allLetters map[rune]map[int]string, maxWidth int) { |
| 176 | f, err := osscip-go gomod github.com/golang/go/src go1.13 os/.Openscip-go gomod github.com/golang/go/src go1.13 os/Open().(filename) |
| 177 | if err != nil { |
| 178 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.13 fmt/Fprint().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., err.Error()) |
| 179 | return nil, 0 |
| 180 | } |
| 181 | img, _, err := imagescip-go gomod github.com/golang/go/src go1.13 image/.Decodescip-go gomod github.com/golang/go/src go1.13 image/Decode().(f) |
| 182 | if err != nil { |
| 183 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.13 fmt/Fprint().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., err.Error()) |
| 184 | return nil, 0 |
| 185 | } |
| 186 | if *widthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/width. == 0 { |
| 187 | *widthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/width. = img.Boundsscip-go gomod github.com/golang/go/src go1.13 image/Image#Bounds().().Dxscip-go gomod github.com/golang/go/src go1.13 image/Rectangle#Dx().() - *startXscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startX. |
| 188 | } |
| 189 | if *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height. == 0 { |
| 190 | *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height. = img.Boundsscip-go gomod github.com/golang/go/src go1.13 image/Image#Bounds().().Dyscip-go gomod github.com/golang/go/src go1.13 image/Rectangle#Dy().() - *startYscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startY. |
| 191 | } |
| 192 | allLetters = make(map[rune]map[int]string) |
| 193 | maxWidth = 0 |
| 194 | |
| 195 | // generate a greyscale histogram of the image |
| 196 | pxc := 0 |
| 197 | clrs := make(map[uint8]int) |
| 198 | for y := 0; y < img.Boundsscip-go gomod github.com/golang/go/src go1.13 image/Image#Bounds().().Dyscip-go gomod github.com/golang/go/src go1.13 image/Rectangle#Dy().(); y++ { |
| 199 | for x := 0; x < img.Boundsscip-go gomod github.com/golang/go/src go1.13 image/Image#Bounds().().Dxscip-go gomod github.com/golang/go/src go1.13 image/Rectangle#Dx().(); x++ { |
| 200 | c := img.Atscip-go gomod github.com/golang/go/src go1.13 image/Image#At().(x, y) |
| 201 | gc := colorscip-go gomod github.com/golang/go/src go1.13 `image/color`/.GrayModelscip-go gomod github.com/golang/go/src go1.13 `image/color`/GrayModel..Convertscip-go gomod github.com/golang/go/src go1.13 `image/color`/Model#Convert().(c).(colorscip-go gomod github.com/golang/go/src go1.13 `image/color`/.Grayscip-go gomod github.com/golang/go/src go1.13 `image/color`/Gray#) |
| 202 | clrs[gc.Yscip-go gomod github.com/golang/go/src go1.13 `image/color`/Gray#Y.]++ |
| 203 | pxc++ |
| 204 | } |
| 205 | } |
| 206 | // find a threshold pixel count for what colors to ignore as background |
| 207 | // (ie assumes background image is fairly solid and colors occur much |
| 208 | // more often than font colors) |
| 209 | pxt := pxc |
| 210 | pxd := 0 |
| 211 | for pxd < (pxc/2) && pxt > 0 { |
| 212 | pxt /= 2 |
| 213 | pxd = 0 |
| 214 | for _, n := range clrs { |
| 215 | if n > pxt { |
| 216 | pxd += n |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // scan across the image in the crop region, saving pixels as you go. |
| 222 | // if at any point we see an "empty" column of pixels, we assume it |
| 223 | // is a character boundary and move to the next alphabet letter. |
| 224 | curAlpha := *alphabetscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/alphabet. |
| 225 | curWidth := 0 |
| 226 | curLetter := make(map[int]string) |
| 227 | for x := *startXscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startX.; x < *startXscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startX.+*widthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/width.; x++ { |
| 228 | curWidth++ |
| 229 | isEmpty := true |
| 230 | ay := 0 |
| 231 | for y := *startYscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startY.; y < *startYscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/startY.+*heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height.; y++ { |
| 232 | c := img.Atscip-go gomod github.com/golang/go/src go1.13 image/Image#At().(x, y) |
| 233 | gc := colorscip-go gomod github.com/golang/go/src go1.13 `image/color`/.GrayModelscip-go gomod github.com/golang/go/src go1.13 `image/color`/GrayModel..Convertscip-go gomod github.com/golang/go/src go1.13 `image/color`/Model#Convert().(c).(colorscip-go gomod github.com/golang/go/src go1.13 `image/color`/.Grayscip-go gomod github.com/golang/go/src go1.13 `image/color`/Gray#) |
| 234 | if clrs[gc.Yscip-go gomod github.com/golang/go/src go1.13 `image/color`/Gray#Y.] <= pxt { |
| 235 | if _, haveDots := curLetter[ay]; !haveDots { |
| 236 | curLetter[ay] = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Repeatscip-go gomod github.com/golang/go/src go1.13 strings/Repeat().(" ", curWidth-1) |
| 237 | } |
| 238 | curLetter[ay] += "X" |
| 239 | isEmpty = false |
| 240 | } else { |
| 241 | if _, haveDots := curLetter[ay]; haveDots { |
| 242 | curLetter[ay] += " " |
| 243 | } |
| 244 | } |
| 245 | ay++ |
| 246 | } |
| 247 | |
| 248 | if isEmpty { |
| 249 | if len(curLetter) != 0 { |
| 250 | if len(curAlpha) > 0 { |
| 251 | curWidth-- // remove last blank column |
| 252 | for yy, ln := range curLetter { |
| 253 | if len(ln) >= curWidth { |
| 254 | curLetter[yy] = ln[:curWidth] |
| 255 | } |
| 256 | } |
| 257 | r, nbytes := utf8scip-go gomod github.com/golang/go/src go1.13 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.13 `unicode/utf8`/DecodeRuneInString().(curAlpha) |
| 258 | allLetters[r] = curLetter |
| 259 | curAlpha = curAlpha[nbytes:] |
| 260 | } |
| 261 | if curWidth > maxWidth { |
| 262 | maxWidth = curWidth |
| 263 | } |
| 264 | } |
| 265 | curWidth = 0 |
| 266 | curLetter = make(map[int]string) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if *outNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/outName. != "" { |
| 271 | return |
| 272 | } |
| 273 | |
| 274 | // output a simple text representation of the extracted characters |
| 275 | for _, a := range *alphabetscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/alphabet. { |
| 276 | if l, found := allLetters[a]; found { |
| 277 | w := 0 |
| 278 | for yy := 0; yy < *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height.; yy++ { |
| 279 | if len(l[yy]) > w { |
| 280 | w = len(l[yy]) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | leftPad := (maxWidth - w) / 2 |
| 285 | if *varWidthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/varWidth. { |
| 286 | leftPad = 0 |
| 287 | } |
| 288 | for yy := 0; yy < *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height.; yy++ { |
| 289 | l[yy] = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Repeatscip-go gomod github.com/golang/go/src go1.13 strings/Repeat().(" ", leftPad) + l[yy] |
| 290 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Printfscip-go gomod github.com/golang/go/src go1.13 fmt/Printf().("%c [%*s]\n", a, -maxWidth, l[yy]) |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | return |
| 295 | } |
| 296 | |
| 297 | func processTextscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/processText().(filename string) (allLetters map[rune]map[int]string, maxWidth int) { |
| 298 | newalpha := "" |
| 299 | input, err := ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/ReadFile().(filename) |
| 300 | if err != nil { |
| 301 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., err.Error()) |
| 302 | return nil, 0 |
| 303 | } |
| 304 | allLetters = make(map[rune]map[int]string) |
| 305 | re := regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(`[^\n]*\n`) |
| 306 | count := 0 |
| 307 | hh, maxHeight := 0, 0 |
| 308 | lastCh := rune(0) |
| 309 | |
| 310 | for _, bline := range re.FindAllscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#FindAll().(input, -1) { |
| 311 | line := string(bline) |
| 312 | c, pixoffs := utf8scip-go gomod github.com/golang/go/src go1.13 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.13 `unicode/utf8`/DecodeRuneInString().(line) |
| 313 | pixoffs += 3 |
| 314 | if lastCh != c { |
| 315 | count = 0 |
| 316 | hh = len(allLetters[lastCh]) |
| 317 | if hh > maxHeight { |
| 318 | maxHeight = hh |
| 319 | } |
| 320 | allLetters[c] = make(map[int]string) |
| 321 | newalpha += string(c) |
| 322 | } |
| 323 | ww := len(line) - (pixoffs + 2) |
| 324 | if ww > maxWidth { |
| 325 | maxWidth = ww |
| 326 | } |
| 327 | allLetters[c][count] = line[pixoffs : pixoffs+ww] |
| 328 | lastCh = c |
| 329 | count++ |
| 330 | } |
| 331 | |
| 332 | *alphabetscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/alphabet. = newalpha |
| 333 | if *widthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/width. == 0 { |
| 334 | *widthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/width. = maxWidth |
| 335 | } |
| 336 | if *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height. == 0 { |
| 337 | *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height. = maxHeight |
| 338 | } |
| 339 | |
| 340 | if *outNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/outName. != "" { |
| 341 | return |
| 342 | } |
| 343 | |
| 344 | // output the same representation again, to allow user to verify it was parsed correctly |
| 345 | for _, a := range *alphabetscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/alphabet. { |
| 346 | if l, found := allLetters[a]; found { |
| 347 | w := 0 |
| 348 | for yy := 0; yy < *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height.; yy++ { |
| 349 | if len(l[yy]) > w { |
| 350 | w = len(l[yy]) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | leftPad := (maxWidth - w) / 2 |
| 355 | for yy := 0; yy < *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height.; yy++ { |
| 356 | l[yy] = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Repeatscip-go gomod github.com/golang/go/src go1.13 strings/Repeat().(" ", leftPad) + l[yy] |
| 357 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Printfscip-go gomod github.com/golang/go/src go1.13 fmt/Printf().("%c [%*s]\n", a, -maxWidth, l[yy]) |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | return |
| 362 | } |
| 363 | |
| 364 | func mainscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/main().() { |
| 365 | flagscip-go gomod github.com/golang/go/src go1.13 flag/.Parsescip-go gomod github.com/golang/go/src go1.13 flag/Parse().() |
| 366 | |
| 367 | allLetters := make(map[rune]map[int]string) |
| 368 | maxWidth := 0 |
| 369 | |
| 370 | if *imageNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/imageName. != "" { |
| 371 | allLetters, maxWidth = processImagescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/processImage().(*imageNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/imageName.) |
| 372 | } else if *textNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/textName. != "" { |
| 373 | allLetters, maxWidth = processTextscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/processText().(*textNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/textName.) |
| 374 | } else { |
| 375 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., "-img or -txt should be provided") |
| 376 | flagscip-go gomod github.com/golang/go/src go1.13 flag/.Usagescip-go gomod github.com/golang/go/src go1.13 flag/Usage.() |
| 377 | return |
| 378 | } |
| 379 | |
| 380 | if *outNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/outName. != "" { |
| 381 | generatePixFontscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/generatePixFont().(*outNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/outName., maxWidth, *heightscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/height., *varWidthscip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/varWidth., allLetters) |
| 382 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., "Created package file:", *outNamescip-go gomod github.com/pbnjay/pixfont . `github.com/pbnjay/pixfont/cmd/fontgen`/outName.+".go") |
| 383 | } |
| 384 | } |