@@ -665,17 +665,24 @@ void compose()
665665""" , false , " mediump" )
666666
667667type ForEachLineAttributeCallback = proc (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, endIndex: int , str: string ) {.nimcall .}
668- proc forEachLineAttribute (gfx: GraphicsContext , origP: Point , t: FormattedText , cb: ForEachLineAttributeCallback ) =
668+ proc forEachLineAttribute (gfx: GraphicsContext , inRect: Rect , origP: Point , t: FormattedText , cb: ForEachLineAttributeCallback ) =
669669 template fontCtx : untyped = gfx.fontCtx
670670 template gl : untyped = gfx.gl
671671 var p = origP
672672 let numLines = t.lines.len
673673 var curLine = 0
674674 let top = topOffset (fontCtx, gl, t) + origP.y
675+ let inRectValid = (inRect.y + inRect.height) > 0.01
675676
676677 while curLine < numLines:
677678 p.x = origP.x + lineLeft (fontCtx, gl, t, curLine)
678679 p.y = t.lines[curLine].top + t.lines[curLine].baseline + top
680+ if inRectValid:
681+ if p.y < inRect.y:
682+ curLine.inc
683+ continue
684+ elif p.y > inRect.height:
685+ break
679686
680687 var lastCurAttrIndex: int
681688 var lastAttrStartIndex: int
@@ -741,9 +748,9 @@ proc forEachLineAttribute(gfx: GraphicsContext, origP: Point, t: FormattedText,
741748 curLine.inc
742749
743750
744- proc drawShadow (gfx: GraphicsContext , origP: Point , t: FormattedText ) =
751+ proc drawShadow (gfx: GraphicsContext , inRect: Rect , origP: Point , t: FormattedText ) =
745752 # TODO : Optimize heavily
746- forEachLineAttribute (gfx, origP, t) do (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
753+ forEachLineAttribute (gfx, inRect, origP, t) do (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
747754 template gl : untyped = gfx.gl
748755 gfx.fillColor = t.mAttributes[curAttrIndex].shadowColor
749756 let font = t.mAttributes[curAttrIndex].font
@@ -784,9 +791,9 @@ proc drawShadow(gfx: GraphicsContext, origP: Point, t: FormattedText) =
784791 font.baseline = oldBaseline
785792 p.x += pp.x - ppp.x
786793
787- proc drawStroke (gfx: GraphicsContext , origP: Point , t: FormattedText ) =
794+ proc drawStroke (gfx: GraphicsContext , inRect: Rect , origP: Point , t: FormattedText ) =
788795 # TODO : Optimize heavily
789- forEachLineAttribute (gfx, origP, t) do (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
796+ forEachLineAttribute (gfx, inRect, origP, t) do (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
790797 const magicStrokeMaxSizeCoof = 0.46
791798 let font = t.mAttributes[curAttrIndex].font
792799
@@ -828,16 +835,16 @@ proc drawStroke(gfx: GraphicsContext, origP: Point, t: FormattedText) =
828835 # Dirty hack to advance x position. Should be optimized, of course.
829836 gfx.drawText (font, p, str)
830837
831- proc drawText * (gfx: GraphicsContext , origP: Point , t: FormattedText ) =
838+ proc drawText * (gfx: GraphicsContext , origP: Point , t: FormattedText , inRect: Rect = zeroRect ) =
832839 template fontCtx : untyped = gfx.fontCtx
833840 template gl : untyped = gfx.gl
834841 updateCacheIfNeeded (fontCtx, gl, t)
835842
836843 if t.overrideColor.a == 0 :
837- if t.shadowAttrs.len > 0 : gfx.drawShadow (origP, t)
838- if t.strokeAttrs.len > 0 : gfx.drawStroke (origP, t)
844+ if t.shadowAttrs.len > 0 : gfx.drawShadow (inRect, origP, t)
845+ if t.strokeAttrs.len > 0 : gfx.drawStroke (inRect, origP, t)
839846
840- forEachLineAttribute (gfx, origP, t) do (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
847+ forEachLineAttribute (gfx, inRect, origP, t) do (gfx: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
841848 let font = t.mAttributes[curAttrIndex].font
842849 let oldBaseline = font.baseline
843850 font.baseline = bAlphabetic
0 commit comments