Debian Codegraph @

doc/sphinx/tutorial/editexample6-5.rst

raw
1 Conditional Features
2 ====================
3
4 OpenType and Apple fonts both provide contextual features. These are features
5 which only take place in a given context and are essential for typesetting Indic
6 and Arabic scripts. In OpenType a context is specified by a set of patterns that
7 are tested against the glyph stream of a document. If a pattern matches then any
8 substitutions it defines will be applied. In an Apple font, the context is
9 specified by a state machine -- a mini program which parses and transforms the
10 glyph stream.
11
12 Conditional features may involve substitutions, ligatures or kerning (and some
13 more obscure behaviors). First I shall provide an example of a contextual
14 substitution, later of :ref:`contextual ligatures <editexample6-5.Greek>`.
15
16 .. image:: /images/bed-script.png
17 :align: right
18
19 Instead of an Indic or Arabic example, let us take something I'm more familiar
20 with, the problem of typesetting a latin script font where the letters "b," "o,"
21 "v" and "w" join their following letter near the x-height, while all other
22 letters join near the baseline. Thus we need two variants for each glyph, one
23 that joins (on the left) at the baseline (the default variant) and one which
24 joins at the x-height. Let us call this second set of letters the "high" letters
25 and name them "a.high," "b.high" and so forth.
26
27
28 OpenType Example
29 ----------------
30
31 .. warning::
32
33 The following example may not work! The font tables produced by it are all
34 correct, but early implementations of OpenType did not support decided the
35 "Contextual Alternative" ('calt') feature for the latin script.
36
37 Why do I provide an example which might not work? It's the best I can do. If
38 I knew enough about Indic or Arabic typesetting I would provide an example
39 for those scripts. But I don't. The procedures are the same. If you follow
40 them for some other scripts they will work.
41
42 As time has gone on 'calt' has been supported by more applications for latn
43 and this warning is no longer as applicable as it once was.
44
45 We divide the set of possible glyphs into three classes: the letters "bovw", all
46 other letters, and all other glyphs. We need to create two patterns, the first
47 will match a glyph in the "bovw" class followed by another glyph in the "bovw"
48 class, while the second will match a glyph in the "bovw" class followed by any
49 other letter. If either of these matches the second glyph should be transformed
50 into its high variant.
51
52 * [bovw] [bovw] => Apply a substitution to second letter
53 * [bovw] <any other letter> => Apply a substitution to the second letter
54
55 (You might wonder why I don't just have a class of all letters and use one rule
56 instead of two? Because in this case all my classes must be disjoint, I mayn't
57 have one glyph appearing in two classes).
58
59 The first thing we must do is create a simple substitution mapping each low
60 letter to its high variant. This is a "Simple Substitution" lookup, but it will
61 not be attached to any feature, instead it will be invoked by a contextual
62 lookup. Let us call this lookup "high". We must (of course) create a subtable to
63 go with our lookup, and we can use the [Default with Suffix:] button to fill it
64 up with the high variants.
65
66 The tricky part is defining the context. This is done by defining yet another
67 lookup, a contextual chaining lookup which should be associated with a 'calt'
68 feature. And of course we want an associated subtable). This will pop up a
69 series of dialogs to edit a contextual subtable
70
71 .. image:: /images/contextchain-format.png
72 :align: left
73
74 The first dialog allows you to specify the overall format of the substitution.
75 We want a class based system -- we've already mentioned what the glyph classes
76 will be.
77
78 .. image:: /images/contextchain-simpleclasses.png
79 :align: right
80
81 The next dialog finally shows something interesting. At the top is a pattern to
82 match and substitutions that will be applied if the string matches. Underneath
83 that are the glyph classes that this substitution uses. A contextual chaining
84 dialog divides the glyph stream into three sections: those glyphs before the
85 current glyph (these are called backtracking glyphs), the current glyph(s)
86 itself (themselves), which may have simple substitutions applied to them, and
87 finally glyphs after the current glyph (these are called lookahead glyphs).
88
89 Each category of glyphs may divide glyphs into a different set of classes, but
90 in this example we use the same classes for all categories (this makes it easier
91 to convert the substitution to Apple's format). The first line (in the "Matching
92 rules" field) should be read thus: If a backtracking glyph (the glyph before the
93 current one) in class "high-after" is followed by the current glyph in class
94 "letter", then that letter in the match string (that is the current glyph)
95 should have simple substitution "To-TopJoin" applied to it.
96
97 If you look at the glyph class definitions you will see that class "high-afters"
98 includes those glyphs which must be followed by a high variant, so this seems
99 reasonable.
100
101 Now there should be a second line which would look like
102 ``"high-after | high-after @<To-TopJoin> |"`` to handle the second case
103 mentioned above. Unfortunately that doesn't seem to work. The OpenType spec
104 allows for it, but implementations of OpenType don't seem to support it. Instead
105 that line must be in a separate sub-table in the same lookup.
106
107 To edit a glyph class simply click on it and type. To create a new one press the
108 <New> button (at the bottom of the list). You can also click on the little box
109 thingy off on the right. This produces another dialog showing a font view. You
110 can select glyphs in the font view if that is easier than remembering their
111 names.
112
113 You may edit a pattern in the obvious way. As you enter glyph names they will be
114 auto completed for you. There are two buttons down at the bottom of the pattern
115 list, one to start a new section (go from back tracking to active, or from
116 active to lookahead), and one to add a new lookup reference.
117
118 .. warning::
119
120 **A warning about contextual behavior:** Not all software supports them. And
121 even more confusing software may support them for some scripts and not for
122 others.
123
124
125 .. _editexample6-5.Apple:
126
127 Apple advanced typography
128 -------------------------
129
130 Apple specifies a context with a finite state machine, which is essentially a
131 tiny program that looks at the glyph stream and decides what substitutions to
132 apply. Each state machine has a set of glyph class definitions (just as in the
133 OpenType example), and a set of states. The process begins in state 0 at the
134 start of the glyph stream. The computer determines what class the current glyph
135 is in and then looks at the current state to see how it will behave when given
136 input from that class. The behavior includes the ability to change to a
137 different state, advancing the input to the next glyph, applying a substitution
138 to either the current glyph or a previous one (the "marked" glyph).
139
140 .. image:: /images/sm-picture.png
141 :align: right
142
143 Using the same example of a latin script font... We again need a simple
144 substitution to convert each letter into its high alternate. The process is the
145 same as it was for OpenType, and indeed we can use the same substitution. Again
146 we divide the glyphs into three classes (Apple gives us some extra classes
147 whether we want them or no, but conceptually we use the same three classes as in
148 the OpenType example). We want a state machine with two states (again Apple
149 gives us an extra state for free, but we shall ignore that), one is the start
150 state (the base state -- where nothing changes), and the other is the state
151 where we've just read a glyph from the "bovw" class.
152
153 .. image:: /images/asm1.png
154 :align: left
155
156 Apple Advanced Typography does not quite fit into the OpentType concepts of
157 lookups and features, but it is close enough that I sort of force it to. So once
158 again we create a GSUB lookup. This time the lookup type is "Mac Contextual
159 State Machine", and the feature is actually a mac feature/setting, two numbers.
160 When we create a new subtable of this type we get a state machine dialog, as
161 shown below.
162
163 .. image:: /images/asm2.png
164 :align: right
165
166 At the top of the dialog we see a set of class definitions, and at the bottom is
167 a representation of the state machine itself.
168
169 .. image:: /images/asm3.png
170 :align: left
171
172 Double clicking on a class brings up a dialog similar to that used in OpenType
173
174 .. image:: /images/asm4.png
175 :align: right
176
177 Clicking on a transition in the state machine (there is a transition for each
178 state / class combination) produces a transition dialog. This controls how the
179 state machine behaves when it is in a given state and receives a glyph in a
180 given class. In this example it is in state 2 (which means it has already read a
181 "bovw" glyph), and it has received a glyph in class 4 (which is another "bovw"
182 glyph). In this case the next state will be state 2 again (we will have just
183 read a new "bovw" glyph), read another glyph and apply the "high" substitution
184 to the current glyph.
185
186 At the bottom of the dialog are a series of buttons that allow you to navigate
187 through the transitions of the state machine.
188
189 Pressing [OK] many times will extract you from this chain of dialogs and add a
190 new state machine to your font.
191
192
193 .. _editexample6-5.Greek:
194
195 OpenType, Greek ligatures
196 -------------------------
197
198 Greek has a character (U+03D7) which is equivalent to the Latin ampersand. Just
199 as the ampersand is (originally) a ligature of "E" and "t", so U+03D7 is a
200 ligature of "kappa" and "iota". However this ligature should only be used if
201 "kappa" and "iota" make up a word unto themselves, it should not be used for
202 more normal occurrences of the two within a longer word.
203
204 .. image:: /images/kappa_iota-lookup.png
205 :align: right
206
207 So the first thing to do is create the ligature itself. Add the glyph for
208 U+03D7, and then create a ligature lookup and subtable (with
209 :ref:`Element->Font Info->Lookups) <fontinfo.Lookups>` to bind U+03D7 to be a
210 ligature of "kappa" and "iota". This lookup will never be used directly -- only
211 under the control of another, a conditional feature -- so we don't give it a
212 feature tag.
213
214 .. image:: /images/kappa_iota-subtable.png
215
216 Next the conditional bit.
217
218 I'm going to use the notation <letters> to represent a class consisting of all
219 greek letters.
220
221 #. <letters> kappa iota => no substitution
222 #. kappa iota <letters> => no substitution
223 #. kappa iota => apply the ligature "WORD"
224
225 :small:`(Now as I read the standard all these rules could be put into one subtable, and the font validation tools I have agree with me -- but the layout engines do not. The layout engines seem to insist that each rule live in its own subtable. This is inconvenient (the classes must be defined in each subtable) but it seems to work.)`
226
227 These rules will be executed in order, and the first one that matches the input
228 text will be the (one and only) rule applied. Consider these three strings,
229 :math:`\alpha\kappa\iota`, :math:`\kappa\iota\theta`, :math:`\alpha\kappa\iota\theta`
230 all contain kappa and iota but each contains more letters around them, so none
231 should be replaced by the ligature.
232
233 * The first string, :math:`\alpha\kappa\iota`, will match the first rule above (it
234 contains letters before the kappa iota sequence) and no substitution will be done.
235 It also matches the third rule, but we never get that far.
236 * The second string, :math:`\kappa\iota\theta`, will match the second rule above
237 (it contains letters after the sequence) and again no substitution will be done.
238 It would match the third rule, but we stop with the first match.
239 * The third string, :math:`\alpha\kappa\iota\theta`, matches all the rules, but
240 since the search stops at the first match, only the first rule will be applied,
241 and no substitution will be done.
242 * The string, :math:`\_\kappa\iota\_`, matches neither of the first two rules
243 but does match the last, so here the ligature will be formed.
244
245 You might wonder why I don't just have one rule
246
247 #. <any non-letter> kappa iota <any non-letter> => apply our ligature
248
249 It seems much simpler.
250
251 Well there's one main reason:
252
253 * This does not work if the kappa is at the beginning of the input stream (it will
254 not be preceded by any glyphs, but might still need replacing), or iota at the
255 end.
256
257 Now how do we convert these rules into a contextual lookup?
258
259 .. image:: /images/kappa_iota-context.png
260 :align: left
261
262 We use :doc:`Element->Font Info->Lookups->Add Lookup </ui/dialogs/lookups>` to
263 create a new contextual chaining lookup. This is the top level lookup and should
264 be bound to a feature tag in the Greek script.
265
266 We have three rules, each rule lives in its own subtable, so we will create
267 three subtables, one for each. The order in which these subtables n the Lookups
268 pane is important because that is the order in which the rules they contain will
269 be executed. We must insure that that final rule which actually invokes the
270 ligature is the last one executed (and the last one in the list).
271
272 .. rubric:: Steps
273
274 .. image:: /images/hligbyclasses.png
275
276 Since we are planning on using the class of all greek letters we will want to
277 use a class format for this feature. Then we press the ``[Next>]`` button.
278
279 .. image:: /images/emptyhlig.png
280
281 The main match will be on the letters kappa and iota in all three rules, so
282 we need one class for each of them. So in the Match Classes area we press the
283 ``[New]`` button...
284
285 .. image:: /images/hligkappaclass.png
286
287 And type in the word "kappa" and press ``[Next>]``
288
289 .. image:: /images/hligkappa.png
290
291 Now we have a class containing the single glyph "kappa". We want to do the
292 same thing for "iota" so we press ``[New]`` again.
293
294 .. image:: /images/hligiotaclass.png
295
296 Again type in "iota" and press ``[Next>]``
297
298 .. image:: /images/hligkappaiota.png
299
300 Now we have all the classes we need here. We still need to create classes for
301 the lookahead and backtrack. We only need one class for these groups and that
302 class will consist of all greek letters.
303
304 .. image:: /images/hligback.png
305
306 The check box ``[*] Same as Match Classes`` is set, but we don't want that,
307 we want our own classes here. So uncheck it.
308
309 .. image:: /images/hligbacknomatch.png
310
311 Now the buttons become active and we can create a new class by pressing
312 ``[New]``
313
314 .. image:: /images/allgreek.png
315
316 Now you could go back to the font view and select all of the greek letters,
317 and then press the [Set From Font] button in the class dialog.
318
319 .. image:: /images/hliggreekclass.png
320
321 But in this case the class we are interested in (all the greek letters) is
322 built it, and you can select it from the Standard Class pulldown list
323 (Letters in script(s)) Then press ``[Next>]``.
324
325 .. flex-grid::
326
327 * - .. image:: /images/hliggreekback.png
328 - .. image:: /images/hliggreekahead.png
329
330 Then go through the same process for the look ahead classes (adding one class
331 which consists of all the greek letters.
332
333 .. image:: /images/hlignewrule.png
334
335 Now we have all our classes defined and are finally ready to create the
336 patterns for our rules. So underneath "List of lists of class numbers" press
337 the ``[New]`` button.
338
339 .. image:: /images/hligbackrule.png
340
341 The first rule begins with all the greek letters in the backtrack area, so
342 click on the "Backtrack" tab, and then press on the class consisting of all
343 the greek letters. This puts the class number into the pattern area (the List
344 of class numbers)
345
346 .. image:: /images/hligrule.png
347
348 In the match area we want to match kappa and then iota, so click on the Match
349 tab, and then on the entries for "kappa" and "iota".
350
351 This rule has no substitutions, so leave the bottom area blank and press
352 ``[Next>]``.
353
354 .. image:: /images/hligbackruledone.png
355
356 We are done with the first rule. It says:
357
358 * The previous character should match class 1 of the backtrack classes (and
359 that class contains all greek letters, which is what we want)
360 * The current character should match class 1 of the match classes (and that
361 class contains "kappa")
362 * The next character should match class 2 of the match classes (which is iota)
363 * And if the match is successful, do absolutely nothing.
364
365 We've got two more rules though, so press ``[OK]`` and then
366 ``[Add Subtable]``. Then go through the process of adding all the classes,
367 and then add the match string for this rule.
368
369 .. image:: /images/hligaheadruledone.png
370
371 We are done with the second rule. It says:
372
373 * The current character should match class 1 of the match classes (and that
374 class contains "kappa")
375 * The next character should match class 2 of the match classes (which is iota)
376 * The character after that should match class 1 of the lookahead classes (and
377 that class contains all the greek letters)
378 * And if the match is successful, do absolutely nothing.
379
380 Press ``[OK]`` and ``[Add Subtable]`` for the final rule.
381
382 .. image:: /images/hligrule.png
383
384 This rule does have substitutions -- we want to take the two characters and
385 convert them into a ligature. So Press ``[New]`` under the sequence position
386 list, we want to start at the first character (sequence position 0) and apply
387 the ligature we called "WORD":
388
389 .. image:: /images/hligseqdlg.png
390
391 So if anything doesn't match the first two rules, and does contain a kappa
392 followed by an iota, it must be a two letter stand-alone greek word. And we
393 want to apply our ligature to it.
394
395 .. image:: /images/hligallrulesdone.png
396
397 Now we are done. Press a series of ``[OK]``\ s until all the dialogs have been
398 accepted.
399
400 .. image:: /images/kappaiota-lookups.png
401
402 Once you have created your lookups you may test the result in the
403 :doc:`metrics view </ui/mainviews/metricsview>`.
404
405 .. image:: /images/metrics-kappa_iota.png
406
407 (This example was provided by Apostolos Syropoulos)