| 1 | // Copyright 2016 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package unit defines units of length such as inches or pixels. |
| 6 | // |
| 7 | // Functions like Inches and Pixels return a Value in the corresponding unit. |
| 8 | // For example: |
| 9 | // |
| 10 | // v := unit.Inches(4.5) |
| 11 | // |
| 12 | // represents four and a half inches. |
| 13 | // |
| 14 | // Converting between pixels (px), physical units (dp, pt, in, mm) and |
| 15 | // font-face-relative measures (em, ex, ch) depends on the context, such as the |
| 16 | // screen's DPI resolution and the active font face. That context is |
| 17 | // represented by the Converter type. |
| 18 | // |
| 19 | // Conversions may be lossy. Converting 4.5 inches to pixels and back may |
| 20 | // result in something slightly different than 4.5. Similarly, converting 4 |
| 21 | // inches and 0.5 inches to pixels and then adding the results won't |
| 22 | // necessarily equal the conversion of 4.5 inches to pixels. |
| 23 | // |
| 24 | // Note that what CSS (Cascading Style Sheets) calls "px" differs from what |
| 25 | // this package calls "px". For legacy reasons, the CSS semantics are that 1 |
| 26 | // inch should roughly equal 96csspx regardless of the actual DPI resolution, |
| 27 | // as per https://developer.mozilla.org/en/docs/Web/CSS/length. This package's |
| 28 | // semantics are that 1px means exactly one physical pixel, always. This |
| 29 | // package represents 1csspx as 1.666666667dp, since there are 160 density |
| 30 | // independent pixels per inch, the same definition as Android. |
| 31 | package unit |
| 32 | |
| 33 | import ( |
| 34 | "fmt" |
| 35 | |
| 36 | "golang.org/x/image/math/fixed" |
| 37 | ) |
| 38 | |
| 39 | const ( |
| 40 | DensityIndependentPixelsPerInch = 160 |
| 41 | MillimetresPerInch = 25.4 |
| 42 | PointsPerInch = 72 |
| 43 | ) |
| 44 | |
| 45 | // Converter converts values from one unit to another. Conversions may be |
| 46 | // lossy. |
| 47 | type Converter interface { |
| 48 | // Convert converts v to the given unit. |
| 49 | Convert(v Value, to Unit) Value |
| 50 | |
| 51 | // Pixels converts v to a 26.6 fixed-point number of physical pixels. |
| 52 | Pixels(v Value) fixed.Int26_6 |
| 53 | } |
| 54 | |
| 55 | // Value is a number and a unit. |
| 56 | type Value struct { |
| 57 | F float64 |
| 58 | U Unit |
| 59 | } |
| 60 | |
| 61 | // String implements the fmt.Stringer interface. |
| 62 | func (v Value) String() string { |
| 63 | return fmt.Sprintf("%f%s", v.F, names[v.U]) |
| 64 | } |
| 65 | |
| 66 | var names = [...]string{ |
| 67 | Px: "px", |
| 68 | Dp: "dp", |
| 69 | Pt: "pt", |
| 70 | In: "in", |
| 71 | Mm: "mm", |
| 72 | Em: "em", |
| 73 | Ex: "ex", |
| 74 | Ch: "ch", |
| 75 | } |
| 76 | |
| 77 | // Unit is a unit of length, such as inches or pixels. |
| 78 | type Unit uint8 |
| 79 | |
| 80 | const ( |
| 81 | // Px is a physical pixel, regardless of the DPI resolution. |
| 82 | Px Unit = iota |
| 83 | |
| 84 | // Dp is 1 density independent pixel: 1/160th of an inch. |
| 85 | Dp |
| 86 | // Pt is 1 point: 1/72th of an inch. |
| 87 | Pt |
| 88 | // Mm is 1 millimetre: 1/25.4th of an inch. |
| 89 | Mm |
| 90 | // In is 1 inch. |
| 91 | // |
| 92 | // If the context does not specify a DPI resolution, the recommended |
| 93 | // fallback value for conversion is 72 pixels per inch. |
| 94 | In |
| 95 | |
| 96 | // Em is the height of the active font face, disregarding extra leading |
| 97 | // such as from double-spaced lines of text. |
| 98 | // |
| 99 | // If the context does not specify an active font face, the recommended |
| 100 | // fallback value for conversion is 12pt. |
| 101 | Em |
| 102 | // Ex is the x-height of the active font face. |
| 103 | // |
| 104 | // If the context does not specify an x-height, the recommended fallback |
| 105 | // value for conversion is 0.5em. |
| 106 | Ex |
| 107 | // Ch is the character width of the numeral zero glyph '0' of the active |
| 108 | // font face. |
| 109 | // |
| 110 | // If the context does not specify a '0' glyph, the recommended fallback |
| 111 | // value for conversion is 0.5em. |
| 112 | Ch |
| 113 | ) |
| 114 | |
| 115 | // Pixels returns the given number of Px as a Value. |
| 116 | func Pixels(f float64) Value { return Value{f, Px} } |
| 117 | |
| 118 | // DIPs returns the given number of Dp as a Value. |
| 119 | func DIPs(f float64) Value { return Value{f, Dp} } |
| 120 | |
| 121 | // Points returns the given number of Pt as a Value. |
| 122 | func Points(f float64) Value { return Value{f, Pt} } |
| 123 | |
| 124 | // Millimetres returns the given number of Mm as a Value. |
| 125 | func Millimetres(f float64) Value { return Value{f, Mm} } |
| 126 | |
| 127 | // Inches returns the given number of In as a Value. |
| 128 | func Inches(f float64) Value { return Value{f, In} } |
| 129 | |
| 130 | // Ems returns the given number of Em as a Value. |
| 131 | func Ems(f float64) Value { return Value{f, Em} } |
| 132 | |
| 133 | // Exs returns the given number of Ex as a Value. |
| 134 | func Exs(f float64) Value { return Value{f, Ex} } |
| 135 | |
| 136 | // Chs returns the given number of Ch as a Value. |
| 137 | func Chs(f float64) Value { return Value{f, Ch} } |