summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/context/base/mp-mlib.mp
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/metapost/context/base/mp-mlib.mp')
-rw-r--r--Master/texmf-dist/metapost/context/base/mp-mlib.mp425
1 files changed, 338 insertions, 87 deletions
diff --git a/Master/texmf-dist/metapost/context/base/mp-mlib.mp b/Master/texmf-dist/metapost/context/base/mp-mlib.mp
index bf2372ca390..6c7115cb7d4 100644
--- a/Master/texmf-dist/metapost/context/base/mp-mlib.mp
+++ b/Master/texmf-dist/metapost/context/base/mp-mlib.mp
@@ -2,7 +2,7 @@
%D [ file=mp-mlib.mp,
%D version=2008.03.21,
%D title=\CONTEXT\ \METAPOST\ graphics,
-%D subtitle=specials,
+%D subtitle=plugins,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright={PRAGMA / Hans Hagen \& Ton Otten}]
@@ -16,6 +16,66 @@ if known context_mlib : endinput ; fi ;
boolean context_mlib ; context_mlib := true ;
+%D Color and transparency
+
+newinternal normaltransparent ; normaltransparent := 1 ;
+newinternal multiplytransparent ; multiplytransparent := 2 ;
+newinternal screentransparent ; screentransparent := 3 ;
+newinternal overlaytransparent ; overlaytransparent := 4 ;
+newinternal softlighttransparent ; softlighttransparent := 5 ;
+newinternal hardlighttransparent ; hardlighttransparent := 6 ;
+newinternal colordodgetransparent ; colordodgetransparent := 7 ;
+newinternal colorburntransparent ; colorburntransparent := 8 ;
+newinternal darkentransparent ; darkentransparent := 9 ;
+newinternal lightentransparent ; lightentransparent := 10 ;
+newinternal differencetransparent ; differencetransparent := 11 ;
+newinternal exclusiontransparent ; exclusiontransparent := 12 ;
+
+vardef transparency_alternative_to_number(expr name) =
+ if string name :
+ if expandafter known scantokens(name & "transparent") :
+ scantokens(name & "transparent")
+ else :
+ 0
+ fi
+ elseif name < 13 :
+ name
+ else :
+ 0
+ fi
+enddef ;
+
+def spotcolor(expr n, v) =
+ 1
+ withprescript "sp_name=" & n
+ withprescript "sp_value=" & v
+enddef ;
+
+def multitonecolor(expr name, fractions, components, value) =
+ 1
+ withprescript "sp_name=" & name
+ withprescript "sp_fractions=" & decimal fractions
+ withprescript "sp_components=" & components
+ withprescript "sp_value=" & value
+enddef ;
+
+def transparent(expr alternative, transparency)(text c) =
+ c
+ withprescript "tr_alternative=" & decimal transparency_alternative_to_number(alternative)
+ withprescript "tr_transparency=" & decimal transparency
+enddef ;
+
+def withtransparency(expr alternative, transparency) =
+ withprescript "tr_alternative=" & decimal transparency_alternative_to_number(alternative)
+ withprescript "tr_transparency=" & decimal transparency
+enddef ;
+
+def cmyk(expr c, m, y, k) =
+ (c,m,y,k)
+enddef ;
+
+% Texts
+
numeric _tt_w_[], _tt_h_[], _tt_d_[] ;
numeric _tt_n_ ; _tt_n_ := 0 ;
picture _tt_p_ ; _tt_p_ := nullpicture ;
@@ -26,34 +86,53 @@ def resettextexts =
_tt_p_ := nullpicture ;
enddef ;
-extra_endfig := ";addto currentpicture also _tt_p_; " & extra_endfig; % was draw _tt_p_
+def flushtextexts =
+ addto currentpicture also _tt_p_
+enddef ;
+
+extra_endfig := "flushtextexts;" & extra_endfig;
extra_beginfig := extra_beginfig & "resettextexts;";
+% We collect and flush them all, as we can also have temporary textexts
+% that gets never really flushed but are used for calculations. So, we
+% 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) =
if str = "" :
nullpicture
elseif _trial_run_ :
+ _tt_n_ := _tt_n_ + 1 ;
+ addto _tt_p_ doublepath unitsquare
+ withprescript "tx_number=" & decimal _tt_n_
+ withprescript "tx_stage=extra"
+ withpostscript str ;
image (
- _tt_n_ := _tt_n_ + 1 ;
- _tt_p_ := image (
- addto currentpicture also _tt_p_ ;
- addto currentpicture doublepath unitsquare withprescript "tf" withpostscript decimal _tt_n_ & ":" & str ;
- ) ;
- addto currentpicture doublepath unitsquare withpen pencircle scaled 0 ;
+ addto currentpicture doublepath unitsquare
+ withprescript "tx_number=" & decimal _tt_n_
+ withprescript "tx_stage=trial"
+ withpostscript str
)
else :
- image (
- _tt_n_ := _tt_n_ + 1 ;
- addto currentpicture doublepath unitsquare
- xscaled _tt_w_[_tt_n_]
- yscaled (_tt_h_[_tt_n_] + _tt_d_[_tt_n_])
- withprescript "ts"
- withpostscript decimal _tt_n_ & ":" & str ;
- ) shifted (0,-_tt_d_[_tt_n_])
+ _tt_n_ := _tt_n_ + 1 ;
+ if known _tt_d_[_tt_n_] :
+ image (
+ addto currentpicture doublepath unitsquare
+ xscaled _tt_w_[_tt_n_]
+ yscaled (_tt_h_[_tt_n_] + _tt_d_[_tt_n_])
+ withprescript "tx_number=" & decimal _tt_n_
+ withprescript "tx_stage=final"
+ ; % withpostscript str ;
+ ) shifted (0,-_tt_d_[_tt_n_])
+ else :
+ image (
+ addto currentpicture doublepath unitsquare ;
+ )
+ fi
fi
enddef ;
-% not ok yet
+% More text
pair laboff.d, laboff.dlft, laboff.drt ; % new positional suffixes
pair laboff.origin, laboff.raw ; % graph mess
@@ -89,44 +168,44 @@ pair laboff.t_r ; laboff.t_r = laboff.urt ;
pair laboff.b_l ; laboff.b_l = laboff.llft ;
pair laboff.b_r ; laboff.b_r = laboff.lrt ;
-labxf.l ; labxf.l = labxf.lft ;
-labxf.r ; labxf.r = labxf.rt ;
-labxf.b ; labxf.b = labxf.bot ;
-labxf.t ; labxf.t = labxf.top ;
-labxf.l_t ; labxf.l_t = labxf.ulft ;
-labxf.r_t ; labxf.r_t = labxf.urt ;
-labxf.l_b ; labxf.l_b = labxf.llft ;
-labxf.r_b ; labxf.r_b = labxf.lrt ;
-labxf.t_l ; labxf.t_l = labxf.ulft ;
-labxf.t_r ; labxf.t_r = labxf.urt ;
-labxf.b_l ; labxf.b_l = labxf.llft ;
-labxf.b_r ; labxf.b_r = labxf.lrt ;
-
-labyf.l ; labyf.l = labyf.lft ;
-labyf.r ; labyf.r = labyf.rt ;
-labyf.b ; labyf.b = labyf.bot ;
-labyf.t ; labyf.t = labyf.top ;
-labyf.l_t ; labyf.l_t = labyf.ulft ;
-labyf.r_t ; labyf.r_t = labyf.urt ;
-labyf.l_b ; labyf.l_b = labyf.llft ;
-labyf.r_b ; labyf.r_b = labyf.lrt ;
-labyf.t_l ; labyf.t_l = labyf.ulft ;
-labyf.t_r ; labyf.t_r = labyf.urt ;
-labyf.b_l ; labyf.b_l = labyf.llft ;
-labyf.b_r ; labyf.b_r = labyf.lrt ;
-
-labtype.l ; labtype.l = labtype.lft ;
-labtype.r ; labtype.r = labtype.rt ;
-labtype.b ; labtype.b = labtype.bot ;
-labtype.t ; labtype.t = labtype.top ;
-labtype.l_t ; labtype.l_t = labtype.ulft ;
-labtype.r_t ; labtype.r_t = labtype.urt ;
-labtype.l_b ; labtype.l_b = labtype.llft ;
-labtype.r_b ; labtype.r_b = labtype.lrt ;
-labtype.t_l ; labtype.t_l = labtype.ulft ;
-labtype.t_r ; labtype.t_r = labtype.urt ;
-labtype.b_l ; labtype.b_l = labtype.llft ;
-labtype.b_r ; labtype.b_r = labtype.lrt ;
+numeric labxf.l ; labxf.l = labxf.lft ;
+numeric labxf.r ; labxf.r = labxf.rt ;
+numeric labxf.b ; labxf.b = labxf.bot ;
+numeric labxf.t ; labxf.t = labxf.top ;
+numeric labxf.l_t ; labxf.l_t = labxf.ulft ;
+numeric labxf.r_t ; labxf.r_t = labxf.urt ;
+numeric labxf.l_b ; labxf.l_b = labxf.llft ;
+numeric labxf.r_b ; labxf.r_b = labxf.lrt ;
+numeric labxf.t_l ; labxf.t_l = labxf.ulft ;
+numeric labxf.t_r ; labxf.t_r = labxf.urt ;
+numeric labxf.b_l ; labxf.b_l = labxf.llft ;
+numeric labxf.b_r ; labxf.b_r = labxf.lrt ;
+
+numeric labyf.l ; labyf.l = labyf.lft ;
+numeric labyf.r ; labyf.r = labyf.rt ;
+numeric labyf.b ; labyf.b = labyf.bot ;
+numeric labyf.t ; labyf.t = labyf.top ;
+numeric labyf.l_t ; labyf.l_t = labyf.ulft ;
+numeric labyf.r_t ; labyf.r_t = labyf.urt ;
+numeric labyf.l_b ; labyf.l_b = labyf.llft ;
+numeric labyf.r_b ; labyf.r_b = labyf.lrt ;
+numeric labyf.t_l ; labyf.t_l = labyf.ulft ;
+numeric labyf.t_r ; labyf.t_r = labyf.urt ;
+numeric labyf.b_l ; labyf.b_l = labyf.llft ;
+numeric labyf.b_r ; labyf.b_r = labyf.lrt ;
+
+numeric labtype.l ; labtype.l = labtype.lft ;
+numeric labtype.r ; labtype.r = labtype.rt ;
+numeric labtype.b ; labtype.b = labtype.bot ;
+numeric labtype.t ; labtype.t = labtype.top ;
+numeric labtype.l_t ; labtype.l_t = labtype.ulft ;
+numeric labtype.r_t ; labtype.r_t = labtype.urt ;
+numeric labtype.l_b ; labtype.l_b = labtype.llft ;
+numeric labtype.r_b ; labtype.r_b = labtype.lrt ;
+numeric labtype.t_l ; labtype.t_l = labtype.ulft ;
+numeric labtype.t_r ; labtype.t_r = labtype.urt ;
+numeric labtype.b_l ; labtype.b_l = labtype.llft ;
+numeric labtype.b_r ; labtype.b_r = labtype.lrt ;
vardef thetextext@#(expr p,z) = % adapted copy of thelabel@
if string p :
@@ -175,6 +254,34 @@ primarydef str infont name = % very naughty !
fi
enddef ;
+% Shades
+
+newinternal shadefactor ; shadefactor := 1 ;
+pair shadeoffset ; shadeoffset := origin ;
+boolean trace_shades ; trace_shades := false ;
+
+def set_linear_vector (suffix a,b)(expr p,n) =
+ if (n=1) : a := llcorner p ; b := urcorner p ;
+ elseif (n=2) : a := lrcorner p ; b := ulcorner p ;
+ elseif (n=3) : a := urcorner p ; b := llcorner p ;
+ elseif (n=4) : a := ulcorner p ; b := lrcorner p ;
+ elseif (n=5) : a := .5[ulcorner p,llcorner p] ; b := .5[urcorner p,lrcorner p] ;
+ elseif (n=6) : a := .5[llcorner p,lrcorner p] ; b := .5[ulcorner p,urcorner p] ;
+ elseif (n=7) : a := .5[lrcorner p,urcorner p] ; b := .5[llcorner p,ulcorner p] ;
+ elseif (n=8) : a := .5[urcorner p,ulcorner p] ; b := .5[lrcorner p,llcorner p] ;
+ else : a := .5[ulcorner p,llcorner p] ; b := .5[urcorner p,lrcorner p] ;
+ fi ;
+enddef ;
+
+def set_circular_vector (suffix ab, r)(expr p,n) =
+ if (n=1) : ab := llcorner p ;
+ elseif (n=2) : ab := lrcorner p ;
+ elseif (n=3) : ab := urcorner p ;
+ elseif (n=4) : ab := ulcorner p ;
+ else : ab := center p ; r := .5r ;
+ fi ;
+enddef ;
+
def circular_shade (expr p, n, ca, cb) =
begingroup ;
save ab, r ; pair ab ; numeric r ;
@@ -186,6 +293,7 @@ def circular_shade (expr p, n, ca, cb) =
fi ;
endgroup ;
enddef ;
+
def linear_shade (expr p, n, ca, cb) =
begingroup ;
save a, b, sh ; pair a, b ;
@@ -196,46 +304,128 @@ def linear_shade (expr p, n, ca, cb) =
fi ;
endgroup ;
enddef ;
+
def withcircularshade (expr a, b, ra, rb, ca, cb) =
- withprescript
- "cs"
- withpostscript
- "0 1 " & decimal shadefactor & " " &
- colordecimals ca & " " & ddecimal (a shifted shadeoffset) & " " & decimal ra & " " &
- colordecimals cb & " " & ddecimal (b shifted shadeoffset) & " " & decimal rb
+ withprescript "sh_type=circular"
+ withprescript "sh_domain=0 1"
+ withprescript "sh_factor=" & decimal shadefactor
+ withprescript "sh_color_a=" & colordecimals ca
+ withprescript "sh_color_b=" & colordecimals cb
+ withprescript "sh_center_a=" & ddecimal (a shifted shadeoffset)
+ withprescript "sh_center_b=" & ddecimal (b shifted shadeoffset)
+ withprescript "sh_radius_a=" & decimal ra
+ withprescript "sh_radius_b=" & decimal rb
enddef ;
+
def withlinearshade (expr a, b, ca, cb) =
- withprescript
- "ls"
- withpostscript
- "0 1 " & decimal shadefactor & " " &
- colordecimals ca & " " & ddecimal (a shifted shadeoffset) & " " &
- colordecimals cb & " " & ddecimal (b shifted shadeoffset)
-enddef ;
-string _defined_cs_pre_[], _defined_cs_post_[] ; numeric _defined_cs_ ; _defined_cs_:= 0 ;
+ withprescript "sh_type=linear"
+ withprescript "sh_domain=0 1"
+ withprescript "sh_factor=" & decimal shadefactor
+ withprescript "sh_color_a=" & colordecimals ca
+ withprescript "sh_color_b=" & colordecimals cb
+ withprescript "sh_center_a=" & ddecimal (a shifted shadeoffset)
+ withprescript "sh_center_b=" & ddecimal (b shifted shadeoffset)
+enddef ;
+
+string _defined_cs_pre_[] ; numeric _defined_cs_ ; _defined_cs_:= 0 ;
+string prescript_separator ; prescript_separator := char(13) ;
+
vardef define_circular_shade (expr a, b, ra, rb, ca, cb) =
_defined_cs_ := _defined_cs_ + 1 ;
- _defined_cs_pre_ [_defined_cs_] := "cs" ;
- _defined_cs_post_[_defined_cs_] := "0 1 " & decimal shadefactor & " " &
- colordecimals ca & " " & ddecimal (a shifted shadeoffset) & " " & decimal ra & " " &
- colordecimals cb & " " & ddecimal (b shifted shadeoffset) & " " & decimal rb ;
+ _defined_cs_pre_ [_defined_cs_] := "sh_type=circular"
+ & prescript_separator & "sh_domain=0 1"
+ & prescript_separator & "sh_factor=" & decimal shadefactor
+ & prescript_separator & "sh_color_a=" & colordecimals ca
+ & prescript_separator & "sh_color_b=" & colordecimals cb
+ & prescript_separator & "sh_center_a=" & ddecimal (a shifted shadeoffset)
+ & prescript_separator & "sh_center_b=" & ddecimal (b shifted shadeoffset)
+ & prescript_separator & "sh_radius_a=" & decimal ra
+ & prescript_separator & "sh_radius_b=" & decimal rb
+ ;
_defined_cs_
enddef ;
+
vardef define_linear_shade (expr a, b, ca, cb) =
_defined_cs_ := _defined_cs_ + 1 ;
- _defined_cs_pre_ [_defined_cs_] := "ls" ;
- _defined_cs_post_[_defined_cs_] := "0 1 " & decimal shadefactor & " " &
- colordecimals ca & " " & ddecimal (a shifted shadeoffset) & " " &
- colordecimals cb & " " & ddecimal (b shifted shadeoffset) ;
+ _defined_cs_pre_ [_defined_cs_] := "sh_type=linear"
+ & prescript_separator & "sh_domain=0 1"
+ & prescript_separator & "sh_factor=" & decimal shadefactor
+ & prescript_separator & "sh_color_a=" & colordecimals ca
+ & prescript_separator & "sh_color_b=" & colordecimals cb
+ & prescript_separator & "sh_center_a=" & ddecimal (a shifted shadeoffset)
+ & prescript_separator & "sh_center_b=" & ddecimal (b shifted shadeoffset)
+ ;
_defined_cs_
enddef ;
+
primarydef p withshade sc =
- p withprescript _defined_cs_pre_[sc] withpostscript _defined_cs_post_[sc]
+ p withprescript _defined_cs_pre_[sc]
+enddef ;
+
+% vardef predefined_linear_shade (expr p, n, ca, cb) =
+% save a, b, sh ; pair a, b ;
+% set_linear_vector(a,b)(p,n) ;
+% define_linear_shade (a,b,ca,cb)
+% enddef ;
+%
+% vardef predefined_circular_shade (expr p, n, ca, cb) =
+% save ab, r ; pair ab ; numeric r ;
+% r := (xpart lrcorner p - xpart llcorner p) ++ (ypart urcorner p - ypart lrcorner p) ;
+% set_circular_vector(ab,r)(p,n) ;
+% define_circular_shade(ab,ab,0,r,ca,cb)
+% enddef ;
+
+% NEW EXPERIMENTAL CODE
+
+def withlinearshading (expr a, b) =
+ withprescript "sh_type=linear"
+ withprescript "sh_domain=0 1"
+ withprescript "sh_factor=" & decimal shadefactor
+ withprescript "sh_center_a=" & ddecimal (a shifted shadeoffset)
+ withprescript "sh_center_b=" & ddecimal (b shifted shadeoffset)
+enddef ;
+
+def withcircularshading (expr a, b, ra, rb) =
+ withprescript "sh_type=circular"
+ withprescript "sh_domain=0 1"
+ withprescript "sh_factor=" & decimal shadefactor
+ withprescript "sh_center_a=" & ddecimal (a shifted shadeoffset)
+ withprescript "sh_center_b=" & ddecimal (b shifted shadeoffset)
+ withprescript "sh_radius_a=" & decimal ra
+ withprescript "sh_radius_b=" & decimal rb
+enddef ;
+
+def withfromshadecolor expr t =
+ withprescript "sh_color=into"
+ withprescript "sh_color_a=" & colordecimals t
+enddef ;
+
+def withtoshadecolor expr t =
+ withprescript "sh_color=into"
+ withprescript "sh_color_b=" & colordecimals t
+enddef ;
+
+def withshading (expr how)(text rest) =
+ if how = "linear" :
+ withlinearshading(rest)
+ elseif how = "circular" :
+ withcircularshading(rest)
+ else :
+ % nothing
+ fi
enddef ;
-def shadecolor(expr sc) = % obsolete
- 1 withprescript _defined_cs_pre_[sc] withpostscript _defined_cs_post_[sc]
+
+primarydef a shadedinto b =
+ 1
+ withprescript "sh_color=into"
+ withprescript "sh_color_a=" & colordecimals a
+ withprescript "sh_color_b=" & colordecimals b
enddef ;
+% END OF NEW
+
+% Graphic text (we will move code here)
+
def graphictext primary t =
if _trial_run_ :
let dographictextindeed = nographictext ;
@@ -244,32 +434,80 @@ def graphictext primary t =
fi
dographictextindeed(t)
enddef ;
+
def dographictext (expr t) =
+ % withprescript "gt_stage=final"
begingroup ;
save figurepicture ; picture figurepicture ;
figurepicture := currentpicture ; currentpicture := nullpicture ;
currentgraphictext := currentgraphictext + 1 ;
dofinishgraphictext
enddef ;
+
def nographictext (expr t) text rest =
- draw unitsquare withprescript "gt" withpostscript t ;
+ draw unitsquare withprescript "gt_stage=trial" withpostscript t
enddef ;
-def savegraphictext (expr str) =
+
+% def savegraphictext (expr str) =
+% enddef ;
+
+% def erasegraphictextfile =
+% enddef ;
+
+% Layers
+
+def onlayer primary name =
+ withprescript "la_name=" & name
enddef ;
-def erasegraphictextfile =
+
+
+% Figures
+
+% def externalfigure primary filename =
+% doexternalfigure (filename)
+% enddef ;
+%
+% def doexternalfigure (expr filename) text transformation =
+% if true : % a bit incompatible esp scaled 1cm now scaled the natural size
+% draw rawtextext("\externalfigure[" & filename & "]") transformation ;
+% else :
+% draw unitsquare transformation withprescript "fg_name=" & filename ;
+% fi ;
+% enddef ;
+
+def withmask primary filename =
+ withprescript "fg_mask=" & filename
enddef ;
def externalfigure primary filename =
- doexternalfigure (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
enddef ;
-def doexternalfigure (expr filename) text transformation =
- draw unitsquare transformation withprescript "fg" withpostscript filename ;
+
+def figure primary filename =
+ rawtextext("\externalfigure[" & filename & "]")
enddef ;
+% Positions
+
def register (expr label, width, height, offset) =
- draw unitsquare xscaled width yscaled height shifted offset withprescript "ps" withpostscript label ;
+ image (
+ addto currentpicture doublepath unitsquare xscaled width yscaled height shifted offset
+ withprescript "ps_label=" & label ;
+ ) ; % no transformations
enddef ;
+% Housekeeping
+
extra_beginfig := extra_beginfig & "currentgraphictext := 0 ; " ;
extra_endfig := extra_endfig & "finishsavingdata ; " ;
extra_endfig := extra_endfig & "resettextexts ; " ;
@@ -277,6 +515,19 @@ extra_endfig := extra_endfig & "resettextexts ; " ;
boolean cmykcolors ; cmykcolors := true ;
boolean spotcolors ; spotcolors := true ;
+% Bonus
+
vardef verbatim(expr str) =
ditto & "\detokenize{" & str & "}" & ditto
enddef ;
+
+% New
+
+def bitmapimage(expr xresolution, yresolution, data) =
+ image (
+ addto currentpicture doublepath unitsquare
+ withprescript "bm_xresolution=" & decimal xresolution
+ withprescript "bm_yresolution=" & decimal yresolution
+ withpostscript data ;
+ )
+enddef ;