diff options
Diffstat (limited to 'Master/texmf-dist/metapost/context/base/mp-mlib.mpiv')
-rw-r--r-- | Master/texmf-dist/metapost/context/base/mp-mlib.mpiv | 456 |
1 files changed, 429 insertions, 27 deletions
diff --git a/Master/texmf-dist/metapost/context/base/mp-mlib.mpiv b/Master/texmf-dist/metapost/context/base/mp-mlib.mpiv index 9a20429241e..252cd5fd06e 100644 --- a/Master/texmf-dist/metapost/context/base/mp-mlib.mpiv +++ b/Master/texmf-dist/metapost/context/base/mp-mlib.mpiv @@ -99,6 +99,8 @@ newinternal textextoffset ; textextoffset := 0 ; numeric mfun_tt_w[], mfun_tt_h[], mfun_tt_d[] ; % we can consider using colors (less hash space) 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 ; @@ -117,6 +119,8 @@ 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 = @@ -131,37 +135,100 @@ 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. -vardef rawtextext(expr str) = +% vardef rawtextext(expr str) = +% if str = "" : +% nullpicture +% elseif mfun_trial_run : +% mfun_tt_n := mfun_tt_n + 1 ; +% mfun_tt_o := image(draw origin) ; % save drawoptions +% addto mfun_tt_p doublepath unitsquare +% withprescript "tx_number=" & decimal mfun_tt_n +% withprescript "tx_stage=extra" +% withpostscript str ; +% image ( +% addto currentpicture doublepath unitsquare +% withprescript "tx_number=" & decimal mfun_tt_n +% withprescript "tx_stage=trial" +% withprescript "tx_color=" & colordecimals colorpart mfun_tt_o +% withpostscript str +% ; ) +% else : +% mfun_tt_n := mfun_tt_n + 1 ; +% if known mfun_tt_d[mfun_tt_n] : +% image ( +% addto currentpicture doublepath unitsquare +% xscaled mfun_tt_w[mfun_tt_n] +% yscaled (mfun_tt_h[mfun_tt_n] + mfun_tt_d[mfun_tt_n]) +% withprescript "tx_number=" & decimal mfun_tt_n +% withprescript "tx_stage=final" +% % withpostscript str ; % for tracing +% ; ) shifted (0,-mfun_tt_d[mfun_tt_n]) +% else : +% image ( +% addto currentpicture doublepath unitsquare +% ; ) +% fi +% fi +% enddef ; + +% vardef rawtextext(expr str) = % todo: avoid currentpicture +% if str = "" : +% 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_p doublepath unitsquare +% withprescript "tx_number=" & decimal mfun_tt_n +% withprescript "tx_stage=extra" +% withpostscript str ; +% 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 ; +% 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 ; + +vardef rawtextext(expr str) = % todo: avoid currentpicture if str = "" : nullpicture - elseif mfun_trial_run : + else : mfun_tt_n := mfun_tt_n + 1 ; - addto mfun_tt_p doublepath unitsquare - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=extra" - withpostscript str ; - image ( - addto currentpicture doublepath unitsquare + 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" - withpostscript str - ) - else : - mfun_tt_n := mfun_tt_n + 1 ; - if known mfun_tt_d[mfun_tt_n] : - image ( - addto currentpicture doublepath unitsquare - xscaled mfun_tt_w[mfun_tt_n] - yscaled (mfun_tt_h[mfun_tt_n] + mfun_tt_d[mfun_tt_n]) - withprescript "tx_number=" & decimal mfun_tt_n - withprescript "tx_stage=final" - ; % withpostscript str ; - ) shifted (0,-mfun_tt_d[mfun_tt_n]) + 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 : - image ( - addto currentpicture doublepath unitsquare ; - ) - fi + addto mfun_tt_c doublepath unitsquare ; % unitpicture + fi ; + mfun_tt_c fi enddef ; @@ -255,10 +322,52 @@ mfun_labtype.drt := 12 ; mfun_labtype.origin := 0 ; mfun_labtype.raw := 0 ; +% installlabel.foo ( 0, 1, 1, (.5,-1) ) ; + +vardef installlabel@# (expr type, x, y, offset) = + numeric labtype@# ; labtype@# := type ; + pair laboff @# ; laboff @# := offset ; + numeric labxf @# ; labxf @# := x ; + numeric labyf @# ; labyf @# := y ; +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)) + fi +enddef; + +def plain_label = % takes two arguments, contrary to textext that takes one + draw plain_thelabel +enddef ; + +let mfun_label = label ; +let mfun_thelabel = thelabel ; + +def useplainlabels = % somehow let doesn't work for all code + def label = plain_label enddef ; + def thelabel = plain_thelabel enddef ; +enddef ; + +def usemetafunlabels = + let label = mfun_label ; + let thelabel = mfun_thelabel ; +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) : @@ -611,8 +720,8 @@ else : elseif colormodel p = 7 : withcolor (cyanpart p,magentapart p,yellowpart p,blackpart p) fi - withprescript prescript p - withpostscript postscript p + withprescript prescriptpart p + withpostscript postscriptpart p enddef ; fi ; @@ -662,3 +771,296 @@ enddef ; % draw textext.autoalign(160)("\strut oeps 2") ; % draw textext.autoalign(260)("\strut oeps 3") ; % draw textext.autoalign(360)("\strut oeps 4") ; + +% new +% +% passvariable("version","1.0") ; +% passvariable("number",123) ; +% passvariable("string","whatever") ; +% passvariable("point",(1,2)) ; +% passvariable("triplet",(1,2,3)) ; +% passvariable("quad",(1,2,3,4)) ; +% passvariable("boolean",false) ; +% passvariable("path",fullcircle scaled 1cm) ; + +vardef mfun_point_to_string(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_transform_to_string(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 +enddef ; + +vardef mfun_numeric_to_string(expr n) = + decimal n +enddef ; + +vardef mfun_pair_to_string(expr p) = + decimal xpart p & " " & + decimal ypart p +enddef ; + +vardef mfun_rgbcolor_to_string(expr c) = + decimal redpart c & " " & + decimal greenpart c & " " & + decimal bluepart c +enddef ; + +vardef mfun_cmykcolor_to_string(expr c) = + decimal cyanpart c & " " & + decimal magentapart c & " " & + decimal yellowpart c & " " & + decimal blackpart c +enddef ; + +vardef mfun_path_to_string(expr p) = + mfun_point_to_string(p,0) for i=1 upto length(p) : & " " & mfun_point_to_string(p,i) endfor +enddef ; + +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) + elseif rgbcolor value : mfun_rgbcolor_to_string(value) + elseif cmykcolor value : mfun_cmykcolor_to_string(value) + elseif boolean value : mfun_boolean_to_string(value) + elseif path value : mfun_path_to_string(value) + elseif transform value : mfun_transform_to_string(value) + else : value + fi +enddef ; + +vardef mfun_tagged_string(expr value) = + if numeric value : "1:" & mfun_numeric_to_string(value) + elseif pair value : "4:" & mfun_pair_to_string(value) + elseif rgbcolor value : "5:" & mfun_rgbcolor_to_string(value) + elseif cmykcolor value : "6:" & mfun_cmykcolor_to_string(value) + elseif boolean value : "3:" & mfun_boolean_to_string(value) + elseif path value : "7:" & mfun_path_to_string(value) + elseif transform value : "8:" & mfun_transform_to_string(value) + else : "2:" & value + fi +enddef ; + +% amore flexible variant for passing data to context + +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) + & "}" +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 + & "}" +enddef ; + +vardef mfun_numeric_to_lua(expr n) = + decimal n +enddef ; + +vardef mfun_pair_to_lua(expr p) = + "{" & + decimal xpart p & "," & + decimal ypart p + & "}" +enddef ; + +vardef mfun_rgbcolor_to_lua(expr c) = + "{" & + decimal redpart c & "," & + decimal greenpart c & "," & + decimal bluepart c + & "}" +enddef ; + +vardef mfun_cmykcolor_to_lua(expr c) = + "{" & + decimal cyanpart c & "," & + decimal magentapart c & "," & + decimal yellowpart c & "," & + decimal blackpart c + & "}" +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 + & "}" +enddef ; + +vardef mfun_boolean_to_lua(expr b) = + if b : "true" else : "false" fi +enddef ; + +vardef mfun_string_to_lua(expr s) = + "[==[" & s & "]==]" +enddef ; + +def mfun_to_lua(expr key)(expr value)(text t) = + special "metapost.variables['" & key & "']=" & t(value) ; +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 + & "}" ; +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) + 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 ; +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) +enddef ; + +def mfun_lua_slot(expr k, v) = + mfun_lua_index(k) + mfun_lua_value(v) +enddef ; + +% moved here from mp-grap.mpiv + +vardef escaped_format(expr s) = + "" for n=0 upto length(s) : & + if ASCII substring (n,n+1) of s = 37 : + "@" + else : + substring (n,n+1) of s + fi + endfor +enddef ; + +vardef strfmt(expr f, x) = % maybe use mfun_ namespace + "\MPgraphformat{" & escaped_format(f) & "}{" & mfun_tagged_string(x) & "}" +enddef ; + +vardef varfmt(expr f, x) = % maybe use mfun_ namespace + "\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 ; |