text

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2024 License: BSD-3-Clause Imports: 11 Imported by: 7

Documentation

Overview

Package text provides types and functions to parse, format and render text.

Index

Examples

Constants

View Source
const (
	PosLeft   = -1
	PosBottom = -1
	PosCenter = 0
	PosTop    = +1
	PosRight  = +1
)

Position specifies the text position.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler added in v0.9.0

type Handler interface {
	// Cache returns the cache of fonts used by the text handler.
	Cache() *font.Cache

	// Extents returns the Extents of a font.
	Extents(fnt font.Font) font.Extents

	// Lines splits a given block of text into separate lines.
	Lines(txt string) []string

	// 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.
	Box(txt string, fnt font.Font) (width, height, depth vg.Length)

	// Draw renders the given text with the provided style and position
	// on the canvas.
	Draw(c vg.Canvas, txt string, sty Style, pt vg.Point)
}

Handler parses, formats and renders text.

type Latex

type Latex struct {
	// Fonts is the cache of font faces used by this text handler.
	Fonts *font.Cache

	// DPI is the dot-per-inch controlling the font resolution used by LaTeX.
	// If zero, the resolution defaults to 72.
	DPI float64
}

Latex parses, formats and renders LaTeX.

Example
package main

import (
	"image/color"
	"log"
	"math"
	"runtime"

	"gonum.org/v1/plot"
	"gonum.org/v1/plot/font"
	"gonum.org/v1/plot/font/liberation"
	"gonum.org/v1/plot/plotter"
	"gonum.org/v1/plot/text"
	"gonum.org/v1/plot/vg"
	"gonum.org/v1/plot/vg/draw"
)

func main() {
	fonts := font.NewCache(liberation.Collection())
	plot.DefaultTextHandler = text.Latex{
		Fonts: fonts,
	}

	p := plot.New()
	p.Title.Text = `$f(x) = \sqrt{\alpha x \Gamma}$`
	p.X.Label.Text = `$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`
	p.Y.Label.Text = `$\beta$`

	p.X.Min = -1
	p.X.Max = +1
	p.Y.Min = -1
	p.Y.Max = +1

	labels, err := plotter.NewLabels(plotter.XYLabels{
		XYs: []plotter.XY{
			{X: +0.0, Y: +0.0},
			{X: -0.9, Y: -0.9},
			{X: +0.6, Y: +0.0},
			{X: +0.5, Y: -0.9},
		},
		Labels: []string{
			`$\frac{\sqrt{x}}{2\pi\Gamma\gamma}$`,
			`$LaTeX$`,
			"plain",
			`$\frac{\sqrt{x}}{2\beta}$`,
		},
	})
	if err != nil {
		log.Fatalf("could not create labels: %+v", err)
	}
	labels.TextStyle[0].Font.Size = 24
	labels.TextStyle[0].Color = color.RGBA{B: 255, A: 255}
	labels.TextStyle[0].XAlign = draw.XCenter
	labels.TextStyle[0].YAlign = draw.YCenter

	labels.TextStyle[1].Font.Size = 24
	labels.TextStyle[1].Color = color.RGBA{R: 255, A: 255}
	labels.TextStyle[1].Rotation = math.Pi / 4

	labels.TextStyle[2].Font.Size = 24
	labels.TextStyle[2].Rotation = math.Pi / 4
	labels.TextStyle[2].YAlign = draw.YCenter
	labels.TextStyle[2].Handler = &text.Plain{Fonts: fonts}

	labels.TextStyle[3].Font.Size = 24
	labels.TextStyle[3].Rotation = math.Pi / 2

	p.Add(labels)
	p.Add(plotter.NewGlyphBoxes())
	p.Add(plotter.NewGrid())

	err = p.Save(10*vg.Centimeter, 5*vg.Centimeter, "testdata/latex_"+runtime.GOARCH+".png")
	if err != nil {
		log.Fatalf("could not save plot: %+v", err)
	}
}
Output:

func (Latex) Box

func (hdlr Latex) Box(txt string, fnt font.Font) (width, height, depth vg.Length)

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.

func (Latex) Cache added in v0.9.0

func (hdlr Latex) Cache() *font.Cache

Cache returns the cache of fonts used by the text handler.

func (Latex) Draw

func (hdlr Latex) Draw(c vg.Canvas, txt string, sty Style, pt vg.Point)

Draw renders the given text with the provided style and position on the canvas.

func (Latex) Extents added in v0.9.0

func (hdlr Latex) Extents(fnt font.Font) font.Extents

Extents returns the Extents of a font.

func (Latex) Lines added in v0.9.0

func (hdlr Latex) Lines(txt string) []string

Lines splits a given block of text into separate lines.

type Plain

type Plain struct {
	Fonts *font.Cache
}

Plain is a text/plain handler.

func (Plain) Box added in v0.9.0

func (hdlr Plain) Box(txt string, fnt font.Font) (width, height, depth vg.Length)

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.

func (Plain) Cache added in v0.9.0

func (hdlr Plain) Cache() *font.Cache

Cache returns the cache of fonts used by the text handler.

func (Plain) Draw added in v0.9.0

func (hdlr Plain) Draw(c vg.Canvas, txt string, sty Style, pt vg.Point)

Draw renders the given text with the provided style and position on the canvas.

func (Plain) Extents added in v0.9.0

func (hdlr Plain) Extents(fnt font.Font) font.Extents

Extents returns the Extents of a font.

func (Plain) Lines added in v0.9.0

func (hdlr Plain) Lines(txt string) []string

Lines splits a given block of text into separate lines.

type Style added in v0.9.0

type Style struct {
	// Color is the text color.
	Color color.Color

	// Font is the font description.
	Font font.Font

	// Rotation is the text rotation in radians, performed around the axis
	// defined by XAlign and YAlign.
	Rotation float64

	// XAlign and YAlign specify the alignment of the text.
	XAlign XAlignment
	YAlign YAlignment

	// Handler parses and formats text according to a given
	// dialect (Markdown, LaTeX, plain, ...)
	// The default is a plain text handler.
	Handler Handler
}

Style describes what text will look like.

func (Style) FontExtents added in v0.9.0

func (s Style) FontExtents() font.Extents

FontExtents returns the extents of this Style's font.

func (Style) Height added in v0.9.0

func (s Style) Height(txt string) vg.Length

Height returns the height of the text when using the given font before any text rotation is applied.

func (Style) Rectangle added in v0.9.0

func (s Style) Rectangle(txt string) vg.Rectangle

Rectangle returns a rectangle giving the bounds of this text assuming that it is drawn at (0, 0).

func (Style) Width added in v0.9.0

func (s Style) Width(txt string) (max vg.Length)

Width returns the width of lines of text when using the given font before any text rotation is applied.

type XAlignment added in v0.9.0

type XAlignment float64

XAlignment specifies text alignment in the X direction. Three preset options are available, but an arbitrary alignment can also be specified using XAlignment(desired number).

const (
	// XLeft aligns the left edge of the text with the specified location.
	XLeft XAlignment = 0
	// XCenter aligns the horizontal center of the text with the specified location.
	XCenter XAlignment = -0.5
	// XRight aligns the right edge of the text with the specified location.
	XRight XAlignment = -1
)

type YAlignment added in v0.9.0

type YAlignment float64

YAlignment specifies text alignment in the Y direction. Three preset options are available, but an arbitrary alignment can also be specified using YAlignment(desired number).

const (
	// YTop aligns the top of of the text with the specified location.
	YTop YAlignment = -1
	// YCenter aligns the vertical center of the text with the specified location.
	YCenter YAlignment = -0.5
	// YBottom aligns the bottom of the text with the specified location.
	YBottom YAlignment = 0
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL