| 1 |
--- |
| 2 |
layout: default |
| 3 |
title: GDL Code Snippets |
| 4 |
nav_order: 50 |
| 5 |
parent: Developers |
| 6 |
--- |
| 7 |
|
| 8 |
# GDL Code Snippets |
| 9 |
|
| 10 |
* [Diacritic attachment](graphite_codeSnippets#diacritic-attachment) |
| 11 |
* [Diacritics with calculated attachment points](graphite_codeSnippets#diacritics-with-calculated-attachment-points) |
| 12 |
* [Stacking diacritics](graphite_codeSnippets#stacking-diacritics) |
| 13 |
* [Bridging diacritics](graphite_codeSnippets#bridging-diacritics) |
| 14 |
* [Ligatures (conjuncts)](graphite_codeSnippets#ligatures-conjuncts) |
| 15 |
* [Ligatures with components](graphite_codeSnippets#ligatures-with-components) |
| 16 |
* [Contextual shaping](graphite_codeSnippets#contextual-shaping) |
| 17 |
* [Word-contextual shaping](graphite_codeSnippets#word-contextual-shaping) |
| 18 |
* [Reordering](graphite_codeSnippets#reordering) |
| 19 |
* [Splitting](graphite_codeSnippets#splitting) |
| 20 |
* [Font features (variations)](graphite_codeSnippets#font-features-variations) |
| 21 |
* [Boolean feature](graphite_codeSnippets#boolean-feature) |
| 22 |
* [Multi-valued feature](graphite_codeSnippets#multi-valued-feature) |
| 23 |
|
| 24 |
These snippets are intended as a quick reference. For complete documentation of the GDL language, download the reference paper: |
| 25 |
|
| 26 |
[Download GDL.pdf](assets/resources/GDL.pdf){: .btn .btn-blue } |
| 27 |
|
| 28 |
## Diacritic attachment |
| 29 |
|
| 30 |
``` |
| 31 |
table(glyph) |
| 32 |
|
| 33 |
// These can also be classes, in which case the attachment point(s) |
| 34 |
// must be defined on each element of the class: |
| 35 |
base = glyphid(34) {TopS = point(130m, 512m); BottomS = point(130m, 0m)}; |
| 36 |
upperdiac = glyphid(101) {TopM = point(86m, -12m)} |
| 37 |
lowerdiac = glyphid(102) {BottomM = point(86m, 43m)}; |
| 38 |
|
| 39 |
endtable; // glyph |
| 40 |
|
| 41 |
table(positioning) |
| 42 |
|
| 43 |
base upperdiac {attach {to=@1; at=TopS; with=TopM}}; |
| 44 |
base lowerdiac {attach {to=@1; at=BottomS; with=BottomM}}; |
| 45 |
|
| 46 |
endtable; // positioning |
| 47 |
``` |
| 48 |
|
| 49 |
### Diacritics with calculated attachment points |
| 50 |
|
| 51 |
``` |
| 52 |
table(glyph) |
| 53 |
|
| 54 |
base = glyphid(...) |
| 55 |
{TopS = point(bb.lsb + bb.width/2, bb.top); |
| 56 |
BottomS = point(bb.lsb + bb.width/2, bb.bottom)}; |
| 57 |
upperdiac = glyphid(...) {TopM = point(bb.width/2, bb.bottom - 10m)} |
| 58 |
lowerdiac = glyphid(...) {BottomM = point(bb.width/2, bb.top + 10m)}; |
| 59 |
|
| 60 |
endtable; // glyph |
| 61 |
|
| 62 |
table(positioning) |
| 63 |
|
| 64 |
base upperdiac {attach {to=@1; at=TopS; with=TopM}}; |
| 65 |
base lowerdiac {attach {to=@1; at=BottomS; with=BottomM}}; |
| 66 |
|
| 67 |
endtable; // positioning |
| 68 |
``` |
| 69 |
|
| 70 |
### Stacking diacritics |
| 71 |
|
| 72 |
The following example shows how to handle a stack/chain of both upper and lower diacritic attachments. Note that, as specified by the Unicode standard, lower diacritics are first. |
| 73 |
|
| 74 |
``` |
| 75 |
table(glyph) |
| 76 |
|
| 77 |
base1 = glyphid(34) {TopS = point(130m, 512m); BottomS = point(130m, 0m)}; |
| 78 |
base1 = glyphid(35) {TopS = point(130m, 5108m); BottomS = point(130m, -18m)}; |
| 79 |
|
| 80 |
upperdiac1 = glyphid(101) {TopM = point(86m, -12m); TopS = point(86m, 43m)}; |
| 81 |
upperdiac2 = glyphid(105) {TopM = point(81m, -10m); TopS = point(81m, 52m)}; |
| 82 |
|
| 83 |
lowerdiac1 = glyphid(102) {BottomM = point(79m, 43m); BottomS = point(79m, -3m)}; |
| 84 |
lowerdiac2 = glyphid(103) {BottomM = point(77m, 48m); BottomS = point(77m, -5m)}; |
| 85 |
// etc. |
| 86 |
|
| 87 |
base = (base1, base2, ...); |
| 88 |
upperdiac = (upperdiac1, upperdiac2, ...); |
| 89 |
lowerdiac = (lowerdiac1, lowerdiac2, ...); |
| 90 |
|
| 91 |
takes_lower = (base, lowerdiac); // lower diacritics can be attached to bases or other lower diacs |
| 92 |
takes_upper = (base, upperdiac); // upper diacritics can be attached to bases or other upper diacs |
| 93 |
|
| 94 |
endtable; // glyph |
| 95 |
|
| 96 |
table(positioning) |
| 97 |
|
| 98 |
// Optionally permit up to three intervening lower diacritics: |
| 99 |
#define LOWERSEQ [ lowerdiac [ lowerdiac lowerdiac? ]? ]? |
| 100 |
|
| 101 |
takes_lower lowerdiac {attach {to=@1; at=BottomS; with=BottomM}} / _ ^ _; |
| 102 |
|
| 103 |
takes_upper upperdiac {attach {to=@1; at=TopS; with=TopM}} / _ LOWERSEQ ^ _; |
| 104 |
|
| 105 |
endtable; // positioning |
| 106 |
``` |
| 107 |
|
| 108 |
### Bridging diacritics |
| 109 |
|
| 110 |
Bridging diacritics must be centered over the two bases they bridge and must be higher than the tallest. |
| 111 |
|
| 112 |
``` |
| 113 |
table(glyph) |
| 114 |
|
| 115 |
base1 = ...; |
| 116 |
base2 = ...; |
| 117 |
bridgediac = ...; |
| 118 |
|
| 119 |
endtable; // glyph |
| 120 |
|
| 121 |
table(positioning) |
| 122 |
|
| 123 |
bridgediac {shift.y = max(@B1.boundingbox.top, @B2.boundingbox.top) - @D.boundingbox.bottom + 100m ; // gap of 100m |
| 124 |
shift.x = (@B2.boundingbox.width - @B1.boundingbox.width)/2} |
| 125 |
/ base1=B1 _=D base2=B2; |
| 126 |
|
| 127 |
endtable; // positioning |
| 128 |
If the base glyphs have other diacritics attached, using @B1.boundingbox.top.1 will give the bounding box of the base plus diacritics. |
| 129 |
``` |
| 130 |
|
| 131 |
## Ligatures (conjuncts) |
| 132 |
|
| 133 |
Ligatures or conjuncts occur when multiple characters combine to form a single glyph shape. |
| 134 |
|
| 135 |
``` |
| 136 |
table(glyph) |
| 137 |
|
| 138 |
char1 = ...; |
| 139 |
char2 = ...; |
| 140 |
ligature = ...; |
| 141 |
|
| 142 |
endtable; // glyph |
| 143 |
|
| 144 |
table(substitution) |
| 145 |
|
| 146 |
char1 char2 > ligature:(1 2) _ ; |
| 147 |
|
| 148 |
endtable; // substitution |
| 149 |
``` |
| 150 |
|
| 151 |
### Ligatures with components |
| 152 |
|
| 153 |
Ligature components correspond to sub-regions of the ligature glyph that visually correspond to the original characters. Selecting and manipulating the visual components allows manipulating the underlying characters. |
| 154 |
|
| 155 |
{: .blue-note } |
| 156 |
> **Note** |
| 157 |
> |
| 158 |
> This feature is only supported by the original Graphite engine; it is not supported by Graphite2. |
| 159 |
|
| 160 |
``` |
| 161 |
table(glyph) |
| 162 |
|
| 163 |
char1 = ...; |
| 164 |
char2 = ...; |
| 165 |
|
| 166 |
ligature = ... // <-- specify which glyph |
| 167 |
// Define the visual components of the conjunct. |
| 168 |
// This conjunct has three components: |
| 169 |
// the top-left, the bottom-left, and the right side. |
| 170 |
{ component {cTL= box(0, bb.height/2, aw/2, bb.top); |
| 171 |
cBL= box(0, bb.bottom, aw/2, bb.height/2); |
| 172 |
cR = box(aw/2, bb.bottom, aw, bb.top) } }; |
| 173 |
|
| 174 |
endtable; // glyph |
| 175 |
|
| 176 |
table(substitution) |
| 177 |
|
| 178 |
char1 char2 char3 > ligature:(1 2 3) {component {cTL.ref=@1; cBL.ref=@2; cR=@3}} _ _; |
| 179 |
|
| 180 |
endtable; // substitution |
| 181 |
``` |
| 182 |
|
| 183 |
## Contextual shaping |
| 184 |
|
| 185 |
Many scripts have characters that must use alternate forms depending on neighboring letters. |
| 186 |
|
| 187 |
``` |
| 188 |
table(glyph) |
| 189 |
|
| 190 |
// Define the glyphs and classes: |
| 191 |
matraI = ...; |
| 192 |
matraI_wide = ...; |
| 193 |
matraI_wider = ...; |
| 194 |
matraI_widest = ...; |
| 195 |
|
| 196 |
class_wide = ...; |
| 197 |
class_wider = ...; |
| 198 |
class_widest = ...; |
| 199 |
|
| 200 |
endtable; // glyph |
| 201 |
|
| 202 |
table(substitution) |
| 203 |
|
| 204 |
// Choose the width of the matra-I that matches the width of the neighboring letter: |
| 205 |
matraI class_wide > matraI_wide @2; |
| 206 |
matraI class_wider > matraI_wider @2; |
| 207 |
matraI class_widest > matraI_widest @2; |
| 208 |
|
| 209 |
endtable; |
| 210 |
``` |
| 211 |
|
| 212 |
### Word-contextual shaping |
| 213 |
|
| 214 |
Arabic is an example of a script whose characters take on alternate forms depending on their location within the word: initial, medial, final, isolate. |
| 215 |
|
| 216 |
``` |
| 217 |
table(glyph) |
| 218 |
|
| 219 |
// All characters are initially isolate forms. |
| 220 |
|
| 221 |
// Define the individual glyphs: |
| 222 |
beh = ...; |
| 223 |
behInit = ...; |
| 224 |
// etc. |
| 225 |
|
| 226 |
// Elements of these four classes must correspond: |
| 227 |
class_isolate = (beh, teh, theh, peh, teheh, ...); |
| 228 |
class_initial = (behInit, tehInit, thehInit, pehInit, tehehInit, ...); |
| 229 |
class_medial = (behMed, behMed, thehMed, pehMed, tehehMed, ...); |
| 230 |
class_final = (behFin, behFin, thehFin, pehFin, tehehFin, ...); |
| 231 |
|
| 232 |
endtable; // glyph |
| 233 |
|
| 234 |
table(substitution) |
| 235 |
|
| 236 |
// For any contiguous pair of word-forming letters: |
| 237 |
// turn the first from isolate to initial, or from final to medial |
| 238 |
// turn the second to final |
| 239 |
// back up and treat the second as the first of the next pair to consider |
| 240 |
// Any unprocessed letters are left as isolates. |
| 241 |
|
| 242 |
(class_isolate class_final) (class_isolate) > (class_initial class_medial) (class_final) / _ ^ _; |
| 243 |
|
| 244 |
endtable; // substitution |
| 245 |
``` |
| 246 |
|
| 247 |
## Reordering |
| 248 |
|
| 249 |
Many scripts of south and southeast Asia have characters that are rendered in an order that is different from their order in the data. |
| 250 |
|
| 251 |
``` |
| 252 |
table(glyph) |
| 253 |
|
| 254 |
// Define the glyphs or classes: |
| 255 |
consonants = ...; |
| 256 |
vowelLeftSide = ...; |
| 257 |
|
| 258 |
endtable; // glyph |
| 259 |
|
| 260 |
table(substitution) |
| 261 |
|
| 262 |
consonant vowelLeftSide > @2 @1; |
| 263 |
|
| 264 |
endtable; // substitution |
| 265 |
``` |
| 266 |
|
| 267 |
## Splitting |
| 268 |
|
| 269 |
Many scripts of south and southeast Asia have characters that are rendered by two or more non-contiguous glyphs. |
| 270 |
|
| 271 |
``` |
| 272 |
table(glyph) |
| 273 |
|
| 274 |
// Define the glyphs or classes: |
| 275 |
consonants = ...; |
| 276 |
|
| 277 |
// If these are classes, the elements of the classes must correspond: |
| 278 |
vowelSplit = (vowel1, vowel2, vowel3, ...); |
| 279 |
vowelLeftHalf = (vowel1Left, vowel2Left, vowel3Left, ...); |
| 280 |
vowelRightHalf = (vowel1Right, vowel2Right, vowel3Right, ...); |
| 281 |
|
| 282 |
endtable; // glyph |
| 283 |
|
| 284 |
table(substitution) |
| 285 |
|
| 286 |
_ consonant vowelSplit > vowelLeftHalf$3:3 @2 vowelRightHalf:3; |
| 287 |
|
| 288 |
end-table; // substitution |
| 289 |
``` |
| 290 |
|
| 291 |
## Font features (variations) |
| 292 |
|
| 293 |
Font features provide a way to define alterative renderings that can be selected by the user. |
| 294 |
|
| 295 |
### Boolean feature |
| 296 |
|
| 297 |
``` |
| 298 |
table(feature) |
| 299 |
|
| 300 |
altk { |
| 301 |
id = "altk"; // can be integer or 4-char string |
| 302 |
name.1033 = string("Alternate K"); // 1033 = LG_USENG, US English |
| 303 |
default = 0; |
| 304 |
settings { |
| 305 |
off { |
| 306 |
value = 0; |
| 307 |
name.1033 = string("False"); |
| 308 |
} |
| 309 |
on { |
| 310 |
value = 1; |
| 311 |
name.1033 = string("True"); |
| 312 |
} |
| 313 |
} |
| 314 |
}; |
| 315 |
|
| 316 |
endtable; // feature |
| 317 |
|
| 318 |
table(glyph) |
| 319 |
|
| 320 |
g_k = ...; |
| 321 |
g_k_alt = ...; |
| 322 |
|
| 323 |
endtable; // glyph |
| 324 |
|
| 325 |
table(substitution) |
| 326 |
|
| 327 |
if (altk) |
| 328 |
g_k > g_k_alt |
| 329 |
endif; |
| 330 |
|
| 331 |
endtable; // substitution |
| 332 |
``` |
| 333 |
|
| 334 |
### Multi-valued feature |
| 335 |
|
| 336 |
``` |
| 337 |
table(feature) |
| 338 |
|
| 339 |
altk { |
| 340 |
id = "altk"; // can be integer or 4-char string |
| 341 |
name.1033 = string("Alternate K"); // 1033 = LG_USENG, US English |
| 342 |
default = 0; |
| 343 |
settings { |
| 344 |
form1 { |
| 345 |
value = 0; |
| 346 |
name.1033 = string("Form 1"); |
| 347 |
} |
| 348 |
form2 { |
| 349 |
value = 1; |
| 350 |
name.1033 = string("Form 2"); |
| 351 |
} |
| 352 |
form3 { |
| 353 |
value = 2; |
| 354 |
name.1033 = string("Form 3"); |
| 355 |
} |
| 356 |
form4 { |
| 357 |
value = 3; |
| 358 |
name.1033 = string("Form 4"); |
| 359 |
} |
| 360 |
} |
| 361 |
}; |
| 362 |
|
| 363 |
endtable; // feature |
| 364 |
|
| 365 |
table(glyph) |
| 366 |
|
| 367 |
g_k = ...; |
| 368 |
g_k1 = ...; |
| 369 |
g_k2 = ...; |
| 370 |
g_k3 = ...; |
| 371 |
g_k4 = ...; |
| 372 |
|
| 373 |
endtable; // glyph |
| 374 |
|
| 375 |
table(substitution) |
| 376 |
|
| 377 |
// Note that for mutually exclusive conditions, successive |
| 378 |
// 'if' statements are more efficient than 'elseif'. |
| 379 |
|
| 380 |
if (altk == form1) |
| 381 |
g_k > g_k1; |
| 382 |
endif; |
| 383 |
|
| 384 |
if (altk == form2) |
| 385 |
g_k > g_k2; |
| 386 |
endif; |
| 387 |
|
| 388 |
if (altk == form3) |
| 389 |
g_k > g_k3; |
| 390 |
endif; |
| 391 |
|
| 392 |
if (altk == form4) |
| 393 |
g_k > g_k3; |
| 394 |
endif; |
| 395 |
|
| 396 |
endtable; // substitution |
| 397 |
``` |