classify Breaks(clusters:Array<Break Cluster>, policy:Break Policy) - > Result<Array<Break Opportunity>, Typeset Error>- Модуль
- std.
typeset → - Получатель
- Неприменимо
- Минимальное число аргументов
- 2
- Максимальное число аргументов
- 2
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
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.
Получатель
Ограничения параметров
Break constructs the variant for the existing CJK and East Asian cluster classification. Break is absent and rejected.
Renaming this variant preserves the classification predicates and the effect of the selected Unicode, locale, custom punctuation, and shaping-boundary policies.
A CjkCluster category does not itself permit a break. classify returns the opportunities admitted by its supplied policy.
For the same ordered input values, policies, calculation grid, and product release, classifyBreaks and breakParagraph produce the same result. This includes boundary order, selected breaks, line widths, costs, and the returned error variant.
Planning reads measured text and policy values without changing caller arrays. It does not shape text, load fonts, or write output. Ordinary argument evaluation finishes before the operation begins.
The selected cost model, admission rules, and search bounds define plan quality. The two reported plans expose their own costs for inspection.
Связанное правило · Repeat a paragraph plan deterministically →classifyBreaks reads measured clusters in array order and returns Result<Array<Break. It does not shape text, normalize strings, split a cluster, or change any supplied record or array.
Each BreakCluster.text is the scalar string for one indivisible measured unit. Empty text still occupies one cluster index. advance includes trackingAfter. A span width sums its advances and removes the trackingAfter of its final cluster.
advance and advance minus trackingAfter must be nonnegative point lengths. Zero advance and signed trackingAfter are accepted. Invalid extents return TypesetError.InvalidInput. Length arithmetic follows the common Length contract.
classifyBreaks sums stored point ticks and compares them with the stored point targets. It has no DistanceGrid argument and does not quantize to a paragraph grid. Exact equality with a target counts as fitting.
boundaryAllowed has exactly one entry for each cluster. Entry i governs the boundary after cluster i. The last entry must be true for nonempty input. A wrong mask length or a false last entry returns TypesetError.InvalidInput.
Validation checks mask length and terminal permission, word targets, forbidBefore, forbidAfter, generated hyphen indices, and measured extents in that order. Array checks visit entries in input order. Unicode mandatory crossings are checked before boundary conflicts are visited in increasing cluster order. An empty result follows successful structural and policy validation.
An empty cluster array with a valid empty boundary mask returns Ok with an empty opportunity array. It does not create a synthetic unit or terminal opportunity.
Successful nonempty results contain increasing, unique unitEnd values from one through the cluster count. unitEnd is an exclusive cluster index rather than a byte or scalar offset. The final opportunity has unitEnd equal to the cluster count and kind ForcedEnd.
Repeated calls with equal cluster values and equal policies produce the same ordered opportunities and categories. Validation and classification do not read host locale, perform I/O, invoke caller callbacks, or mutate input values. Argument expressions use the ordinary language evaluation order.
A BreakOpportunity carries a boundary and its category. It does not carry a penalty width or BreakRule. A caller preserving mandatory boundaries maps every ForcedEnd opportunity to a Forced penalty before calling breakParagraph.
Связанное правило · Classify measured clusters into an ordered boundary sequence →Результат
Эффекты
Ошибки
Result and effects
СсылкаThe function validates its measured clusters and explicit break policy, then returns ordered opportunities. Empty input has an empty result. Nonempty successful results end with ForcedEnd. Invalid input and policy values are reported through TypesetError without mutating the supplied values.
Why
СсылкаMeasured clusters retain shaping boundaries while explicit Unicode and locale policies choose line-break opportunities. Their separation lets the same paragraph model serve Chinese, Japanese, Korean, and mixed text without an implicit language-dependent category name.
Related entries
СсылкаName the CJK cluster break category
СсылкаName the CJK cluster break category
Break constructs the variant for the existing CJK and East Asian cluster classification. Break is absent and rejected.
Renaming this variant preserves the classification predicates and the effect of the selected Unicode, locale, custom punctuation, and shaping-boundary policies.
A CjkCluster category does not itself permit a break. classify returns the opportunities admitted by its supplied policy.
Причина
A break category names the text it classifies. CjkCluster covers the existing Chinese, Japanese, and Korean classification path and matches the module's CjkInterCharacter vocabulary. Locale policy continues to determine where a break is allowed.
Use the CJK cluster variant
Ссылкаimport std.typeset as tslet kind: ts.BreakKind = ts.BreakKind.CjkCluster()
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Проверка принимает программу. Код завершения 0.
Reject the former KoreanCluster name
Ссылкаimport std.typeset as tslet kind = ts.BreakKind.KoreanCluster()
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-korean-cluster at
main.tpz:2:12
Diagnostics
СсылкаRepeat a paragraph plan deterministically
СсылкаRepeat a paragraph plan deterministically
For the same ordered input values, policies, calculation grid, and product release, classifyBreaks and breakParagraph produce the same result. This includes boundary order, selected breaks, line widths, costs, and the returned error variant.
Planning reads measured text and policy values without changing caller arrays. It does not shape text, load fonts, or write output. Ordinary argument evaluation finishes before the operation begins.
The selected cost model, admission rules, and search bounds define plan quality. The two reported plans expose their own costs for inspection.
Причина
Repeatable paragraph layout lets applications cache plans and reproduce published pages. An explicit search contract separates reproducibility from the quality of a chosen plan.
Retain a deterministic choice among equal cost candidates
Ссылкаimport std.typeset as tsimport std.ioimport std.testlet items: Array<ts.Item> = [ts.Item.BoxItem(ts.Box { unitIndex: 0, text: "word", width: 10pt, trackingAfter: 0pt }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Allowed(0), kind: ts.BreakKind.LatinSpace(), flagged: false }),ts.Item.PenaltyItem(ts.Penalty { unitEnd: 1, width: 0pt, rule: ts.BreakRule.Allowed(0), kind: ts.BreakKind.ClosingPunctuation(), 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 }}function category(kind: ts.BreakKind) -> string {match (kind) {case LatinSpace() => "space"case ClosingPunctuation() => "punctuation"case _ => "unexpected kind"}}for pass in ([0, 1]) {let result = test.assertOk(ts.breakParagraph(input))let optimized = result.optimized.lines[0]let greedy = result.greedy.lines[0]io.writeLine("{pass} {optimized.start} {optimized.endExclusive} {category(optimized.kind)} {category(greedy.kind)}")}
0 0 1 space punctuation
1 0 1 space punctuation
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
0 0 1 space punctuation1 0 1 space punctuation
Report the same first failing stage for repeated invalid input
Ссылкаimport std.typeset as tsimport std.iolet items: Array<ts.Item> = [ts.Item.BoxItem(ts.Box { unitIndex: 2, text: "a", width: 1pt, trackingAfter: 0pt })]let input = ts.Paragraph {grid: ts.DistanceGrid.MilliPoint(),items: items,geometry: ts.LineGeometry { bodyTarget: 0pt, firstTarget: 0pt, 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 }}for pass in ([0, 1]) {match (ts.breakParagraph(input)) {case Err(InvalidPolicy(_)) => io.writeLine("{pass} InvalidPolicy")case _ => io.writeLine("unexpected result")}}
0 InvalidPolicy
1 InvalidPolicy
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
0 InvalidPolicy1 InvalidPolicy
Classify measured clusters into an ordered boundary sequence
СсылкаClassify measured clusters into an ordered boundary sequence
classifyBreaks reads measured clusters in array order and returns Result<Array<Break. It does not shape text, normalize strings, split a cluster, or change any supplied record or array.
Each BreakCluster.text is the scalar string for one indivisible measured unit. Empty text still occupies one cluster index. advance includes trackingAfter. A span width sums its advances and removes the trackingAfter of its final cluster.
advance and advance minus trackingAfter must be nonnegative point lengths. Zero advance and signed trackingAfter are accepted. Invalid extents return TypesetError.InvalidInput. Length arithmetic follows the common Length contract.
classifyBreaks sums stored point ticks and compares them with the stored point targets. It has no DistanceGrid argument and does not quantize to a paragraph grid. Exact equality with a target counts as fitting.
boundaryAllowed has exactly one entry for each cluster. Entry i governs the boundary after cluster i. The last entry must be true for nonempty input. A wrong mask length or a false last entry returns TypesetError.InvalidInput.
Validation checks mask length and terminal permission, word targets, forbidBefore, forbidAfter, generated hyphen indices, and measured extents in that order. Array checks visit entries in input order. Unicode mandatory crossings are checked before boundary conflicts are visited in increasing cluster order. An empty result follows successful structural and policy validation.
An empty cluster array with a valid empty boundary mask returns Ok with an empty opportunity array. It does not create a synthetic unit or terminal opportunity.
Successful nonempty results contain increasing, unique unitEnd values from one through the cluster count. unitEnd is an exclusive cluster index rather than a byte or scalar offset. The final opportunity has unitEnd equal to the cluster count and kind ForcedEnd.
Repeated calls with equal cluster values and equal policies produce the same ordered opportunities and categories. Validation and classification do not read host locale, perform I/O, invoke caller callbacks, or mutate input values. Argument expressions use the ordinary language evaluation order.
A BreakOpportunity carries a boundary and its category. It does not carry a penalty width or BreakRule. A caller preserving mandatory boundaries maps every ForcedEnd opportunity to a Forced penalty before calling breakParagraph.
Причина
A measured cluster is the unit that shaping can safely keep together. Exclusive indices connect classification to paragraph items without converting through text offsets. A complete terminal boundary and deterministic order make the output suitable for reproducible layout.
Classify atomic clusters in repeatable boundary order
Ссылкаimport std.typeset as tsimport std.ioimport std.testlet clusters = [ts.BreakCluster { text: "한", advance: 1pt, trackingAfter: 0pt }, ts.BreakCluster { text: "글", advance: 1pt, trackingAfter: 0pt }]let selected = ts.BreakPolicy {mode: ts.BreakMode.Character(),rules: ts.KinsokuRules.AtlasDefault(),forbidBefore: [],forbidAfter: [],boundaryAllowed: [true, true],generatedHyphenEnds: [],protectFittingLatin: false,preserveShortWords: false,narrowestWordTarget: 0pt,bodyWordTarget: 0pt}for pass in ([0, 1]) {let breaks = test.assertOk(ts.classifyBreaks(clusters, selected))for boundary in (breaks) {let category = match (boundary.kind) {case CjkCluster() => "CJK"case ForcedEnd() => "end"case _ => "unexpected kind"}io.writeLine("{pass} {boundary.unitEnd} {category}")}}io.writeLine(clusters[0].text)
0 1 CJK
0 2 end
1 1 CJK
1 2 end
한
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
0 1 CJK0 2 end1 1 CJK1 2 end한
Classify an empty cluster sequence
Ссылкаimport std.typeset as tsimport std.ioimport std.testlet clusters: Array<ts.BreakCluster> = []let selected = ts.BreakPolicy {mode: ts.BreakMode.Character(),rules: ts.KinsokuRules.AtlasDefault(),forbidBefore: [],forbidAfter: [],boundaryAllowed: [],generatedHyphenEnds: [],protectFittingLatin: false,preserveShortWords: false,narrowestWordTarget: 0pt,bodyWordTarget: 0pt}let breaks = test.assertOk(ts.classifyBreaks(clusters, selected))io.writeLine("{breaks.length}")
0
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
0
Reject a mask with the wrong number of entries
Ссылкаimport std.typeset as tsimport std.ioimport std.testfunction outcome(value: Result<Array<ts.BreakOpportunity>, ts.TypesetError>) -> string {match (value) {case Ok(_) => "Ok"case Err(InvalidInput(_)) => "InvalidInput"case Err(InvalidPolicy(_)) => "InvalidPolicy"case Err(_) => "unexpected error"}}let clusters = [ts.BreakCluster { text: "a", advance: 1pt, trackingAfter: 0pt }]let selected = ts.BreakPolicy {mode: ts.BreakMode.Character(),rules: ts.KinsokuRules.AtlasDefault(),forbidBefore: [],forbidAfter: [],boundaryAllowed: [],generatedHyphenEnds: [],protectFittingLatin: false,preserveShortWords: false,narrowestWordTarget: 0pt,bodyWordTarget: 0pt}io.writeLine(outcome(ts.classifyBreaks(clusters, selected)))
InvalidInput
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
InvalidInput
Reject negative measured and untracked cluster extents
Ссылкаimport std.typeset as tsimport std.ioimport std.testfunction outcome(value: Result<Array<ts.BreakOpportunity>, ts.TypesetError>) -> string {match (value) {case Ok(_) => "Ok"case Err(InvalidInput(_)) => "InvalidInput"case Err(InvalidPolicy(_)) => "InvalidPolicy"case Err(_) => "unexpected error"}}let selected = ts.BreakPolicy {mode: ts.BreakMode.Character(),rules: ts.KinsokuRules.AtlasDefault(),forbidBefore: [],forbidAfter: [],boundaryAllowed: [true],generatedHyphenEnds: [],protectFittingLatin: false,preserveShortWords: false,narrowestWordTarget: 0pt,bodyWordTarget: 0pt}io.writeLine(outcome(ts.classifyBreaks([ts.BreakCluster { text: "a", advance: -1pt, trackingAfter: 0pt }], selected)))io.writeLine(outcome(ts.classifyBreaks([ts.BreakCluster { text: "b", advance: 1pt, trackingAfter: 2pt }], selected)))
InvalidInput
InvalidInput
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
InvalidInputInvalidInput
Reject a masked paragraph end in every ruleset
Ссылкаimport std.typeset as tsimport std.ioimport std.testfunction outcome(value: Result<Array<ts.BreakOpportunity>, ts.TypesetError>) -> string {match (value) {case Ok(_) => "Ok"case Err(InvalidInput(_)) => "InvalidInput"case Err(InvalidPolicy(_)) => "InvalidPolicy"case Err(_) => "unexpected error"}}let clusters = [ts.BreakCluster { text: "a", advance: 1pt, trackingAfter: 0pt }]let atlas = ts.BreakPolicy {mode: ts.BreakMode.Character(),rules: ts.KinsokuRules.AtlasDefault(),forbidBefore: [],forbidAfter: [],boundaryAllowed: [false],generatedHyphenEnds: [],protectFittingLatin: false,preserveShortWords: false,narrowestWordTarget: 0pt,bodyWordTarget: 0pt}let unicode = ts.BreakPolicy {mode: ts.BreakMode.Character(),rules: ts.KinsokuRules.Unicode16Ja(),forbidBefore: [],forbidAfter: [],boundaryAllowed: [false],generatedHyphenEnds: [],protectFittingLatin: false,preserveShortWords: false,narrowestWordTarget: 0pt,bodyWordTarget: 0pt}io.writeLine(outcome(ts.classifyBreaks(clusters, atlas)))io.writeLine(outcome(ts.classifyBreaks(clusters, unicode)))
InvalidInput
InvalidInput
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
InvalidInputInvalidInput
Keep exact point measurements at the classifier boundary
Ссылкаimport std.typeset as tsimport std.testimport std.iolet clusters = [ts.BreakCluster { text: "a", advance: 0.5002pt, trackingAfter: 0pt },ts.BreakCluster { text: "b", advance: 0.5002pt, trackingAfter: 0pt }]let policy = ts.BreakPolicy {mode: ts.BreakMode.Word(), rules: ts.KinsokuRules.AtlasDefault(),forbidBefore: [], forbidAfter: [], boundaryAllowed: [true, true],generatedHyphenEnds: [], protectFittingLatin: false, preserveShortWords: false,narrowestWordTarget: 0pt, bodyWordTarget: 1pt}let result = test.assertOk(ts.classifyBreaks(clusters, policy))for boundary in (result) {io.writeLine("{boundary.unitEnd}")}
1
2
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
12