Support clip path on any draw definition

This commit is contained in:
Daniel Imms
2025-12-24 04:25:59 -08:00
parent 2114abe88c
commit 11dae84ff7
3 changed files with 32 additions and 28 deletions
@@ -46,6 +46,12 @@ function drawDefinitionPart(
devicePixelRatio: number,
backgroundColor?: string
): void {
// Handle clipPath generically for any definition type
if (part.clipPath) {
ctx.save();
applyClipPath(ctx, part.clipPath, xOffset, yOffset, deviceCellWidth, deviceCellHeight);
}
switch (part.type) {
case CustomGlyphDefinitionType.SOLID_OCTANT_BLOCK_VECTOR:
drawBlockVectorChar(ctx, part.data, xOffset, yOffset, deviceCellWidth, deviceCellHeight);
@@ -56,9 +62,6 @@ function drawDefinitionPart(
case CustomGlyphDefinitionType.BLOCK_PATTERN_WITH_REGION:
drawBlockPatternWithRegion(ctx, part.data, xOffset, yOffset, deviceCellWidth, deviceCellHeight);
break;
case CustomGlyphDefinitionType.BLOCK_PATTERN_WITH_CLIP_PATH:
drawBlockPatternWithClipPath(ctx, part.data, xOffset, yOffset, deviceCellWidth, deviceCellHeight);
break;
case CustomGlyphDefinitionType.PATH_FUNCTION:
case CustomGlyphDefinitionType.PATH:
drawPathDefinitionCharacter(ctx, part.data, xOffset, yOffset, deviceCellWidth, deviceCellHeight);
@@ -73,6 +76,10 @@ function drawDefinitionPart(
drawPathDefinitionCharacterWithWeight(ctx, part.data, xOffset, yOffset, deviceCellWidth, deviceCellHeight, devicePixelRatio);
break;
}
if (part.clipPath) {
ctx.restore();
}
}
function drawBlockVectorChar(
@@ -491,21 +498,16 @@ function drawPathDefinitionCharacterWithWeight(
}
/**
* Draws a pattern clipped to an arbitrary path (for triangular shades, etc.)
* Applies a clip path to the canvas context from SVG-like path instructions.
*/
function drawBlockPatternWithClipPath(
function applyClipPath(
ctx: CanvasRenderingContext2D,
definition: [pattern: CustomGlyphPatternDefinition, clipPath: string],
clipPath: string,
xOffset: number,
yOffset: number,
deviceCellWidth: number,
deviceCellHeight: number
): void {
const [pattern, clipPath] = definition;
ctx.save();
// Build clip path from SVG-like instructions
ctx.beginPath();
for (const instruction of clipPath.split(' ')) {
const type = instruction[0];
@@ -526,11 +528,6 @@ function drawBlockPatternWithClipPath(
}
}
ctx.clip();
// Draw the pattern
drawPatternChar(ctx, pattern, xOffset, yOffset, deviceCellWidth, deviceCellHeight);
ctx.restore();
}
function drawVectorShape(