Box returns the bounding box of the given non-multiline text where: - width is the horizontal space from the origin. - height is the vertical space above the baseline. - depth is the vertical space below the baseline, a positive number.
(txt string, fnt font.Font)
| 54 | // - height is the vertical space above the baseline. |
| 55 | // - depth is the vertical space below the baseline, a positive number. |
| 56 | func (hdlr Latex) Box(txt string, fnt font.Font) (width, height, depth vg.Length) { |
| 57 | cnv := drawtex.New() |
| 58 | face := hdlr.Fonts.Lookup(fnt, fnt.Size) |
| 59 | fnts := hdlr.fontsFor(fnt) |
| 60 | box, err := mtex.Parse(txt, face.Font.Size.Points(), latexDPI, ttf.NewFrom(cnv, fnts)) |
| 61 | if err != nil { |
| 62 | panic(fmt.Errorf("could not parse math expression: %w", err)) |
| 63 | } |
| 64 | |
| 65 | var sh tex.Ship |
| 66 | sh.Call(0, 0, box.(tex.Tree)) |
| 67 | |
| 68 | width = vg.Length(box.Width()) |
| 69 | height = vg.Length(box.Height()) |
| 70 | depth = vg.Length(box.Depth()) |
| 71 | |
| 72 | // Add a bit of space, with a linegap as mtex.Box is returning |
| 73 | // a very tight bounding box. |
| 74 | // See gonum/plot#661. |
| 75 | if depth != 0 { |
| 76 | var ( |
| 77 | e = face.Extents() |
| 78 | linegap = e.Height - (e.Ascent + e.Descent) |
| 79 | ) |
| 80 | depth += linegap |
| 81 | } |
| 82 | |
| 83 | dpi := vg.Length(hdlr.dpi() / latexDPI) |
| 84 | return width * dpi, height * dpi, depth * dpi |
| 85 | } |
| 86 | |
| 87 | // Draw renders the given text with the provided style and position |
| 88 | // on the canvas. |