Skip to content

Commit

Permalink
use LineBreaker per item and only draw firstLine once
Browse files Browse the repository at this point in the history
  • Loading branch information
filecage committed Dec 20, 2023
1 parent a3af02a commit 7f85b5a
Showing 1 changed file with 65 additions and 58 deletions.
123 changes: 65 additions & 58 deletions lib/mixins/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,73 +153,80 @@ export default {
}
};

wrapper = new LineWrapper(this, options);
wrapper.on('line', this._line);
const drawItem = function(listItem) {
wrapper = new LineWrapper(this, options);
wrapper.on('line', this._line);

level = 1;
let i = 0;
wrapper.once('firstLine', () => {
let item, itemType, labelType, bodyType;
if (options.structParent) {
if (options.structTypes) {
[itemType, labelType, bodyType] = options.structTypes;
} else {
[itemType, labelType, bodyType] = ['LI', 'Lbl', 'LBody'];
}
}

level = 1;
let i = 0;
wrapper.on('firstLine', () => {
let item, itemType, labelType, bodyType;
if (options.structParent) {
if (options.structTypes) {
[ itemType, labelType, bodyType ] = options.structTypes;
} else {
[ itemType, labelType, bodyType ] = [ 'LI', 'Lbl', 'LBody' ];
if (itemType) {
item = this.struct(itemType);
options.structParent.add(item);
} else if (options.structParent) {
item = options.structParent;
}
}

if (itemType) {
item = this.struct(itemType);
options.structParent.add(item);
} else if (options.structParent) {
item = options.structParent;
}
let l;
if ((l = levels[i++]) !== level) {
const diff = itemIndent * (l - level);
this.x += diff;
wrapper.lineWidth -= diff;
level = l;
}

let l;
if ((l = levels[i++]) !== level) {
const diff = itemIndent * (l - level);
this.x += diff;
wrapper.lineWidth -= diff;
level = l;
}
if (item && (labelType || bodyType)) {
item.add(this.struct(labelType || bodyType,
[this.markStructureContent(labelType || bodyType)]));
}
switch (listType) {
case 'bullet':
this.circle(this.x - indent + r, this.y + midLine, r);
this.fill();
break;
case 'numbered':
case 'lettered':
var text = label(numbers[i - 1]);
this._fragment(text, this.x - indent, this.y, options);
break;
}

if (item && (labelType || bodyType)) {
item.add(this.struct(labelType || bodyType,
[ this.markStructureContent(labelType || bodyType) ]));
}
switch (listType) {
case 'bullet':
this.circle(this.x - indent + r, this.y + midLine, r);
this.fill();
break;
case 'numbered':
case 'lettered':
var text = label(numbers[i - 1]);
this._fragment(text, this.x - indent, this.y, options);
break;
}
if (item && labelType && bodyType) {
item.add(this.struct(bodyType, [this.markStructureContent(bodyType)]));
}
if (item && item !== options.structParent) {
item.end();
}
});

if (item && labelType && bodyType) {
item.add(this.struct(bodyType, [ this.markStructureContent(bodyType) ]));
}
if (item && item !== options.structParent) {
item.end();
}
});
wrapper.on('sectionStart', () => {
const pos = indent + itemIndent * (level - 1);
this.x += pos;
return (wrapper.lineWidth -= pos);
});

wrapper.on('sectionStart', () => {
const pos = indent + itemIndent * (level - 1);
this.x += pos;
return (wrapper.lineWidth -= pos);
});
wrapper.on('sectionEnd', () => {
const pos = indent + itemIndent * (level - 1);
this.x -= pos;
return (wrapper.lineWidth += pos);
});

wrapper.wrap(listItem, options);
};

wrapper.on('sectionEnd', () => {
const pos = indent + itemIndent * (level - 1);
this.x -= pos;
return (wrapper.lineWidth += pos);
});

wrapper.wrap(items.join('\n'), options);
for (let i = 0; i < items.length; i++) {
drawItem.call(this, items[i]);
}

return this;
},
Expand Down

0 comments on commit 7f85b5a

Please sign in to comment.