Skip to content

Commit

Permalink
adding support for strikethrough (#538)
Browse files Browse the repository at this point in the history
* adding support for strikethrough

 + doc test

* remove warning

this is called internally to get and set formattedstring attributes
  • Loading branch information
typemytype authored Dec 5, 2023
1 parent 22927bb commit b3c1e32
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/content/text/textProperties.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Text Properties
.. autofunction:: drawBot.fontSize
.. autofunction:: drawBot.fallbackFont
.. autofunction:: drawBot.underline
.. autofunction:: drawBot.strikethrough
.. autofunction:: drawBot.hyphenation
.. autofunction:: drawBot.lineHeight
.. autofunction:: drawBot.tracking
Expand Down
26 changes: 26 additions & 0 deletions drawBot/context/baseContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,18 @@ class FormattedString(SVGContextPropertyMixin, ContextPropertyMixin):
# byWord=0x8000 # AppKit.NSUnderlineByWord,
)

_textstrikethroughMap = dict(
single=AppKit.NSUnderlineStyleSingle,
thick=AppKit.NSUnderlineStyleThick,
double=AppKit.NSUnderlineStyleDouble,
# solid=AppKit.NSUnderlinePatternSolid,
# dotted=AppKit.NSUnderlinePatternDot,
# dashed=AppKit.NSUnderlinePatternDash,
# dotDashed=AppKit.NSUnderlinePatternDashDot,
# dotDotted=AppKit.NSUnderlinePatternDashDotDot,
# byWord=0x8000 # AppKit.NSUnderlineByWord,
)

_formattedAttributes = dict(
font=_FALLBACKFONT,
fallbackFont=None,
Expand All @@ -1067,6 +1079,7 @@ class FormattedString(SVGContextPropertyMixin, ContextPropertyMixin):
tracking=None,
baselineShift=None,
underline=None,
strikethrough=None,
url=None,
openTypeFeatures=dict(),
fontVariations=dict(),
Expand Down Expand Up @@ -1339,6 +1352,8 @@ def append(self, txt, **kwargs):
attributes[AppKit.NSBaselineOffsetAttributeName] = self._baselineShift
if self._underline in self._textUnderlineMap:
attributes[AppKit.NSUnderlineStyleAttributeName] = self._textUnderlineMap[self._underline]
if self._strikethrough in self._textstrikethroughMap:
attributes[AppKit.NSStrikethroughStyleAttributeName] = self._textstrikethroughMap[self._strikethrough]
if self._url is not None:
attributes[AppKit.NSLinkAttributeName] = AppKit.NSURL.URLWithString_(self._url)
if self._language:
Expand Down Expand Up @@ -1551,6 +1566,13 @@ def underline(self, underline):
"""
self._underline = underline

def strikethrough(self, strikethrough):
"""
Set the strikethrough value.
Underline must be `single`, `thick`, `double` or `None`.
"""
self._strikethrough = strikethrough

def url(self, url):
"""
set the url value.
Expand Down Expand Up @@ -2091,6 +2113,7 @@ class BaseContext(object):
_textAlignMap = FormattedString._textAlignMap
_textTabAlignMap = FormattedString._textTabAlignMap
_textUnderlineMap = FormattedString._textUnderlineMap
_textstrikethroughMap = FormattedString._textstrikethroughMap

_colorSpaceMap = dict(
genericRGB=AppKit.NSColorSpace.genericRGBColorSpace(),
Expand Down Expand Up @@ -2435,6 +2458,9 @@ def baselineShift(self, baselineShift):
def underline(self, underline):
self._state.text.underline(underline)

def strikethrough(self, strikethrough):
self._state.text.strikethrough(strikethrough)

def url(self, value):
self._state.text.url(value)

Expand Down
13 changes: 13 additions & 0 deletions drawBot/context/svgContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ class SVGContext(BaseContext):
AppKit.NSUnderlineStyleDouble: "double",
}

_svgStrikethroughStylesMap = {
AppKit.NSUnderlineStyleSingle: "",
AppKit.NSUnderlineStyleThick: "",
AppKit.NSUnderlineStyleDouble: "double",
}

indentation = " "
fileExtensions = ["svg"]
saveImageOptions = [
Expand Down Expand Up @@ -440,6 +446,7 @@ def _textBox(self, rawTxt, box, align):
baselineShift = attributes.get(AppKit.NSBaselineOffsetAttributeName, 0)
openTypeFeatures = attributes.get("drawBot.formattedString.properties", dict()).get("openTypeFeatures")
underline = attributes.get(AppKit.NSUnderlineStyleAttributeName)
strikethrough = attributes.get(AppKit.NSStrikethroughStyleAttributeName)
url = attributes.get(AppKit.NSLinkAttributeName)

fontName = font.fontName()
Expand Down Expand Up @@ -477,6 +484,12 @@ def _textBox(self, rawTxt, box, align):
if underlineStyle:
style["text-decoration-style"] = underlineStyle

if strikethrough is not None:
style["text-decoration"] = "line-through"
strikethroughStyle = self._svgStrikethroughStylesMap.get(strikethrough)
if strikethroughStyle:
style["text-decoration-style"] = strikethroughStyle

if style:
spanData["style"] = self._svgStyle(**style)
self._save()
Expand Down
17 changes: 17 additions & 0 deletions drawBot/drawBotDrawingTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,23 @@ def underline(self, value):
self._dummyContext.underline(value)
self._addInstruction("underline", value)

def strikethrough(self, value):
"""
Set the strikethrough value.
Underline must be `single`, `thick`, `double` or `None`.
.. downloadcode:: strikethrough.py
size(1000, 200)
strikethrough("single")
fontSize(100)
text("hello strikethrough", (40, 60))
"""
if value is not None and value not in self._dummyContext._textstrikethroughMap:
raise DrawBotError("strikethrough must be %s" % (", ".join(sorted(self._dummyContext._textstrikethroughMap.keys()))))
self._dummyContext.strikethrough(value)
self._addInstruction("strikethrough", value)

def url(self, value):
"""
Set the url value for text.
Expand Down
Binary file added tests/data/example_strikethrough.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b3c1e32

Please sign in to comment.