From 65ed578259121f17a365b97956d19d67e0be8f60 Mon Sep 17 00:00:00 2001 From: Context Git Mirror Bot Date: Sat, 20 Aug 2016 14:47:44 +0200 Subject: 2016-08-20 13:44:00 --- metapost/context/base/mpiv/mp-luas.mpiv | 4 + metapost/context/base/mpiv/mp-mlib.mpiv | 229 ++++++++++---------------------- metapost/context/base/mpiv/mp-tool.mpiv | 74 +++++------ 3 files changed, 111 insertions(+), 196 deletions(-) (limited to 'metapost') diff --git a/metapost/context/base/mpiv/mp-luas.mpiv b/metapost/context/base/mpiv/mp-luas.mpiv index c7c97228e..2c640960b 100644 --- a/metapost/context/base/mpiv/mp-luas.mpiv +++ b/metapost/context/base/mpiv/mp-luas.mpiv @@ -57,6 +57,8 @@ vardef mlib_luas_luacall(text t) = & decimal s elseif boolean s : & if s : "true" else : "false" fi + else : + & ditto & tostring(s) & ditto fi endfor ) enddef ; @@ -75,6 +77,8 @@ vardef mlib_luas_lualist(expr c)(text t) = & decimal s elseif boolean s : & if s : "true" else : "false" fi + else : + & ditto & tostring(s) & ditto fi endfor & ")" ) enddef ; diff --git a/metapost/context/base/mpiv/mp-mlib.mpiv b/metapost/context/base/mpiv/mp-mlib.mpiv index add58a43b..6daf5672e 100644 --- a/metapost/context/base/mpiv/mp-mlib.mpiv +++ b/metapost/context/base/mpiv/mp-mlib.mpiv @@ -1268,11 +1268,7 @@ primarydef t asgroup s = % s = isolated|knockout wrappedpicture:= nullpicture ; addto wrappedpicture contour groupbounds withprescript "gr_state=start" - withprescript "gr_type=" & s - withprescript "gr_llx=" & decimal xpart llcorner groupbounds - withprescript "gr_lly=" & decimal ypart llcorner groupbounds - withprescript "gr_urx=" & decimal xpart urcorner groupbounds - withprescript "gr_ury=" & decimal ypart urcorner groupbounds ; + withprescript "gr_type=" & s ; addto wrappedpicture also grouppicture ; addto wrappedpicture contour groupbounds withprescript "gr_state=stop" ; @@ -1369,19 +1365,6 @@ vardef mfun_boolean_to_string(expr b) = if b : "true" else : "false" fi enddef ; -% def passvariable(expr key, value) = -% special -% if numeric value : "1:" & key & "=" & mfun_numeric_to_string(value) -% elseif pair value : "4:" & key & "=" & mfun_pair_to_string(value) -% elseif rgbcolor value : "5:" & key & "=" & mfun_rgbcolor_to_string(value) -% elseif cmykcolor value : "6:" & key & "=" & mfun_cmykcolor_to_string(value) -% elseif boolean value : "3:" & key & "=" & mfun_boolean_to_string(value) -% elseif path value : "7:" & key & "=" & mfun_path_to_string(value) -% elseif transform value : "8:" & key & "=" & mfun_transform_to_string(value) -% else : "2:" & key & "=" & value -% fi ; -% enddef ; - vardef tostring(expr value) = if numeric value : mfun_numeric_to_string(value) elseif pair value : mfun_pair_to_string(value) @@ -1407,178 +1390,106 @@ vardef mfun_tagged_string(expr value) = fi enddef ; -% amore flexible variant for passing data to context +% A more flexible variant for passing data to context. We used to construct strings +% but running lua is fast enough so we can gain on string construction in metapost +% which is also not that efficient. -vardef mfun_point_to_lua(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) - & "}" +vardef mfun_key_to_lua(expr k) = + if numeric k : decimal k else : "'" & k & "'" fi enddef ; -vardef mfun_transform_to_lua(expr t) = - "{" & - decimal xxpart t & "," & % rx - decimal xypart t & "," & % sx - decimal yxpart t & "," & % sy - decimal yypart t & "," & % ry - decimal xpart t & "," & % tx - decimal ypart t % ty - & "}" +vardef mfun_point_to_lua(expr k,p,i) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & ",{" & + 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_numeric_to_lua(expr n) = - decimal n -enddef ; - -vardef mfun_pair_to_lua(expr p) = - "{" & - decimal xpart p & "," & - decimal ypart p - & "}" +vardef mfun_transform_to_lua(expr k,t) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & ",{" & + decimal xxpart t & "," & % rx + decimal xypart t & "," & % sx + decimal yxpart t & "," & % sy + decimal yypart t & "," & % ry + decimal xpart t & "," & % tx + decimal ypart t % ty + & "})" ) ; enddef ; -vardef mfun_rgbcolor_to_lua(expr c) = - "{" & - decimal redpart c & "," & - decimal greenpart c & "," & - decimal bluepart c - & "}" +vardef mfun_numeric_to_lua(expr k,n) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & "," & decimal n & ")" ) ; enddef ; -vardef mfun_cmykcolor_to_lua(expr c) = - "{" & - decimal cyanpart c & "," & - decimal magentapart c & "," & - decimal yellowpart c & "," & - decimal blackpart c - & "}" +vardef mfun_pair_to_lua(expr k,p) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & ",{" & + decimal xpart p & "," & + decimal ypart p + & "})" ) ; enddef ; -vardef mfun_path_to_lua(expr p) = - "{" & - mfun_point_to_lua(p,0) for i=1 upto length(p) : & "," & mfun_point_to_lua(p,i) endfor - & "}" +vardef mfun_rgbcolor_to_lua(expr k,c) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & ",{" & + decimal redpart c & "," & + decimal greenpart c & "," & + decimal bluepart c + & "})" ) ; enddef ; -vardef mfun_boolean_to_lua(expr b) = - if b : "true" else : "false" fi +vardef mfun_cmykcolor_to_lua(expr k,c) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & ",{" & + decimal cyanpart c & "," & + decimal magentapart c & "," & + decimal yellowpart c & "," & + decimal blackpart c + & "})" ) ; enddef ; -vardef mfun_string_to_lua(expr s) = - "[==[" & s & "]==]" +vardef mfun_path_to_lua(expr k,p) = + runscript("metapost.pushvariable(" & mfun_key_to_lua(k) & ")") ; + for i=0 upto length(p) : + mfun_point_to_lua(i+1,p,i) ; + endfor ; + runscript("metapost.popvariable()") ; enddef ; -def mfun_to_lua(expr key)(expr value)(text t) = - special "metapost.variables['" & key & "']=" & t(value) ; +vardef mfun_boolean_to_lua(expr k,b) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & if b : ",true)" else : ",false)" fi ) ; enddef ; -def mfun_array_to_lua(expr key)(suffix value)(expr first, last, stp)(text t) = - special - "metapost.variables['" & key & "']={" - for i=first step stp until last : - & "[" & decimal i & "]=" & t(value[i]) & "," - endfor - & "}" ; +vardef mfun_string_to_lua(expr k,s) = + runscript( "metapost.setvariable(" & mfun_key_to_lua(k) & ",[==[" & s & "]==])" ) ; enddef ; def passvariable(expr key, value) = - if numeric value : mfun_to_lua(key,value,mfun_numeric_to_lua) - elseif pair value : mfun_to_lua(key,value,mfun_pair_to_lua) - elseif string value : mfun_to_lua(key,value,mfun_string_to_lua) - elseif boolean value : mfun_to_lua(key,value,mfun_boolean_to_lua) - elseif path value : mfun_to_lua(key,value,mfun_path_to_lua) - elseif rgbcolor value : mfun_to_lua(key,value,mfun_rgbcolor_to_lua) - elseif cmykcolor value : mfun_to_lua(key,value,mfun_cmykcolor_to_lua) - elseif transform value : mfun_to_lua(key,value,mfun_transform_to_lua) + if numeric value : mfun_numeric_to_lua (key,value) ; + elseif pair value : mfun_pair_to_lua (key,value) ; + elseif string value : mfun_string_to_lua (key,value) ; + elseif boolean value : mfun_boolean_to_lua (key,value) ; + elseif path value : mfun_path_to_lua (key,value) ; + elseif rgbcolor value : mfun_rgbcolor_to_lua (key,value) ; + elseif cmykcolor value : mfun_cmykcolor_to_lua(key,value) ; + elseif transform value : mfun_transform_to_lua(key,value) ; fi ; enddef ; def passarrayvariable(expr key)(suffix values)(expr first, last, stp) = - if numeric values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_numeric_to_lua) - elseif pair values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_pair_to_lua) - elseif string values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_string_to_lua) - elseif boolean values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_boolean_to_lua) - elseif path values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_path_to_lua) - elseif rgbcolor values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_rgbcolor_to_lua) - elseif cmykcolor values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_cmykcolor_to_lua) - elseif transform values[first] : mfun_array_to_lua(key,values,first,last,stp,mfun_transform_to_lua) - fi ; + runscript("metapost.pushvariable(" & mfun_key_to_lua(key) & ")") ; + for i=first step stp until last : + passvariable(i, values[i]) ; + endfor + runscript("metapost.popvariable()") ; enddef ; def startpassingvariable(expr k) = - begingroup ; - save stoppassingvariable, startarray, stoparray, starthash, stophash, index, key, value, slot, entry ; - let stoppassingvariable = mfun_stop_lua_variable ; - let startarray = mfun_start_lua_array ; - let stoparray = mfun_stop_lua_array ; - let starthash = mfun_start_lua_hash ; - let stophash = mfun_stop_lua_hash ; - let index = mfun_lua_index ; - let key = mfun_lua_key ; - let value = mfun_lua_value ; - let slot = mfun_lua_slot ; - let entry = mfun_lua_entry ; - save s ; string s ; - s := "metapost.variables['" & k & "']=" -enddef ; - -def mfun_stop_lua_variable = - ; - special substring(0,length(s)-1) of s ; - endgroup ; -enddef ; - -% currently there is no difference between array and hash - -def mfun_start_lua_array = - & "{" -enddef ; - -def mfun_stop_lua_array = - & "}," -enddef ; - -def mfun_start_lua_hash = - & "{" -enddef ; - -def mfun_stop_lua_hash = - & "}," -enddef ; - -def mfun_lua_key(expr k) = - & "['" & k & "']=" -enddef ; - -def mfun_lua_index(expr k) = - & "[" & decimal k & "]=" -enddef ; - -def mfun_lua_value(expr v) = - if numeric v : & mfun_numeric_to_lua(v) & "," - elseif pair v : & mfun_pair_to_lua(v) & "," - elseif string v : & mfun_string_to_lua(v) & "," - elseif boolean v : & mfun_boolean_to_lua(v) & "," - elseif path v : & mfun_path_to_lua(v) & "," - elseif rgbcolor v : & mfun_rgbcolor_to_lua(v) & "," - elseif cmykcolor v : & mfun_cmykcolor_to_lua(v) & "," - elseif transform v : & mfun_transform_to_lua(v) & "," - fi -enddef ; - -def mfun_lua_entry(expr k, v) = - mfun_lua_key(k) - mfun_lua_value(v) + runscript("metapost.pushvariable(" & mfun_key_to_lua(k) & ")") ; enddef ; -def mfun_lua_slot(expr k, v) = - mfun_lua_index(k) - mfun_lua_value(v) +def stoppassingvariable = + runscript("metapost.popvariable()") ; enddef ; % moved here from mp-grap.mpiv diff --git a/metapost/context/base/mpiv/mp-tool.mpiv b/metapost/context/base/mpiv/mp-tool.mpiv index 01691724f..97e817075 100644 --- a/metapost/context/base/mpiv/mp-tool.mpiv +++ b/metapost/context/base/mpiv/mp-tool.mpiv @@ -2615,41 +2615,41 @@ vardef undecorated (text imagedata) text decoration = currentpicture enddef ; -if metapostversion < 1.770 : - - vardef decorated (text imagedata) text decoration = - save mfun_decorated_path, currentpicture ; - picture mfun_decorated_path, currentpicture ; - currentpicture := nullpicture ; - imagedata ; - mfun_decorated_path := currentpicture ; - currentpicture := nullpicture ; - for i within mfun_decorated_path : - addto currentpicture - if stroked i : - doublepath pathpart i - dashed dashpart i - withpen penpart i - withcolor colorpart i - decoration - elseif filled i : - contour pathpart i - withpen penpart i - withcolor colorpart i - decoration - elseif textual i : - also i - withcolor colorpart i - decoration - else : - also i - fi - ; - endfor ; - currentpicture - enddef ; - -else: +% if metapostversion < 1.770 : +% +% vardef decorated (text imagedata) text decoration = +% save mfun_decorated_path, currentpicture ; +% picture mfun_decorated_path, currentpicture ; +% currentpicture := nullpicture ; +% imagedata ; +% mfun_decorated_path := currentpicture ; +% currentpicture := nullpicture ; +% for i within mfun_decorated_path : +% addto currentpicture +% if stroked i : +% doublepath pathpart i +% dashed dashpart i +% withpen penpart i +% withcolor colorpart i +% decoration +% elseif filled i : +% contour pathpart i +% withpen penpart i +% withcolor colorpart i +% decoration +% elseif textual i : +% also i +% withcolor colorpart i +% decoration +% else : +% also i +% fi +% ; +% endfor ; +% currentpicture +% enddef ; +% +% else: vardef decorated (text imagedata) text decoration = save mfun_decorated_path, currentpicture ; @@ -2689,7 +2689,7 @@ else: currentpicture enddef ; -fi ; +% fi ; vardef redecorated (text imagedata) text decoration = save mfun_decorated_path, currentpicture ; @@ -2918,7 +2918,7 @@ enddef ; %D The sorter is used in: -vardef shaped (suffix p) = % takes a list of paths +vardef shapedlist(suffix p) = % takes a list of paths save l ; pair l[] ; save r ; pair r[] ; save i ; i := 1 ; -- cgit v1.2.3