Skip to content

Commit bd2f35d

Browse files
committed
temp
1 parent a012a12 commit bd2f35d

7 files changed

Lines changed: 758 additions & 717 deletions

File tree

examples/images-1.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const rootElement = flow.dom(
4040
flow.h('html', {style: rootStyle, attrs: {'x-dropflow-log': 'true'}}, [
4141
flow.h('img', {
4242
style: image1Style,
43-
attrs: {src: 'https://chearon.github.io/dropflow/assets/images/frogmage.gif'}
43+
attrs: {id: 'img', src: 'file:///Users/caleb/Code/dropflow/assets/images/frogmage.gif'}
4444
}),
4545
flow.h('p', `
4646
Dropflow now supports images! These are loaded by calling flow.load()
@@ -57,7 +57,7 @@ const rootElement = flow.dom(
5757
of Ada Lovelace, the well-tempered Great Pyrenees mix as an inline image: `,
5858
flow.h('img', {
5959
style: image2Style,
60-
attrs: {src: 'https://chearon.github.io/dropflow/assets/images/ada.png'}
60+
attrs: {src: 'file:///Users/caleb/Code/dropflow/assets/images/ada.png'}
6161
}),
6262
' after'
6363
]),
@@ -66,12 +66,11 @@ const rootElement = flow.dom(
6666
`),
6767
flow.h('img', {
6868
style: image3Style,
69-
attrs: {src: 'https://chearon.github.io/dropflow/assets/images/tiramisu.jpeg'}
69+
attrs: {src: 'file:///Users/caleb/Code/dropflow/assets/images/tiramisu.jpeg'}
7070
})
7171
])
7272
);
7373

74-
7574
// Normal layout, logging
7675
await flow.load(rootElement);
7776
const layout = flow.layout(rootElement);

examples/rtl-1.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@ flow.fonts.add(flow.createFaceFromTablesSync(p('Cairo/Cairo-Regular.ttf')));
88
flow.fonts.add(flow.createFaceFromTablesSync(p('Raleway/Raleway-Regular.ttf')));
99

1010
const rootElement = parse(`
11-
<div style="background-color: #ccc; zoom: 2;" x-dropflow-log>
12-
<div style="line-height: 2.5; direction: rtl;" x-dropflow-log>
13-
<span style="background-color: red; color: white; border: 2px solid green;">
14-
أجمل التهاني بمناسبة الميلاد
15-
</span>
16-
</div>
17-
<span style="background-color: red; color: white; border: 2px solid green;">
18-
(ajmil at-tihānī bimunāsabah al-mīlād)
19-
</span>
20-
<div x-dropflow-log>
21-
عربي
22-
ع<span style="color: blue;">ر</span>ب<span style="color: red;">ي</span>
23-
</div>
11+
<div style="zoom: 2;" x-dropflow-log>
12+
<span style="background-color: veronicayellow;">Why</span> is Sommer so
13+
beautiful? <span style="background-color: veronicayellow; padding: 5px;">They say it's
14+
because they spilled <span style="background-color: purple; color: white;">
15+
pretty girl juice</span> when they made her</span>. What do you think?
16+
<span style="color: #afe; background-color: #666; margin: 10px;">Let us know in the comments!</span>
2417
</div>
2518
`);
2619

@@ -30,7 +23,7 @@ const layout = flow.layout(rootElement);
3023

3124
flow.log(layout);
3225

33-
const canvas = createCanvas(200, 600);
26+
const canvas = createCanvas(300, 400);
3427
flow.reflow(layout, canvas.width, canvas.height);
3528
const ctx = canvas.getContext('2d');
3629
flow.paintToCanvas(layout, ctx);

src/api.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@ export function staticLayoutContribution(layout: Layout, box: BlockContainer): n
225225
}
226226

227227
if (box.isBlockContainerOfInlines()) {
228-
for (const line of box.lineboxes) {
229-
intrinsicSize = Math.max(intrinsicSize, line.width);
230-
}
228+
intrinsicSize = Math.max(intrinsicSize, box.intrinsicISize);
231229
// TODO: floats
232230
} else {
233231
for (let i = box.treeStart + 1; i <= box.treeFinal; i++) {

src/layout-flow.ts

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
getIfcContribution,
1111
createIfcShapedItems,
1212
createIfcLineboxes,
13-
positionIfcItems,
1413
sliceIfcRenderText,
1514
collapseWhitespace
1615
} from './layout-text.ts';
@@ -830,16 +829,18 @@ export class BlockContainerOfInlines extends BlockContainerBase {
830829
text: string;
831830
buffer: AllocatedUint16Array;
832831
items: ShapedItem[];
833-
lineboxes: Linebox[];
834-
fragments: Map<Inline, InlineFragment[]>;
832+
fragments: InlineFragment[];
833+
lastBaseline: number;
834+
intrinsicISize: number;
835835

836836
constructor(style: Style, attrs: number) {
837837
super(style, attrs);
838838
this.text = '';
839839
this.buffer = EmptyBuffer;
840840
this.items = [];
841-
this.lineboxes = [];
842-
this.fragments = new Map();
841+
this.fragments = [];
842+
this.lastBaseline = 0;
843+
this.intrinsicISize = 0;
843844
}
844845

845846
prelayoutPostorder(layout: Layout, ctx: PrelayoutContext) {
@@ -849,7 +850,7 @@ export class BlockContainerOfInlines extends BlockContainerBase {
849850
this.buffer.destroy();
850851
this.buffer = createIfcBuffer(this.text);
851852
this.items = createIfcShapedItems(layout, this, inline);
852-
this.fragments.clear();
853+
this.fragments = [];
853854
}
854855
}
855856

@@ -908,14 +909,11 @@ export class BlockContainerOfInlines extends BlockContainerBase {
908909
}
909910
}
910911

911-
for (const [inline, fragments] of this.fragments) {
912-
const {dx, dy} = inlineShifts.get(inline)!;
913-
914-
for (const fragment of fragments) {
915-
fragment.blockOffset += contentArea.y + dy;
916-
fragment.start += contentArea.x + dx;
917-
fragment.end += contentArea.x + dx;
918-
}
912+
for (const fragment of this.fragments) {
913+
const {dx, dy} = inlineShifts.get(fragment.inline)!;
914+
fragment.blockOffset += contentArea.y + dy;
915+
fragment.left += contentArea.x + dx;
916+
fragment.right += contentArea.x + dx;
919917
}
920918
}
921919

@@ -956,6 +954,7 @@ export class BlockContainerOfInlines extends BlockContainerBase {
956954
let ml = i;
957955
let mr = i;
958956

957+
// TODO this is wrong!
959958
while (mr < r && !layout.tree[mr].isRun() && !layout.tree[mr].isInline()) mr++;
960959
while (ml > l && !layout.tree[ml].isRun() && !layout.tree[ml].isInline()) ml--;
961960

@@ -977,15 +976,6 @@ export class BlockContainerOfInlines extends BlockContainerBase {
977976
return sliceIfcRenderText(layout, this, item, start, end);
978977
}
979978

980-
getLineboxHeight() {
981-
if (this.lineboxes.length) {
982-
const line = this.lineboxes.at(-1)!;
983-
return line.blockOffset + line.height();
984-
} else {
985-
return 0;
986-
}
987-
}
988-
989979
shouldLayoutContent(layout: Layout) {
990980
const inline = layout.tree[this.treeStart + 1];
991981
if (!inline.isInline()) throw new Error('Assertion failed');
@@ -998,10 +988,12 @@ export class BlockContainerOfInlines extends BlockContainerBase {
998988
doTextLayout(layout: Layout, ctx: LayoutContext) {
999989
const blockSize = this.style.getBlockSize(this.getContainingBlock());
1000990
if (this.shouldLayoutContent(layout)) {
1001-
this.lineboxes = createIfcLineboxes(layout, this, ctx);
1002-
positionIfcItems(layout, this);
991+
const ifc = createIfcLineboxes(layout, this, ctx);
992+
if (blockSize === 'auto' && ifc.lines.length) {
993+
const line = ifc.lines[ifc.lines.length - 1];
994+
this.setBlockSize( line.blockOffset + line.height());
995+
}
1003996
}
1004-
if (blockSize === 'auto') this.setBlockSize(this.getLineboxHeight());
1005997
}
1006998
}
1007999

@@ -1384,23 +1376,22 @@ export class Inline extends Box {
13841376
return this.style.hasLineRightGap(this.getContainingBlock());
13851377
}
13861378

1387-
getInlineSideSize(side: 'pre' | 'post') {
1379+
getInlineStartSize() {
13881380
const direction = this.getDirectionAsParticipant();
13891381
const containingBlock = this.getContainingBlock();
1390-
if (
1391-
direction === 'ltr' && side === 'pre' ||
1392-
direction === 'rtl' && side === 'post'
1393-
) {
1394-
const marginLineLeft = this.style.getMarginLineLeft(containingBlock);
1395-
return (marginLineLeft === 'auto' ? 0 : marginLineLeft)
1396-
+ this.style.getBorderLineLeftWidth(containingBlock)
1397-
+ this.style.getPaddingLineLeft(containingBlock);
1398-
} else {
1399-
const marginLineRight = this.style.getMarginLineRight(containingBlock);
1400-
return (marginLineRight === 'auto' ? 0 : marginLineRight)
1401-
+ this.style.getBorderLineRightWidth(containingBlock)
1402-
+ this.style.getPaddingLineRight(containingBlock);
1403-
}
1382+
const marginStart = this.style.getMarginInlineStart(containingBlock, direction);
1383+
return (marginStart === 'auto' ? 0 : marginStart)
1384+
+ this.style.getBorderInlineStartWidth(containingBlock, direction)
1385+
+ this.style.getPaddingInlineStart(containingBlock, direction);
1386+
}
1387+
1388+
getInlineEndSize() {
1389+
const direction = this.getDirectionAsParticipant();
1390+
const containingBlock = this.getContainingBlock();
1391+
const marginEnd = this.style.getMarginInlineEnd(containingBlock, direction);
1392+
return (marginEnd === 'auto' ? 0 : marginEnd)
1393+
+ this.style.getBorderInlineEndWidth(containingBlock, direction)
1394+
+ this.style.getPaddingInlineEnd(containingBlock, direction);
14041395
}
14051396

14061397
isInline(): this is Inline {
@@ -1463,9 +1454,12 @@ export class ReplacedBox extends FormattingBox {
14631454
}
14641455

14651456
getLogSymbol() {
1466-
return "◼️";
1457+
if (this.isFloat()) {
1458+
return '●';
1459+
} else {
1460+
return '◼️';
1461+
}
14671462
}
1468-
14691463
hasBackground() {
14701464
return this.style.hasPaint();
14711465
}
@@ -1530,7 +1524,7 @@ export class ReplacedBox extends FormattingBox {
15301524
export type InlineLevel = Inline | Run | Break | BlockContainer | ReplacedBox;
15311525

15321526
type InlineIteratorBuffered = {state: 'pre' | 'post', item: Inline}
1533-
| {state: 'text', item: Run}
1527+
| {state: 'text', item: Run, treeIndex: number}
15341528
| {state: 'box', item: BlockLevel}
15351529
| {state: 'break'}
15361530
| {state: 'breakop'};
@@ -1593,7 +1587,8 @@ export function inlineIteratorStateNext(state: InlineIteratorState) {
15931587
state.emitBreakspot = state.minlevel !== state.parents.length;
15941588
state.minlevel = state.parents.length;
15951589
if (item.isRun()) {
1596-
state.buffered.push({state: 'text', item});
1590+
const treeIndex = state.index - 1;
1591+
state.buffered.push({state: 'text', item, treeIndex});
15971592
} else if (item.isBreak()) {
15981593
state.buffered.push({state: 'break'});
15991594
} else {

0 commit comments

Comments
 (0)