wordwrap

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: Apache-2.0 Imports: 10 Imported by: 2

README

golang-wrapper

This is a library to provide positional and rendering word wrapping of text any arbitrary rectangle. It is capable of giving you the positions of each individual word, as well as rendering it to an image if desired.

To

It is technically capable of handling rich text and non-text objects; adding them can be done manually as a library user.

Part of the goal of the library was to:

  • Provide just positioning details without drawing
  • Be as simple as necessary
  • Be usable with the image package and not provide too much of a framework
  • Support rich text eventually
  • Support arbitrary locations provided as a image.Rectangle or image.Image

Concept

Box

The basics of the library is that it first breaks up the line in to "boxes" boxes can essentially be anything however in the 'simple' implementation here they are assumed to be a set of characters, spaces, or a control character (essentially a new line) A box can be thought of the lowest level representation that the library is willing to dea with.

This is created by a boxer a standard iterator like component:

type Boxer interface {
    Next() (Box, int, error)
    SetFontDrawer(face *font.Drawer)
    FontDrawer() *font.Drawer
    Back(i int)
}

For simple uses of the wrapping library you shouldn't need to worry about this. But you might care if you want to insert your box, which can be done provided it implements the: Box interface. But basically a box interface tells the size of the object, the baseline, and a mechanism for drawing it.

The baseline is probably the most important part, it is the "line" which the character is positioned. If you were to mix multiple font sizes you would want a consistent baseline so they all appear on the same level:

Line

A line is just that a line of boxes that fit in the given space (provided in a rect). A line is produced by a Liner. Such as a simple liner. Line does the real folding work; in the 'simple' version it calls the boxer (subject to change) for the next element. Then stops when it runs out of space. If it ends on a space it will return a new line character instead.

Simple

Simple basically is the first iteration which is simple enough to be used. I have used it as I intend to add versions which work differently. Simple makes many assumptions such as language assumptions.

Wrap

Wrap is just a container object for ease of use.

Usage

How do I use this to draw text in the simplest possible way?

    i := image.NewRGBA(image.Rect(0, 0, *width, *height))
    gr, err := OpenFont(*fontname)
    if err != nil {
        log.Panicf("Error opening font %s: %s", *fontname, err)
    }
    grf := GetFontFace(*fontsize, *dpi, gr)
    text, err := GetText(*textsource)
    if err != nil {
        log.Panicf("Text fetch error: %s", err)
    }
    if err := wordwrap.SimpleWrapTextToImage(text, i, grf, options); err != nil {
        log.Panicf("Text wrap and draw error: %s", err)
    }

Note:

  • OpenFont - left to an exercise for the user
  • GetFontFace - left to an exercise for the user
  • GetText - left to an exercise for the user

You could also do it in 2 steps, this provides the rectangles incase you wanted to make a word clickable.

    i := image.NewRGBA(image.Rect(0, 0, *width, *height))
    gr, err := OpenFont(*fontname)
    if err != nil {
        log.Panicf("Error opening font %s: %s", *fontname, err)
    }
    grf := GetFontFace(*fontsize, *dpi, gr)
    text, err := GetText(*textsource)
    if err != nil {
        log.Panicf("Text fetch error: %s", err)
    }
    target := image.Rect(350,44,592, 209)
    sw, lines, _, err := wordwrap.SimpleWrapTextToRect(text, target, grf)
    if err != nil {
    log.Panicf("Text wrap error: %s", err)
    }
    if err := sw.RenderLines(i, lines, target.Min); err != nil {
    log.Panicf("Text draw error: %s", err)
    }

Options

There will be more options but some are:

WordWrap/Line Option: wordwrap.BoxLine

Using the option BoxLine will cause the image to draw a box around the lines of boxes. Like such

Usage:

wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.BoxLine)
WordWrap/Box Option: wordwrap.BoxBox

Using the option BoxLine will cause the image to draw a box around the boxes. Like such

Usage:

wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.BoxBox)
wordwrap.NewPageBreakBox

Adds a box that will be put at the end of every "page" of the word wrap. For instance a "more text" option used in: https://github.com/arran4/golang-rpg-textbox

Usage:

wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.NewPageBreakBox(NewImageBox(image)))
wordwrap.ImageBoxMetricAboveTheLine (default)

Puts the image above the line as you would expect on a modern word processor

Usage:

wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.NewPageBreakBox(wordwrap.NewImageBox(chevronImage, wordwrap.ImageBoxMetricAboveTheLine), wordwrap.BoxBox))
wordwrap.ImageBoxMetricBelowTheLine (default)

Puts the image below the line as you would expect on a modern word processor

Usage:

wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.NewPageBreakBox(wordwrap.NewImageBox(chevronImage, wordwrap.ImageBoxMetricBelowTheLine), wordwrap.BoxBox))
wordwrap.ImageBoxMetricCenterLine

Vertically centers the box line

Usage:

wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.NewPageBreakBox(wordwrap.NewImageBox(chevronImage, wordwrap.ImageBoxMetricCenter(fontDrawer)), wordwrap.BoxBox))
wordwrap.SourceImageMapper

This allows you to substitute the source image for a box. Such as the source image for text is a color.Uniform image, so it allows you to change the color, or apply some other effect such as a pattern or fade it out.

For an image you can use proxy / interceptor pattern draw.Image structure to modify the source image as you want.

wordwrap.BoxDrawMap

Is a function to the form:

func(box Box, drawOps *DrawConfig, bps *BoxPositionStats) Box

Which is executed just before each box is drawn if provided. This allows you to substitute a box, such as with an empty box if you don't wish for it to be drawn, or you could use it to mask input.

Positioning functions: wordwrap.HorizontalCenterLines wordwrap.RightLines wordwrap.HorizontalCenterBlock wordwrap.RightBlock wordwrap.VerticalCenterBlock wordwrap.BottomBlock

Vertical or horizontally justifies or positions the lines or block, as per below.

Block is the entire block of text, while the line is just each line individually.

Option Result Usage
wordwrap.HorizontalCenterLines wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.HorizontalCenterLines)
wordwrap.RightLines wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.RightLines)
wordwrap.HorizontalCenterBlock wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.HorizontalCenterBlock)
wordwrap.RightBlock wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.RightBlock)
wordwrap.VerticalCenterBlock wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.VerticalCenterBlock)
wordwrap.BottomBlock wordwrap.SimpleWrapTextToImage(text, i, grf, wordwrap.BottomBlock)

CLI app

For demo purposes there is a CLI app in cmd/simplewraptoimage

  -boxbox
    	Box the box
  -boxline
    	Box the line
  -dpi float
    	Doc dpi (default 180)
  -font string
    	Text font (default "goregular")
  -height int
    	Doc height (default 600)
  -out string
    	file to write to, in some cases this is ignored (default "out.png")
  -size float
    	font size (default 16)
  -text string
    	File in, or - for std input (default "-")
  -width int
    	Doc width (default 400)

Only font that is supported is "goregular" as this is only a demo. Happy to accept PRs to expand the util package to make it more general. (Or to include more cli.)

The contents of the images directory are outputs from this using the test data from the folder testdata

License

TBH I really haven't thought about it. Contact me

Documentation

Index

Constants

View Source
const (
	RSimpleBox = iota
	RCRLF
	RNIL
)

Matches objects

Variables

View Source
var BoxBox = boxerOptionFunc(func(f interface{}) {
	bf := func(box Box) {
		switch box := box.(type) {
		case interface{ turnOnBox() }:
			box.turnOnBox()
		}
	}
	switch f := f.(type) {
	case *SimpleBoxer:
		f.postBoxOptions = append(f.postBoxOptions, bf)
	case Box:
		bf(f)
	}
})

BoxBox is a BoxerOption that tells the Box to draw a box around itself mostly for debugging purposes but will be the basis of how select and highlighting could work, such as the cursor

View Source
var BoxLine = folderOptionFunc(func(f interface{}) {
	if f, ok := f.(*SimpleFolder); ok {
		f.lineOptions = append(f.lineOptions, func(line Line) {
			switch line := line.(type) {
			case interface{ turnOnBox() }:
				line.turnOnBox()
			default:
				log.Printf("can't apply")
			}
		})
	}
})

BoxLine is a FolderOption that tells the Liner to draw a box around the line mostly for debugging purposes but will be the basis of how select and highlighting could work

View Source
var ImageBoxMetricAboveTheLine imageBoxOptionMetricCalcFunc = func(ib *ImageBox) font.Metrics {
	return font.Metrics{
		Height: fixed.I(ib.I.Bounds().Dy()),
		Ascent: fixed.I(ib.I.Bounds().Dy()),
	}
}

ImageBoxMetricAboveTheLine Puts the image above the baseline as you would expect if you were using a word processor

View Source
var ImageBoxMetricBelowTheLine imageBoxOptionMetricCalcFunc = func(ib *ImageBox) font.Metrics {
	return font.Metrics{
		Height:  fixed.I(ib.I.Bounds().Dy()),
		Descent: fixed.I(ib.I.Bounds().Dy()),
	}
}

ImageBoxMetricBelowTheLine Puts the image above the baseline. Rarely done

View Source
var ImageBoxMetricCenter = func(fd *font.Drawer) imageBoxOptionMetricCalcFunc {
	return func(ib *ImageBox) font.Metrics {
		if fd == nil {
			fd = ib.fontDrawer
		}
		if fd == nil {
			return ImageBoxMetricBelowTheLine(ib)
		}
		m := fd.Face.Metrics()
		return font.Metrics{
			Height:  fixed.I(ib.I.Bounds().Dy()),
			Descent: fixed.I(ib.I.Bounds().Dy())/2 - m.Descent/2,
			Ascent:  fixed.I(ib.I.Bounds().Dy())/2 + m.Descent/2,
		}
	}
}

ImageBoxMetricCenter Puts the image running from the top down

Functions

func DrawBox

func DrawBox(i draw.Image, s image.Rectangle, dc *DrawConfig)

DrawBox literally draws a simple box

func IsCR

func IsCR(r rune) bool

IsCR Is a carriage return

func IsLF

func IsLF(r rune) bool

IsLF is a line feed

func IsSpaceButNotCRLF

func IsSpaceButNotCRLF(r rune) bool

IsSpaceButNotCRLF Because spaces are different to CR and LF for word wrapping

func Once

func Once(f func(r rune) bool) func(rune) bool

Once counts matches

func SimpleBoxerGrab

func SimpleBoxerGrab(text []rune) (int, []rune, int)

SimpleBoxerGrab Consumer of characters until change. Could be made to conform to strings.Scanner

func SimpleWrapTextToImage

func SimpleWrapTextToImage(text string, i Image, grf font.Face, opts ...WrapperOption) error

SimpleWrapTextToImage all in one helper function to wrap text onto an image. Use image.Image's SubImage() to specify the exact location to render:

SimpleWrapTextToImage("text", i.SubImage(image.Rect(30,30,400,400)), font)

func SimpleWrapTextToRect

func SimpleWrapTextToRect(text string, r image.Rectangle, grf font.Face, opts ...WrapperOption) (*SimpleWrapper, []Line, image.Point, error)

SimpleWrapTextToRect calculates and returns the position of each box and the image.Point it would end.

Types

type Box

type Box interface {
	// AdvanceRect width of text
	AdvanceRect() fixed.Int26_6
	// MetricsRect all other font details of text
	MetricsRect() font.Metrics
	// Whitespace if this is a white space or not
	Whitespace() bool
	// DrawBox renders object
	DrawBox(i Image, y fixed.Int26_6, dc *DrawConfig)
	// FontDrawer font used
	FontDrawer() *font.Drawer
	// Len the length of the buffer represented by the box
	Len() int
	// TextValue extracts the text value
	TextValue() string
}

Box is a representation of a non-divisible unit (can be nested)

func NewSimpleTextBox

func NewSimpleTextBox(drawer *font.Drawer, t string) (Box, error)

NewSimpleTextBox constructor

type BoxDrawMap

type BoxDrawMap func(box Box, drawOps *DrawConfig, bps *BoxPositionStats) Box

BoxDrawMap allows the modification of boxes

func (BoxDrawMap) Apply

func (s BoxDrawMap) Apply(config *DrawConfig)

Apply installs the image source mapper

type BoxPositionStats

type BoxPositionStats struct {
	*LinePositionStats
	NumberInLine  int
	PageBoxOffset int
	WordOffset    int
}

BoxPositionStats Box position stats

type Boxer

type Boxer interface {
	// Next gets the next word in a Box
	Next() (Box, int, error)
	// SetFontDrawer Changes the default font
	SetFontDrawer(face *font.Drawer)
	// FontDrawer encapsulates default fonts and more
	FontDrawer() *font.Drawer
	// Back goes back i spaces (ie unreads)
	Back(i int)
	// HasNext if there are any unprocessed runes
	HasNext() bool
	// Push puts a box back on to the cache stack
	Push(box ...Box)
	// Pos text pos
	Pos() int
	// Unshift basically is Push but to the start
	Unshift(b ...Box)
	// Shift removes the first element but unlike Next doesn't attempt to generate a new one if there is nothing
	Shift() Box
}

Boxer is the tokenizer that splits the line into it's literal components

type BoxerOption

type BoxerOption interface {
	// WrapperOption Allows you to pass the option to a Wrapper and assume it gets passed to the constructor of the
	// Boxer
	WrapperOption
	// ApplyBoxConfig applies the config.
	ApplyBoxConfig(interface{})
}

BoxerOption for folders

type DrawConfig

type DrawConfig struct {
	SourceImageMapper SourceImageMapper
	BoxDrawMap        BoxDrawMap
}

DrawConfig options for the drawer

func NewDrawConfig

func NewDrawConfig(options ...DrawOption) *DrawConfig

NewDrawConfig construct a draw config from DrawOptions

func (*DrawConfig) ApplyMap

func (c *DrawConfig) ApplyMap(b Box, bps *BoxPositionStats) Box

ApplyMap applies the box mapping function used for conditionally rendering or modifying the object being rendered

type DrawOption

type DrawOption interface {
	Apply(*DrawConfig)
}

DrawOption options applied and passed down the drawing functions

type FitterConfig

type FitterConfig struct {
	IgnoreY bool
}

type FitterIgnoreY

type FitterIgnoreY struct{}

func (FitterIgnoreY) Apply

func (fiy FitterIgnoreY) Apply(c *FitterConfig)

type FitterOption

type FitterOption interface {
	Apply(*FitterConfig)
}

type Folder

type Folder interface {
	// Next line
	Next(yspace int) (Line, error)
}

Folder is the literal line sizer & producer function

type FolderOption

type FolderOption interface {
	// WrapperOption Allows you to pass the option to a Wrapper and assume it gets passed to the constructor of the
	// Folder
	WrapperOption
	// ApplyFoldConfig applies the config.
	ApplyFoldConfig(interface{})
}

FolderOption for folders

type FontDrawer

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

FontDrawer a wrapper around *font.Draw used to set the font

func NewFontDrawer

func NewFontDrawer(d *font.Drawer) *FontDrawer

NewFontDrawer a wrapper around *font.Draw used to set the font mostly for image

type HorizontalBlockPosition

type HorizontalBlockPosition int

HorizontalBlockPosition information about how to position the entire block of text rather than just the line horizontally

const (
	// LeftBLock positions the entire block of text left rather than just the line horizontally (default)
	LeftBLock HorizontalBlockPosition = iota
	// HorizontalCenterBlock positions the entire block of text center rather than just the line horizontally
	HorizontalCenterBlock
	// RightBlock positions the entire block of text right rather than just the line horizontally
	RightBlock
)

func (HorizontalBlockPosition) ApplyWrapperConfig

func (hp HorizontalBlockPosition) ApplyWrapperConfig(wr interface{})

ApplyWrapperConfig Stores the position against the wrapper object

type HorizontalLinePosition

type HorizontalLinePosition int

HorizontalLinePosition is the type for per-line level alignment.

const (
	// LeftLines default, produces lines that are individually left justified.
	LeftLines HorizontalLinePosition = iota
	// HorizontalCenterLines produces lines that are individually center justified.
	HorizontalCenterLines
	// RightLines produces lines that are individually right justified.
	RightLines
)

func (HorizontalLinePosition) ApplyFoldConfig

func (hp HorizontalLinePosition) ApplyFoldConfig(f interface{})

ApplyFoldConfig applies the configuration to the wrapper where it will be stored in the line.

func (HorizontalLinePosition) ApplyWrapperConfig

func (hp HorizontalLinePosition) ApplyWrapperConfig(wr interface{})

ApplyWrapperConfig Is required to pass the configuration through to the appropriate level -- Hopefully will be refactored

type HorizontalLinePositioner

type HorizontalLinePositioner interface {
	// contains filtered or unexported methods
}

HorizontalLinePositioner is a simple interface denoting a getter

type Image

type Image interface {
	draw.Image
	SubImage(image.Rectangle) image.Image
}

Image because image.Image / draw.Image should really have SubImage as part of it.

type ImageBox

type ImageBox struct {
	I image.Image
	M font.Metrics
	// contains filtered or unexported fields
}

ImageBox is a box that contains an image

func NewImageBox

func NewImageBox(i image.Image, options ...ImageBoxOption) *ImageBox

NewImageBox constructs a new ImageBox

func (*ImageBox) AdvanceRect

func (ib *ImageBox) AdvanceRect() fixed.Int26_6

AdvanceRect width of text

func (*ImageBox) CalculateMetrics

func (ib *ImageBox) CalculateMetrics()

CalculateMetrics calculate dimension and positioning

func (*ImageBox) DrawBox

func (ib *ImageBox) DrawBox(i Image, y fixed.Int26_6, dc *DrawConfig)

DrawBox renders object

func (*ImageBox) FontDrawer

func (ib *ImageBox) FontDrawer() *font.Drawer

FontDrawer font used

func (*ImageBox) Len

func (ib *ImageBox) Len() int

Len the length of the buffer represented by the box

func (*ImageBox) MetricsRect

func (ib *ImageBox) MetricsRect() font.Metrics

MetricsRect all other font details of text

func (*ImageBox) TextValue

func (ib *ImageBox) TextValue() string

TextValue returns the text suppressed by the line break (probably a white space including a \r\n)

func (*ImageBox) Whitespace

func (ib *ImageBox) Whitespace() bool

Whitespace if this is a white space or not

type ImageBoxOption

type ImageBoxOption interface {
	// contains filtered or unexported methods
}

ImageBoxOption modifiers for the ImageBox

type Line

type Line interface {
	// Size the line consumes
	Size() image.Rectangle
	// DrawLine draws the line
	DrawLine(i Image, options ...DrawOption) error
	// Boxes are the lines contents
	Boxes() []Box
	// TextValue extracts the text value
	TextValue() string
	// YValue where the baseline is
	YValue() int
	// PopSpaceFor will push box at the end, if there isn't enough width, it will make width space.
	PopSpaceFor(sf *SimpleFolder, r image.Rectangle, box Box) (int, error)
	// contains filtered or unexported methods
}

Line refers to a literal line of text

type LineBreakBox

type LineBreakBox struct {
	// Box is the box that linebreak contains if any
	Box
}

LineBreakBox represents a natural or an effective line break

func (*LineBreakBox) AdvanceRect

func (sb *LineBreakBox) AdvanceRect() fixed.Int26_6

AdvanceRect width of text

func (*LineBreakBox) DrawBox

func (sb *LineBreakBox) DrawBox(i Image, y fixed.Int26_6, dc *DrawConfig)

DrawBox renders object

type LinePositionStats

type LinePositionStats struct {
	LineNumber    int
	PageBoxOffset int
	WordOffset    int
	PageNumber    int
}

LinePositionStats numbers to use for pin pointing location

func (*LinePositionStats) BoxPositionStats

func (lps *LinePositionStats) BoxPositionStats(numberInLine int) *BoxPositionStats

BoxPositionStats generates object of same name

type OverflowMode

type OverflowMode int

OverflowMode Ways of describing overflow

const (
	// StrictBorders default overflow mode. Do not allow
	StrictBorders OverflowMode = iota
	// DescentOverflow Allow some decent overflow. Characters such as yjqp will overflow
	DescentOverflow
	// FullOverflowDuplicate Will allow the full line to overflow, and duplicate line next run
	FullOverflowDuplicate
)

type PageBreakBox

type PageBreakBox struct {
	// VisualBox is the box to render and use
	VisualBox Box
	// ContainerBox is the box that linebreak contains if any
	ContainerBox Box
}

PageBreakBox represents a natural or an effective page break

func NewPageBreak

func NewPageBreak(pbb Box) *PageBreakBox

NewPageBreak basic constructor for a page break.

func (*PageBreakBox) AdvanceRect

func (p *PageBreakBox) AdvanceRect() fixed.Int26_6

AdvanceRect width of text

func (*PageBreakBox) DrawBox

func (p *PageBreakBox) DrawBox(i Image, y fixed.Int26_6, dc *DrawConfig)

DrawBox renders object

func (*PageBreakBox) FontDrawer

func (p *PageBreakBox) FontDrawer() *font.Drawer

FontDrawer font used

func (*PageBreakBox) Len

func (p *PageBreakBox) Len() int

Len the length of the buffer represented by the box

func (*PageBreakBox) MetricsRect

func (p *PageBreakBox) MetricsRect() font.Metrics

MetricsRect all other font details of text

func (*PageBreakBox) TextValue

func (p *PageBreakBox) TextValue() string

TextValue returns the text suppressed by the line break (probably a white space including a \r\n)

func (*PageBreakBox) Whitespace

func (p *PageBreakBox) Whitespace() bool

Whitespace if contains a white space or not

type SimpleBoxer

type SimpleBoxer struct {
	Grabber func(text []rune) (int, []rune, int)
	// contains filtered or unexported fields
}

SimpleBoxer simple tokenizer basically determines if something unicode.IsSpace or is a new line, or is text and tells the calling Folder that. Putting the elements in the correct Box.

func NewSimpleBoxer

func NewSimpleBoxer(text []rune, drawer *font.Drawer, options ...BoxerOption) *SimpleBoxer

NewSimpleBoxer simple tokenizer basically determines if something unicode.IsSpace or is a new line, or is text and tells the calling Folder that. Putting the elements in the correct Box.

func (*SimpleBoxer) Back

func (sb *SimpleBoxer) Back(i int)

Back goes back i spaces (ie unreads)

func (*SimpleBoxer) FontDrawer

func (sb *SimpleBoxer) FontDrawer() *font.Drawer

FontDrawer encapsulates default fonts and more

func (*SimpleBoxer) HasNext

func (sb *SimpleBoxer) HasNext() bool

HasNext unprocessed bytes exist

func (*SimpleBoxer) Next

func (sb *SimpleBoxer) Next() (Box, int, error)

Next gets the next word in a Box

func (*SimpleBoxer) Pos

func (sb *SimpleBoxer) Pos() int

Pos current parser position.

func (*SimpleBoxer) Push

func (sb *SimpleBoxer) Push(box ...Box)

Push puts a box back on to the cache stack

func (*SimpleBoxer) SetFontDrawer

func (sb *SimpleBoxer) SetFontDrawer(face *font.Drawer)

SetFontDrawer Changes the default font

func (*SimpleBoxer) Shift

func (sb *SimpleBoxer) Shift() Box

func (*SimpleBoxer) Unshift

func (sb *SimpleBoxer) Unshift(box ...Box)

Unshift basically is Push but to the start

type SimpleFolder

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

SimpleFolder is a simple Folder

func NewSimpleFolder

func NewSimpleFolder(boxer Boxer, container image.Rectangle, lastFontDrawer *font.Drawer, options ...FolderOption) *SimpleFolder

NewSimpleFolder constructs a SimpleFolder applies options provided.

func (*SimpleFolder) NewLine

func (sf *SimpleFolder) NewLine() *SimpleLine

NewLine constructs a new simple line. (Later to be a factory proxy)

func (*SimpleFolder) Next

func (sf *SimpleFolder) Next(yspace int) (Line, error)

Next generates the next life if space

type SimpleLine

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

SimpleLine is a simple implementation to prevent name space names later. Represents a line

func (*SimpleLine) Boxes

func (l *SimpleLine) Boxes() []Box

Boxes are the lines contents

func (*SimpleLine) DrawLine

func (l *SimpleLine) DrawLine(i Image, options ...DrawOption) error

DrawLine renders image to image, you can control the location by using the SubImage function.

func (*SimpleLine) Pop

func (l *SimpleLine) Pop() Box

Pop a box off of the end of a line. Ignores all height components that will require a recalculation, drops PageBreak

func (*SimpleLine) PopSpaceFor

func (l *SimpleLine) PopSpaceFor(sf *SimpleFolder, r image.Rectangle, box Box) (int, error)

PopSpaceFor will push box at the end, if there isn't enough width, it will make width space.

func (*SimpleLine) Push

func (l *SimpleLine) Push(b Box, a fixed.Int26_6)

Push a box onto the end, and also copy values in appropriately

func (*SimpleLine) Size

func (l *SimpleLine) Size() image.Rectangle

Size is the size consumed of the line

func (*SimpleLine) TextValue

func (l *SimpleLine) TextValue() string

TextValue extracts the text value of the line

func (*SimpleLine) YValue

func (l *SimpleLine) YValue() int

YValue where the baseline is

type SimpleTextBox

type SimpleTextBox struct {
	Contents string
	Bounds   fixed.Rectangle26_6

	Advance fixed.Int26_6
	Metrics font.Metrics
	// contains filtered or unexported fields
}

SimpleTextBox represents an indivisible series of characters.

func (*SimpleTextBox) AdvanceRect

func (sb *SimpleTextBox) AdvanceRect() fixed.Int26_6

AdvanceRect width of text

func (*SimpleTextBox) DrawBox

func (sb *SimpleTextBox) DrawBox(i Image, y fixed.Int26_6, dc *DrawConfig)

DrawBox renders object

func (*SimpleTextBox) FontDrawer

func (sb *SimpleTextBox) FontDrawer() *font.Drawer

FontDrawer font used

func (*SimpleTextBox) Len

func (sb *SimpleTextBox) Len() int

Len is the string length of the contents of the box

func (*SimpleTextBox) MetricsRect

func (sb *SimpleTextBox) MetricsRect() font.Metrics

MetricsRect all other font details of text

func (*SimpleTextBox) TextValue

func (sb *SimpleTextBox) TextValue() string

TextValue stored value of the box

func (*SimpleTextBox) Whitespace

func (sb *SimpleTextBox) Whitespace() bool

Whitespace if this is a white space or not

type SimpleWrapper

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

SimpleWrapper quick and dirty wrapper.

func NewSimpleWrapper

func NewSimpleWrapper(text string, grf font.Face, opts ...WrapperOption) *SimpleWrapper

NewSimpleWrapper creates a new wrapper. This function retains previous text position, useful for creating "pages." assumes black text

func (*SimpleWrapper) ApplyOptions

func (sw *SimpleWrapper) ApplyOptions(opts ...WrapperOption)

ApplyOptions allows the application of options to the SimpleWrapper (Such as new fonts, or turning on / off boxes.

func (*SimpleWrapper) HasNext

func (sw *SimpleWrapper) HasNext() bool

HasNext are there any unprocessed bytes in the boxer

func (*SimpleWrapper) RenderLines

func (sw *SimpleWrapper) RenderLines(i Image, ls []Line, at image.Point, options ...DrawOption) error

RenderLines draws the boxes for the given lines. on the image, starting at the specified point ignoring the original boundaries but maintaining the wrapping. Also applies alignment options.

func (*SimpleWrapper) TextToRect

func (sw *SimpleWrapper) TextToRect(r image.Rectangle, ops ...FitterOption) ([]Line, image.Point, error)

TextToRect calculates and returns the position of each box and the image.Point it would end.

type SourceImageMapper

type SourceImageMapper func(image.Image) image.Image

SourceImageMapper allows passing in of an option that will map the original input in some way

func (SourceImageMapper) Apply

func (s SourceImageMapper) Apply(config *DrawConfig)

Apply installs the image source mapper

type VerticalBlockPosition

type VerticalBlockPosition int

VerticalBlockPosition information about how to position the entire block of text rather than just the line vertically

const (
	// TopBLock positions the entire block of text top
	TopBLock VerticalBlockPosition = iota
	// VerticalCenterBlock positions the entire block of text center
	VerticalCenterBlock
	// BottomBlock positions the entire block of text bottom
	BottomBlock
)

func (VerticalBlockPosition) ApplyWrapperConfig

func (hp VerticalBlockPosition) ApplyWrapperConfig(wr interface{})

ApplyWrapperConfig Stores the position against the wrapper object

type WrapperOption

type WrapperOption interface {
	// ApplyWrapperConfig applies the config.
	ApplyWrapperConfig(interface{})
}

WrapperOption for folders

func NewPageBreakBox

func NewPageBreakBox(b Box, opts ...BoxerOption) WrapperOption

NewPageBreakBox is a FolderOption that tells the Liner to add a chevron image to the end of every text block that continues past the given rect.

func YOverflow

func YOverflow(i OverflowMode) WrapperOption

YOverflow is a FolderOption that sets the type over overflow mode we will allow

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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