add textDecoration

This commit is contained in:
csldev 2018-07-17 13:51:36 +08:00
parent 5c8d3b3c1a
commit db221b934d

View File

@ -207,9 +207,30 @@ export default class Painter {
while (this.ctx.measureText(`${text}...`).width > width) {
text = text.substring(0, text.length - 1);
}
text += '...';
text += "..."
}
const x = -(width / 2);
const y = -(height / 2) + (i === 0 ? view.css.fontSize.toPx() : (view.css.fontSize.toPx() + i * lineHeight));
this.ctx.fillText(text, x, y);
const fontSize = view.css.fontSize.toPx();
if (view.css.textDecoration) {
this.ctx.beginPath();
if (/\bunderline\b/.test(view.css.textDecoration)) {
this.ctx.moveTo(x, y);
this.ctx.lineTo(x + measuredWith, y);
}
if (/\boverline\b/.test(view.css.textDecoration)) {
this.ctx.moveTo(x, y - fontSize);
this.ctx.lineTo(x + measuredWith, y - fontSize);
}
if (/\bline-through\b/.test(view.css.textDecoration)) {
this.ctx.moveTo(x, y - fontSize / 3);
this.ctx.lineTo(x + measuredWith, y - fontSize / 3);
}
this.ctx.closePath();
this.ctx.setStrokeStyle(view.css.color);
this.ctx.stroke();
}
this.ctx.fillText(text, -(width / 2), -(height / 2) + (i === 0 ? view.css.fontSize.toPx() : (view.css.fontSize.toPx() + i * lineHeight)));
}
this.ctx.restore();