From 7b271baae19db1528fbe6621bdf50af89a5a336b Mon Sep 17 00:00:00 2001 From: Hans Hagen Date: Fri, 22 Feb 2019 20:29:46 +0100 Subject: 2019-02-22 19:43:00 --- metapost/context/base/mpii/mp-text.mpii | 3 +- metapost/context/base/mpiv/metafun.mpiv | 1 + metapost/context/base/mpiv/minifun.mpiv | 45 +++ metapost/context/base/mpiv/mp-asnc.mpiv | 216 +++++--------- metapost/context/base/mpiv/mp-bare.mpiv | 92 +++--- metapost/context/base/mpiv/mp-base.mpiv | 105 ++++--- metapost/context/base/mpiv/mp-blob.mpiv | 112 ++++++++ metapost/context/base/mpiv/mp-grap.mpiv | 28 +- metapost/context/base/mpiv/mp-grph.mpiv | 155 +--------- metapost/context/base/mpiv/mp-luas.mpiv | 146 ++++++++-- metapost/context/base/mpiv/mp-mlib.mpiv | 485 +++++++++++++++++++------------- metapost/context/base/mpiv/mp-node.mpiv | 8 +- metapost/context/base/mpiv/mp-page.mpiv | 5 +- metapost/context/base/mpiv/mp-tool.mpiv | 68 ++++- metapost/context/base/mpiv/mp-tres.mpiv | 63 ++++- 15 files changed, 898 insertions(+), 634 deletions(-) create mode 100644 metapost/context/base/mpiv/minifun.mpiv create mode 100644 metapost/context/base/mpiv/mp-blob.mpiv (limited to 'metapost/context') diff --git a/metapost/context/base/mpii/mp-text.mpii b/metapost/context/base/mpii/mp-text.mpii index 5f96f6788..b7787a42d 100644 --- a/metapost/context/base/mpii/mp-text.mpii +++ b/metapost/context/base/mpii/mp-text.mpii @@ -56,12 +56,11 @@ boolean hobbiestextext ; hobbiestextext := false ; % enddef ; vardef textext@#(expr txt) = - save _s_ ; string _s_ ; interim labeloffset := textextoffset ; noftexpictures := noftexpictures + 1 ; if string txt : if hobbiestextext : % the tex.mp method as fallback (see tex.mp) - write _s_ & "btex " & txt & " etex" to "mptextmp.mp" ; + write "btex " & txt & " etex" to "mptextmp.mp" ; write EOF to "mptextmp.mp" ; scantokens "input mptextmp" else : diff --git a/metapost/context/base/mpiv/metafun.mpiv b/metapost/context/base/mpiv/metafun.mpiv index 818fd7c56..fa43d3460 100644 --- a/metapost/context/base/mpiv/metafun.mpiv +++ b/metapost/context/base/mpiv/metafun.mpiv @@ -38,6 +38,7 @@ input "mp-func.mpiv" ; % under construction input "mp-node.mpiv" ; % relatively small so preloaded input "mp-apos.mpiv" ; input "mp-abck.mpiv" ; +input "mp-blob.mpiv" ; string metafunversion ; metafunversion = "metafun iv" & " " & diff --git a/metapost/context/base/mpiv/minifun.mpiv b/metapost/context/base/mpiv/minifun.mpiv new file mode 100644 index 000000000..6d877fddb --- /dev/null +++ b/metapost/context/base/mpiv/minifun.mpiv @@ -0,0 +1,45 @@ +%D \module +%D [ file=minifun.mp, +%D version=2018.06.02, +%D title=\CONTEXT\ \METAPOST\ graphics, +%D subtitle=format generation file, +%D author=Hans Hagen, +%D date=\currentdate, +%D copyright={PRAGMA / Hans Hagen \& Ton Otten}] +%C +%C This module is part of the \CONTEXT\ macro||package and is +%C therefore copyrighted by \PRAGMA. See mreadme.pdf for +%C details. + +%D This is a minimal \METAFUN\ instance which can be handy for isolated +%D subruns. + +prologues := 0 ; +mpprocset := 1 ; + +input "mp-base.mpiv" ; +input "mp-tool.mpiv" ; +input "mp-mlib.mpiv" ; +input "mp-luas.mpiv" ; +input "mp-page.mpiv" ; + +string minifunversion ; minifunversion = + "minifun iv" & " " & + decimal year & "-" & + decimal month & "-" & + decimal day & " " & + if ((time div 60) < 10) : "0" & fi + decimal (time div 60) & ":" & + if ((time-(time div 60)*60) < 10) : "0" & fi + decimal (time-(time div 60)*60) ; + +let normalend = end ; + +if known mplib : + def end = ; message "" ; message minifunversion ; message "" ; endinput ; enddef ; + def bye = ; message "" ; message minifunversion ; message "" ; endinput ; enddef ; +else : + def end = ; message "" ; message minifunversion ; message "" ; normalend ; enddef ; +fi ; + +% dump ; % obsolete in mplib diff --git a/metapost/context/base/mpiv/mp-asnc.mpiv b/metapost/context/base/mpiv/mp-asnc.mpiv index fba182a64..2eb409124 100644 --- a/metapost/context/base/mpiv/mp-asnc.mpiv +++ b/metapost/context/base/mpiv/mp-asnc.mpiv @@ -15,163 +15,103 @@ if known context_asnc : endinput ; fi ; boolean context_asnc ; context_asnc := true ; -% will be replaced - -numeric sync_n[], sync_p[][], sync_w[][], sync_h[][], sync_d[][], sync_t[][] ; -pair sync_xy[][] ; color sync_c[][] ; - -def ResetSyncTasks = - path SyncPaths[] ; numeric SyncTasks[], NOfSyncPaths, CurrentSyncClass ; - NOfSyncPaths := CurrentSyncClass := 0 ; - if unknown SyncLeftOffset : numeric SyncLeftOffset ; SyncLeftOffset := 0 ; fi ; - if unknown SyncWidth : numeric SyncWidth ; SyncWidth := 0 ; fi ; - if unknown SyncThreshold : numeric SyncThreshold ; SyncThreshold := LineHeight ; fi ; - if unknown SyncColor : color SyncColor ; SyncColor := .5white ; fi ; - if (SyncLeftOffset = 0) and (SyncWidth = 0) : - SyncWidth := if known TextWidth : TextWidth else : -1cm fi ; +%D This is a rather old mechanism that we once needed in an actually nice +%D design. Those were the times that processing a rather complex xml file +%D with pdftex into a 400 page document took 45 minutes (a few runs) while +%D nowadays with luatex and mkiv we express runtime in seconds. +%D +%D The logic is mostly the same but some work is delegated to Lua so that +%D we save memory and also run faster. As this was not really a used module +%D the interface also was upgraded. As we know the bottlenecks in mp we also +%D work around it a bit. + +numeric mfun_sync_count ; +numeric mfun_sync_page ; + +vardef StartSync(expr n) = + numeric CurrentSyncClass ; CurrentSyncClass := n ; + numeric SyncHOffset ; SyncHOffset := 0 ; + numeric SyncVOffset ; SyncVOffset := 0 ; + numeric SyncWidth ; SyncWidth := 0 ; + path SyncPaths[] ; + numeric SyncTasks[] ; + numeric SyncKinds[] ; + % + mfun_sync_page := RealPageNumber ; + mfun_sync_count := 0 ; +enddef ; + +def StopSync = + % maybe some cleanup +enddef ; + +vardef CollectSyncDataPage = + mfun_sync_count := lua.mp.sync_collect(CurrentSyncClass,mfun_sync_page) ; +enddef ; + +vardef CollectSyncDataRegion(expr region) = + mfun_sync_count := lua.mp.sync_collect(CurrentSyncClass,mfun_sync_page,region) ; +enddef ; + +vardef MakeSyncPaths = + if mfun_sync_count > 0 : + save k, t, b ; + save l ; l := SyncHOffset ; + save r ; r := SyncHOffset + SyncWidth ; + save y ; y := lua.mp.sync_get_y() + SyncVOffset ; + for i=1 upto mfun_sync_count : + k := lua.mp.sync_get_kind(i) ; + t := lua.mp.sync_get_top(i) - y ; + b := lua.mp.sync_get_bottom(i) - y ; + SyncPaths[i] = ((l,t) -- (r,t) -- (r,b) -- (l,b) -- cycle) ; + SyncTasks[i] = lua.mp.sync_get_task(i) ; + SyncKinds[i] = k ; + endfor ; fi ; enddef ; -ResetSyncTasks ; +% Extend to the top of the text area. -vardef SyncBox(expr n, i, leftoffset, width, topoffset, bottomoffset) = - save o ; pair o ; o := (xpart llcorner PlainTextArea,ypart sync_xy[n][i]) ; - o shifted (leftoffset,sync_h[n][i]+topoffset) -- - o shifted (width+leftoffset,sync_h[n][i]+topoffset) -- - o shifted (width+leftoffset,bottomoffset) -- - o shifted (leftoffset,bottomoffset) -- cycle +vardef ExtendSyncPaths = + mfun_sync_count := lua.mp.sync_extend() ; enddef ; -def SetSyncColor(expr n, i, c) = - sync_c[n][i] := c ; +% Clip to the text area. + +vardef PruneSyncPaths = + mfun_sync_count := lua.mp.sync_prune() ; enddef ; -def SetSyncThreshold(expr n, i, th) = - sync_th[n][i] := th ; +% Remove duplicate tasks + +vardef CollapseSyncPaths = + mfun_sync_count := lua.mp.sync_collapse() ; enddef ; -vardef TheSyncColor(expr n, i) = - if known sync_c[n][i] : sync_c[n][i] else : SyncColor fi +def SetSyncColor(expr n, i, c) = + lua.mp.sync_set_color(n,i,c) ; enddef ; -vardef TheSyncThreshold(expr n, i) = - if known sync_th[n][i] : sync_th[n][i] else : SyncThreshold fi +vardef TheSyncColor(expr n, i) = + lua.mp.sync_get_color(n,i) enddef ; -vardef PrepareSyncTasks(expr n, collapse, extendtop, prestartnext) = - ResetSyncTasks ; - if known sync_n[n] : - CurrentSyncClass := n ; - save ok, l, d ; boolean ok ; ok := false ; NOfSyncPaths := l := 0 ; - for i=1 upto sync_n[n] : - if RealPageNumber > sync_p[n][i] : - l := i ; - elseif RealPageNumber = sync_p[n][i] : - NOfSyncPaths := NOfSyncPaths + 1 ; - if not ok : - if i>1 : - if sync_t[n][i-1] = sync_t[n][i] : - SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ; - SyncTasks[NOfSyncPaths] := i ; - else : - SyncPaths[NOfSyncPaths] := SyncBox(n, i-1, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ; - SyncTasks[NOfSyncPaths] := i-1 ; - NOfSyncPaths := NOfSyncPaths + 1 ; - SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ; - SyncTasks[NOfSyncPaths] := i ; - fi ; - else : - SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ; - SyncTasks[NOfSyncPaths] := i ; - fi ; - else : - SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ; - SyncTasks[NOfSyncPaths] := i ; - fi ; - ok := true ; - fi ; - endfor ; - if (NOfSyncPaths = 0) and (l > 0) : - NOfSyncPaths := 1 ; - SyncPaths[NOfSyncPaths] := SyncBox(n, l, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ; - SyncTasks[NOfSyncPaths] := l ; - fi ; - if NOfSyncPaths > 0 : - for i = 1 upto NOfSyncPaths-1 : - SyncPaths[i] := topboundary SyncPaths[i] -- reverse topboundary SyncPaths[i+1] -- cycle ; - endfor ; - if unknown SyncThresholdMethod : - numeric SyncThresholdMethod ; SyncThresholdMethod := 2 ; - fi ; - if extendtop : - if SyncThresholdMethod = 1 : - if NOfSyncPaths>1 : - d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[2]]) ; - if (SyncTasks[2]>1) and (d > 0pt) and (d <= TheSyncThreshold(n,sync_t[n][SyncTasks[2]])) and (sync_p[n][SyncTasks[2]] = RealPageNumber) : - SyncPaths[2] := SyncPaths[2] topenlarged PaperHeight ; - fi ; - fi ; - else : - for i = 1 upto NOfSyncPaths : - d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[i]]) ; - if (d > 0) and (d <= TheSyncThreshold(n,sync_t[n][SyncTasks[i]])) and (sync_p[n][SyncTasks[i]] = RealPageNumber) : - SyncPaths[i] := SyncPaths[i] topenlarged PaperHeight ; - fi ; - endfor ; - fi ; - fi ; - if prestartnext : - if NOfSyncPaths>1 : - if SyncTasks[NOfSyncPaths] < sync_n[n] : % there is a next one - d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[NOfSyncPaths]+1]) ; - if (d > 0) and (d <= TheSyncThreshold(n, sync_t[n][SyncTasks[i]])) and (sync_p[n][SyncTasks[NOfSyncPaths]+1] = RealPageNumber+1) : - SyncPaths[NOfSyncPaths+1] := - (xpart ulcorner SyncPaths[NOfSyncPaths],ypart llcorner PlainTextArea) -- - (xpart urcorner SyncPaths[NOfSyncPaths],ypart llcorner PlainTextArea) -- - lrcorner SyncPaths[NOfSyncPaths] -- - llcorner SyncPaths[NOfSyncPaths] -- cycle ; - SyncTasks[NOfSyncPaths+1] := SyncTasks[NOfSyncPaths]+1 ; - NOfSyncPaths := NOfSyncPaths + 1 ; - fi ; - fi ; - fi ; - else : - if NOfSyncPaths>1 : - d := ypart (sync_xy[n][SyncTasks[NOfSyncPaths]] - llcorner PlainTextArea) ; - if (d < TheSyncThreshold(n, SyncTasks[NOfSyncPaths])) : - NOfSyncPaths := NOfSyncPaths - 1 ; - SyncPaths[NOfSyncPaths] := SyncPaths[NOfSyncPaths] bottomenlarged PaperHeight ; - fi ; - fi ; - fi ; - if (NOfSyncPaths>1) and collapse : - save j ; numeric j ; j := 1 ; - for i = 2 upto NOfSyncPaths : - if sync_t[n][SyncTasks[i]] = sync_t[n][SyncTasks[j]] : - SyncPaths[j] := boundingbox image (draw SyncPaths[i] ; draw SyncPaths[j] ; ) ; - SyncTasks[j] := SyncTasks[i] ; - else : - j := j + 1 ; - SyncPaths[j] := SyncPaths[i] ; - SyncTasks[j] := SyncTasks[i] ; - fi ; - endfor ; - NOfSyncPaths := j ; - fi ; - fi ; - fi ; +vardef SyncPathColor(expr i) = + lua.mp.sync_get_color(CurrentSyncClass,SyncTasks[i]) enddef ; -def SyncTask(expr n) = - if known SyncTasks[n] : SyncTasks[n] else : 0 fi +def DrawSyncPaths = + for i=1 upto NOfSyncPaths : + draw SyncPaths[i] withcolor SyncPathColor(i) ; + endfor ; enddef ; -def FlushSyncTasks = - for i = 1 upto NOfSyncPaths : - ProcessSyncTask(SyncPaths[i], TheSyncColor(CurrentSyncClass,sync_t[CurrentSyncClass][SyncTasks[i]])) ; +def FillSyncPaths = + for i=1 upto NOfSyncPaths : + fill SyncPaths[i] withcolor SyncPathColor(i) ; endfor ; enddef ; -def ProcessSyncTask(expr p, c) = - fill p withcolor c ; +vardef NOfSyncPaths = + mfun_sync_count enddef ; diff --git a/metapost/context/base/mpiv/mp-bare.mpiv b/metapost/context/base/mpiv/mp-bare.mpiv index c6194b1ee..8f517b268 100644 --- a/metapost/context/base/mpiv/mp-bare.mpiv +++ b/metapost/context/base/mpiv/mp-bare.mpiv @@ -14,75 +14,71 @@ if known context_bare : endinput ; fi ; boolean context_bare ; context_bare := true ; -numeric mfun_tt_w[], mfun_tt_h[], mfun_tt_d[] ; +vardef colordecimals primary c = + if cmykcolor c : + decimal cyanpart c & ":" & decimal magentapart c & ":" & decimal yellowpart c & ":" & decimal blackpart c + elseif rgbcolor c : + decimal redpart c & ":" & decimal greenpart c & ":" & decimal bluepart c + else : + decimal c + fi +enddef ; + +rgbcolor mfun_tt_r ; numeric mfun_tt_n ; mfun_tt_n := 0 ; -picture mfun_tt_p ; mfun_tt_p := nullpicture ; picture mfun_tt_o ; mfun_tt_o := nullpicture ; picture mfun_tt_c ; mfun_tt_c := nullpicture ; -if unknown mfun_trial_run : - boolean mfun_trial_run ; - mfun_trial_run := false ; -fi ; - -if unknown mfun_first_run : - boolean mfun_first_run ; - mfun_first_run := true ; -fi ; - def mfun_reset_tex_texts = mfun_tt_n := 0 ; - mfun_tt_p := nullpicture ; mfun_tt_o := nullpicture ; % redundant mfun_tt_c := nullpicture ; % redundant enddef ; def mfun_flush_tex_texts = - addto currentpicture also mfun_tt_p -enddef ; - -extra_beginfig := extra_beginfig & "mfun_reset_tex_texts ;" ; -extra_endfig := "mfun_flush_tex_texts ; mfun_reset_tex_texts ; " & extra_endfig ; - -vardef colordecimals primary c = - if cmykcolor c : - decimal cyanpart c & ":" & decimal magentapart c & ":" & decimal yellowpart c & ":" & decimal blackpart c - elseif rgbcolor c : - decimal redpart c & ":" & decimal greenpart c & ":" & decimal bluepart c - else : - decimal c - fi enddef ; -vardef rawtextext(expr str) = % todo: avoid currentpicture - if str = "" : +vardef rawtextext(expr s) = + if s = "" : nullpicture else : mfun_tt_n := mfun_tt_n + 1 ; mfun_tt_c := nullpicture ; - if mfun_trial_run : - mfun_tt_o := nullpicture ; - addto mfun_tt_o doublepath origin _op_ ; % save drawoptions - addto mfun_tt_c doublepath unitsquare - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=trial" - withprescript "tx_color=" & colordecimals colorpart mfun_tt_o - withpostscript str ; - addto mfun_tt_p also mfun_tt_c ; - elseif known mfun_tt_d[mfun_tt_n] : - addto mfun_tt_c doublepath unitsquare - xscaled mfun_tt_w[mfun_tt_n] - yscaled (mfun_tt_h[mfun_tt_n] + mfun_tt_d[mfun_tt_n]) - shifted (0,-mfun_tt_d[mfun_tt_n]) - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=final" ; - else : - addto mfun_tt_c doublepath unitsquare ; % unitpicture - fi ; + mfun_tt_o := nullpicture ; + addto mfun_tt_o doublepath origin _op_ ; % save drawoptions + mfun_tt_r := runscript("mp.SomeText(" & decimal mfun_tt_n & "," & ditto & s & ditto & ")") ; + addto mfun_tt_c doublepath unitsquare + xscaled redpart mfun_tt_r + yscaled (greenpart mfun_tt_r + bluepart mfun_tt_r) + shifted (0,-bluepart mfun_tt_r) + withprescript "mf_object=text" + withprescript "tx_index=" & decimal mfun_tt_n + withprescript "tx_color=" & colordecimals colorpart mfun_tt_o + ; mfun_tt_c fi enddef ; +vardef rawmadetext = + mfun_tt_n := mfun_tt_n + 1 ; + mfun_tt_c := nullpicture ; + mfun_tt_o := nullpicture ; + addto mfun_tt_o doublepath origin _op_ ; % save drawoptions + mfun_tt_r := runscript("mp.MadeText(" & decimal mfun_tt_n & ")") ; + addto mfun_tt_c doublepath unitsquare + xscaled redpart mfun_tt_r + yscaled (greenpart mfun_tt_r + bluepart mfun_tt_r) + shifted (0,-bluepart mfun_tt_r) + withprescript "mf_object=text" + withprescript "tx_index=" & decimal mfun_tt_n + withprescript "tx_color=" & colordecimals colorpart mfun_tt_o + ; + mfun_tt_c +enddef ; + +extra_beginfig := extra_beginfig & "mfun_reset_tex_texts ;" ; +extra_endfig := "mfun_flush_tex_texts ; mfun_reset_tex_texts ; " & extra_endfig ; + primarydef str infont name = % nasty hack if name = "" : rawtextext(str) diff --git a/metapost/context/base/mpiv/mp-base.mpiv b/metapost/context/base/mpiv/mp-base.mpiv index 0cc209302..617855473 100644 --- a/metapost/context/base/mpiv/mp-base.mpiv +++ b/metapost/context/base/mpiv/mp-base.mpiv @@ -201,7 +201,7 @@ let graycolor = numeric ; % color part (will be overloaded) def colorpart primary t = - if colormodel t=7: + if colormodel t = 7: (cyanpart t, magentapart t, yellowpart t, blackpart t) elseif colormodel t = 5 : (redpart t, greenpart t, bluepart t) @@ -250,7 +250,7 @@ vardef whatever = ? enddef ; -% unary operators +% unary operators (with patched round) let abs = length ; @@ -258,7 +258,14 @@ vardef round primary u = if numeric u : floor(u+.5) elseif pair u : - (round xpart u, round ypart u) + (floor(xpart u+.5), floor(ypart u+.5)) + elseif path u : + % added by HH + for i=0 upto length u-1 : + round(point i of u) .. + controls round(postcontrol i of u) and round(precontrol i+1 of u) .. + endfor + if cycle u : cycle else : point infinity of u fi else : u fi @@ -323,53 +330,18 @@ primarydef w dotprod z = (xpart w * xpart z + ypart w * ypart z) enddef ; -primarydef x**y = - if y = 2 : - x*x - else : - takepower y of x - fi -enddef ; - -def takepower expr y of x = - if x>0 : - mexp(y*mlog x) - elseif (x=0) and (y>0) : - 0 - else : - 1 - if y = floor y : - if y >= 0 : - for n=1 upto y : - *x - endfor - else : - for n=-1 downto y : - /x - endfor - fi - else : - hide(errmessage "Undefined power: " & decimal x & "**" & decimal y) - fi - fi -enddef ; - -% for big number systems: -% % primarydef x**y = -% if y = 1 : -% x -% elseif y = 2 : +% if y = 2 : % x*x -% elseif y = 3 : -% x*x*x % else : % takepower y of x % fi % enddef ; -% -% vardef takepower expr y of x = -% if (x=0) and (y>0) : +% +% def takepower expr y of x = +% if x>0 : +% mexp(y*mlog x) +% elseif (x=0) and (y>0) : % 0 % else : % 1 @@ -389,6 +361,51 @@ enddef ; % fi % enddef ; +% for big number systems: + +primarydef x**y = + if y = 0 : + 1 + elseif x = 0 : + 0 + elseif y < 0 : + 1/(x**-y) + elseif y = 1 : + x + elseif y = 2 : + x*x + elseif y = 3 : + x*x*x + else : + takepower y of x + fi +enddef ; + +def takepower expr y of x = + if y=0 : % isn't x**0 = 1 even if x=0 ? + 1 + elseif x=0 : + 0 + else : + if y = floor y : + 1 + if y >= 0 : + for n=1 upto y : + *x + endfor + else : + for n=-1 downto y : + /x + endfor + fi + elseif x>0 : + mexp(y*mlog x) + else : + -mexp(y*mlog -x) + fi + fi +enddef ; + vardef direction expr t of p = postcontrol t of p - precontrol t of p enddef ; diff --git a/metapost/context/base/mpiv/mp-blob.mpiv b/metapost/context/base/mpiv/mp-blob.mpiv new file mode 100644 index 000000000..d1fc7357b --- /dev/null +++ b/metapost/context/base/mpiv/mp-blob.mpiv @@ -0,0 +1,112 @@ +%D \module +%D [ file=mp-blob.mpiv, +%D version=2018.04.08, +%D title=\CONTEXT\ \METAPOST\ graphics, +%D subtitle=Blobs, +%D author=Hans Hagen, +%D date=\currentdate, +%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}] +%C +%C This module is part of the \CONTEXT\ macro||package and is +%C therefore copyrighted by \PRAGMA. See mreadme.pdf for +%C details. + +%D This is a follow up on good old \type {meta-imp-txt}. + +if known context_blob : endinput ; fi ; + +boolean context_blob ; context_blob := true ; + +numeric mfun_blob_n ; mfun_blob_n := 0 ; +picture mfun_blob_c ; +color mfun_blob_b ; + +def mfun_reset_tex_blobs = + mfun_blob_n := 0 ; + mfun_blob_c := nullpicture ; +enddef ; + +extra_endfig := extra_endfig & "mfun_reset_tex_blobs ; " ; + +vardef mfun_inject_blob(expr n) = + mfun_blob_c := nullpicture ; + mfun_blob_b := lua.mp.mf_blob_dimensions(mfun_blob_n,n) ; + addto mfun_blob_c doublepath unitsquare + xscaled redpart mfun_blob_b + yscaled (greenpart mfun_blob_b + bluepart mfun_blob_b) + shifted (0,- bluepart mfun_blob_b) + withprescript "mf_object=texblob" + withprescript "tb_blob=" & decimal lua.mp.mf_blob_index(mfun_blob_n,n) ; + mfun_blob_c +enddef ; + +% An example of usage: + +newinternal followtextalternative ; followtextalternative := 1 ; +newinternal tracingfollowtext ; tracingfollowtext := 0 ; +newinternal autoscaleupfollowtext ; autoscaleupfollowtext := 2 ; +newinternal autoscaledownfollowtext ; autoscaledownfollowtext := 0 ; + +vardef followtext(expr pth, txt) = + image ( + mfun_blob_n := mfun_blob_n + 1 ; + lua.mp.mf_inject_blob(mfun_blob_n,txt); + save pat, al, at, pl, pc, wid, pos, ap, ad, pic, len, n, sc ; + path pat ; pat := pth ; + numeric al, at, pl, pc, wid, pos, len[], n, sc ; + pair ap, ad ; + picture pic[] ; + len[0] := 0 ; + n := lua.mp.mf_blob_size(mfun_blob_n) ; + sc := 0 ; + for i=1 upto n : + pic[i] := mfun_inject_blob(i) ; + pic[i] := pic[i] shifted - llcorner pic[i] ; + len[i] := len[i-1] + lua.mp.mf_blob_width(mfun_blob_n,i) ; + endfor ; + al := arclength pth ; + if al = 0 : + al := len[n] ; + pat := origin -- (al,0) ; + fi ; + if ((al < len[n]) and (autoscaleupfollowtext > 0)) or + ((al > len[n]) and (autoscaledownfollowtext > 0)) : + sc := len[n] /al ; + pat := pat scaled sc ; + al := arclength pat ; + fi ; + if followtextalternative = 1 : + pl := (al-len[n])/(if n>1 : (n-1) else : 1 fi) ; + pc := 0 ; + else : % centered / MP + pl := 0 ; + pc := arclength pat/2 - len[n]/2 ; + fi ; + if tracingfollowtext = 1 : + draw pat withpen pencircle scaled 1pt withcolor blue ; + fi ; + for i=1 upto n : + wid := lua.mp.mf_blob_width(mfun_blob_n,i) ; + pos := len[i]-wid/2 + (i-1)*pl + pc ; + at := arctime pos of pat ; + ap := point at of pat ; + ad := direction at of pat ; + pic[i] := pic[i] shifted (-wid/2,0) rotated(angle(ad)) shifted ap ; + draw pic[i] ; + if tracingfollowtext = 1 : + draw boundingbox pic[i] withpen pencircle scaled .25pt withcolor red ; + draw ap withpen pencircle scaled .50pt withcolor green ; + fi ; + endfor ; + if ((autoscaleupfollowtext = 2) or (autoscaledownfollowtext = 2)) and + (sc <> 0) and (sc <> 1): + currentpicture := currentpicture scaled (1/sc) ; + fi ; + if tracingfollowtext = 1 : + draw boundingbox currentpicture withpen pencircle scaled .25pt withcolor blue ; + fi ; + draw fullcircle scaled 100bp + withprescript "mf_object=followtext" + withprescript "ft_category=" & decimal mfun_blob_n ; + ) +enddef ; diff --git a/metapost/context/base/mpiv/mp-grap.mpiv b/metapost/context/base/mpiv/mp-grap.mpiv index e799f629c..0299c0587 100644 --- a/metapost/context/base/mpiv/mp-grap.mpiv +++ b/metapost/context/base/mpiv/mp-grap.mpiv @@ -266,19 +266,27 @@ enddef ; graph_margin_fraction.low=-.07 ; % bbox fraction for default range start graph_margin_fraction.high=1.07 ; % bbox fraction for default range stop +%def graph_with_pen_and_color(expr q) = +% withpen penpart q +% withcolor +% if colormodel q=1 : +% false +% elseif colormodel q=3 : +% (greypart q) +% elseif colormodel q=5 : +% (redpart q, greenpart q, bluepart q) +% elseif colormodel q=7 : +% (cyanpart q, magentapart q, yellowpart q, blackpart q) +% fi +% withprescript prescriptpart q +% withpostscript postscriptpart q +%enddef ; + def graph_with_pen_and_color(expr q) = - withpen penpart q withcolor - if colormodel q=1 : - false - elseif colormodel q=3 : - (greypart q) - elseif colormodel q=5 : - (redpart q, greenpart q, bluepart q) - elseif colormodel q=7 : - (cyanpart q, magentapart q, yellowpart q, blackpart q) - fi + withproperties q enddef ; + % Add picture component q to picture @# and change part p to tp, % where p is something from q that needs coordinate transformation. % The type of p is pair or path. diff --git a/metapost/context/base/mpiv/mp-grph.mpiv b/metapost/context/base/mpiv/mp-grph.mpiv index 04d4920f6..d4316eb91 100644 --- a/metapost/context/base/mpiv/mp-grph.mpiv +++ b/metapost/context/base/mpiv/mp-grph.mpiv @@ -65,8 +65,6 @@ enddef ; numeric currentgraphictext ; currentgraphictext := 0 ; -def data_mpy_file = job_name & "-mpgraph.mpy" enddef ; - def begingraphictextfig (expr n) = foundpicture := n ; scratchpicture := nullpicture ; @@ -117,7 +115,7 @@ def mfun_load_figure (expr filename) text figureattributes = endgroup ; enddef ; -% shared between old and new +% We only use the new method now. boolean mfun_gt_color_fill ; boolean mfun_gt_color_draw ; @@ -126,148 +124,12 @@ boolean mfun_gt_reverse_fill ; boolean mfun_gt_outline_fill ; picture mfun_gt_picture ; -% this is the old version: - -def old_graphictext primary t = - hide ( - if mfun_trial_run : - let mfun_graphic_text = mfun_no_graphic_text ; - else : - let mfun_graphic_text = mfun_do_graphic_text ; - fi - ) - mfun_graphic_text(t) -enddef ; - -def mfun_do_graphic_text (expr t) = - % withprescript "gt_stage=final" - begingroup ; - save figurepicture ; picture figurepicture ; - figurepicture := currentpicture ; currentpicture := nullpicture ; - currentgraphictext := currentgraphictext + 1 ; - mfun_finish_graphic_text % picks up directives -enddef ; - -def mfun_no_graphic_text (expr t) text rest = - currentgraphictext := currentgraphictext + 1 ; - draw unitsquare - withprescript "gt_stage=trial" - withprescript "gt_index=" & decimal currentgraphictext - withpostscript t -enddef ; - -def mfun_finish_graphic_text text rest = - protectgraphicmacros ; % resets currentpicture - interim linecap := butt ; % normally rounded - interim linejoin := mitered ; % normally rounded - interim miterlimit := 10 ; % todo - let normalwithshade = withshade ; - save foundpicture, scratchpicture, str ; - save fill, draw, withshade, reversefill, outlinefill ; - save withfillcolor, withdrawcolor ; % quite important - numeric foundpicture ; picture scratchpicture ; string str ; - def draw expr p = - % the first, naive implementation was: - % addto scratchpicture doublepath p withpen currentpen ; - % but it is better to turn lines into fills - addto scratchpicture contour boundingbox - image (addto currentpicture doublepath p withpen currentpen) ; - enddef ; - def fill expr p = - addto scratchpicture contour p withpen currentpen ; - enddef ; - def mfun_gt_fill = enddef ; boolean mfun_gt_color_fill ; mfun_gt_color_fill := false ; - def mfun_gt_draw = enddef ; boolean mfun_gt_color_draw ; mfun_gt_color_draw := false ; - def mfun_gt_shade = enddef ; boolean mfun_gt_shade_fill ; mfun_gt_shade_fill := false ; - boolean mfun_gt_reverse_fill ; mfun_gt_reverse_fill := false ; - boolean mfun_gt_outline_fill ; mfun_gt_outline_fill := false ; - def reversefill = - hide(mfun_gt_reverse_fill := true ) - enddef ; - def outlinefill = - hide(mfun_gt_outline_fill := true ) - enddef ; - def withshade primary c = - hide(def mfun_gt_shade = normalwithshade c enddef ; mfun_gt_shade_fill := true ) - enddef ; - def withfillcolor primary c = - hide(def mfun_gt_fill = withcolor c enddef ; mfun_gt_color_fill := true ) - enddef ; - def withdrawcolor primary c = - hide(def mfun_gt_draw = withcolor c enddef ; mfun_gt_color_draw := true ) - enddef ; - scratchpicture := nullpicture ; - addto scratchpicture doublepath origin rest ; % pre-roll - for i within scratchpicture : % Below here is a dirty tricky test! - if (urcorner dashpart i) = origin : - mfun_gt_outline_fill := false ; - fi ; - endfor ; - scratchpicture := nullpicture ; - readfile(data_mpy_file) ; - scratchpicture := (scratchpicture shifted -llcorner scratchpicture) scaled (1/10) ; - if not mfun_gt_color_draw and not mfun_gt_color_fill : - mfun_gt_color_draw := true ; - fi - if mfun_gt_shade_fill : - mfun_gt_color_draw := false ; - mfun_gt_color_fill := false ; - fi ; - currentpicture := figurepicture ; - if mfun_gt_shade_fill : - for i within scratchpicture : - if filled i : - addto currentpicture contour pathpart i _op_ rest mfun_gt_shade ; - fi ; - endfor ; - else : - if mfun_gt_color_draw and not mfun_gt_reverse_fill : - for i within scratchpicture : - if mfun_gt_color_fill and mfun_gt_outline_fill : - addto currentpicture doublepath pathpart i _op_ rest mfun_gt_fill dashed nullpicture ; - fi ; - if filled i : - addto currentpicture doublepath pathpart i _op_ rest mfun_gt_draw ; - fi ; - endfor ; - fi ; - if mfun_gt_color_fill : - for i within scratchpicture : - if filled i : - addto currentpicture contour pathpart i _op_ rest mfun_gt_fill withpen pencircle scaled 0 ; - fi ; - endfor ; - fi ; - if mfun_gt_color_draw and mfun_gt_reverse_fill : - for i within scratchpicture : - if filled i : - addto currentpicture doublepath pathpart i _op_ rest mfun_gt_draw ; - fi ; - endfor ; - fi ; - for i within scratchpicture : - if stroked i : - addto currentpicture doublepath pathpart i _op_ rest mfun_gt_draw ; - fi ; - endfor ; - fi ; - endgroup ; -enddef ; - -% and this is the new one: - -% boolean mfun_gt_color_fill ; -% boolean mfun_gt_color_draw ; -% boolean mfun_gt_shade_fill ; -% boolean mfun_gt_reverse_fill ; -% picture mfun_gt_picture ; - def mfun_gt_default = % somewhat compatible scaled 11.5 withpen pencircle scaled .1 enddef ; -def new_graphictext primary t = % use outlinetext instead +def graphictext primary t = % use outlinetext instead begingroup ; mfun_graphic_text_indeed(t) enddef ; @@ -310,24 +172,21 @@ def mfun_graphic_text_indeed(expr t) text rest = draw outlinetext.f(t)(mfun_gt_shade) rest; elseif mfun_gt_color_fill and mfun_gt_color_draw : if mfun_gt_reverse_fill : - draw outlinetext.r(t)(mfun_gt_default mfun_gt_fill rest)(mfun_gt_default mfun_gt_draw rest) ; + draw outlinetext.r(t)(mfun_gt_default mfun_gt_fill)(mfun_gt_default mfun_gt_draw) rest; else : - draw outlinetext.b(t)(mfun_gt_default mfun_gt_draw rest)(mfun_gt_default mfun_gt_fill rest); + draw outlinetext.b(t)(mfun_gt_default mfun_gt_draw)(mfun_gt_default mfun_gt_fill) rest; fi ; elseif mfun_gt_color_fill : - draw outlinetext.f(t)(mfun_gt_default mfun_gt_fill rest) ; + draw outlinetext.f(t)(mfun_gt_default mfun_gt_fill) rest; elseif mfun_gt_color_draw : - draw outlinetext.d(t)(mfun_gt_default mfun_gt_draw rest) ; + draw outlinetext.d(t)(mfun_gt_default mfun_gt_draw) rest ; else : - draw outlinetext.d(t)(mfun_gt_default rest) ; + draw outlinetext.d(t)(mfun_gt_default) rest ; fi ; % endgroup ; enddef ; -let graphictext = old_graphictext ; -%%% graphictext = new_graphictext ; % more than 10 times faster - % example % % beginfig (1) ; diff --git a/metapost/context/base/mpiv/mp-luas.mpiv b/metapost/context/base/mpiv/mp-luas.mpiv index d35701ec6..e5cfe0371 100644 --- a/metapost/context/base/mpiv/mp-luas.mpiv +++ b/metapost/context/base/mpiv/mp-luas.mpiv @@ -49,41 +49,95 @@ boolean context_luas ; context_luas := true ; % % Fourth variant: -string mfun_lua_bs ; mfun_lua_bs := "[[" ; -string mfun_lua_es ; mfun_lua_es := "]]" ; +string mfun_lua_bs ; mfun_lua_bs := "[===[" ; +string mfun_lua_es ; mfun_lua_es := "]===]" ; vardef mlib_luas_luacall(text t) = runscript("" for s = t : if string s : & s + % & mfun_lua_bs & s & mfun_lua_es elseif numeric s : & decimal s elseif boolean s : & if s : "true" else : "false" fi + elseif pair s : + & mfun_pair_to_table(s) + elseif path s : + & mfun_path_to_table(s) + elseif rgbcolor s : + & mfun_rgb_to_table(s) + elseif cmykcolor s : + & mfun_cmyk_to_table(s) else : & ditto & tostring(s) & ditto fi endfor ) enddef ; +% vardef mlib_luas_lualist(expr c)(text t) = +% save b ; boolean b ; b := false ; +% runscript(c & "(" for s = t : +% if b : +% & "," +% else : +% hide(b := true) +% fi +% if string s : +% % & ditto & s & ditto +% & mfun_lua_bs & s & mfun_lua_es +% elseif numeric s : +% & decimal s +% elseif boolean s : +% & if s : "true" else : "false" fi +% elseif pair s : +% & mfun_pair_to_table(s) +% elseif path s : +% & mfun_path_to_table(s) +% elseif rgbcolor s : +% & mfun_rgb_to_table(s) +% elseif cmykcolor s : +% & mfun_cmyk_to_table(s) +% else : +% & ditto & tostring(s) & ditto +% fi endfor & ")" +% ) +% enddef ; + +newinternal mfun_luas_b ; + +def mlib_luas_luadone = + exitif numeric begingroup mfun_luas_b := 1 ; endgroup ; +enddef ; + vardef mlib_luas_lualist(expr c)(text t) = - save b ; boolean b ; b := false ; - runscript(c & "(" for s = t : - if b : - & "," + interim mfun_luas_b := 0 ; + runscript(c & for s = t : + if mfun_luas_b = 0 : + "(" + % hide(mfun_luas_b := 1) + mlib_luas_luadone else : - hide(b := true) + "," fi + & if string s : - % & ditto & s & ditto - & mfun_lua_bs & s & mfun_lua_es + mfun_lua_bs & s & mfun_lua_es elseif numeric s : - & decimal s + decimal s elseif boolean s : - & if s : "true" else : "false" fi + if s : "true" else : "false" fi + elseif pair s : + mfun_pair_to_table(s) + elseif path s : + mfun_path_to_table(s) + elseif rgbcolor s : + mfun_rgb_to_table(s) + elseif cmykcolor s : + mfun_cmyk_to_table(s) else : - & ditto & tostring(s) & ditto - fi endfor & ")" + ditto & tostring(s) & ditto + fi & endfor if mfun_luas_b = 0 : "()" else : ")" fi ) enddef ; @@ -107,9 +161,14 @@ vardef MP@#(text t) = enddef ; def message expr t = - if t <> "" : lua.mp.report(t) fi ; + lua.mp.report(tostring(t)) ; enddef ; +% Modes: + +vardef texmode (expr s) = lua.mp("mode", s) enddef ; +vardef systemmode(expr s) = lua.mp("systemmode",s) enddef ; + % A few helpers vardef isarray suffix a = lua.mp.isarray (str a) enddef ; @@ -118,19 +177,15 @@ vardef dimension suffix a = lua.mp.dimension(str a) enddef ; % More access -def getdimen(expr k) = lua.mp._get_dimen_(k) enddef ; -def getcount(expr k) = lua.mp._get_count_(k) enddef ; -def gettoks (expr k) = lua.mp._get_toks_ (k) enddef ; -def setdimen(expr k, v) = lua.mp._set_dimen_(k,v) enddef ; -def setcount(expr k, v) = lua.mp._set_count_(k,v) enddef ; -def settoks (expr k, v) = lua.mp._set_toks_ (k,v) enddef ; +vardef getmacro(expr k) = lua.mp._get_macro_(k) enddef ; +vardef getdimen(expr k) = lua.mp._get_dimen_(k) enddef ; +vardef getcount(expr k) = lua.mp._get_count_(k) enddef ; +vardef gettoks (expr k) = lua.mp._get_toks_ (k) enddef ; -% vardef getdimen(expr k) = save getdimen ; lua.mp.getdimen(k) enddef ; -% vardef getcount(expr k) = save getcount ; lua.mp.getcount(k) enddef ; -% vardef gettoks (expr k) = save gettoks ; lua.mp.gettoks (k) enddef ; -% vardef setdimen(expr k,v) = save setdimen ; lua.mp.setdimen(k,v) enddef ; -% vardef setcount(expr k,v) = save setcount ; lua.mp.setcount(k,v) enddef ; -% vardef settoks (expr k,v) = save settoks ; lua.mp.settoks (k,v) enddef ; +def setmacro(expr k,v) = lua.mp._set_macro_(k,v) enddef ; +def setdimen(expr k,v) = lua.mp._set_dimen_(k,v) enddef ; +def setcount(expr k,v) = lua.mp._set_count_(k,v) enddef ; +def settoks (expr k,v) = lua.mp._set_toks_ (k,v) enddef ; vardef positionpath (expr name) = lua.mp.positionpath (name) enddef ; vardef positioncurve (expr name) = lua.mp.positioncurve (name) enddef ; @@ -154,6 +209,43 @@ vardef positionatanchor(expr name) = currentpicture := currentpicture shifted - positionxy(name) ; enddef ; - vardef texvar(expr name) = lua.mp.texvar(name) enddef ; vardef texstr(expr name) = lua.mp.texstr(name) enddef ; + +%D New experimental feature for Alan-The-Number-Cruncher: + +% \startMPcode{doublefun} +% path p ; p := ( +% for i=1 upto 20000 : +% origin randomized 10000 .. +% endfor cycle +% ) xysized (TextWidth,TextHeight) ; +% draw for i inpath p: +% pointof i .. controls (leftof i) and (rightof i) .. +% endfor cycle ; +% \stopMPcode + +% def inpath suffix p = +% = 1 step 1 until lua.mp.mf_path_length(str p) +% enddef ; + +% def inpath suffix p = +% % = 1 step 1 until lua.mp.mf_path_length(str p) +% = 1 step 1 until (begingroup save n ; n := lua.mp.mf_path_length(str p) ; if n = 0 : 1 else : n fi endgroup) +% enddef ; + +def inpath suffix p = % permits p[0] + = 1 step 1 until + begingroup + save mfun_inpath_r,mfun_inpath_n ; path mfun_inpath_r ; + mfun_inpath_r = p ; + mfun_inpath_n := lua.mp.mf_path_length(str mfun_inpath_r) ; + if mfun_inpath_n = 0 : 1 else : mfun_inpath_n fi + endgroup +enddef ; + +vardef pointof primary i = lua.mp.mf_path_point(i) enddef ; +vardef leftof primary i = lua.mp.mf_path_left (i) enddef ; +vardef rightof primary i = lua.mp.mf_path_right(i) enddef ; + +extra_endfig := extra_endfig & " lua.mp.mf_path_reset() ; " ; diff --git a/metapost/context/base/mpiv/mp-mlib.mpiv b/metapost/context/base/mpiv/mp-mlib.mpiv index 115fe63ae..131796757 100644 --- a/metapost/context/base/mpiv/mp-mlib.mpiv +++ b/metapost/context/base/mpiv/mp-mlib.mpiv @@ -16,6 +16,18 @@ if known context_mlib : endinput ; fi ; boolean context_mlib ; context_mlib := true ; +numeric LUATEXFUNCTIONALITY ; LUATEXFUNCTIONALITY := runscript("mp.print(LUATEXFUNCTIONALITY)") ; + +%D Objects: + +vardef isobject expr p = + if picture p : + lua.mp.isobject(prescriptpart p) + else : + false + fi +enddef ; + %D Color and transparency %D %D Separable: @@ -60,16 +72,16 @@ def namedcolor expr n = withprescript "sp_name=" & n enddef ; -% def spotcolor(expr n, v) = +% def mfun_spotcolor(expr n, v) = % 1 -% withprescript "sp_type=spot" +% withprescript "sp_type=xspot" % withprescript "sp_name=" & n % withprescript "sp_value=" & (if numeric v : decimal v else : v fi) % enddef ; -% -% def multitonecolor(expr name, fractions, components, value) = + +% def mfun_multispotcolor(expr name, fractions, components, value) = % 1 -% withprescript "sp_type=multitone" +% withprescript "sp_type=multispot" % withprescript "sp_name=" & name % withprescript "sp_fractions=" & decimal fractions % withprescript "sp_components=" & components @@ -79,10 +91,12 @@ enddef ; def spotcolor(expr name, v) = (1) withprescript "sp_type=spot" - withprescript "sp_name=" & name + withprescript "sp_name=" & name withprescript "sp_value=" & colordecimals v enddef ; +% In this case a mixed color will be calculated: + def multitonecolor(expr name)(text t) = (1) withprescript "sp_type=multitone" @@ -92,7 +106,7 @@ enddef ; def transparent(expr a, t)(text c) = % use withtransparency instead (1) % this permits withcolor x intoshade y - withprescript "tr_alternative=" & decimal transparency_alternative_to_number(a) + withprescript "tr_alternative=" & decimal transparency_alternative_to_number(a) withprescript "tr_transparency=" & decimal t withcolor c enddef ; @@ -174,72 +188,52 @@ extra_beginfig := extra_beginfig & "mfun_reset_tex_texts ;" ; % flush twice: once in location in order to pick up e.g. color properties, % and once at the end because we need to flush missing ones. -% see mp-keep.mpiv for older code +boolean mfun_onetime_textext ; mfun_onetime_textext := false ; -% vardef rawtextext(expr s) = % todo: avoid currentpicture -% if s = "" : -% nullpicture -% else : -% mfun_tt_n := mfun_tt_n + 1 ; -% mfun_tt_c := nullpicture ; -% if mfun_trial_run : -% mfun_tt_o := nullpicture ; -% addto mfun_tt_o doublepath origin _op_ ; % save drawoptions -% addto mfun_tt_c doublepath unitsquare -% withprescript "tx_number=" & decimal mfun_tt_n -% withprescript "tx_stage=trial" -% withprescript "tx_color=" & colordecimals colorpart mfun_tt_o -% withpostscript s ; -% addto mfun_tt_p also mfun_tt_c ; -% elseif known mfun_tt_d[mfun_tt_n] : -% addto mfun_tt_c doublepath unitsquare -% xscaled mfun_tt_w[mfun_tt_n] -% yscaled (mfun_tt_h[mfun_tt_n] + mfun_tt_d[mfun_tt_n]) -% shifted (0,-mfun_tt_d[mfun_tt_n]) -% withprescript "tx_number=" & decimal mfun_tt_n -% withprescript "tx_stage=final" ; -% else : -% addto mfun_tt_c doublepath unitsquare ; % unitpicture -% fi ; -% mfun_tt_c -% fi -% enddef ; +def notcached = withprescript "tx_cache=no" enddef ; -boolean mfun_onetime_textext ; mfun_onetime_textext := false ; +% todo: onetime + +rgbcolor mfun_tt_r ; -vardef rawtextext(expr s) = % todo: avoid currentpicture +vardef rawtextext(expr s) = if s = "" : - mfun_onetime_textext := false ; nullpicture else : mfun_tt_n := mfun_tt_n + 1 ; mfun_tt_c := nullpicture ; - if mfun_trial_run : - mfun_tt_o := nullpicture ; - addto mfun_tt_o doublepath origin _op_ ; % save drawoptions - addto mfun_tt_c doublepath unitsquare - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=trial" - withprescript "tx_color=" & colordecimals colorpart mfun_tt_o - withpostscript s ; - if not mfun_onetime_textext : - addto mfun_tt_p also mfun_tt_c - withprescript "tx_global=yes" ; - fi ; - else : - mfun_tt_b := lua.mp.tt_dimensions(mfun_tt_n) ; - addto mfun_tt_c doublepath unitsquare - xscaled redpart mfun_tt_b - yscaled (greenpart mfun_tt_b + bluepart mfun_tt_b) - shifted (0,- bluepart mfun_tt_b) - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=final" ; - fi ; - mfun_onetime_textext := false ; + mfun_tt_o := nullpicture ; + addto mfun_tt_o doublepath origin _op_ ; % save drawoptions + mfun_tt_r := lua.mp.mf_some_text(mfun_tt_n,s) ; + addto mfun_tt_c doublepath unitsquare + xscaled wdpart mfun_tt_r + yscaled (htpart mfun_tt_r + dppart mfun_tt_r) + shifted (0,-dppart mfun_tt_r) + withprescript "mf_object=text" + withprescript "tx_index=" & decimal mfun_tt_n + withprescript "tx_color=" & colordecimals colorpart mfun_tt_o + ; mfun_tt_c fi enddef ; +vardef rawmadetext = + mfun_tt_n := mfun_tt_n + 1 ; + mfun_tt_c := nullpicture ; + mfun_tt_o := nullpicture ; + addto mfun_tt_o doublepath origin _op_ ; % save drawoptions + mfun_tt_r := lua.mp.mf_made_text(mfun_tt_n) ; + addto mfun_tt_c doublepath unitsquare + xscaled wdpart mfun_tt_r + yscaled (htpart mfun_tt_r + dppart mfun_tt_r) + shifted (0,-dppart mfun_tt_r) + withprescript "mf_object=text" + withprescript "tx_index=" & decimal mfun_tt_n + withprescript "tx_color=" & colordecimals colorpart mfun_tt_o + ; + mfun_tt_c +enddef ; + vardef validtexbox(expr category, name) = if category == "" : false @@ -255,11 +249,12 @@ enddef ; vardef rawtexbox(expr category, name) = mfun_tt_c := nullpicture ; if validtexbox(category,name) : - mfun_tt_b := lua.mp.tb_dimensions(category, name) ; + mfun_tt_b := lua.mp.mf_tb_dimensions(category, name) ; addto mfun_tt_c doublepath unitsquare - xscaled redpart mfun_tt_b - yscaled (greenpart mfun_tt_b + bluepart mfun_tt_b) - shifted (0,- bluepart mfun_tt_b) + xscaled wdpart mfun_tt_b + yscaled (htpart mfun_tt_b + dppart mfun_tt_b) + shifted (0,- dppart mfun_tt_b) + withprescript "mf_object=box" withprescript "bx_category=" & if numeric category : decimal fi category withprescript "bx_name=" & if numeric name : decimal fi name ; fi @@ -368,13 +363,25 @@ vardef installlabel@# (expr type, x, y, offset) = numeric labyf @# ; labyf @# := y ; enddef ; +vardef mfun_labshift@#(expr p) = + (mfun_labxf@#*lrcorner p + + mfun_labyf@#*ulcorner p + + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p) +enddef ; + +vardef mfun_picshift@#(expr p) = + (mfun_labxf@#*ulcorner p + + mfun_labyf@#*lrcorner p + + (1-mfun_labxf@#-mfun_labyf@#)*urcorner p) +enddef ; + % we save the plain variant vardef plain_thelabel@#(expr p,z) = if string p : plain_thelabel@#(rawtextext("\definedfont[" & defaultfont & "]" & p) scaled defaultscale,z) else : - p shifted (z + labeloffset*laboff@# - (labxf@#*lrcorner p + labyf@#*ulcorner p + (1-labxf@#-labyf@#)*llcorner p)) + p shifted (z + labeloffset*laboff@# - mfun_labshift@#(p)) fi enddef; @@ -403,7 +410,22 @@ enddef ; plain_compatibility_data := plain_compatibility_data & "save label, thelabel ;" & "useplainlabels ;" ; -% next comes own own: +% vardef thetextext@#(expr p,z) = +% % interim labeloffset := textextoffset ; +% if string p : +% thetextext@#(rawtextext(p),z) +% elseif numeric p : +% thetextext@#(rawtextext(decimal p),z) +% else : +% p +% if (mfun_labtype@# >= 10) : +% shifted (0,ypart center p) +% fi +% shifted (z + textextoffset*mfun_laboff@# - mfun_labshift@#(p)) +% fi +% enddef ; + +newinternal anchortextexts ; anchortextexts := 0 ; % disabled by default vardef thetextext@#(expr p,z) = % interim labeloffset := textextoffset ; @@ -411,12 +433,18 @@ vardef thetextext@#(expr p,z) = thetextext@#(rawtextext(p),z) elseif numeric p : thetextext@#(rawtextext(decimal p),z) + elseif pair p : + thetextext@#(rawtextext(ddecimal p),z) else : - p - if (mfun_labtype@# >= 10) : - shifted (0,ypart center p) - fi - shifted (z + textextoffset*mfun_laboff@# - (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) + if anchortextexts > 0 : + image(draw p withprescript "tx_anchor=" & ddecimal z) + else : + p + fi + if (mfun_labtype@# >= 10) : + shifted (0,ypart center p) + fi + shifted (z + textextoffset*mfun_laboff@# - mfun_labshift@#(p)) fi enddef ; @@ -433,56 +461,38 @@ enddef ; pair mfun_tt_z ; -vardef rawfmttext(text t) = % todo: avoid currentpicture +vardef rawfmttext(text t) = mfun_tt_n := mfun_tt_n + 1 ; mfun_tt_c := nullpicture ; - if mfun_trial_run : - mfun_tt_o := nullpicture ; - addto mfun_tt_o doublepath origin _op_ ; % save drawoptions - addto mfun_tt_c doublepath unitsquare - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=trial" - withprescript "tx_color=" & colordecimals colorpart mfun_tt_o - % begin of fmt specific - withprescript "tx_type=format" - for s = t : - if string s : withpostscript "s:" & s - elseif numeric s : withpostscript "n:" & decimal s - elseif boolean s : withpostscript "b:" & if s : "true" else : "false" fi - elseif pair s : hide(mfun_tt_z := s ; ) - fi - endfor ; - % end of fmt specific - if not mfun_onetime_textext : - addto mfun_tt_p also mfun_tt_c - withprescript "tx_global=yes" ; - fi ; - else : - mfun_tt_b := lua.mp.tt_dimensions(mfun_tt_n) ; - addto mfun_tt_c doublepath unitsquare - xscaled redpart mfun_tt_b - yscaled (greenpart mfun_tt_b + bluepart mfun_tt_b) - shifted (0,- bluepart mfun_tt_b) - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=final" ; - % begin of fmt specific - for s = t : - if pair s : mfun_tt_z := s ; fi - endfor ; - % end of fmt specific - fi ; - mfun_onetime_textext := false ; + mfun_tt_o := nullpicture ; + addto mfun_tt_o doublepath origin _op_ ; % save drawoptions + mfun_tt_r := lua.mp.mf_formatted_text(mfun_tt_n,t) ; + addto mfun_tt_c doublepath unitsquare + xscaled wdpart mfun_tt_r + yscaled (htpart mfun_tt_r + dppart mfun_tt_r) + shifted (0,-dppart mfun_tt_r) + withprescript "mf_object=text" + withprescript "tx_index=" & decimal mfun_tt_n + withprescript "tx_color=" & colordecimals colorpart mfun_tt_o + ; + for s = t : + if pair s : mfun_tt_z := s ; fi + endfor ; mfun_tt_c enddef ; vardef thefmttext@#(text t) = - mfun_tt_z := origin ; + mfun_tt_z := origin ; % initialization save p ; picture p ; p := rawfmttext(t) ; - p + if anchortextexts > 0 : + image(draw p withprescript "tx_anchor=" & ddecimal mfun_tt_z) + else : + p + fi if (mfun_labtype@# >= 10) : shifted (0,ypart center p) fi - shifted (mfun_tt_z + textextoffset*mfun_laboff@# - (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) + shifted (mfun_tt_z + textextoffset*mfun_laboff@# - mfun_labshift@#(p)) enddef ; vardef fmttext@#(text t) = % no draw here @@ -504,18 +514,44 @@ vardef thetexbox@#(expr category, name, z) = if (mfun_labtype@# >= 10) : shifted (0,ypart center p) fi - shifted (z + textextoffset*mfun_laboff@# - (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) + shifted (z + textextoffset*mfun_laboff@# - mfun_labshift@#(p)) enddef ; vardef texbox@#(expr category, name) = % no draw here thetexbox@#(category,name,origin) enddef ; +% vardef thelabel@#(expr p,z) = +% if string p : +% thelabel@#(rawtextext("\definedfont[" & defaultfont & "]" & p) scaled defaultscale,z) +% else : +% p shifted (z + labeloffset*mfun_laboff@# - (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) +% fi +% enddef; + +vardef theoffset@#(expr z) = + if pair z : + z + elseif path z : + if mfun_laboff@# = origin : + center z + else : + ((center z)-- mfun_picshift@#(z)) intersectionpoint (z if not cycle z: --cycle fi) + fi + else : % picture + mfun_picshift@#(z) + fi +enddef; + vardef thelabel@#(expr p,z) = if string p : thelabel@#(rawtextext("\definedfont[" & defaultfont & "]" & p) scaled defaultscale,z) + elseif numeric p : + thelabel@#(decimal p,z) + elseif pair p : + thelabel@#("(" & decimal(xpart p) & "," & decimal(ypart p) & ")",z) else : - p shifted (z + labeloffset*mfun_laboff@# - (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) + p shifted (theoffset@#(z) + labeloffset*mfun_laboff@# - mfun_labshift@#(p)) fi enddef; @@ -528,7 +564,7 @@ vardef anchored@#(expr p, z) = % beware: no "+ mfun_laboff@#" here (never!) if (mfun_labtype@# >= 10) : shifted (0,ypart center p) fi - shifted (z + (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) + shifted (z + mfun_labshift@#(p)) enddef ; let normalinfont = infont ; @@ -984,18 +1020,16 @@ def withmask primary filename = withprescript "fg_mask=" & filename enddef ; -def externalfigure primary filename = - if false : - rawtextext("\externalfigure[" & filename & "]") - else : - image ( - addto currentpicture doublepath unitsquare - withprescript "fg_name=" & filename ; - ) -% unitsquare -% withpen pencircle scaled 0 -% withprescript "fg_name=" & filename - fi +vardef externalfigure primary filename = + mfun_tt_c := nullpicture ; + mfun_tt_r := lua.mp.mf_external_figure(filename) ; + addto mfun_tt_c doublepath unitsquare + xscaled wdpart mfun_tt_r + yscaled htpart mfun_tt_r + withprescript "mf_object=figure" + withprescript "fg_name=" & filename ; + ; + mfun_tt_c enddef ; def figure primary filename = @@ -1015,101 +1049,85 @@ enddef ; numeric currentoutlinetext ; currentoutlinetext := 0 ; -vardef mfun_do_outline_text_flush (expr kind, n, x, y) (text t) = +vardef mfun_do_outline_text_flush (expr kind, n, x, y, c) (text t) = if kind = "f" : - mfun_do_outline_text_f (n, x, y) (t) + mfun_do_outline_text_f (n, x, y, c) (t) elseif kind = "d" : - mfun_do_outline_text_d (n, x, y) (t) + mfun_do_outline_text_d (n, x, y, c) (t) elseif kind = "b" : - mfun_do_outline_text_b (n, x, y) (t) + mfun_do_outline_text_b (n, x, y, c) (t) elseif kind = "r" : - mfun_do_outline_text_r (n, x, y) (t) + mfun_do_outline_text_r (n, x, y, c) (t) elseif kind = "p" : - mfun_do_outline_text_p (n, x, y) (t) + mfun_do_outline_text_p (n, x, y, c) (t) elseif kind = "u" : - mfun_do_outline_text_u (n, x, y) (t) + mfun_do_outline_text_u (n, x, y, c) (t) else : - mfun_do_outline_text_n (n, x, y) (t) + mfun_do_outline_text_n (n, x, y, c) (t) fi ; enddef ; vardef mfun_do_outline_rule_flush (expr kind, x, y, w, h) = - mfun_do_outline_text_flush (kind, 1, x, y) (fullsquare xyscaled(w,h)) + mfun_do_outline_text_flush (kind, 1, x, y, "") (fullsquare xyscaled(w,h)) enddef ; numeric mfun_do_outline_n ; mfun_do_outline_n := 0 ; -vardef mfun_do_outline_text_f (expr n, x, y) (text t) = +vardef mfun_do_outline_text_f (expr n, x, y, c) (text t) = mfun_do_outline_n := 0 ; for i=t : mfun_do_outline_n := mfun_do_outline_n + 1 ; - if mfun_do_outline_n = n : - fill i shifted(x,y) mfun_do_outline_options_f withpen pencircle scaled 0 - else : - nofill i shifted(x,y) - fi ; + if mfun_do_outline_n = n : fill else : nofill fi (i shifted(x,y)) mfun_do_outline_options_f withpen pencircle scaled 0 withprescript c ; endfor ; enddef ; -vardef mfun_do_outline_text_u (expr n, x, y) (text t) = +vardef mfun_do_outline_text_u (expr n, x, y, c) (text t) = mfun_do_outline_n := 0 ; for i=t : mfun_do_outline_n := mfun_do_outline_n + 1 ; - if mfun_do_outline_n = n : - fillup i shifted(x,y) mfun_do_outline_options_f - else : - nofill i shifted(x,y) - fi ; + if mfun_do_outline_n = n : fillup else : nofill fi (i shifted(x,y)) mfun_do_outline_options_f withprescript c ; endfor ; enddef ; -vardef mfun_do_outline_text_d (expr n, x, y) (text t) = +vardef mfun_do_outline_text_d (expr n, x, y, c) (text t) = for i=t : draw i shifted(x,y) mfun_do_outline_options_d ; endfor ; enddef ; -vardef mfun_do_outline_text_p (expr n, x, y) (text t) = +vardef mfun_do_outline_text_p (expr n, x, y, c) (text t) = for i=t : - draw i shifted(x,y) ; + draw i shifted(x,y) withprescript c ; endfor ; enddef ; -vardef mfun_do_outline_text_b (expr n, x, y) (text t) = +vardef mfun_do_outline_text_b (expr n, x, y, c) (text t) = mfun_do_outline_n := 0 ; for i=t : mfun_do_outline_n := mfun_do_outline_n + 1 ; - if mfun_do_outline_n = n : - fill i shifted(x,y) mfun_do_outline_options_f - else : - nofill i shifted(x,y) - fi ; + if mfun_do_outline_n = n : fill else : nofill fi (i shifted(x,y)) mfun_do_outline_options_f ; endfor ; for i=t : draw i shifted(x,y) mfun_do_outline_options_d ; endfor ; enddef ; -vardef mfun_do_outline_text_r (expr n, x, y) (text t) = +vardef mfun_do_outline_text_r (expr n, x, y, c) (text t) = mfun_do_outline_n := 0 ; for i=t : draw i shifted(x,y) mfun_do_outline_options_d ; endfor ; for i=t : mfun_do_outline_n := mfun_do_outline_n + 1 ; - if mfun_do_outline_n = n : - fill i shifted(x,y) mfun_do_outline_options_f - else : - nofill i shifted(x,y) - fi ; + if mfun_do_outline_n = n : fill else : nofill fi (i shifted(x,y)) mfun_do_outline_options_f; endfor ; enddef ; -vardef mfun_do_outline_text_n (expr n, x, y) (text t) = +vardef mfun_do_outline_text_n (expr n, x, y, c) (text t) = mfun_do_outline_n := 0 ; for i=t : mfun_do_outline_n := mfun_do_outline_n + 1 ; - if mfun_do_outline_n = n : fill else : nofill fi i shifted(x,y) ; + if mfun_do_outline_n = n : fill else : nofill fi (i shifted(x,y)) ; endfor ; enddef ; @@ -1146,6 +1164,39 @@ def mfun_do_outline_options_d = enddef ; def mfun_do_outline_options_f = enddef ; def mfun_do_outline_options_r = enddef ; +def outlinetexttopath(text o, p, n) = + scantokens("numeric " & str n & ";") ; + scantokens("path " & str p & "[];") ; + n := 0 ; + for i within o : p[incr(n)] := pathpart i ; endfor ; +enddef ; + +def filloutlinetext(expr o) = + draw image ( + save n, m ; numeric n, m ; n := m := 0 ; + for i within o : + n := n + 1 ; + endfor ; + for i within o : + m := m + 1 ; + if n = m : + eofill + else : + nofill + fi pathpart i ; + endfor ; + ) +enddef ; + +def drawoutlinetext(expr o) = + draw image ( + % nicer for properties + for i within o : + draw pathpart i ; + endfor ; + ) +enddef ; + vardef outlinetext@# (expr t) text rest = save kind ; string kind ; kind := str @# ; currentoutlinetext := currentoutlinetext + 1 ; @@ -1153,32 +1204,25 @@ vardef outlinetext@# (expr t) text rest = def mfun_do_outline_options_f = enddef ; def mfun_do_outline_options_r = enddef ; image ( normaldraw image ( - if mfun_trial_run : - % lua.mp.report("set outline text",currentoutlinetext); - normaldraw unitsquare - withprescript "ot_stage=trial" - withprescript "ot_index=" & decimal currentoutlinetext - withprescript "ot_kind=" & kind - withpostscript t ; + % lua.mp.report("set outline text",currentoutlinetext); + lua.mp.mf_outline_text(currentoutlinetext,t,kind) ; + % lua.mp.report("get outline text",currentoutlinetext); + if kind = "f" : + mfun_do_outline_text_set_f rest ; + elseif kind = "d" : + mfun_do_outline_text_set_d rest ; + elseif kind = "b" : + mfun_do_outline_text_set_b rest ; + elseif kind = "u" : + mfun_do_outline_text_set_f rest ; + elseif kind = "r" : + mfun_do_outline_text_set_r rest ; + elseif kind = "p" : + mfun_do_outline_text_set_p ; else : - % lua.mp.report("get outline text",currentoutlinetext); - if kind = "f" : - mfun_do_outline_text_set_f rest ; - elseif kind = "d" : - mfun_do_outline_text_set_d rest ; - elseif kind = "b" : - mfun_do_outline_text_set_b rest ; - elseif kind = "u" : - mfun_do_outline_text_set_f rest ; - elseif kind = "r" : - mfun_do_outline_text_set_r rest ; - elseif kind = "p" : - mfun_do_outline_text_set_p ; - else : - mfun_do_outline_text_set_n rest ; - fi ; - lua.mp.get_outline_text(currentoutlinetext) ; + mfun_do_outline_text_set_n rest ; fi ; + lua.mp.mf_get_outline_text(currentoutlinetext) ; ) mfun_do_outline_options_r ; ) enddef ; @@ -1209,7 +1253,6 @@ vardef rule(expr wd,ht,dp) = image (fill (0,-dp)--(wd,-dp)--(wd,ht)--(0,ht)--cycle) enddef ; - % Housekeeping extra_beginfig := extra_beginfig & "currentgraphictext := 0 ; " ; @@ -1254,9 +1297,9 @@ def withproperties expr p = withcolor (cyanpart p,magentapart p,yellowpart p,blackpart p) fi withpen penpart p - % if dashpart p <> nullpicture : % fails + if length (dashpart p) > 0 : dashed dashpart p - % fi + fi withprescript prescriptpart p withpostscript postscriptpart p enddef ; @@ -1357,7 +1400,42 @@ vardef mfun_cmykcolor_to_string(expr c) = decimal blackpart c enddef ; -vardef mfun_greycolor_to_string(expr n) = +vardef mfun_pair_to_table(expr p) = + "{" & decimal xpart p & + "," & decimal ypart p & + "}" +enddef ; + +vardef mfun_point_to_table(expr p,i) = + "{" & decimal xpart (point i of p) & + "," & decimal ypart (point i of p) & + "," & decimal xpart (precontrol i of p) & + "," & decimal ypart (precontrol i of p) & + "," & decimal xpart (postcontrol i of p) & + "," & decimal ypart (postcontrol i of p) & + "}" +enddef ; + +vardef mfun_path_to_table(expr p) = + "{" & mfun_point_to_table(p,0) for i=1 upto length(p) : & "," & mfun_point_to_table(p,i) endfor & "}" +enddef ; + +vardef mfun_rgb_to_table(expr c) = + "{" & decimal redpart c & + "," & decimal greenpart c & + "," & decimal bluepart c & + "}" +enddef ; + +vardef mfun_cmyk_to_table(expr c) = + "{" & decimal cyanpart c & + "," & decimal magentapart c & + "," & decimal yellowpart c & + "," & decimal blackpart c & + "}" +enddef ; + +vardef mfun_grey_to_string(expr n) = decimal n enddef ; @@ -1568,8 +1646,8 @@ enddef ; vardef strfmt(expr f, x) = "\MPgraphformat{" & escaped_format(f) & "}{" & mfun_tagged_string(x) & "}" enddef ; vardef varfmt(expr f, x) = "\MPformatted{" & escaped_format(f) & "}{" & mfun_tagged_string(x) & "}" enddef ; -vardef format (expr f, x) = textext(strfmt(f, x)) enddef ; -vardef formatted(expr f, x) = textext(varfmt(f, x)) enddef ; +vardef format@# (expr f, x) = textext@#(strfmt(f, x)) enddef ; +vardef formatted@#(expr f, x) = textext@#(varfmt(f, x)) enddef ; % could be this (something to discuss with alan as it involves graph): % @@ -1596,12 +1674,15 @@ def nofill text t = fill t withpostscript "collect" enddef ; % so we can do: withcolor "red" -vardef resolvedcolor primary s = - % lua.mp.namedcolor(s) % conflicts with macro namedcolor - % lua.mp.NamedColor(s) % okay but, can also be - % lua.mp("NamedColor",s) % which gives expansion mess +% We do a low level runscript: +% +% lua.mp.namedcolor(s) % conflicts with macro namedcolor +% lua.mp.mf_named_color(s) % okay but, can also be +% lua.mp("mf_named_color",s) % which gives expansion mess + +def resolvedcolor primary s = % no vardef if string s : - runscript("mp.NamedColor('" & s & "')") % faster anyway + runscript("mp.mf_named_color('" & s & "')") % faster anyway else : s fi @@ -1614,6 +1695,10 @@ def comment expr str = special "metapost.comment[[" & str & "]]" ; enddef ; +vardef report(text t) = + lua.mp.report(t) +enddef ; + % This overloads a dummy: vardef uniquelist(suffix list) = diff --git a/metapost/context/base/mpiv/mp-node.mpiv b/metapost/context/base/mpiv/mp-node.mpiv index 79391220b..5829558e6 100644 --- a/metapost/context/base/mpiv/mp-node.mpiv +++ b/metapost/context/base/mpiv/mp-node.mpiv @@ -53,6 +53,8 @@ vardef makenode@#(text t) = for a = t : if (path a) or (unknown a) : mfun_makenode@#(t,) + elseif (string a) and (length(a) = 0) : + mfun_makenode@#(t,) else : mfun_makenode@#(nodepath, t,) fi @@ -64,6 +66,8 @@ vardef node@#(text t) = for a = t : if (path a) or (unknown a) : mfun_node@#(t,) + elseif (string a) and (length(a) = 0) : + mfun_node@#(t,) else : mfun_node@#(nodepath, t,) fi @@ -75,6 +79,8 @@ vardef nodeboundingpoint@#(text t) = for a = t : if (path a) or (unknown a) : mfun_nodeboundingpoint@#(t) + elseif (string a) and (length(a) = 0) : + mfun_nodeboundingpoint@#(t) else : mfun_nodeboundingpoint@#(nodepath,a) fi @@ -231,7 +237,7 @@ vardef betweennodes@#(suffix p)(expr f)(suffix q)(text s) = for a = s : if unknown t : t = a ; - nodeboundingpoint@#(q,t) + nodeboundingpoint@#(p,f) + mfun_nodeboundingpoint@#(q,t) + mfun_nodeboundingpoint@#(p,f) else : + relative@#(a) fi diff --git a/metapost/context/base/mpiv/mp-page.mpiv b/metapost/context/base/mpiv/mp-page.mpiv index 6e93bdaae..eba66a005 100644 --- a/metapost/context/base/mpiv/mp-page.mpiv +++ b/metapost/context/base/mpiv/mp-page.mpiv @@ -188,6 +188,8 @@ fi ; string CurrentLayout ; CurrentLayout := "default" ; +% runscript("mp.PaperHeight()") % way faster of course + vardef PaperHeight = lua.mp.PaperHeight () enddef ; vardef PaperWidth = lua.mp.PaperWidth () enddef ; vardef PrintPaperHeight = lua.mp.PrintPaperHeight () enddef ; @@ -319,8 +321,9 @@ vardef OverlayHeight = lua.mp.OverlayHeight () enddef ; vardef OverlayDepth = lua.mp.OverlayDepth () enddef ; vardef OverlayLineWidth = lua.mp.OverlayLineWidth() enddef ; vardef OverlayOffset = lua.mp.OverlayOffset () enddef ; +vardef OverlayRegion = lua.mp.OverlayRegion () enddef ; -vardef defaultcolormodel = lua.mp.defaultcolormodel() enddef ; +vardef defaultcolormodel = lua.mp.mf_default_color_model() enddef ; % def OverlayLineColor = lua.mp.OverlayLineColor() enddef ; % def OverlayColor = lua.mp.OverlayColor () enddef ; diff --git a/metapost/context/base/mpiv/mp-tool.mpiv b/metapost/context/base/mpiv/mp-tool.mpiv index 75706e09b..2cdb5fb39 100644 --- a/metapost/context/base/mpiv/mp-tool.mpiv +++ b/metapost/context/base/mpiv/mp-tool.mpiv @@ -17,6 +17,8 @@ boolean context_tool ; context_tool := true ; let @## = @# ; +let noexpand = quote ; + %D New, version number testing: %D %D \starttyping @@ -242,19 +244,19 @@ boolean savingdata ; savingdata := false ; boolean savingdatadone ; savingdatadone := false ; def savedata expr txt = - lua.mp.save_data(txt); + lua.mp.mf_save_data(txt); enddef ; def startsavingdata = - lua.mp.start_saving_data(); + lua.mp.mf_start_saving_data(); enddef ; def stopsavingdata = - lua.mp.stop_saving_data() ; + lua.mp.mf_stop_saving_data() ; enddef ; def finishsavingdata = - lua.mp.finish_saving_data() ; + lua.mp.mf_finish_saving_data() ; enddef ; %D Instead of a keystroke eating save and allocation @@ -302,7 +304,7 @@ def newpair text v = forsuffixes i=v : save i ; pair i ; endfor ; endd %D set_inner_boundingbox p %D \stoptyping -path mfun_boundingbox_stack ; +path mfun_boundingbox_stack[] ; numeric mfun_boundingbox_stack_depth ; mfun_boundingbox_stack_depth := 0 ; @@ -314,7 +316,7 @@ enddef ; def popboundingbox text p = setbounds p to mfun_boundingbox_stack[mfun_boundingbox_stack_depth] ; - mfun_boundingbox_stack[mfun_boundingbox_stack_depth] := origin ; + mfun_boundingbox_stack[mfun_boundingbox_stack_depth] := origin -- cycle ; mfun_boundingbox_stack_depth := mfun_boundingbox_stack_depth - 1 ; enddef ; @@ -323,7 +325,7 @@ let pop_boundingbox = popboundingbox ; % downward compatible vardef boundingbox primary p = if (path p) or (picture p) : - llcorner p -- lrcorner p -- urcorner p -- ulcorner p + llcorner p -- lrcorner p -- urcorner p -- ulcorner p else : origin fi -- cycle @@ -395,6 +397,14 @@ vardef boundingpoint@#(expr p) = fi enddef ; +def mirrored primary a = + a scaled -1 +enddef ; + +primarydef a mirroredabout b = + (a shifted -b) scaled -1 shifted b +enddef ; + %D Some missing functions can be implemented rather straightforward (thanks to %D Taco and others): @@ -2881,13 +2891,15 @@ enddef ; % This could be standard mplib 2 behaviour: +% vardef rcomponent expr p = if rgbcolor p : redpart elseif cmykcolor p : 1 - cyanpart fi p enddef ; + vardef rcomponent expr p = if rgbcolor p : redpart p elseif cmykcolor p : 1 - cyanpart p else : p fi enddef ; vardef gcomponent expr p = if rgbcolor p : greenpart p elseif cmykcolor p : 1 - magentapart p else : p fi enddef ; vardef bcomponent expr p = if rgbcolor p : bluepart p elseif cmykcolor p : 1 - yellowpart p else : p fi enddef ; vardef ccomponent expr p = if cmykcolor p : cyanpart p elseif rgbcolor p : 1 - redpart p else : p fi enddef ; vardef mcomponent expr p = if cmykcolor p : magentapart p elseif rgbcolor p : 1 - greenpart p else : p fi enddef ; vardef ycomponent expr p = if cmykcolor p : yellowpart p elseif rgbcolor p : 1 - bluepart p else : p fi enddef ; -vardef bcomponent expr p = if cmykcolor p : blackpart p elseif rgbcolor p : 0 else : p fi enddef ; +vardef kcomponent expr p = if cmykcolor p : blackpart p elseif rgbcolor p : 0 else : p fi enddef ; % draw image (...) ... ; % prescripts prepended to first, postscripts appended to last % draw decorated (...) ... ; % prescripts prepended to each, postscripts appended to each @@ -3405,8 +3417,7 @@ primarydef p crossingunder q = a := hold[hold] ; hold := hold - 1 ; fi if i = crossingnumbermax : - message("crossingunder reached maximum " & decimal i & - " intersections."); + message("crossingunder reached maximum " & decimal i & " intersections."); fi endfor @@ -3554,3 +3565,40 @@ enddef ; vardef tocycle(suffix p)(text t) = topath(p,t) t cycle enddef ; + +% reimplemented to support paths and pictures + +def drawdot expr p = + if pair p : + addto currentpicture doublepath p + withpen currentpen _op_ + elseif path p : + draw image ( + for i=0 upto length p : + addto currentpicture doublepath point i of p + withpen currentpen _op_ ; + endfor ; + ) + elseif picture p : + draw image ( + save pp ; path pp ; + for i within p : + if stroked i or filled i : + pp := pathpart i ; + for j=0 upto length pp : + addto currentpicture doublepath point j of pp + withpen currentpen _op_ ; + endfor ; + fi ; + endfor ; + ) + fi +enddef ; + +% vardef textlength(text t) = +% save n ; n := 0 ; +% for i = t : +% n := n + 1 ; +% endfor; +% n +% enddef; diff --git a/metapost/context/base/mpiv/mp-tres.mpiv b/metapost/context/base/mpiv/mp-tres.mpiv index 335670a98..c331d71af 100644 --- a/metapost/context/base/mpiv/mp-tres.mpiv +++ b/metapost/context/base/mpiv/mp-tres.mpiv @@ -96,14 +96,14 @@ primarydef p XYZscaled q = (q*Xpart p,q*Ypart p,q*Zpart p) enddef ; -vardef projection expr t = - if triplet t : +vardef projection primary t = + (if triplet t : (Xpart t, Ypart t) transformed Txy shifted (0,Zpart t) - elseif pair t : + elseif pair t : t transformed Txy - else : + else : origin transformed Txy - fi + fi) enddef ; %D This overloads the plain macro \type {abs} (being \type {length}): @@ -166,6 +166,59 @@ vardef draw_vector@# (expr v, s) text t = fi enddef ; +%D Transform a (2D) path to a 3D plane + +def XYpath primary p = + (for i=0 upto (length p) if cycle p: -1 fi : + if i>0 : .. fi + projection (xpart point i of p, + ypart point i of p, + 0) + ..controls + projection (xpart postcontrol i of p, + ypart postcontrol i of p, + 0) + and + projection (xpart precontrol (i+1) of p, + ypart precontrol (i+1) of p, + 0) + endfor if cycle p: ..cycle fi) +enddef ; + +def XZpath primary p = + (for i=0 upto (length p) if cycle p: -1 fi : + if i>0 : .. fi + projection (xpart point i of p, + 0, + ypart point i of p) + ..controls + projection (xpart postcontrol i of p, + 0, + ypart postcontrol i of p) + and + projection (xpart precontrol (i+1) of p, + 0, + ypart precontrol (i+1) of p) + endfor if cycle p: ..cycle fi) +enddef ; + +def YZpath primary p = + (for i=0 upto (length p) if cycle p: -1 fi : + if i>0 : .. fi + projection (0, + xpart point i of p, + ypart point i of p) + ..controls + projection (0, + xpart postcontrol i of p, + ypart postcontrol i of p) + and + projection (0, + xpart precontrol (i+1) of p, + ypart precontrol (i+1) of p) + endfor if cycle p: ..cycle fi) +enddef ; + %D Some constants... triplet Origin, Xunitvector, Yunitvector, Zunitvector ; -- cgit v1.2.3