Debian Codegraph @

site/tutorial/graide_tutorial3.md

raw
1 ---
2 layout: default
3 title: Unit 3
4 nav_order: 30
5 parent: Graphite Tutorial
6 grand_parent: Developers
7 ---
8
9 {: .tut-nav-bar }
10 | [◀ Unit 2: A very simple GDL program](graide_tutorial2) | [▲ Contents](../graide_tutorial#contents) | [Unit 4: Corresponding glyph classes ▶](graide_tutorial4) |
11
12 # Unit 3: The glyph table
13
14 [Exercises](graide_tutorial3#exercise-3a)
15
16 ## Discussion
17
18 In addition to the tables that contain rules, there are tables containing other information. One of these is the glyph table. Among other purposes, the glyph table can be used to define classes of glyphs, and/or give meaningful names to single glyphs. The glyph table is marked by the following syntax:
19
20 ```
21 table(glyph)
22 <glyph class definitions>
23 endtable;
24 ```
25
26 The glyph class definitions include lists of glyphs or codepoints that comprise the members of the class. The syntax of the glyph class definition is the following:
27
28 ```
29 class-name = <list-of-glyphs>;
30 ```
31
32 For instance, you can give names to a group of digit characters as follows:
33
34 ```
35 gZero = U+0030;
36 gOne = U+0031;
37 gTwo = U+0032;
38 gThree = U+0033;
39 gFour = U+0034;
40 gFive = U+0035;
41 gSix = U+0036;
42 gSeven = U+0037;
43 gEight = U+0038;
44 gNine = U+0039;
45 clsDigit = (U+0030..U+0039);
46 ```
47
48 Notice the final statement defines a class of glyphs that includes ten digits. It could also have been written as follows:
49
50 ```
51 clsDigit = (gZero, gOne, gTwo, gThree, gFour, gFive, gSix, gSeven, gEight, gNine);
52 ```
53
54 Parentheses are needed around the list of glyphs when more than one item is included. (Note: the use of an initial “g” to indicate single-glyph definitions and “cls” to indicate multiple-glyph classes is simply a convention.)
55
56 In Unit 2 we mentioned briefly that glyphid can be used to reference a glyph directly, i.e.:
57
58 ```
59 glyphid(<number>);
60 ```
61
62 where the number is the ID of the glyph in the font. (Note that glyphid can be less useful than postscript because it is more font-specific and therefore makes a program that is not as easily converted to work with a different font.)
63
64 A fourth way to identify a glyph is using the codepoint function. It takes as an argument either a string that contains a list of characters or a numerical value. The codepage can be included as an optional second argument (Codepage 1252 is the default).
65
66 ```
67 codepoint(“a”);
68 codepoint(97, 1252);
69 ```
70
71 As indicated in the example above, the codepoint, unicode, and glyphid functions can specify ranges of values by including the “..” syntax, for instance:
72
73 ```
74 clsLowercase = codepoint(97..122); // a through z
75 ```
76
77 (Note that a comment is preceded by two slashes as shown above.) When a glyph has been given a name within the glyph table, that name can be used within a rule in place of the functions. The following is equivalent to the rule discussed in Unit 2 that replaces a’s with b’s.
78
79 ```
80 table(glyph)
81 g_a = unicode(0x61);
82 g_b = unicode(0x62);
83 endtable;
84
85 table(sub)
86 g_a > g_b;
87 endtable;
88 ```
89
90 ## Exercise 3a
91
92 Rewrite your program from Exercise 2 to define the dollar and pound glyphs in the glyph table.
93
94 [Solution](graphite_tut_solutions#exercise-3a)
95
96 ### Exploring Graide: the Passes and Glyph tab
97
98 After Exercise 3a is correct, let’s take a few minutes to explore a couple features of Graide. Enter the following data in the text data pane: $hello$. Click the right-pointing arrow to run it. The result should look like: **£hello£**.
99
100 The Passes pane is displayed automatically. It shows the original glyphs (labeled “Init”) and the result of Graphite’s rendering for each pass. Since there is only one pass in this program, there are two rows in the Passes pane: the original glyphs (“Init”) and the output of Pass 1. The second column shows the glyphs that were output for the pass, and the third column shows the corresponding glyph names.
101
102 {: .image-tight }
103 ![Passes tab in Graide](../assets/images/graide3_1_passesTestsTabs.png)
104
105 <figcaption>Passes tab in Graide</figcaption>
106
107 Glyph names are the names given to single glyphs in your GDL program. For instance, if your GDL source code included the line
108
109 ```
110 gDollar = unicode(0x0024);
111 ```
112
113 you will see “gDollar” twice in the third column of the first row.
114
115 Click on one instance of “gDollar”, and then click on the Glyph tab. This area shows basic information about the dollar (‘$’) glyph from the font and your GDL program.
116
117 {: .image-tight }
118 ![Passes and Glyph tab in Graide](../assets/images/graide3_2_passesGlyphTabs.png)
119
120 <figcaption>Passes and Glyph tab in Graide</figcaption>
121
122 You can also click on the glyph itself in the second column of the Passes tab to select it. But notice that you must carefully click on the black part of the glyph; clicking on white pixels inside or near the glyph will have no effect. (So clicking on the glyph name in the third column may be easier!)
123
124 Notice that some of the glyphs in your test data were not given names in your GDL program. For these glyphs Graide generates a name based on the Postscript name from the font. The autogenerated name has “g_” prepended, and any uppercase letters are replaced by an underscore plus the lowercase letter. For instance:
125
126 | Glyph | Postscript name | Graide name |
127 |:------|:----------------|:------------|
128 | a | a | g_a |
129 | B | B | g__b |
130 | 5 | five | g_five |
131 | ( | parenleft | g_parenleft |
132 | È | Egrave | g__egrave |
133
134 ## Exericse 3b
135
136 Write a program to replace all digits with an asterisk. Use the glyph table to define a class containing the digit characters.
137
138 [Solution](graphite_tut_solutions#exercise-3b)
139
140 ### Exploring Graide: the Passes and Glyph tab
141
142 After Exercise 3b is correct, let’s explore another feature of Graide. Enter the following test data: 234.5; click the arrow to render it. The result should be: **\*\*\*.\***.
143
144 {: .image-tight }
145 ![Passes tab in Graide](../assets/images/graide3_3_PassesTestsTab2.png)
146
147 <figcaption>Passes tab</figcaption>
148
149 The Passes pane is displayed automatically. It shows the initial glyphs and the result of Graphite’s rendering for the single pass in this program.
150
151 The row labeled **Pass: 1 – substitution** is highlighted in yellow, indicating that there was at least one rule matched in that pass. When you double-click on the yellow cell, the Rules tab appears. Each row of the Rules pane corresponds to one instance of a rule that was matched in pass 1. For this example, if your code looks like the supplied solution, all the rows are labeled **Rule: 0** since that is the only rule in the program.
152
153 {: .image-tight }
154 ![Rules tab in Graide](../assets/images/graide3_4_RulesTab.png)
155
156 <figcaption>Rules tab</figcaption>
157
158 Note that the output of any rule becomes the input to the next rule fired. So the glyphs shown in row 2 are the output of the first application of the rule, but are also the glyphs that are matched in order to fire the rule the second time.
159
160 In each row, the glyphs that are generated—that is, modified—by a rule are highlighted in green. For this program, these are the asterisks. The glyphs that were matched in the previous row are highlighted in pink. As you can see, each row in the Rules tab represents an instance of **Rule 0** applied to successive glyphs in the data.
161
162 Double-clicking on any of the cells labeled Rule: 0 will highlight the rule in the code pane.
163
164 {: .image-tight }
165 ![Highlighting a rule from the Rules tab](../assets/images/graide3_5_RulesHighlightRule.png)
166
167 <figcaption>Highlighting a rule from the Rules tab</figcaption>
168
169 {: .tut-nav-bar }
170 | [&#x25C0; Unit 2: A very simple GDL program](graide_tutorial2) | [&#x25B2; Contents](../graide_tutorial#contents) | [Unit 4: Corresponding glyph classes &#x25B6;](graide_tutorial4) |