Debian Codegraph @

doc/sphinx/olddocs/old/ja/scripting-tutorial.html

raw symbols SCIP
1 <HTML>
2 <HEAD>
3 <!-- Created with AOLpress/2.0 -->
4 <!-- AP: Created on: 21-Apr-2006 -->
5 <!-- AP: Last modified: 21-Apr-2006 -->
6 <!--<TITLE>FontForge Scripting Tutorial</TITLE>-->
7 <TITLE>FontForge スクリプトのチュートリアル</TITLE>
8 <LINK REL="icon" href="../../_static/fftype16.png">
9 <LINK REL="stylesheet" TYPE="text/css" HREF="FontForge.css">
10 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
11 </HEAD>
12 <BODY>
13 <DIV id="in">
14 <H1 ALIGN=Center>
15 <!--FontForge Scripting Tutorial-->
16 FontForge スクリプトのチュートリアル
17 </H1>
18 <P>
19 <UL>
20 <LI>
21 <!-- <A HREF="scripting-tutorial.html#simple">A simple example</A> -->
22 <A HREF="scripting-tutorial.html#simple">簡単な例</A>
23 <UL>
24 <LI>
25 <!-- <A HREF="scripting-tutorial.html#simple">Stating the problem</A> -->
26 <A HREF="scripting-tutorial.html#simple">問題の解説</A>
27 <LI>
28 <!-- <A HREF="scripting-tutorial.html#Intial">Initial solution</A> -->
29 <A HREF="scripting-tutorial.html#Intial">とりあえずの解決法</A>
30 <LI>
31 <!-- <A HREF="#RealWorld">Real world considerations</A> -->
32 <A HREF="#RealWorld">実世界で考慮すべき事柄</A>
33 <LI>
34 <!-- <A HREF="#Invoking">Invoking a script and passing it arguments</A> -->
35 <A HREF="#Invoking">スクリプトの起動法・引数の渡し方</A>
36 <LI>
37 <!-- <A HREF="scripting-tutorial.html#loops">Using loops</A> -->
38 <A HREF="scripting-tutorial.html#loops">ループの使い方</A>
39 <LI>
40 <!-- <A HREF="scripting-tutorial.html#Complexities">Complexities</A> -->
41 <A HREF="scripting-tutorial.html#Complexities">より複雑な例</A>
42 </UL>
43 <LI>
44 <!-- <A HREF="scripting-tutorial.html#OtherExamples">Other Examples</A> -->
45 <A HREF="scripting-tutorial.html#OtherExamples">その他の実例</A>
46 <UL>
47 <LI>
48 <!-- <A HREF="#Accented">Adding accented characters to a type1 font</A> -->
49 <A HREF="#Accented">Type1 フォントにアクセントつき文字を追加する</A>
50 <LI>
51 <!-- <A HREF="#GSUB">Merging a type1 and type1 expert font and creating appropriate
52 GSUB tables.</A> -->
53 <A HREF="#GSUB">Type1 フォントと Type1 エキスパートフォントを併合し、適切な GSUB テーブルを作成する</A>
54 </UL>
55 <LI>
56 <!-- <A HREF="scripting.html#Example">Examples elsewhere</A> -->
57 <A HREF="scripting.html#Example">この他の実例集</A>
58 </UL>
59 <P>
60 <!--
61 I try to keep things at a fairly elementary level, but this is <EM>not</EM>
62 an attempt to teach programming. -->
63 説明をとても解りやすくするつもりではありますが、ここでプログラムを教えるつもりは<EM>ありません</EM>。
64 <H2>
65 <!--<A NAME="simple">A simple example</A>-->
66 <A NAME="simple">簡単な例</A>
67 </H2>
68 <P>
69 <!--
70 Suppose you have a Type1 PostScript font (a pfb/afm combination) and you
71 would like to convert it into a TrueType font. What would a script look like
72 that could do this? -->
73 (PFB と AFM が対になった) Type1 形式の PostScript フォントがあって、それを TrueTypeフォントを変換したいものとしましょう。これを行うのにはどのようなスクリプトを書けばいいのでしょう?
74 <P>
75 <!--
76 If you were doing this with the UI you would first
77 <A HREF="filemenu.html#Open">File-&gt;Open</A> the font and then
78 <A HREF="filemenu.html#Generate">File-&gt;Generate</A> a truetype font. You
79 do essentially the same thing when writing a script: -->
80 これを UI 上で行う場合には、フォントを <A HREF="filemenu.html#Open">ファイル((<U>F</U>)→開く(<U>O</U>)</A> で開き、続いて <A HREF="filemenu.html#Generate">ファイル((<U>F</U>)→フォントを出力(<U>G</U>)...</A> で TrueType フォントを書き出します。スクリプトを書く時には、これと本質的に同じ事を行うことになります。
81 <H3>
82 <!--<A NAME="Intial">Intial Solution</A> -->
83 <A NAME="Intial">とりあえずの解決法</A>
84 </H3>
85 <BLOCKQUOTE>
86 <PRE><A HREF="scripting-alpha.html#Open">Open</A>($1)
87 <A HREF="scripting-alpha.html#Generate">Generate</A>($1:r + ".ttf")
88 </PRE>
89 </BLOCKQUOTE>
90 <P>
91 <!--
92 There is usually a scripting function with the same name as a menu command
93 (well, the same as the English name of the menu command). -->
94 大体において、メニューコマンドと同じ名前のスクリプト関数が存在します (まあ、英語メニューのコマンド名と同じ物だけですが)。
95 <P>
96 <!--
97 '<A HREF="scripting.html#variables">$1</A>' is magic. It means the
98 <A HREF="#Invoking">first argument passed to the script</A>. -->
99 '<A HREF="scripting.html#variables">$1</A>' はおまじないです。これは<A HREF="#Invoking">スクリプトに渡す最初の引数</A>の意味です。
100 <P>
101 <!--
102 '<A HREF="scripting.html#Expressions">$1:r + ".ttf" </A>' is even more
103 complicated magic. It means: 'take the first argument ($1) and remove the
104 extension (which is probably ".pfb") and then append the string ".ttf" to
105 the filename.' -->
106 '<A HREF="scripting.html#Expressions">$1:r + ".ttf" </A>' はずっと複雑なおまじないです。その意味は、「最初の引数 (<CODE>$1</CODE>) を取り、拡張子 (おそらく ".pfb" ) を削除し、文字列 ".ttf" をファイル名に付け加える」というものです。
107 <P>
108 <!--
109 The Generate scripting command decides what type of font to generate depending
110 on the extension of the filename you give it. Here we give it an extension
111 of "ttf" which means truetype. -->
112 スクリプトコマンド Generate は、どのタイプのフォントを出力するかを与えられた拡張子によって決定します。この例では“ttf”なので TrueType を指定しています。
113 <P>
114 <!--
115 Note that I make no attempt to load an afm file. That's because the Open
116 command will do this automagically if it is in the same directory as the
117 pfb. -->
118 AFM ファイルを読み込むように指定していないことに注意して下さい。これは、Open コマンドが PFB と同じディレクトリにあるかどうかを自動的にチェックしているからです。
119 <H3>
120 <!--<A NAME="RealWorld">Real World Considerations</A> -->
121 <A NAME="RealWorld">実世界で考慮すべき事柄</A>
122 </H3>
123 <P>
124 <!--
125 So that's what the script looks like. To be useful it should probably live
126 in a file of its own. So create a file called "convert.pe" and store the
127 above script in it. -->
128 スクリプトがどのようなものかについて説明しました。これを実用的な物にするには、このデータを格納する専用のファイルがおそらく必要でしょう。“convert.pe”という名前のファイルを作成し、上記のスクリプトをそれに格納します。
129 <P>
130 <!--
131 But to be even more useful you should add a comment line to the beginning
132 of the script (a comment line is one that starts with the '#' character: -->
133 しかし、それを遥かに有用な物にするために、スクリプトの先頭に 1 行のコメント行を追加するべきです (コメント行とは文字‘#’で始まる行のことです)。
134 <BLOCKQUOTE>
135 <PRE>#!/usr/local/bin/fontforge
136 Open($1)
137 Generate($1:r + ".ttf")
138 </PRE>
139 </BLOCKQUOTE>
140 <P>
141 <!--
142 Having done that type: -->
143 このように記述したならば以下のようにタイプします:
144 <BLOCKQUOTE id="shell">
145 <PRE>$ chmod +x convert.pe
146 </PRE>
147 </BLOCKQUOTE>
148 <P>
149 <!--
150 This comment is not important to fontforge, but it is meaningful to the unix
151 shell, as we will see in the next section. -->
152 このコメントは FontForge にとって重要ではありませんが、Unix シェルでは意味があります。これについては次のセクションで説明します。
153 <H3>
154 <!--<A NAME="Invoking">Invoking a script and passing it arguments</A>-->
155 <A NAME="Invoking">スクリプトの起動法と引数の指定方法</A>
156 </H3>
157 <P>
158 <!--
159 Ok, now we've got basic script. How do we use it? -->
160 OK, これで基本的なスクリプトができました。使うにはどうするのでしょうか?
161 <P>
162 <!--
163 Well we can pass it to FontForge directly by typing -->
164 まあ、以下のようにタイプして FontForge に直接渡すこともできますが、
165 <BLOCKQUOTE id="shell">
166 <PRE>$ fontforge -script convert.pe foo.pfb
167 </PRE>
168 </BLOCKQUOTE>
169 <P>
170 <!--
171 But if you added the comment above you can also type: -->
172 前述のコメントを追加しているならば、以下のようにタイプすれば十分です:
173 <BLOCKQUOTE id="shell">
174 <PRE>$ convert.pe foo.pfb
175 </PRE>
176 </BLOCKQUOTE>
177 <P>
178 <!--
179 And the shell knows to call fontforge to process the script. -->
180 この場合シェルは、スクリプトを処理するために FontForge を呼び出すことを知っています。
181 <H3>
182 <!--<A NAME="loops">Using loops</A> -->
183 <A NAME="loops">ループの使い方</A>
184 </H3>
185 <P>
186 <!--
187 That's all well and good, but if you have lots of fonts to convert this might
188 get tedious. So let's change our script so it will take lots of filenames
189 which we can then process one at a time. -->
190 これでどこにも問題は無いのですが、多数のフォントを変換したいとなった時には、それは面倒な作業となるでしょう。そういうわけで、多数のファイル名をとるように変更し、一度にそれらを処理できるようにしましょう。
191 <BLOCKQUOTE>
192 <PRE>#!/usr/local/bin/fontforge
193 i=1
194 while ( i&lt;$argc )
195 Open($argv[i])
196 Generate($argv[i]:r + ".ttf")
197 i = i+1
198 endloop
199 </PRE>
200 </BLOCKQUOTE>
201 <P>
202 <!--
203 Here we have introduced the variables <CODE>$argc </CODE>and
204 <CODE>$argv</CODE>. The first is simple the number of arguments passed to
205 this script, while the second is an array containing all those arguments,
206 and <CODE>$argv[i]</CODE> means the i'th argument passed. -->
207 ここで我々は <CODE>$argc</CODE> と <CODE>$argv</CODE> という変数を導入しました。前者はこのスクリプトに対して指定した個数を単に表し、後者はそれらの引数をすべて含む配列ですから、<CODE>$argv[i]</CODE> は i 番目に指定した引数ということになります。
208 <P>
209 <!--
210 Then we have: -->
211 次に、以下の記述が現れます:
212 <BLOCKQUOTE>
213 <PRE>i=1
214 </PRE>
215 </BLOCKQUOTE>
216 <P>
217 <!--
218 This declares that we have a local variable called "i" and assigns it the
219 value 1. -->
220 これは、“i”というローカル変数を用いることを宣言し、それに値 1 を代入しています。
221 <P>
222 <!--
223 The while loop will execute all statements between the "<CODE>while</CODE>"
224 keyword and the "<CODE>endloop</CODE>" keyword as long as the condition <CODE>(
225 i&lt;$argv ) </CODE>is true. In other words as long as there are more arguments
226 to convert the loop will keep going. -->
227 while ループは“"<CODE>while</CODE>”キーワードと“<CODE>endloop</CODE>”キーワードの間にあるすべての文を、条件 <CODE>(i&lt;$argv )</CODE> が真である限り実行します。言い替えれば、まだ変換を行うべき引数が存在する限り、このループは実行され続けます。
228 <P>
229 <!--
230 And we can invoke this script with -->
231 これにより、我々はこのスクリプトを
232 <BLOCKQUOTE id="shell">
233 <PRE>$ convert.pe *.pfb
234 </PRE>
235 </BLOCKQUOTE>
236 <P>
237 <!--
238 Or something similar. -->
239 または何らかの同様のコマンドにより実行することができます。
240 <H3>
241 <!--<A NAME="Complexities">Complexities</A> -->
242 <A NAME="Complexities">より複雑な例</A>
243 </H3>
244 <P>
245 <!--
246 Now suppose that you wanted a script that could convert a truetype font to
247 an opentype font as well as a type1 font to a truetype. Well let's make our
248 script even more complex: -->
249 ここで、Type1 フォントから TrueType への変換に加え、TrueType フォントを OpenType フォントに変換することができるスクリプトが必要になったとしましょう。ではこのスクリプトをもっと複雑にしてみましょう:
250 <BLOCKQUOTE>
251 <PRE>#!/usr/local/bin/fontforge
252 i=1
253 format=".ttf"
254 while ( i&lt;$argc )
255 if ( $argv[i]=="-format" || $argv[i]=="--format" )
256 i=i+1
257 format = $argv[i]
258 else
259 Open($argv[i])
260 Generate($argv[i]:r + format)
261 endif
262 i = i+1
263 endloop
264 </PRE>
265 </BLOCKQUOTE>
266 <P>
267 <!--
268 And this could be invoked with something like: -->
269 そして、このスクリプトの起動法は以下のようになります:
270 <BLOCKQUOTE id="shell">
271 <PRE>$ convert.pe --format ".ttf" *.pfb --format ".otf" *.ttf
272 </PRE>
273 </BLOCKQUOTE>
274 <P>
275 <!--
276 So now we have a new variable, <CODE>format</CODE>, which contains the type
277 of output we want to use from now on. We initialize it to truetype, ".ttf",
278 but if the user gives us an argument called "- -format" (or "-format") then
279 we change the output to be whatever the user asked for. -->
280 ここでは、<CODE>format</CODE> という新しい変数を導入しています。これは、そこから先で使用する出力形式を表します。この値はまず TrueType の“.ttf”で初期化されますが、ユーザが“--format”(または“-format”) という引数を指定したならば、どんな出力形式にでも、ユーザが指定して切り替えることができます。
281 <P>
282 <!--
283 We've introduced the "<CODE>if</CODE>" statement here. This statement will
284 execute the statements between "<CODE>if</CODE>" and "<CODE>else</CODE>"
285 if the condition <CODE>( $argv[i]=="-format" || $argv[i]=="- -format" )</CODE>
286 is true, otherwise it will execute the statements between "<CODE>else</CODE>"
287 and "<CODE>endif</CODE>". The || operator means "or", so the condition is
288 true if $argv[i] is either "-format" or "- -format". -->
289 ここで新しく“<CODE>if</CODE>”を付け加えています。この文は、条件 <CODE>( $argv[i]=="-format" || $argv[i]=="--format" )</CODE> が真である場合に“<CODE>if</CODE>”と“<CODE>else</CODE>”の間の各文を実行し、偽の場合には“<CODE>else</CODE>”と“<CODE>endif</CODE>”の間の各文を実行します。 || 演算子は“または”を意味しますので、この条件文は $argv[i] が“-format”または“--format”のどちらかである時に真になります。
290 <P>
291 <!--
292 We really should do some error checking to make sure: -->
293 実際には、以下の事を確認するために、いくつかのエラーチェックを行う必要があります:
294 <UL>
295 <LI>
296 <!-- There was another argument to store into the <CODE>format</CODE> variable -->
297 <CODE>format</CODE> 変数に格納するための次の引数が指定されているか
298 <LI>
299 <!-- The argument contained a reasonable value (.ttf, .pfb, .otf, .svg, ...) -->
300 引数が適切な値 (.ttf, .pfb, .otf, .svg, …) を含んでいるか
301 </UL>
302 <P>
303 <BLOCKQUOTE>
304 <PRE>#!/usr/local/bin/fontforge
305 i=1
306 format=".ttf"
307 while ( i&lt;$argc )
308 if ( $argv[i]=="-format" || $argv[i]=="--format" )
309 i=i+1
310 if ( i&lt;$argc )
311 format = $argv[i]
312 if ( format!=".ttf" &amp;&amp; format!=".otf" &amp;&amp; \
313 format!=".pfb" &amp;&amp; format!=".svg" )
314 <A HREF="scripting-alpha.html#Error">Error</A>( "Expected one of '.ttf', '.otf', '.pfb' or '.svg' for format" )
315 endif
316 endif
317 else
318 Open($argv[i])
319 Generate($argv[i]:r + format)
320 endif
321 i = i+1
322 endloop
323 </PRE>
324 </BLOCKQUOTE>
325 <P>
326 <!--
327 Note that when we had a long line we broke it in two by using a backslash.
328 Normally the end of a line marks the end of a statement, so we need the backslash
329 to indicate the statement continues onto the next line. -->
330 長い行が含まれているため、バックスラッシュを用いて 2 行に分割していることに注意してください。通常行末は文の終わりを表しますので、バックスラッシュを用いて次の行まで文が続いていることを示す必要があるのです。
331 <P>
332 <!--
333 Now that will produce a valid postscript font from a truetype one if we want...
334 But we can improve on that conversion: -->
335 これで、TrueType から PostScript フォントを変換したい場合にも、正しく出力ができるようになりました。しかし、この変換をより改善することができます:
336 <BLOCKQUOTE>
337 <PRE>#!/usr/local/bin/fontforge
338 i=1
339 format=".ttf"
340 while ( i&lt;$argc )
341 if ( $argv[i]=="-format" || $argv[i]=="--format" )
342 i=i+1
343 if ( i&lt;$argc )
344 format = $argv[i]
345 if ( format!=".ttf" &amp;&amp; format!=".otf" &amp;&amp; \
346 format!=".pfb" &amp;&amp; format!=".svg" )
347 Error( "Expected one of '.ttf', '.otf', '.pfb' or '.svg' for format" )
348 endif
349 endif
350 else
351 Open($argv[i])
352 if ( <A HREF="scripting.html#variables">$order</A>==2 &amp;&amp; (format==".otf" || format==".pfb" ))
353 <A HREF="scripting-alpha.html#SetFontOrder">SetFontOrder</A>(3)
354 <A HREF="scripting-alpha.html#SelectAll">SelectAll</A>()
355 <A HREF="scripting-alpha.html#Simplify">Simplify</A>(128+32+8,1.5)
356 <A HREF="scripting-alpha.html#ScaleToEm">ScaleToEm</A>(1000)
357 elseif ( $order==3 &amp;&amp; format==".ttf" )
358 ScaleToEm(2048)
359 <A HREF="scripting-alpha.html#RoundToInt">RoundToInt</A>()
360 endif
361 Generate($argv[i]:r + format)
362 endif
363 i = i+1
364 endloop
365 </PRE>
366 </BLOCKQUOTE>
367 <P>
368 <!--
369 By its nature a truetype font will contain more points than will a postscript
370 font, but we can use the Simplify command to reduce that number when we convert
371 from one format to another. Also PostScript fonts should have 1000 units
372 to the em while TrueType fonts should have a power of 2 units/em (generally
373 2048 or 1024), so enforce these conventions. Finally TrueType fonts only
374 support integral (or in some cases half-integral) coordinates for points. -->
375 TrueType はその性質上、PostScript よりも多くの点を含む傾向があります。しかし、Simplify コマンドを用いれば、あるフォーマットから別のフォーマットへの変換を行う際に、点の個数を減らすことができます。また、TrueType フォントが em あたりユニット数が 2 の冪であるべきなのに対し、PostScript フォントは em あたり 1000 ユニットであるべきです。最後に、TrueType フォントは点の座標に整数値 (また、ある場合には半整数値) しか使用することができません。
376 <H2>
377 <!--<A NAME="OtherExamples">Other Examples</A> -->
378 <A NAME="OtherExamples">その他の実例</A>
379 </H2>
380 <H3>
381 <!--Adding <A NAME="Accented">Accented</A> Characters -->
382 Type1 フォントに<A NAME="Accented">アクセントつき</A>文字を追加する
383 </H3>
384 <P>
385 <!--
386 Very few Type1 fonts have the full unicode range of accented characters.
387 With FontForge it is fairly easy to load a Type1 font, add as many possible
388 accented characters as the font permits (If the font does not contain ogonek,
389 then FF won't be able to create Aogonek). -->
390 Unicode で定義されたアクセントつき文字の全範囲を含んだ Type1 フォントはほとんどありません。FontForge を使えば、Type1 フォントを読み込み、フォントが許す限り (フォントにオゴネクが含まれていなければ、FF はオゴネクつき A を作成することはできません) できるだけ多くのアクセントつき文字を追加することは簡単です。
391 <BLOCKQUOTE>
392 <PRE>#!/usr/local/bin/fontforge
393 Open($1)
394 <A HREF="scripting-alpha.html#Reencode">Reencode</A>("unicode")
395 <A HREF="scripting-alpha.html#SelectWorthOutputting">SelectWorthOutputting</A>()
396 <A HREF="scripting-alpha.html#SelectInvert">SelectInvert</A>()
397 <A HREF="scripting-alpha.html#BuildAccented">BuildAccented</A>()
398 Generate($1:r + ".otf")
399 </PRE>
400 </BLOCKQUOTE>
401 <H3>
402 <!--<A NAME="GSUB">Merging a type1 and type1 expert font and creating appropriate
403 GSUB tables.</A> -->
404 <A NAME="GSUB">Type1 フォントと Type1 エキスパートフォントを併合し、適切な GSUB テーブルを作成する</A>
405 </H3>
406 <P>
407 <!--
408 Adobe used to ship font packs containing a normal font and an "expert" font
409 which included small caps, lower case numbers, etc. Now-a-days that should
410 all be stuffed into one otf font with appropriate GSUB entries linking the
411 glyphs. -->
412 かつて Adobe は通常フォントを含むフォントパックと、小型大文字・ノンライニング数字などを含む“エキスパート”フォントを含むフォントパックを出荷していました。現在では、それらはすべて、適切な GSUB 項目によってグリフ同士にリンクが張られた 1 個の OTF フォントに詰め込まれています。
413 <BLOCKQUOTE>
414 <PRE>#!/usr/local/bin/fontforge
415 Open($1)
416 <A HREF="scripting-alpha.html#MergeFonts">MergeFonts</A>($2)
417 <A HREF="scripting-alpha.html#RenameGlyphs">RenameGlyphs</A>("AGL with PUA")
418 SelectAll()
419 <A HREF="scripting-alpha.html#DefaultATT">DefaultATT</A>("*")
420 </PRE>
421 </BLOCKQUOTE>
422 <H3>
423 <!--More examples-->
424 より多くの例
425 </H3>
426 <P>
427 <!--
428 See the <A HREF="scripting.html#Example">page on scripting</A>. -->
429 <A HREF="scripting.html#Example">スクリプト処理に関するページ</A>を参照してください。
430 </DIV>
431 </BODY></HTML>