generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from doodlewind/text
measureText
- Loading branch information
Showing
18 changed files
with
757 additions
and
377 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
draw-text.png | ||
draw-text-with-baseline.png |
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,36 @@ | ||
const { readFileSync, writeFileSync } = require('fs') | ||
const { join } = require('path') | ||
|
||
const { createCanvas, GlobalFonts } = require('../index.js') | ||
|
||
const fontPath = join(__dirname, '..', '__test__', 'fonts', 'iosevka-slab-regular.ttf') | ||
const fontData = readFileSync(fontPath) | ||
|
||
console.info(GlobalFonts.families) | ||
|
||
GlobalFonts.register(fontData) | ||
|
||
console.info(GlobalFonts.families) | ||
|
||
const canvas = createCanvas(1024, 768) | ||
const ctx = canvas.getContext('2d') | ||
ctx.fillStyle = 'yellow' | ||
ctx.fillRect(0, 0, canvas.width, canvas.height) | ||
const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'] | ||
ctx.font = '36px Iosevka Slab' | ||
ctx.strokeStyle = 'red' | ||
ctx.fillStyle = 'black' | ||
|
||
baselines.forEach(function (baseline, index) { | ||
ctx.textBaseline = baseline | ||
const y = 75 + index * 75 | ||
ctx.beginPath() | ||
ctx.moveTo(0, y + 0.5) | ||
ctx.lineTo(550, y + 0.5) | ||
ctx.stroke() | ||
ctx.fillText('Abcdefghijklmnop (' + baseline + ')', 0, y) | ||
}) | ||
|
||
const b = canvas.toBuffer('image/png') | ||
|
||
writeFileSync(join(__dirname, 'draw-text-with-baseline.png'), b) |
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,21 @@ | ||
const { join } = require('path') | ||
|
||
const { createCanvas, GlobalFonts } = require('../index.js') | ||
|
||
const fontPath = join(__dirname, '..', '__test__', 'fonts', 'iosevka-slab-regular.ttf') | ||
|
||
GlobalFonts.registerFromPath(fontPath) | ||
|
||
const canvas = createCanvas(1024, 768) | ||
const ctx = canvas.getContext('2d') | ||
|
||
ctx.font = '50px Iosevka Slab' | ||
ctx.lineWidth = 3 | ||
|
||
const baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'] | ||
|
||
baselines.forEach(function (baseline) { | ||
ctx.textBaseline = baseline | ||
const metrics = ctx.measureText('@napi-rs/canvas') | ||
console.info(baseline, metrics) | ||
}) |
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
Oops, something went wrong.