unit

package
v0.0.0-...-6f71fe9 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package units defines units of length such as inches or pixels.

Functions like Inches and Pixels return a Value in the corresponding unit. For example:

v := unit.Inches(4.5)

represents four and a half inches.

Converting between pixels (px), physical units (dp, pt, in, mm) and font-face-relative measures (em, ex, ch) depends on the context, such as the screen's DPI resolution and the active font face. That context is represented by the Converter type.

Conversions may be lossy. Converting 4.5 inches to pixels and back may result in something slightly different than 4.5. Similarly, converting 4 inches and 0.5 inches to pixels and then adding the results won't necessarily equal the conversion of 4.5 inches to pixels.

Note that what CSS (Cascading Style Sheets) calls "px" differs from what this package calls "px". For legacy reasons, the CSS semantics are that 1 inch should roughly equal 96csspx regardless of the actual DPI resolution, as per https://developer.mozilla.org/en/docs/Web/CSS/length. This package's semantics are that 1px means exactly one physical pixel, always. This package represents 1csspx as 1.666666667dp, since there are 160 density independent pixels per inch, the same definition as Android.

Package units defines units of length such as inches or pixels.

Functions like Inches and Pixels return a Value in the corresponding unit. For example:

v := unit.Inches(4.5)

represents four and a half inches.

Converting between pixels (px), physical units (dp, pt, in, mm) and font-face-relative measures (em, ex, ch) depends on the context, such as the screen's DPI resolution and the active font face. That context is represented by the Converter type.

Conversions may be lossy. Converting 4.5 inches to pixels and back may result in something slightly different than 4.5. Similarly, converting 4 inches and 0.5 inches to pixels and then adding the results won't necessarily equal the conversion of 4.5 inches to pixels.

Note that what CSS (Cascading Style Sheets) calls "px" differs from what this package calls "px". For legacy reasons, the CSS semantics are that 1 inch should roughly equal 96csspx regardless of the actual DPI resolution, as per https://developer.mozilla.org/en/docs/Web/CSS/length. This package's semantics are that 1px means exactly one physical pixel, always. This package represents 1csspx as 1.666666667dp, since there are 160 density independent pixels per inch, the same definition as Android.

Index

Constants

View Source
const (
	DensityIndependentPixelsPerInch = 160
	MillimetresPerInch              = 25.4
	PointsPerInch                   = 72.0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Dimension

type Dimension struct {
	Top    Value
	Bottom Value
	Left   Value
	Right  Value
}

func NewDimension

func NewDimension(t, b, l, r Value) Dimension

func ParseDimension

func ParseDimension(s string) (Dimension, error)

ParseDimension parses a string consisting of 1 to 4 Values that describe a Dimension. When 1 value is specified, it applies to all 4 sides When 2 values are specified, first is for Top and Bottom, second for Left & Right When 3 values, first is top, second left & right, and third bottom When 4 then in this order: top,right,bottom,left

This is based on how css works going clockwise from the top https://developer.mozilla.org/en-US/docs/Web/CSS/margin

func RectangleDimension

func RectangleDimension(t, r Value) Dimension

func SquareDimension

func SquareDimension(v Value) Dimension

func (Dimension) Add

Add adds two dimensions, increasing the values for each side. The result will be of the dimension on the left hand side

func (Dimension) Convert

func (d Dimension) Convert(gc *draw2dimg.GraphicContext, to Unit) Dimension

func (Dimension) ExpandRect

ExpandRect expands a rectangle by the dimension

func (Dimension) IsZero

func (d Dimension) IsZero() bool

func (Dimension) ReduceRect

ReduceRect reduces a rectangle by the dimension

func (Dimension) String

func (d Dimension) String() string

func (Dimension) Sub

Sub subtracts two dimensions, increasing the values for each side. The result will be of the dimension on the left hand side

type Rectangle

type Rectangle struct {
	X1 Value
	Y1 Value
	X2 Value
	Y2 Value
}

func FromRectangle

func FromRectangle(gc *draw2dimg.GraphicContext, r util.Rectangle, unit Unit) Rectangle

FromRectangle takes a plain rectangle and converts it into one with the specified units

func ParseRectangle

func ParseRectangle(s string) (Rectangle, error)

ParseRectangle parses a string consisting of 4 Values that describe a Rectangle

func ParseSizedRectangle

func ParseSizedRectangle(s string) (Rectangle, error)

func (Rectangle) Add

func (Rectangle) Convert

func (d Rectangle) Convert(gc *draw2dimg.GraphicContext, to Unit) Rectangle

func (Rectangle) Expand

func (Rectangle) Height

func (d Rectangle) Height(gc *draw2dimg.GraphicContext) Value

func (Rectangle) IsZero

func (d Rectangle) IsZero() bool

func (Rectangle) Rectangle

func (d Rectangle) Rectangle(gc *draw2dimg.GraphicContext) util.Rectangle

func (Rectangle) Reduce

func (Rectangle) SizeString

func (d Rectangle) SizeString() string

func (Rectangle) String

func (d Rectangle) String() string

func (Rectangle) Width

func (d Rectangle) Width(gc *draw2dimg.GraphicContext) Value

type Unit

type Unit uint8

Unit is a unit of length, such as inches or pixels.

const (
	// Px is a physical pixel, regardless of the DPI resolution.
	Px Unit = iota

	// Dp is 1 density independent pixel: 1/160th of an inch.
	Dp
	// Pt is 1 point: 1/72th of an inch.
	Pt
	// Mm is 1 millimetre: 1/25.4th of an inch.
	Mm
	// In is 1 inch.
	//
	// If the context does not specify a DPI resolution, the recommended
	// fallback value for conversion is 72 pixels per inch.
	In

	// Em is the height of the active font face, disregarding extra leading
	// such as from double-spaced lines of text.
	//
	// If the context does not specify an active font face, the recommended
	// fallback value for conversion is 12pt.
	Em
	// Ex is the x-height of the active font face.
	//
	// If the context does not specify an x-height, the recommended fallback
	// value for conversion is 0.5em.
	Ex
	// Ch is the character width of the numeral zero glyph '0' of the active
	// font face.
	//
	// If the context does not specify a '0' glyph, the recommended fallback
	// value for conversion is 0.5em.
	Ch
)

func (Unit) PixelsPer

func (u Unit) PixelsPer(gc *draw2dimg.GraphicContext) float64

PixelsPer returns the number of pixels in the unit u.

type Value

type Value struct {
	F float64
	U Unit
}

Value is a number and a unit.

func ParseValue

func ParseValue(s string) (Value, error)

func Pixels

func Pixels(f float64) Value

func (Value) Add

func (v Value) Add(gc *draw2dimg.GraphicContext, b Value) Value

Add adds two values. The result will be in the unit of the left hand side.

func (Value) Convert

func (v Value) Convert(gc *draw2dimg.GraphicContext, to Unit) Value

func (Value) Equals

func (v Value) Equals(b Value) bool

func (Value) IsZero

func (v Value) IsZero() bool

func (Value) Max

func (v Value) Max(gc *draw2dimg.GraphicContext, b Value) Value

func (Value) Min

func (v Value) Min(gc *draw2dimg.GraphicContext, b Value) Value

func (Value) Pixels

func (v Value) Pixels(gc *draw2dimg.GraphicContext) float64

Pixels returns the value in pixels as a float64

func (Value) String

func (v Value) String() string

String implements the fmt.Stringer interface.

func (Value) Sub

func (v Value) Sub(gc *draw2dimg.GraphicContext, b Value) Value

Sub subtracts two values. The result will be in the unit of the left hand side.

Jump to

Keyboard shortcuts

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