@@ -645,15 +645,22 @@ void compose()
645645""" , false , " mediump" )
646646
647647type ForEachLineAttributeCallback = proc (c: GraphicsContext , t: FormattedText , p: var Point , curLine, endIndex: int , str: string ) {.nimcall .}
648- proc forEachLineAttribute (c: GraphicsContext , origP: Point , t: FormattedText , cb: ForEachLineAttributeCallback ) =
648+ proc forEachLineAttribute (c: GraphicsContext , inRect: Rect , origP: Point , t: FormattedText , cb: ForEachLineAttributeCallback ) =
649649 var p = origP
650650 let numLines = t.lines.len
651651 var curLine = 0
652652 let top = t.topOffset () + origP.y
653+ let inRectValid = (inRect.y + inRect.height) > 0.01
653654
654655 while curLine < numLines:
655656 p.x = origP.x + t.lineLeft (curLine)
656657 p.y = t.lines[curLine].top + t.lines[curLine].baseline + top
658+ if inRectValid:
659+ if p.y < inRect.y:
660+ curLine.inc
661+ continue
662+ elif p.y > inRect.height:
663+ break
657664
658665 var lastCurAttrIndex: int
659666 var lastAttrStartIndex: int
@@ -719,9 +726,9 @@ proc forEachLineAttribute(c: GraphicsContext, origP: Point, t: FormattedText, cb
719726 curLine.inc
720727
721728
722- proc drawShadow (c: GraphicsContext , origP: Point , t: FormattedText ) =
729+ proc drawShadow (c: GraphicsContext , inRect: Rect , origP: Point , t: FormattedText ) =
723730 # TODO : Optimize heavily
724- forEachLineAttribute (c, origP, t) do (c: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
731+ forEachLineAttribute (c, inRect, origP, t) do (c: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
725732 c.fillColor = t.mAttributes[curAttrIndex].shadowColor
726733 let font = t.mAttributes[curAttrIndex].font
727734 let oldBaseline = font.baseline
@@ -762,9 +769,9 @@ proc drawShadow(c: GraphicsContext, origP: Point, t: FormattedText) =
762769 font.baseline = oldBaseline
763770 p.x += pp.x - ppp.x
764771
765- proc drawStroke (c: GraphicsContext , origP: Point , t: FormattedText ) =
772+ proc drawStroke (c: GraphicsContext , inRect: Rect , origP: Point , t: FormattedText ) =
766773 # TODO : Optimize heavily
767- forEachLineAttribute (c, origP, t) do (c: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
774+ forEachLineAttribute (c, inRect, origP, t) do (c: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
768775 const magicStrokeMaxSizeCoof = 0.46
769776 let font = t.mAttributes[curAttrIndex].font
770777
@@ -806,14 +813,14 @@ proc drawStroke(c: GraphicsContext, origP: Point, t: FormattedText) =
806813 # Dirty hack to advance x position. Should be optimized, of course.
807814 c.drawText (font, p, str)
808815
809- proc drawText * (c: GraphicsContext , origP: Point , t: FormattedText ) =
816+ proc drawText * (c: GraphicsContext , origP: Point , t: FormattedText , inRect: Rect = zeroRect ) =
810817 t.updateCacheIfNeeded ()
811818
812819 if t.overrideColor.a == 0 :
813- if t.shadowAttrs.len > 0 : c.drawShadow (origP, t)
814- if t.strokeAttrs.len > 0 : c.drawStroke (origP, t)
820+ if t.shadowAttrs.len > 0 : c.drawShadow (inRect, origP, t)
821+ if t.strokeAttrs.len > 0 : c.drawStroke (inRect, origP, t)
815822
816- forEachLineAttribute (c, origP, t) do (c: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
823+ forEachLineAttribute (c, inRect, origP, t) do (c: GraphicsContext , t: FormattedText , p: var Point , curLine, curAttrIndex: int , str: string ):
817824 let font = t.mAttributes[curAttrIndex].font
818825 let oldBaseline = font.baseline
819826 font.baseline = bAlphabetic
0 commit comments