]> git.donarmstrong.com Git - lilypond.git/blob - tex/lily-pdf-defs.tex
patch::: 1.3.139.jcn6
[lilypond.git] / tex / lily-pdf-defs.tex
1 % A Native PDF version of lily-ps-defs.tex, in which the language
2 % features of the PS code are handled by TeX.  This takes the place of
3 % lilyponddefs.ps, lily.ps, and lily-ps-defs.tex for PDFTeX.
4 %
5 % Note that this file will probably require changes if the lily.ps
6 % file changes, which is annoying in the long run.  It might be best
7 % if sometime the intelligence embodied in lily.ps could be moved up
8 % to the GUILE level, so that the \embeddedps commands could consist
9 % simply of moveto, lineto, curveto, fill, and stroke commands, with
10 % numeric arguments.  Such a setup would allow this file to be simpler
11 % and probably cause the resulting PostScript code to be faster as
12 % well.
13
14 % Redefine @ and _ so we can use them in definition names here.
15 \catcode`\@=11
16 \catcode`\_=11
17
18 % Define a helper procedure for PDF coding.  This file really
19 % shouldn't be read if \pdfliteral is undefined, but the alternate
20 % definition is nice for testing.
21
22 \ifx\pdfliteral\undefined
23   \def\LYPDF#1{\message{[ignored pdfliteral: #1]}}
24 \else
25   \let\LYPDF=\pdfliteral
26 \fi
27
28 % Strip 'pt' off of dimensions.  Borrowed from latex.
29 \begingroup
30   \catcode`P=12
31   \catcode`T=12
32   \lowercase{
33      \def\x{\def\lypdf@rempt##1.##2PT{##1\ifnum##2>\z@.##2\fi}}}
34   \expandafter\endgroup\x
35 \def\lypdf@strippt{\expandafter\lypdf@rempt\the}
36
37 \def\LYDIM#1{\expandafter\lypdf@strippt\dimen#1\space}
38
39 \def\lypdf@correctfactor{65536}
40 \def\lypdf@divcorrect#1{\multiply\dimen#1 \lypdf@correctfactor\relax}
41
42 %% Stack handling.  The design for this is borrowed from supp-pdf.tex
43
44 \newcount\nofLYPDFargs
45 \def\@@LYPDF{@@LYPDF}
46
47 % Add an argument to the `stack'
48 \def\setLYPDFarg#1{
49   \advance\nofLYPDFargs by 1
50   \expandafter\def
51     \csname\@@LYPDF\the\nofLYPDFargs\endcsname
52     {#1}
53 }
54
55 % Get the values for stack variables.  The a form includes a closing
56 % \space and is thus useful for embedding in \LYPDF macros.
57 \def\gLYPDFa#1
58   {\csname\@@LYPDF#1\endcsname\space}
59 \def\gLYPDFan#1
60   {\csname\@@LYPDF#1\endcsname}
61
62 % Reset the stack back to normal.
63 \def\resetLYPDFstack{\nofLYPDFargs=0}
64
65 % A translator for \embeddedps commands.  This simply stacks up the
66 % arguments and then passes the last arg to the appropriate lypdf@name
67 % macro.
68
69 \def\embeddedps#1{  
70   \lypdf@handleArgs#1 \\}
71
72 %% Handle the argument list.  Note: when working with arrays, just
73 %% keep tacking things onto a string until we get a close bracket.
74 %% The various LYPDFarray... variables are used for that.
75 \newif\ifLYPDFarray
76 \def\LYPDFarraystart{[}
77 \def\LYPDFarrayend{]}
78 \def\LYPDFarraystring{}
79
80 \def\lypdf@{lypdf@}
81 \def\lypdf@handleArgs#1 #2\\{
82   \ifx\\#2\\% 
83     \csname\lypdf@#1\endcsname
84     \resetLYPDFstack
85   \else
86     \edef\argstring{#1}
87     \ifLYPDFarray%
88       \edef\LYPDFarraystring{\LYPDFarraystring\space\argstring}
89       \ifx\argstring\LYPDFarrayend%
90         \LYPDFarrayfalse
91         \setLYPDFarg{\LYPDFarraystring}
92       \fi
93     \else
94       \ifx\argstring\LYPDFarraystart%
95         \LYPDFarraytrue
96         \edef\LYPDFarraystring{[}
97       \else
98         \setLYPDFarg{#1}
99       \fi
100     \fi
101     \lypdf@handleArgs#2\\
102   \fi}
103
104 % Here turning on PostScript sets up the bracket stuff.  This should
105 % probably be called by a more generic header macro.
106 \def\turnOnPostScript{\lypdf@load_bracket_dimens}%
107 \def\turnOnExperimentalFeatures{}%
108
109 %% TODO: lily-ps-defs sets a linecap of 1.  I'm not yet sure how to do that 
110 %% for the Page Description level in PDFTeX.
111
112 %% What follows are the definitions for the embeddedps commands.
113 %% Notes that in general, \dimen0 and \dimen1 are the x and y
114 %% positions of the cursor (used for rlineto handling), and dimen2-9
115 %% are used for local dimension handling in the various commands.
116
117 \def\lypdf@resetstring{\edef\lypdf@curstring{}}
118
119 \def\lypdf@moveto#1#2{
120   \dimen0=#1pt
121   \dimen1=#2pt
122   \edef\lypdf@curstring{\lypdf@curstring\space\LYDIM0 \LYDIM1 m}
123 }
124
125 \def\lypdf@rmoveto#1#2{
126   \advance\dimen0 by #1 pt
127   \advance\dimen1 by #2 pt
128   \edef\lypdf@curstring{\lypdf@curstring\space\LYDIM0 \LYDIM1 m}
129 }
130
131 \def\lypdf@rlineto#1#2{
132   \advance\dimen0 by #1 pt
133   \advance\dimen1 by #2 pt
134   \edef\lypdf@curstring{\lypdf@curstring\space\LYDIM0 \LYDIM1 l}
135 }
136
137 \def\lypdf@draw_beam{% takes width, slope, thick
138   \dimen2=\gLYPDFa3 pt\divide\dimen2 by 2
139   \dimen3=\gLYPDFa1 pt\dimen3=\gLYPDFa2 \dimen3
140   \lypdf@resetstring
141   \lypdf@moveto{0}{-\LYDIM2}
142   \lypdf@rlineto{\gLYPDFa1}{\LYDIM3}
143   \lypdf@rlineto{0}{\gLYPDFa3}
144   \LYPDF{\lypdf@curstring\space 0 \LYDIM2 l b}
145 }
146        
147 \def\lypdf@draw_decrescendo{% takes width, ht, cont, thick
148   \LYPDF{\gLYPDFa4 w
149          \gLYPDFa1 \gLYPDFa3 m 0 \gLYPDFa2 l S 
150          \gLYPDFa1 -\gLYPDFa3 m 0 -\gLYPDFa2 l S}
151 }
152 \def\lypdf@draw_crescendo{% takes width, ht, cont, thick
153   \LYPDF{\gLYPDFa4 w
154          0 \gLYPDFa3 m \gLYPDFa1 \gLYPDFa2 l S -\gLYPDFa3 m 
155          \gLYPDFa1 -\gLYPDFa2 l S}
156 }
157
158 \def\lypdf@draw_tuplet{% takes height, gap, dx, dy, thickness, dir
159   \dimen2=\gLYPDFa1 pt\multiply\dimen2 by \gLYPDFa6 \relax   
160                                         % height*dir
161   \dimen3=\gLYPDFa2 pt                  % tuplet_gapx
162   \dimen0=\gLYPDFa3 pt
163   \dimen4=\gLYPDFa4 \dimen3 \divide\dimen4 by \dimen0
164       \lypdf@divcorrect4                % tuplet_gapy
165   \dimen5=\gLYPDFa3 pt \advance\dimen5 by-\dimen3
166       \divide\dimen5 by 2               % (dx-gx)/2
167   \dimen6=\gLYPDFa4 pt \advance\dimen6 by-\dimen4
168       \divide\dimen6 by 2               % (dx-gx)/2
169
170   \lypdf@resetstring
171   \lypdf@moveto{0}{0}
172   \lypdf@rlineto{0}{\LYDIM2}
173   \lypdf@rlineto{\LYDIM5}{\LYDIM6}
174   \lypdf@rmoveto{\LYDIM3}{\LYDIM4}
175   \lypdf@rlineto{\LYDIM5}{\LYDIM6}
176   \lypdf@rlineto{0}{-\LYDIM2}
177   \LYPDF{\gLYPDFa5 w 1 j 1 J \lypdf@curstring}
178 }
179
180 \def\lypdf@draw_volta{% takes height, width, thickness, v_start, v_end
181   \dimen2=\gLYPDFa1 pt                  % volta height
182   \ifnum\gLYPDFa4 =0
183     \edef\vstartstr{0 0 m 0 \LYDIM2 l\space}
184   \else
185     \edef\vstartstr{0 \LYDIM2 m\space}
186   \fi
187   \ifnum\gLYPDFa5 =0
188     \edef\vendstr{\gLYPDFa2 0 l\space}
189   \else
190     \edef\vendstr{}
191   \fi
192   \LYPDF{\gLYPDFa3 w 1 J 1 j \vstartstr \gLYPDFa2 \LYDIM2 l \vendstr S}
193 }
194
195 \def\lypdf@draw_bezier_sandwich{% sixteen coords, thickness
196   \LYPDF{\gLYPDFa17 w
197          \gLYPDFa15 \gLYPDFa16 m
198          \gLYPDFa9 \gLYPDFa10 \gLYPDFa11 \gLYPDFa12 \gLYPDFa13 \gLYPDFa14 c
199          \gLYPDFa7 \gLYPDFa8 l
200          \gLYPDFa1 \gLYPDFa2 \gLYPDFa3 \gLYPDFa4 \gLYPDFa5 \gLYPDFa6 c
201          b}}
202
203 \def\lypdf@draw_dashed_slur{%
204   \LYPDF{1 J 1 j \gLYPDFa10 \gLYPDFa11 d \gLYPDFa9 w
205          \gLYPDFa1 \gLYPDFa2 m
206          \gLYPDFa3 \gLYPDFa4 \gLYPDFa5 \gLYPDFa6 \gLYPDFa7 \gLYPDFa8 c
207          S}}
208
209 %% Definitions for the various dimensions used by the brackets.
210 \newdimen\lypdf@interline     
211 \newdimen\lypdf@bracket_b     
212 \newdimen\lypdf@bracket_w     
213 \newdimen\lypdf@bracket_v     
214 \newdimen\lypdf@bracket_u     
215 \newdimen\lypdf@bracket_t     
216
217 \def\lypdf@load_bracket_dimens{
218   \lypdf@interline=\lilypondpaperinterline pt
219   \lypdf@bracket_b=0.3333\lypdf@interline
220   \lypdf@bracket_w=2\lypdf@interline
221   \lypdf@bracket_v=1.5\lypdf@interline
222   \lypdf@bracket_u=\lypdf@bracket_v
223   \lypdf@bracket_t=\lilypondpaperstaffline pt
224   \lypdf@bracket_t=2\lypdf@bracket_t
225   \relax
226 }
227
228 %alpha=50.  We calculate the sin and cos directly because TeX can't.
229 \def\lypdf@bracket_sin{0.76604}%
230 \def\lypdf@bracket_cos{0.64279}%
231
232 \def\lypdf@draw_half_bracket{% dimen2 is the bracket height
233   \dimen3=\dimen2\advance\dimen3 by -\lypdf@bracket_t  % h - t
234
235   % Here, dimen0 and dimen1 are the end points of the bracket
236   \dimen0=\lypdf@bracket_b\relax\advance\dimen0 by \lypdf@bracket_v
237   \dimen1=\dimen3\advance\dimen1 by \lypdf@bracket_u
238
239   % bottom of half bracket and inner side
240   \edef\lypdf@halfbrack{0 0 m \lypdf@strippt\lypdf@bracket_b\space 0 l 
241         \lypdf@strippt\lypdf@bracket_b\space \LYDIM3 l}
242
243   % inner curve -- first control point is just 0.4*v to the right
244   \dimen4=\lypdf@bracket_b\advance\dimen4 by 0.4\lypdf@bracket_v
245   %  ... second point is calc'd using alpha
246   \dimen5=-0.25\lypdf@bracket_v\relax
247   \dimen6=\dimen0\advance\dimen6 by \lypdf@bracket_cos\dimen5\relax
248   \dimen7=\dimen1\advance\dimen7 by \lypdf@bracket_sin\dimen5\relax
249   % draw the curve
250   \edef\lypdf@halfbrack
251     {\lypdf@halfbrack\space\LYDIM4 \LYDIM3 \LYDIM6 \LYDIM7 \LYDIM0 \LYDIM1 c}
252
253   % outer curve -- second control point is just .5*v to the right
254   % (plus 1 pt)
255   \dimen4=0.5\lypdf@bracket_v\advance\dimen4 by 1pt
256   % ... first point is calc'd using alpha  
257   \dimen5=-0.15\lypdf@bracket_v\relax
258   \dimen6=\dimen0\advance\dimen6 by \lypdf@bracket_cos\dimen5\relax
259   \dimen7=\dimen1\advance\dimen7 by \lypdf@bracket_sin\dimen5\relax
260   % draw the curve, close, stroke, fill
261   \edef\lypdf@halfbrack
262     {\lypdf@halfbrack\space\LYDIM6 \LYDIM7 \LYDIM4 \LYDIM2 0 \LYDIM2 c b} 
263 }
264
265 \def\lypdf@draw_bracket{% height
266   \dimen2=\gLYPDFa1 pt \divide\dimen2 by 2
267                 \advance\dimen2 by \lypdf@bracket_b\relax
268   % calculate the half bracket
269   \lypdf@draw_half_bracket
270   % set up graphics state, gsave, and flip the coord system 
271   % then draw both half brackets.
272   \LYPDF{\lypdf@strippt\lypdf@bracket_t\space w 
273         1 J 1 j q 1 0 0 -1 0 0 cm
274         \lypdf@halfbrack\space Q \lypdf@halfbrack}
275   }
276
277
278 %% Clean up after ourselves.
279
280 \catcode`\@=12
281 \catcode`\_=8
282
283 \endinput