Add pretty boxes around various typing environments.
[matthijs/master-project/report.git] / Utils / Metapost.tex
1 % Some setup for metapost
2 \startMPinclusions 
3
4 % Use metaobj for drawing objects
5 input metaobj; 
6
7 % Set some defaults for pretty pictures
8 setObjectDefaultOption("Box")("framewidth")(.75mm);
9 setObjectDefaultOption("Circle")("framewidth")(.75mm);
10 setObjectDefaultOption("Circle")("circmargin")(3mm);
11 %ahlength := 1.5mm;
12 ahangle := 60;
13
14 setCurveDefaultOption("linewidth",.75mm);
15 % Add a nodesep that's equal to the linewidth, since the linewidth isn't
16 % included in the bounding box for line cutoff.
17 setCurveDefaultOption("nodesep",curve_linewidth_default);
18
19
20 % Define a register.
21 % |v| is either a picture, a string or an object given by its number which
22 % will be contained inside the register. Supports the same options as Box,
23 % plus:
24 %   reflect    Reflect the register vertically, so input is right and output
25 %              is left. (default: false)
26 %   labels     Show labels for the input and output ports. (default: false)
27 vardef newReg@#(expr v) text options=
28   ExecuteOptions(@#)(options);
29   assignObj(@#,"Reg");
30   StandardInterface;
31   
32   % We contain a box (that contains the passed object in turn)
33   SubObject(box,obj(newobjstring_));
34   newBox.obj(@#box)(v) options;
35  
36   % Some points for ports
37   ObjPoint ck,d,out;
38
39   ObjCode StandardEquations,
40     % Align our bounding box with the enclosed box
41     "obj(@#box).c = @#c",
42     "obj(@#box).se = @#se",
43     "obj(@#box).nw = @#nw",
44     % Put the clock, data and output ports at the right spots
45     if OptionValue@#("reflect"):
46       "xpart @#out = xpart @#w",
47       "xpart @#d = xpart @#ck = xpart @#e",
48     else:
49       "xpart @#out = xpart @#e",
50       "xpart @#d = xpart @#ck = xpart @#w",
51     fi
52     "ypart @#d = ypart @#out = ypart (@#sw * .25 + @#nw * .75)",
53     "ypart @#ck = ypart (@#sw * .75 + @#nw * .25)";
54
55   StandardTies;
56 enddef;
57
58 % Draw a register
59 def drawReg(suffix reg)=
60   save clkwh, refl;
61   % Draw the box
62   drawObj(obj(reg.box));
63
64   % Fill refl with 1 or -1, to invert coordinates when reflected.
65   if OptionValue.reg("reflect"):
66     refl = -1;
67   else:
68     refl = 1;
69   fi;
70
71   % Calculate (half of) the height and width of the triangle
72   clkwh = ypart((reg.n - reg.s)) * .1;
73   % Draw the "clock" triangle
74   draw ((0, clkwh) -- (refl * clkwh, 0) -- (0, -clkwh)) shifted reg.ck;
75
76   % Draw input and output labels
77   if OptionValue.reg("labels"):
78     if OptionValue.reg("reflect"):
79       label.lft(\sometxt{D}, reg.d);
80       label.rt(\sometxt{O}, reg.out);
81     else:
82       label.rt(\sometxt{D}, reg.d);
83       label.lft(\sometxt{O}, reg.out);
84     fi;
85   fi
86 enddef;
87
88 % Bounding path for a register
89 def BpathReg(suffix n)=BpathObj(obj(n.box)) enddef;
90
91 % Define options
92 define_global_boolean_option("reflect");
93 define_global_boolean_option("labels");
94 setObjectDefaultOption("Reg")("reflect")(false);
95 setObjectDefaultOption("Reg")("labels")(false);
96
97 \stopMPinclusions
98
99 % Make \overrightarrow "unexpanded", to make it work within metapost.
100 % http://www.ntg.nl/pipermail/ntg-context/2009/043620.html. Will be fixed in
101 % context.
102 \let\normaloverrightarrow\overrightarrow                                                                                                                     
103 \unexpanded\def\overrightarrow{\normaloverrightarrow}