summaryrefslogtreecommitdiff
path: root/metapost
diff options
context:
space:
mode:
Diffstat (limited to 'metapost')
-rw-r--r--metapost/context/base/metafun.mp4
-rw-r--r--metapost/context/base/mp-grph.mp33
-rw-r--r--metapost/context/base/mp-mlib.mp130
-rw-r--r--metapost/context/base/mp-spec.mp3
-rw-r--r--metapost/context/base/mp-tool.mp127
5 files changed, 252 insertions, 45 deletions
diff --git a/metapost/context/base/metafun.mp b/metapost/context/base/metafun.mp
index 98ea1980f..b828b8b9e 100644
--- a/metapost/context/base/metafun.mp
+++ b/metapost/context/base/metafun.mp
@@ -35,7 +35,7 @@ else :
fi ;
input mp-tool.mp ;
-input mp-spec.mp ;
+input mp-spec.mp ; % will be skipped in mkiv, some day
input mp-core.mp ;
input mp-page.mp ;
input mp-text.mp ;
@@ -47,6 +47,8 @@ input mp-step.mp ;
input mp-grph.mp ;
input mp-figs.mp ;
+input mp-mlib.mp ;
+
% mp-form.mp ;
input mp-grid.mp ;
input mp-func.mp ;
diff --git a/metapost/context/base/mp-grph.mp b/metapost/context/base/mp-grph.mp
index 18c43c57b..e2e7e7529 100644
--- a/metapost/context/base/mp-grph.mp
+++ b/metapost/context/base/mp-grph.mp
@@ -56,23 +56,24 @@ string graphictextformat ; graphictextformat := "plain" ;
string graphictextstring ; graphictextstring := "" ;
string graphictextfile ; graphictextfile := "dummy.mpo" ;
-def savegraphictext (expr str) =
- graphictextfile := jobname & ".mpo" ;
- if (graphictextstring<>"") :
- write graphictextstring to graphictextfile ;
- graphictextstring := "" ;
- fi ;
- write str to graphictextfile ;
- let erasegraphictextfile = relax ;
-enddef ;
+def data_mpo_file = job_name & "-mp.mpo" enddef ;
+def data_mpy_file = job_name & "-mp.mpy" enddef ;
-def erasegraphictextfile =
- graphictextfile := jobname & ".mpo" ;
- write EOF to graphictextfile ;
- let erasegraphictextfile = relax ;
-enddef ;
+% def savegraphictext (expr str) =
+% if (graphictextstring<>"") :
+% write graphictextstring to data_mpo_file ;
+% graphictextstring := "" ;
+% fi ;
+% write str to data_mpo_file ;
+% let erasegraphictextfile = relax ;
+% enddef ;
+
+% def erasegraphictextfile =
+% write EOF to data_mpo_file ;
+% let erasegraphictextfile = relax ;
+% enddef ;
-extra_beginfig := extra_beginfig & " erasegraphictextfile ;" ;
+% extra_beginfig := extra_beginfig & " erasegraphictextfile ;" ;
def begingraphictextfig (expr n) =
foundpicture := n ; scratchpicture := nullpicture ;
@@ -213,7 +214,7 @@ def dofinishgraphictext text x_op_x =
if (urcorner dashpart i) = origin : outline_fill := false ; fi ;
endfor ;
scratchpicture := nullpicture ;
- readfile(jobname & ".mpy") ;
+ readfile(data_mpy_file) ;
scratchpicture := (scratchpicture shifted -llcorner scratchpicture) scaled (1/10) ;
if not d_color and not f_color : d_color := true ; fi
if s_color : d_color := false ; f_color := false ; fi ;
diff --git a/metapost/context/base/mp-mlib.mp b/metapost/context/base/mp-mlib.mp
new file mode 100644
index 000000000..4ebe576e6
--- /dev/null
+++ b/metapost/context/base/mp-mlib.mp
@@ -0,0 +1,130 @@
+
+if unknown mplib : endinput ; fi ;
+if known context_mlib : endinput ; fi ;
+
+boolean context_mlib ; context_mlib := true ;
+
+numeric _tt_w_[], _tt_h_[], _tt_d_[] ;
+numeric _tt_n_ ; _tt_n_ := 0 ;
+boolean _trial_run_ ; _trial_run_ := false ;
+
+vardef textext(expr str) =
+ if _trial_run_ :
+ image (
+ draw unitsquare
+ withprescript "tf"
+ withpostscript str ;
+ )
+ else :
+ image (
+ _tt_n_ := _tt_n_ + 1 ;
+ draw unitsquare
+ xscaled _tt_w_[_tt_n_]
+ yscaled (_tt_h_[_tt_n_] + _tt_d_[_tt_n_])
+ withprescript "ts"
+ withpostscript decimal _tt_n_ ;
+ )
+ fi
+enddef ;
+
+def circular_shade (expr p, n, ca, cb) =
+ begingroup ;
+ 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) ;
+ fill p withcircularshade(ab,ab,0,r,ca,cb) ;
+ if trace_shades :
+ drawarrow ab -- ab shifted (0,r) withpen pencircle scaled 1pt withcolor .5white ;
+ fi ;
+ endgroup ;
+enddef ;
+def linear_shade (expr p, n, ca, cb) =
+ begingroup ;
+ save a, b, sh ; pair a, b ;
+ set_linear_vector(a,b)(p,n) ;
+ fill p withlinearshade(a,b,ca,cb) ;
+ if trace_shades :
+ drawarrow a -- b withpen pencircle scaled 1pt withcolor .5white ;
+ 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
+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 ;
+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_
+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_
+enddef ;
+def withshade(expr sc) =
+ withprescript _defined_cs_pre_[sc] withpostscript _defined_cs_post_[sc]
+enddef ;
+def shadecolor(expr sc) = % obsolete
+ 1 withprescript _defined_cs_pre_[sc] withpostscript _defined_cs_post_[sc]
+enddef ;
+
+% def _graphic_text_f_(expr t) text rest =
+% draw unitsquare withprescript "gt" withpostscript t ;
+% enddef ;
+% def _graphic_text_s_(expr t) =
+% begingroup ;
+% save figurepicture ; picture figurepicture ;
+% figurepicture := currentpicture ; currentpicture := nullpicture ;
+% currentgraphictext := currentgraphictext + 1 ;
+% dofinishgraphictext
+% enddef ;
+
+vardef graphictext primary t =
+ save next ;
+ if _trial_run_ :
+ let next = nographictext ;
+ else :
+ let next = dographictext ;
+ fi
+ next(t)
+enddef ;
+def dographictext (expr t) =
+ 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 ;
+enddef ;
+
+
+def doexternalfigure (expr filename) text transformation =
+ draw unitsquare transformation withprescript "fg" withpostscript filename ;
+enddef ;
+
+extra_beginfig := extra_beginfig & "currentgraphictext := 0 ; " ;
+
+boolean cmykcolors ; cmykcolors := true ;
+boolean spotcolors ; spotcolors := true ;
diff --git a/metapost/context/base/mp-spec.mp b/metapost/context/base/mp-spec.mp
index 351f9fe1c..f28e5a5d3 100644
--- a/metapost/context/base/mp-spec.mp
+++ b/metapost/context/base/mp-spec.mp
@@ -495,7 +495,8 @@ vardef cmyk(expr c,m,y,k) =
ok := true ; % globally already defined
fi ;
if not ok :
- save s ; string s ; s := dddecimal (c,m,y) & " " & decimal k ;
+% save s ; string s ; s := dddecimal (c,m,y) & " " & decimal k ;
+ save s ; string s ; s := ddddecimal (c,m,y,k) ;
_cmyk_counter_ := _cmyk_counter_ + 1 ;
cmykcolorpattern[_cmyk_counter_/_special_div_] := s ;
cmykcolorhash[c][m][y][k] := _cmyk_counter_ ;
diff --git a/metapost/context/base/mp-tool.mp b/metapost/context/base/mp-tool.mp
index b8e2dd668..24f2e6676 100644
--- a/metapost/context/base/mp-tool.mp
+++ b/metapost/context/base/mp-tool.mp
@@ -102,16 +102,59 @@ vardef ddecimal primary p =
decimal xpart p & " " & decimal ypart p
enddef ;
-extra_endfig := extra_endfig
- & "special "
- & "("
- & ditto
- & "%%HiResBoundingBox: "
- & ditto
- & "&ddecimal llcorner currentpicture"
- & "&space"
- & "&ddecimal urcorner currentpicture"
- & ");";
+% is now built in
+
+% extra_endfig := extra_endfig
+% & "special "
+% & "("
+% & ditto
+% & "%%HiResBoundingBox: "
+% & ditto
+% & "&ddecimal llcorner currentpicture"
+% & "&space"
+% & "&ddecimal urcorner currentpicture"
+% & ");";
+
+%D Colors:
+
+nocolormodel := 1 ;
+greycolormodel := 3 ;
+rgbcolormodel := 5 ;
+cmykcolormodel := 7 ;
+
+let grayscale = numeric ;
+
+% def colorlike(expr c) text v = % colorlike(a) b, c, d ;
+% forsuffixes i=v : % save i ;
+% if cmykcolor c :
+% cmykcolor i ;
+% elseif rgbcolor c :
+% rgbcolor i ;
+% else :
+% grayscale i ;
+% fi ;
+% endfor ;
+% enddef ;
+
+vardef colorlike(text c) text v = % colorlike(a) b, c, d ;
+ save _p_ ; picture _p_ ;
+ forsuffixes i=v :
+ _p_ := image(draw origin withcolor c ;) ; % intercept pre and postscripts
+ if (colormodel _p_ = cmykcolormodel) :
+ cmykcolor i ;
+ elseif (colormodel _p_ = rgbcolormodel) :
+ rgbcolor i ;
+ else :
+ grayscale i ;
+ fi ;
+ endfor ;
+enddef ;
+
+% if (unknown colormodel) :
+% def colormodel =
+% rgbcolormodel
+% enddef ;
+% fi ;
%D Also handy (when we flush colors):
@@ -124,7 +167,7 @@ vardef ddddecimal primary c =
enddef ;
vardef colordecimals primary c =
- if cmykcolor 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
@@ -135,15 +178,12 @@ enddef ;
%D We have standardized data file names:
-if not known _data_prefix_ :
-
- string _data_prefix_ ; _data_prefix_ = "mpd-" ;
- string _data_suffix_ ; _data_suffix_ = ".tmp" ;
-
-fi ;
+def job_name =
+ jobname
+enddef ;
-def data_file =
- _data_prefix_ & decimal charcode & _data_suffix_
+def data_mpd_file =
+ job_name & "-mp.mpd"
enddef ;
%D Because \METAPOST\ has a hard coded limit of 4~datafiles,
@@ -157,12 +197,12 @@ boolean savingdata ; savingdata := false ;
def savedata expr txt =
if collapse_data :
+ write txt to data_mpd_file ;
+ else :
write if savingdata : txt else :
"\MPdata{" & decimal charcode & "}{" & txt & "}"
fi
- & "%" to jobname & _data_suffix_ ;
- else :
- write txt to data_file ;
+ & "%" to data_mpd_file ;
fi ;
enddef ;
@@ -172,15 +212,19 @@ def startsavingdata =
write
"\MPdata{" & decimal charcode & "}{%"
to
- jobname & _data_suffix_ ;
+ data_mpd_file ;
fi ;
enddef ;
def stopsavingdata =
- savingdata := false ;
if collapse_data :
- write "}%" to jobname & _data_suffix_ ;
+ write "}%" to data_mpd_file ;
fi ;
+ savingdata := false ;
+enddef ;
+
+def finishsavingdata =
+ write EOF to data_mpd_file ;
enddef ;
%D Instead of a keystroke eating save and allocation
@@ -664,6 +708,16 @@ color cyan ; cyan = (0,1,1) ;
color magenta ; magenta = (1,0,1) ;
color yellow ; yellow = (1,1,0) ;
+def colortype(expr c) =
+ if cmykcolor c : cmykcolor elseif rgbcolor c : rgbcolor else : grayscale fi
+enddef ;
+vardef whitecolor(expr c) =
+ if cmykcolor c : (0,0,0,0) elseif rgbcolor c : (1,1,1) else : 1 fi
+enddef ;
+vardef blackcolor(expr c) =
+ if cmykcolor c : (0,0,0,1) elseif rgbcolor c : (0,0,0) else : 0 fi
+enddef ;
+
%D Well, this is the dangerous and naive version:
def drawfill text t =
@@ -833,7 +887,18 @@ primarydef p randomized s =
fi
elseif pair p :
p randomshifted s
- elseif color p :
+ elseif cmykcolor p :
+ if color s :
+ (uniformdeviate cyanpart s * cyanpart p,
+ uniformdeviate magentapart s * magentapart p,
+ uniformdeviate yellowpart s * yellowpart p,
+ uniformdeviate blackpart s * blackpart p)
+ elseif pair s :
+ ((xpart s + uniformdeviate (ypart s - xpart s)) * p)
+ else :
+ (uniformdeviate s * p)
+ fi
+ elseif rgbcolor p :
if color s :
(uniformdeviate redpart s * redpart p,
uniformdeviate greenpart s * greenpart p,
@@ -843,6 +908,14 @@ primarydef p randomized s =
else :
(uniformdeviate s * p)
fi
+ elseif color p :
+ if color s :
+ (uniformdeviate graypart s * graypart p)
+ elseif pair s :
+ ((xpart s + uniformdeviate (ypart s - xpart s)) * p)
+ else :
+ (uniformdeviate s * p)
+ fi
else :
p + uniformdeviate s
fi)
@@ -1152,7 +1225,7 @@ def visualizedfill expr c =
enddef ;
def do_visualizeddraw text t =
- draworigin ;
+ draworigin ;
drawpath _c_ t ;
drawcontrollines _c_ ;
drawcontrolpoints _c_ ;