units

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: BSD-3-Clause Imports: 10 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 point size of the font 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 rem-size of font in points -- root Em size -- typically 12 point
	FontRem float32

	// Vw is viewport width in dots
	Vw float32

	// Vh is viewport height in dots
	Vh float32

	// Ew is width of element in dots
	Ew float32

	// Eh is height of element in dots
	Eh float32

	// Pw is width of parent in dots
	Pw float32

	// Ph is height of parent in dots
	Ph float32
}

Context specifies everything about the current context necessary for converting the number into specific display-dependent pixels

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) PxToDots

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

PxToDots just converts a value from pixels to dots

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)

SetSizes sets the context values for the non-font sizes to the given values; the values are ignored if they are zero.

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)

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 -- generally only use 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:"-"`

	// function to compute dots from units, using arbitrary expressions; if nil, standard ToDots is used
	DotsFunc 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 creates a new value of type UnitCh (width of '0' in the font of the element)

func Cm

func Cm(val float32) Value

Cm creates a new value of type UnitCm (centimeters; 1cm = 96px/2.54)

func Dot

func Dot(val float32) Value

Dot creates a new value of type UnitDot (actual raw display pixels)

func Dp

func Dp(val float32) Value

Dp creates a new value of type UnitDp (density-independent pixels; 1dp = 1/160 of 1in)

func Eh

func Eh(val float32) Value

Eh creates a new value of type UnitEh (percentage of element height)

func Em

func Em(val float32) Value

Em creates a new value of type UnitEm (font size of the element)

func Ew

func Ew(val float32) Value

Ew creates a new value of type UnitEw (percentage of element width)

func Ex

func Ex(val float32) Value

Ex creates a new value of type UnitEx (height of 'x' in the font of the element)

func In

func In(val float32) Value

In creates a new value of type UnitIn (inches; 1in = 96px)

func Mm

func Mm(val float32) Value

Mm creates a new value of type UnitMm (millimeters; 1mm = 1/10 of 1cm)

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 creates a new value of type UnitPc (picas; 1pc = 1/6 of 1in)

func Ph

func Ph(val float32) Value

Ph creates a new value of type UnitPh (percentage of parent height)

func Pt

func Pt(val float32) Value

Pt creates a new value of type UnitPt (points; 1pt = 1/72 of 1in)

func Pw

func Pw(val float32) Value

Pw creates a new value of type UnitPw (percentage of parent width)

func Px

func Px(val float32) Value

Px creates a new value of type UnitPx (1px = 1/96 of 1in; not raw display pixels)

func Q

func Q(val float32) Value

Q creates a new value of type UnitQ (quarter-millimeters; 1q = 1/40 of 1cm)

func Rem

func Rem(val float32) Value

Rem creates a new value of type UnitRem (font size of the root element)

func StringToValue

func StringToValue(str string) Value

StringToValue converts a string to a value representation.

func Vh

func Vh(val float32) Value

Vh creates a new value of type UnitVh (percentage of viewport height)

func Vmax

func Vmax(val float32) Value

Vmax creates a new value of type UnitVmax (percentage of viewport's bigger dimension)

func Vmin

func Vmin(val float32) Value

Vmin creates a new value of type UnitVmin (percentage of viewport's smaller dimension)

func Vw

func Vw(val float32) Value

Vw creates a new value of type UnitVw (percentage of viewport width)

func (*Value) Convert

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

Convert converts value to the given units, given unit context

func (*Value) Set

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

Set sets the value and units of an existing value

func (*Value) SetCh

func (v *Value) SetCh(val float32)

SetCh sets the value in terms of UnitCh (width of '0' in the font of the element)

func (*Value) SetCm

func (v *Value) SetCm(val float32)

SetCm sets the value in terms of UnitCm (centimeters; 1cm = 96px/2.54)

func (*Value) SetDot

func (v *Value) SetDot(val float32)

SetDot sets the value in terms of [UnitDots] directly (actual raw display pixels)

func (*Value) SetDp

func (v *Value) SetDp(val float32)

SetDp sets the value in terms of UnitDp (density-independent pixels; 1dp = 1/160 of 1in)

func (*Value) SetEh

func (v *Value) SetEh(val float32)

SetEh sets the value in terms of UnitEh (percentage of element height)

func (*Value) SetEm

func (v *Value) SetEm(val float32)

SetEm sets the value in terms of UnitEm (font size of the element)

func (*Value) SetEw

func (v *Value) SetEw(val float32)

SetEw sets the value in terms of UnitEw (percentage of element width)

func (*Value) SetEx

func (v *Value) SetEx(val float32)

SetEx sets the value in terms of UnitEx (height of 'x' in the font of the element)

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) SetIn

func (v *Value) SetIn(val float32)

SetIn sets the value in terms of UnitIn (inches; 1in = 96px)

func (*Value) SetMm

func (v *Value) SetMm(val float32)

SetMm sets the value in terms of UnitMm (millimeters; 1mm = 1/10 of 1cm)

func (*Value) SetPc

func (v *Value) SetPc(val float32)

SetPc sets the value in terms of UnitPc (picas; 1pc = 1/6 of 1in)

func (*Value) SetPh

func (v *Value) SetPh(val float32)

SetPh sets the value in terms of UnitPh (percentage of parent height)

func (*Value) SetPt

func (v *Value) SetPt(val float32)

SetPt sets the value in terms of UnitPt (points; 1pt = 1/72 of 1in)

func (*Value) SetPw

func (v *Value) SetPw(val float32)

SetPw sets the value in terms of UnitPw (percentage of parent width)

func (*Value) SetPx

func (v *Value) SetPx(val float32)

SetPx sets the value in terms of UnitPx (1px = 1/96 of 1in; not raw display pixels)

func (*Value) SetQ

func (v *Value) SetQ(val float32)

SetQ sets the value in terms of UnitQ (quarter-millimeters; 1q = 1/40 of 1cm)

func (*Value) SetRem

func (v *Value) SetRem(val float32)

SetRem sets the value in terms of UnitRem (font size of the root element)

func (*Value) SetString

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

SetString sets value from a string

func (*Value) SetVh

func (v *Value) SetVh(val float32)

SetVh sets the value in terms of UnitVh (percentage of viewport height)

func (*Value) SetVmax

func (v *Value) SetVmax(val float32)

SetVmax sets the value in terms of UnitVmax (percentage of viewport's bigger dimension)

func (*Value) SetVmin

func (v *Value) SetVmin(val float32)

SetVmin sets the value in terms of UnitVmin (percentage of viewport's smaller dimension)

func (*Value) SetVw

func (v *Value) SetVw(val float32)

SetVw sets the value in terms of UnitVw (percentage of viewport width)

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

Jump to

Keyboard shortcuts

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