units

package
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: BSD-3-Clause Imports: 11 Imported by: 19

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

View Source
const (
	PxPerInch = 96.0
	DpPerInch = 160.0
	MmPerInch = 25.4
	CmPerInch = 2.54
	PtPerInch = 72.0
	PcPerInch = 6.0
)

standard conversion factors -- Px = DPI-independent pixel instead of 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 added in v0.0.14

func (uc *Context) Ch(val float32) float32

Ch converts the given ch value to dots. Ch is width of the '0' glyph in the element's font -- fallback to 0.5em by default.

func (*Context) Cm added in v0.0.14

func (uc *Context) Cm(val float32) float32

Cm converts the given cm value to dots. Cm is centimeters -- 1cm = 96px/2.54.

func (*Context) Defaults

func (uc *Context) Defaults()

Defaults are generic defaults

func (*Context) Dots

func (uc *Context) Dots(un Units) float32

ToDotsFact returns factor needed to convert given unit into raw pixels (dots in DPI)

func (*Context) DotsToPx

func (uc *Context) DotsToPx(val float32) float32

DotsToPx just converts a value from dots to pixels

func (*Context) Dp added in v0.0.14

func (uc *Context) Dp(val float32) float32

Dp converts the given dp value to dots. Dp is density-independent pixels -- 1dp = 1/160th of 1in.

func (*Context) Eh

func (uc *Context) Eh(val float32) float32

Eh converts the given eh value to dots. Eh is percentage of element height (equivalent to CSS % in some contexts).

func (*Context) Em added in v0.0.14

func (uc *Context) Em(val float32) float32

Em converts the given em value to dots. Em is font size of the element -- fallback to 12pt by default.

func (*Context) Ew

func (uc *Context) Ew(val float32) float32

Ew converts the given ew value to dots. Ew is percentage of element width (equivalent to CSS % in some contexts).

func (*Context) Ex added in v0.0.14

func (uc *Context) Ex(val float32) float32

Ex converts the given ex value to dots. Ex is x-height of the element's font (size of 'x' glyph) -- fallback to 0.5em by default.

func (*Context) In added in v0.0.14

func (uc *Context) In(val float32) float32

In converts the given in value to dots. In is inches -- 1in = 2.54cm = 96px.

func (*Context) Mm added in v0.0.14

func (uc *Context) Mm(val float32) float32

Mm converts the given mm value to dots. Mm is millimeters -- 1mm = 1/10th of cm.

func (*Context) Pc added in v0.0.14

func (uc *Context) Pc(val float32) float32

Pc converts the given pc value to dots. Pc is picas -- 1pc = 1/6th of 1in.

func (*Context) Ph

func (uc *Context) Ph(val float32) float32

Ph converts the given ph value to dots. Ph is percentage of parent height (equivalent to CSS % in some contexts).

func (*Context) Pt added in v0.0.14

func (uc *Context) Pt(val float32) float32

Pt converts the given pt value to dots. Pt is points -- 1pt = 1/72th of 1in.

func (*Context) Pw

func (uc *Context) Pw(val float32) float32

Pw converts the given pw value to dots. Pw is percentage of parent width (equivalent to CSS % in some contexts).

func (*Context) Px added in v0.0.14

func (uc *Context) Px(val float32) float32

Px converts the given px value to dots. Px is pixels -- 1px = 1/96th of 1in -- these are NOT raw display pixels.

func (*Context) PxToDots

func (uc *Context) PxToDots(val float32) float32

PxToDots just converts a value from pixels to dots

func (*Context) Q added in v0.0.14

func (uc *Context) Q(val float32) float32

Q converts the given q value to dots. Q is quarter-millimeters -- 1q = 1/40th of cm.

func (*Context) Rem added in v0.0.14

func (uc *Context) Rem(val float32) float32

Rem converts the given rem value to dots. Rem is font size of the root element -- defaults to 12pt scaled by DPI factor.

func (*Context) Set

func (uc *Context) Set(em, ex, ch, rem, vw, vh, ew, eh, pw, ph float32)

Set sets the context values to the given values

func (*Context) SetFont

func (uc *Context) SetFont(em, ex, ch, rem float32)

SetFont sets the context values for fonts: note these are already in raw DPI dots, not points or anything else

func (*Context) SetSizes

func (uc *Context) SetSizes(vw, vh, ew, eh, pw, ph float32) bool

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) String added in v0.0.19

func (uc *Context) String() string

func (*Context) ToDots

func (uc *Context) ToDots(val float32, un Units) float32

ToDots converts value in given units into raw display pixels (dots in DPI)

func (*Context) Vh

func (uc *Context) Vh(val float32) float32

Vh converts the given vh value to dots. Vh is 1% of the viewport's height.

func (*Context) Vmax added in v0.0.14

func (uc *Context) Vmax(val float32) float32

Vmax converts the given vmax value to dots. Vmax is 1% of the viewport's larger dimension.

func (*Context) Vmin added in v0.0.14

func (uc *Context) Vmin(val float32) float32

Vmin converts the given vmin value to dots. Vmin is 1% of the viewport's smaller dimension.

func (*Context) Vw

func (uc *Context) Vw(val float32) float32

Vw converts the given vw value to dots. Vw is 1% of the viewport's width.

type Units

type Units int32 //enums:enum -trim-prefix Unit -transform lower

Units is an enum that represents a unit (px, em, etc)

const (
	// UnitPx = pixels -- 1px = 1/96th of 1in -- these are NOT raw display pixels
	UnitPx Units = iota

	// UnitDp = density-independent pixels -- 1dp = 1/160th of 1in
	UnitDp

	// UnitEw = percentage of element width (equivalent to CSS % in some contexts)
	UnitEw

	// UnitEh = percentage of element height (equivalent to CSS % in some contexts)
	UnitEh

	// UnitPw = percentage of parent width (equivalent to CSS % in some contexts)
	UnitPw

	// UnitPh = percentage of parent height (equivalent to CSS % in some contexts)
	UnitPh

	// UnitRem = font size of the root element -- defaults to 12pt scaled by DPI factor
	UnitRem

	// UnitEm = font size of the element -- fallback to 12pt by default
	UnitEm

	// UnitEx = x-height of the element's font (size of 'x' glyph) -- fallback to 0.5em by default
	UnitEx

	// UnitCh = width of the '0' glyph in the element's font -- fallback to 0.5em by default
	UnitCh

	// UnitVw = 1% of the viewport's width
	UnitVw

	// UnitVh = 1% of the viewport's height
	UnitVh

	// UnitVmin = 1% of the viewport's smaller dimension
	UnitVmin

	// UnitVmax = 1% of the viewport's larger dimension
	UnitVmax

	// UnitCm = centimeters -- 1cm = 96px/2.54
	UnitCm

	// UnitMm = millimeters -- 1mm = 1/10th of cm
	UnitMm

	// UnitQ = quarter-millimeters -- 1q = 1/40th of cm
	UnitQ

	// UnitIn = inches -- 1in = 2.54cm = 96px
	UnitIn

	// UnitPc = picas -- 1pc = 1/6th of 1in
	UnitPc

	// UnitPt = points -- 1pt = 1/72th of 1in
	UnitPt

	// UnitDot = actual real display pixels, which 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) Desc

func (i Units) Desc() string

Desc returns the description of the Units value.

func (Units) Int64

func (i Units) Int64() int64

Int64 returns the Units value as an int64.

func (Units) IsValid

func (i Units) IsValid() bool

IsValid returns whether the value is a valid option for type Units.

func (Units) MarshalText

func (i Units) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Units) SetInt64

func (i *Units) SetInt64(in int64)

SetInt64 sets the Units value from an int64.

func (*Units) SetString

func (i *Units) SetString(s string) error

SetString sets the Units value from its string representation, and returns an error if the string is invalid.

func (Units) String

func (i Units) String() string

String returns the string representation of this Units value.

func (*Units) UnmarshalText

func (i *Units) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Units) Values

func (i Units) Values() []enums.Enum

Values returns all possible values for the type Units.

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:"-"`
}

Value and units, and converted value into raw pixels (dots in DPI)

func Ch

func Ch(val float32) Value

Ch returns a new ch value. Ch is width of the '0' glyph in the element's font -- fallback to 0.5em by default.

func Cm

func Cm(val float32) Value

Cm returns a new cm value. Cm is centimeters -- 1cm = 96px/2.54.

func Custom added in v0.0.14

func Custom(fun func(uc *Context) float32) Value

Custom returns a new custom value that has the dots of the value returned by the given function.

func Dot

func Dot(val float32) Value

Dot returns a new dots value. Dots are actual real display pixels, which are generally only used internally.

func Dp

func Dp(val float32) Value

Dp returns a new dp value. Dp is density-independent pixels -- 1dp = 1/160th of 1in.

func Eh

func Eh(val float32) Value

Eh returns a new eh value. Eh is percentage of element height (equivalent to CSS % in some contexts).

func Em

func Em(val float32) Value

Em returns a new em value. Em is font size of the element -- fallback to 12pt by default.

func Ew

func Ew(val float32) Value

Ew returns a new ew value. Ew is percentage of element width (equivalent to CSS % in some contexts).

func Ex

func Ex(val float32) Value

Ex returns a new ex value. Ex is x-height of the element's font (size of 'x' glyph) -- fallback to 0.5em by default.

func In

func In(val float32) Value

In returns a new in value. In is inches -- 1in = 2.54cm = 96px.

func Mm

func Mm(val float32) Value

Mm returns a new mm value. Mm is millimeters -- 1mm = 1/10th of cm.

func New

func New(val float32, un Units) Value

New creates a new value with the given unit type

func Pc

func Pc(val float32) Value

Pc returns a new pc value. Pc is picas -- 1pc = 1/6th of 1in.

func Ph

func Ph(val float32) Value

Ph returns a new ph value. Ph is percentage of parent height (equivalent to CSS % in some contexts).

func Pt

func Pt(val float32) Value

Pt returns a new pt value. Pt is points -- 1pt = 1/72th of 1in.

func Pw

func Pw(val float32) Value

Pw returns a new pw value. Pw is percentage of parent width (equivalent to CSS % in some contexts).

func Px

func Px(val float32) Value

Px returns a new px value. Px is pixels -- 1px = 1/96th of 1in -- these are NOT raw display pixels.

func Q

func Q(val float32) Value

Q returns a new q value. Q is quarter-millimeters -- 1q = 1/40th of cm.

func Rem

func Rem(val float32) Value

Rem returns a new rem value. Rem is font size of the root element -- defaults to 12pt scaled by DPI factor.

func StringToValue

func StringToValue(str string) Value

StringToValue converts a string to a value representation.

func Vh

func Vh(val float32) Value

Vh returns a new vh value. Vh is 1% of the viewport's height.

func Vmax

func Vmax(val float32) Value

Vmax returns a new vmax value. Vmax is 1% of the viewport's larger dimension.

func Vmin

func Vmin(val float32) Value

Vmin returns a new vmin value. Vmin is 1% of the viewport's smaller dimension.

func Vw

func Vw(val float32) Value

Vw returns a new vw value. Vw is 1% of the viewport's width.

func Zero added in v0.0.18

func Zero() Value

Zero returns a new zero (0) value.

func (*Value) Ch added in v0.0.14

func (v *Value) Ch(val float32)

Ch sets the value in terms of ch. Ch is width of the '0' glyph in the element's font -- fallback to 0.5em by default.

func (*Value) Cm added in v0.0.14

func (v *Value) Cm(val float32)

Cm sets the value in terms of cm. Cm is centimeters -- 1cm = 96px/2.54.

func (*Value) Convert

func (v *Value) Convert(to Units, uc *Context) Value

Convert converts value to the given units, given unit context

func (*Value) Dot added in v0.0.19

func (v *Value) Dot(val float32)

Dot sets the value directly in terms of dots. Dots are actual real display pixels, which are generally only used internally.

func (*Value) Dp added in v0.0.14

func (v *Value) Dp(val float32)

Dp sets the value in terms of dp. Dp is density-independent pixels -- 1dp = 1/160th of 1in.

func (*Value) Eh added in v0.0.14

func (v *Value) Eh(val float32)

Eh sets the value in terms of eh. Eh is percentage of element height (equivalent to CSS % in some contexts).

func (*Value) Em added in v0.0.14

func (v *Value) Em(val float32)

Em sets the value in terms of em. Em is font size of the element -- fallback to 12pt by default.

func (*Value) Ew added in v0.0.14

func (v *Value) Ew(val float32)

Ew sets the value in terms of ew. Ew is percentage of element width (equivalent to CSS % in some contexts).

func (*Value) Ex added in v0.0.14

func (v *Value) Ex(val float32)

Ex sets the value in terms of ex. Ex is x-height of the element's font (size of 'x' glyph) -- fallback to 0.5em by default.

func (*Value) In added in v0.0.14

func (v *Value) In(val float32)

In sets the value in terms of in. In is inches -- 1in = 2.54cm = 96px.

func (*Value) Mm added in v0.0.14

func (v *Value) Mm(val float32)

Mm sets the value in terms of mm. Mm is millimeters -- 1mm = 1/10th of cm.

func (*Value) Pc added in v0.0.14

func (v *Value) Pc(val float32)

Pc sets the value in terms of pc. Pc is picas -- 1pc = 1/6th of 1in.

func (*Value) Ph added in v0.0.14

func (v *Value) Ph(val float32)

Ph sets the value in terms of ph. Ph is percentage of parent height (equivalent to CSS % in some contexts).

func (*Value) Pt added in v0.0.14

func (v *Value) Pt(val float32)

Pt sets the value in terms of pt. Pt is points -- 1pt = 1/72th of 1in.

func (*Value) Pw added in v0.0.14

func (v *Value) Pw(val float32)

Pw sets the value in terms of pw. Pw is percentage of parent width (equivalent to CSS % in some contexts).

func (*Value) Px added in v0.0.14

func (v *Value) Px(val float32)

Px sets the value in terms of px. Px is pixels -- 1px = 1/96th of 1in -- these are NOT raw display pixels.

func (*Value) Q added in v0.0.14

func (v *Value) Q(val float32)

Q sets the value in terms of q. Q is quarter-millimeters -- 1q = 1/40th of cm.

func (*Value) Rem added in v0.0.14

func (v *Value) Rem(val float32)

Rem sets the value in terms of rem. Rem is font size of the root element -- defaults to 12pt scaled by DPI factor.

func (*Value) Set

func (v *Value) Set(val float32, un Units)

Set sets the value and units of an existing value

func (*Value) SetCustom added in v0.0.14

func (v *Value) SetCustom(fun func(uc *Context) float32)

SetCustom sets the value to be a custom value that has the dots of the value returned by the given function.

func (*Value) SetIFace

func (v *Value) SetIFace(iface any, key string) error

SetIFace 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) SetString

func (v *Value) SetString(str string) error

SetString sets value from a string

func (*Value) String

func (v *Value) String() string

String implements the fmt.Stringer interface.

func (*Value) ToDots

func (v *Value) ToDots(uc *Context) float32

ToDots converts value to raw display pixels (dots as in DPI), setting also the Dots field

func (*Value) ToDotsFixed

func (v *Value) ToDotsFixed(uc *Context) fixed.Int26_6

ToDotsFixed converts value to raw display pixels (dots in DPI) in fixed-point 26.6 format for rendering

func (*Value) Vh added in v0.0.14

func (v *Value) Vh(val float32)

Vh sets the value in terms of vh. Vh is 1% of the viewport's height.

func (*Value) Vmax added in v0.0.14

func (v *Value) Vmax(val float32)

Vmax sets the value in terms of vmax. Vmax is 1% of the viewport's larger dimension.

func (*Value) Vmin added in v0.0.14

func (v *Value) Vmin(val float32)

Vmin sets the value in terms of vmin. Vmin is 1% of the viewport's smaller dimension.

func (*Value) Vw added in v0.0.14

func (v *Value) Vw(val float32)

Vw sets the value in terms of vw. Vw is 1% of the viewport's width.

func (*Value) Zero added in v0.0.18

func (v *Value) Zero()

Zero sets the value to zero (0).

type XY added in v0.0.19

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) Dim added in v0.0.19

func (xy *XY) Dim(d mat32.Dims) Value

Dim returns the value for given dimension

func (*XY) Dots added in v0.0.19

func (xy *XY) Dots() mat32.Vec2

Dots returns the dots values as a mat32.Vec2 vector

func (*XY) Set added in v0.0.19

func (xy *XY) Set(v ...Value)

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.

func (*XY) SetDim added in v0.0.19

func (xy *XY) SetDim(d mat32.Dims, val Value)

SetDim sets the value for given dimension

func (*XY) String added in v0.0.19

func (xy *XY) String() string

String implements the fmt.Stringer interface.

func (*XY) ToDots added in v0.0.19

func (xy *XY) ToDots(uc *Context)

ToDots converts value to raw display pixels (dots as in DPI), setting also the Dots field

func (*XY) Zero added in v0.0.19

func (xy *XY) Zero()

Zero sets values to 0

Jump to

Keyboard shortcuts

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