From a48b0e0a043e79881e5e12230402844f1edbc7f4 Mon Sep 17 00:00:00 2001 From: 0JARVIS0 <709406687@qq.com> Date: Tue, 26 Nov 2019 11:55:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3textDecoration?= =?UTF-8?q?=E9=94=99=E4=BD=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pen.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/pen.js b/lib/pen.js index 3a8b7f4..3dc9cd8 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -583,15 +583,19 @@ export default class Painter { } this.ctx.setTextAlign(view.css.textAlign ? view.css.textAlign : 'left'); let x; + let lineX; switch (view.css.textAlign) { case 'center': x = 0; + lineX = x - measuredWith / 2; break; case 'right': x = (width / 2); + lineX = x - measuredWith; break; default: x = -(width / 2); + lineX = x; break; } const y = -(height / 2) + (lineIndex === 0 ? view.css.fontSize.toPx() : (view.css.fontSize.toPx() + lineIndex * lineHeight)); @@ -605,27 +609,27 @@ export default class Painter { 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); + this.ctx.moveTo(lineX, y); + this.ctx.lineTo(lineX + measuredWith, y); !this.isMoving && (this.callbackInfo.textDecoration = { - moveTo: [x, y], - lineTo: [x + measuredWith, y] + moveTo: [lineX, y], + lineTo: [lineX + measuredWith, y] }) } if (/\boverline\b/.test(view.css.textDecoration)) { - this.ctx.moveTo(x, y - fontSize); - this.ctx.lineTo(x + measuredWith, y - fontSize); + this.ctx.moveTo(lineX, y - fontSize); + this.ctx.lineTo(lineX + measuredWith, y - fontSize); !this.isMoving && (this.callbackInfo.textDecoration = { - moveTo: [x, y - fontSize], - lineTo: [x + measuredWith, y - fontSize] + moveTo: [lineX, y - fontSize], + lineTo: [lineX + 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.moveTo(lineX, y - fontSize / 3); + this.ctx.lineTo(lineX + measuredWith, y - fontSize / 3); !this.isMoving && (this.callbackInfo.textDecoration = { - moveTo: [x, y - fontSize / 3], - lineTo: [x + measuredWith, y - fontSize / 3] + moveTo: [lineX, y - fontSize / 3], + lineTo: [lineX + measuredWith, y - fontSize / 3] }) } this.ctx.closePath();