Skip to content

Commit 5e8e735

Browse files
committed
use correct word-spacing when reshaping after break
man, this was hard to repro, probably would've rarely even happened
1 parent 96c48dd commit 5e8e735

3 files changed

Lines changed: 22 additions & 33 deletions

File tree

src/layout-flow.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -937,38 +937,12 @@ export class BlockContainerOfInlines extends BlockContainerBase {
937937
}
938938

939939
getRunIndex(layout: Layout, ci: number) {
940-
let l = this.treeStart, r = this.treeFinal;
941-
942-
while (true) {
943-
const i = Math.floor((l + r) / 2);
944-
945-
if (layout.tree[i].isRun()) {
946-
if (ci < layout.tree[i].textStart) {
947-
r = i - 1;
948-
} else if (ci >= layout.tree[i].textEnd) {
949-
l = i + 1;
950-
} else {
951-
return i;
952-
}
953-
} else if (layout.tree[i].isInline()) {
954-
if (ci < layout.tree[i].textStart) {
955-
r = i - 1;
956-
} else {
957-
l = i + 1;
958-
}
959-
} else { // inline-block, float, or image. pick a side.
960-
let ml = i;
961-
let mr = i;
962-
963-
while (mr < r && !layout.tree[mr].isRun() && !layout.tree[mr].isInline()) mr++;
964-
while (ml > l && !layout.tree[ml].isRun() && !layout.tree[ml].isInline()) ml--;
965-
966-
const item = layout.tree[mr];
967-
if ((item.isRun() || item.isInline()) && ci < item.textStart) {
968-
r = ml;
969-
} else {
970-
l = mr;
971-
}
940+
for (let i = this.treeStart + 2; i <= this.treeFinal; i++) {
941+
const item = layout.tree[i];
942+
if (item.isFormattingBox()) {
943+
i = item.treeFinal;
944+
} else if (item.isRun() && ci >= item.textStart && ci < item.textEnd) {
945+
return i;
972946
}
973947
}
974948
}

src/layout-text.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2398,10 +2398,11 @@ function splitItem(
23982398
ifc.block.items.splice(mark.itemIndex + 1, 0, right);
23992399

24002400
if (needsReshape) {
2401-
const inlineIndex = ifc.block.getRunIndex(layout, left.offset);
24022401
left.reshape(true);
24032402
right.reshape(false);
24042403
if (left.mayHaveModifiedWordSepGlyphs(layout)) {
2404+
const inlineIndex = ifc.block.getRunIndex(layout, left.offset);
2405+
if (inlineIndex === undefined) throw new Error('Assertion failed');
24052406
postShapeAddWordSpacing(layout, ifc.block, ifc.block.items, inlineIndex, mark.itemIndex, mark.itemIndex + 2);
24062407
}
24072408
}

test/text.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,20 @@ describe('Word Spacing', function () {
16641664
const block = this.get('#t');
16651665
expect(block.items[0].measure().advance).to.equal(50);
16661666
});
1667+
1668+
it('re-adds spaces when it needs to reshape', function () {
1669+
this.reflow(`
1670+
<div style="word-spacing: 10px; width: 260px;">
1671+
كلمة
1672+
<span style="display: inline-block; word-spacing: 100px;">كلمة كلمة</span>
1673+
كل&shy;مة
1674+
</div>
1675+
`);
1676+
1677+
/** @type import('../src/layout-flow.ts').BlockContainerOfInlines */
1678+
const [,, item] = this.get('div').items;
1679+
expect(item.measure().advance).to.equal(35.408);
1680+
});
16671681
});
16681682

16691683
describe('Vertical Align', function () {

0 commit comments

Comments
 (0)