Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · std.typeset

    Length.resolve for Em

    Resolves an em length using an explicit font size.

    resolve(fontSize: Length<Pt>) -> Length<Pt>
    Receiver
    Length<Em>
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.typeset in language mode 5.21.

    The current compiler selects std/typeset.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Length<Em>

    Parameter constraints

    Physical Length values with the Pt, Mm, Cm, or In marker provide toScaledPoints returning int. One point is 65,536 scaled points. The result is the exact stored signed 64-bit count.

    The scaledPoints member is absent from every Length type. Calling that removed member is rejected with TPZ5006. Length<Em> has no toScaledPoints member and must resolve to a physical length first.

    fromScaledPoints accepts every int from -9223372036854775808 through 9223372036854775807 inclusive. It returns Length<Pt> with that exact scaled-point count. Zero and negative inputs are valid. Construction performs no rounding and has no value-dependent failure.

    fromMilliPoints interprets its int argument as thousandths of a point. Its scaled-point result is roundEven of the exact rational input times 8192 divided by 125.

    fromMilliPoints accepts -140737488355328000 through 140737488355327999 inclusive. An input outside that range stops with runtime diagnostic TPZ4004. It does not return Result, wrap, saturate, or clamp.

    Length<Pt>.toMilliPoints returns roundEven of the exact stored count times 125 divided by 8192. Every representable point length has a representable int result.

    roundEven selects the nearest integer and chooses the even integer at an exact tie. Positive and negative ties follow the same rule. Range checking applies to the final rounded value. Intermediate arithmetic must preserve the exact rational value.

    A count of 4096 scaled points converts to 62 millipoints. A count of 12288 converts to 188. Their negatives convert to -62 and -188. One millipoint constructs 66 scaled points and negative one constructs -66.

    Millipoint conversion can lose scaled-point precision. A conversion to millipoints and back need not recover the original length. The maximum point length converts to 140737488355328000 millipoints, which exceeds the fromMilliPoints input range.

    The constructors require int arguments. A float or Length argument is rejected with TPZ5001. Physical conversions and scalar arithmetic do not provide an implicit integer constructor.

    A constructor evaluates its argument once before checking the result range. A receiver conversion evaluates its receiver once. These operations perform no I/O or mutation and read no font or paragraph state.

    A fromMilliPoints overflow is reported at the public constructor call. Earlier argument effects remain visible and later expressions do not execute.

    LatinSpacing stores minWordSpaceRatio and maxWordSpaceRatio as dimensionless float values, maxLetterSpacingEm as Length<Em>, and fontSizePt as Length<Pt>. There is no implicit scalar or cross-unit conversion.

    Record construction and field reads store and return the supplied values. They do not validate a paragraph policy or resolve the relative length. Field expressions follow ordinary source-order evaluation once.

    A paragraph validates every supplied LatinSpacing record before planning. This validation also applies in Unjustified mode and with the KnuthPlass cost model.

    minWordSpaceRatio must be finite and between 0.8 and 1.0 inclusive. maxWordSpaceRatio must be finite and between 1.0 and 1.33 inclusive. Invalid ratios return Err containing TypesetError.InvalidPolicy.

    maxLetterSpacingEm must be between 0em and 0.015em inclusive on the stored relative grid. The upper bound is 983 em ticks. A negative tick or 984 ticks returns Err containing TypesetError.InvalidPolicy.

    fontSizePt must have a strictly positive scaled-point count. A one-scaled-point font is valid even when the paragraph uses millipoints. Zero and negative fonts return Err containing TypesetError.InvalidPolicy, including when letter spacing is zero.

    InvalidPolicy carries explanatory text. Its exact message text and the order among independent invalid policy fields are not fixed by this contract.

    Resolve the stored maximum with maxLetterSpacingEm.resolve(fontSizePt). The stored relative count is multiplied by the physical font count and divided by 65,536 with one nearest-even rounding to scaled points. No ambient font is read.

    Convert the resolved result to the selected computation grid. ScaledPoint preserves its integer count. MilliPoint applies the shared nearest-even conversion. Every eligible gap receives this rounded bound before multiplication by the gap count.

    An eligible Latin letter gap lies between adjacent retained text units. Each unit must contain an alphabetic scalar and every scalar must be in U+0041 through U+024F, U+0300 through U+036F, or U+1E00 through U+1EFF. Paragraph trimming determines the retained units.

    The retained line uses this capacity path when it contains a CJK unit, or contains a Latin letter unit and no other alphabetic unit. A non-Latin unit containing an alphabetic scalar counts as another alphabetic unit. Paragraph classification determines CJK units.

    Let S be the sum of the grid widths of retained units whose text is U+0020. Word shrink capacity is floor(float(S) * (1.0 - minWordSpaceRatio)). Word stretch capacity is floor(float(S) * (maxWordSpaceRatio - 1.0)). Each expression keeps that binary64 evaluation order and its independent floor.

    The letter stretch contribution is the eligible gap count times the quantized relative limit. CJK capacity retains its separate floor rule. Combining these contributions does not repeat font resolution or round their total as one relative length.

    Unjustified mode does not apply spacing capacity. KnuthPlass obtains stretch and shrink from glue after the shared policy validation. FixedPoint uses the validated Latin spacing in its justification capacity and requires MilliPoint.

    Resolution, integer accumulation, and capacity arithmetic use their checked numeric contracts. Overflow stops with TPZ4004 and does not return InvalidPolicy. Validation and conversion perform no I/O and do not mutate the record.

    For 0.01em at 12pt, 655 relative ticks resolve to 7,860 scaled points and then 120 millipoints. For 0.0078125em at 24pt, each gap receives 188 millipoints. Two such gaps provide 376 millipoints.

    Length<Em> stores a signed 64-bit relative count with 65,536 ticks per em. Its inclusive count range is -9223372036854775808 through 9223372036854775807. Zero and negative relative lengths are valid.

    An em literal rounds its exact decimal magnitude to relative ticks with nearest-even rounding. Resolution uses those stored ticks and does not recover discarded literal precision.

    resolve takes one Length<Pt> font size and returns Length<Pt>. The font size must have a strictly positive scaled-point count. A one-scaled-point font is valid.

    If e is the stored em count and f is the stored font-size count, the physical result is roundEven of the exact rational e times f divided by 65,536. Multiply before rounding and round once to the physical grid.

    The result must fit the common signed 64-bit physical range after rounding. Overflow stops with runtime diagnostic TPZ4004. Exact intermediates must not create a smaller accepted domain.

    A zero or negative font size stops with runtime diagnostic TPZ4004, including when the relative length is zero. These failures are runtime faults and are not Result values.

    A font size with a different unit marker, an int, or a float is rejected with TPZ5001. A physical Length has no resolve member. Relative lengths have no direct physical conversion or integer grid member.

    The receiver evaluates once and then the font-size argument evaluates once. Resolution reads no ambient font state and performs no I/O or mutation. It preserves effects already caused by evaluating those inputs.

    After resolution, use the physical conversion methods and integer interchange operations. LatinSpacing uses this same resolution before the selected paragraph grid quantizes the per-gap distance.

    For 0.01em and 12pt, the relative count is 655 and the resolved count is 7,860 scaled points. Its nearest-even millipoint conversion is 120.

    Result

    The declared result type is Length<Pt>.

    Effects

    Failures

    Result and effects

    Link

    Multiply the stored relative ticks by the explicit positive Length<Pt> font size and round once to the physical scaled-point grid. A nonpositive font or unrepresentable result stops with TPZ4004. No ambient font participates.

    Why

    Link

    A font-relative length needs an explicit font size to become a physical distance. The argument keeps stored lengths independent of an ambient or subsequently changed font.

    Related entries

    Link

    std.typeset

    Convert physical lengths with toScaledPoints

    Link
    Ruletopaz.stdlib.contract.to-scaled-points.rule

    Convert physical lengths with toScaledPoints

    Physical Length values with the Pt, Mm, Cm, or In marker provide toScaledPoints returning int. One point is 65,536 scaled points. The result is the exact stored signed 64-bit count.

    The scaledPoints member is absent from every Length type. Calling that removed member is rejected with TPZ5006. Length<Em> has no toScaledPoints member and must resolve to a physical length first.

    fromScaledPoints accepts every int from -9223372036854775808 through 9223372036854775807 inclusive. It returns Length<Pt> with that exact scaled-point count. Zero and negative inputs are valid. Construction performs no rounding and has no value-dependent failure.

    fromMilliPoints interprets its int argument as thousandths of a point. Its scaled-point result is roundEven of the exact rational input times 8192 divided by 125.

    fromMilliPoints accepts -140737488355328000 through 140737488355327999 inclusive. An input outside that range stops with runtime diagnostic TPZ4004. It does not return Result, wrap, saturate, or clamp.

    Length<Pt>.toMilliPoints returns roundEven of the exact stored count times 125 divided by 8192. Every representable point length has a representable int result.

    roundEven selects the nearest integer and chooses the even integer at an exact tie. Positive and negative ties follow the same rule. Range checking applies to the final rounded value. Intermediate arithmetic must preserve the exact rational value.

    A count of 4096 scaled points converts to 62 millipoints. A count of 12288 converts to 188. Their negatives convert to -62 and -188. One millipoint constructs 66 scaled points and negative one constructs -66.

    Millipoint conversion can lose scaled-point precision. A conversion to millipoints and back need not recover the original length. The maximum point length converts to 140737488355328000 millipoints, which exceeds the fromMilliPoints input range.

    The constructors require int arguments. A float or Length argument is rejected with TPZ5001. Physical conversions and scalar arithmetic do not provide an implicit integer constructor.

    A constructor evaluates its argument once before checking the result range. A receiver conversion evaluates its receiver once. These operations perform no I/O or mutation and read no font or paragraph state.

    A fromMilliPoints overflow is reported at the public constructor call. Earlier argument effects remain visible and later expressions do not execute.

    Why

    Integer conversions use the same to prefix as unit conversions and collection conversions. Scaled points and millipoints remain distinct because they represent different units and rounding boundaries.

    Source evidence

    Convert every physical length unit to scaled points

    Link
    main.tpzConvert every physical length unit to scaled pointsOpen in Playground
    1. import std.io
    2. import std.typeset as ts
    3. let pt = (1pt).toScaledPoints()
    4. let mm = (25.4mm).toScaledPoints()
    5. let cm = (2.54cm).toScaledPoints()
    6. let inch = (1in).toScaledPoints()
    7. io.writeLine("{pt}|{mm}|{cm}|{inch}|{ts.fromScaledPoints(pt) == 1pt}")
    Expected outputexit 0
    65536|4718592|4718592|4718592|true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 65536|4718592|4718592|4718592|true
    Source evidence

    Keep the millipoint conversion boundary

    Link
    main.tpzKeep the millipoint conversion boundaryOpen in Playground
    1. import std.io
    2. import std.typeset as ts
    3. let tie = ts.fromScaledPoints(4096).toMilliPoints()
    4. let rounded = ts.fromMilliPoints(1) == ts.fromScaledPoints(66)
    5. io.writeLine("{tie}|{rounded}")
    Expected outputexit 0
    62|true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 62|true
    Source evidence

    Reject scaledPoints on `Length<Pt>`

    Link
    main.tpzReject scaledPoints on `Length<Pt>`Open in Playground
    1. let distance = 1pt
    2. distance.scaledPoints()
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5006 stdlib-removed-scaled-points at main.tpz:2:10
    Source evidence

    Reject scaledPoints on `Length<Mm>`

    Link
    main.tpzReject scaledPoints on `Length<Mm>`Open in Playground
    1. let distance = 1mm
    2. distance.scaledPoints()
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5006 stdlib-removed-scaled-points at main.tpz:2:10
    Source evidence

    Reject scaledPoints on `Length<Cm>`

    Link
    main.tpzReject scaledPoints on `Length<Cm>`Open in Playground
    1. let distance = 1cm
    2. distance.scaledPoints()
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5006 stdlib-removed-scaled-points at main.tpz:2:10
    Source evidence

    Reject scaledPoints on `Length<In>`

    Link
    main.tpzReject scaledPoints on `Length<In>`Open in Playground
    1. let distance = 1in
    2. distance.scaledPoints()
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5006 stdlib-removed-scaled-points at main.tpz:2:10
    Source evidence

    Resolve font-relative lengths before integer conversion

    Link
    main.tpzResolve font-relative lengths before integer conversionOpen in Playground
    1. let distance = 1em
    2. distance.toScaledPoints()
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5006 stdlib-unresolved-em-scaled-points at main.tpz:2:10
    Source evidence

    Read both endpoints of the exact integer representation

    Link
    main.tpzRead both endpoints of the exact integer representationOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let low = ts.fromScaledPoints(-9223372036854775808).toScaledPoints()
    4. let high = ts.fromScaledPoints(9223372036854775807).toScaledPoints()
    5. io.writeLine("{low}|{high}")
    Expected outputexit 0
    -9223372036854775808|9223372036854775807
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. -9223372036854775808|9223372036854775807
    Source evidence

    Construct the millipoint endpoints and keep signed rounding

    Link
    main.tpzConstruct the millipoint endpoints and keep signed roundingOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let low = ts.fromMilliPoints(-140737488355328000)
    4. let high = ts.fromMilliPoints(140737488355327999)
    5. let negative = ts.fromMilliPoints(-1)
    6. let lowGrid = ts.fromScaledPoints(-9223372036854775808).toMilliPoints()
    7. let highGrid = ts.fromScaledPoints(9223372036854775807).toMilliPoints()
    8. io.writeLine("{low == -140737488355328pt}|{high == ts.fromScaledPoints(9223372036854775742)}|{negative == ts.fromScaledPoints(-66)}")
    9. io.writeLine("{lowGrid}|{highGrid}")
    Expected outputexit 0
    true|true|true
    -140737488355328000|140737488355328000
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true|true|true
    2. -140737488355328000|140737488355328000
    Source evidence

    Round both signs and both sides of a millipoint tie

    Link
    main.tpzRound both signs and both sides of a millipoint tieOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let below = ts.fromScaledPoints(4095).toMilliPoints()
    4. let tie = ts.fromScaledPoints(4096).toMilliPoints()
    5. let above = ts.fromScaledPoints(4097).toMilliPoints()
    6. let negativeBelow = ts.fromScaledPoints(-4095).toMilliPoints()
    7. let negativeTie = ts.fromScaledPoints(-4096).toMilliPoints()
    8. let negativeAbove = ts.fromScaledPoints(-4097).toMilliPoints()
    9. let upperTie = ts.fromScaledPoints(12288).toMilliPoints()
    10. let negativeUpperTie = ts.fromScaledPoints(-12288).toMilliPoints()
    11. io.writeLine("{below}|{tie}|{above}|{negativeBelow}|{negativeTie}|{negativeAbove}|{upperTie}|{negativeUpperTie}")
    Expected outputexit 0
    62|62|63|-62|-62|-63|188|-188
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 62|62|63|-62|-62|-63|188|-188
    Source evidence

    Reject a millipoint input above the representable range

    Link
    main.tpzReject a millipoint input above the representable rangeOpen in Playground
    1. import std.typeset as ts
    2. ts.fromMilliPoints(140737488355328000)
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4004 stdlib-length-unrepresentable at main.tpz:2:1
    Source evidence

    Reject a millipoint input below the representable range

    Link
    main.tpzReject a millipoint input below the representable rangeOpen in Playground
    1. import std.typeset as ts
    2. ts.fromMilliPoints(-140737488355328001)
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4004 stdlib-length-unrepresentable at main.tpz:2:1
    Source evidence

    Require an int for fromScaledPoints

    Link
    main.tpzRequire an int for fromScaledPointsOpen in Playground
    1. import std.typeset as ts
    2. ts.fromScaledPoints(1.0)
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5001 stdlib-length-int-required at main.tpz:2:21
    Source evidence

    Require an int for fromMilliPoints

    Link
    main.tpzRequire an int for fromMilliPointsOpen in Playground
    1. import std.typeset as ts
    2. ts.fromMilliPoints(1.0)
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5001 stdlib-length-int-required at main.tpz:2:20
    Source evidence

    Diagnostics

    Link

    Unknown member or field

    Integer overflow

    TPZ5001

    Resolve relative letter spacing through Length

    Link
    Ruletopaz.stdlib.contract.latin-spacing-em.rule

    Resolve relative letter spacing through Length

    LatinSpacing stores minWordSpaceRatio and maxWordSpaceRatio as dimensionless float values, maxLetterSpacingEm as Length<Em>, and fontSizePt as Length<Pt>. There is no implicit scalar or cross-unit conversion.

    Record construction and field reads store and return the supplied values. They do not validate a paragraph policy or resolve the relative length. Field expressions follow ordinary source-order evaluation once.

    A paragraph validates every supplied LatinSpacing record before planning. This validation also applies in Unjustified mode and with the KnuthPlass cost model.

    minWordSpaceRatio must be finite and between 0.8 and 1.0 inclusive. maxWordSpaceRatio must be finite and between 1.0 and 1.33 inclusive. Invalid ratios return Err containing TypesetError.InvalidPolicy.

    maxLetterSpacingEm must be between 0em and 0.015em inclusive on the stored relative grid. The upper bound is 983 em ticks. A negative tick or 984 ticks returns Err containing TypesetError.InvalidPolicy.

    fontSizePt must have a strictly positive scaled-point count. A one-scaled-point font is valid even when the paragraph uses millipoints. Zero and negative fonts return Err containing TypesetError.InvalidPolicy, including when letter spacing is zero.

    InvalidPolicy carries explanatory text. Its exact message text and the order among independent invalid policy fields are not fixed by this contract.

    Resolve the stored maximum with maxLetterSpacingEm.resolve(fontSizePt). The stored relative count is multiplied by the physical font count and divided by 65,536 with one nearest-even rounding to scaled points. No ambient font is read.

    Convert the resolved result to the selected computation grid. ScaledPoint preserves its integer count. MilliPoint applies the shared nearest-even conversion. Every eligible gap receives this rounded bound before multiplication by the gap count.

    An eligible Latin letter gap lies between adjacent retained text units. Each unit must contain an alphabetic scalar and every scalar must be in U+0041 through U+024F, U+0300 through U+036F, or U+1E00 through U+1EFF. Paragraph trimming determines the retained units.

    The retained line uses this capacity path when it contains a CJK unit, or contains a Latin letter unit and no other alphabetic unit. A non-Latin unit containing an alphabetic scalar counts as another alphabetic unit. Paragraph classification determines CJK units.

    Let S be the sum of the grid widths of retained units whose text is U+0020. Word shrink capacity is floor(float(S) * (1.0 - minWordSpaceRatio)). Word stretch capacity is floor(float(S) * (maxWordSpaceRatio - 1.0)). Each expression keeps that binary64 evaluation order and its independent floor.

    The letter stretch contribution is the eligible gap count times the quantized relative limit. CJK capacity retains its separate floor rule. Combining these contributions does not repeat font resolution or round their total as one relative length.

    Unjustified mode does not apply spacing capacity. KnuthPlass obtains stretch and shrink from glue after the shared policy validation. FixedPoint uses the validated Latin spacing in its justification capacity and requires MilliPoint.

    Resolution, integer accumulation, and capacity arithmetic use their checked numeric contracts. Overflow stops with TPZ4004 and does not return InvalidPolicy. Validation and conversion perform no I/O and do not mutate the record.

    For 0.01em at 12pt, 655 relative ticks resolve to 7,860 scaled points and then 120 millipoints. For 0.0078125em at 24pt, each gap receives 188 millipoints. Two such gaps provide 376 millipoints.

    Why

    One relative length model gives a letter-spacing limit an explicit font and unit. Validating the stored policy before planning separates field construction from recoverable policy errors. Per-gap quantization makes the permitted adjustment reproducible.

    Source evidence

    Store a relative spacing length and an explicit point font size

    Link
    main.tpzStore a relative spacing length and an explicit point font sizeOpen in Playground
    1. import std.typeset as ts
    2. let value = ts.LatinSpacing {
    3. minWordSpaceRatio: 0.8,
    4. maxWordSpaceRatio: 1.33,
    5. maxLetterSpacingEm: 0.01em,
    6. fontSizePt: 12pt
    7. }
    8. let relative: Length<Em> = value.maxLetterSpacingEm
    9. let physical: Length<Pt> = relative.resolve(value.fontSizePt)
    Expected outputexit 0
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Checking accepts the program. Exit code 0.

    Source evidence

    Resolve the stored spacing before converting to the computation grid

    Link
    main.tpzResolve the stored spacing before converting to the computation gridOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let spacing = ts.LatinSpacing {
    4. minWordSpaceRatio: 0.8,
    5. maxWordSpaceRatio: 1.33,
    6. maxLetterSpacingEm: 0.01em,
    7. fontSizePt: 12pt
    8. }
    9. let limit = spacing.maxLetterSpacingEm.resolve(spacing.fontSizePt)
    10. let exact = limit == ts.fromScaledPoints(7860)
    11. let millipoints = limit.toMilliPoints()
    12. io.writeLine("{exact}|{millipoints}")
    Expected outputexit 0
    true|120
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true|120
    Source evidence

    Apply nearest-even rounding at each typed value boundary

    Link
    main.tpzApply nearest-even rounding at each typed value boundaryOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let lowTie = (0.5em).resolve(ts.fromScaledPoints(3)) == ts.fromScaledPoints(2)
    4. let highTie = (0.5em).resolve(ts.fromScaledPoints(5)) == ts.fromScaledPoints(2)
    5. let literalGrid = (0.01em).resolve(12pt) == ts.fromScaledPoints(7860)
    6. let gridLow = (0.5em).resolve(0.125pt).toMilliPoints()
    7. let gridHigh = (0.5em).resolve(0.375pt).toMilliPoints()
    8. io.writeLine("{lowTie}|{highTie}|{literalGrid}|{gridLow}|{gridHigh}")
    Expected outputexit 0
    true|true|true|62|188
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true|true|true|62|188
    Source evidence

    Reject an untyped multiplier in the relative-length field

    Link
    main.tpzReject an untyped multiplier in the relative-length fieldOpen in Playground
    1. import std.typeset as ts
    2. let spacing = ts.LatinSpacing {
    3. minWordSpaceRatio: 0.8,
    4. maxWordSpaceRatio: 1.33,
    5. maxLetterSpacingEm: 0.01,
    6. fontSizePt: 12pt
    7. }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5001 stdlib-relative-distance-requires-em at main.tpz:5:23
    Source evidence

    Reject a physical length where an em length is required

    Link
    main.tpzReject a physical length where an em length is requiredOpen in Playground
    1. import std.typeset as ts
    2. let spacing = ts.LatinSpacing {
    3. minWordSpaceRatio: 0.8,
    4. maxWordSpaceRatio: 1.33,
    5. maxLetterSpacingEm: 0.12pt,
    6. fontSizePt: 12pt
    7. }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5001 stdlib-relative-distance-requires-em at main.tpz:5:23
    Source evidence

    Reject treating the stored relative length as a float

    Link
    main.tpzReject treating the stored relative length as a floatOpen in Playground
    1. import std.typeset as ts
    2. function multiplier(value: ts.LatinSpacing) -> float {
    3. value.maxLetterSpacingEm
    4. }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5001 stdlib-relative-distance-is-not-float at main.tpz:3:3
    Source evidence

    Admit the inclusive ratio and font bounds

    Link
    main.tpzAdmit the inclusive ratio and font boundsOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. function paragraph(grid: ts.DistanceGrid, spacing: Option<ts.LatinSpacing>) -> ts.Paragraph {
    4. ts.Paragraph {
    5. grid: grid,
    6. items: [
    7. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "abc", width: 10pt, trackingAfter: 0pt }),
    8. ts.Item.PenaltyItem(ts.Penalty {
    9. unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(),
    10. kind: ts.BreakKind.ForcedEnd(), flagged: false
    11. })
    12. ],
    13. geometry: ts.LineGeometry {
    14. bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt
    15. },
    16. justification: ts.JustificationPolicy {
    17. mode: ts.JustificationMode.Unjustified(),
    18. minGapCount: 0, minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    19. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    20. latinSpacing: spacing, cjkStretchRatio: 0.0
    21. },
    22. costModel: ts.CostModel.FixedPoint(),
    23. knuthPlass: ts.KnuthPlassPolicy {
    24. toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0,
    25. flaggedDemerit: 0, finalFlaggedDemerit: 0
    26. }
    27. }
    28. }
    29. for value in ([ts.LatinSpacing { minWordSpaceRatio: 0.8, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0em, fontSizePt: ts.fromScaledPoints(1) },
    30. ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.33, maxLetterSpacingEm: 0.015em, fontSizePt: 12pt }]) {
    31. match (ts.breakParagraph(paragraph(ts.DistanceGrid.MilliPoint(), Some(value)))) {
    32. case Ok(_) => io.writeLine("valid policy")
    33. case Err(_) => io.writeLine("unexpected error")
    34. }
    35. }
    Expected outputexit 0
    valid policy
    valid policy
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. valid policy
    2. valid policy
    Source evidence

    Reject ratios just outside every allowed endpoint

    Link
    main.tpzReject ratios just outside every allowed endpointOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. function paragraph(grid: ts.DistanceGrid, spacing: Option<ts.LatinSpacing>) -> ts.Paragraph {
    4. ts.Paragraph {
    5. grid: grid,
    6. items: [
    7. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "abc", width: 10pt, trackingAfter: 0pt }),
    8. ts.Item.PenaltyItem(ts.Penalty {
    9. unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(),
    10. kind: ts.BreakKind.ForcedEnd(), flagged: false
    11. })
    12. ],
    13. geometry: ts.LineGeometry {
    14. bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt
    15. },
    16. justification: ts.JustificationPolicy {
    17. mode: ts.JustificationMode.Unjustified(),
    18. minGapCount: 0, minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    19. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    20. latinSpacing: spacing, cjkStretchRatio: 0.0
    21. },
    22. costModel: ts.CostModel.FixedPoint(),
    23. knuthPlass: ts.KnuthPlassPolicy {
    24. toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0,
    25. flaggedDemerit: 0, finalFlaggedDemerit: 0
    26. }
    27. }
    28. }
    29. for value in ([ts.LatinSpacing { minWordSpaceRatio: 0.7999, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0em, fontSizePt: 12pt },
    30. ts.LatinSpacing { minWordSpaceRatio: 1.0001, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0em, fontSizePt: 12pt },
    31. ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 0.9999, maxLetterSpacingEm: 0em, fontSizePt: 12pt },
    32. ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.3301, maxLetterSpacingEm: 0em, fontSizePt: 12pt }]) {
    33. match (ts.breakParagraph(paragraph(ts.DistanceGrid.MilliPoint(), Some(value)))) {
    34. case Err(InvalidPolicy(_)) => io.writeLine("invalid ratio")
    35. case _ => io.writeLine("unexpected result")
    36. }
    37. }
    Expected outputexit 0
    invalid ratio
    invalid ratio
    invalid ratio
    invalid ratio
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. invalid ratio
    2. invalid ratio
    3. invalid ratio
    4. invalid ratio
    Source evidence

    Reject negative spacing and nonpositive fonts

    Link
    main.tpzReject negative spacing and nonpositive fontsOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. function paragraph(grid: ts.DistanceGrid, spacing: Option<ts.LatinSpacing>) -> ts.Paragraph {
    4. ts.Paragraph {
    5. grid: grid,
    6. items: [
    7. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "abc", width: 10pt, trackingAfter: 0pt }),
    8. ts.Item.PenaltyItem(ts.Penalty {
    9. unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(),
    10. kind: ts.BreakKind.ForcedEnd(), flagged: false
    11. })
    12. ],
    13. geometry: ts.LineGeometry {
    14. bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt
    15. },
    16. justification: ts.JustificationPolicy {
    17. mode: ts.JustificationMode.Unjustified(),
    18. minGapCount: 0, minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    19. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    20. latinSpacing: spacing, cjkStretchRatio: 0.0
    21. },
    22. costModel: ts.CostModel.FixedPoint(),
    23. knuthPlass: ts.KnuthPlassPolicy {
    24. toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0,
    25. flaggedDemerit: 0, finalFlaggedDemerit: 0
    26. }
    27. }
    28. }
    29. for value in ([ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: -0.0000152587890625em, fontSizePt: 12pt },
    30. ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0.0150146484375em, fontSizePt: 12pt },
    31. ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0em, fontSizePt: 0pt },
    32. ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0em, fontSizePt: -12pt }]) {
    33. match (ts.breakParagraph(paragraph(ts.DistanceGrid.MilliPoint(), Some(value)))) {
    34. case Err(InvalidPolicy(_)) => io.writeLine("invalid length policy")
    35. case _ => io.writeLine("unexpected result")
    36. }
    37. }
    Expected outputexit 0
    invalid length policy
    invalid length policy
    invalid length policy
    invalid length policy
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. invalid length policy
    2. invalid length policy
    3. invalid length policy
    4. invalid length policy
    Source evidence

    Read a stored policy before paragraph validation

    Link
    main.tpzRead a stored policy before paragraph validationOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let value = ts.LatinSpacing { minWordSpaceRatio: 0.0, maxWordSpaceRatio: 2.0, maxLetterSpacingEm: 1em, fontSizePt: 0pt }
    4. io.writeLine("{value.minWordSpaceRatio == 0.0}|{value.maxWordSpaceRatio == 2.0}|{value.maxLetterSpacingEm == 1em}|{value.fontSizePt == 0pt}")
    Expected outputexit 0
    true|true|true|true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true|true|true|true
    Source evidence

    Fill a line using a rounded bound for every letter gap

    Link
    main.tpzFill a line using a rounded bound for every letter gapOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. import std.test
    4. function paragraph(grid: ts.DistanceGrid, spacing: Option<ts.LatinSpacing>) -> ts.Paragraph {
    5. ts.Paragraph {
    6. grid: grid,
    7. items: [
    8. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 1pt, trackingAfter: 0pt }),
    9. ts.Item.BoxItem(ts.Box { unitIndex: 1, text: "b", width: 1pt, trackingAfter: 0pt }),
    10. ts.Item.BoxItem(ts.Box { unitIndex: 2, text: "c", width: 1pt, trackingAfter: 0pt }),
    11. ts.Item.PenaltyItem(ts.Penalty {
    12. unitEnd: 3, width: 0pt, rule: ts.BreakRule.Forced(),
    13. kind: ts.BreakKind.LatinSpace(), flagged: false
    14. }),
    15. ts.Item.BoxItem(ts.Box { unitIndex: 3, text: "xyz", width: 3.376pt, trackingAfter: 0pt }),
    16. ts.Item.PenaltyItem(ts.Penalty {
    17. unitEnd: 4, width: 0pt, rule: ts.BreakRule.Forced(),
    18. kind: ts.BreakKind.ForcedEnd(), flagged: false
    19. })
    20. ],
    21. geometry: ts.LineGeometry {
    22. bodyTarget: 3.376pt, firstTarget: 3.376pt, dropCapSpan: 0, dropCapInset: 0pt
    23. },
    24. justification: ts.JustificationPolicy {
    25. mode: ts.JustificationMode.LatinWordSpace(),
    26. minGapCount: 0, minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    27. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    28. latinSpacing: spacing, cjkStretchRatio: 0.0
    29. },
    30. costModel: ts.CostModel.FixedPoint(),
    31. knuthPlass: ts.KnuthPlassPolicy {
    32. toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0,
    33. flaggedDemerit: 0, finalFlaggedDemerit: 0
    34. }
    35. }
    36. }
    37. let value = ts.LatinSpacing { minWordSpaceRatio: 1.0, maxWordSpaceRatio: 1.0, maxLetterSpacingEm: 0.0078125em, fontSizePt: 24pt }
    38. let result = test.assertOk(ts.breakParagraph(paragraph(ts.DistanceGrid.MilliPoint(), Some(value))))
    39. let filled = match (result.optimized.boundedFillFeasible) {
    40. case Some(value) => value
    41. case None() => false
    42. }
    43. io.writeLine("{filled}|{result.optimized.lines.length}|{result.optimized.lines[0].target == 3.376pt}")
    Expected outputexit 0
    true|2|true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true|2|true
    Source evidence

    Resolve an em length with an explicit positive font size

    Link
    Ruletopaz.stdlib.contract.length-resolve.rule

    Resolve an em length with an explicit positive font size

    Length<Em> stores a signed 64-bit relative count with 65,536 ticks per em. Its inclusive count range is -9223372036854775808 through 9223372036854775807. Zero and negative relative lengths are valid.

    An em literal rounds its exact decimal magnitude to relative ticks with nearest-even rounding. Resolution uses those stored ticks and does not recover discarded literal precision.

    resolve takes one Length<Pt> font size and returns Length<Pt>. The font size must have a strictly positive scaled-point count. A one-scaled-point font is valid.

    If e is the stored em count and f is the stored font-size count, the physical result is roundEven of the exact rational e times f divided by 65,536. Multiply before rounding and round once to the physical grid.

    The result must fit the common signed 64-bit physical range after rounding. Overflow stops with runtime diagnostic TPZ4004. Exact intermediates must not create a smaller accepted domain.

    A zero or negative font size stops with runtime diagnostic TPZ4004, including when the relative length is zero. These failures are runtime faults and are not Result values.

    A font size with a different unit marker, an int, or a float is rejected with TPZ5001. A physical Length has no resolve member. Relative lengths have no direct physical conversion or integer grid member.

    The receiver evaluates once and then the font-size argument evaluates once. Resolution reads no ambient font state and performs no I/O or mutation. It preserves effects already caused by evaluating those inputs.

    After resolution, use the physical conversion methods and integer interchange operations. LatinSpacing uses this same resolution before the selected paragraph grid quantizes the per-gap distance.

    For 0.01em and 12pt, the relative count is 655 and the resolved count is 7,860 scaled points. Its nearest-even millipoint conversion is 120.

    Why

    An explicit positive font size gives a relative length one physical meaning. Shared integer rounding makes standalone lengths and letter-spacing budgets agree, including ties, negative offsets, and range boundaries.

    Source evidence

    Resolve zero and negative relative lengths with a positive font

    Link
    main.tpzResolve zero and negative relative lengths with a positive fontOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let negative = (-0.5em).resolve(12pt)
    4. let zero = (0em).resolve(ts.fromScaledPoints(1))
    5. let large = (140737488355327.9999847412109375em).resolve(ts.fromScaledPoints(1))
    6. io.writeLine("{negative == -6pt}|{zero == 0pt}|{large == ts.fromScaledPoints(140737488355328)}")
    Expected outputexit 0
    true|true|true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true|true|true
    Source evidence

    Evaluate the relative value and font size once in order

    Link
    main.tpzEvaluate the relative value and font size once in orderOpen in Playground
    1. import std.io
    2. function relative() -> Length<Em> {
    3. io.writeLine("relative")
    4. 0.5em
    5. }
    6. function font() -> Length<Pt> {
    7. io.writeLine("font")
    8. 12pt
    9. }
    10. let result = relative().resolve(font())
    11. io.writeLine("{result == 6pt}")
    Expected outputexit 0
    relative
    font
    true
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. relative
    2. font
    3. true
    Source evidence

    Reject a zero font size

    Link
    main.tpzReject a zero font sizeOpen in Playground
    1. let relative = 0em
    2. let font = 0pt
    3. relative.resolve(font)
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4004 stdlib-length-font-positive at main.tpz:3:1
    Source evidence

    Reject a negative font size

    Link
    main.tpzReject a negative font sizeOpen in Playground
    1. let relative = 1em
    2. let font = -12pt
    3. relative.resolve(font)
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4004 stdlib-length-font-positive at main.tpz:3:1
    Source evidence

    Reject an unrepresentable physical result

    Link
    main.tpzReject an unrepresentable physical resultOpen in Playground
    1. import std.typeset as ts
    2. let relative = 2em
    3. let font = ts.fromScaledPoints(9223372036854775807)
    4. relative.resolve(font)
    Expected outputNonzero exit
    No stdout.
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during run. Nonzero exit.

    • TPZ4004 stdlib-length-unrepresentable at main.tpz:4:1
    Source evidence

    Require a point font size

    Link
    main.tpzRequire a point font sizeOpen in Playground
    1. let relative = 1em
    2. relative.resolve(1mm)
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5001 stdlib-physical-distance-requires-points at main.tpz:2:18
    Source evidence

    Menu

    Download