Reintent

@reintent/scene/measure

Measuring: where every semantic thing is, in scene units, and how much room a text takes.

Geometry comes from the shapes themselves (SVG bounding boxes), each owned by the nearest data-sem: occlusion is ignored, so the man behind the door still has his whole box. What can be seen of each thing comes from the pick buffer (@reintent/scene/pick). Atmosphere is measured on its own, for the verifier to ask about any that covers most of the frame. A text is measured in the DOM, off screen, or approximately where there is no DOM.

Interfaces

AtmosphereBox

Defined in: packages/scene/src/measure.ts

A piece of atmosphere: how much of the frame it covers, and whether it stays on its thing.

Properties

PropertyTypeDescription
attachedbooleanIts box stays within its owner's (a rim, a glint): decoration on a thing.
copybooleanOnly copies of the scene (<use>): a second image, not a new drawing.
namestring-
ownerstring | nullThe semantic path it is drawn inside, if any.
rationumberOf the frame (the viewBox), the part its box covers.

Box

Defined in: packages/scene/src/measure.ts

A box in scene units.

Properties

PropertyTypeDescription
pxnumberPixels (visible boxes), in scene units squared; 0 for geometry.
x0number-
x1number-
y0number-
y1number-

TextSize

Defined in: packages/scene/src/measure.ts

How much room a text takes, in CSS pixels.

Properties

PropertyType
hnumber
wnumber

Type Aliases

TextMeasurer

type TextMeasurer = (text, style, maxWidth) => TextSize;

Defined in: packages/scene/src/measure.ts

Measures a text in a style (CSS properties, camel-cased: fontSize, lineHeight, textWrap, …), at most maxWidth wide.

Parameters

ParameterType
textstring
styleReadonly<Record<string, string>>
maxWidthnumber

Returns

TextSize

Functions

approxTextSize()

function approxTextSize(
   text, 
   size, 
   maxWidth, 
   lineHeight?
): TextSize;

Defined in: packages/scene/src/measure.ts

A text's size without a DOM (in Node, in a test, in a Worker): a CJK character a full em, any other about half, broken into lines at maxWidth, each line lineHeight ems.

Parameters

ParameterTypeDefault value
textstringundefined
sizenumberundefined
maxWidthnumberundefined
lineHeightnumber1.3

Returns

TextSize

Example

import { approxTextSize } from '@reintent/scene/measure';

console.log(approxTextSize('别再为每块屏幕各做一份', 20, 120));
// → { w: 120, h: 52 }

createTextMeasurer()

function createTextMeasurer(doc?): TextMeasurer;

Defined in: packages/scene/src/measure.ts

A text measurer on the DOM: one probe, off screen, which takes each text in its style and is read for its box; each answer is kept. A balanced text (textWrap: 'balance') with lines of its own is set a block per line, so each balances by itself: the browser does not balance across a forced break.

Parameters

ParameterTypeDefault value
docDocumentdocument

Returns

TextMeasurer


measureAtmosphere()

function measureAtmosphere(svg): AtmosphereBox[];

Defined in: packages/scene/src/measure.ts

Every piece of atmosphere (<Atmosphere>, data-sem-post): how much of the frame it covers, and whether it stays on the thing it belongs to. Atmosphere is left out of everything else, so it is where an undeclared thing could hide.

Parameters

ParameterType
svgSVGSVGElement

Returns

AtmosphereBox[]


measureGeometry()

function measureGeometry(svg): Map<string, Box>;

Defined in: packages/scene/src/measure.ts

Every semantic path's box, from the shapes drawn under it, in scene units: a part's box, and its thing's (a box for each of the path's prefixes). Atmosphere and paint servers are left out.

Parameters

ParameterType
svgSVGSVGElement

Returns

Map<string, Box>


sceneToClient()

function sceneToClient(svg): [number, number, number, number, number, number];

Defined in: packages/scene/src/measure.ts

Scene units → the svg's client pixels (what the viewer sees), as [a, b, c, d, e, f].

Parameters

ParameterType
svgSVGSVGElement

Returns

[number, number, number, number, number, number]

Last updated on

Edit on GitHub