Documentation ¶
Overview ¶
Package Units supports full range of CSS-style length units (em, px, dp, etc)
The unit is stored along with a value, and can be converted at a later point into a raw display pixel value using the Context which contains all the necessary reference values to perform the conversion. Typically the unit value is parsed early from a style and then converted later once the context is fully resolved. The Value also holds the converted value (Dots) so it can be used directly without further re-conversion.
'Dots' are used as term for underlying raw display pixels because "Pixel" and the px unit are actually not conventionally used as raw display pixels in the current HiDPI environment. See https://developer.mozilla.org/en/docs/Web/CSS/length: 1 px = 1/96 in Also supporting dp = density-independent pixel = 1/160 in
Index ¶
- Constants
- type Context
- func (uc *Context) Ch(val float32) float32
- func (uc *Context) Cm(val float32) float32
- func (uc *Context) Defaults()
- func (uc *Context) Dots(un Units) float32
- func (uc *Context) DotsToPx(val float32) float32
- func (uc *Context) Dp(val float32) float32
- func (uc *Context) Eh(val float32) float32
- func (uc *Context) Em(val float32) float32
- func (uc *Context) Ew(val float32) float32
- func (uc *Context) Ex(val float32) float32
- func (uc *Context) In(val float32) float32
- func (uc *Context) Mm(val float32) float32
- func (uc *Context) Pc(val float32) float32
- func (uc *Context) Ph(val float32) float32
- func (uc *Context) Pt(val float32) float32
- func (uc *Context) Pw(val float32) float32
- func (uc *Context) Px(val float32) float32
- func (uc *Context) PxToDots(val float32) float32
- func (uc *Context) Q(val float32) float32
- func (uc *Context) Rem(val float32) float32
- func (uc *Context) SetFont(em, ex, ch, rem float32)
- func (uc *Context) SetSizes(vw, vh, ew, eh, pw, ph float32) bool
- func (uc *Context) String() string
- func (uc *Context) ToDots(val float32, un Units) float32
- func (uc *Context) Vh(val float32) float32
- func (uc *Context) Vmax(val float32) float32
- func (uc *Context) Vmin(val float32) float32
- func (uc *Context) Vw(val float32) float32
- type Units
- func (i Units) Desc() string
- func (i Units) Int64() int64
- func (i Units) IsValid() bool
- func (i Units) MarshalText() ([]byte, error)
- func (i *Units) SetInt64(in int64)
- func (i *Units) SetString(s string) error
- func (i Units) String() string
- func (i *Units) UnmarshalText(text []byte) error
- func (i Units) Values() []enums.Enum
- type Value
- func Ch(val float32) Value
- func Cm(val float32) Value
- func Custom(fun func(uc *Context) float32) Value
- func Dot(val float32) Value
- func Dp(val float32) Value
- func Eh(val float32) Value
- func Em(val float32) Value
- func Ew(val float32) Value
- func Ex(val float32) Value
- func In(val float32) Value
- func Mm(val float32) Value
- func New(val float32, un Units) Value
- func Pc(val float32) Value
- func Ph(val float32) Value
- func Pt(val float32) Value
- func Pw(val float32) Value
- func Px(val float32) Value
- func Q(val float32) Value
- func Rem(val float32) Value
- func StringToValue(str string) Value
- func Vh(val float32) Value
- func Vmax(val float32) Value
- func Vmin(val float32) Value
- func Vw(val float32) Value
- func Zero() Value
- func (v *Value) Ch(val float32)
- func (v *Value) Cm(val float32)
- func (v *Value) Convert(to Units, uc *Context) Value
- func (v *Value) Dot(val float32)
- func (v *Value) Dp(val float32)
- func (v *Value) Eh(val float32)
- func (v *Value) Em(val float32)
- func (v *Value) Ew(val float32)
- func (v *Value) Ex(val float32)
- func (v *Value) In(val float32)
- func (v *Value) Mm(val float32)
- func (v *Value) Pc(val float32)
- func (v *Value) Ph(val float32)
- func (v *Value) Pt(val float32)
- func (v *Value) Pw(val float32)
- func (v *Value) Px(val float32)
- func (v *Value) Q(val float32)
- func (v *Value) Rem(val float32)
- func (v *Value) Set(val float32, un Units)
- func (v *Value) SetAny(iface any, key string) error
- func (v *Value) SetCustom(fun func(uc *Context) float32)
- func (v *Value) SetString(str string) error
- func (v *Value) String() string
- func (v *Value) ToDots(uc *Context) float32
- func (v *Value) ToDotsFixed(uc *Context) fixed.Int26_6
- func (v *Value) Vh(val float32)
- func (v *Value) Vmax(val float32)
- func (v *Value) Vmin(val float32)
- func (v *Value) Vw(val float32)
- func (v *Value) Zero()
- type XY
Constants ¶
const ( PxPerInch = 96 DpPerInch = 160 MmPerInch = 25.4 CmPerInch = 2.54 PtPerInch = 72 PcPerInch = 6 )
Standard conversion factors. Px is a logical, DPI-independent pixel instead of an actual "dot" raw pixel.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { // DPI is dots-per-inch of the display DPI float32 // FontEm is the size of the font of the element in raw dots (not points) FontEm float32 // FontEx is the height x-height of font in points (size of 'x' glyph) FontEx float32 // FontCh is the ch-size character size of font in points (width of '0' glyph) FontCh float32 // FontRem is the size of the font of the root element in raw dots (not points) FontRem float32 // Vpw is viewport width in dots Vpw float32 // Vph is viewport height in dots Vph float32 // Elw is width of element in dots Elw float32 // Elh is height of element in dots Elh float32 // Paw is width of parent in dots Paw float32 // Pah is height of parent in dots Pah float32 }
Context specifies everything about the current context necessary for converting the number into specific display-dependent pixels
func (*Context) Ch ¶
Ch converts the given ch value to dots. Ch is width of the '0' glyph in the element's font. It falls back to a default of 0.5em.
func (*Context) Dots ¶
ToDotsFact returns factor needed to convert given unit into raw pixels (dots in DPI)
func (*Context) Dp ¶
Dp converts the given dp value to dots. Dp is density-independent pixels. 1dp is 1/160 in.
func (*Context) Eh ¶
Eh converts the given eh value to dots. Eh is percentage of element height, which is equivalent to CSS % in some contexts.
func (*Context) Ew ¶
Ew converts the given ew value to dots. Ew is percentage of element width, which is equivalent to CSS % in some contexts.
func (*Context) Ex ¶
Ex converts the given ex value to dots. Ex is x-height of the element's font (size of 'x' glyph). It falls back to a default of 0.5em.
func (*Context) Ph ¶
Ph converts the given ph value to dots. Ph is percentage of parent height, which is equivalent to CSS % in some contexts.
func (*Context) Pw ¶
Pw converts the given pw value to dots. Pw is percentage of parent width, which is equivalent to CSS % in some contexts.
func (*Context) Px ¶
Px converts the given px value to dots. Px is logical pixels. 1px is 1/96 in. These are not raw display pixels, for which you should use dots.
func (*Context) Rem ¶
Rem converts the given rem value to dots. Rem is the font size of the root element, which is always 16dp.
func (*Context) SetFont ¶
SetFont sets the context values for fonts: note these are already in raw DPI dots, not points or anything else
func (*Context) SetSizes ¶
SetSizes sets the context values for the non-font sizes to the given values; the values are ignored if they are zero. returns true if any are different.
func (*Context) Vh ¶
Vh converts the given vh value to dots. Vh is percentage of viewport (Scene) height.
func (*Context) Vmax ¶
Vmax converts the given vmax value to dots. Vmax is percentage of the larger dimension of the viewport (Scene).
type Units ¶
type Units int32 //enums:enum -trim-prefix Unit -transform lower
Units is an enum that represents a unit (px, em, etc)
const ( // UnitDp represents density-independent pixels. 1dp is 1/160 in. UnitDp Units = iota // UnitPx represents logical pixels. 1px is 1/96 in. // These are not raw display pixels, for which you should use dots. UnitPx // UnitEw represents percentage of element width, which is equivalent to CSS % in some contexts. UnitEw // UnitEh represents percentage of element height, which is equivalent to CSS % in some contexts. UnitEh // UnitPw represents percentage of parent width, which is equivalent to CSS % in some contexts. UnitPw // UnitPh represents percentage of parent height, which is equivalent to CSS % in some contexts. UnitPh // UnitRem represents the font size of the root element, which is always 16dp. UnitRem // UnitEm represents the font size of the element. UnitEm // UnitEx represents x-height of the element's font (size of 'x' glyph). // It falls back to a default of 0.5em. UnitEx // UnitCh represents width of the '0' glyph in the element's font. // It falls back to a default of 0.5em. UnitCh // UnitVw represents percentage of viewport (Scene) width. UnitVw // UnitVh represents percentage of viewport (Scene) height. UnitVh // UnitVmin represents percentage of the smaller dimension of the viewport (Scene). UnitVmin // UnitVmax represents percentage of the larger dimension of the viewport (Scene). UnitVmax // UnitCm represents centimeters. 1cm is 1/2.54 in. UnitCm // UnitMm represents millimeters. 1mm is 1/10 cm. UnitMm // UnitQ represents quarter-millimeters. 1q is 1/40 cm. UnitQ // UnitIn represents inches. 1in is 2.54cm or 96px. UnitIn // UnitPc represents picas. 1pc is 1/6 in. UnitPc // UnitPt represents points. 1pt is 1/72 in. UnitPt // UnitDot represents real display pixels. They are generally only used internally. UnitDot )
const UnitsN Units = 21
UnitsN is the highest valid value for type Units, plus one.
func UnitsValues ¶
func UnitsValues() []Units
UnitsValues returns all possible values for the type Units.
func (Units) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Units) SetString ¶
SetString sets the Units value from its string representation, and returns an error if the string is invalid.
func (*Units) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Value ¶
type Value struct { // the value in terms of the specified unit Val float32 `label:""` // the unit used for the value Un Units `label:""` // the computed value in raw pixels (dots in DPI) Dots float32 `view:"-"` // custom function that returns the dots of the value; // if non-nil, it overrides all other fields; // if nil, standard ToDots with the other fields is used Custom func(uc *Context) float32 `view:"-" json:"-" xml:"-"` }
Value and units, and converted value into raw pixels (dots in DPI)
func Ch ¶
Ch returns a new ch value. Ch is width of the '0' glyph in the element's font. It falls back to a default of 0.5em.
func Custom ¶
Custom returns a new custom value that has the dots of the value returned by the given function.
func Dot ¶
Dot returns a new dots value. Dots are actual real display pixels, which are generally only used internally.
func Eh ¶
Eh returns a new eh value. Eh is percentage of element height, which is equivalent to CSS % in some contexts.
func Ew ¶
Ew returns a new ew value. Ew is percentage of element width, which is equivalent to CSS % in some contexts.
func Ex ¶
Ex returns a new ex value. Ex is x-height of the element's font (size of 'x' glyph). It falls back to a default of 0.5em.
func Ph ¶
Ph returns a new ph value. Ph is percentage of parent height, which is equivalent to CSS % in some contexts.
func Pw ¶
Pw returns a new pw value. Pw is percentage of parent width, which is equivalent to CSS % in some contexts.
func Px ¶
Px returns a new px value. Px is logical pixels. 1px is 1/96 in. These are not raw display pixels, for which you should use dots.
func Rem ¶
Rem returns a new rem value. Rem is the font size of the root element, which is always 16dp.
func StringToValue ¶
StringToValue converts a string to a value representation.
func Vmax ¶
Vmax returns a new vmax value. Vmax is percentage of the larger dimension of the viewport (Scene).
func Vmin ¶
Vmin returns a new vmin value. Vmin is percentage of the smaller dimension of the viewport (Scene).
func (*Value) Ch ¶
Ch sets the value in terms of ch. Ch is width of the '0' glyph in the element's font. It falls back to a default of 0.5em.
func (*Value) Dot ¶
Dot sets the value directly in terms of dots. Dots are actual real display pixels, which are generally only used internally.
func (*Value) Dp ¶
Dp sets the value in terms of dp. Dp is density-independent pixels. 1dp is 1/160 in.
func (*Value) Eh ¶
Eh sets the value in terms of eh. Eh is percentage of element height, which is equivalent to CSS % in some contexts.
func (*Value) Ew ¶
Ew sets the value in terms of ew. Ew is percentage of element width, which is equivalent to CSS % in some contexts.
func (*Value) Ex ¶
Ex sets the value in terms of ex. Ex is x-height of the element's font (size of 'x' glyph). It falls back to a default of 0.5em.
func (*Value) Ph ¶
Ph sets the value in terms of ph. Ph is percentage of parent height, which is equivalent to CSS % in some contexts.
func (*Value) Pw ¶
Pw sets the value in terms of pw. Pw is percentage of parent width, which is equivalent to CSS % in some contexts.
func (*Value) Px ¶
Px sets the value in terms of px. Px is logical pixels. 1px is 1/96 in. These are not raw display pixels, for which you should use dots.
func (*Value) Rem ¶
Rem sets the value in terms of rem. Rem is the font size of the root element, which is always 16dp.
func (*Value) SetAny ¶ added in v0.0.5
SetAny sets value from an interface value representation as from map[string]any key is optional property key for error message -- always logs the error
func (*Value) SetCustom ¶
SetCustom sets the value to be a custom value that has the dots of the value returned by the given function.
func (*Value) ToDots ¶
ToDots converts value to raw display pixels (dots as in DPI), setting also the Dots field
func (*Value) ToDotsFixed ¶
ToDotsFixed converts value to raw display pixels (dots in DPI) in fixed-point 26.6 format for rendering
func (*Value) Vmax ¶
Vmax sets the value in terms of vmax. Vmax is percentage of the larger dimension of the viewport (Scene).
func (*Value) Vmin ¶
Vmin sets the value in terms of vmin. Vmin is percentage of the smaller dimension of the viewport (Scene).
type XY ¶
type XY struct { // X is the horizontal axis value X Value // Y is the vertical axis value Y Value }
XY represents unit Value for X and Y dimensions
func (*XY) Set ¶
Set sets the X, Y values according to the given values. no values: set to 0. 1 value: set both to that value. 2 values, set X, Y to the two values respectively.