func (f *Fractal) RenewResolution(w, h int) { f.width, f.height = w, h f.height = f.width var last = f.grid f.grid = texture.NewPixelGrid(f.width, f.height) if last != nil { w, h = last.Sizes() var xmax, ymax = f.grid.Sizes() rangewh(w, h, func(x, y int) { if x < xmax && y < ymax { var col = last.GetPixel24(x, y) f.grid.SetPixel24(x, y, col) } }) } f.grid.SetAlpha(255) f.renewTexture() }
func NewFontTexture(font *truetype.Font, text string, size int) *FontTexture { this := new(FontTexture) this.text, this.size, this.name = text, size, textPlusSizeToName(text, size) lines := strings.Split(text, "\n") nlines := len(lines) maxLen := 0 for _, s := range lines { if len(s) > maxLen { maxLen = len(s) } } // Initialize the context. fsize := Double(size) fg := image.White bg := image.Transparent //estimate width and height: width := int(widthPerChar * fsize * Double(maxLen)) height := int(Double(nlines) * fsize * yfactor) Println(height, nlines) rgba := image.NewRGBA(image.Rect(0, 0, width, height)) draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src) c := freetype.NewContext() c.SetDPI(cdpi) c.SetFont(font) c.SetFontSize(fsize.F()) c.SetClip(rgba.Bounds()) c.SetDst(rgba) c.SetSrc(fg) // Draw the text. ptf32 := c.PointToFix32((fsize * cspacing).F()) pt := freetype.Pt(0, size) //pt := freetype.Pt(0, 1) for _, s := range lines { _, err := c.DrawString(s, pt) //Println(s) if err != nil { log.Println(err) return nil } pt.Y += ptf32 } //find actual bounds: xmin, xmax, ymin, ymax := 111111, 0, 111111, 0 bounds := rgba.Bounds() //Println(bounds.Min.X, bounds.Max.X, bounds.Min.Y, bounds.Max.Y) for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { _, _, _, a := rgba.At(x, bounds.Max.Y-y-1).RGBA() if a != 0 { if x < xmin { xmin = x } if x > xmax { xmax = x } if y < ymin { ymin = y } if y > ymax { ymax = y } } } } //Println(bounds.Min.X, bounds.Max.X, bounds.Min.Y, bounds.Max.Y) //fill a pixel grid for the textureData w, h := xmax-xmin+1, ymax-ymin+1 grid := texture.NewPixelGrid(w, h) for y := ymin; y <= ymax; y++ { for x := xmin; x <= xmax; x++ { r, g, b, a := rgba.At(x, bounds.Max.Y-y-1).RGBA() xx, yy := x-xmin, y-ymin grid.SetPixel(xx, yy, NewColor32(uint8(r/256), uint8(g/256), uint8(b/256), uint8(a/256))) } } grid.WriteToFile("fonttest") this.rgba, this.context = rgba, c td := texture.NewTextureData(this.name) td.Pix = grid td.CreateMipmaps() td.LoadIntoGL() this.textureData = td w, h = this.textureData.Sizes() this.widthFactor = Double(w) / Double(h) return this }