Paragraph. justification: Justification Policy- Module
- std.
typeset - Receiver
- Paragraph
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
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
- compiler/crates/topaz_resolve/src/std/typeset/model.tpz at
b48527d0a174· 52 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 40
Receiver
Paragraph
Parameter constraints
breakParagraph evaluates its Paragraph argument through the ordinary call rules, then reads its item sequence and policies. Planning performs no input mutation, text shaping, external I/O, or random selection. The same ordered input and computation rules produce the same lines and scores.
Paragraph.items supplies measured BoxItem and GlueItem units with contiguous indices from zero. PenaltyItem entries refer to boundaries between those units. The planner uses these measurements and supplied penalties directly. It does not call classifyBreaks or insert a terminal penalty.
Paragraph.grid selects integer quantization at the computation boundary. Every public distance remains Length<Pt>. FixedPoint requires MilliPoint. KnuthPlass accepts either DistanceGrid. The ordinary checked length conversion applies before integer planning.
Policy validation precedes item validation. Geometry, the cost model and grid combination, justification values, and KnuthPlassPolicy are validated even when a chosen model does not use a policy field for scoring. Invalid configuration returns TypesetError.InvalidPolicy. Invalid unit indices, capacities, or penalty endpoints return TypesetError.InvalidInput.
A valid empty items array returns two empty plans with zero totalCost and zero breakOpportunityCount. This case still validates the policies. Each empty plan reports its selected grid and None() for boundedFillFeasible.
For nonempty input, the planner orders penalties by increasing unitEnd and preserves supplied order at equal boundaries. Allowed and Forced entries are candidates. Forbidden entries are excluded individually. Duplicate candidates remain distinct. A Forced entry requires the plan to visit its boundary, where another eligible entry may be selected.
No candidates returns TypesetError.NoBreakOpportunities. The planner constructs the greedy baseline before the optimized search. Failure to complete that baseline returns TypesetError.NoGreedyPlan. A completed baseline followed by an unsuccessful optimized search returns TypesetError.NoPlan. An Err result contains no partial ParagraphResult.
For FixedPoint, greedy chooses the farthest raw width that fits the next target before the first overwide candidate or Forced boundary. If the first candidate is already overwide, greedy chooses it to advance. For KnuthPlass, greedy chooses the last ratio-admissible candidate up to the next Forced boundary. Every chosen boundary advances past the previous one.
FixedPoint optimization starts with the preceding 80 candidates at each destination. It extends the search backward while the preceding span fits the larger base target plus available shrink and one grid unit. It retains the least score for each boundary and semantic state. It admits at most two consecutive LatinSoftHyphen breaks.
FixedPoint optimized admission uses trimmed text width plus the selected penalty width. The width limit is the target plus available shrink for a nonfinal line, with one grid unit of rounding allowance. A line containing one unit can exceed this limit so an indivisible unit can make progress. Greedy reports the same limit but selects candidates using its raw width rule.
With FixedPoint, a justified mode, and Some(latinSpacing), optimization first searches for a complete plan whose lines satisfy the bounded fill check. It prefers that plan when found. Otherwise it repeats the search without that check. This preference can select a plan with a higher totalCost than the greedy plan.
KnuthPlass optimization considers every preceding candidate and retains each distinct line geometry bucket, fitness class, and flagged state at a boundary. FixedPoint also retains its soft hyphen streak. State buckets distinguish drop cap lines and collapse after the inset ends.
Equal scores retain the first visited predecessor. Candidates are visited in boundary order, with supplied order at equal boundaries. States are visited by increasing line geometry bucket, soft hyphen streak, fitness class, and then unflagged before flagged. Final selection uses the same first-on-equal rule.
The optimized field reports the selected model search and admission policy. The greedy field reports its baseline from the same input. Each reported cost belongs to the plan selected by these candidate, state, and admission rules. Ordinary checked arithmetic faults remain faults and are not converted into a TypesetError value.
Related rule: Choose deterministic paragraph plansResult
The declared result type is Justification.
Effects
Failures
Result and effects
LinkSelect this field from a value of its named type without call parentheses.
The policy is always validated. FixedPoint uses it for bounded capacity, fill preference, and line scores. KnuthPlass uses explicit glue capacities for admission and scoring.
Why
LinkApplications need reproducible line boundaries and a clear choice between a bounded fixed point search and a Knuth Plass state search. Reporting both selected plans preserves the baseline for inspection. Keeping policy failure, missing candidates, and unsuccessful searches distinct lets a caller decide which input to revise.
Resolved contracts
LinkRelated entries
LinkChoose deterministic paragraph plans
LinkChoose deterministic paragraph plans
breakParagraph evaluates its Paragraph argument through the ordinary call rules, then reads its item sequence and policies. Planning performs no input mutation, text shaping, external I/O, or random selection. The same ordered input and computation rules produce the same lines and scores.
Paragraph.items supplies measured BoxItem and GlueItem units with contiguous indices from zero. PenaltyItem entries refer to boundaries between those units. The planner uses these measurements and supplied penalties directly. It does not call classifyBreaks or insert a terminal penalty.
Paragraph.grid selects integer quantization at the computation boundary. Every public distance remains Length<Pt>. FixedPoint requires MilliPoint. KnuthPlass accepts either DistanceGrid. The ordinary checked length conversion applies before integer planning.
Policy validation precedes item validation. Geometry, the cost model and grid combination, justification values, and KnuthPlassPolicy are validated even when a chosen model does not use a policy field for scoring. Invalid configuration returns TypesetError.InvalidPolicy. Invalid unit indices, capacities, or penalty endpoints return TypesetError.InvalidInput.
A valid empty items array returns two empty plans with zero totalCost and zero breakOpportunityCount. This case still validates the policies. Each empty plan reports its selected grid and None() for boundedFillFeasible.
For nonempty input, the planner orders penalties by increasing unitEnd and preserves supplied order at equal boundaries. Allowed and Forced entries are candidates. Forbidden entries are excluded individually. Duplicate candidates remain distinct. A Forced entry requires the plan to visit its boundary, where another eligible entry may be selected.
No candidates returns TypesetError.NoBreakOpportunities. The planner constructs the greedy baseline before the optimized search. Failure to complete that baseline returns TypesetError.NoGreedyPlan. A completed baseline followed by an unsuccessful optimized search returns TypesetError.NoPlan. An Err result contains no partial ParagraphResult.
For FixedPoint, greedy chooses the farthest raw width that fits the next target before the first overwide candidate or Forced boundary. If the first candidate is already overwide, greedy chooses it to advance. For KnuthPlass, greedy chooses the last ratio-admissible candidate up to the next Forced boundary. Every chosen boundary advances past the previous one.
FixedPoint optimization starts with the preceding 80 candidates at each destination. It extends the search backward while the preceding span fits the larger base target plus available shrink and one grid unit. It retains the least score for each boundary and semantic state. It admits at most two consecutive LatinSoftHyphen breaks.
FixedPoint optimized admission uses trimmed text width plus the selected penalty width. The width limit is the target plus available shrink for a nonfinal line, with one grid unit of rounding allowance. A line containing one unit can exceed this limit so an indivisible unit can make progress. Greedy reports the same limit but selects candidates using its raw width rule.
With FixedPoint, a justified mode, and Some(latinSpacing), optimization first searches for a complete plan whose lines satisfy the bounded fill check. It prefers that plan when found. Otherwise it repeats the search without that check. This preference can select a plan with a higher totalCost than the greedy plan.
KnuthPlass optimization considers every preceding candidate and retains each distinct line geometry bucket, fitness class, and flagged state at a boundary. FixedPoint also retains its soft hyphen streak. State buckets distinguish drop cap lines and collapse after the inset ends.
Equal scores retain the first visited predecessor. Candidates are visited in boundary order, with supplied order at equal boundaries. States are visited by increasing line geometry bucket, soft hyphen streak, fitness class, and then unflagged before flagged. Final selection uses the same first-on-equal rule.
The optimized field reports the selected model search and admission policy. The greedy field reports its baseline from the same input. Each reported cost belongs to the plan selected by these candidate, state, and admission rules. Ordinary checked arithmetic faults remain faults and are not converted into a TypesetError value.
Why
Applications need reproducible line boundaries and a clear choice between a bounded fixed point search and a Knuth Plass state search. Reporting both selected plans preserves the baseline for inspection. Keeping policy failure, missing candidates, and unsuccessful searches distinct lets a caller decide which input to revise.
Source evidence
- compiler/crates/topaz_resolve/src/std/typeset/paragraph.tpz at
f3807a34248e· 1-106 - compiler/crates/topaz_resolve/src/std/typeset/greedy.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/admission.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/measure.tpz at
f3807a34248e· 27-87 - compiler/crates/topaz_resolve/src/std/typeset/line.tpz at
f3807a34248e· 11-27 - compiler/crates/topaz_resolve/src/std/typeset/wide.tpz at
f3807a34248e· 1-28 -
topaz.stdlib.contract.typeset-paragraph-planningatworking-tree
Plan a paragraph through its supplied boundaries
Linkimport std.typeset as tsimport std.iolet items: Array<ts.Item> = [ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.BoxItem(ts.Box { unitIndex: 1, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 2, width: 0pt, rule: ts.BreakRule.Allowed(0), kind: ts.BreakKind.LatinSpace(), flagged: false }),ts.Item.BoxItem(ts.Box { unitIndex: 2, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.BoxItem(ts.Box { unitIndex: 3, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 4, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.ForcedEnd(), flagged: false })]let input = ts.Paragraph {grid: ts.DistanceGrid.MilliPoint(),items: items,geometry: ts.LineGeometry { bodyTarget: 2pt, firstTarget: 2pt, dropCapSpan: 0, dropCapInset: 0pt },justification: ts.JustificationPolicy {mode: ts.JustificationMode.Unjustified(), minGapCount: 0,minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,latinSpacing: None(), cjkStretchRatio: 0.0},costModel: ts.CostModel.FixedPoint(),knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }}match (ts.breakParagraph(input)) {case Ok(result) => {for line in (result.optimized.lines) {io.writeLine("{line.start} {line.endExclusive}")}io.writeLine("{result.greedy.lines.length} {result.breakOpportunityCount}")}case Err(_) => io.writeLine("unexpected error")}
0 2 2 4 2 2
Expected result
Expected result
Execution completes. Exit code 0.
0 22 42 2
Source evidence
- compiler/crates/topaz_resolve/src/std/typeset/paragraph.tpz at
f3807a34248e· 1-106 - compiler/crates/topaz_resolve/src/std/typeset/greedy.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/admission.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/measure.tpz at
f3807a34248e· 27-87 - compiler/crates/topaz_resolve/src/std/typeset/line.tpz at
f3807a34248e· 11-27 - compiler/crates/topaz_resolve/src/std/typeset/wide.tpz at
f3807a34248e· 1-28 -
topaz.stdlib.contract.typeset-paragraph-planningatworking-tree
Plan a valid empty paragraph
Linkimport std.typeset as tsimport std.iolet items: Array<ts.Item> = []let input = ts.Paragraph {grid: ts.DistanceGrid.MilliPoint(),items: items,geometry: ts.LineGeometry { bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt },justification: ts.JustificationPolicy {mode: ts.JustificationMode.Unjustified(), minGapCount: 0,minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,latinSpacing: None(), cjkStretchRatio: 0.0},costModel: ts.CostModel.FixedPoint(),knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }}match (ts.breakParagraph(input)) {case Ok(result) => {io.writeLine("{result.optimized.lines.length} {result.greedy.lines.length}")io.writeLine("{result.optimized.totalCost} {result.greedy.totalCost} {result.breakOpportunityCount}")}case Err(_) => io.writeLine("unexpected error")}
0 0 0 0 0
Expected result
Expected result
Execution completes. Exit code 0.
0 00 0 0
Source evidence
- compiler/crates/topaz_resolve/src/std/typeset/paragraph.tpz at
f3807a34248e· 1-106 - compiler/crates/topaz_resolve/src/std/typeset/greedy.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/admission.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/measure.tpz at
f3807a34248e· 27-87 - compiler/crates/topaz_resolve/src/std/typeset/line.tpz at
f3807a34248e· 11-27 - compiler/crates/topaz_resolve/src/std/typeset/wide.tpz at
f3807a34248e· 1-28 -
topaz.stdlib.contract.typeset-paragraph-planningatworking-tree
Report absent break opportunities
Linkimport std.typeset as tsimport std.iolet items: Array<ts.Item> = [ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 1pt, trackingAfter: 0pt })]let input = ts.Paragraph {grid: ts.DistanceGrid.MilliPoint(),items: items,geometry: ts.LineGeometry { bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt },justification: ts.JustificationPolicy {mode: ts.JustificationMode.Unjustified(), minGapCount: 0,minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,latinSpacing: None(), cjkStretchRatio: 0.0},costModel: ts.CostModel.FixedPoint(),knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }}match (ts.breakParagraph(input)) {case Err(NoBreakOpportunities()) => io.writeLine("NoBreakOpportunities")case _ => io.writeLine("unexpected result")}
NoBreakOpportunities
Expected result
Expected result
Execution completes. Exit code 0.
NoBreakOpportunities
Source evidence
- compiler/crates/topaz_resolve/src/std/typeset/paragraph.tpz at
f3807a34248e· 1-106 - compiler/crates/topaz_resolve/src/std/typeset/greedy.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/admission.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/measure.tpz at
f3807a34248e· 27-87 - compiler/crates/topaz_resolve/src/std/typeset/line.tpz at
f3807a34248e· 11-27 - compiler/crates/topaz_resolve/src/std/typeset/wide.tpz at
f3807a34248e· 1-28 -
topaz.stdlib.contract.typeset-paragraph-planningatworking-tree
Report a missing greedy completion
Linkimport std.typeset as tsimport std.iolet items: Array<ts.Item> = [ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.BoxItem(ts.Box { unitIndex: 1, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Allowed(0), kind: ts.BreakKind.ForcedEnd(), flagged: false })]let input = ts.Paragraph {grid: ts.DistanceGrid.MilliPoint(),items: items,geometry: ts.LineGeometry { bodyTarget: 10pt, firstTarget: 10pt, dropCapSpan: 0, dropCapInset: 0pt },justification: ts.JustificationPolicy {mode: ts.JustificationMode.Unjustified(), minGapCount: 0,minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,latinSpacing: None(), cjkStretchRatio: 0.0},costModel: ts.CostModel.FixedPoint(),knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }}match (ts.breakParagraph(input)) {case Err(NoGreedyPlan()) => io.writeLine("NoGreedyPlan")case _ => io.writeLine("unexpected result")}
NoGreedyPlan
Expected result
Expected result
Execution completes. Exit code 0.
NoGreedyPlan
Source evidence
- compiler/crates/topaz_resolve/src/std/typeset/paragraph.tpz at
f3807a34248e· 1-106 - compiler/crates/topaz_resolve/src/std/typeset/greedy.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/admission.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/measure.tpz at
f3807a34248e· 27-87 - compiler/crates/topaz_resolve/src/std/typeset/line.tpz at
f3807a34248e· 11-27 - compiler/crates/topaz_resolve/src/std/typeset/wide.tpz at
f3807a34248e· 1-28 -
topaz.stdlib.contract.typeset-paragraph-planningatworking-tree
Report three forced soft hyphens in a row
Linkimport std.typeset as tsimport std.iolet items: Array<ts.Item> = [ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.LatinSoftHyphen(), flagged: false }),ts.Item.BoxItem(ts.Box { unitIndex: 1, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 2, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.LatinSoftHyphen(), flagged: false }),ts.Item.BoxItem(ts.Box { unitIndex: 2, text: "a", width: 1pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 3, width: 0pt, rule: ts.BreakRule.Forced(), kind: ts.BreakKind.LatinSoftHyphen(), flagged: false })]let input = ts.Paragraph {grid: ts.DistanceGrid.MilliPoint(),items: items,geometry: ts.LineGeometry { bodyTarget: 1pt, firstTarget: 1pt, dropCapSpan: 0, dropCapInset: 0pt },justification: ts.JustificationPolicy {mode: ts.JustificationMode.Unjustified(), minGapCount: 0,minLineWidthRatioM: 0, minFinalLineWidthRatioM: None(),maxAdjustment: 0pt, maxGapAdjustment: 0pt, maxGapRatioR: 0,latinSpacing: None(), cjkStretchRatio: 0.0},costModel: ts.CostModel.FixedPoint(),knuthPlass: ts.KnuthPlassPolicy { toleranceR: 100000000, linePenalty: 10, fitnessDemerit: 0, flaggedDemerit: 0, finalFlaggedDemerit: 0 }}match (ts.breakParagraph(input)) {case Err(NoPlan()) => io.writeLine("NoPlan")case _ => io.writeLine("unexpected result")}
NoPlan
Expected result
Expected result
Execution completes. Exit code 0.
NoPlan
Source evidence
- compiler/crates/topaz_resolve/src/std/typeset/paragraph.tpz at
f3807a34248e· 1-106 - compiler/crates/topaz_resolve/src/std/typeset/greedy.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/admission.tpz at
f3807a34248e· 1-36 - compiler/crates/topaz_resolve/src/std/typeset/measure.tpz at
f3807a34248e· 27-87 - compiler/crates/topaz_resolve/src/std/typeset/line.tpz at
f3807a34248e· 11-27 - compiler/crates/topaz_resolve/src/std/typeset/wide.tpz at
f3807a34248e· 1-28 -
topaz.stdlib.contract.typeset-paragraph-planningatworking-tree