Erik van Blokland 3646055ea2 initial import
git-svn-id: http://svn.robofab.com/trunk@1 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
2008-01-07 17:40:34 +00:00

402 lines
31 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>
RoboThon 2006: Font and Info objects
</title>
<link href="../default.css" type="text/css" rel="stylesheet" />
<link href="../code/robothon06/py2html.css" type="text/css" rel="stylesheet" />
</head><body>
<script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script>
<script type="text/javascript">
_uacct = "UA-2044310-2";
urchinTracker();
</script>
<div id="modellogo">
<img src="../img/drawmodel_header.jpg" width="595" height="112" />
</div>
<div class="leftcontent">
<h1 class="crb-seealso">
RoboFab
</h1>
<p class="menu">
<a href="../index.html">
Home
</a>
<br />
<a href="../download/index.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="../howto/index.html">
How to's
</a>
<br />
<a href="index.html">
Step by step
</a>
<br />
<a href="../tools/dialogs.html">
Fab Dialogs
</a>
<br />
<a href="../objects/index.html">
Object reference
</a>
<br />
<a href="../objects/model.html">
Object map
</a>
<br />
<a href="../tools/index.html">
Tools
</a>
<br />
<a href="../ufo/index.html">
UFO Overview
</a>
<br />
<a href="../ufo/specification.html">
UFO Spec
</a>
<br />
<a href="../limitations.html">
Fab Limitations
</a>
<a href="../links/index.html">
Links
</a>
<br />
<a href="../credits.html">
Credits
</a>
</p>
<h1 class="crb-seealso">
Support RoboFab
</h1>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but21.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----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-----END PKCS7-----
">
</form>
<h2 class="crb-seealso">
Up
</h2>
<p class="menu">
<p class="bonbon_uplink"><a href="index.html">Back to Step by step</a></p>
</p>
<h2 class="crb-seealso">
See also
</h2>
<p class="menu">
<a href="../howto/generatefonts.html">Generate Fonts</a><br /><a href="../howto/interpolate.html">How to interpolate</a><br /><a href="../objects/anchor.html">RAnchor</a><br /><a href="../objects/bpoint.html">bPoint</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/index.html">Objects</a><br /><a href="../objects/info.html">RInfo</a><br /><a href="../objects/kerning.html">RKerning</a><br /><a href="../objects/lib.html">RLib</a><br /><a href="../objects/pen.html">Pen objects</a><br /><a href="../objects/point.html">RPoint</a><br /><a href="../objects/segment.html">RSegment</a><br /><a href="robofab_session2.html">Glyph object and Pen object</a><br /><a href="robofab_session3.html">Kerning object and glyph building</a><br /><a href="robofab_session5.html">Interpolation</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-2006 RoboFab
<br />
version 1.1.1
</div>
<div class="google">
<h2>RoboFab Mailinglist</h2>
<p>
Join the RoboFab users community at Google groups.
</p>
<img src="http://groups.google.com/groups/img/3nb/groups_bar.gif" alt="Google Groups" height="26" width="132" />
<p>
<form action="http://groups.google.com/group/robofab/boxsubscribe">
Email: <input type="text" name="../email" />
<input type="submit" name="../sub" value="Subscribe" />
</form>
</p>
<p>
<a href="http://groups.google.com/group/robofab">Visit this group</a>
</p>
<h2>RoboFab Sponsors</h2>
<script type="text/javascript">
google_ad_client = "pub-3703953449723704";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "336699";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>
<div class="content">
<h1>
Font & Info objects
</h1>
<img src="http://letterror.com/code/robofab/img/offdrawmodel_07.gif" width="140" height="96" />
<h2>
Code!
</h2>
<p>
So now then, you should have your editor fired up. Your RoboFab installed. Locate the
<strong>
output
</strong>
window as well.
</p>
<div class="pythonsource"><pre><span class="py_res">import</span> <span class="py_key">robofab</span>.<span class="py_key">world</span></pre></div>
<p>
If that runs without problems you're good to go. If a new window pops up with a <strong>traceback</strong> like this it means there's something wrong with the installation.
</p>
<div class="pythonoutput"><pre><span class="py_key">Traceback</span> (<span class="py_key">most</span> <span class="py_key">recent</span> <span class="py_key">call</span> <span class="py_key">last</span>):<br><span class="py_key">File</span> <span class="py_str">"&lt;string&gt;"</span>, <span class="py_key">line</span> <span class="py_num">1</span>, <span class="py_res">in</span><span class="py_com"> </span><span class="py_com">?</span><br><span class="py_key">ImportError</span>: <span class="py_key">No</span> <span class="py_key">module</span> <span class="py_key">named</span> <span class="py_key">robofab</span>.<span class="py_key">world</span></pre></div>
<p>
In this documentation, stuff in the output window is indicated with a tinted background. Whenever something is printed in Python code it will end up in the output window.
</p>
<h2>
Huh, import ?
</h2>
<p>
Python can do a lot of different things. Some of its functionality is always available (the
<strong>
built-in
</strong>
things) but most of it is stored in seperate
<strong>
modules</strong>. When you want to use code from a different module, you need to
<strong>
import
</strong>
it first so that Python knows it needs to look somewhere else for objects, functions and stuff. Most of the Robofab stuff is stored in the
<strong>
robofab.world
</strong>
module. Notice that dot there? The dot syntax also works for modules and modules within modules. If you want to import a module and Python can't find it, you will get a traceback with an <strong>ImportError</strong>. You can also import specific things from another module, then you write:
</p>
<div class="pythonsource"><pre><span class="py_res">from</span> <span class="py_key">someModule</span> <span class="py_res">import</span> <span class="py_key">oneSpecificThing</span><br><span class="py_res">from</span> <span class="py_key">someModule</span>.<span class="py_key">subModule</span> <span class="py_res">import</span> <span class="py_key">oneSpecificThing</span><br><span class="py_res">from</span> <span class="py_key">someModule</span> <span class="py_res">import</span> <span class="py_key">anotherSpecificThing</span>, <span class="py_key">andAnotherThing</span><br> <br># and these:<br><span class="py_res">import</span> <span class="py_key">someModule</span><br><span class="py_res">import</span> <span class="py_key">someModule</span>.<span class="py_key">subModule</span></pre></div>
<h2>CurrentFont()</h2>
<p>
So, suppose you have FontLab, and a font file open. Make sure it is a font you can trash if you have to, and not the single copy of the production master of your newest bestseller. How do you get started talking to that font in Python? Use <strong>CurrentFont()</strong>. This is a special function which will return an object for the font which is at the front. When there are no fonts it will return <strong>None</strong>.
</p>
<div class="pythonsource"><pre><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br><span class="py_res">print</span> <span class="py_key">CurrentFont</span>()</pre></div>
<div class="pythonoutput"><pre>&lt;<span class="py_key">RFont</span> <span class="py_key">font</span> <span class="py_res">for</span> <span class="py_key">DemoFont</span> <span class="py_key">Italic</span>&gt;</pre></div>
<p>
A <a href="http://letterror.com/code/robofab/objects/font.html" target="new" class="reference">Font</a> object! We'll be using CurrentFont and that font object shortly, but first let's have a look at CurrentFont's siblings: <strong>CurrentGlyph</strong> and <strong>AllFonts</strong>.
</p>
<div class="pythonsource"><pre># open a glyph in FL first!<br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentGlyph</span><br><span class="py_res">print</span> <span class="py_key">CurrentGlyph</span>()</pre></div>
<div class="pythonoutput"><pre>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">DemoFont</span>.<span class="py_key">ograve</span>&gt;</pre></div>
<p>
<strong>CurrentGlyph()</strong> returns a <a href="http://letterror.com/code/robofab/objects/glyph.html" target="new" class="reference">Glyph</a> object for the glyph which is at the front. So this is a useful place to start if you want to write a script which manipulates a single glyph and you want an object for that glyph.
</p>
<div class="pythonsource"><pre># open a couple of fonts in FL first!<br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">AllFonts</span><br><span class="py_res">print</span> <span class="py_key">AllFonts</span>()</pre></div>
<div class="pythonoutput"><pre>[&lt;<span class="py_key">RFont</span> <span class="py_key">font</span> <span class="py_res">for</span> <span class="py_key">MyDemoFont</span>&gt;,<br>&lt;<span class="py_key">RFont</span> <span class="py_key">font</span> <span class="py_res">for</span> <span class="py_key">AnotherFont</span> <span class="py_key">Plain</span>&gt;,<br>&lt;<span class="py_key">RFont</span> <span class="py_key">font</span> <span class="py_res">for</span> <span class="py_key">AnotherFont</span> <span class="py_key">Italic</span>&gt;]</pre></div>
<p>
<strong>AllFonts()</strong> returns a list with Font objects, one object for each open font. CurrentFont, CurrentGlyph and AllFonts are three very useful functions, and they all live in the robofab.world module. We'll be using them a lot.
</p>
<h2>Some Font attributes</h2>
<p>
So what are attributes of fonts objects? Let's have a look (at the documentation!).
</p>
<div class="pythonsource"><pre># open a couple of fonts in FL first!<br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">path</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">kerning</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span></pre></div>
<div class="pythonoutput"><pre>/<span class="py_key">aFolder</span>/<span class="py_key">anotherFolder</span>/<span class="py_key">demoStuff</span>/<span class="py_key">myFont</span>.<span class="py_key">vfb</span><br>&lt;<span class="py_key">RKerning</span> <span class="py_res">for</span> <span class="py_key">MyFont</span> <span class="py_key">Plain</span>&gt;<br>&lt;<span class="py_key">RInfo</span> <span class="py_res">for</span> <span class="py_key">MyFont</span> <span class="py_key">Plain</span>&gt;</pre></div>
<p>
Hang on! that didn't print anything that looks like kerning, and what's that font.info thing? Remember that objects can contain objects? The object model splits all font related data into smaller, easier to manage pieces. So a <strong>Font</strong> object has one single <strong>Info</strong> object which in turn stores all of the naming and dimensions. Same for font.kerning, it's an object which represents all kerning data of the font. We'll get back to the <a href="http://letterror.com/code/robofab/objects/kerning.html" target="new" class="reference">kerning object</a> later.
</p>
<h2>Some Info attributes</h2>
<p>
The <strong>Info</strong> object stores all of the <a href="http://letterror.com/code/robofab/objects/info.html" target="new" class="reference">font's names, key dimensions</a> etc.
</p>
<div class="pythonsource"><pre># robothon06<br># getting data from the info object<br> <br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br> <br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br># naming attributes<br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">familyName</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">styleName</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">fullName</span><br># dimension attributes<br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">unitsPerEm</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">ascender</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">descender</span><br></pre></div><div class="pythonsourcetitle"><a href="examples/someFontInfoAttributes.py" target="new">download examples/someFontInfoAttributes.py</a></div>
<div class="pythonoutput"><pre><span class="py_key">MyDemo</span><br><span class="py_key">Plain</span><br><span class="py_key">MyDemo</span> <span class="py_key">Plain</span><br><span class="py_num">1000</span><br><span class="py_num">720</span><br>-<span class="py_num">280</span></pre></div>
<p>
Almost all attributes can also be set to new values. This is when it starts getting interesting. But it also opens new ways of messing your font up.
</p>
<div class="pythonsource"><pre># robothon06<br># setting data in the info object<br> <br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br> <br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br># naming attributes<br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">familyName</span> = <span class="py_str">"MyFamily"</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">familyName</span><br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">styleName</span> = <span class="py_str">"Roman"</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">styleName</span><br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">fullName</span> = <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">familyName</span> + <span class="py_str">'-'</span> + <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">styleName</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">fullName</span><br># dimension attributes<br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">ascender</span> = <span class="py_num">600</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">ascender</span><br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">descender</span> = -<span class="py_num">400</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">descender</span><br><span class="py_key">font</span>.<span class="py_key">update</span>()<br> <br></pre></div><div class="pythonsourcetitle"><a href="examples/someFontInfoAttributes2.py" target="new">download examples/someFontInfoAttributes2.py</a></div>
<div class="pythonoutput"><pre><span class="py_key">MyFamily</span><br><span class="py_key">Roman</span><br><span class="py_key">MyFamily</span>-<span class="py_key">Roman</span><br><span class="py_num">600</span><br>-<span class="py_num">400</span></pre></div>
<p>
A useful method of the Info object is <strong>autoNaming()</strong>. It assumes you have entered correct data for <strong>familyName</strong> and <strong>styleName</strong>. Based on these 2 values, a bunch of variations and permutations are generated and stored in the appropriate fields. These are the basic names, no fancy OpenType stuff.
</p>
<div class="pythonsource"><pre># robothon06<br># get a particular glyph<br> <br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br> <br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">familyName</span> = <span class="py_str">"myFamilyName"</span><br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">styleName</span> = <span class="py_str">"myStyleName"</span><br> <br><span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">autoNaming</span>()<br> <br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">fullName</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">fontName</span><br><span class="py_res">print</span> <span class="py_key">font</span>.<span class="py_key">info</span>.<span class="py_key">fondName</span></pre></div><div class="pythonsourcetitle"><a href="examples/infoAutonaming.py" target="new">download examples/infoAutonaming.py</a></div>
<div class="pythonoutput"><pre><span class="py_key">myFamilyName</span> <span class="py_key">myStyleName</span><br><span class="py_key">myFamilyName</span>-<span class="py_key">myStyleName</span><br><span class="py_key">myFamilyName</span></pre></div>
<h2>Getting to glyphs</h2>
<p>
We've seen <strong>CurrentGlyph</strong> and <strong>CurrentFont</strong>, but how do you we get to other glyphs in a font? A <strong>Font</strong> object contains glyphs and this is what you do to get to them:
</p>
<div class="pythonsource"><pre># robothon06<br># get a particular glyph<br> <br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br><span class="py_res">print</span> <span class="py_key">font</span>[<span class="py_str">'A'</span>]<br><span class="py_res">print</span> <span class="py_key">font</span>[<span class="py_str">'Adieresis'</span>]<br><span class="py_res">print</span> <span class="py_key">font</span>[<span class="py_str">'two'</span>]<br><span class="py_res">print</span> <span class="py_key">font</span>[<span class="py_str">'afii12934'</span>]<br> <br></pre></div><div class="pythonsourcetitle"><a href="examples/getSomeGlyphs.py" target="new">download examples/getSomeGlyphs.py</a></div>
<div class="pythonoutput"><pre>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">A</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Adieresis</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">two</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">afii12934</span>&gt;</pre></div>
<p>
The Font object in this case behaves like a Python dictionary object. Between the <strong>[</strong> square brackets <strong>]</strong> you can ask for a glyph by its (postscript) name. In Python speak:
</p>
<div class="pythonsource"><pre><span class="py_key">value</span> = <span class="py_key">dictionary</span>[<span class="py_key">key</span>]</pre></div>
<p>
If you want to look at all glyphs in a font, one at a time, you can <strong>loop</strong> or <strong>iterate</strong> through the font. It's written like this:
</p>
<div class="pythonsource"><pre># robothon06<br># iteration through glyphs in a font<br> <br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br> <br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br><span class="py_res">print</span> <span class="py_str">"font has %d glyphs"</span> % <span class="py_key">len</span>(<span class="py_key">font</span>)<br><span class="py_res">for</span> <span class="py_key">glyph</span> <span class="py_res">in</span> <span class="py_key">font</span>:<br>&nbsp; &nbsp; <span class="py_res">print</span> <span class="py_key">glyph</span><br> <br></pre></div><div class="pythonsourcetitle"><a href="examples/iterateFont.py" target="new">download examples/iterateFont.py</a></div>
<div class="pythonoutput"><pre><span class="py_key">font</span> <span class="py_key">has</span> <span class="py_num">201</span> <span class="py_key">glyphs</span><br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">aring</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">ordfeminine</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">less</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">ograve</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">V</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">dollar</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">circumflex</span>&gt;<br>..<span class="py_key">etc</span>..</pre></div>
<p>
A couple of things to look for in the example above:
<ul>
<li><strong>len(font)</strong> shows Python's built-in <strong>len()</strong> function, which will try to count the thing its given and it will return the number. Fonts like to be counted and they respond with the number of glyphs. In this case the font has 201 glyphs.</li>
<li>
All the glyphs are mixed up! there is no particular order! chaos! In Python dictionaries there is no standard order in which the keys appear. It will iterate through all the glyphs though.
</li>
<li>Notice the indentation at the beginning of the line under <strong>for glyph in font:</strong> This is Python's way of showing that all of the code that's indented belongs to the same loop. When the code is <strong>dedented</strong> again that's where Python will continue when it is done with the loop.
</li>
</ul>
</p>
<p>
When you want to be sure about the order in which the glyphs are looked at, you need to sort them first. Example:
</p>
<div class="pythonsource"><pre># iteration through alphabetically sorted glyphnames<br><span class="py_res">from</span> <span class="py_key">robofab</span>.<span class="py_key">world</span> <span class="py_res">import</span> <span class="py_key">CurrentFont</span><br> <br><span class="py_key">font</span> = <span class="py_key">CurrentFont</span>()<br><span class="py_res">print</span> <span class="py_str">"font has %d glyphs"</span> % <span class="py_key">len</span>(<span class="py_key">font</span>)<br> <br># names is now a list of strings, the names of the glyphs<br># not the glyphs themselves!<br><span class="py_key">names</span> = <span class="py_key">font</span>.<span class="py_key">keys</span>()<br> <br># the list of names is sorted<br><span class="py_key">names</span>.<span class="py_key">sort</span>()<br> <br># now we iterate through the list of names<br><span class="py_res">for</span> <span class="py_key">glyphName</span> <span class="py_res">in</span> <span class="py_key">names</span>:<br>&nbsp; &nbsp; # now we ask for the glyph with glyphName<br>&nbsp; &nbsp; <span class="py_res">print</span> <span class="py_key">font</span>[<span class="py_key">glyphName</span>]</pre></div>
<div class="pythonoutput"><pre><span class="py_key">font</span> <span class="py_key">has</span> <span class="py_num">201</span> <span class="py_key">glyphs</span><br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">A</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">AE</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Aacute</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Acircumflex</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Adieresis</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Agrave</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Aring</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">Atilde</span>&gt;<br>&lt;<span class="py_key">RGlyph</span> <span class="py_res">for</span> <span class="py_key">MyFamily</span>-<span class="py_key">Roman</span>.<span class="py_key">B</span>&gt;<br>..<span class="py_key">etc</span>..</pre></div>
</div>
</body></html>