Class UnitType
Everything is a postfix scale. Every unit, percentages included, is exactly one operation: multiply the value on top of the evaluation stack by a scalar. There is no unit system in the evaluator, only a stack machine and a double [] scale table indexed by the UT_* constants. px is 1.0, pt is dpi/72, em is the font size, % is axisParentDimension/100, and so on.
Only TWO of the 29 units are axis-dependent, UT_PC and UT_DLG. The scale table can therefore be built once per element with fillCommon(IScaleProvider,double[]) and have just those two slots swapped between the X pass and the Y pass with fillAxis(IScaleProvider,double[],int). One parsed program serves both axes.
The unit numbering 1 to 19 is unchanged from the original GUnit implementation and is part of the wire format; units 20 to 29 are new. The name array is ONE-BASED, i.e. getName(UT_PX) rather than TYPES[UT_PX-1].
- Author:
- Christopher Mindus
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intAxis constant for a horizontal, i.e.static final intAxis constant for a vertical, i.e.static final intThe size of a scale table, i.e.static final intThe number of unit types, i.e.static final intThe component's own computed CSSmax-heightin pixels,chmax.static final intThe component's own computed CSSmin-heightin pixels,chmin.static final intThe component's own computed CSSmax-widthin pixels,cwmax.static final intThe component's own computed CSSmin-widthin pixels,cwmin.static final intDialog units,dlg.static final intTHE DEVICE PIXEL RATIO ITSELF,dpr: 1, 1.25, 1.5, 2, 3.static final intONE PHYSICAL DEVICE PIXEL expressed in CSS pixels,dpx, i.e.static final intThe font size of the element's own font,em.static final intThe x-height of the element's own font,ex.static final intThe safe-area inset on the BOTTOM,nb.static final intThe notch cutout bounding-box HEIGHT in SCREEN space,nh.static final intThe safe-area inset on the LEFT,nl.static final intThe safe-area inset on the RIGHT,nr.static final intThe safe-area inset on the TOP,nt.static final intThe notch cutout bounding-box WIDTH in SCREEN space,nw.static final intThe offset along the top or bottom edge to the LEFT side of the notch,nx.static final intThe offset along the left or right edge to the TOP of the notch,ny.static final intPercent of the parent along the axis being calculated,%.static final intPercent of the parent HEIGHT,%h.static final intPercent of the LARGER of the parent's width and height,%max.static final intPercent of the SMALLER of the parent's width and height,%min.static final intPercent of the parent WIDTH,%w.static final intPoints,pt, i.e.static final intPixels,px.static final intThe ROOT element font size,rem.static final intPercent of the viewport height,vh.static final intPercent of the larger viewport dimension,vmax.static final intPercent of the smaller viewport dimension,vmin.static final intPercent of the viewport width,vw.Method Summary
Modifier and TypeMethodDescriptionstatic double[]buildScaleTable(IScaleProvider p, int axis) Convenience method building a complete scale table for one axis in a single call.static double[]Allocates an empty scale table of the correct size.static voidfillAxis(IScaleProvider p, double[] scales, int axis) static voidfillCommon(IScaleProvider p, double[] scales) Fills every axis-INDEPENDENT slot of the scale table.static intLooks a unit name up, case insensitively.static StringgetName(int ut) Gets the name of a unit type.static booleanisAxisDependent(int ut) Checks if the unit type is axis-dependent, i.e.static StringSuggests a unit name for a misspelling, for the "did you mean" part of an error message.
Field Details
AXIS_X
public static final int AXIS_XAxis constant for a horizontal, i.e. X, evaluation pass.- See Also:
AXIS_Y
public static final int AXIS_YAxis constant for a vertical, i.e. Y, evaluation pass.- See Also:
UT_PX
public static final int UT_PXPixels,px. The identity scale of 1.0; the emitter emits no scale opcode for it, and it is the implicit unit of a bare number.- See Also:
UT_PT
public static final int UT_PTPoints,pt, i.e. 1/72 inch.- See Also:
UT_EM
public static final int UT_EMThe font size of the element's own font,em. Per-element.- See Also:
UT_EX
public static final int UT_EXThe x-height of the element's own font,ex. Per-element.- See Also:
UT_DLG
public static final int UT_DLGDialog units,dlg. Axis-dependent, and per-element since it depends on the element's own font. Windows dialog units use a different base unit AND a different divisor per axis: horizontalpx = dlu*baseUnitX/4, verticalpx = dlu*baseUnitY/8.- See Also:
UT_PC
public static final int UT_PCPercent of the parent along the axis being calculated,%. Axis-dependent.- See Also:
UT_PCW
public static final int UT_PCWPercent of the parent WIDTH,%w. Axis-independent, so it is usable on the Y axis to obtain an X-relative value.- See Also:
UT_PCH
public static final int UT_PCHPercent of the parent HEIGHT,%h. Axis-independent, the mirror ofUT_PCW.- See Also:
UT_PCMIN
public static final int UT_PCMINPercent of the SMALLER of the parent's width and height,%min. Axis-independent: the same value on the X pass and the Y pass.- See Also:
UT_PCMAX
public static final int UT_PCMAXPercent of the LARGER of the parent's width and height,%max. Axis-independent.- See Also:
UT_VW
public static final int UT_VWPercent of the viewport width,vw.- See Also:
UT_VH
public static final int UT_VHPercent of the viewport height,vh.- See Also:
UT_VMIN
public static final int UT_VMINPercent of the smaller viewport dimension,vmin.- See Also:
UT_VMAX
public static final int UT_VMAXPercent of the larger viewport dimension,vmax.- See Also:
UT_NL
public static final int UT_NLThe safe-area inset on the LEFT,nl.- See Also:
UT_NR
public static final int UT_NRThe safe-area inset on the RIGHT,nr.- See Also:
UT_NW
public static final int UT_NWThe notch cutout bounding-box WIDTH in SCREEN space,nw.The meaning of this unit is restated. It is the screen-space width of the cutout, not "the maximum of the left and right insets" as the original javadoc said. Because
nwandUT_NHdescribe the bounding box in screen space, they swap as the device rotates under the cutout: a 40x10 notch in portrait becomes 10x40 in landscape, and a formula author never has to branch on orientation.In landscape the two descriptions coincide, since the inset perpendicular to the edge IS the cutout extent along the screen's horizontal; that is why the previous definition worked in practice. In portrait the width has no data source on the web platform and resolves to whatever the
IScaleProviderreports, which is 0 unless a measured value has been supplied.- See Also:
UT_NT
public static final int UT_NTThe safe-area inset on the TOP,nt.- See Also:
UT_NB
public static final int UT_NBThe safe-area inset on the BOTTOM,nb. Note that in portrait on an iPhone X or later this is the home-indicator inset, around 34px, and NOT zero.- See Also:
UT_CWMIN
public static final int UT_CWMINThe component's own computed CSSmin-widthin pixels,cwmin.Naming.
%min,%max,%wand%hall measure the PARENT; these four measure the COMPONENT. Naming them%wminand so on would put two different subjects under one prefix, so that%wand%wminwould read as a family while meaning different things. Thecprefix marks the subject.- See Also:
UT_CWMAX
public static final int UT_CWMAX- See Also:
UT_CHMIN
public static final int UT_CHMIN- See Also:
UT_CHMAX
public static final int UT_CHMAX- See Also:
UT_REM
public static final int UT_REMThe ROOT element font size,rem. Real CSS and universally supported;UT_EMcovers the element's own font, nothing else was root-relative.- See Also:
UT_DPX
public static final int UT_DPXONE PHYSICAL DEVICE PIXEL expressed in CSS pixels,dpx, i.e.1/devicePixelRatio. A LENGTH.0.333px on a 3x display and 0.25px at 4x, so on a 4x device
4dpxis exactly1px. Non-integer ratios are the common case, since Windows display scaling gives 1.25, 1.5, 1.75 and 2.5, and they work normally:1dpxat 1.25 is 0.8px.Two caveats. First, browsers re-round hairlines: for
border-width,outline-width,column-rule-widthandoutline-offsetthe computed length is rounded to an integer number of device pixels, so adpx-derived value landing in one of those four properties gets the browser's rounding applied on top of ours. Width, height, margin and padding pass through untouched. Second,devicePixelRatiois not constant for the life of a page: it changes on browser zoom and when a window moves between displays of different density, with NO resize event, so the client must refill this slot from amatchMedia('(resolution: Ndppx)')change listener.Neither
dpxnorUT_DPRis a CSS unit. CSS has no<length>unit for a physical device pixel;dppxexists but is a<resolution>unit valid only inside media queries. This is no different fromdlg,%wornlin that the browser only ever receives the evaluatedpxvalue.- See Also:
UT_DPR
public static final int UT_DPRTHE DEVICE PIXEL RATIO ITSELF,dpr: 1, 1.25, 1.5, 2, 3.This unit is unlike every other one. Every other unit is a measurement that stands alone.
dpris a bare MULTIPLIER, NOT A LENGTH:1dprby itself resolves to an arbitrary pixel count, 3px on a 3x display, and is not a meaningful size. It is only useful multiplied by something else, e.g.16px*1dprwhich gives 48px there. It is intended for density-aware sizing.dprandUT_DPXare exact reciprocals, so1dpx*1dpris exactly1pxin IEEE double even at 3x, since(1.0/3.0)*3.0==1.0. They can therefore cancel silently in a formula.Do not confuse the IIZI unit
dprwith the JavaScript variablewindow.devicePixelRatio. They are the same quantity at different layers: the unit is what a designer writes in a formula, the variable is what fills the scale table.- See Also:
UT_NH
public static final int UT_NHThe notch cutout bounding-box HEIGHT in SCREEN space,nh. Pairs withUT_NW; see there for the screen-space rotation behaviour.- See Also:
UT_NX
public static final int UT_NXThe offset along the top or bottom edge to the LEFT side of the notch,nx. Used mainly in portrait and zero in landscape.EDGE, not centre.
nxis the left edge,nx+nwthe right edge, and the centre isnx+nw/2when wanted. Centre-based offsets would make the common case pay a-nw/2tax, and edge is consistent withnl,nr,ntandnband with every platform API, all of which return rectangles. A useful expression worth knowing rather than adding a unit for: the gap to the right of the notch in portrait is100%w-(1nx+1nw).- See Also:
UT_NY
public static final int UT_NYThe offset along the left or right edge to the TOP of the notch,ny. Used mainly in landscape and zero in portrait. SeeUT_NXfor the edge-not-centre decision.- See Also:
UNIT_COUNT
public static final int UNIT_COUNTThe number of unit types, i.e. the largest legalUT_*value.- See Also:
SCALE_TABLE_SIZE
public static final int SCALE_TABLE_SIZEThe size of a scale table, i.e.UNIT_COUNT+1since the table is indexed one-based by theUT_*constants and slot 0 is unused.- See Also:
Method Details
getName
Gets the name of a unit type.- Parameters:
ut- The unit type, 1 toUNIT_COUNT.- Returns:
- The unit name, e.g.
"%w". - Throws:
IllegalArgumentException- If the unit type is out of range.
fromName
Looks a unit name up, case insensitively.- Parameters:
name- The candidate unit name, exactly as written in the source.- Returns:
- The unit type 1 to
UNIT_COUNT, or -1 if the name is not a unit.
suggest
Suggests a unit name for a misspelling, for the "did you mean" part of an error message. A suggestion is only offered when the edit distance is exactly one AND the match is unique, so that the suggestion is never a guess between two candidates.- Parameters:
name- The unknown unit name as written in the source.- Returns:
- The suggested unit name, or
nullwhen there is no unique edit-distance-one match.
isAxisDependent
createScaleTable
public static double[] createScaleTable()Allocates an empty scale table of the correct size.- Returns:
- A new
double []ofSCALE_TABLE_SIZEentries.
fillCommon
Fills every axis-INDEPENDENT slot of the scale table. Call this once per element, then callfillAxis(IScaleProvider,double[],int)once per axis pass.- Parameters:
p- The value provider.scales- The scale table to fill, ofSCALE_TABLE_SIZEentries.
fillAxis
Fills the two axis-DEPENDENT slots of the scale table,UT_PCandUT_DLG. Call once per axis pass afterfillCommon(IScaleProvider, double[]).- Parameters:
p- The value provider.scales- The scale table, ofSCALE_TABLE_SIZEentries.axis-AXIS_XorAXIS_Y.
buildScaleTable
Convenience method building a complete scale table for one axis in a single call. PreferfillCommon(IScaleProvider, double[])plusfillAxis(IScaleProvider, double[], int)when both axes are evaluated for the same element, which is the normal layout case.