Debian Codegraph @

texk/texlive/linked_scripts/musixtex/musixflx.lua

raw symbols SCIP
1 #!/usr/bin/env texlua
2
3 -- The MUSIXFLXVERSION below is the one checked against.
4
5 MUSIXFLXVERSION = "0.83"
6 VERSION = "0.83.3.lua7"
7
8 --[[
9
10 Line breaking program for MusiXTeX.
11
12 (c) Copyright Ross Mitchell 1992-1997 [email protected]
13 (c) Copyright J. Hunsberger 1997 [email protected]
14 (c) Copyright 2009, 2010 Peter Breitenlohner <[email protected]>
15 (c) Copyright 2011 Nikhil Helferty [email protected]
16 (c) Copyright 2011 Bob Tennent [email protected]
17
18 This program is free software; you can redistribute it and/or modify it
19 under the terms of the GNU General Public License as published by the
20 Free Software Foundation; either version 2 of the License, or (at your
21 option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
26 Public License for more details.
27
28 You should have received a copy of the GNU General Public License along
29 with this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31
32 --]]
33
34 --[[
35
36 Conversion to Lua by Nikhil Helferty, 2011.
37 Minor corrections by Bob Tennent [email protected].
38
39 NH: Comments marked NH (such as this one) are comments new to the Lua
40 version. Most have to do with explaining any additions or changes made
41 necessary due to differences between Lua and C. Comments not so marked
42 are retained from C version.
43
44 Almost all comments from the C version have been put in this version as
45 well. Note that Lua variables are dynamically typed, and Lua has only
46 one data structure called "tables" which, though more flexible than
47 arrays, are essentially just used as arrays here.
48
49 --]]
50
51 --[[
52
53 ChangeLog:
54
55 lua7 2011/04/21 RDT
56 Decode option argument for debugging.
57 Revert to accepting .mx1 or .tex filenames.
58 Rationalize file-name variables.
59
60 lua6 2011/04/18 RDT
61 Revert to stand-alone use.
62
63 lua5 2011/04/13 RDT
64 Allow .mx1 file to have a version number only.
65
66 lua4 2011/04/12 RDT
67 Adaptation to being called from musixtex.tex
68
69 lua3 2011/04/11 RDT
70 Replaced body of eightfivefloat by string.format("%8.5f", ... )
71
72 lua2 2011/04/11 RDT
73 Change PRINT to print for dbug output.
74
75 lua1 2011/04/08 RDT
76 Strip off pt for L line.
77
78 --]]
79
80 -- max sizes
81 -- NH: these aren't needed for array sizes in Lua (tables grow dynamically)
82 -- but I am keeping the checks in case the maximums are necessary for
83 -- TeX processing
84
85 MAX_SIGNS = 128; MAX_SECTIONS = 128; MAX_BARS = 2048
86
87 function GETLINE()
88 linebuf = infile:read()
89 return linebuf
90 end
91
92 -- NH: declare tables
93
94 -- NH: the following tables were all arrays of type int in C
95 -- the following tables were size MAX_BARS in C (2048)
96 zbar = {}; lr_repeat = {}; raggedline = {}
97 l_repeat = {}; barno = {}
98
99 -- NH: the following tables were size MAX_SECTIONS in C (128)
100 autolines = {}; bars = {}; mulooseness = {}
101
102 -- NH: the following was of size MAX_SIGNS in C (128)
103 signchange = {}
104
105 -- NH: the following were counters declared for xbar sign change detection
106 -- xbar of size MAX_BARS, linegoal MAX_SECTIONS
107 -- xbar_count, xbar_flag, softspace_count were integers declared here
108 -- as they aren't tables they'll be declared upon use
109 xbar = {}; linegoal = {}
110
111 -- NH: the following tables were all arrays of type double in C
112 -- the following were size MAX_BARS
113 hardbarlength = {}; softbarlength = {}
114 width_leftrightrepeat = {}; width_leftrepeat = {}
115
116 -- NH: the following were size MAX_SECTIONS
117 eff_hardlength = {}; eff_softlength = {}
118
119 -- NH: the following were size MAX_SIGNS
120 oldsignskip = {}; signskip = {}; futuresignskip = {}
121
122
123 -- NH: (no need to add last \n as print does this)
124 function error_exit (error_number)
125 -- NH: the following if/elseif/else construction mirrors case structure
126
127 if error_number == 0 then
128 print("\nFile error.")
129 elseif error_number == 1 then
130 print("\nUsage: [texlua] musixflx.lua basename[.tex | .mx1] [ d | m | f | s ]")
131 elseif error_number == 2 then
132 print("\nThis shouldn't happen ! Too few bars or \\mulooseness too large ?")
133 elseif error_number == 3 then
134 print("\nThis shouldn't happen ! Too few bars in section !")
135 elseif error_number == 4 then
136 print("\nMissing endmark ! Forgotten \\stop[end]piece ?")
137 elseif error_number == 5 then
138 print("\nDivision by zero ! Ask a wizard !")
139 elseif error_number == 6 then
140 print("\nVersion mis-match:")
141 print("MusiXTeX version " .. linebuf)
142 print("musixflx expected version " .. MUSIXFLXVERSION)
143 elseif error_number == 7 then
144 print("\nFile not found: " .. infilename)
145 elseif error_number == 8 then
146 print("\nCorrupted " .. infilename)
147 elseif error_number == 9 then
148 print("\nToo many sections, maximum number of sections: " .. MAX_SECTIONS)
149 elseif error_number == 10 then
150 print("\nError in " .. infilename .. " at line " .. currentline)
151 elseif error_number == 11 then
152 print("\nToo many bars, maximum number of bars: " .. MAX_BARS)
153 else
154 print("!!! Can't go on !!!")
155 end
156 os.exit(3)
157
158 end
159 -- end error_exit
160
161 -- NH: there is really no analogue to the strrchr function in C, which is frequently used
162 -- to locate the location of a last occurrence of a string
163 -- so I coded a similar function for Lua
164 -- takes two strings (str and pat), and returns everything after the last occurence of pat
165 -- in str - if no occurrences of pat, then returns nil
166 function strrchr(str, pat)
167 notfound = true
168 currentIndex = 0
169 lastIndex = 0
170 while notfound do
171 currentIndex = string.find(str, pat, lastIndex+1, true)
172 if (not currentIndex) then -- NH: returned nil - no occurence past lastIndex
173 notfound = false
174 else -- NH: got a hit for an occurence - store this value as lastIndex
175 lastIndex = currentIndex
176 end
177 end
178 if lastIndex == 0 then return nil end -- NH: no occurence found
179 return string.sub(str, lastIndex+1)
180 end
181
182
183 -- NH: start "main chunk"
184
185 io.write(string.format("Musixflx-%s", VERSION))
186
187 -- initialize variables
188 junk = -9999
189 dbug = false
190 dbug_lines = false
191 dbug_logfile = false
192 showresult = false
193 detectraggedline = false
194 currentline = 1
195 samechapter = true
196 line_number = 0
197 chapterno = 1
198
199 lthick = .4
200
201 if dbug then
202 print("\n ... decoding command line")
203 end
204
205 filename = arg[1]
206 if not filename then
207 error_exit(1)
208 end
209
210 extension = string.sub(filename, -4, -1)
211 if extension == ".tex" or extension == ".mx1" then
212 basename = string.sub(filename, 1, -5)
213 else
214 basename = filename
215 end
216
217 --[[
218
219 debugging options:
220 d output debugging information to the screen
221 m output line numbers to the screen
222 f output debugging information to file basename.mxl (not mx1)
223 s output computed lines to the screen
224
225 --]]
226
227 if (#arg == 2) then
228 if (arg[2] == "d") then
229 dbug = true
230 elseif (arg[2] == "m") then
231 dbug_lines = true
232 elseif (arg[2] == "f") then
233 dbug = true
234 dbug_logfile = true
235 dbug_lines = true
236 elseif (arg[2] == "s") then
237 showresult = true
238 else
239 error_exit(1)
240 end
241 end
242
243 infilename = basename .. ".mx1"
244
245 -- open the .mx1 file containing bar length information
246
247 if dbug then
248 print(" ... opening " .. infilename .. " for input")
249 -- NH: io.open analagous to fopen C function
250 -- will return nil value if not successful
251 -- nil is taken as false in if statements
252 end
253 infile = io.open(infilename, "r")
254 if (not infile) then
255 error_exit(7)
256 end
257 io.write(string.format(" (%s", infilename))
258 if showresult then
259 print("")
260 end
261
262 currentline = currentline + 1
263 if GETLINE() and (linebuf ~= MUSIXFLXVERSION) then
264 error_exit(6)
265 end
266
267
268 -- Open the output file
269 outfilename = basename .. ".mx2"
270 if dbug then
271 print(" ... opening " .. outfilename .. " for output")
272 end
273 outfile = io.open(outfilename, "w")
274 if (not outfile) then
275 print("\nCan't create: " .. outfilename)
276 os.exit(3)
277 end
278
279 -- Open the logfile
280 if dbug_logfile then
281 logfilename = basename .. ".mxl"
282 print(" ... open " .. logfilename .. " for debugging")
283 logfile = io.open(logfilename, "w")
284 if (not logfile) then
285 print("\nCan't create: " .. logfilename)
286 os.exit(3)
287 end
288 logfile:write("Version ", VERSION, "\n")
289 end
290
291 -- skip startindicator
292
293 if GETLINE() and (linebuf ~= "S") then
294 error_exit(8)
295 end
296
297 --[[
298 do...while loop for
299 moretimes call of \startpiece
300 >>>>>>>>>>>>>>>>>>>>>>>>>>>
301
302 Do while equivalent is repeat...until
303 --]]
304
305 GETLINE()
306
307 repeat
308
309 -- reset all arrays
310
311 if dbug then print("\n------- Chapter " .. chapterno .. " -------\n"); end
312 if dbug_logfile then logfile:write("\n------- Chapter ", chapterno, " -------\n\n"); end
313 -- NH: copying C method of loops but this means will be initializing the max spaces every time
314 -- ... not taking advantage of dynamic sizes of tables in Lua
315 -- however necessary to initialize since the starting values are used in operations
316 -- note upper bounds for these loops are the max-1, as for syntax in lua will include the upper (<= instead of <)
317
318 for i = 0, MAX_SIGNS-1 do
319 signchange[i] = junk
320 oldsignskip[i] = 0
321 futuresignskip[i] = 0 -- jh-1
322 signskip[i] = 0
323 end
324
325 for i = 0, MAX_BARS-1 do
326 hardbarlength[i] = 0
327 softbarlength[i] = 0
328 width_leftrightrepeat[i] = 0
329 lr_repeat[i] = false
330 width_leftrepeat[i] = 0
331 xbar[i] = 0 -- jh-1
332 l_repeat[i] = false
333 zbar[i] = false
334 raggedline[i] = false
335 barno[i] = 0
336 end
337
338 for i = 0, MAX_SECTIONS-1 do
339 eff_hardlength[i] = 0
340 eff_softlength[i] = 0
341 bars[i] = 0
342 autolines[i] = false
343 linegoal[i] = 0
344 mulooseness[i] = 0
345 end
346
347 --[[
348
349 Read and decode header items:
350
351 1. Linewidth;
352 2. Parindent;
353 3. Beforeruleskip;
354 4. Afterruleskip;
355 5. Elemskip;
356 6. Clefskip;
357 7. Signskip;
358 --]]
359
360 -- NH: for those curious why it is necessary to take a substring of the line, it is to strip the "pt" off of the string
361 -- to make them valid strings for coercion to numbers later on (something that the atof does automatically)
362 -- RDT: linebuf tests are to avoid taking a substring of an empty string
363 if linebuf then linewidth = string.sub(linebuf, 1, -3); currentline=currentline+1 end
364 GETLINE(); if linebuf then parindent = string.sub(linebuf, 1, -3); currentline=currentline+1 end
365 GETLINE(); if linebuf then beforerule = string.sub(linebuf, 1, -3); currentline=currentline+1 end
366 GETLINE(); if linebuf then afterrule = string.sub(linebuf, 1, -3); currentline=currentline+1 end
367 GETLINE(); if linebuf then elemskip = string.sub(linebuf, 1, -3); currentline=currentline+1 end
368 GETLINE(); if linebuf then clefskip = string.sub(linebuf, 1, -3); currentline=currentline+1 end
369 GETLINE(); if linebuf then signskip[0] = string.sub(linebuf, 1, -3); currentline=currentline+1 end
370
371 futuresignskip[0] = signskip[0] -- Initialize for xbar signs, jh-1
372
373 --[[
374 Read the records specifying contributions to bar length.
375 Lengths are of two types:
376 (a) Hard or unscaleable, eg barlines, clef or meter changes (typ=0).
377 (b) Soft or scaleable, eg noteboxes which scale with \elemskip (typ=1).
378 Note that \afterruleskip and \beforerulskip are considered soft.
379 --]]
380
381 jbar = junk
382 i = 0
383 sign = 0
384 xbar_count = 0 -- Used to detect presence of xbars jh-1
385 xbar_flag = false -- True allows check for xbar jh-1
386 hardsign = 0 -- Accumulator for hard skip from sign change jh-1
387 softspace_count = 0 -- Watch for softspace after bars are posted jh-1
388
389 all_section = 0
390
391 if dbug then
392 print(" ... reading")
393 end
394
395 while (samechapter and GETLINE()) do
396
397 currentline = currentline + 1
398 local firstChar = string.sub(linebuf, 1, 1)
399
400 -- NH: use if/elseif/else to mirror switch/case statements
401
402 --[[
403 '\startpiece'
404 stop reading,
405 compute,
406 write
407 and start again
408 --]]
409
410 if (firstChar == "S") then
411 samechapter = false
412
413 --[[
414 End of section. Action:
415 Right justify the material ending at the previous bar.
416 Read the number following the *, which is the 'looseness'
417 parameter of the section just ended.
418 Reset the bar test integer to JUNK in case the bar number
419 was reset between sections.
420 --]]
421
422 elseif (firstChar == "*") then
423 local firstSpace = string.find(linebuf, " ")
424 local secondSpace = string.find(linebuf, " ", firstSpace+1)
425 if not secondSpace then -- NH: possibility that there is no second number in line
426 mulooseness[all_section] = string.sub(linebuf, firstSpace+1)
427 else
428 mulooseness[all_section] = string.sub(linebuf, firstSpace+1, secondSpace-1)
429 end
430 -- NH: above firstSpace/secondSpace mathematics were to mimic function of atol use in original C function
431 linegoal[all_section] = 0 -- jh-2 + WS-1
432 if secondSpace then
433 linegoal[all_section] = string.sub(linebuf, secondSpace+1) -- NH: this my interpretation of an sscanf line signed jh-2 + WS-1 in C
434 end
435 linegoal[all_section] = linegoal[all_section] + 0 -- NH: this is a fairly silly trick to force coercion for valid comparison to numbers, could also use tonumber function
436 mulooseness[all_section] = mulooseness[all_section] + 0
437 if ((mulooseness[all_section] ~= 0) and (linegoal[all_section] > 0)) then
438 print("\\linegoal{" .. linegoal[all_section] .. "} ignored because \\mulooseness not equal to zero")
439 print(" for section " .. all_section+1 .. " in chapter " .. chapterno)
440 linegoal[all_section] = 0 -- Reset to ignore it. jh-2 + WS-1
441 end
442 all_section = all_section + 1
443 if all_section > (MAX_SECTIONS - 1) then
444 error_exit(9)
445 end
446 jbar=junk
447
448 --[[
449 Right justify the material ending at the previous bar.
450 Set flag.
451 Reset the bar test integer to JUNK in case the bar number
452 was reset between sections.
453 ]]
454
455
456 elseif (firstChar == "a") then
457 autolines[all_section] = true
458 all_section = all_section + 1
459 if (all_section > (MAX_SECTIONS - 1)) then
460 error_exit(9)
461 end
462 jbar=junk
463
464 --[[
465 found a raggedline, let's set a flag
466 I think, I'll hardly get a Nobel-Award for coding this
467 What a pity! :-(
468 but perhaps a Pulitzer-Award for my comments. :-)
469 --]]
470
471 elseif (firstChar == "r") then
472 raggedline[i+1] = true
473
474
475 -- found \zbar, set flag and store barno
476 elseif (firstChar == "z") then
477 zbar[i] = true
478 barno[i] = strrchr(linebuf, " ")
479 xbar_count = xbar_count + 1 -- Help detect xbars, track zbar offsets. jh-1
480 hardsign = 0 -- reset any sign change skip accumulated. jh-1
481 softspace_count = 0 -- Reset for next bar. jh-1
482
483
484 --[[
485 found a leftrightrepeat
486 set a flag and store the different widths
487 ]]
488
489 elseif (firstChar == "l") then
490 -- NH: there are some commented out printf lines in C code in this case
491 -- I'm assuming they were used at one point for debugging purposes and
492 -- am not translating them, but if should be reinstated they exist at lines
493 -- 380-387 in C program
494 lr_repeat[i] = true
495 local firstSpace = string.find(linebuf, " ")
496 local secondSpace = string.find(linebuf, " ", firstSpace + 1)
497 width_leftrightrepeat[i] = string.sub(linebuf, firstSpace+1, secondSpace-3) -- NH: secondspace-3 as secondspace-1 will include the "pt"
498 width_leftrepeat[i] = string.sub(linebuf, secondSpace+1, -3) -- NH: again, -3 to chop off the "pt"
499
500
501 --[[
502 found a leftrepeat
503 set a flag and store width
504 ]]
505
506 elseif (firstChar == "L") then
507 l_repeat[i] = true
508 local firstSpace = string.find(linebuf, " ")
509 width_leftrepeat[i] = string.sub(linebuf, firstSpace+1, -3) -- RDT: again, -3 to chop off the "pt"
510
511
512 -- store barno
513 elseif (firstChar == "b") then
514 barno[i] = strrchr(linebuf, " ")
515 xbar_count = xbar_count+1 -- Track possible xbars. jh-1
516 xbar_flag = true -- Allow check of next line to detect xbar. jh-1
517 hardsign = 0 -- Reset accumulated hard sign skip. jh-1
518 softspace_count = 0 -- Reset for next bar. jh-1
519
520 --[[
521 enabling the use of 'hard' offsets
522 advance current hardwith
523 reduce current softwidth
524 --]]
525
526 elseif (firstChar == "h") then
527 x = string.sub(strrchr(linebuf, " "), 1, -3) -- -3 is to chop off "pt" in string to allow x to be valid for arithmetic
528 softbarlength[i] = softbarlength[i] - x
529 hardbarlength[i] = hardbarlength[i] + x
530 eff_softlength[all_section] = eff_softlength[all_section] - x
531 eff_hardlength[all_section] = eff_hardlength[all_section] + x
532
533
534 --[[
535 This record began with 's' and specifies a key signature change
536 store the signskip, s.b.
537 ]]
538
539 elseif (firstChar == "s") then -- Changes to detect xbar and signchange interaction, jh-1
540
541 local firstSpace = string.find(linebuf, " ")
542 local secondSpace = string.find(linebuf, " ", firstSpace+1)
543 if not secondSpace then
544 tempholdskip = string.sub(linebuf, firstSpace+1, -3)
545 else
546 tempholdskip = string.sub(linebuf, firstSpace+1, secondSpace-3)
547 end
548 -- NH: all of above to mimic atof statement
549 -- wasn't sure if statements with s would have a second space and more info
550 -- so accounted for either possibility
551
552 -- We might be in the middle of an xbar setup, and we only want to increment
553 -- the sign pointer if this is the first sign change. jh-1
554 if (not (signchange[sign] == i)) then -- first time for this bar set jh-1
555 sign = sign + 1
556 signchange[sign] = i
557 signskip[sign] = tempholdskip
558 oldsignskip[sign] = hardsign -- Capture accumulated hard space. jh-1
559 xbar[i] = xbar[i] + 1 -- Increment to detect xbars with sign changes. jh-1
560 --[[
561 Housekeeping is done... Now, one more condition to check. If there
562 has been any soft space since the bar was declared, then musixtex
563 will NOT publish a sign change notice at the end of the line for
564 this sign change when there is a line break in this bar.
565 In that case, signal that this should be treated as an xbar, which
566 will effectively suppress the transfer of hardspace for the sign
567 change notice. jh-1
568 --]]
569 if softspace_count > 0 then xbar[i] = 2 end-- Suppress space move jh-1
570 end
571 -- Always update the futuresignskip value in case this is the last
572 futuresignskip[sign] = tempholdskip
573
574 -- comment, do nothing
575 elseif (firstChar == "%") then
576
577 -- This is an 'ordinary' line, listing a contribution to the barlength.
578 else
579 if (not (tonumber(firstChar))) then
580 error_exit(10)
581 end
582 local firstSpace = string.find(linebuf, " ")
583 local secondSpace = string.find(linebuf, " ", firstSpace + 1)
584 bar = string.sub(linebuf, 1, firstSpace-1)
585 typ = string.sub(linebuf, firstSpace+1, secondSpace-1)
586 x = string.sub(linebuf, secondSpace+1, -3) -- the -3 is to strip the "pt" off of the x
587 if (typ ~= "0") then eff_softlength[all_section] = eff_softlength[all_section] + x
588 else eff_hardlength[all_section] = eff_hardlength[all_section] + x
589 end
590
591 --[[
592 Increment bar number if the bar number
593 read from the file has changed.
594 Accumulate current bar length.
595 --]]
596 bar = bar + 0 -- NH: forcing coercion to number type from string
597 if tonumber(bar) > jbar then
598 i = i + 1
599 bars[all_section] = bars[all_section] + 1
600 if i > MAX_BARS then
601 error_exit(11)
602 end
603 end
604
605 -- At this point, can check if this is an xbar...Only check once. jh-1
606 if xbar_flag then
607 if ((xbar_count - bar - 1) == 0) then -- find an xbar. jh-1
608 xbar_count = xbar_count-1 -- adjust offset to stay on track
609
610 --[[
611 To handle the special conditions caused by possible xbars and
612 sign changes, the xbar logic state has to be examined and changed
613 only if this is the first xbar, and not for subsequent ones. jh-1
614 --]]
615 if (xbar[i] == 0) then -- then this is the first xbar in the setup
616 -- or there has already been a sign change
617 xbar[i] = 1 -- jh-1
618 end
619 end
620 end
621
622 xbar_flag = false -- Reset to prevent checks until next bar. jh-1
623
624 if (typ ~= "0") then
625 softbarlength[i] = softbarlength[i] + x
626 -- Count softspace entries to help in sign/linebreak decisions jh-1
627 softspace_count = softspace_count+1
628 hardsign = 0 -- Safety - just be sure in case line break jh-1
629 else -- jh-1
630 hardbarlength[i] = hardbarlength[i] + x
631 hardsign = hardsign + x -- accumulate hardspace, there may be a sign change
632 end
633
634 jbar = bar
635
636 end -- end if statements
637
638 end -- end while loop
639
640 --[[
641 Decrement the number of sections if the final section is void.
642 This will be the usual case where the input file ends with
643 an end of section record.
644 If this record has been omitted, stop going on to avoid
645 'You can't use \raise....'.
646 ]]
647
648 if dbug then
649 print(" ... compute")
650 end
651
652 if (bars[all_section] == 0) then
653 all_section = all_section - 1
654 else
655 error_exit(4)
656 end
657
658 -- Summarize sectioning information
659
660 if dbug then
661 print("\nNumber of sections : " .. (all_section+1) .."\n")
662
663 for section = 0, all_section do
664 if (autolines[section]) then
665 print("---- autoline section ----")
666 end
667 print("Section : " .. (section+1))
668 print("Number of bars in section : " .. bars[section])
669 print("Length(hard) of section " .. (section+1) .. " : " .. eff_hardlength[section])
670 print("Length(soft) of section " .. (section+1) .. " : " .. eff_softlength[section])
671 if (linegoal[section] ~= 0) then -- linegoal applies, signed jh-2 in C
672 print("Section line goal was determined by \\linegoal value...")
673 print("Line Goal for section : " .. linegoal[section])
674 else -- mulooseness applies, signed jh-2 in C
675 print("Looseness of section : " .. mulooseness[section])
676 end
677
678 io.read() -- NH: mimics getchar()
679 end -- end for
680
681 end -- end if
682
683 if dbug_logfile then
684 logfile:write("\nNumber of sections : ", all_section+1, "\n\n")
685
686 for section = 0, all_section do
687 if (autolines[section]) then
688 logfile:write("---- autoline section ----\n")
689 end
690 logfile:write("Section : ", section+1, "\n")
691 logfile:write("Number of bars in section : ", bars[section], "\n")
692 logfile:write("Length(hard) of section ", section+1, " : ", eff_hardlength[section], "\n")
693 logfile:write("Length(soft) of section ", section+1, " : ", eff_softlength[section], "\n")
694 if (linegoal[section] ~= 0) then -- line goal applies. signed jh-2 in C
695 logfile:write("Section line goal was determined by \\linegoal value...\n")
696 logfile:write("Line goal for section : ", linegoal[section], "\n")
697 else -- mulooseness applies, signed jh-2 in C
698 logfile:write("Looseness of section : ", mulooseness[section], "\n")
699 end
700 end
701 end
702
703 --[[
704 C Comments:
705 Loop over the sections defined in the input file.
706 Each section must be right justified.
707 LAST is the absolute number of the last bar
708 in the current section.
709 --]]
710
711 sign = 0
712 mark = 0
713 lastbarnumber = 0
714
715
716 for section = 0, all_section do
717 line_in_section = 1
718 lastbarnumber = lastbarnumber + bars[section]
719
720 -- Find number of lines to work towards
721 lines = math.floor(((eff_hardlength[section] + eff_softlength[section] + parindent)/
722 (linewidth-(clefskip+signskip[sign]))) + .5) -- need to use floor function as lua has no "integer" type, just numbers
723 if (lines == 0) then lines = 1 end -- safety
724
725 natural_lines = lines -- Keep this for debug report. Signed jh-2 in C
726 lines = lines + mulooseness[section]
727 if ((mulooseness[section] ~= 0) and (linegoal[section] > 0)) then -- signed jh-2 in C
728 print("Unexpected line goal reset occured for section " .. section+1)
729 linegoal[section] = 0 -- Zero it, Safety, should not happen, signed jh-2 in C
730 end
731 if (linegoal[section] > 0) then lines = linegoal[section] end -- Signed jh-2 in C
732 if lines < 1 then
733 lines = 1
734 print("Don't stress \\mulooseness too much !!!")
735 end
736
737 --[[
738 C comments:
739 autolinesflag set in current section ?
740 iftrue force number of lines to 1
741 --]]
742
743 if (autolines[section]) then lines = 1 end
744
745 if dbug then
746 print("Section number : " .. section+1)
747 print("Last bar in this section : " .. lastbarnumber)
748 print("Number of bars : " .. bars[section])
749 print("Natural number of lines : " .. natural_lines) -- signed jh-2 in C
750 print("Chosen number of lines : " .. lines .. "\n")
751 end
752
753 if dbug_logfile then
754 logfile:write("Section number : ", section+1, "\n")
755 logfile:write("Last bar in this section : ", lastbarnumber, "\n")
756 logfile:write("Number of bars : ", bars[section], "\n")
757 logfile:write("Natural number of lines : ", natural_lines, "\n") -- signed jh-2 in C
758 logfile:write("Chosen number of lines : ", lines, "\n\n")
759 end
760
761 if (bars[section]<1) then error_exit(3) end
762
763 --[[
764 C comments:
765 fill_length is the length of 'bar' material (ie excluding
766 signature space) required to fill the remainder
767 of the piece. This value will not be exact if there are
768 sign changes within the section. However,
769 fill_length is used only to keep track of the mean scale factor
770 for the remainder of the piece, as opposed to individual lines.
771
772 Loop over lines, working out number of bars
773 and revised \elemskip for each line.
774 added correct computing of fill_length
775 --]]
776
777
778 for j = 1, lines do
779 line_number = line_number + 1
780 fill_length = (lines-j+1) * (linewidth - (clefskip + signskip[sign]))
781
782
783 --[[
784 C comments:
785 Work out mean element skip over remaining bars
786 in the current section.
787 EFFWID is the effective line width once
788 key signature have been written.
789 Set parindent to zero after it has been used for the
790 first line of the first section.
791 --]]
792
793 if (eff_softlength[section] == 0) then error_exit(5) end
794 spc_factor = (fill_length - eff_hardlength[section])/eff_softlength[section]
795 if ((xbar[mark+1] > 1) and (mark>0)) then
796 -- The bar is an bar+xbar with a sign change. Signed jh-1 in C
797 eff_linewidth = linewidth - (clefskip+signskip[sign-1]) - parindent
798 else -- This is a normal bar. Signed jh-1 in C
799 eff_linewidth = linewidth - (clefskip+signskip[sign]) - parindent
800 end -- Signed jh-1 in C
801
802 signskip[sign] = futuresignskip[sign] -- Supports xbar signs. Signed jh-1 in C
803
804 parindent = 0
805
806 -- Fill the current line by adding bars until overflow.
807
808 i = mark
809 firstbarno = barno[mark+1]
810 hardlength = 0
811 softlength = 0
812 x = 0
813 lastbar = 0
814 detect_end = false
815
816 while (x < eff_linewidth) do
817 if detect_end then break end
818 i = i + 1
819 -- Check for raggedline
820
821 if raggedline[i] then detectraggedline = true end
822
823 -- Check for key signature change at this bar.
824
825 if (i == signchange[sign+1]) then sign = sign + 1 end
826
827 lastbar = hardbarlength[i] + spc_factor*softbarlength[i]
828 x = x + lastbar
829
830 -- Enforce termination at last bar and last line
831
832 if (i==lastbarnumber) then detect_end = true
833 elseif (line_in_section == lines) then
834 detect_end = false
835 x = 0
836 end
837
838 hardlength = hardlength + hardbarlength[i]
839 softlength = softlength + softbarlength[i]
840
841 end
842
843 --[[
844 C comments:
845 If the overhang is less than half the barlength,
846 include the latest bar in the line,
847 and shrink the line accordingly.
848 --]]
849
850 if ((x-eff_linewidth) < (lastbar/2)) then
851 barsinline = i - mark
852 mark = i
853 lastbarno = barno[mark]
854
855 --[[
856 C comments:
857 last bar in line a zbar?
858 if true -> add to the first bar in next line
859 the amount of afterruleskip
860 --]]
861
862 if zbar[mark] then
863 softbarlength[i+1] = softbarlength[i+1] + afterrule
864 eff_softlength[section] = eff_softlength[section] + afterrule
865 end
866
867 --[[
868 C comments:
869 last bar in line a leftrightrepeat?
870 if true -> reduce hardwidth of current line
871 advance the hardwidth of next bar
872 advance the softwidth of next bar
873 --]]
874
875 if lr_repeat[mark] then
876 -- NH: there are some commented lines of code out here (printf statements) that I have not preserved
877 -- if they should be reinstated then they are located at lines 744-746 of the original program
878 hardlength = hardlength - (width_leftrightrepeat[i] - width_leftrepeat[i])
879 eff_hardlength[section] = eff_hardlength[section] + (width_leftrightrepeat[i] - width_leftrepeat[i])
880 hardbarlength[i+1] = hardbarlength[i+1] + width_leftrepeat[i]
881 softbarlength[i+1] = softbarlength[i+1] + (afterrule/2)
882 eff_softlength[section] = eff_softlength[section] + (afterrule/2)
883 end
884
885 --[[
886 C comments:
887 last bar in line a leftrepeat?
888 if true -> reduce hardwidth of current line
889 advance the hardwidth of next bar
890 advance the softwidth of next bar
891 --]]
892
893 if l_repeat[mark] then
894 hardlength = hardlength - (width_leftrepeat[i] - lthick)
895 hardbarlength[i+1] = hardbarlength[i+1] + width_leftrepeat[i]
896 softbarlength[i+1] = softbarlength[i+1] + (afterrule/2)
897 eff_softlength[section] = eff_softlength[section] + (afterrule/2)
898 end
899
900 if (signchange[sign+1] == mark+1) then -- signed s.b. in C
901 sign = sign + 1
902 --[[
903 C comments:
904 Because the bar is staying here in the line, we look ahead
905 to see if the upcoming bar is a sign change, and adjust space
906 to account for the complimentary sign change notice that will
907 be posted at the end of this line. However, if the upcoming
908 sign change bar is really a bar+xbar set, where the sign change
909 is buried in the xbar, then we don't do the move because the
910 change notice really won't be posted in this line.
911
912 Signed jh-1 in C.
913
914 --]]
915 if (xbar[mark+1] < 2) then -- okay to do the move. signed jh-1 in C
916 hardlength = hardlength + oldsignskip[sign]
917 hardbarlength[mark+1] = hardbarlength[mark+1] - oldsignskip[sign]
918 end
919 end
920
921 -- Exclude the latest bar, and stretch the line.
922
923 else
924 barsinline=i-1-mark
925 if (barsinline < 1) then error_exit(2) end
926 mark = i - 1
927 lastbarno = barno[mark]
928 hardlength = hardlength - hardbarlength[i]
929 softlength = softlength - softbarlength[i]
930
931 if zbar[mark] then softbarlength[i] = softbarlength[i] + afterrule end
932
933 if lr_repeat[mark] then
934 hardlength = hardlength - (width_leftrightrepeat[i-1]-width_leftrepeat[i-1])
935 eff_hardlength[section] = eff_hardlength[section] + (width_leftrightrepeat[i-1] - width_leftrepeat[i-1])
936 hardbarlength[i] = hardbarlength[i] + width_leftrepeat[i-1]
937 softbarlength[i] = softbarlength[i] + (afterrule/2)
938 eff_softlength[section] = eff_softlength[section] + (afterrule/2)
939 end
940
941 if l_repeat[mark] then
942 hardlength = hardlength - (width_leftrepeat[i-1] - lthick)
943 hardbarlength[i] = hardbarlength[i] + width_leftrepeat[i-1]
944 softbarlength[i] = softbarlength[i] + (afterrule/2)
945 eff_softlength[section] = eff_softlength[section] + (afterrule/2)
946 end
947
948 --[[
949 C comments:
950 Error (o/u-hbox) occurs only when signature change start in next line
951 -> look for signature change in next line
952 if true then advance the hardwidth of current line
953 reduce next hard barlength by signature change
954 --]]
955
956 if (signchange[sign] == (mark+1)) then
957 --[[
958 C comments:
959 However, if the next bar is a bar+xbar set where the
960 sign change comes from the xbar, then don't do this
961 move, because the extra skip is not really there!
962
963 Signed jh-1 in C.
964 --]]
965 if(xbar[mark+1] < 2) then -- alright, do the move. signed jh-1 in C
966 hardlength = hardlength + oldsignskip[sign]
967 hardbarlength[mark+1] = hardbarlength[mark+1] - oldsignskip[sign]
968 end
969
970 end
971
972 end
973
974 --[[
975 C comments:
976 Define a flex factor for this line as the ratio
977 of soft part of the specified line width,
978 to soft width in the approximate line.
979 --]]
980
981 if (softlength == 0) then error_exit(5) end
982 flexit = (eff_linewidth - hardlength) / softlength
983 if detectraggedline then
984 flexit = 1
985 detectraggedline = false
986 end
987 cor_elemskip = elemskip * flexit
988 cor_afterrule = afterrule * flexit
989 cor_beforerule = beforerule * flexit
990
991 if dbug then
992 print("Line number : " .. line_number)
993 print("Fill length : " .. fill_length)
994 print("Effective length : " .. eff_softlength[section] + eff_hardlength[section])
995 print("Mean space factor : " .. spc_factor)
996 print("Bars in line : " .. barsinline)
997 print("Effective linewidth : " .. eff_linewidth)
998 print("Uncorrected hard length : " .. hardlength)
999 print("Uncorrected soft length : " .. softlength)
1000 print("fLex factor (soft) : " .. flexit)
1001 print("Corrected elemskip : " .. cor_elemskip)
1002 print("Corrected afterrule : " .. cor_afterrule)
1003 print("Corrected beforerule : " .. cor_beforerule)
1004
1005 io.read() -- NH: mimics getchar()
1006 end
1007
1008 if dbug_logfile then
1009 logfile:write("Line number : ", line_number, "\n")
1010 logfile:write("Fill length : ", fill_length, "\n")
1011 logfile:write("Effective length : ", eff_softlength[section] + eff_hardlength[section], "\n")
1012 logfile:write("Mean space factor : ", spc_factor, "\n")
1013 logfile:write("Bars in line : ", barsinline, "\n")
1014 logfile:write("Effective linewidth : ", eff_linewidth, "\n")
1015 logfile:write("Uncorrected hard length : ", hardlength, "\n")
1016 logfile:write("Uncorrected soft length : ", softlength, "\n")
1017 logfile:write("Flex factor (soft) : ", flexit, "\n")
1018 logfile:write("Corrected elemskip : ", cor_elemskip, "\n")
1019 logfile:write("Corrected afterrule : ", cor_afterrule, "\n")
1020 logfile:write("Corrected beforerule : ", cor_beforerule, "\n\n")
1021 end
1022
1023 eff_hardlength[section] = eff_hardlength[section] - hardlength
1024 eff_softlength[section] = eff_softlength[section] - softlength
1025 fill_length = fill_length - eff_linewidth
1026
1027 -- Write a record to the output file
1028
1029 outfile:write(
1030 string.format("\\lineset{%3d}{%2d}{%8.5fpt}{%8.5fpt}{%8.5fpt}%% %d - %d\n",
1031 line_number, barsinline, cor_elemskip, cor_afterrule,
1032 cor_beforerule,
1033 firstbarno, lastbarno))
1034
1035
1036
1037 if showresult then
1038 print(
1039 string.format("\\lineset{%3d}{%2d}{%8.5fpt}{%8.5fpt}{%8.5fpt}%% %d - %d",
1040 line_number, barsinline, cor_elemskip, cor_afterrule,
1041 cor_beforerule,
1042 firstbarno, lastbarno))
1043 end
1044
1045 if dbug_lines then print(" ... writing line : " .. line_number) end
1046 line_in_section = line_in_section + 1 -- NH: need to do this in loop, lua syntax doesn't allow for it in for loop spec
1047 end -- end lines for
1048 end -- end sections for
1049
1050 if dbug_lines then print("") end
1051
1052
1053 samechapter = true
1054 chapterno = chapterno + 1
1055 until (not GETLINE()) -- end repeat loop
1056
1057 -- closing files
1058 infile:close()
1059 io.write(")")
1060 if dbug_logfile then
1061 logfile:close()
1062 if (not logfile) then error_exit(0) end
1063 end
1064 outfile:close()
1065 if (not outfile) then error_exit(0) end
1066
1067 if dbug then
1068 print(" ... that's all, bye")
1069 end
1070 print("")
1071 -- NH: end main chunk and program!