Skip to content

Commit

Permalink
Address PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
xkxx committed Sep 19, 2023
1 parent 5570b4d commit 2ef0a38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
22 changes: 4 additions & 18 deletions src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var CONTEXT_PROPERTIES = [
'shadowBlur',
'shadowOffsetX',
'shadowOffsetY',
'letterSpacing',
'lineCap',
'lineDashOffset',
'lineJoin',
Expand All @@ -94,8 +95,8 @@ var CONTEXT_PROPERTIES = [

const traceArrMax = 100;

interface CanvasRenderingContext2DFeatureDetection extends CanvasRenderingContext2D {
letterSpacing: string | undefined;
interface ExtendedCanvasRenderingContext2D extends CanvasRenderingContext2D {
letterSpacing: string;
}

/**
Expand Down Expand Up @@ -712,21 +713,6 @@ export class Context {
translate(x: number, y: number) {
this._context.translate(x, y);
}
/**
* Set letterSpacing if supported by browser.
* @method
* @name Konva.Context#trySetLetterSpacing
* @returns true if successful, false if not supported.
*/
trySetLetterSpacing(letterSpacing: number): boolean {
var context = this._context as CanvasRenderingContext2DFeatureDetection;
var letterSpacingSupported = 'letterSpacing' in context;

if (letterSpacingSupported) {
context.letterSpacing = letterSpacing + "px";
}
return letterSpacingSupported;
}
_enableTrace() {
var that = this,
len = CONTEXT_METHODS.length,
Expand Down Expand Up @@ -785,7 +771,7 @@ export class Context {

// supported context properties
type CanvasContextProps = Pick<
CanvasRenderingContext2D,
ExtendedCanvasRenderingContext2D,
(typeof CONTEXT_PROPERTIES)[number]
>;

Expand Down
9 changes: 6 additions & 3 deletions src/shapes/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ export class Text extends Shape<TextConfig> {
var lineTranslateX = 0;
var lineTranslateY = 0;

context.setAttr('direction', direction);
if (direction === RTL) {
context.setAttr('direction', direction);
}

context.setAttr('font', this._getContextFont());

Expand Down Expand Up @@ -315,13 +317,14 @@ export class Text extends Shape<TextConfig> {
lineTranslateX += this.measureSize(letter).width + letterSpacing;
}
} else {
context.trySetLetterSpacing(letterSpacing);
if (letterSpacing !== 0) {
context.setAttr('letterSpacing', `${letterSpacing}px`);
}
this._partialTextX = lineTranslateX;
this._partialTextY = translateY + lineTranslateY;
this._partialText = text;

context.fillStrokeShape(this);
context.trySetLetterSpacing(0);
}
context.restore();
if (textArrLen > 1) {
Expand Down

0 comments on commit 2ef0a38

Please sign in to comment.