func (this *TextRenderer) Update(delta float32) { if len(this.runeImgs) == 0 { return } // Model := mathgl.Ident4() // Model = this.GetParent().transform.GetUpdatedModel() fInfo := truetype.Font(*Font.GetFont(this.font)) curTotWidth = float32(0.0) for idx, img := range this.runeImgs { i := fInfo.Index(rune(this.text[idx])) hmetric := fInfo.HMetric(100, i) w := float32(hmetric.AdvanceWidth) //float32(img.Bounds().Dx()) this.Render(img) curTotWidth += w } }
//GetSectionMap //Returns section mapping for each letter in ttf in form of image.Rect with y=0 func (this FontInfo) GetSectionMap() map[rune]image.Rectangle { f := truetype.Font(this) secMap := map[rune]image.Rectangle{} curW := 0 curH := 0 // fmt.Println("Font: ", this.Name()) for idx, v := range text { if idx > 0 && int(math.Mod(float64(idx), float64(itemsPerRow))) == 0 { curH += int(f.Bounds(sizeFixed).Max.Sub(f.Bounds(sizeFixed).Min).Y) + insetPadding curW = 0 } i := f.Index(v) hmet := f.HMetric(sizeFixed, i) // vmet := f.VMetric(sizeFixed, i) // advance width is how much font advances on x which next font starts AFTER that. minX := curW //+ 1 minY := curH //+ 1 maxX := curW + int(hmet.AdvanceWidth) maxY := curH + int(f.Bounds(sizeFixed).Max.Sub(f.Bounds(sizeFixed).Min).Y) curW += int(hmet.AdvanceWidth) + insetPadding secMap[v] = image.Rect(minX, minY, maxX, maxY) // fmt.Println("Letter: ", string(v)) // fmt.Println("Bounds: ", secMap[v]) } return secMap }
// returns official font name: eg. "Times New Roman Normal"" func (this FontInfo) Name() string { f := truetype.Font(this) return f.Name(truetype.NameIDFontFullName) }
// returns image of rendered font func (this FontInfo) GetImage() image.Image { f := truetype.Font(this) maxWidth := 0 curW := 0 maxHeight := int(f.Bounds(sizeFixed).Max.Sub(f.Bounds(sizeFixed).Min).Y) + insetPadding for idx, val := range text { if idx > 0 && int(math.Mod(float64(idx), float64(itemsPerRow))) == 0 { maxHeight += int(f.Bounds(sizeFixed).Max.Sub(f.Bounds(sizeFixed).Min).Y) + insetPadding curW = 0 } i := f.Index(val) hmet := f.HMetric(sizeFixed, i) curW += int(hmet.AdvanceWidth) + insetPadding if curW > maxWidth { maxWidth = curW } } Logging.Debug("Font ", this.Name()) Logging.Debug("Max width: ", maxWidth) Logging.Debug("Max height: ", maxHeight) // Draw the background and the guidelines. fg, bg := image.Black, image.Transparent // diffY := int(f.Bounds(sizeFixed).Max.Sub(f.Bounds(sizeFixed).Min).Y) // add a constant to the y term because of subtle bleeding between vertical space, probably because of advance height rgba := image.NewRGBA(image.Rect(0, 0, maxWidth, maxHeight)) //diffY+5)) draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src) // Draw the text. h := font.HintingNone switch hinting { case "full": h = font.HintingFull } d := &font.Drawer{ Dst: rgba, Src: fg, Face: truetype.NewFace(&f, &truetype.Options{ Size: size, DPI: dpi, Hinting: h, }), } // y := int(math.Abs(float64(f.Bounds(sizeFixed).Max.Y))) //10 + int(math.Ceil(*size**dpi/72)) // dy := int(math.Ceil(size * spacing * dpi / 72)) tWidth := 0 tHeight := int(math.Abs(float64(f.Bounds(sizeFixed).Max.Y))) for idx, r := range text { if idx > 0 && int(math.Mod(float64(idx), float64(itemsPerRow))) == 0 { tHeight += int(f.Bounds(sizeFixed).Max.Sub(f.Bounds(sizeFixed).Min).Y) + insetPadding tWidth = 0 } i := f.Index(r) hmet := f.HMetric(sizeFixed, i) d.Dot = fixed.P(tWidth, tHeight) tWidth += int(hmet.AdvanceWidth) + insetPadding d.DrawString(string(r)) } return rgba }