Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions drawBot/context/baseContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def textBox(
path, (x, y) = context._getPathForFrameSetter(box)
attributedString = context.attributedString(txt, align)

setter = CoreText.CTFramesetterCreateWithAttributedString(attributedString)
setter = newFramesetterWithAttributedString(attributedString)
frame = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)
ctLines = CoreText.CTFrameGetLines(frame)
origins = CoreText.CTFrameGetLineOrigins(frame, (0, len(ctLines)), None)
Expand Down Expand Up @@ -1086,7 +1086,7 @@ def block(value, rng, stop):
AppKit.NSParagraphStyleAttributeName, (0, len(attributedString)), 0, block
)

setter = CoreText.CTFramesetterCreateWithAttributedString(attributedString)
setter = newFramesetterWithAttributedString(attributedString)
path = Quartz.CGPathCreateMutable()
Quartz.CGPathAddRect(path, None, Quartz.CGRectMake(x, y, w, h * 2))
frame = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def block(value, rng, stop):
box = (lineX, lineY, width, h * 2)
else:
lineY = y + originY + firstLineJump - h * 2
subSetter = CoreText.CTFramesetterCreateWithAttributedString(attributedSubstring)
subSetter = newFramesetterWithAttributedString(attributedSubstring)
subPath = Quartz.CGPathCreateMutable()
Quartz.CGPathAddRect(subPath, None, Quartz.CGRectMake(lineX, lineY, w, h * 2))
subFrame = CoreText.CTFramesetterCreateFrame(subSetter, (0, 0), subPath, None)
Expand Down Expand Up @@ -2837,7 +2837,7 @@ def clippedText(self, txt, box, align):
if self._state.hyphenation:
hyphenIndexes = [i for i, c in enumerate(attrString.string()) if c == "-"]
attrString = self.hyphenateAttributedString(attrString, path)
setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
box = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)
visibleRange = CoreText.CTFrameGetVisibleStringRange(box)
clip = visibleRange.length
Expand Down Expand Up @@ -2869,7 +2869,7 @@ def _getTypesetterLinesWithPath(self, attrString, path, offset=None):
# get lines for an attribute string with a given path
if offset is None:
offset = 0, 0
setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
frame = CoreText.CTFramesetterCreateFrame(setter, offset, path, None)
return CoreText.CTFrameGetLines(frame)

Expand Down Expand Up @@ -2902,7 +2902,7 @@ def textSize(self, txt, align, width, height):
path = CoreText.CGPathCreateMutable()
CoreText.CGPathAddRect(path, None, CoreText.CGRectMake(0, 0, width, height))
attrString = self.hyphenateAttributedString(attrString, path)
setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
(w, h), _ = CoreText.CTFramesetterSuggestFrameSizeWithConstraints(
setter, (0, 0), None, (width, height), None
)
Expand Down Expand Up @@ -3042,3 +3042,11 @@ def getFontName(font) -> str | None:
if fontName is not None:
fontName = str(fontName)
return fontName


def newFramesetterWithAttributedString(attrString):
allowUnbounded = len(attrString) > 2000 # somewhat arbitrary
typesetter = CoreText.CTTypesetterCreateWithAttributedStringAndOptions(
attrString, {CoreText.kCTTypesetterOptionAllowUnboundedLayout: allowUnbounded}
)
return CoreText.CTFramesetterCreateWithTypesetter(typesetter)
4 changes: 2 additions & 2 deletions drawBot/context/pdfContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ..macOSVersion import macOSVersion
from ..misc import DrawBotError, isGIF, isPDF
from .baseContext import BaseContext, FormattedString
from .baseContext import BaseContext, FormattedString, newFramesetterWithAttributedString
from .tools import gifTools


Expand Down Expand Up @@ -164,7 +164,7 @@ def _textBox(self, txt, box, align):
if self._state.hyphenation:
attrString = self.hyphenateAttributedString(attrString, path)

setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
frame = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)

ctLines = CoreText.CTFrameGetLines(frame)
Expand Down
12 changes: 10 additions & 2 deletions drawBot/context/svgContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

from drawBot.misc import formatNumber, warnings

from .baseContext import BaseContext, Color, FormattedString, Gradient, GraphicsState, Shadow
from .baseContext import (
BaseContext,
Color,
FormattedString,
Gradient,
GraphicsState,
Shadow,
newFramesetterWithAttributedString,
)
from .imageContext import _makeBitmapImageRep


Expand Down Expand Up @@ -398,7 +406,7 @@ def _textBox(self, rawTxt, box, align):
if self._state.hyphenation:
attrString = self.hyphenateAttributedString(attrString, path)
txt = attrString.string()
setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
box = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)

self._svgBeginClipPath()
Expand Down
5 changes: 3 additions & 2 deletions drawBot/drawBotDrawingTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
getFontName,
getNSFontFromNameOrPath,
makeTextBoxes,
newFramesetterWithAttributedString,
)
from .context.dummyContext import DummyContext
from .context.tools import drawBotbuiltins, gifTools
Expand Down Expand Up @@ -2007,7 +2008,7 @@ def textBoxBaselines(self, txt: FormattedString | str, box: BoundingBox, align:
raise TypeError("expected 'str' or 'FormattedString', got '%s'" % type(txt).__name__)
path, (x, y) = self._dummyContext._getPathForFrameSetter(box)
attrString = self._dummyContext.attributedString(txt, align=align)
setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
box = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)
ctLines = CoreText.CTFrameGetLines(box)
origins = CoreText.CTFrameGetLineOrigins(box, (0, len(ctLines)), None)
Expand All @@ -2030,7 +2031,7 @@ def textBoxCharacterBounds(self, txt: FormattedString | str, box: BoundingBox, a
bounds = list()
path, (x, y) = self._dummyContext._getPathForFrameSetter(box)
attrString = self._dummyContext.attributedString(txt)
setter = CoreText.CTFramesetterCreateWithAttributedString(attrString)
setter = newFramesetterWithAttributedString(attrString)
box = CoreText.CTFramesetterCreateFrame(setter, (0, 0), path, None)
ctLines = CoreText.CTFrameGetLines(box)
origins = CoreText.CTFrameGetLineOrigins(box, (0, len(ctLines)), None)
Expand Down
Binary file added tests/data/expected_textBoxLongText.pdf
Binary file not shown.
Binary file added tests/data/expected_textBoxLongText.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions tests/data/expected_textBoxLongText.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/drawBotScripts/textBoxLongText.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# See bug: https://github.com/typemytype/drawbot/issues/585
import drawBot

drawBot.size(200, 200)
box = (10, 10, 180, 180)
drawBot.fontSize(18)
drawBot.font("Hoefler Text")
drawBot.textBox("fifl" * 3000, box)