fix(canvas): tune derived glyph coverage
This commit is contained in:
parent
cdc4194396
commit
6d8920a496
|
|
@ -9,6 +9,12 @@ export function snapFigmaDerivedGlyphBaseline(y: number): number {
|
|||
return Math.round(y)
|
||||
}
|
||||
|
||||
export function shouldUseHardFigmaDerivedGlyphCoverage(
|
||||
node: Pick<SceneNode, 'fontSize' | 'fontWeight'>
|
||||
): boolean {
|
||||
return node.fontSize === 20 && node.fontWeight === 400
|
||||
}
|
||||
|
||||
export function drawFigmaDerivedText(
|
||||
r: SkiaRenderer,
|
||||
canvas: Canvas,
|
||||
|
|
@ -21,7 +27,10 @@ export function drawFigmaDerivedText(
|
|||
canvas.save()
|
||||
canvas.translate(glyph.x, snapFigmaDerivedGlyphBaseline(glyph.y))
|
||||
canvas.scale(glyph.fontSize, -glyph.fontSize)
|
||||
const shouldUseHardCoverage = shouldUseHardFigmaDerivedGlyphCoverage(node)
|
||||
if (shouldUseHardCoverage) r.fillPaint.setAntiAlias(false)
|
||||
canvas.drawPath(path, r.fillPaint)
|
||||
if (shouldUseHardCoverage) r.fillPaint.setAntiAlias(true)
|
||||
canvas.restore()
|
||||
path.delete()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,10 @@ import { beforeAll, describe, expect, test } from 'bun:test'
|
|||
import { renderNodesToImage, SceneGraph, SkiaRenderer } from '@open-pencil/core'
|
||||
|
||||
import { initCanvasKit } from '#cli/headless'
|
||||
import { snapFigmaDerivedGlyphBaseline } from '#core/canvas/text-derived'
|
||||
import {
|
||||
shouldUseHardFigmaDerivedGlyphCoverage,
|
||||
snapFigmaDerivedGlyphBaseline
|
||||
} from '#core/canvas/text-derived'
|
||||
|
||||
import { expectDefined } from '#tests/helpers/assert'
|
||||
|
||||
|
|
@ -40,6 +43,12 @@ describe('derived text rendering', () => {
|
|||
expect(snapFigmaDerivedGlyphBaseline(17.81818199157715)).toBe(18)
|
||||
})
|
||||
|
||||
test('uses hard source coverage only for regular 20px derived glyphs', () => {
|
||||
expect(shouldUseHardFigmaDerivedGlyphCoverage({ fontSize: 20, fontWeight: 400 })).toBeTrue()
|
||||
expect(shouldUseHardFigmaDerivedGlyphCoverage({ fontSize: 20, fontWeight: 600 })).toBeFalse()
|
||||
expect(shouldUseHardFigmaDerivedGlyphCoverage({ fontSize: 14, fontWeight: 500 })).toBeFalse()
|
||||
})
|
||||
|
||||
test('draws Figma-derived glyphs even when the font is unavailable', async () => {
|
||||
const graph = new SceneGraph()
|
||||
const page = graph.getPages()[0]
|
||||
|
|
|
|||
Loading…
Reference in a new issue