text

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// errors returned by NewController
	ErrTolerance = fmt.Errorf("unable to break lines using current tolerances")
	ErrWordSize  = fmt.Errorf("source text contains an unbreakable word that is longer than the maximum line length")

	// errors returned by DrawText
	ErrWidth   = fmt.Errorf("target area must be at least as wide as the maximum line width")
	ErrEmpty   = fmt.Errorf("source text buffer is empty")
	ErrLeading = fmt.Errorf("font leading must be greater than 0")
	ErrHeight  = fmt.Errorf("target area must be at least as tall as the font leading")
)

Functions

func CenterH

func CenterH(c *gdf.ContentStream, t []rune, rect gdf.Rect) float64

CenterH returns the x offset, in points, from the start of rect, needed to center t horizontally, if drawn with c's current font at c's current font size.

func CenterV

func CenterV(height float64, rect gdf.Rect) float64

CenterV returns the y offset, in points, from the start of rect, needed to center a line of text vertically based on the text's height. This is a naive approach.

Types

type Alignment

type Alignment uint
const (
	Left Alignment = iota
	Right
	Center
)

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

A Controller is a struct that aids in writing text to a ContentStream. The Controller can break text into lines and paragraphs, determine the appropriate kerning for glyphs in a string, and draw text according to the format specified by the ControllerCfg struct.

func NewController

func NewController(src FormatText, lineWidth float64, f FontFamily, cfg ControllerCfg) (Controller, error)

NewController returns a Controller that is ready to write src to ContentStreams. It returns an invalid Controller and an error if it encounters a problem while parsing and shaping src. lineWidth should be the maximum desired width, in points, of each line of the output text when drawn to a gdf.ContentStream.

func (*Controller) DrawText

func (tc *Controller) DrawText(c *gdf.ContentStream, area gdf.Rect) (gdf.Point, bool, error)

DrawText draws the text from the Controller to the area of c specified by area. The return gdf.Point is the position of c's TextCursor after the text has been drawn. (This value would not be otherwise accessible because each call to DrawText encompasses a c.BeginText/EndText pair.) The returned bool indicates whether the Controller's buffer still contains additional source text. If this value is true, then future calls to DrawText can be used to draw the remaining source text - usually to different areas or ContentStreams.

type ControllerCfg

type ControllerCfg struct {
	Alignment
	Justification
	RenderMode     gdf.RenderMode
	FontSize       float64
	Leading        float64
	IsIndented     bool
	NColor, SColor gdf.Color // default nonstroking and stroking colors
	Looseness      float64   // the ratio of the maximum allowable space advance and the normal space advance in justified text
	Tightness      float64   // the ratio of the minimum allowable space advance and the normal space advance in justified text
	IsBold, IsItal bool
}

A ControllerCfg specifies options for the formatting of text drawn by a Controller.

func NewControllerCfg

func NewControllerCfg(fontSize, leading float64) ControllerCfg

type FontFamily

type FontFamily struct {
	Regular, Bold, Ital, BoldItal *gdf.Font
}

type FormatText

type FormatText []rune

FormatText represents a slice of runes that can specify the formatting and content of the source text. The TextController applies the following rules to the formatting directives contained in FormatText:

  1. rune(-1) is interpreted as end of text. Any runes that appear after this character will not be parsed.
  2. rune(-2) is interpreted as a color indicator. This character must be followed by three comma-separated 3-digit integers in [0,255] that specify the red, green, and blue components of an RGBColor. (This is equivalent to setting the nonstroking color of the document to RGBColor{R:float64(red)/255, G:float64(green)/255, B:float64(blue)/255}).
  3. rune(-3) toggles bold text on and off.
  4. rune(-4) toggles italic text on and off. The bold and italic indicators can be used to switch among fonts in a given font family.

type Justification

type Justification uint
const (
	Ragged Justification = iota
	Justified
)

Jump to

Keyboard shortcuts

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