metafun学习笔记
主要是学习官方文档METAPOST manual、metafun(metafun-p.pdf)和metafun xl(luametafun.pdf),以及官网。
惯例#
在ConTeXt中使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
欢迎来到MetaPost#
坐标点,数值对(pair),矢量,路径,控制点。
1 2 |
|
路径#
1 2 3 4 5 |
|
变形#
变量和赋值:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
METAPOST | code | mathematical equivalent |
---|---|---|
(x,y) | shifted (a,b) | (𝑥 + 𝑎, 𝑦 + 𝑏) |
(x,y) | scaled s | (𝑠𝑥, 𝑠𝑦) |
(x,y) | xscaled s | (𝑠𝑥, 𝑦) |
(x,y) | yscaled s | (𝑥, 𝑠𝑦) |
(x,y) | zscaled (u,v) | (𝑥𝑢 − 𝑦𝑣, 𝑥𝑣 + 𝑦𝑢) |
(x,y) | slanted s | (𝑥 + 𝑠𝑦, 𝑦) |
(x,y) | rotated r | (𝑥 cos(𝑟) − 𝑦 sin(𝑟), 𝑥 sin(𝑟) + 𝑦 cos(𝑟)) |
构造路径#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
预定义的directionpoint
- right ( 1, 0)
- up ( 0, 1)
- left (-1, 0)
- down ( 0,-1)
1 2 |
|
角度#
方向函数dir
1 2 3 4 5 6 7 8 9 10 11 12 |
|
画图#
根据路径画线和填充
1 2 3 |
|
颜色,笔型,线型
1 2 3 4 |
|
图形变量,赋值,清空,操纵
1 2 3 4 |
|
unfill其实是使用背景色填充
添加
1 2 3 |
|
变量#
METAPOST是一种简单的宏语言,用PASCAL编写而成。
数据类型:
numeric | real number in the range −4096… + 4096 |
boolean | a variable that takes one of two states: true or false |
string | sequence of characters, like "metapost" |
pair | point or vector in 2--dimensional space |
path | a piecewise collection of curves and line segments |
picture | collection of stroked or filled paths |
color | vector of three (rgb) or four (cmyk) numbers |
pen | pen specification |
transform | transformation vector with six elements |
十种基础数据类型:
- numeric, 1/65536(epsilon)的整数倍
- pair, 数对,可进行四则运算,可用于区间定位表达式(mediation expressions)
- path,路径
- transform,变换组合,包括旋转、缩放、倾斜、位移的组合,可用于数对、路径、图像、笔和变换自身。
- color/rgbcolor,三元数组,运算同数对(结果会修剪到0与1之间)。也指预定义的常量:
- black,(0,0,0)
- white,(1,1,1)
- red
- green
- blue
- cmykcolor,四元数组,运算同color,小心纯黑色(0,0,0,1)与混色黑(1,1,1,𝑘)(𝑐,𝑚,𝑦,1)的在印刷实际中的区别
- string,"...",不能包括双引号和换行,但可以通过
n := scantokens(str);
输入 - boolean
- true, false
- and, or, not
- =, <>, <, >, <=, >=
- picture, 任何可绘制对象,draw把绘制结果存在currentpicture,图像可叠加和变换
- pen, 主要确定笔画粗细,表达式如pencircle scaled ⟨numeric primary⟩,非圆形笔可以取得书法效果,pickup ⟨pen expression⟩ 影响会面的draw和drawdot
未定义的变量默认为数字。以下行相同:
1 2 |
|
在宏集中最好先定义,以免冲突。
数组是同类变量的集合:
1 |
|
超界索引不会出错,而返回unknown
实际上并没有数组,而是生成了一些哈希条目。以下是定价的:
1 2 3 |
|
变量后缀#
如lable后缀:<空> | lft | rt | top | bot | ulft | urt | llft | lrt
z后缀,如:
dotlabels.rt(0, 1, a); % 等价于dotlabel.rt("0",z0); dotlabel.rt("1",z1); dotlabel.rt("a",z.a);
token#
运算符#
条件表达式#
1 2 3 |
|
一行式:
1 2 |
|
变量的布尔检测:
1 2 3 4 |
|
支持用and, or, not, 和 ( )构造表达式。
循环#
1 2 3 4 5 |
|
迭代一列对象:
1 2 3 4 |
|
改变循环变量自身:
1 2 3 |
|
查找特定值后跳出/终止循环:
1 2 3 4 5 6 7 8 |
|
宏#
定义宏:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
可接受的参数:
- expr: something that can be assigned to a variable
- text: arbitrary METAPOST code ending with a ;
- suffix: a variable bound to another variable
- primary
expr是传值的,suffix则是传引用。
默认所有变量都是全局的,局部变量需要通过分组来管理。
1 2 3 4 5 6 7 |
|
其他定义:
- primarydef x mod y = ... enddef ;
- secondarydef p intersectionpoint q = ... enddef ;
- tertiarydef p softjoin q = ... enddef ;
参数#
馈送参数相当灵活:
1 2 3 4 5 6 7 8 |
|
text参数,如果没有括号限制,会一直匹配到分号。
限定参数,用于定义z1或z234:
vardef z@# = (x@#,y@#) enddef ;
笔型#
绘图时可以使用三种属性:a dashpattern, a pen and/or a color
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
连接线段#
1 2 3 4 5 6 |
|
参见:Figure 1.3 The nine ways to end and join lines.
颜色#
1 2 3 4 5 6 7 8 |
|
虚线#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
文本#
- label⟨标签后缀⟩(⟨字符串或图片表达式⟩, ⟨数对表达式⟩);
- thelabel⟨标签后缀⟩(⟨字符串或图片表达式⟩, ⟨数对表达式⟩); % 返回图像原语
- dotlabel⟨标签后缀⟩(⟨字符串或图片表达式⟩, ⟨数对表达式⟩);
- dotlabels.rt(0, 1, a); % z后缀,等价于dotlabel.rt("0",z0); dotlabel.rt("1",z1); dotlabel.rt("a",z.a);
- ⟨标签后缀⟩ -> <空> | lft | rt | top | bot | ulft | urt | llft | lrt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
改变默认字体、字号:
- defaultfont:="ptmr8r";
- defaultscale:= 1.2; % 1是默认值
- defaultscale:= 12pt/fontsize defaultfont;
绑定盒bbox与文本测量
线性方程#
解析线性方程(符号计算)
1 2 3 4 |
|
剪裁#
取得外框数据:
- llcorner p: lower left corner
- lrcorner p: lower right corner
- urcorner p: upper right corner
- ulcorner p: upper left corner
- center p: the center point
1 2 3 4 5 6 |
|
METAFUN扩展#
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 |
|
剪切和粘贴#
1 2 3 4 5 6 7 8 9 10 11 |
|
当前图像(currentpicture)#
我们绘画时是在当前图像上操作。还可以把当前图像当做一个整体来处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
更多细节#
生成图形(graphic)#
独立文件,比如yourfile.mp:
1 2 3 4 5 6 7 8 9 |
|
在CONTEXT文档中不需要beginfig (7) ; endfig ;和end .
生成图像文件:
1 |
|
使用图像:
\externalfigure[yourfile.7]
单位:
- cm
- pt, point
- bp, big point, METAPOST的缺省单位,对应pt的POSTSCRIPT的近似值
围盒#
bbox宏生成的围盒是有偏置量的。偏置量控制:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
解散字模#
调试#
读写文件#
导出通用文件#
prologues := 2 ;
#
嵌入图形#
开始#
外部图形#
- \externalfigure[graphic.123][width=4cm]
- useexternalfigure
- \useexternalfigure[pentastar][star.803][height=4cm]
- \useexternalfigure[octostar] [star.804][pentastar]
- \placefigure{A five||point star drawn by \METAPOST.}{\externalfigure[pentastar]}
整合图形#
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 |
|
不用CONTEXT,只用METAFUN#
1 2 3 4 |
|
Lua#
导语#
基础#
metafun向tex输入流馈入字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
辅助函数#
CONTEXT的方式:
1 2 |
|
字符串#
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
数字#
runscript 10000;
CONTEXT/METAFUN辅助函数#
1 2 |
|
在lua段,被映射到相应的函数:
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|
从lua打印到mp(传递数据)#
所打印的字符串即为合法的mp代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Direct值#
与打印机近似的注入器:
1 2 3 |
|
包括: boolean, cmykcolor, color, integer, number, numeric, pair, path, quadruplet, string, transform, triplet and whatever (kind of automatic)
寄存器#
特殊辅助函数#
哈希
模式
1 2 3 4 5 6 |
|
定位
TeX数量
UTF8
1 2 3 4 5 6 |
|
使用变量#
库#
CONTEXT辅助函数#
- mp.string string passed as it is but with percent, double quote and newline escaped
- mp.boolean boolean the true or false primitives
- mp.integer number an integer
- mp.number number a float
- mp.numeric number a float (same as previous)
- mp.points number a scaled numeric with pt unit
- mp.pair numbers or table a pair (x,y) or (x,x)
- mp.pairpoints numbers or table idem but with scaled numbers and a pt unit
- mp.triplet numbers or table a rgb triplet (r,g,b)
- mp.tripletpoints numbers or table idem but with scaled numbers and a pt unit
- mp.quadruple numbers or table a cmyk quadruple (c,m,y,k)
- mp.quadruplepoints numbers or table idem but with scaled numbers and a pt unit
- mp.color numbers or table a numeric, triplet or quadruple
- mp.transform numbers or table a six element transform
- mp.print whatever the normal semi-intelligent printer
- mp.print.path
- mp.inject
- mp.inject.path
- mp.fprint format, whatever the normal semi-intelligent printer using a format
- mp.vprint variable the normal semi-intelligent printer with escaped percents, quotes and newlines
- mp.quoted string a valid string surrounded by quotes with an optional first format specifier
- mp.print(...) returns one or more values
- mp.pair(x,y) pair(t) returns a proper pair
- mp.triplet(x,y,z) triplet(t) returns an RGB color
- mp.quadruple(w,x,y,z) quadruple(t) returns an CMYK color
- mp.format(fmt,...) returns a formatted string
- mp.quoted(fmt,...) quoted(s) returns a (formatted) quoted string
- mp.path(t[,connect][,close]) returns a connected (closed) path
- mp.get.numeric(name) gets a numeric from METAPOST
- mp.get.boolean(name) gets a boolean from METAPOST
- mp.get.string(name) gets a string from METAPOST
METAPOST向CONTEXT/lua传递参数#
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 |
|
lua端通过表metapost.variables使用
1 2 3 |
|
tex用法:
\MPruntab{quad}{3}
跟踪参数:
\enabletrackers[metapost.variables]