MCPcopy Create free account
hub / github.com/PageBot/PageBot / fitVariableWidth

Function fitVariableWidth

Lib/pagebot/fonttoolbox/variablefontbuilder.py:62–153  ·  view source on GitHub ↗

Answers the font instance that makes string s width on the given width *w* for the given *fontSize*. The *condensedLocation* dictionary defines the most condensed font instance (optionally including the opsz) and the *wideLocation* dictionary defines the most wide font instance (optiona

(context, varFont, s, w, fontSize, condensedLocation,
        wideLocation, fixedSize=True, tracking=None, cached=True, lazy=True)

Source from the content-addressed store, hash-verified

60 return varFont, varFont
61
62def fitVariableWidth(context, varFont, s, w, fontSize, condensedLocation,
63 wideLocation, fixedSize=True, tracking=None, cached=True, lazy=True):
64 """Answers the font instance that makes string s width on the given width
65 *w* for the given *fontSize*. The *condensedLocation* dictionary defines
66 the most condensed font instance (optionally including the opsz) and the
67 *wideLocation* dictionary defines the most wide font instance (optionally
68 including the opsz). The string width for s is calculated with both
69 locations and then the [wdth] value is interpolated and iterated until the
70 location is found where the string *s* fits width *w*). Note that
71 interpolation may not be enough, as the width axis may contain non-linear
72 masters.
73
74 If the requested w outside of what is possible with two locations, then
75 interations are performed to change the size. Again this cannot be done by
76 simple interpolation, as the [opsz] also changes the width. It one of the
77 axes does not exist in the font, then use the default setting of the font.
78 """
79 # TODO: Adjusting by size change (if requested width is not possible with
80 # the width limits of the font)
81 # TODO: is not yet implemented.
82
83 # Get the instances for the extreme width locations. This allows the
84 # calling function to define the actual range of the [wdth] axis to be
85 # user, instead of the default minValue and maxValue. E.g. for a range of
86 # widths in a headline, the typographer may only want a small change before
87 # the line is wrapping, instead using the full spectrum to extreme
88 # condensed.
89 condensedFont = getVarFontInstance(varFont, condensedLocation, cached=cached, lazy=lazy)
90 wideFont = getVarFontInstance(varFont, wideLocation, cached=cached, lazy=lazy)
91 # Calculate the widths of the string using these two instances.
92 condensedString = context.newString(s,
93 style=dict(font=condensedFont.path,
94 fontSize=fontSize,
95 tracking=tracking,
96 textFill=blackColor))
97 wideString = context.newString(s,
98 style=dict(font=wideFont.path,
99 fontSize=fontSize,
100 tracking=tracking,
101 textFill=blackColor))
102
103 # Calculate the widths of the strings.
104 # TODO: Handle if these lines would wrap on the given width. In that case
105 # we may want to set the wrapped first line back to it's uncondensed value,
106 # to make the first wrapped line fit the width.
107 condensedWidth, _ = context.textSize(condensedString)
108 wideWidth, _ = context.textSize(wideString)
109
110 # Check if the requested with is inside the boundaries of the font width axis
111 # Requested width is smaller than was was possible using the extreme value
112 # of [wdth] axis.
113 if w < condensedWidth:
114 font = condensedFont
115 bs = condensedString
116 location = condensedLocation
117 elif w > wideWidth:
118 # Requested width is larger than was was possible using the extreme
119 # value of [wdth] axis.

Callers

nothing calls this directly

Calls 4

getVarFontInstanceFunction · 0.85
newStringMethod · 0.80
textSizeMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected