@@ -512,6 +512,11 @@ function drawPathFunctionCharacter(
512512 devicePixelRatio : number ,
513513 strokeWidth ?: number
514514) : void {
515+ ctx . save ( ) ;
516+ ctx . beginPath ( ) ;
517+ ctx . rect ( xOffset , yOffset , deviceCellWidth , deviceCellHeight ) ;
518+ ctx . clip ( ) ;
519+
515520 ctx . beginPath ( ) ;
516521 let actualInstructions : string ;
517522 if ( typeof charDefinition === 'function' ) {
@@ -538,7 +543,7 @@ function drawPathFunctionCharacter(
538543 if ( ! args [ 0 ] || ! args [ 1 ] ) {
539544 continue ;
540545 }
541- f ( ctx , translateArgs ( args , deviceCellWidth , deviceCellHeight , xOffset , yOffset , true , devicePixelRatio ) , state ) ;
546+ f ( ctx , translateArgs ( args , deviceCellWidth , deviceCellHeight , xOffset , yOffset , true , devicePixelRatio , 0 , 0 , false ) , state ) ;
542547 state . lastCommand = type ;
543548 }
544549 if ( strokeWidth !== undefined ) {
@@ -549,6 +554,7 @@ function drawPathFunctionCharacter(
549554 ctx . fill ( ) ;
550555 }
551556 ctx . closePath ( ) ;
557+ ctx . restore ( ) ;
552558}
553559
554560/**
@@ -697,7 +703,7 @@ const svgToCanvasInstructionMap: { [index: string]: (ctx: CanvasRenderingContext
697703 }
698704} ;
699705
700- function translateArgs ( args : string [ ] , cellWidth : number , cellHeight : number , xOffset : number , yOffset : number , doClamp : boolean , devicePixelRatio : number , leftPadding : number = 0 , rightPadding : number = 0 ) : number [ ] {
706+ function translateArgs ( args : string [ ] , cellWidth : number , cellHeight : number , xOffset : number , yOffset : number , doClamp : boolean , devicePixelRatio : number , leftPadding : number = 0 , rightPadding : number = 0 , clampToCell : boolean = true ) : number [ ] {
701707 const result = args . map ( e => parseFloat ( e ) || parseInt ( e ) ) ;
702708
703709 if ( result . length < 2 ) {
@@ -707,10 +713,11 @@ function translateArgs(args: string[], cellWidth: number, cellHeight: number, xO
707713 for ( let x = 0 ; x < result . length ; x += 2 ) {
708714 // Translate from 0-1 to 0-cellWidth
709715 result [ x ] *= cellWidth - ( leftPadding * devicePixelRatio ) - ( rightPadding * devicePixelRatio ) ;
710- // Ensure coordinate doesn't escape cell bounds and round to the nearest 0.5 to ensure a crisp
711- // line at 100% devicePixelRatio
716+ // Round to the nearest 0.5 to ensure a crisp line at 100% devicePixelRatio, and optionally
717+ // clamp to the cell bounds.
712718 if ( doClamp && result [ x ] !== 0 ) {
713- result [ x ] = clamp ( Math . round ( result [ x ] + 0.5 ) - 0.5 , cellWidth , 0 ) ;
719+ const rounded = Math . round ( result [ x ] + 0.5 ) - 0.5 ;
720+ result [ x ] = clampToCell ? clamp ( rounded , cellWidth , 0 ) : rounded ;
714721 }
715722 // Apply the cell's offset (ie. x*cellWidth)
716723 result [ x ] += xOffset + ( leftPadding * devicePixelRatio ) ;
@@ -719,10 +726,11 @@ function translateArgs(args: string[], cellWidth: number, cellHeight: number, xO
719726 for ( let y = 1 ; y < result . length ; y += 2 ) {
720727 // Translate from 0-1 to 0-cellHeight
721728 result [ y ] *= cellHeight ;
722- // Ensure coordinate doesn't escape cell bounds and round to the nearest 0.5 to ensure a crisp
723- // line at 100% devicePixelRatio
729+ // Round to the nearest 0.5 to ensure a crisp line at 100% devicePixelRatio, and optionally
730+ // clamp to the cell bounds.
724731 if ( doClamp && result [ y ] !== 0 ) {
725- result [ y ] = clamp ( Math . round ( result [ y ] + 0.5 ) - 0.5 , cellHeight , 0 ) ;
732+ const rounded = Math . round ( result [ y ] + 0.5 ) - 0.5 ;
733+ result [ y ] = clampToCell ? clamp ( rounded , cellHeight , 0 ) : rounded ;
726734 }
727735 // Apply the cell's offset (ie. x*cellHeight)
728736 result [ y ] += yOffset ;
0 commit comments