generateFontAtlas generates the font atlas for this given font, using the first `c` Unicode characters.
(c int)
| 195 | |
| 196 | // generateFontAtlas generates the font atlas for this given font, using the first `c` Unicode characters. |
| 197 | func (f *Font) generateFontAtlas(c int) FontAtlas { |
| 198 | atlas := FontAtlas{ |
| 199 | XLocation: make([]float32, c), |
| 200 | YLocation: make([]float32, c), |
| 201 | Width: make([]float32, c), |
| 202 | Height: make([]float32, c), |
| 203 | OffsetX: make([]float32, c), |
| 204 | RightSide: make([]float32, c), |
| 205 | OffsetY: make([]float32, c), |
| 206 | } |
| 207 | |
| 208 | currentX := float32(0) |
| 209 | currentY := float32(0) |
| 210 | |
| 211 | // Default colors |
| 212 | if f.FG == nil { |
| 213 | f.FG = color.NRGBA{0, 0, 0, 0} |
| 214 | } |
| 215 | if f.BG == nil { |
| 216 | f.BG = color.NRGBA{0, 0, 0, 0} |
| 217 | } |
| 218 | |
| 219 | d := &font.Drawer{} |
| 220 | d.Src = image.NewUniform(f.FG) |
| 221 | d.Face = truetype.NewFace(f.TTF, &truetype.Options{ |
| 222 | Size: f.Size, |
| 223 | DPI: dpi, |
| 224 | Hinting: font.HintingNone, |
| 225 | }) |
| 226 | |
| 227 | lineHeight := d.Face.Metrics().Height |
| 228 | ascent := d.Face.Metrics().Ascent |
| 229 | prev := 0 |
| 230 | |
| 231 | for i := 0; i < c; i++ { |
| 232 | bounds, adv, ok := d.Face.GlyphBounds(rune(i)) |
| 233 | if !ok { |
| 234 | continue |
| 235 | } |
| 236 | advance := float32(adv.Ceil()) |
| 237 | |
| 238 | atlas.Width[i] = float32((bounds.Max.X - bounds.Min.X).Ceil()) |
| 239 | atlas.Height[i] = float32((bounds.Max.Y - bounds.Min.Y).Ceil()) |
| 240 | atlas.OffsetX[i] = float32(bounds.Min.X.Ceil()) |
| 241 | atlas.OffsetY[i] = float32((ascent + bounds.Min.Y).Ceil()) |
| 242 | atlas.RightSide[i] = advance - float32(bounds.Max.X.Ceil()) |
| 243 | |
| 244 | if prev > 0 { |
| 245 | currentX += float32(f.face.Kern(rune(prev), rune(i)).Ceil()) |
| 246 | } |
| 247 | |
| 248 | //overlapping characters |
| 249 | if atlas.Width[i] > advance { |
| 250 | if atlas.OffsetX[i] < 0 { |
| 251 | advance -= atlas.OffsetX[i] |
| 252 | } else if advance < float32(bounds.Max.X.Ceil()) { |
| 253 | advance = float32(bounds.Max.X.Ceil()) |
| 254 | } |
no test coverage detected