diff options
author | Karl Berry <karl@freefriends.org> | 2021-03-06 21:58:15 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2021-03-06 21:58:15 +0000 |
commit | 974640d66e61e81cb197ad96fdff7b08343e4c5a (patch) | |
tree | 2e1f75f32f312b7f24ba82b4590ae230bcd6f399 /Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex | |
parent | b4fa72e61230aca75f7f6fbf988821f71edfb6b2 (diff) |
context
git-svn-id: svn://tug.org/texlive/trunk@58167 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex')
-rw-r--r-- | Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex | 309 |
1 files changed, 300 insertions, 9 deletions
diff --git a/Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex b/Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex index a52c7a35a63..479f396022b 100644 --- a/Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex +++ b/Master/texmf-dist/doc/context/sources/general/manuals/metafun/metafun-basics.tex @@ -51,8 +51,8 @@ wish, your comments, your grocery list, whatever. Comments in \METAPOST, prefixe by a percent sign, as in \typ {% Let's draw a circle}, are ignored by the interpreter, but useful reminders for the programmer. -If the file is saved as \type {yourfile.mp}, then the file is processed by -\METAPOST\ by issuing the following command: +Traditionally, if the file is saved as \type {yourfile.mp}, then the file is +processed by \METAPOST\ by issuing the following command: \starttyping mpost yourfile @@ -88,7 +88,7 @@ If you have installed \CONTEXT, somewhere on your system there resides a file following line at the top of your file: \starttyping -input mp-tool ; % or input metafun ; +input metafun ; \stoptyping By loading this file, the resulting graphic will provide a high resolution @@ -96,10 +96,44 @@ bounding box, which enables more accurate placement. The file also sets the \typ {prologues := 1} so that viewers like \GHOSTVIEW\ can refresh the file when it is changed. +{\bf However!} When you use \METAPOST\ in \CONTEXT, you will not run the +mentioned program at all: you embed your graphic in the document and we will +discuss this later. You can still use separate files but then you process them as +follows: + +\starttyping +mtxrun --script metapost somefile.mp +\stoptyping + +Alternatively you can wrap the individual pictures in a \TEX\ file, say \type +{yourfile.tex}: + +\starttyping +\starttext + \startMPpage + % code + \stopMPpage + \startMPpage + % code + \stopMPpage +\stoptext +\stoptyping + +and then run: + +\starttyping +context somefile.tex +\stoptyping + +after which you can use the resulting \PDF\ file (if it has more pages you just +filter the page from that file). + Next we will introduce some more \METAPOST\ commands. From now on, we will omit the encapsulating \type {beginfig} and \type {endfig} macros. If you want to process these examples yourself, you should add those commands yourself, or if -you use \CONTEXT\ you don't need them at all. +you use \CONTEXT\ you don't need them at all. You can for instance wrap them in a +\type {\startMPpage} \unknown\ \type {\stopMPpage} environment if you want to +play around. \startbuffer pickup pencircle scaled .5cm ; @@ -463,7 +497,7 @@ draw boundingcircle p withpen pencircle scaled 1mm withcolor .625yellow ; You can consider the \type {boundingcircle} to be a round boundingbox. -\startlinecorrection +\startlinecorrection[blank] \startcombination[nx=3,ny=1,location=middle] {\processMPbuffer[a]} {square} {\processMPbuffer[b]} {circle} @@ -776,9 +810,9 @@ draw {\def\Xcom{2}\def\Ycom{3}\def\Zcom{\the\textwidth }} Chapter 3 of the \METAFONT\ book explains the mathematics behind the construction -of curves. Both \METAFONT\ and \METAPOST\ implement B\'ezier curves. The fact -that these curves are named after Pierre B\'ezier obscures the fact that the math -behind them originates with Serge\u{\i} Bernshte\u{\i}n. +of curves. Both \METAFONT\ and \METAPOST\ implement Bézier curves. The fact that +these curves are named after Pierre Bézier obscures the fact that the math behind +them originates with Sergeĭ Bernshteĭn. The points on the curve are determined by the following formula: @@ -2875,6 +2909,20 @@ The leftmost shape uses \type {eofill} and therefore the odd||even rule gets applied. This time we follow the line and when it gets crossed en even number of times the area will not be filled. +Here is another example: + +\startbuffer +eofill for i=1 upto 12: fullcircle scaled ((i/12)*cm) -- endfor cycle ; +\stopbuffer + +\typebuffer + +The successive larger circle are unfilled and basically form one path: + +\startlinecorrection[blank] +\processMPbuffer +\stoplinecorrection + A glyph is often constructed from more than one path and eventually the shape is filled with an odd||even fill (\type {eofill}) operation. Take the following sequence: @@ -3363,7 +3411,43 @@ context("%3!texexp!", 10.4698E30,"@2.3f","@2i") \typebuffer -This gives: \inlinebuffer . +This gives: \inlinebuffer . In \in {figure} [fig:formatters] we see some more +formatters. + +\startbuffer +for i=1 upto 12 : + draw + thefmttext("\letterpercent 3!date!","month,space,year",2019,i) + shifted (0,i *cm / 2); + draw + thefmttext("@3!date!","month,space,year",2019,i) + shifted (4cm,i *cm / 2); +endfor ; + +for i=1 upto 20 : + draw + thefmttext(decimal i) + shifted (1cm,-i/2*cm); + draw + thefmttext("@!month!",i) + shifted (4cm,-i/2*cm); + draw + thefmttext("@!weekday!",i) + shifted (7cm,-i/2*cm); + draw + thefmttext("@!monthshort!",i) + shifted (10cm,-i/2*cm); + draw + thefmttext("@3!dayoftheweek!",i,1,2018) + shifted (13cm,-i/2*cm); +endfor ; +\stopbuffer + +\typebuffer + +\startplacefigure[title=formatters,reference=fig:formatters] + \framed{\scale[width=.8\textwidth]{\processMPbuffer}} +\stopplacefigure \stopsection @@ -3582,6 +3666,213 @@ a default value of 20. \stopsection +\startsection[title=Grouping] + +The grouping model in \METAPOST\ is kind of special. Basically anything that +happens in a group is processed in some nested action and doesn't interfere with +the outside. However, the last value put (back) into the input is picked up after +the group. A \type {vardef} acts that way: + +\startbuffer +vardef foo (expr i, j) = + save ii, jj ; ii := 2*i ; jj :=j/2 ; + (i, j) -- (ii,jj) +enddef ; + +draw (foo(1,2) .. foo(5,1) .. foo(12,3)) + ysized 1cm + withpen pencircle scaled 2mm + withcolor darkred ; + +draw boundingbox currentpicture withcolor darkyellow ; +\stopbuffer + +\typebuffer + +This weird shape comes out: + +\startlinecorrection[blank] + \processMPbuffer +\stoplinecorrection +We save two variables, and then give new local numerics with their names some +values. Then we \quote {return} a path. A \type {vardef} automatically starts +with a \type {begingroup} and ends with an \type {endgroup}. The next example +shows how we can use that grouping trick directly. The \type {--} has to come +before the \type {begingroup}. + +\startbuffer +vardef turtle expr p = + save a ; pair a ; a := point 0 of p ; a + for i = 1 upto length(p) if cycle p : - 1 fi : + -- begingroup a := a shifted point i of p ; a endgroup + endfor + if cycle p : -- cycle fi +enddef ; + +draw ((0, 0) -- (10, 0) -- (100, 10) -- (-5,20) -- cycle) + withpen pencircle scaled 2 withcolor darkred ; +draw turtle ((0, 0) -- (10, 0) -- (100, 10) -- (-5,20) -- cycle) + withpen pencircle scaled 2 withcolor darkyellow ; +\stopbuffer + +\typebuffer + +Turtle graphics use relative positions. Actually they use one number and switch +direction but the above is okay too. Effectively we loop over the path and use +each point as a relative move, so we recalculate it. + +\startlinecorrection[blank] + \processMPbuffer +\stoplinecorrection + +We could have said \type {-- hide(a := a shifted point i of p) a} because the +\type {hide} macro does that the grouping trick. + +\stopsection + +\startsection[title=External files] + +You can put code in an external file and load that one when needed. The low level +command is \type {input}: + +\starttyping +input myfile ; +input myfile.mp ; +input myfile.mpiv ; +input myfile.mpxl ; +input "myfile.mpxl" ; +\stoptyping + +In \CONTEXT\ we have modules and their name starts with \type {mp-} and the +suffix can depend on what version you use (\type {mpii}, \type {mpiv}, \type +{mpxl}) but that will be dealt with automatically. + +\starttyping +loadmodule("mine") ; +\stoptyping + +This will locate \type {mp-mine} with the proper suffix and often a module will +be set up to be loaded only once. For loading files \LUAMETAFUN\ provides: + +\starttyping +loadfile("somefile.mp") ; +\stoptyping + +Which is a bit more robust than the \type {input} commands that is kind of fuzzy +because it takes a sequence of tokens or a string. When you embed code in a +document source there are some environments that permit loading of definitions +and extensions. + +\starttyping +\startMPdefinitions + % code passed to the library directly +\stopMPdefinitions + +\startMPextensions + % code passed to the library but via tex (expansion step) +\stopMPextensions +\stoptyping + +\stopsection + +\startsection[title=Instances] + +The library supports several numbering models. The default is \type {scaled} that +uses integers that represent a (relatively small) float. Although you can go up +to 32K it's wise to stay below 4096. The \type {double} number model supports 64 +bit floating point and the \type {decimal} model arbitrary precision. In \LUATEX\ +there is also \type {binary} for arbitrary precision but in \LUAMETATEX\ that +model is not provided. + +You can use different models and instances alongside. In \MKIV\ and \LMTX\ we have +a few predefined. The \type {metafun} instance is the default. Here we show the +ones that use \METAFUN: + +\starttyping[style=\ttx] +\defineMPinstance[metafun] [format=metafun,extensions=yes,initializations=yes] +\defineMPinstance[minifun] [format=minifun,extensions=yes,initializations=yes] +\defineMPinstance[extrafun] [format=metafun,extensions=yes,initializations=yes] +\defineMPinstance[lessfun] [format=metafun] +\defineMPinstance[doublefun] [format=metafun,extensions=yes,initializations=yes,method=double] +\defineMPinstance[decimalfun][format=metafun,extensions=yes,initializations=yes,method=decimal] +\defineMPinstance[mprun] [format=metafun,extensions=yes,initializations=yes] +\defineMPinstance[simplefun] [format=metafun,method=double] % for internal usage +\stoptyping + +All instances will load additional definitions btu extensions (expanded +definitions) and initializations (before each graphic) are optional. + +\starttyping +\startMPdefinitions +\stopMPdefinitions + +\startMPextensions +\stopMPextensions + +\startMPinitializations +\stopMPinitializations +\stoptyping + +You can search the sources of manuals and test suite for examples of usage, but here are +some: + +\startbuffer +\defineMPinstance[foo][definitions=yes] +\defineMPinstance[bar][definitions=yes] + +\startMPdefinitions{foo} + vardef p = image ( + draw fullsquare scaled 1cm withcolor "darkgreen" ; + draw textext("1") ; + ) + enddef ; +\stopMPdefinitions + +\startMPdefinitions{bar} + vardef p = image ( + draw (fullsquare rotated 45) scaled 1cm withcolor "darkyellow" ; + draw textext("2") ; + ) + enddef ; +\stopMPdefinitions +\stopbuffer + +\typebuffer \getbuffer + +We use both as: + +\startbuffer[demo-1] +\startMPcode{foo} + draw p ; + draw image ( + draw fullcircle scaled 1cm withcolor "darkblue" ; + draw textext("3") withcolor "darkred" ; + ) ; + draw p ; +\stopMPcode +\stopbuffer + +\startbuffer[demo-2] +\startMPcode{bar} + draw p ; + draw image ( + draw fullcircle scaled 1cm withcolor "darkblue" ; + draw textext("4") withcolor "darkred" ; + ) ; +\stopMPcode +\stopbuffer + +\typebuffer[demo-1,demo-2] + +\startlinecorrection +\startcombination[nx=2,ny=1] + {\getbuffer[demo-1]} {foo} + {\getbuffer[demo-2]} {bar} +\stopcombination +\stoplinecorrection + +\stopsection + \stopchapter \stopcomponent |