styled

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: BSD-3-Clause Imports: 5 Imported by: 3

README

Warning

This is work in progress, do not use

Documentation

Overview

Package styled makes styled text.

Status

Work in progress.

_________________________________________________________________________

BSD 3-Clause License

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func T

func T() tracing.Trace

T traces to a global core-tracer.

Types

type Paragraph

type Paragraph struct {
	Offset uint64 // the paragraph's start position in terms of positions of the embedding text.
	// contains filtered or unexported fields
}

Paragraph represents a styled paragraph of text. It usually is a substring of a styled text, but differs from a text insofar as it may be prepared for output. Outputting styled text in general includes identifying runs of bidirectional text, which is an operation defined on paragraphs (at least by Unicode Annex #9). Moreover, output of styled text may include breaking up paragraphs into lines. Linebreaking in turn interacts with the handling of runs of bidirectional text, which in a sense restricts line-breaking to paragraphs.

After a styled paragraph has been created, e.g., by copying out a section from a styled text, it's textual content is not to change any more. However, it is allowed to change styles for spans of a paragraph's text.

Offset is the paragraph's start position in terms of byte positions of the embedding text. It is provided when creating a paragraph and held solely for a client's bookkeeping purposes. The functions of this package do not in any way depend on it.

func ParagraphFromText

func ParagraphFromText(text *Text, from, to uint64, embBidi bidi.Direction,
	m bidi.OutOfLineBidiMarkup) (*Paragraph, error)

ParagraphFromText creates a styled paragraph from a segment of a styled text. Parameters `from` and `to` denote the segment.

Paragraphs may contain left-to-right text as well as right-to-left text. Clients should provide the overall Bidi context to apply, together with an optional function providing hints for Bidi runs. ParagraphFromText will apply the Unicode Bidi Algorithm to the paragraph's text. Clients may then call BidiLevels() to receive the resolved Bidi levels found by the algorithm.

A paragraph remembers the `from` parameter in member `Offset`.

func (*Paragraph) BidiLevels

func (para *Paragraph) BidiLevels() *bidi.ResolvedLevels

BidiLevels returns the resolved Bidi levels in a paragraph of text.

func (*Paragraph) EachStyleRun

func (para *Paragraph) EachStyleRun(f func(content string, sty Style, pos, length uint64) error) error

EachStyleRun applies a function to each run of a single style. pos is the text position of this run of text within the overall styled text, i.e., it included para.Offset.

This may be thought of as a “push”-interface to access style runs for a text. For a “pull”-interface please refer to interface `itemized.Iterator`.

func (*Paragraph) Raw

func (para *Paragraph) Raw() cords.Cord

Raw returns the underlying raw text of the paragraph.

func (*Paragraph) Reader

func (para *Paragraph) Reader() io.Reader

Reader returns an io.Reader for the raw text of the paragraph (without styles).

func (*Paragraph) Style

func (para *Paragraph) Style(style Style, from, to uint64) *Paragraph

Style styles a run of text of a styled paragraph, given the start and end position.

func (*Paragraph) StyleAt

func (para *Paragraph) StyleAt(pos uint64) (Style, uint64, error)

StyleAt returns the active style at text position pos, together with an index relative to the start of the style run.

Overwrites StyleAt from cords.styled.Text

func (*Paragraph) StyleRuns

func (para *Paragraph) StyleRuns() []StyleChange

StyleRuns returns a slice of style runs for a styled text.

func (*Paragraph) WrapAt

func (para *Paragraph) WrapAt(pos uint64) (*Text, *bidi.Ordering, error)

WrapAt splits of a front segment (usually a “line”) from a paragraph.

type Style

type Style interface {
	Equals(other Style) bool // does this Style look equal or differently than another one ?
	String() string          // return some kind of identifying string
}

Style represents a styling-format which can be applied to a run of text.

type StyleChange

type StyleChange struct {
	Style    Style
	Position uint64
	Length   uint64
}

StyleChange holds a style and the text position where the style run starts.

type Text

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

Text is a styled text. Its text and its styles are automatically synchronized.

func Section

func Section(t *Text, from, to uint64) (*Text, error)

Section copies a piece of styled text, delimited by parameters from and to.

func TextFromCord

func TextFromCord(text cords.Cord) *Text

TextFromCord creates a stylable text from a cord.

func TextFromString

func TextFromString(s string) *Text

TextFromString creates a stylable text from a string.

func (*Text) EachStyleRun

func (t *Text) EachStyleRun(f func(content string, sty Style, pos uint64) error) error

EachStyleRun applies a function to each run of a single style. pos is the text position of this run of text within the overall styled text.

This may be thought of as a “push”-interface to access style runs for a text. For a “pull”-interface please refer to interface `itemized.Iterator`.

func (*Text) Raw

func (t *Text) Raw() cords.Cord

Raw returns a copy of the text without any styles.

func (*Text) Style

func (t *Text) Style(sty Style, from, to uint64) *Text

Style styles a run of text, given the start and end position.

func (*Text) StyleAt

func (t *Text) StyleAt(pos uint64) (Style, uint64, error)

StyleAt returns the style at byte position pos of the styled text.

func (*Text) StyleRuns

func (t *Text) StyleRuns() []StyleChange

StyleRuns returns a slice of style runs for a styled text.

type TextBuilder

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

TextBuilder is for building styled text from style runs.

func NewTextBuilder

func NewTextBuilder() *TextBuilder

NewTextBuilder creates a new and empty builder for styled.Text.

func (*TextBuilder) Append

func (b *TextBuilder) Append(leaf cords.Leaf, style Style) error

Append appends a text fragement represented by a cord leaf at the end of the text to build.

func (*TextBuilder) Len

func (b *TextBuilder) Len() uint64

Len returns the provisional total length of the fragments collected up to now.

func (TextBuilder) Text

func (b TextBuilder) Text() *Text

Text returns the styled text which this builder is holding up to now. It is illegal to continue adding fragments after `Text` has been called, but `Text` may be called multiple times.

Directories

Path Synopsis
Package formatter formats styled text on output devices with fixed-width fonts.
Package formatter formats styled text on output devices with fixed-width fonts.
Package inline styles inline text such as HTML-spans or console-output.
Package inline styles inline text such as HTML-spans or console-output.
Package itemized helps itemizing paragraphs and prepare them for formatting.
Package itemized helps itemizing paragraphs and prepare them for formatting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳