/usr/share/doc/xgridfit/html/expressions.html is in xgridfit-doc 2.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Xgridfit</title>
<link rel="stylesheet" href="oeg.css" media="screen" type="text/css" />
<link rel="stylesheet" href="parchment.css" media="screen"
type="text/css" title="parchment" />
<link rel="alternate stylesheet" href="legible.css" media="screen"
type="text/css" title="legible" />
<style type="text/css" media="print"> @import "oeg.print.css"; </style>
<meta name="AUTHOR" content="Peter S. Baker" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="jumplist">
<a href="http://sourceforge.net"><img src="" width="125" height="37" border="0" alt="SourceForge.net Logo" /></a>
<a href="http://xgridfit.sourceforge.net/">Home Page</a>
<a href="http://sourceforge.net/projects/xgridfit">Project Page</a>
<a href="http://sourceforge.net/project/showfiles.php?group_id=159705">Download</a>
<a href="http://xgridfit.cvs.sourceforge.net/xgridfit/xgridfit/">CVS repository</a>
<hr/>
<a href="#expressions">Expressions</a>
<a href="#formulas">Formulas</a>
</div>
<div id="content">
<h1>Expressions and Formulas</h1>
<p>
The TrueType engine is capable of performing simple
arithmetical operations, and it is easy to combine these to
perform more complex operations. You must always remember,
however, that TrueType arithmetic operates on F26dot6
fixed-point numbers, and all operations return the same
kind of fixed-point numbers. There is no way to obtain
results with higher precision. Thus certain kinds of
calculations are impossible in TrueType, and you must always
be careful, when combining the operations that TrueType
can do, to consider the limited precision of the
intermediate results that get passed from operation to
operation.
</p>
<p>
There are two ways to combine operations in Xgridfit. One is
to write expressions rather like the expressions used in
other programming languages (though fewer operators are
available); the other is to use <formula> elements,
within which operations can be chained.
</p>
<h2 id="expressions">Expressions</h2>
<p>
Expressions in Xgridfit resemble expressions in other
programming languages: they consist of numbers and
identifiers coordinated with operators; they can be simple
(e.g. "bottom-pt + 1") or complex; Xgridfit parses them
according to certain rules of precedence which are worth
knowing; and the rules of precedence may be overridden by
using parentheses, which can be nested.
</p>
<p>
There is little point in describing the syntax of
expressions in detail, since they are so familiar to
everyone who has done any programming; instead this section
will list the operators, note a few peculiarities, and
present some examples
</p>
<table id="op-table">
<thead>
<tr><th colspan="2">Operators</th></tr>
</thead>
<tbody>
<tr><th colspan="2">First precedence</th></tr>
<tr><td>and</td><td>Logical and.
<b>Example:</b> <tt>pixels-per-em &gt;
10 and pixels-per-em &lt; 20</tt></td></tr>
<tr><td>or</td><td>Logical or.
<b>Example:</b> <tt>pixels-per-em &lt; 10 or
pixels-per-em &gt; 20</tt></td></tr>
<tr><th colspan="2">Second precedence</th></tr>
<tr><td>=</td><td>Equals.
<b>Example:</b> <tt>pixels-per-em = 15</tt></td></tr>
<tr><td>&gt;</td><td>Greater than.
<b>Example:</b> <tt>control-value(lc-vert-stem)
&gt; 1p</tt></td></tr>
<tr><td>&lt;</td><td>Less than.
<b>Example:</b> <tt>control-value(lc-vert-stem)
&lt; 1p</tt></td></tr>
<tr><td>&gt;=</td><td>Greater than or equal.
<b>Example</b> (where <tt>v</tt> has
previously been declared as a variable): <tt>v
&gt;= 0.35</tt></td></tr>
<tr><td>&lt;=</td><td>Less than or equal.
<b>Example:</b> <tt>v
&lt;= 0.35</tt></td></tr>
<tr><td>!=</td><td>Not equal.
<b>Example:</b> <tt>round(control-value(left-side)) !=
control-value(left-side)</tt></td></tr>
<tr><th colspan="2">Third precedence</th></tr>
<tr><td>+</td><td>Addition. <b>Example:</b>
<tt>top-point + 1</tt></td></tr>
<tr><td>-</td><td>Subtraction. <b>Example:</b>
<tt>top-point - 1</tt></td></tr>
<tr><td>*</td><td>Multiplication. <b>Example:</b>
<tt>lc-vert-stem * 1.2</tt></td></tr>
<tr><td>/</td><td>Division. <b>Example:</b>
<tt>lc-vert-stem / 2.0</tt></td></tr>
<tr><th colspan="2">Fourth precedence</th></tr>
<tr>
<td>--</td>
<td>
Treats the arguments on both sides of the
operator as point numbers and returns the
current distance (in pixels) between them,
as measured on the projection
vector. Ordinarily the argument on the
left-hand side of the operator should be the
point on the left or bottom; reverse the
numbers to change the sign of the
result.<br/><b>Example:</b>
<tt>round(stem-left -- stem-right)</tt>
</td>
</tr>
<tr>
<td>---</td>
<td>
Like <tt>--</tt>, but returns the distance
between points in the original
outline.<br/><b>Example:</b>
<tt>absolute((stem-left --- stem-right) - (stem-left -- stem-right))</tt>
</td>
</tr>
<tr><th colspan="2">Fifth precedence</th></tr>
<tr><td>odd</td><td>True if the argument is odd.
<b>Example:</b> <tt>odd(v)</tt></td></tr>
<tr><td>even</td><td>True if the argument is even.
<b>Example:</b> <tt>even v</tt></td></tr>
<tr><td>not</td><td>Reverses the boolean value of
the argument.
<b>Example:</b> <tt>not(v &gt; 4.0)</tt></td></tr>
<tr><td>floor</td><td>The greatest integer value less than
the argument. <b>Example:</b>
<tt>floor(control-value(lc-vert-stem))</tt></td></tr>
<tr><td>ceiling</td><td>The smallest integer value greater
than the argument. <b>Example:</b>
<tt>ceiling(control-value(lc-vert-stem) / 2)</tt></td></tr>
<tr><td>absolute</td><td>The absolute value of the argument.
<b>Example:</b>
<tt>absolute(control-value(lc-vert-stem))</tt></td></tr>
<tr><td>negative</td><td>The negation of the argument.
<b>Example:</b> <tt>negative(v)</tt></td></tr>
<tr>
<td>round</td>
<td>
The argument rounded according to the
current round state. The "color" is the
<default> set with
<tt>type="color"</tt> (gray if not
set). <b>Example:</b>
<tt>round(control-value(lc-vert-stem))</tt>
</td>
</tr>
<tr>
<td>round-gray</td>
<td>
The argument rounded according to the
current round state. The "color" is
gray. <b>Example:</b>
<tt>round-gray(control-value(lc-vert-stem))</tt>
</td>
</tr>
<tr>
<td>round-black</td>
<td>
The argument rounded according to the
current round state. The "color" is
black. <b>Example:</b>
<tt>round-black(control-value(lc-vert-stem))</tt>
</td>
</tr>
<tr>
<td>round-white</td>
<td>
The argument rounded according to the
current round state. The "color" is
white. <b>Example:</b>
<tt>round-white(control-value(lc-vert-stem))</tt>
</td>
</tr>
<tr><td>index</td><td>Returns an index of (pointer to) a
control value or variable. <b>Example:</b>
<tt>index(lc-vert-stem)</tt></td></tr>
<tr>
<td>control-value</td>
<td>
The control value at the index represented
by the argument. <b>Example:</b>
<tt>control-value(lc-vert-stem)</tt>
</td>
</tr>
<tr>
<td>coord</td>
<td>
The current coordinate (x or y depending on
the projection vector) of a point. <b>Example:</b>
<tt>coord(stem-left)</tt>
</td>
</tr>
<tr>
<td>initial-coord</td>
<td>
Like <tt>coord</tt>, but returns the
original coordinate of a point: that is, its
coordinate at the beginning of the glyph
program. <b>Example:</b>
<tt>initial-coord(bottom-point + 1)</tt>
</td>
</tr>
<tr>
<td>x-coord</td>
<td>
Like <tt>coord</tt>, but always returns the
current coordinate of a point on the x
axis. The setting of the projection vector
is the same after the operation as
before. <tt>coord</tt> is more efficient
than this and the following three operators
when the coordinate of a point on the
current projection vector is needed.
</td>
</tr>
<tr>
<td>y-coord</td>
<td>
Like <tt>coord</tt>, but always returns the
current coordinate of a point on the y
axis. The setting of the projection vector
is the same after the operation as before.
</td>
</tr>
<tr>
<td>initial-x-coord</td>
<td>
Like <tt>initial-coord</tt>, but always
returns the original coordinate of a point
on the x axis. The setting of the projection
vector is the same after the operation as
before.
</td>
</tr>
<tr>
<td>initial-y-coord</td>
<td>
Like <tt>initial-coord</tt>, but always
returns the original coordinate of a point
on the y axis. The setting of the projection
vector is the same after the operation as
before.
</td>
</tr>
<tr>
<td>variable</td>
<td>
Treats the argument as an index of (pointer
to) a variable and returns its
value. <b>Example:</b> <tt>variable(v)</tt>
</td>
</tr>
<tr>
<td>nan</td>
<td>
Returns true (1) if the argument cannot be
resolved to a number at compile-time
(e.g. it is the name of a variable, or the
name of a <range>). If the argument is
a number, returns false (0).
<b>Example:</b> <tt>nan(v)</tt>
</td>
</tr>
<tr>
<td>point</td>
<td>
Causes the compiler to regard the argument
as a point number. In a <glyph>
program with an <tt>offset</tt> parameter,
the <tt>offset</tt> is automatically added
to the argument. Use this operator in
contexts where the compiler does not
automatically recognize a number as a point
number. Do not use it in the <tt>num</tt>
attribute of a <point> element, as
this will cause the <tt>offset</tt> to be
added twice. <b>Example:</b> <tt>point(a) --
point(b)</tt>
</td>
</tr>
</tbody>
</table>
<p>
Here is an example of precedence:
</p>
<pre>
<if test="pixels-per-em &lt; 10 or pixels-per-em &gt; 20 and
round-state = to-grid">
</pre>
<p>
Xgridfit breaks the expression at the "or" (which has the
same precedence as the "and" but occurs farther to the
left); it evaluates first <tt>"pixels-per-em &lt;
10"</tt>, second <tt>"pixels-per-em &gt; 20 and
round-state = to-grid"</tt>, and finally
executes OR on the two values. If that is not what you want,
you may use parentheses to alter the order in which
constituents are evaluated:
</p>
<pre>
<if test="(pixels-per-em &lt; 10 or pixels-per-em &gt; 20) and
round-state = to-grid">
</pre>
<p>
Now Xgridfit breaks the expression at the "and" and
evaluates everything to the left of it (inside the
parentheses), then everything to the right of it, and
finally executes AND.
</p>
<p>
Fifth-precedence operators are all unary: they operate
on a single value. If this is a simple value it may be
separated from the operator by a space; if it is an
expression it must be enclosed in parentheses.
</p>
<p>
Binary operators (those that operate on two values) must
always be surrounded by whitespace. This will not work:
</p>
<pre>
<point num="top+2"/>
</pre>
<p>
It must be like this:
</p>
<pre>
<point num="top + 2"/>
</pre>
<p>
However, the whitespace may be any number of spaces, tabs, a
line break, and so on, for the spacing of an expression is
always normalized before it is evaluated.
</p>
<p>
Note that when all of the values in an expression
are number literals, constants or other
identifiers that can be resolved to numbers at
compile time, and the only operators are first- or
second-precedence operators and the arithmetic
operators "+" and "-", Xgridfit resolves the whole
expression to a single number at compile
time. This optimizes the most common cases, where
a point number is expressed by addition to or
subtraction from a constant.
</p>
<h2 id="formulas">Formulas</h2>
<p>
A <formula> is a list of arithmetical operations whose
result can be assigned to a variable, a control value, or
any of those graphics variables that can be written to. The
format is like this:
</p>
<pre>
<formula result-to="minimum-distance">
<divide dividend="minimum-distance" divisor="2.0"/>
<add value1="0.5"/>
</formula>
</pre>
<p>
The <formula> may contain any of the arithmetic
elements: <add>, <subtract>, <divide>,
<multiply>, <absolute>, <negate>,
<floor>, <ceiling>, <minimum>,
<maximum>, <round>. These can also occur outside
the <formula>, but they behave differently there. When
one of these elements outside the formula lacks a
<tt>result-to</tt> attribute, Xgridfit tries to write the
result back to one of the operands; failing that, it leaves
the result on the run-time stack and prints a warning. But
within the <formula> no attempt is made to write the
result to one of the operands, and no warning is printed
when the result is left on the stack; instead, it is assumed
that the next arithmetic element will take one or both of
its operands from the stack.
</p>
<p>
In the example above, the current minimum distance
is divided by two, and the result of that operation
is passed to the <add> element, where 0.5 is added to
this new value; then the sum is passed back to the
<formula> element, which writes it to the
<tt>minimum-distance</tt> graphics variable, thus setting a
new minimum distance.
</p>
<p>
The <formula> element was added to Xgridfit before the
expression feature, and it looks rather clunky in
comparison. The same operation could be performed more
tersely thus:
</p>
<pre>
<set-minimum-distance value="(minimum-distance / 2.0) + 0.5"/>
</pre>
<p>
and the code generated by Xgridfit would be the same.
</p>
</div>
</body>
</html>
|