Skip to content

Commit

Permalink
Set font variations settings while extracting a glyph by name (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
typemytype authored Aug 7, 2021
1 parent 6eedf01 commit eccbca3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
20 changes: 6 additions & 14 deletions drawBot/context/baseContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,20 +1223,8 @@ def append(self, txt, **kwargs):
# The value 0 means kerning is disabled.
attributes[AppKit.NSKernAttributeName] = 0

coreTextFontVariations = dict()
if self._fontVariations:
existingAxes = variation.getVariationAxesForFont(font)
for axis, value in self._fontVariations.items():
if axis in existingAxes:
existinsAxis = existingAxes[axis]
# clip variation value within the min max value
if value < existinsAxis["minValue"]:
value = existinsAxis["minValue"]
if value > existinsAxis["maxValue"]:
value = existinsAxis["maxValue"]
coreTextFontVariations[variation.convertVariationTagToInt(axis)] = value
else:
warnings.warn("variation axis '%s' not available for '%s'" % (axis, self._font))
coreTextFontVariations = variation.getFontVariationAttributes(font, self._fontVariations)

fontAttributes = {}
if coreTextFontFeatures:
fontAttributes[CoreText.kCTFontFeatureSettingsAttribute] = coreTextFontFeatures
Expand Down Expand Up @@ -1954,6 +1942,10 @@ def appendGlyph(self, *glyphNames):
# disable calt features, as this seems to be on by default
# for both the font stored in the nsGlyphInfo as in the replacement character
fontAttributes = {}
coreTextFontVariations = variation.getFontVariationAttributes(font, self._fontVariations)
if coreTextFontVariations:
fontAttributes[CoreText.NSFontVariationAttribute] = coreTextFontVariations

fontAttributes[CoreText.kCTFontFeatureSettingsAttribute] = [dict(CTFeatureOpenTypeTag="calt", CTFeatureOpenTypeValue=False)]
fontDescriptor = font.fontDescriptor()
fontDescriptor = fontDescriptor.fontDescriptorByAddingAttributes_(fontAttributes)
Expand Down
20 changes: 19 additions & 1 deletion drawBot/context/tools/variation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from fontTools.ttLib import TTFont

from drawBot.misc import memoize
from drawBot.misc import memoize, warnings

"""
https://developer.apple.com/documentation/coretext/ctfont/font_variation_axis_dictionary_keys?language=objc
Expand Down Expand Up @@ -87,3 +87,21 @@ def getNamedInstancesForFont(font):

ft.close()
return instances


def getFontVariationAttributes(font, fontVariations):
coreTextFontVariations = dict()
if fontVariations:
existingAxes = getVariationAxesForFont(font)
for axis, value in fontVariations.items():
if axis in existingAxes:
existinsAxis = existingAxes[axis]
# clip variation value within the min max value
if value < existinsAxis["minValue"]:
value = existinsAxis["minValue"]
if value > existinsAxis["maxValue"]:
value = existinsAxis["maxValue"]
coreTextFontVariations[convertVariationTagToInt(axis)] = value
else:
warnings.warn("variation axis '%s' not available for '%s'" % (axis, font.fontName()))
return coreTextFontVariations
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/data/expected_appendGlyphWithFontVariations.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/drawBotScripts/appendGlyphWithFontVariations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://github.com/typemytype/drawbot/issues/402
import drawBot
drawBot.size(640, 160)

fs = drawBot.FormattedString()
fs.font("Skia")
fs.fontSize(200)
fs.fontVariations(wght=1)
fs += "&"
fs.appendGlyph("ampersand")
fs.fontVariations(wght=2)
fs.appendGlyph("ampersand")
fs += "&"

drawBot.text(fs, (10, 10))

0 comments on commit eccbca3

Please sign in to comment.