TextDimensions returns the total width, total height and total line size of the input string written out in the Font.
(text string)
| 100 | // TextDimensions returns the total width, total height and total line size |
| 101 | // of the input string written out in the Font. |
| 102 | func (f *Font) TextDimensions(text string) (int, int, int) { |
| 103 | fnt := f.TTF |
| 104 | size := f.Size |
| 105 | var ( |
| 106 | totalWidth = fixed.Int26_6(0) |
| 107 | totalHeight = fixed.Int26_6(size) |
| 108 | maxYBearing = fixed.Int26_6(0) |
| 109 | ) |
| 110 | fupe := fixed.Int26_6(fnt.FUnitsPerEm()) |
| 111 | |
| 112 | for _, char := range text { |
| 113 | idx := fnt.Index(char) |
| 114 | hm := fnt.HMetric(fupe, idx) |
| 115 | vm := fnt.VMetric(fupe, idx) |
| 116 | g := truetype.GlyphBuf{} |
| 117 | err := g.Load(fnt, fupe, idx, font.HintingNone) |
| 118 | if err != nil { |
| 119 | log.Println(err) |
| 120 | return 0, 0, 0 |
| 121 | } |
| 122 | totalWidth += hm.AdvanceWidth |
| 123 | |
| 124 | yB := (vm.TopSideBearing * fixed.Int26_6(size)) / fupe |
| 125 | if yB > maxYBearing { |
| 126 | maxYBearing = yB |
| 127 | } |
| 128 | dY := (vm.AdvanceHeight * fixed.Int26_6(size)) / fupe |
| 129 | if dY > totalHeight { |
| 130 | totalHeight = dY |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Scale to actual pixel size |
| 135 | totalWidth *= fixed.Int26_6(size) |
| 136 | totalWidth /= fupe |
| 137 | |
| 138 | return int(totalWidth), int(totalHeight), int(maxYBearing) |
| 139 | } |
| 140 | |
| 141 | // RenderNRGBA returns an *image.NRGBA in the Font based on the input string. |
| 142 | func (f *Font) RenderNRGBA(text string) *image.NRGBA { |