From e043630f927f3d7fa0003a4198e139c68100c2d6 Mon Sep 17 00:00:00 2001 From: aarzilli Date: Sat, 29 Oct 2016 11:53:04 +0200 Subject: [PATCH] Revert "moved freetype to internal, added glyph caching" Not a big enough win. This reverts commit 7df672bc8b35e1be49a0aa61c1528842577d368b. --- _demo/demo.go | 6 - shiny.go | 2 +- style/style.go | 4 +- .../github.com/golang}/freetype/AUTHORS | 0 .../github.com/golang}/freetype/CONTRIBUTORS | 0 .../github.com/golang}/freetype/LICENSE | 0 .../github.com/golang}/freetype/README | 0 .../github.com/golang}/freetype/freetype.go | 13 +- .../golang}/freetype/raster/geom.go | 0 .../golang}/freetype/raster/paint.go | 0 .../golang}/freetype/raster/raster.go | 0 .../golang}/freetype/raster/stroke.go | 0 .../golang}/freetype/truetype/face.go | 111 +++++++----------- .../golang}/freetype/truetype/glyph.go | 13 -- .../golang}/freetype/truetype/hint.go | 0 .../golang}/freetype/truetype/opcodes.go | 0 .../golang}/freetype/truetype/truetype.go | 0 vendor/vendor.json | 12 ++ 18 files changed, 65 insertions(+), 96 deletions(-) rename {internal => vendor/github.com/golang}/freetype/AUTHORS (100%) rename {internal => vendor/github.com/golang}/freetype/CONTRIBUTORS (100%) rename {internal => vendor/github.com/golang}/freetype/LICENSE (100%) rename {internal => vendor/github.com/golang}/freetype/README (100%) rename {internal => vendor/github.com/golang}/freetype/freetype.go (97%) rename {internal => vendor/github.com/golang}/freetype/raster/geom.go (100%) rename {internal => vendor/github.com/golang}/freetype/raster/paint.go (100%) rename {internal => vendor/github.com/golang}/freetype/raster/raster.go (100%) rename {internal => vendor/github.com/golang}/freetype/raster/stroke.go (100%) rename {internal => vendor/github.com/golang}/freetype/truetype/face.go (85%) rename {internal => vendor/github.com/golang}/freetype/truetype/glyph.go (98%) rename {internal => vendor/github.com/golang}/freetype/truetype/hint.go (100%) rename {internal => vendor/github.com/golang}/freetype/truetype/opcodes.go (100%) rename {internal => vendor/github.com/golang}/freetype/truetype/truetype.go (100%) diff --git a/_demo/demo.go b/_demo/demo.go index 4bf0b5e..ef4d9c9 100644 --- a/_demo/demo.go +++ b/_demo/demo.go @@ -32,12 +32,6 @@ func main() { trace.Start(fh) defer trace.Stop() } - fhp, _ := os.Create("demo.cpu.pprof") - if fhp != nil { - defer fhp.Close() - pprof.StartCPUProfile(fhp) - defer pprof.StopCPUProfile() - } } switch whichdemo { diff --git a/shiny.go b/shiny.go index c24a84f..3cc35b1 100644 --- a/shiny.go +++ b/shiny.go @@ -19,7 +19,7 @@ import ( "github.com/aarzilli/nucular/rect" nstyle "github.com/aarzilli/nucular/style" - "github.com/aarzilli/nucular/internal/freetype/raster" + "github.com/golang/freetype/raster" "golang.org/x/exp/shiny/driver" "golang.org/x/exp/shiny/screen" diff --git a/style/style.go b/style/style.go index 0eeb471..e3b63f8 100644 --- a/style/style.go +++ b/style/style.go @@ -11,8 +11,8 @@ import ( "golang.org/x/image/font" - "github.com/aarzilli/nucular/internal/freetype" - "github.com/aarzilli/nucular/internal/freetype/truetype" + "github.com/golang/freetype" + "github.com/golang/freetype/truetype" ) type WidgetStates int diff --git a/internal/freetype/AUTHORS b/vendor/github.com/golang/freetype/AUTHORS similarity index 100% rename from internal/freetype/AUTHORS rename to vendor/github.com/golang/freetype/AUTHORS diff --git a/internal/freetype/CONTRIBUTORS b/vendor/github.com/golang/freetype/CONTRIBUTORS similarity index 100% rename from internal/freetype/CONTRIBUTORS rename to vendor/github.com/golang/freetype/CONTRIBUTORS diff --git a/internal/freetype/LICENSE b/vendor/github.com/golang/freetype/LICENSE similarity index 100% rename from internal/freetype/LICENSE rename to vendor/github.com/golang/freetype/LICENSE diff --git a/internal/freetype/README b/vendor/github.com/golang/freetype/README similarity index 100% rename from internal/freetype/README rename to vendor/github.com/golang/freetype/README diff --git a/internal/freetype/freetype.go b/vendor/github.com/golang/freetype/freetype.go similarity index 97% rename from internal/freetype/freetype.go rename to vendor/github.com/golang/freetype/freetype.go index f58f677..bec01f6 100644 --- a/internal/freetype/freetype.go +++ b/vendor/github.com/golang/freetype/freetype.go @@ -13,8 +13,8 @@ import ( "image" "image/draw" - "github.com/aarzilli/nucular/internal/freetype/raster" - "github.com/aarzilli/nucular/internal/freetype/truetype" + "github.com/golang/freetype/raster" + "github.com/golang/freetype/truetype" "golang.org/x/image/font" "golang.org/x/image/math/fixed" ) @@ -24,10 +24,9 @@ import ( // position for the mask image. Sub-pixel positions are quantized to // nXFractions possible values in both the x and y directions. const ( - nLoadedGlyphs = 2048 - nGlyphs = 256 - nXFractions = 4 - nYFractions = 1 + nGlyphs = 256 + nXFractions = 4 + nYFractions = 1 ) // An entry in the glyph cache is keyed explicitly by the glyph index and @@ -72,7 +71,7 @@ type Context struct { fontSize, dpi float64 scale fixed.Int26_6 hinting font.Hinting - // cache is the rendered glyph cache. + // cache is the glyph cache. cache [nGlyphs * nXFractions * nYFractions]cacheEntry } diff --git a/internal/freetype/raster/geom.go b/vendor/github.com/golang/freetype/raster/geom.go similarity index 100% rename from internal/freetype/raster/geom.go rename to vendor/github.com/golang/freetype/raster/geom.go diff --git a/internal/freetype/raster/paint.go b/vendor/github.com/golang/freetype/raster/paint.go similarity index 100% rename from internal/freetype/raster/paint.go rename to vendor/github.com/golang/freetype/raster/paint.go diff --git a/internal/freetype/raster/raster.go b/vendor/github.com/golang/freetype/raster/raster.go similarity index 100% rename from internal/freetype/raster/raster.go rename to vendor/github.com/golang/freetype/raster/raster.go diff --git a/internal/freetype/raster/stroke.go b/vendor/github.com/golang/freetype/raster/stroke.go similarity index 100% rename from internal/freetype/raster/stroke.go rename to vendor/github.com/golang/freetype/raster/stroke.go diff --git a/internal/freetype/truetype/face.go b/vendor/github.com/golang/freetype/truetype/face.go similarity index 85% rename from internal/freetype/truetype/face.go rename to vendor/github.com/golang/freetype/truetype/face.go index b782975..099006f 100644 --- a/internal/freetype/truetype/face.go +++ b/vendor/github.com/golang/freetype/truetype/face.go @@ -9,7 +9,7 @@ import ( "image" "math" - "github.com/aarzilli/nucular/internal/freetype/raster" + "github.com/golang/freetype/raster" "golang.org/x/image/font" "golang.org/x/image/math/fixed" ) @@ -35,19 +35,12 @@ type Options struct { // A zero value means to use no hinting. Hinting font.Hinting - // RenderCacheEntries is the number of entries in the glyph mask image + // GlyphCacheEntries is the number of entries in the glyph mask image // cache. // // If non-zero, it must be a power of 2. // // A zero value means to use 512 entries. - RenderCacheEntries int - - // GlyphCacheEntries is the number of entries in the glyph cache. - // - // If non-zero, it must be a power of 2. - // - // A zero value means to use 2048 entries. GlyphCacheEntries int // SubPixelsX is the number of sub-pixel locations a glyph's dot is @@ -100,22 +93,15 @@ func (o *Options) hinting() font.Hinting { return font.HintingNone } -func (o *Options) renderCacheEntries() int { - if o != nil && powerOf2(o.RenderCacheEntries) { - return o.RenderCacheEntries +func (o *Options) glyphCacheEntries() int { + if o != nil && powerOf2(o.GlyphCacheEntries) { + return o.GlyphCacheEntries } // 512 is 128 * 4 * 1, which lets us cache 128 glyphs at 4 * 1 subpixel // locations in the X and Y direction. return 512 } -func (o *Options) glyphCacheEntries() int { - if o != nil && o.GlyphCacheEntries != 0 { - return o.GlyphCacheEntries - } - return 2048 -} - func (o *Options) subPixelsX() (value uint32, halfQuantum, mask fixed.Int26_6) { if o != nil { switch o.SubPixelsX { @@ -167,28 +153,23 @@ func subPixels(q int) (value uint32, bias, mask fixed.Int26_6) { } // glyphCacheEntry caches the arguments and return values of rasterize. -type renderCacheEntry struct { - key renderCacheKey - val renderCacheVal +type glyphCacheEntry struct { + key glyphCacheKey + val glyphCacheVal } -type renderCacheKey struct { +type glyphCacheKey struct { index Index fx, fy uint8 } -type renderCacheVal struct { +type glyphCacheVal struct { advanceWidth fixed.Int26_6 offset image.Point gw int gh int } -type glyphCacheEntry struct { - key Index - val GlyphBuf -} - type indexCacheEntry struct { rune rune index Index @@ -197,19 +178,18 @@ type indexCacheEntry struct { // NewFace returns a new font.Face for the given Font. func NewFace(f *Font, opts *Options) font.Face { a := &face{ - f: f, - hinting: opts.hinting(), - scale: fixed.Int26_6(0.5 + (opts.size() * opts.dpi() * 64 / 72)), - renderCache: make([]renderCacheEntry, opts.renderCacheEntries()), - glyphCache: make([]glyphCacheEntry, opts.glyphCacheEntries()), + f: f, + hinting: opts.hinting(), + scale: fixed.Int26_6(0.5 + (opts.size() * opts.dpi() * 64 / 72)), + glyphCache: make([]glyphCacheEntry, opts.glyphCacheEntries()), } a.subPixelX, a.subPixelBiasX, a.subPixelMaskX = opts.subPixelsX() a.subPixelY, a.subPixelBiasY, a.subPixelMaskY = opts.subPixelsY() // Fill the cache with invalid entries. Valid glyph cache entries have fx // and fy in the range [0, 64). Valid index cache entries have rune >= 0. - for i := range a.renderCache { - a.renderCache[i].key.fy = 0xff + for i := range a.glyphCache { + a.glyphCache[i].key.fy = 0xff } for i := range a.indexCache { a.indexCache[i].rune = -1 @@ -223,7 +203,7 @@ func NewFace(f *Font, opts *Options) font.Face { ymax := -int(b.Min.Y-63) >> 6 a.maxw = xmax - xmin a.maxh = ymax - ymin - a.masks = image.NewAlpha(image.Rect(0, 0, a.maxw, a.maxh*len(a.renderCache))) + a.masks = image.NewAlpha(image.Rect(0, 0, a.maxw, a.maxh*len(a.glyphCache))) a.r.SetBounds(a.maxw, a.maxh) a.p = facePainter{a} @@ -241,13 +221,13 @@ type face struct { subPixelBiasY fixed.Int26_6 subPixelMaskY fixed.Int26_6 masks *image.Alpha - renderCache []renderCacheEntry glyphCache []glyphCacheEntry r raster.Rasterizer p raster.Painter paintOffset int maxw int maxh int + glyphBuf GlyphBuf indexCache [indexCacheLen]indexCacheEntry // TODO: clip rectangle? @@ -308,23 +288,23 @@ func (a *face) Glyph(dot fixed.Point26_6, r rune) ( cIndex := uint32(index) cIndex = cIndex*a.subPixelX - uint32(fx/a.subPixelMaskX) cIndex = cIndex*a.subPixelY - uint32(fy/a.subPixelMaskY) - cIndex &= uint32(len(a.renderCache) - 1) + cIndex &= uint32(len(a.glyphCache) - 1) a.paintOffset = a.maxh * int(cIndex) - k := renderCacheKey{ + k := glyphCacheKey{ index: index, fx: uint8(fx), fy: uint8(fy), } - var v renderCacheVal - if a.renderCache[cIndex].key != k { + var v glyphCacheVal + if a.glyphCache[cIndex].key != k { var ok bool v, ok = a.rasterize(index, fx, fy) if !ok { return image.Rectangle{}, nil, image.Point{}, 0, false } - a.renderCache[cIndex] = renderCacheEntry{k, v} + a.glyphCache[cIndex] = glyphCacheEntry{k, v} } else { - v = a.renderCache[cIndex].val + v = a.glyphCache[cIndex].val } dr.Min = image.Point{ @@ -339,14 +319,13 @@ func (a *face) Glyph(dot fixed.Point26_6, r rune) ( } func (a *face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) { - glyphBuf, err := a.loadGlyph(a.index(r)) - if err != nil { + if err := a.glyphBuf.Load(a.f, a.scale, a.index(r), a.hinting); err != nil { return fixed.Rectangle26_6{}, 0, false } - xmin := +glyphBuf.Bounds.Min.X - ymin := -glyphBuf.Bounds.Max.Y - xmax := +glyphBuf.Bounds.Max.X - ymax := -glyphBuf.Bounds.Min.Y + xmin := +a.glyphBuf.Bounds.Min.X + ymin := -a.glyphBuf.Bounds.Max.Y + xmax := +a.glyphBuf.Bounds.Max.X + ymax := -a.glyphBuf.Bounds.Min.Y if xmin > xmax || ymin > ymax { return fixed.Rectangle26_6{}, 0, false } @@ -359,33 +338,31 @@ func (a *face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.In X: xmax, Y: ymax, }, - }, glyphBuf.AdvanceWidth, true + }, a.glyphBuf.AdvanceWidth, true } func (a *face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) { - glyphBuf, err := a.loadGlyph(a.index(r)) - if err != nil { + if err := a.glyphBuf.Load(a.f, a.scale, a.index(r), a.hinting); err != nil { return 0, false } - return glyphBuf.AdvanceWidth, true + return a.glyphBuf.AdvanceWidth, true } // rasterize returns the advance width, integer-pixel offset to render at, and // the width and height of the given glyph at the given sub-pixel offsets. // // The 26.6 fixed point arguments fx and fy must be in the range [0, 1). -func (a *face) rasterize(index Index, fx, fy fixed.Int26_6) (v renderCacheVal, ok bool) { - glyphBuf, err := a.loadGlyph(index) - if err != nil { - return renderCacheVal{}, false +func (a *face) rasterize(index Index, fx, fy fixed.Int26_6) (v glyphCacheVal, ok bool) { + if err := a.glyphBuf.Load(a.f, a.scale, index, a.hinting); err != nil { + return glyphCacheVal{}, false } // Calculate the integer-pixel bounds for the glyph. - xmin := int(fx+glyphBuf.Bounds.Min.X) >> 6 - ymin := int(fy-glyphBuf.Bounds.Max.Y) >> 6 - xmax := int(fx+glyphBuf.Bounds.Max.X+0x3f) >> 6 - ymax := int(fy-glyphBuf.Bounds.Min.Y+0x3f) >> 6 + xmin := int(fx+a.glyphBuf.Bounds.Min.X) >> 6 + ymin := int(fy-a.glyphBuf.Bounds.Max.Y) >> 6 + xmax := int(fx+a.glyphBuf.Bounds.Max.X+0x3f) >> 6 + ymax := int(fy-a.glyphBuf.Bounds.Min.Y+0x3f) >> 6 if xmin > xmax || ymin > ymax { - return renderCacheVal{}, false + return glyphCacheVal{}, false } // A TrueType's glyph's nodes can have negative co-ordinates, but the // rasterizer clips anything left of x=0 or above y=0. xmin and ymin are @@ -399,13 +376,13 @@ func (a *face) rasterize(index Index, fx, fy fixed.Int26_6) (v renderCacheVal, o pixOffset := a.paintOffset * a.maxw clear(a.masks.Pix[pixOffset : pixOffset+a.maxw*a.maxh]) e0 := 0 - for _, e1 := range glyphBuf.Ends { - a.drawContour(glyphBuf.Points[e0:e1], fx, fy) + for _, e1 := range a.glyphBuf.Ends { + a.drawContour(a.glyphBuf.Points[e0:e1], fx, fy) e0 = e1 } a.r.Rasterize(a.p) - return renderCacheVal{ - glyphBuf.AdvanceWidth, + return glyphCacheVal{ + a.glyphBuf.AdvanceWidth, image.Point{xmin, ymin}, xmax - xmin, ymax - ymin, diff --git a/internal/freetype/truetype/glyph.go b/vendor/github.com/golang/freetype/truetype/glyph.go similarity index 98% rename from internal/freetype/truetype/glyph.go rename to vendor/github.com/golang/freetype/truetype/glyph.go index f96e80f..c2935a5 100644 --- a/internal/freetype/truetype/glyph.go +++ b/vendor/github.com/golang/freetype/truetype/glyph.go @@ -170,19 +170,6 @@ func (g *GlyphBuf) Load(f *Font, scale fixed.Int26_6, i Index, h font.Hinting) e return nil } -func (a *face) loadGlyph(idx Index) (*GlyphBuf, error) { - e := &a.glyphCache[int(idx)%len(a.glyphCache)] - if e.key == idx { - return &e.val, nil - } - err := e.val.Load(a.f, a.scale, idx, a.hinting) - if err != nil { - return nil, err - } - e.key = idx - return &e.val, nil -} - func (g *GlyphBuf) load(recursion uint32, i Index, useMyMetrics bool) (err error) { // The recursion limit here is arbitrary, but defends against malformed glyphs. if recursion >= 32 { diff --git a/internal/freetype/truetype/hint.go b/vendor/github.com/golang/freetype/truetype/hint.go similarity index 100% rename from internal/freetype/truetype/hint.go rename to vendor/github.com/golang/freetype/truetype/hint.go diff --git a/internal/freetype/truetype/opcodes.go b/vendor/github.com/golang/freetype/truetype/opcodes.go similarity index 100% rename from internal/freetype/truetype/opcodes.go rename to vendor/github.com/golang/freetype/truetype/opcodes.go diff --git a/internal/freetype/truetype/truetype.go b/vendor/github.com/golang/freetype/truetype/truetype.go similarity index 100% rename from internal/freetype/truetype/truetype.go rename to vendor/github.com/golang/freetype/truetype/truetype.go diff --git a/vendor/vendor.json b/vendor/vendor.json index 891aeb5..d132731 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -18,6 +18,18 @@ "path": "github.com/BurntSushi/xgb/xproto", "revision": "e13605d223ac0bca1fc3948d9782db9545c331e8" }, + { + "path": "github.com/golang/freetype", + "revision": "38b4c392adc5eed94207994c4848fff99f4ac234" + }, + { + "path": "github.com/golang/freetype/raster", + "revision": "38b4c392adc5eed94207994c4848fff99f4ac234" + }, + { + "path": "github.com/golang/freetype/truetype", + "revision": "38b4c392adc5eed94207994c4848fff99f4ac234" + }, { "path": "github.com/hashicorp/golang-lru", "revision": "0a025b7e63adc15a622f29b0b2c4c3848243bbf6"