-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve text metrics measurement and compatibility with other browsers
- Loading branch information
1 parent
ca25be0
commit 8528aee
Showing
3 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Utility class for measruing text metrics | ||
* measuring tricks from https://github.com/soulwire/FontMetrics | ||
*/ | ||
export class TextMetrics { | ||
private fontFamily: string; | ||
private fontSize: number; | ||
|
||
private canvas: HTMLCanvasElement; | ||
private ctx: CanvasRenderingContext2D; | ||
|
||
constructor (fontFamily: string, fontSize: number) { | ||
this.fontFamily = fontFamily; | ||
this.fontSize = fontSize; | ||
|
||
this.canvas = document.createElement("canvas"); | ||
|
||
// setting the dimension to 2 * fontSize in case of any oferflow | ||
this.canvas.width = fontSize * 2; | ||
this.canvas.height = fontSize * 2; | ||
|
||
this.ctx = this.canvas.getContext("2d")!; | ||
} | ||
|
||
/** | ||
* max distance from any ascender to any descender | ||
*/ | ||
measureMaxHeight (): number { | ||
return this.getDimension("h").height - | ||
this.getDimension("x").height + | ||
this.getDimension("g").height; | ||
} | ||
|
||
measureMaxWidth (): number { | ||
return this.getDimension("W").width; | ||
} | ||
|
||
measureMaxDescent (): number { | ||
return this.getDimension("g").height - | ||
this.getDimension("x").height; | ||
} | ||
|
||
private getCanvasFont (): string { | ||
return `${this.fontSize}px ${this.fontFamily}`; | ||
} | ||
|
||
private clearCanvas () { | ||
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
} | ||
|
||
private pixelIndexToPosition (index: number): {x: number, y: number} { | ||
index = Math.floor(index / 4); | ||
return { | ||
x: index % this.canvas.width, | ||
y: Math.floor(index / this.canvas.width) | ||
}; | ||
} | ||
|
||
private getDimension (text: string): { width: number, height: number } { | ||
this.clearCanvas(); | ||
|
||
this.ctx.font = this.getCanvasFont(); | ||
this.ctx.fillStyle = "rgba(255, 255, 255, 255)"; | ||
this.ctx.fillText(text, this.fontSize, this.fontSize); | ||
|
||
const pixels = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height).data; | ||
|
||
console.log(pixels); | ||
|
||
let minX = this.canvas.width, maxX = 0; | ||
let minY = this.canvas.height, maxY = 0; | ||
|
||
for (let i = 0; i < pixels.length; i++) { | ||
if (pixels[i] != 0) { | ||
const {x, y} = this.pixelIndexToPosition(i); | ||
|
||
if (x < minX) minX = x; | ||
if (x > maxX) maxX = x; | ||
if (y < minY) minY = y; | ||
if (y > maxY) maxY = y; | ||
} | ||
} | ||
|
||
console.log({ | ||
text: text, | ||
width: maxX - minX, | ||
height: maxY - minY | ||
}); | ||
|
||
return { | ||
width: maxX - minX, | ||
height: maxY - minY | ||
}; | ||
} | ||
}; |