improve Atlas creation, atlas is now square picture (was one row of characters)
This commit is contained in:
parent
cfa9180bb7
commit
847b48292b
169
text/atlas.go
169
text/atlas.go
|
@ -3,6 +3,7 @@ package text
|
|||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
"sort"
|
||||
"unicode"
|
||||
|
||||
"github.com/faiface/pixel"
|
||||
|
@ -26,73 +27,52 @@ type Atlas struct {
|
|||
}
|
||||
|
||||
func NewAtlas(face font.Face, runes []rune) *Atlas {
|
||||
//FIXME: don't put glyphs in just one row, make a square
|
||||
|
||||
width := fixed.Int26_6(0)
|
||||
for _, r := range runes {
|
||||
b, _, ok := face.GlyphBounds(r)
|
||||
if !ok && r != unicode.ReplacementChar {
|
||||
continue
|
||||
}
|
||||
width += b.Max.X - b.Min.X
|
||||
|
||||
// padding to avoid filtering artifacts
|
||||
width = fixed.I(width.Ceil())
|
||||
width += fixed.I(2)
|
||||
}
|
||||
fixedMapping, fixedBounds := makeSquareMapping(face, runes, fixed.I(2))
|
||||
|
||||
atlasImg := image.NewRGBA(image.Rect(
|
||||
0, 0,
|
||||
width.Ceil(), (face.Metrics().Ascent + face.Metrics().Descent).Ceil(),
|
||||
fixedBounds.Min.X.Floor(),
|
||||
fixedBounds.Min.Y.Floor(),
|
||||
fixedBounds.Max.X.Ceil(),
|
||||
fixedBounds.Max.Y.Ceil(),
|
||||
))
|
||||
atlasHeight := float64(atlasImg.Bounds().Dy())
|
||||
|
||||
for r, fg := range fixedMapping {
|
||||
dr, mask, maskp, _, _ := face.Glyph(fg.dot, r)
|
||||
draw.Draw(atlasImg, dr, mask, maskp, draw.Src)
|
||||
}
|
||||
|
||||
bounds := pixel.Rect{}
|
||||
for _, fg := range fixedMapping {
|
||||
b := pixel.R(
|
||||
i2f(fg.frame.Min.X),
|
||||
i2f(fg.frame.Min.Y),
|
||||
i2f(fg.frame.Max.X),
|
||||
i2f(fg.frame.Max.Y),
|
||||
)
|
||||
bounds = bounds.Union(b)
|
||||
}
|
||||
|
||||
mapping := make(map[rune]Glyph)
|
||||
|
||||
dot := fixed.Point26_6{
|
||||
X: 0,
|
||||
Y: face.Metrics().Ascent,
|
||||
for r, fg := range fixedMapping {
|
||||
mapping[r] = Glyph{
|
||||
Dot: pixel.V(
|
||||
i2f(fg.dot.X),
|
||||
bounds.Max.Y()-(i2f(fg.dot.Y)-bounds.Min.Y()),
|
||||
),
|
||||
Frame: pixel.R(
|
||||
i2f(fg.frame.Min.X),
|
||||
bounds.Max.Y()-(i2f(fg.frame.Min.Y)-bounds.Min.Y()),
|
||||
i2f(fg.frame.Max.X),
|
||||
bounds.Max.Y()-(i2f(fg.frame.Max.Y)-bounds.Min.Y()),
|
||||
).Norm(),
|
||||
Advance: i2f(fg.advance),
|
||||
}
|
||||
|
||||
for _, r := range runes {
|
||||
b, _, ok := face.GlyphBounds(r)
|
||||
if !ok && r != unicode.ReplacementChar {
|
||||
continue
|
||||
}
|
||||
|
||||
dot.X -= b.Min.X
|
||||
|
||||
dr, mask, maskp, _, _ := face.Glyph(dot, r)
|
||||
draw.Draw(atlasImg, dr, mask, maskp, draw.Src)
|
||||
|
||||
orig := pixel.V(
|
||||
float64(dot.X)/(1<<6),
|
||||
atlasHeight-float64(dot.Y)/(1<<6),
|
||||
)
|
||||
|
||||
frame := pixel.R(
|
||||
float64(dr.Min.X),
|
||||
atlasHeight-float64(dr.Min.Y),
|
||||
float64(dr.Max.X),
|
||||
atlasHeight-float64(dr.Max.Y),
|
||||
).Norm()
|
||||
|
||||
adv, _ := face.GlyphAdvance(r)
|
||||
advance := float64(adv) / (1 << 6)
|
||||
|
||||
mapping[r] = Glyph{orig, frame, advance}
|
||||
|
||||
dot.X += b.Max.X
|
||||
|
||||
// padding
|
||||
dot.X = fixed.I(dot.X.Ceil())
|
||||
dot.X += fixed.I(2)
|
||||
}
|
||||
|
||||
kern := make(map[struct{ r0, r1 rune }]float64)
|
||||
for _, r0 := range runes {
|
||||
for _, r1 := range runes {
|
||||
kern[struct{ r0, r1 rune }{r0, r1}] = float64(face.Kern(r0, r1)) / (1 << 6)
|
||||
kern[struct{ r0, r1 rune }{r0, r1}] = i2f(face.Kern(r0, r1))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,9 +80,9 @@ func NewAtlas(face font.Face, runes []rune) *Atlas {
|
|||
pic: pixel.PictureDataFromImage(atlasImg),
|
||||
mapping: mapping,
|
||||
kern: kern,
|
||||
ascent: float64(face.Metrics().Ascent) / (1 << 6),
|
||||
descent: float64(face.Metrics().Descent) / (1 << 6),
|
||||
lineHeight: float64(face.Metrics().Height) / (1 << 6),
|
||||
ascent: i2f(face.Metrics().Ascent),
|
||||
descent: i2f(face.Metrics().Descent),
|
||||
lineHeight: i2f(face.Metrics().Height),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,3 +148,74 @@ func (a *Atlas) DrawRune(prevR, r rune, dot pixel.Vec) (rect, frame, bounds pixe
|
|||
|
||||
return rect, glyph.Frame, bounds, dot
|
||||
}
|
||||
|
||||
type fixedGlyph struct {
|
||||
dot fixed.Point26_6
|
||||
frame fixed.Rectangle26_6
|
||||
advance fixed.Int26_6
|
||||
}
|
||||
|
||||
func makeSquareMapping(face font.Face, runes []rune, padding fixed.Int26_6) (map[rune]fixedGlyph, fixed.Rectangle26_6) {
|
||||
width := sort.Search(int(fixed.I(1024*1024)), func(i int) bool {
|
||||
width := fixed.Int26_6(i)
|
||||
_, bounds := makeMapping(face, runes, padding, width)
|
||||
return bounds.Max.X-bounds.Min.X >= bounds.Max.Y-bounds.Min.Y
|
||||
})
|
||||
return makeMapping(face, runes, padding, fixed.Int26_6(width))
|
||||
}
|
||||
|
||||
func makeMapping(face font.Face, runes []rune, padding, width fixed.Int26_6) (map[rune]fixedGlyph, fixed.Rectangle26_6) {
|
||||
mapping := make(map[rune]fixedGlyph)
|
||||
bounds := fixed.Rectangle26_6{}
|
||||
|
||||
dot := fixed.P(0, 0)
|
||||
|
||||
for _, r := range runes {
|
||||
// face.Glyph gives more useful results for drawing than face.GlyphBounds
|
||||
dr, _, _, advance, ok := face.Glyph(fixed.P(0, 0), r)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
frame := fixed.Rectangle26_6{
|
||||
Min: fixed.P(dr.Min.X, dr.Min.Y),
|
||||
Max: fixed.P(dr.Max.X, dr.Max.Y),
|
||||
}
|
||||
|
||||
dot.X -= frame.Min.X
|
||||
frame = frame.Add(dot)
|
||||
|
||||
mapping[r] = fixedGlyph{
|
||||
dot: dot,
|
||||
frame: frame,
|
||||
advance: advance,
|
||||
}
|
||||
bounds = bounds.Union(frame)
|
||||
|
||||
dot.X = frame.Max.X
|
||||
|
||||
// padding + align to integer
|
||||
dot.X += padding
|
||||
dot.X = fixed.I(dot.X.Ceil())
|
||||
|
||||
// width exceeded, new row
|
||||
if frame.Max.X >= width {
|
||||
dot.X = 0
|
||||
dot.Y += face.Metrics().Ascent + face.Metrics().Descent
|
||||
|
||||
// padding + align to integer
|
||||
dot.Y += padding
|
||||
dot.Y = fixed.I(dot.Y.Ceil())
|
||||
}
|
||||
}
|
||||
|
||||
return mapping, bounds
|
||||
}
|
||||
|
||||
func i2f(i fixed.Int26_6) float64 {
|
||||
return float64(i) / (1 << 6)
|
||||
}
|
||||
|
||||
func f2i(f float64) fixed.Int26_6 {
|
||||
return fixed.Int26_6(f * (1 << 6))
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"golang.org/x/image/font"
|
||||
)
|
||||
|
||||
// ASCII is a set of all ASCII runes. These runes are codepoints from 32 to 127 inclusive.
|
||||
var ASCII []rune
|
||||
|
||||
func init() {
|
||||
|
@ -19,6 +20,8 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
// RangeTable takes a *unicode.RangeTable and generates a set of runes contained within that
|
||||
// RangeTable.
|
||||
func RangeTable(table *unicode.RangeTable) []rune {
|
||||
var runes []rune
|
||||
for _, rng := range table.R16 {
|
||||
|
|
Loading…
Reference in New Issue