| 1 |
.. _autowidth.AutoWidth: |
| 2 |
|
| 3 |
Auto Width |
| 4 |
========== |
| 5 |
|
| 6 |
.. image:: /images/autowidth.png |
| 7 |
:align: right |
| 8 |
|
| 9 |
The Auto Width command will attempt to guess reasonable widths (more accurately |
| 10 |
reasonable left and right side bearings) for your font. The command splits the |
| 11 |
selection into scripts and looks at all possible combinations of selected glyphs |
| 12 |
within each script. |
| 13 |
|
| 14 |
The Separation text box allows you to say how close you want the glyphs to be. |
| 15 |
If you look at a vertical bar (or sanserif "I"), and you autowidth it the sum of |
| 16 |
the left and right side bearings will be equal to the spacing. The min and max |
| 17 |
values will prevent sidebearings from becoming unrealistically large (these |
| 18 |
rarely matter). |
| 19 |
|
| 20 |
Adjusting the left and right bearings of a letter will also adjust the left and |
| 21 |
right bearings of all accented letters based (by reference) on it. You should |
| 22 |
not try to use AutoWidth on accented letters directly (unless those glyphs do |
| 23 |
not use references). |
| 24 |
|
| 25 |
|
| 26 |
.. _autowidth.AutoKern: |
| 27 |
|
| 28 |
Auto Kern |
| 29 |
--------- |
| 30 |
|
| 31 |
There used to be an Auto Kern dialog. There is no longer, this is now done in |
| 32 |
the :ref:`kerning subtable <lookups.Pair>` dialog and the |
| 33 |
:ref:`kerning class <metricsview.kernclass>` dialog. |
| 34 |
|
| 35 |
-------------------------------------------------------------------------------- |
| 36 |
|
| 37 |
|
| 38 |
.. _autowidth.GlyphSeparationHook: |
| 39 |
|
| 40 |
The python GlyphSeparationHook |
| 41 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 42 |
|
| 43 |
When FontForge does AutoKerning (and in a more complicated fashion, when it does |
| 44 |
AutoWidthing) it tries to guess the optical separation between two glyphs, and |
| 45 |
then it applies an appropriate kern value to make the optical separation be the |
| 46 |
desired spacing. |
| 47 |
|
| 48 |
FontForge assumes that the optical spacing is linear (so if you increase the |
| 49 |
separation between two glyphs by 3 em-units, then the optical spacing will also |
| 50 |
increase by 3 em-units). |
| 51 |
|
| 52 |
FF guesses the optical separation between two glyphs by figuring a value for the |
| 53 |
separation of two glyphs when their bounding boxes are adjacent. For two |
| 54 |
rectangles (where the bounding box is the same as the glyph itself) the |
| 55 |
separation will be 0 as the two glyphs will be touching. For a combination like |
| 56 |
"To" the optical separation will be quite large. |
| 57 |
|
| 58 |
.. flex-grid:: |
| 59 |
|
| 60 |
* - .. image:: /images/GlyphSep-rectangles.png |
| 61 |
- .. image:: /images/GlyphSep-To.png |
| 62 |
|
| 63 |
The GlyphSeparationHook is a python routine which will be called with a bunch of |
| 64 |
information about two glyphs, and is expected to return an integer representing |
| 65 |
the optical separation between the two. It is a low-level routine; the actual |
| 66 |
calculation of kerning offsets or setting glyph widths is done elsewhere, but |
| 67 |
this routine provides the information those higher level routines need. Of |
| 68 |
course, if you don't provide a routine, FontForge has a built-in routine which |
| 69 |
will provide a default behavior. Here is a python version of that default |
| 70 |
routine: |
| 71 |
|
| 72 |
:: |
| 73 |
|
| 74 |
import fontforge; |
| 75 |
|
| 76 |
def GlyphSeparation(onLeft,onRight,context) : |
| 77 |
# the goal is to give a weighted average that expresses the visual |
| 78 |
# separation between two glyphs when they are placed so their bounding |
| 79 |
# boxes are adjacent. The separation between two rectangles would be 0 |
| 80 |
# While the separation between "T" and "o" would be fairly large |
| 81 |
# The trick is to guess a good weighting function. My guess is that |
| 82 |
# things that look close are more important than those which look far |
| 83 |
# So "T" and "O" should be dominated by the crossbar of the "T"... |
| 84 |
# |
| 85 |
# Find the area the two glyphs have in common (so when comparing "o" and |
| 86 |
# "l" or "g", ignore the ascenders and descenders since that's outside |
| 87 |
# the range of "o" and won't affect its visual proximity. |
| 88 |
imin_y = max(onRight.iminY,onLeft.iminY); |
| 89 |
imax_y = min(onRight.imaxY,onLeft.imaxY); |
| 90 |
# |
| 91 |
# Some glyph combinations will have no overlap -- the grave accent and the |
| 92 |
# letter "a" will have none. So they don't interact visually. |
| 93 |
if imax_y < imin_y : |
| 94 |
return( 0 ) |
| 95 |
# |
| 96 |
# Otherwise compute some sort of weighted average of the separations between |
| 97 |
# the two glyphs at various heights. |
| 98 |
tot,cnt = 0,0 |
| 99 |
j = imin_y |
| 100 |
while j<=imax_y : |
| 101 |
if onRight.left[j] < 32767 and onLeft.right[j] > -32767 : |
| 102 |
# beware of gaps such as those in "i" or "aaccute" |
| 103 |
# a gap has a left or right value which is huge |
| 104 |
# so ignore any such, again it doesn't contribute to the |
| 105 |
# visual separation. |
| 106 |
sep = onRight.left[j] - onLeft.right[j] |
| 107 |
weight = 1.0/(sep + context.denom) |
| 108 |
weight *= weight |
| 109 |
tot += weight*sep |
| 110 |
cnt += weight |
| 111 |
j += 1 |
| 112 |
if cnt!=0 : |
| 113 |
tot /= cnt |
| 114 |
return( int(round( tot )) ); |
| 115 |
|
| 116 |
fontforge.registerGlyphSeparationHook(GlyphSeparation) |
| 117 |
|
| 118 |
The hook will be called with three arguments, a structure with information about |
| 119 |
the glyph on the left, a structure with information about the glyph on the |
| 120 |
right, and a structure containing general information about the operation as a |
| 121 |
whole. |
| 122 |
|
| 123 |
The context argument contains the following fields: |
| 124 |
|
| 125 |
.. object:: font |
| 126 |
|
| 127 |
The font being worked on |
| 128 |
|
| 129 |
.. object:: emSize |
| 130 |
|
| 131 |
The emsize of the font |
| 132 |
|
| 133 |
.. object:: layer |
| 134 |
|
| 135 |
The active layer for this operation |
| 136 |
|
| 137 |
.. object:: regionHeight |
| 138 |
|
| 139 |
(explained below) by default emSize/100 |
| 140 |
|
| 141 |
.. object:: denom |
| 142 |
|
| 143 |
1/50th of the emSize. A number I found handy. |
| 144 |
|
| 145 |
FontForge preprocess all the glyphs, extracting pertinant information from each |
| 146 |
and storing it in a separate per-glyph structure than the normal python Glyph |
| 147 |
object. The most important information is a representation of the leftmost and |
| 148 |
rightmost edges of the glyph. Basically, for every value of y, FontForge finds |
| 149 |
the smallest x coordinate on any of the glyph's contours at that height and |
| 150 |
stores that information in an array. There is a similar array containing the |
| 151 |
largest x coordinates. Of course there are an infinite number of y values, so |
| 152 |
that isn't quite accurate. FontForge divides the vertical axis into many |
| 153 |
regions, each regionHeight high (this is usually 1/100th of the emsize). It then |
| 154 |
finds the smallest x coordinate achieved by any contour within that region. |
| 155 |
|
| 156 |
The value stored in the array representing the leftmost edge of the glyph is not |
| 157 |
quite the smallest x coordinate. Instead we normalize the numbers so that the x |
| 158 |
coordinate of the left side bearing of the glyph is 0. None of the numbers in |
| 159 |
the left array will be negative. |
| 160 |
|
| 161 |
Similarly the value stored in the rightmost array is normalized so that the |
| 162 |
glyph's right side bearing is 0. None of the numbers in the right array will be |
| 163 |
positive. |
| 164 |
|
| 165 |
There can be gaps in a glyph -- for example in a lower case "i" there is a hole |
| 166 |
in the body of the glyph between the dot on top and the rest down below. In this |
| 167 |
case the left array will hold the special value 32767, and the right array will |
| 168 |
hold -32767. |
| 169 |
|
| 170 |
So I don't pass a standard glyph object. The underlying glyph object can be |
| 171 |
obtained through the ``glyph`` member. The most important things in this |
| 172 |
structure are the arrays representing the left and right contours called |
| 173 |
``left`` and ``right``. The structure also contains the upper and lower bounds |
| 174 |
of the arrays (both arrays have the same bounds). The glyph structures contain |
| 175 |
the following members: |
| 176 |
|
| 177 |
.. object:: glyph |
| 178 |
|
| 179 |
This is the standard python glyph object. This can (rarely) be None. |
| 180 |
|
| 181 |
.. object:: boundingbox |
| 182 |
|
| 183 |
A tuple of 4 values (minx,miny,maxx,maxy) for the glyph |
| 184 |
|
| 185 |
.. object:: iminY |
| 186 |
|
| 187 |
The low bound of the arrays. |
| 188 |
|
| 189 |
.. object:: imaxX |
| 190 |
|
| 191 |
The high bound of the arrays |
| 192 |
|
| 193 |
.. object:: left |
| 194 |
|
| 195 |
an array of integers representing the separation between the left edge of the |
| 196 |
bounding box and the leftmost contour at that height (never negative) |
| 197 |
|
| 198 |
.. object:: right |
| 199 |
|
| 200 |
an array of integers representing the separation between the right edge of |
| 201 |
the bounding box and the rightmost contour at that height (never positive) |
| 202 |
|
| 203 |
``iminY`` and ``imaxY`` are scaled by ``regionHeight`` from the y units in the |
| 204 |
glyph itself. The value contain at left[0] would represent minimum separation |
| 205 |
between boundingbox.minx and any the x location of any contour when the y |
| 206 |
location was between 0 and regionHeight. Similarly left[1] would be the minimum |
| 207 |
separation with the y location between regionHeight and 2*regionHeight. And so |
| 208 |
forth. |
| 209 |
|
| 210 |
:: |
| 211 |
|
| 212 |
imin_y = max(onRight.iminY,onLeft.iminY); |
| 213 |
imax_y = min(onRight.imaxY,onLeft.imaxY); |
| 214 |
if imax_y < imin_y : |
| 215 |
return( 0 ) |
| 216 |
|
| 217 |
Here we figure out the range along the y axis where the glyphs both exist. If |
| 218 |
the two glyphs don't share any area along the y axis (as, for example, the glyph |
| 219 |
"a" and the grave accent might not) then we assume they do not interact and |
| 220 |
return a visual separation of 0. |
| 221 |
|
| 222 |
:: |
| 223 |
|
| 224 |
tot,cnt = 0,0 |
| 225 |
j = imin_y |
| 226 |
while j<=imax_y : |
| 227 |
if onRight.left[j] < 32767 and onLeft.right[j] > -32767 : |
| 228 |
sep = onRight.left[j] - onLeft.right[j] |
| 229 |
weight = 1.0/(sep + context.denom) |
| 230 |
weight *= weight |
| 231 |
tot += weight*sep |
| 232 |
cnt += weight |
| 233 |
j += 1 |
| 234 |
|
| 235 |
Otherwise loop over the y range where both glyphs are active. Checking to make |
| 236 |
sure there are no holes. |
| 237 |
|
| 238 |
We find the actual separation between the two glyphs at this y value. |
| 239 |
|
| 240 |
Then we calculate some magic weighting function (this is handwaving. your |
| 241 |
routine will have a better weighting function) |
| 242 |
|
| 243 |
:: |
| 244 |
|
| 245 |
if cnt!=0 : |
| 246 |
tot /= cnt |
| 247 |
return( int(round( tot )) ); |
| 248 |
|
| 249 |
And finally we take the weighted average of those separations, and return that |
| 250 |
as the optical separation. |
| 251 |
|
| 252 |
:: |
| 253 |
|
| 254 |
fontforge.registerGlyphSeparationHook(GlyphSeparation) |
| 255 |
|
| 256 |
And this routine will tell FontForge to use the hook you provide. |