MCPcopy Index your code
hub / github.com/g3n/engine / MeasureText

Method MeasureText

text/font.go:172–197  ·  view source on GitHub ↗

MeasureText returns the minimum width and height in pixels necessary for an image to contain the specified text. The supplied text string can contain line break escape sequences (\n).

(text string)

Source from the content-addressed store, hash-verified

170// MeasureText returns the minimum width and height in pixels necessary for an image to contain
171// the specified text. The supplied text string can contain line break escape sequences (\n).
172func (f *Font) MeasureText(text string) (int, int) {
173
174 // Create font drawer
175 f.updateFace()
176 d := &font.Drawer{Dst: nil, Src: f.fg, Face: f.face}
177
178 // Draw text
179 var width, height int
180 metrics := f.face.Metrics()
181 lineHeight := (metrics.Ascent + metrics.Descent).Ceil()
182 lineGap := int((f.attrib.LineSpacing - float64(1)) * float64(lineHeight))
183
184 lines := strings.Split(text, "\n")
185 for i, s := range lines {
186 d.Dot = fixed.P(0, height)
187 lineWidth := d.MeasureString(s).Ceil()
188 if lineWidth > width {
189 width = lineWidth
190 }
191 height += lineHeight
192 if i > 1 {
193 height += lineGap
194 }
195 }
196 return width, height
197}
198
199// Metrics returns the font metrics.
200func (f *Font) Metrics() font.Metrics {

Callers 6

DrawTextMethod · 0.95
setTextCaretMethod · 0.80
CursorInputMethod · 0.80
onMouseMethod · 0.80
DrawTextCaretMethod · 0.80
NewAtlasFunction · 0.80

Calls 4

updateFaceMethod · 0.95
MetricsMethod · 0.80
SplitMethod · 0.80
CeilMethod · 0.45

Tested by

no test coverage detected