fribidi

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: LGPL-2.1, LGPL-2.1 Imports: 5 Imported by: 5

Documentation

Overview

Package fribidi is a Golang port of the C/C++ Free Implementation of the Unicode Bidirectional Algorithm (https://github.com/fribidi/fribidi). It supports the main features required to do text layout. It depends on golang.org/x/text/unicode/bidi to fetch unicode properties of runes.

Index

Constants

View Source
const (
	LTR  = maskSTRONG | maskLETTER
	RTL  = maskSTRONG | maskLETTER | maskRTL
	EN   = maskWEAK | maskNUMBER
	ON   = maskNEUTRAL
	WLTR = maskWEAK
	WRTL = maskWEAK | maskRTL
	PDF  = maskWEAK | maskEXPLICIT
	LRI  = maskNEUTRAL | maskISOLATE
	RLI  = maskNEUTRAL | maskISOLATE | maskRTL
	FSI  = maskNEUTRAL | maskISOLATE | maskFIRST
	BS   = maskNEUTRAL | maskSPACE | maskSEPARATOR | maskBS
	NSM  = maskWEAK | maskNSM
	AL   = maskSTRONG | maskLETTER | maskRTL | maskARABIC
	AN   = maskWEAK | maskNUMBER | maskARABIC
	CS   = maskWEAK | maskNUMSEPTER | maskCS
	ET   = maskWEAK | maskNUMSEPTER | maskET
	PDI  = maskNEUTRAL | maskWEAK | maskISOLATE // Pop Directional Isolate
	LRO  = maskSTRONG | maskEXPLICIT | maskOVERRIDE
	RLO  = maskSTRONG | maskEXPLICIT | maskOVERRIDE | maskRTL
	RLE  = maskSTRONG | maskEXPLICIT | maskRTL
	LRE  = maskSTRONG | maskEXPLICIT
	WS   = maskNEUTRAL | maskSPACE | maskWS
	ES   = maskWEAK | maskNUMSEPTER | maskES
	BN   = maskWEAK | maskSPACE | maskBN
	SS   = maskNEUTRAL | maskSPACE | maskSEPARATOR | maskSS
)

Variables

This section is empty.

Functions

func GetParEmbeddingLevels

func GetParEmbeddingLevels(bidiTypes []CharType, bracketTypes []BracketType,
	pbaseDir *ParType) (embeddingLevels []Level, maxLevel Level)

GetParEmbeddingLevels finds the bidi embedding levels of a single paragraph, as defined by the Unicode Bidirectional Algorithm available at http://www.unicode.org/reports/tr9/. This function implements rules P2 to I1 inclusive, and parts 1 to 3 of L1, except for rule X9 which is implemented in removeBidiMarks(). Part 4 of L1 is implemented in ReorderLine().

`bidiTypes` is a list of bidi types as returned by GetBidiTypes() `bracketTypes` is either empty or a list of bracket types as returned by GetBracketTypes()

Returns a slice of same length as `bidiTypes`, and the maximum level found plus one, which is thus always >= 1.

func LogicalToVisual

func LogicalToVisual(flags Options, str []rune, paragraphBaseDir *ParType) (Visual, Level)

LogicalToVisual converts the logical input string to the visual output strings as specified by the Unicode Bidirectional Algorithm. As a side effect it also generates mapping lists between the two strings, and the list of embedding levels as defined by the algorithm.

Note that this function handles one-line paragraphs. For multi- paragraph texts it is necessary to first split the text into separate paragraphs and then carry over the resolved `paragraphBaseDir` between the subsequent invocations.

The maximum level found plus one is also returned.

func Shape

func Shape(flags Options, embeddingLevels []Level,
	arabProps []JoiningType, str []rune)

Shape does all shaping work that depends on the resolved embedding levels of the characters. Currently it does mirroring and Arabic shaping, but the list may grow in the future.

If arabProps is nil, no Arabic shaping is performed.

Feel free to do your own shaping before or after calling this function, but you should take care of embedding levels yourself then.

Types

type BracketType

type BracketType uint32

BracketType is a rune value with its MSB is used to indicate an opening bracket

const (
	NoBracket BracketType = 0
)

func GetBracket

func GetBracket(ch rune) BracketType

GetBracket finds the bracketed equivalent of a character as defined in the file BidiBrackets.txt of the Unicode Character Database available at http://www.unicode.org/Public/UNIDATA/BidiBrackets.txt.

If the input character is declared as a brackets character in the Unicode standard and has a bracketed equivalent, the matching bracketed character is returned, with its high bit set. Otherwise zero is returned.

type CharType

type CharType uint32

func GetBidiType

func GetBidiType(ch rune) CharType

GetBidiType returns the bidi type of a character as defined in Table 3.7 Bidirectional Character Types of the Unicode Bidirectional Algorithm available at http://www.unicode.org/reports/tr9/#Bidirectional_Character_Types, using data provided by golang.org/x/text/unicode/bidi

func (CharType) IsArabic

func (p CharType) IsArabic() bool

IsArabic checks if `p` is arabic: AL, AN?

func (CharType) IsIsolate

func (p CharType) IsIsolate() bool

IsIsolate checks is `p` is isolator

func (CharType) IsLetter

func (p CharType) IsLetter() bool

IsLetter checks is `p` is letter : L, R, AL ?

func (CharType) IsNumber

func (p CharType) IsNumber() bool

IsNumber checks is `p` is number : EN, AN ?

func (CharType) IsRtl

func (p CharType) IsRtl() bool

IsRtl checks is `p` is right to left: RTL, AL, RLE, RLO ?

func (CharType) IsStrong

func (p CharType) IsStrong() bool

IsStrong checks if `p` is string.

func (CharType) IsWeak

func (p CharType) IsWeak() bool

IsWeak checks if `p` is weak

func (CharType) String

func (p CharType) String() string

type JoiningType

type JoiningType uint8
const (
	U JoiningType = 0                                   /* Un-joining, e.g. Full Stop */
	R JoiningType = joinsRight | arabShapes             /* Right-joining, e.g. Arabic Letter Dal */
	D JoiningType = joinsRight | joinsLeft | arabShapes /* Dual-joining, e.g. Arabic Letter Ain */
	C JoiningType = joinsRight | joinsLeft              /* Join-Causing, e.g. Tatweel, ZWJ */
	L JoiningType = joinsLeft | arabShapes              /* Left-joining, i.e. fictional */
	T JoiningType = transparent | arabShapes            /* Transparent, e.g. Arabic Fatha */
	G JoiningType = ignored                             /* Ignored, e.g. LRE, RLE, ZWNBSP */
)

type Level

type Level int8

Level is the embedding level in a paragraph

func ReorderLine

func ReorderLine(
	flags Options, bidiTypes []CharType,
	length, off int,
	baseDir ParType,

	embeddingLevels []Level, visualStr []rune, map_ []int) Level

ReorderLine reorders the characters in a line of text from logical to final visual order. This function implements part 4 of rule L1, and rules L2 and L3 of the Unicode Bidirectional Algorithm available at http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels.

As a side effect it also sets position maps if not nil.

You should provide the resolved paragraph direction and embedding levels as set by GetParEmbeddingLevels(), which may change a bit. To be exact, the embedding level of any sequence of white space at the end of line is reset to the paragraph embedding level (according to part 4 of rule L1).

Note that the bidi types and embedding levels are not reordered. You can reorder these arrays using the map later.

`visualStr` and `map_` must be either empty, or with same length as other inputs.

See `Options` for more information.

The maximum level found in this line plus one is returned

type Options

type Options int

Options is a flag to customize fribidi behaviour. The flags beginning with Shape affects the `Shape` function.

const (
	// Whether non-spacing marks for right-to-left parts of the text should be reordered to come after
	// their base characters in the visual string or not.
	// Most rendering engines expect this behavior, but console-based systems for example do not like it.
	// It is on in DefaultFlags.
	ReorderNSM Options = 1 << 1

	ShapeMirroring Options = 1      // in DefaultFlags, do mirroring
	ShapeArabPres  Options = 1 << 8 // in DefaultFlags, shape Arabic characters to their presentation form glyphs
	ShapeArabLiga  Options = 1 << 9 // in DefaultFlags, form mandatory Arabic ligatures

	// Perform additional Arabic shaping suitable for text rendered on
	// grid terminals with no mark rendering capabilities.
	// NOT SUPPORTED YET
	ShapeArabConsole Options = 1 << 10

	DefaultFlags = baseDefault | arabic
)

Define option flags that various functions use.

type ParType

type ParType = CharType

type Visual

type Visual struct {
	Str             []rune  // visual string
	VisualToLogical []int   // mapping from visual string back to the logical string indexes
	EmbeddingLevels []Level // list of embedding levels
}

Visual is the visual output as specified by the Unicode Bidirectional Algorithm

func (Visual) LogicalToVisual

func (v Visual) LogicalToVisual() []int

LogicalToVisual reverts `VisualToLogical`, return the mapping from logical to visual string indexes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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