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

    KnuthPlassPolicy.linePenalty

    Set the nonnegative base of each line demerit.

    KnuthPlassPolicy.linePenalty: int
    Receiver
    KnuthPlassPolicy
    Minimum arity
    0
    Maximum arity
    0

    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

    KnuthPlassPolicy

    Parameter constraints

    Every KnuthPlassPolicy field is a nonnegative int. A negative toleranceR, linePenalty, fitnessDemerit, flaggedDemerit, or finalFlaggedDemerit returns TypesetError.InvalidPolicy. Validation applies even when the selected CostModel is FixedPoint.

    KnuthPlass uses explicit GlueItem stretch and shrink. JustificationPolicy values are validated but do not replace these capacities or its ratio score. Leading glue is discarded for the natural width and capacities. An ordinary break discards trailing glue, while a Forced break consumes trailing glue so a caller can represent a ragged final line.

    Let slack be the target minus natural text width and selected penalty width. Nonnegative slack uses total stretch and negative slack uses total shrink. Zero capacity admits an exact fit and rejects a nonzero slack. Shrink admits ratios down to negative one. Positive ratios must be no greater than toleranceR divided by 100000000.

    The reported adjustment ratio is scaled by 100000000 and rounded to nearest with ties to even. Admission and fitness use the exact ratio before rounding. Badness is 100 times the absolute ratio cubed, rounded to nearest with ties to even and capped at 10000.

    Add linePenalty and badness, then square their sum for the basic line score. An Allowed penalty adds its squared value when nonnegative and subtracts its squared value when negative. Forced penalties add no such term. The resulting score is a signed checked int.

    The four fitness classes are ratios below negative one half, ratios from negative one half through one half, ratios above one half through one, and ratios above one. Add fitnessDemerit when consecutive lines differ by more than one class. The first line has no preceding fitness transition.

    Add flaggedDemerit when both the preceding and selected penalties are flagged. Add finalFlaggedDemerit when the final line follows a flagged penalty, whether or not its own penalty is flagged. The first line has no preceding flag.

    An exact fit with linePenalty equal to 10, zero penalty cost, and no transition demerit has line cost 100 and adjustmentRatioR equal to zero. A nonexact line with no usable stretch or shrink is inadmissible, including a final line whose caller supplied no terminal capacity.

    Result

    The declared result type is int.

    Effects

    Failures

    Result and effects

    Link

    Select this field from a value of its named type without call parentheses.

    Add linePenalty to cubic badness and square the sum before applying break and transition demerits.

    Why

    Link

    Tolerance controls acceptable adjustment and demerits control the relative cost of adjacent lines. Explicit glue capacities make final line behavior and flagged transitions reproducible without hidden default glue or a separate final line algorithm.

    Resolved contracts

    Link

    Related entries

    Link

    std.typeset

    Set Knuth Plass tolerance and transition costs

    Link

    Set Knuth Plass tolerance and transition costs

    Every KnuthPlassPolicy field is a nonnegative int. A negative toleranceR, linePenalty, fitnessDemerit, flaggedDemerit, or finalFlaggedDemerit returns TypesetError.InvalidPolicy. Validation applies even when the selected CostModel is FixedPoint.

    KnuthPlass uses explicit GlueItem stretch and shrink. JustificationPolicy values are validated but do not replace these capacities or its ratio score. Leading glue is discarded for the natural width and capacities. An ordinary break discards trailing glue, while a Forced break consumes trailing glue so a caller can represent a ragged final line.

    Let slack be the target minus natural text width and selected penalty width. Nonnegative slack uses total stretch and negative slack uses total shrink. Zero capacity admits an exact fit and rejects a nonzero slack. Shrink admits ratios down to negative one. Positive ratios must be no greater than toleranceR divided by 100000000.

    The reported adjustment ratio is scaled by 100000000 and rounded to nearest with ties to even. Admission and fitness use the exact ratio before rounding. Badness is 100 times the absolute ratio cubed, rounded to nearest with ties to even and capped at 10000.

    Add linePenalty and badness, then square their sum for the basic line score. An Allowed penalty adds its squared value when nonnegative and subtracts its squared value when negative. Forced penalties add no such term. The resulting score is a signed checked int.

    The four fitness classes are ratios below negative one half, ratios from negative one half through one half, ratios above one half through one, and ratios above one. Add fitnessDemerit when consecutive lines differ by more than one class. The first line has no preceding fitness transition.

    Add flaggedDemerit when both the preceding and selected penalties are flagged. Add finalFlaggedDemerit when the final line follows a flagged penalty, whether or not its own penalty is flagged. The first line has no preceding flag.

    An exact fit with linePenalty equal to 10, zero penalty cost, and no transition demerit has line cost 100 and adjustmentRatioR equal to zero. A nonexact line with no usable stretch or shrink is inadmissible, including a final line whose caller supplied no terminal capacity.

    Why

    Tolerance controls acceptable adjustment and demerits control the relative cost of adjacent lines. Explicit glue capacities make final line behavior and flagged transitions reproducible without hidden default glue or a separate final line algorithm.

    Source evidence

    Score an exact Knuth Plass line

    Link
    main.tpzScore an exact Knuth Plass lineOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let items: Array<ts.Item> = [
    4. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 10pt, trackingAfter: 0pt }),
    5. ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.ForcedEnd(), flagged: false })
    6. ]
    7. let input = ts.Paragraph {
    8. grid: ts.DistanceGrid.MilliPoint(),
    9. items: items,
    10. geometry: ts.LineGeometry { bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt },
    11. justification: ts.JustificationPolicy {
    12. mode: ts.JustificationMode.Unjustified(), minGapCount: 0,
    13. minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    14. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    15. latinSpacing: None(), cjkStretchRatio: 0.0
    16. },
    17. costModel: ts.CostModel.KnuthPlass(),
    18. knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }
    19. }
    20. match (ts.breakParagraph(input)) {
    21. case Ok(result) => {
    22. let line = result.optimized.lines[0]
    23. io.writeLine("{line.cost} {line.adjustmentRatioR} {result.optimized.totalCost}")
    24. match (line.postValidationWidthLimit) {
    25. case None() => io.writeLine("ratio admission")
    26. case Some(_) => io.writeLine("unexpected width limit")
    27. }
    28. }
    29. case Err(_) => io.writeLine("unexpected error")
    30. }
    Expected outputexit 0
    100 0 100
    ratio admission
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 100 0 100
    2. ratio admission
    Source evidence

    Validate Knuth Plass policy before choosing a cost model

    Link
    main.tpzValidate Knuth Plass policy before choosing a cost modelOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let items: Array<ts.Item> = [
    4. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 1pt, trackingAfter: 0pt }),
    5. ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.ForcedEnd(), flagged: false })
    6. ]
    7. let input = ts.Paragraph {
    8. grid: ts.DistanceGrid.MilliPoint(),
    9. items: items,
    10. geometry: ts.LineGeometry { bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt },
    11. justification: ts.JustificationPolicy {
    12. mode: ts.JustificationMode.Unjustified(), minGapCount: 0,
    13. minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    14. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    15. latinSpacing: None(), cjkStretchRatio: 0.0
    16. },
    17. costModel: ts.CostModel.FixedPoint(),
    18. knuthPlass: ts.KnuthPlassPolicy { toleranceR: -1, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }
    19. }
    20. match (ts.breakParagraph(input)) {
    21. case Err(InvalidPolicy(_)) => io.writeLine("InvalidPolicy")
    22. case _ => io.writeLine("unexpected result")
    23. }
    Expected outputexit 0
    InvalidPolicy
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. InvalidPolicy
    Source evidence

    Report a line with no capacity to reach its target

    Link
    main.tpzReport a line with no capacity to reach its targetOpen in Playground
    1. import std.typeset as ts
    2. import std.io
    3. let items: Array<ts.Item> = [
    4. ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 9pt, trackingAfter: 0pt }),
    5. ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.ForcedEnd(), flagged: false })
    6. ]
    7. let input = ts.Paragraph {
    8. grid: ts.DistanceGrid.MilliPoint(),
    9. items: items,
    10. geometry: ts.LineGeometry { bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt },
    11. justification: ts.JustificationPolicy {
    12. mode: ts.JustificationMode.Unjustified(), minGapCount: 0,
    13. minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),
    14. maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,
    15. latinSpacing: None(), cjkStretchRatio: 0.0
    16. },
    17. costModel: ts.CostModel.KnuthPlass(),
    18. knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }
    19. }
    20. match (ts.breakParagraph(input)) {
    21. case Err(NoGreedyPlan()) => io.writeLine("NoGreedyPlan")
    22. case _ => io.writeLine("unexpected result")
    23. }
    Expected outputexit 0
    NoGreedyPlan
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. NoGreedyPlan
    Source evidence

    Menu

    Download