-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhogan.sty
240 lines (223 loc) · 7.7 KB
/
hogan.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
\ProvidesPackage{hogan}[2017/08/15 v0.1]
% Requirements
\RequirePackage{tikz}
\RequirePackage{xkeyval}
% constants
\def\@hogan@const@cbox{centerbox}
\def\@hogan@const@lbox{leftbox}
\def\@hogan@const@rbox{rightbox}
% Main envieonments' Options
\define@cmdkey{hogan}{cells}[3*3]{\@hogan@set@cells#1!}
\def\@hogan@set@cells#1*#2!{%
\def\@hogan@cells@x{#1}%
\def\@hogan@cells@y{#2}%
}
\define@cmdkey{hogan}{size}[1cm*1cm]{\@hogan@set@size#1!}
\def\@hogan@set@size#1*#2!{%
\def\@hogan@size@x{#1}%
\def\@hogan@size@y{#2}%
}
\presetkeys{hogan}{cells, size}{}
% Main environment
\newenvironment{hogan}[1][]{%
% Execute Options
\setkeys{hogan}{#1}%
\begin{tikzpicture}[x=\@hogan@size@x,y=\@hogan@size@y]
\useasboundingbox (0,0) rectangle (\@hogan@cells@x,\@hogan@cells@y);
}{\end{tikzpicture}}
% Lines
\newcount\@hogan@linecount
\def\lines{%
% initialize
\@hogan@linecount=\@hogan@cells@y\relax % cell counter
\def\@hogan@stack@cell@size{}% cell size calculation
\def\@hogan@stack@cell@text{}% cell text stack
% command preparation. See vertical lines definition
\edef\c{\@hogan@const@cbox}% center
\edef\l{\@hogan@const@lbox}% left
\edef\r{\@hogan@const@rbox}% right
% start execution
\@hogan@hline{0}}
\def\endlines{%
\@hogan@stack@cell@size
\@hogan@stack@cell@text
}
% #1 -> cell counter, #2 -> gobble, #3 -> pattern or overread
\def\@hogan@hline#1#2+#3{%
\ifnum#1=\@hogan@cells@x\relax
\def\@hogan@hline@next{%
\advance\@hogan@linecount-1\relax
\@hogan@vline{0}#3%
}%
\else
\csname @hogan@hline@next@#3\endcsname{#1}%
\fi
\@hogan@hline@next
}
% Go to next column
\def\@hogan@hline@gobble#1#2{%
\if#2+
\def\@hogan@hline@next{\@hogan@hline{#1}+}%
\else
\def\@hogan@hline@next{\@hogan@hline@gobble{#1}}%
\fi
\@hogan@hline@next
}
% #1 -> cell counter, #2 -> pattern or overread
\def\@hogan@vline#1#2{%
\ifnum\@hogan@linecount<0
\def\@hogan@vline@next{#2}%
\else\ifnum#1>\@hogan@cells@x\relax
\def\@hogan@vline@next{\@hogan@hline{0}#2}%
\else
\csname @hogan@vline@next@#2\endcsname{#1}%
\fi\fi
\@hogan@vline@next
}
% hline and vline normal definitions
%% definition command
\def\hoganDefLine#1#2#3{% #1 -> horizontal letter, #2 -> vertical letter, #3 -> options of draw
% horizontal
\expandafter\def\csname @hogan@hline@next@#1\endcsname##1{%
\@hogan@h@common{##1}%
\draw[#3] (##1, \@hogan@linecount) -- (\the\numexpr##1+1, \@hogan@linecount);
}%
% vertical
\expandafter\def\csname @hogan@vline@next@#2\endcsname##1{%
\@hogan@v@common{##1}%
\draw[#3] (##1, \the\numexpr\@hogan@linecount+1) -- (##1, \@hogan@linecount);
}%
}
%% common part
\def\@hogan@h@common#1{%
\edef\@hogan@hline@next{\noexpand\@hogan@hline@gobble{\the\numexpr#1+1}}%
}
\def\@hogan@v@common#1{%
\edef\@hogan@vline@next{\noexpand\@hogan@vline{\the\numexpr#1+1}}%
% default cell size
\edef\@hogan@tmp@y{\the\numexpr\@hogan@cells@y-\@hogan@linecount-1}%
\expandafter\def\csname @hogan@h@#1@\@hogan@tmp@y\endcsname{1}%
\expandafter\def\csname @hogan@v@#1@\@hogan@tmp@y\endcsname{1}%
}
%% concrete definition
\hoganDefLine{-}{|}{red} % normal line
\hoganDefLine{*}{*}{very thick,red} % bold line
\hoganDefLine{"}{:}{dash pattern=on 0.25mm off 0.25mm, red} % dotted (TODO: pattern)
% special definition util
\def\@hogan@add@stack@cell@size#1#2#3#4#5{% #1 -> h or v, (#2,#3) -> target, (#4,#5) -> referenced
\edef\@hogan@target@x{#2}%
\edef\@hogan@target@y{#3}%
\edef\@hogan@ref@x{#4}%
\edef\@hogan@ref@y{#5}%
\edef\@hogan@tmp{%
\edef
\unexpanded\expandafter{%
\csname @hogan@#1@\@hogan@target@x @\@hogan@target@y\endcsname}{%
\noexpand\the\noexpand\numexpr
\unexpanded\expandafter{%
\csname @hogan@#1@\@hogan@ref@x @\@hogan@ref@y\endcsname}%
+1}}%
\edef\@hogan@stack@cell@size{%
\unexpanded\expandafter{\@hogan@tmp}%
\unexpanded\expandafter{\@hogan@stack@cell@size}}%
}
% hline special definitions
\expandafter\def\csname @hogan@hline@next@+\endcsname#1{%
\edef\@hogan@hline@next{\noexpand\@hogan@hline@gobble{\the\numexpr#1+1}+}%
}
\expandafter\def\csname @hogan@hline@next@&\endcsname#1{%
\@hogan@h@common{#1}%
% caluclate cell size
\edef\@hogan@tmp@y{\the\numexpr\@hogan@cells@y-\@hogan@linecount}%
\@hogan@add@stack@cell@size{v}{#1}{\the\numexpr\@hogan@tmp@y-1}{#1}{\@hogan@tmp@y}%
}
% vline special definitions
\expandafter\def\csname @hogan@vline@next@.\endcsname#1{%
\@hogan@v@common{#1}%
}
\expandafter\def\csname @hogan@vline@next@&\endcsname#1{%
\@hogan@v@common{#1}%
% caluclate cell size
\edef\@hogan@tmp@y{\the\numexpr\@hogan@cells@y-\@hogan@linecount-1}%
\@hogan@add@stack@cell@size{h}{\the\numexpr#1-1}{\@hogan@tmp@y}{#1}{\@hogan@tmp@y}%
}
%% textboxes
%%% common
\def\@hogan@add@stack@cell@text#1#2#3#4{% #1 -> text command, (#2,#3) -> target cell, #4 -> text
\edef\@hogan@target@x{#2}%
\edef\@hogan@target@y{#3}%
\edef\@hogan@tmp{%
\noexpand#1[at={(\@hogan@target@x,\@hogan@target@y)},%
align=center,% TODO
box=\unexpanded\expandafter{\csname @hogan@h@\@hogan@target@x @\@hogan@target@y\endcsname}%
*\unexpanded\expandafter{\csname @hogan@v@\@hogan@target@x @\@hogan@target@y\endcsname}]{%
\unexpanded{#4}}%
}%
\edef\@hogan@stack@cell@text{%
\unexpanded\expandafter{\@hogan@tmp}%
\unexpanded\expandafter{\@hogan@stack@cell@text}}%
}
\def\@hogan@repeat@span#1{% #1 -> num
\ifnum#1>1 &\expandafter\@hogan@repeat@span\expandafter{\the\numexpr#1-1}\fi
}
%%% center
\expandafter\def\csname @hogan@vline@next@\@hogan@const@cbox\endcsname#1{%
\def\@hogan@vline@next{\@hogan@create@cbox{#1}}%
}
\def\@hogan@create@cbox#1{%
\@ifnextchar[{\@hogan@create@cbox@{#1}}{\@hogan@create@cbox@{#1}[1]}%
}
\def\@hogan@create@cbox@#1[#2]#3{% #1 -> cell count, #2 -> num of span cells, #3 -> text
% cell stack
\@hogan@add@stack@cell@text\ctextbox{\the\numexpr#1-1}{\the\numexpr\@hogan@cells@y-\@hogan@linecount-1}{#3}%
% TODO span cells
\edef\@hogan@vline@next{\noexpand\@hogan@vline{#1}\@hogan@repeat@span{#2}}%
\@hogan@vline@next
}
% Text box commands' Options
\define@cmdkey{textbox}{at}[(0,0)]{\@hogan@tbox@set@at#1}
\def\@hogan@tbox@set@at(#1,#2){%
\def\@hogan@tbox@x{#1}%
\def\@hogan@tbox@y{#2}%
}
\define@cmdkey{textbox}{box}[1*1]{\@hogan@tbox@set@box#1!}
\def\@hogan@tbox@set@box#1*#2!{%
\edef\@hogan@tbox@w{\the\dimexpr\@hogan@size@x * #1}%
\edef\@hogan@tbox@ww{#1}%
\edef\@hogan@tbox@h{\the\dimexpr\@hogan@size@y * #2}%
\edef\@hogan@tbox@hh{#2}%
}
\define@cmdkey{textbox}[@hogan@tbox@]{align}[left]{}
\presetkeys{textbox}{at, box, align}{}
% Text box
\newcommand\ctextbox[2][]{%
% Execute options
\setkeys{textbox}{#1}%
\node[
align=\@hogan@tbox@align,
anchor=north west,
minimum width=\@hogan@tbox@w,
minimum height=\@hogan@tbox@h]
at (\@hogan@tbox@x, \the\numexpr\@hogan@cells@y-\@hogan@tbox@y)
{#2};
}
\newcommand\ltextbox[2][]{%
% Execute options
\setkeys{textbox}{#1}%
\node[
align=\@hogan@tbox@align,
anchor=north west,
minimum height=\@hogan@tbox@h]
at (\@hogan@tbox@x, \the\numexpr\@hogan@cells@y-\@hogan@tbox@y)
{#2};
}
\newcommand\rtextbox[2][]{%
% Execute options
\setkeys{textbox}{#1}%
\node[
align=\@hogan@tbox@align,
anchor=north east,
minimum height=\@hogan@tbox@h]
at (\the\numexpr\@hogan@tbox@x+\@hogan@tbox@ww, \the\numexpr\@hogan@cells@y-\@hogan@tbox@y)
{#2};
}