235 lines
7.7 KiB
HTML
235 lines
7.7 KiB
HTML
<!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">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>How to: Glyph Math</title>
|
|
|
|
<link href="../default.css" type="text/css" rel="stylesheet" />
|
|
|
|
</head>
|
|
<body>
|
|
|
|
|
|
|
|
<div id="modellogo">
|
|
<img src="../img/drawmodel_header.jpg" width="595" height="112" />
|
|
</div>
|
|
<div class="leftcontent">
|
|
<h2 class="crb-seealso">
|
|
RoboFab
|
|
</h2>
|
|
<p class="menu">
|
|
<a href="../index.html">
|
|
Home
|
|
</a>
|
|
<br />
|
|
<a href="../download/license.html">
|
|
Download v1.1.1
|
|
</a>
|
|
<br />
|
|
<a href="../intro.html">
|
|
Intro
|
|
</a>
|
|
<br />
|
|
<a href="../install.html">
|
|
Install Notes
|
|
</a>
|
|
<br />
|
|
<a href="../history.html">
|
|
History
|
|
</a>
|
|
<br />
|
|
<a href="../executive.html">
|
|
Summary
|
|
</a>
|
|
</p>
|
|
<p class="menu">
|
|
<a href="index.html">
|
|
How to's
|
|
</a>
|
|
<br />
|
|
<a href="../objects/index.html">
|
|
Fab Objects
|
|
</a>
|
|
<br />
|
|
<a href="../objects/model.html">
|
|
Fab Map
|
|
</a>
|
|
<br />
|
|
<a href="../tools/index.html">
|
|
Fab Tools
|
|
</a>
|
|
<br />
|
|
<a href="../ufo/index.html">
|
|
UFO Overview
|
|
</a>
|
|
<br />
|
|
<a href="../limitations.html">
|
|
Fab Limitations
|
|
</a>
|
|
</p>
|
|
<p class="menu">
|
|
<a href="../links/index.html">
|
|
Links
|
|
</a>
|
|
<br />
|
|
<a href="../glossary/index.html">
|
|
Glossary
|
|
</a>
|
|
<br />
|
|
<a href="../credits.html">
|
|
Credits
|
|
</a>
|
|
</p>
|
|
<br />
|
|
<br />
|
|
<p class="crb-uplink"><a href="index.html">Back to How To</a></p>
|
|
|
|
<br />
|
|
<br />
|
|
<p class="menu">
|
|
<a href="../glossary/glyphmath.html">Glossary: GlyphMath</a><br />
|
|
<a href="../objects/component.html">RComponent</a><br />
|
|
<a href="../objects/contour.html">RContour</a><br />
|
|
<a href="../objects/font.html">RFont</a><br />
|
|
<a href="../objects/glyph.html">RGlyph</a><br />
|
|
<a href="../objects/lib.html">RLib</a><br />
|
|
<a href="../objects/pen.html">Pen objects</a><br />
|
|
<a href="usetransformations.html">Using transformations</a><br />
|
|
|
|
</p>
|
|
</div>
|
|
<div class="footer">
|
|
<a href="../feedback.html">
|
|
Feedback
|
|
</a>
|
|
<br />
|
|
<a href="../map.html">
|
|
Sitemap
|
|
</a>
|
|
<br />
|
|
Please also refer to the
|
|
<a href="../download/license.html">
|
|
Legal
|
|
</a>
|
|
<br />
|
|
Copyright 2003-2005 RoboFab
|
|
<br />
|
|
version 1.1.1
|
|
</div>
|
|
|
|
|
|
|
|
<div class="content">
|
|
<h1>Glyph Math</h1>
|
|
<p>
|
|
RGlyph objects have methods that allow the objects to behave a bit like variables in simple math. These methods do not do additions or substractions of the surface area of the glyphs, like layering two glyphs on top of each other and than doing "remove overlap". Instead, they return new glyph objects in which each coordinate in each contour is the product of the two glyphs.
|
|
</p>
|
|
<h2>Objects</h2>
|
|
<p>
|
|
All glyph math operations in have new, orphaned, objects as result. For instance a substraction of two FontLab RoboFab glyphs will result in a new glyph object, but it won't be part of the font. If you want the result to be part of the font you have to add it explicitly, see the example at the bottom of this page. There are several reasons for this:
|
|
</p>
|
|
<ul>
|
|
<li>
|
|
the result might not even come from glyphs in the same font, i.e. you can substract a glyph in one font from a glyph in another font. Where should the result live? you decide.
|
|
</li>
|
|
<li>
|
|
You might not want the result to be part of your font when you're using it for further calculations. So: results from glyphmath operations are orphan glyphs that do not belong to any font.
|
|
</li>
|
|
<li>
|
|
the results need to use floating point (19.234943) numbers for precision, FontLab only stores integer numbers (19)
|
|
</li>
|
|
</ul>
|
|
<p>
|
|
If you want to add a glyph (of any flavor, FontLab or UFO) to a font use the appendGlyph method:
|
|
</p>
|
|
<pre>
|
|
|
|
someNewGlyph = aFont.newGlyph("someNewGlyph")
|
|
someNewGlyph.appendGlyph(restultFromGlyphMath)
|
|
|
|
# note you have to set the width, appendGlyph does not automatically
|
|
# take the value.
|
|
someNewGlyph.width = restultFromGlyphMath.width
|
|
</pre>
|
|
<h2>Substraction</h2>
|
|
<p>
|
|
Substraction returns a new glyph object with contours which represent the <strong>difference</strong> between the two previous glyphs. As a glyph itself, it's not much to look at. If you draw the result of a substraction it will probably look like a crumpled outline.
|
|
</p>
|
|
<h3>Example Substraction</h3> <pre>f = CurrentFont()
|
|
g = f["a"]
|
|
h = f["b"]
|
|
# suppose g and h have compatible point structures
|
|
myRelativeGlyph = g - h
|
|
</pre> <h2>Addition</h2>
|
|
<p>
|
|
Addition returns a new glyph object with the contours which is the product of the two previous glyphs. If you just add two "normal" glyphs from a font (or multiple fonts for that matter) it will look odd. But you can also easily add a relative glyph (a result of substracting one glyph from another), which effectively means you're applying the difference between two glyphs to a third. And that can be a very useful action.
|
|
</p>
|
|
<h3>Example Addition</h3> <pre># continue with myRelativeGlyph from the previous example
|
|
newglyph = f["x"] + myRelativeGlyph
|
|
</pre> <h2>Multiplication</h2>
|
|
<p>
|
|
When a normal glyph is multiplied it looks as if the glyph has been scaled. For instance multiplying a glyph with 0.5 scales the shapes 50%.
|
|
</p>
|
|
<h3>Example Multiplication</h3> <pre># continue with myRelativeGlyph from the previous example
|
|
newglyph = f["x"] + 0.25 * myRelativeGlyph
|
|
|
|
# this works too:
|
|
newglyph = f["x"] + myRelativeGlyph * 0.25
|
|
|
|
# and this works as well:
|
|
newglyph = f["x"] + myRelativeGlyph * (0.33, 0.99)
|
|
# see what that does? it multiplies with different factors for x and y!
|
|
# for instance:
|
|
newglyph = f["x"] + myRelativeGlyph * (1, 0)
|
|
# would only apply the horizontal effect of the relative glyph to f['x'].
|
|
</pre> <h2>Division</h2>
|
|
<p>
|
|
Divisions works just like multiplications, you just need to make sure not to divide by zero.
|
|
</p>
|
|
<h3>Example Division</h3> <pre># continue with myRelativeGlyph from the previous example
|
|
newglyph = f["x"] + myRelativeGlyph / 4
|
|
|
|
# this works too:
|
|
newglyph = f["x"] + myRelativeGlyph / (0.25, 2)
|
|
</pre> <h2>Combinations</h2>
|
|
<p>
|
|
These examples are simple enough, but when you combine them the operations can become really powerful. You could recreate font interpolation using GlyphMath, or construct new networks of interpolations, additions, shifts, deltas that were impossible to build.
|
|
</p>
|
|
<h3>All together now</h3>
|
|
<p>
|
|
This is from the demo_GlyphMath.py which should be in the Scripts/RoboFabIntro folder.
|
|
</p>
|
|
<pre>
|
|
#FLM: Fun with GlyphMath
|
|
|
|
# this example is meant to run with the RoboFab Demo Font
|
|
# as the Current Font. So, if you're doing this in FontLab
|
|
# import the Demo Font UFO first.
|
|
|
|
from robofab.world import CurrentFont
|
|
from random import random
|
|
|
|
f = CurrentFont()
|
|
condensedLight = f["a#condensed_light"]
|
|
wideLight = f["a#wide_light"]
|
|
wideBold = f["a#wide_bold"]
|
|
|
|
diff = wideLight - condensedLight
|
|
|
|
destination = f.newGlyph("a#deltaexperiment")
|
|
destination.clear()
|
|
x = wideBold + (condensedLight-wideLight)*random()
|
|
|
|
destination.appendGlyph( x)
|
|
destination.width = x.width
|
|
|
|
f.update()
|
|
</pre> <h2>Implementation limits</h2>
|
|
<p>
|
|
In objectsFL (for use in FontLab), only RGlyph has glyphmath operators implemented. The result of a glyphmath operation in FontLab is <strong>always</strong> an object from objectsRF. In ObjectsRF most objects have *, + and - implemented. But considering the operators are mainly used for Glyph stuff, the RGlyph object is a bit more kitted out with division as well.
|
|
</p>
|
|
</div>
|
|
</body>
|
|
</html> |