Documentation
¶
Overview ¶
CPU-based vectorial text rendering has always been a performance sensitive task, with glyph rasterization being one of the most critical steps in the process. One of the techniques that can be used to improve the rasterization speed is using fixed point arithmetic instead of floating point arithmetic — and that's what brings us to this subpackage.
The fract subpackage defines a Unit type representing a 26.6 fixed point value and provides numerous methods to perform fixed point operations. Additionally, the subpackage also defines the Point and Rect helper types.
Other font related Golang packages tend to depend on golang.org/x/image/math/fixed instead, but this subpackage offers more methods, more accurate algorithms and is designed to integrate directly with etxt.
Index ¶
- Constants
- type Point
- func (self Point) AddPoint(point Point) Point
- func (self Point) AddUnits(x, y Unit) Point
- func (self Point) Floor() Point
- func (self Point) ImagePoint() image.Point
- func (self Point) In(rect Rect) bool
- func (self Point) String() string
- func (self Point) ToFloat32s() (x, y float32)
- func (self Point) ToFloat64s() (x, y float64)
- func (self Point) ToInts() (int, int)
- type Rect
- func (self Rect) AddImagePoint(point image.Point) Rect
- func (self Rect) AddInts(x, y int) Rect
- func (self Rect) AddPoint(pt Point) Rect
- func (self Rect) AddUnits(x, y Unit) Rect
- func (self Rect) CenteredAtIntCoords(x, y int) Rect
- func (self Rect) Clip(image *ebiten.Image) *ebiten.Image
- func (self Rect) Contains(point Point) bool
- func (self Rect) Empty() bool
- func (self Rect) HasZeroOrigin() bool
- func (self Rect) Height() Unit
- func (self Rect) ImageRect() image.Rectangle
- func (self Rect) IntHeight() int
- func (self Rect) IntOrigin() (int, int)
- func (self Rect) IntSize() (width, height int)
- func (self Rect) IntWidth() int
- func (self Rect) PadInts(horzMargin, vertMargin int) Rect
- func (self Rect) PadUnits(horzPad, vertPad Unit) Rect
- func (self Rect) Size() (width, height Unit)
- func (self Rect) String() string
- func (self Rect) ToFloat32s() (minX, minY, maxX, maxY float32)
- func (self Rect) ToFloat64s() (minX, minY, maxX, maxY float64)
- func (self Rect) ToInts() (minX, minY, maxX, maxY int)
- func (self Rect) Width() Unit
- type Unit
- func (self Unit) Abs() Unit
- func (self Unit) Away(reference int) Unit
- func (self Unit) Ceil() Unit
- func (self Unit) Div(divisor Unit) Unit
- func (self Unit) Floor() Unit
- func (self Unit) Fract() Unit
- func (self Unit) FractShift() Unit
- func (self Unit) HalfAway(reference int) Unit
- func (self Unit) HalfDown() Unit
- func (self Unit) HalfToward(reference int) Unit
- func (self Unit) HalfUp() Unit
- func (self Unit) IsWhole() bool
- func (self Unit) Mul(multiplier Unit) Unit
- func (self Unit) MulDown(multiplier Unit) Unit
- func (self Unit) MulInt(multiplier int) Unit
- func (self Unit) MulUp(multiplier Unit) Unit
- func (self Unit) QuantizeDown(step Unit) Unit
- func (self Unit) QuantizeUp(step Unit) Unit
- func (self Unit) Rescale(from, to Unit) Unit
- func (self Unit) ToFloat32() float32
- func (self Unit) ToFloat64() float64
- func (self Unit) ToInt() int
- func (self Unit) ToIntAway(reference int) int
- func (self Unit) ToIntCeil() int
- func (self Unit) ToIntFloor() int
- func (self Unit) ToIntHalfAway(reference int) int
- func (self Unit) ToIntHalfDown() int
- func (self Unit) ToIntHalfToward(reference int) int
- func (self Unit) ToIntHalfUp() int
- func (self Unit) ToIntToward(reference int) int
- func (self Unit) Toward(reference int) Unit
Constants ¶
const ( MaxUnit Unit = +0x7FFFFFFF MinUnit Unit = -0x7FFFFFFF - 1 One Unit = 64 // fract.One.ToInt() == 1 MaxInt int = +33554431 // max representable int MinInt int = -33554432 // min representable int MaxFloat64 float64 = +33554431.984375 // max representable float MinFloat64 float64 = -33554432 // min representable float Delta float64 = 0.015625 // float equivalent of Unit(1) => 1.0/64.0 HalfDelta float64 = 0.0078125 // 1.0/128.0 (used for rounding) )
Miscellaneous constants related to Unit.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Point ¶
A pair of Unit coordinates. Commonly used during rendering processes to keep track of the pen position within the rendering target.
func (Point) AddUnits ¶
Returns the result of adding the given pair of units to the current point coordinates.
func (Point) ImagePoint ¶
Converts the point coordinates to ints and returns them as an image.Point stdlib value. The conversion will round the units if necessary, which could be problematic in some contexts.
func (Point) ToFloat32s ¶
Returns the point coordinates as a pair of float32s.
func (Point) ToFloat64s ¶
Returns the point coordinates as a pair of float64s.
type Rect ¶
A pair of Point values defining a rectangular region. Like image.Rectangle, the Max point is not included in the rectangle. The behavior for malformed rectangles is undefined.
func FromImageRect ¶
Creates a rect from an image.Rectangle.
func IntsToRect ¶
Creates a rect from a set of four integers.
func UnitsToRect ¶
Creates a rect from a set of four units.
func (Rect) AddImagePoint ¶
Returns the result of translating the rect by the given value.
func (Rect) CenteredAtIntCoords ¶
Returns the result of translating the rect so its center becomes aligned to the given coordinates.
func (Rect) Contains ¶
Returns whether the rect contains the given point or not.
Remember that point == Rect.Min is included, but point == Rect.Max is not.
func (Rect) HasZeroOrigin ¶
Returns whether the Min point is (0, 0) or not.
func (Rect) ImageRect ¶
Converts the rect coordinates to ints and returns them as an image.Rectangle stdlib value. The conversion will round the units if necessary, which could be problematic in some contexts.
func (Rect) IntHeight ¶
Returns the height of the rect as an int. The conversion may introduce a loss of precision, but the returned int height is guaranteed to be >= than the original.
func (Rect) IntOrigin ¶
Returns the Min point as a pair of ints. The conversion may introduce a loss of precision, but the returned coordinates are guaranteed to be <= than the original.
func (Rect) IntSize ¶
Utility method equivalent to (Rect.IntWidth(), Rect.IntHeight()).
func (Rect) IntWidth ¶
Returns the width of the rect as an int. The conversion may introduce a loss of precision, but the returned int width is guaranteed to be >= than the original.
func (Rect) PadInts ¶
Same as Rect.PadUnits() but with ints.
func (Rect) PadUnits ¶
Returns the result of applying the given paddings to each side of the rect. In other words, the rect's width after the padding is increased by horzPad*2 (likewise for the height with vertPad*2).
func (Rect) Size ¶
Utility method equivalent to (Rect.Width(), Rect.Height()).
func (Rect) ToFloat32s ¶
Returns the rect coordinates as a set of four float32s.
func (Rect) ToFloat64s ¶
Returns the rect coordinates as a set of four float64s.
type Unit ¶
type Unit int32
Fixed point type to represent fractional values used for font rendering.
26 bits represent the integer part of the value, while the remaining 6 bits represent the decimal part. For an intuitive understanding, if you know that var ms Millis = 1000 is storing the equivalent to 1 second, then with Unit instead of 1/1000ths of a value you are storing 1/64ths. For example: var pixels Unit = 64 would represent 1 pixel; 96 would be equivalent to 1.5 instead.
The internal representation is compatible with fixed.Int26_6.
func FromFloat64 ¶
Converts a float64 to the closest Unit, rounding away from zero in case of ties. Doesn't account for NaNs, infinites nor overflows. See also FromFloat64Up() and FromFloat64Down().
func FromFloat64Down ¶
Converts a float64 to the closest Unit, rounding down in case of ties. Doesn't account for NaNs, infinites nor overflows.
func FromFloat64Up ¶
Converts a float64 to the closest Unit, rounding up in case of ties. Doesn't account for NaNs, infinites nor overflows.
func FromInt ¶
Fast conversion from int to Unit. If the int value is not representable with a Unit, the result is undefined. If you want to account for overflows, check MinInt <= value <= MaxInt.
func (Unit) Away ¶
Returns the closest whole value in the direction opposite to the refence parameter.
func (Unit) Div ¶
Returns the result of dividing the unit by the given divisor, rounding the unrepresentable decimals away from zero in case of ties.
func (Unit) FractShift ¶
Returns the fractional distance to self.Floor() (the distance to the nearest smaller or equal integer).
This is commonly used for glyph position quantization.
func (Unit) HalfAway ¶
Returns the result of rounding the unit away from the given reference parameter.
func (Unit) HalfToward ¶
Returns the result of rounding the unit towards the given reference parameter.
func (Unit) IsWhole ¶
Returns true if the current value is a whole number, or false if the fractional part is non-zero.
func (Unit) Mul ¶
Returns the result of multiplying the unit by the given value, rounding the unrepresentable decimals away from zero in case of ties.
func (Unit) MulDown ¶
Returns the result of multiplying the unit by the given value, rounding the unrepresentable decimals down in case of ties.
func (Unit) MulUp ¶
Returns the result of multiplying the unit by the given value, rounding the unrepresentable decimals up in case of ties.
func (Unit) QuantizeDown ¶
Given a fractional step between 1 and 64, it quantizes the unit to that fractional value and returns it, favoring the lower value in case of ties.
func (Unit) QuantizeUp ¶
Given a fractional step between 1 and 64, it quantizes the unit to that fractional value and returns it, favoring the higher value in case of ties.
func (Unit) Rescale ¶
Returns the result of rescaling the unit from the 'from' scale to the 'to' scale, rounding the unrepresentable decimals away from zero in case of ties.
Within etxt, this is often used to rescale font metrics between different EM sizes (e.g. an advance of 512 on a font with EM of 1024 units corresponds to an advance of 384 with an EM size of 768, or 512.Rescale(1024, 768) = 384).
Notice that there's an implicit division in this operation; if 'from' is zero, the function will panic.
func (Unit) ToFloat32 ¶
Returns the unit as a float32. The conversion is exact in the +/-16777216 units range. Beyond that range, which corresponds to +/-2^18 (+/-262144) in the decimal numbering system, conversions become progressively less precise.
func (Unit) ToInt ¶
Utility method equivalent to Unit.ToIntHalfAway(0). For the fastest possible conversion to int, check Unit.ToIntFloor() instead.
func (Unit) ToIntAway ¶
Returns the closest int to the unit in the direction opposite to the reference parameter.
func (Unit) ToIntFloor ¶
Returns the unit as a truncated int. This is the fastest Unit to int conversion method.
func (Unit) ToIntHalfAway ¶
Rounds the unit away from the reference value and returns the result as an int.
func (Unit) ToIntHalfDown ¶
Returns the unit as a rounded down int.
func (Unit) ToIntHalfToward ¶
Rounds the unit towards the reference value and returns the result as an int.
func (Unit) ToIntToward ¶
Returns the closest int to the unit in the direction given by the reference parameter.