-- Author: John Maxwell
-- Last Edited by: Maxwell, November 22, 1983 10:21 am
DIRECTORY
Beam USING [SetStems],
Event USING [GetScoreIndex, Invisible, SetStave, Staves],
Graphics USING [Context, DrawBox, GetColor, SetCP, Scale, SetPaintMode, SetStipple, Color],
Inline USING [LongCOPY, LowHalf],
MusicDefs,
Score USING [BuildCache, GetKey],
Sheet USING [default, DrawKey, FindLine, FindSection, Height, Map, MapHeight, NextLine, OctavaHeight, PriorLine, Reset, SetBegin],
String USING [AppendDecimal],
Utility USING [DrawChar, DrawLine, DrawString, FreeSegment, NewSegment, SetFont];
SheetImplA: PROGRAM
IMPORTS Beam, Graphics, Inline, MusicDefs, Score, Sheet, String, Event, Utility
EXPORTS Sheet =
BEGIN
OPEN MusicDefs, Score, Sheet;
-- begin: PUBLIC Time;
-- endTime: PUBLIC Time;
-- scale, sheet.width: PUBLIC INTEGER;
-- ****************************************************************************
-- the sheet consists of an array of Sections
-- ****************************************************************************
nullSheet: SheetPTR ← zone.NEW[SheetRec[0]];
New: PUBLIC PROC[length: CARDINAL, old: SheetPTR ← NIL] RETURNS[sheet: SheetPTR] =
BEGIN
oldSize: CARDINAL;
nullSize: CARDINAL ← SIZE[SheetRec[0]];
sheet ← Utility.NewSegment[SIZE[SheetRec[length]], length, nullSize-1];
IF old = NIL THEN old ← nullSheet; -- gets the default values for us
oldSize ← SIZE[SheetRec[old.max]];
Inline.LongCOPY[from: old, nwords: nullSize-1, to: sheet]; -- skip 'max'
Inline.LongCOPY[from: old+nullSize, nwords: oldSize-nullSize, to: sheet+nullSize];
IF sheet.max # length THEN ERROR;
sheet.length ← length;
IF old # nullSheet THEN {Utility.FreeSegment[old]; RETURN};
END;
Free: PUBLIC PROC[sheet: SheetPTR] =
BEGIN
Utility.FreeSegment[sheet];
END;
Reset: PUBLIC PROCEDURE[score: ScorePTR] =
BEGIN
sync: EventPTR ← NIL;
staves: StavesPTR ← NIL;
page, pitch: INTEGER ← 2;
sheet: SheetPTR ← score.sheet;
v, j, last, next: CARDINAL ← 0;
time, eol, priorEol, eventTime: Time ← 0;
top, height, x, sc, priorHeight: INTEGER ← 0;
IF sheet.scale = 2 THEN sc ← 3 ELSE sc ← 2*sheet.scale;
[staves, last, next] ← NextStyle[score, NIL, 0, 0, 100];
IF staves = NIL THEN RETURN;
FOR i: CARDINAL IN [0..score.length] DO
IF i = score.length
THEN eventTime ← LONG[sheet.width]*sheet.length
ELSE {IF score.event[i].type # staves THEN LOOP; eventTime ← score.event[i].time};
WHILE time < eventTime OR eventTime = eol DO -- fill in all of the sections between
IF j = sheet.length THEN EXIT;
sheet[j].page ← 0;
sheet[j].time ← time;
IF j # 0 AND sheet[j].time = sheet[j-1].time
THEN sheet[j].key ← sheet[j-1].key
ELSE sheet[j].key ← Key[score, time, eol];
IF time < eol THEN x ← x+Inline.LowHalf[time-sheet[j-1].time];
IF time >= eol THEN { -- new line
height ← height-priorHeight;
x ← MAX[8, ABS[8*(sheet[j].key)]];
priorEol ← eol;
eol ← priorEol+sheet.width-x;
[staves, last, next] ← NextStyle[score, staves, last, next, eol-20];
priorHeight ← -staves.staff[staves.length-1].y+staves.offset;
IF top-height > (650*sc)/2 THEN { -- new page
top ← height; sheet[j].page ← page; page ← page+1}};
sheet[j].x ← x;
sheet[j].y ← height;
sheet[j].staves ← staves;
time ← MIN[eol, eventTime];
j ← j + 1;
ENDLOOP;
IF i = score.length THEN EXIT;
staves ← Event.Staves[score.event[i]];
IF staves.staves = style THEN LOOP;
Event.SetStave[score, staves, staves];
IF staves.staves # clef THEN LOOP;
IF Event.Invisible[score, i, priorEol] THEN time ← priorEol;
ENDLOOP;
IF sheet.scale = 2 THEN Paginate[sheet];
Sheet.SetBegin[sheet, sheet.begin];
Score.BuildCache[score, sheet.begin];
FOR i: CARDINAL IN [0..score.beamHeap.length) DO
IF score.beamHeap.beam[i].sync2.time < score.sheet.dirty1 THEN LOOP;
IF score.beamHeap.beam[i].sync1.time > score.sheet.dirty2 THEN LOOP;
Beam.SetStems[sheet, score.beamHeap.beam[i]];
ENDLOOP;
END;
NextStyle: PROCEDURE[score: ScorePTR, old: StavesPTR, last, next: CARDINAL, eol: Time] RETURNS[StavesPTR, CARDINAL, CARDINAL] =
BEGIN
temp, new: StavesPTR;
newLast, newNext: CARDINAL ← 0;
new ← old;
newLast ← last;
newNext ← next;
FOR i: CARDINAL IN [next..score.length) DO
IF score.event[i].time >= eol+40 THEN EXIT;
IF score.event[i].type # staves THEN LOOP;
temp ← Event.Staves[score.event[i]];
IF temp.staves # style THEN LOOP;
newNext ← i;
IF score.event[i].time >= eol THEN EXIT;
IF temp = old THEN LOOP;
IF new # NIL AND i # next AND
new.staff[new.length-1].y < temp.staff[temp.length-1].y THEN LOOP;
new ← temp;
newLast ← i;
ENDLOOP;
IF newLast = last AND old # NIL THEN RETURN[old, newLast, newNext];
Event.SetStave[score, old, Event.Staves[score.event[newLast]]];
RETURN[new, newLast, newNext];
END;
Key: PROCEDURE[score: ScorePTR, time, eol: Time] RETURNS[key: INTEGER ← 0] =
INLINE BEGIN
keySig: LONG POINTER TO EventRec.keySignature ← NIL;
key ← Score.GetKey[score, time];
FOR i: CARDINAL IN [Event.GetScoreIndex[score, score.cache.key1]..score.length) DO
IF score.event[i].time >= time+score.sheet.width THEN EXIT;
IF score.event[i].type # keySignature THEN LOOP;
keySig ← LOOPHOLE[score.event[i]];
IF time = eol THEN EXIT;
ENDLOOP;
IF keySig = NIL THEN RETURN;
IF ~Event.Invisible[score, Event.GetScoreIndex[score, keySig], eol] THEN RETURN;
RETURN[keySig.key];
END;
limit: INTEGER ← 1050;
Paginate: PROCEDURE[sheet: SheetPTR] =
BEGIN
min: INTEGER = 75;
start: INTEGER = 40;
page: CARDINAL ← 2;
height: INTEGER ← -start;
line, last: CARDINAL ← 1;
count, first: CARDINAL ← 0;
FOR line ← 0, Sheet.NextLine[sheet, line] WHILE line # last DO
height ← height-sheet[line].staves.staff[3].y+min;
count ← count+1;
IF height < limit THEN {last ← line; LOOP};
IF count # 1 THEN {-- subtract out the last line
count ← count-1;
height ← height+sheet[line].staves.staff[3].y-min};
-- layout the page
LayoutPage[sheet, count, first, line, limit+count*min-start-height];
sheet[line].page ← page;
page ← page+1;
-- set up for next page
first ← line; line ← last;
count ← 0;
height ← -start;
ENDLOOP;
END;
LayoutPage: PROCEDURE[sheet: SheetPTR, lines, first, last, space: CARDINAL] =
BEGIN
oldHeight, height: INTEGER ← sheet[first].y;
-- layout this page
FOR i: CARDINAL IN (first..sheet.length) DO
IF sheet[i].y # oldHeight THEN {-- new line
IF i > last THEN EXIT;
oldHeight ← sheet[i].y;
height ← sheet[i-1].y+sheet[i-1].staves.staff[3].y;
height ← height-space/lines};
sheet[i].y ← height;
sheet[i].page ← 0;
ENDLOOP;
END;
FindLine: PUBLIC PROCEDURE[sheet: SheetPTR, t: Time] RETURNS[l: INTEGER] =
BEGIN
start: CARDINAL ← 0;
FOR i: CARDINAL IN [0..sheet.length-1) DO
IF sheet[i].time # sheet[i+1].time THEN {start ← i; EXIT};
ENDLOOP;
l ← start;
FOR i: CARDINAL ← start, NextLine[sheet, i] DO
IF sheet[i].time <= t THEN l ← i ELSE RETURN;
IF i = sheet.length-1 THEN RETURN;
ENDLOOP;
END;
FindSection: PUBLIC PROCEDURE[sheet: SheetPTR, t: Time] RETURNS[INTEGER] =
BEGIN
IF t >= sheet[sheet.current].time
THEN BEGIN
IF sheet.current = sheet.length-1 THEN RETURN[sheet.current];
WHILE t >= sheet[sheet.current+1].time DO
sheet.current ← sheet.current+1;
IF sheet.current = sheet.length-1 THEN RETURN[sheet.current];
ENDLOOP;
RETURN[sheet.current];
END
ELSE BEGIN
WHILE sheet.current # 0 AND t < sheet[sheet.current].time DO
sheet.current ← sheet.current -1;
ENDLOOP;
RETURN[sheet.current];
END;
END;
-- ****************************************************************************
-- drawing the sheet
-- ****************************************************************************
HiLite: PUBLIC PROCEDURE[sheet: SheetPTR, texture: CARDINAL, t1, t2: Time] =
BEGIN
OPEN Graphics;
x1, y1, x2, y2, lastY: INTEGER;
offset: INTEGER ← IF texture = white THEN 58 ELSE 40;
section1, section2, i, j: CARDINAL;
IF t1 >= t2 THEN RETURN;
-- EnableClipping[sheet.context];
[] ← Graphics.SetPaintMode[sheet.context, IF texture = white THEN opaque ELSE invert];
Graphics.SetStipple[sheet.context, texture];
i ← Sheet.FindLine[sheet, t1];
j ← Sheet.PriorLine[sheet, i];
IF i = j THEN lastY ← 100 ELSE lastY ← sheet[j].y;
j ← section1 ← FindSection[sheet, t1];
section2 ← FindSection[sheet, t2];
FOR i IN [section1..section2] DO
IF sheet[i].time > sheet.endTime THEN EXIT;
IF i # section2 AND sheet[i].time = sheet[i+1].time THEN LOOP;
IF texture # white AND i # section2 AND sheet[i].y = sheet[i+1].y THEN LOOP;
offset ← sheet[i].staves.offset;
IF texture = white THEN offset ← offset/2 ELSE offset ← offset/4;
[x1, y1] ← Sheet.Map[sheet, MAX[t1, sheet[j].time], , 0];
[x2, y2] ← Sheet.Map[sheet, MIN[t2, sheet[i+1].time-1], , sheet[i].staves.length-1];
y1 ← y1+offset+34; y2 ← y2-offset;
IF x2 < sheet.width-1 OR texture = white THEN x2 ← x2+1 ELSE x2 ← x2+16;
IF texture = white AND sheet[i].y # lastY AND x1 = sheet[i].x THEN x1 ← -25;
lastY ← sheet[i].y;
IF texture = white THEN {
Graphics.SetStipple[sheet.context, white];
[] ← Graphics.SetPaintMode[sheet.context, opaque]};
Graphics.DrawBox[sheet.context, [x1, y1, x2, y2]];
IF texture = white THEN DrawSection[sheet, sheet[i], x1, x2];
j ← i+1;
ENDLOOP;
-- EnableClipping[sheet.context];
[] ← Graphics.SetPaintMode[sheet.context, transparent];
END;
Draw: PUBLIC PROCEDURE[sheet: SheetPTR] =
BEGIN
x: INTEGER;
j: CARDINAL;
staves: StavesPTR = sheet[0].staves;
sheetHeight: INTEGER = staves.staff[staves.length-1].y;
IF sheet.begin = 0 THEN Utility.DrawLine[sheet.context, -23, 0, -23, sheetHeight];
sheet.endTime ← sheet.begin;
IF sheet.scale = 2 THEN x ← 3 ELSE x ← 2*sheet.scale;
FOR j ← Sheet.FindLine[sheet, sheet.begin], Sheet.NextLine[sheet, j] DO
IF sheet[j].y-sheet.top < (-650*x)/2 THEN {sheet.endTime ← sheet[j].time; EXIT};
DrawSection[sheet, sheet[j], -25, sheet.width];
ENDLOOP;
END;
DrawSection: PROCEDURE[sheet: SheetPTR, l: Section, start, length: INTEGER] =
BEGIN
sheetHeight: INTEGER ← l.staves.staff[l.staves.length-1].y;
i: CARDINAL;
y: INTEGER ← l.y-sheet.top;
context: Graphics.Context ← sheet.context;
oldStaff ← [0, 0];
Graphics.SetStipple[context, black];
IF start = -25 THEN Utility.DrawLine[context, start, y, start, y+sheetHeight];
IF start = -25 AND sheet.accidental THEN Sheet.DrawKey[sheet, l.key, 0, l.time];
IF l.page > 1 THEN BEGIN
s: STRING ← [5];
String.AppendDecimal[s, l.page];
Graphics.SetCP[context, sheet.width+10, y+30];
Utility.SetFont[context, text, 12];
Utility.DrawString[context, s];
Utility.SetFont[context, music, 8];
END;
IF sheet.scale > 3 THEN
BEGIN
-- Graphics.SetLineWidth[context, 8];
IF length = sheet.width THEN Utility.DrawLine[context, length, y, length, y+sheetHeight];
IF NOT sheet.notehead THEN Graphics.SetStipple[context, grey];
Utility.DrawLine[context, start, y, length, y];
Utility.DrawLine[context, start, y+sheetHeight, length, y+sheetHeight];
Graphics.SetStipple[context, black];
-- Graphics.SetLineWidth[context, 4];
END
ELSE FOR i IN [0..l.staves.length) DO
DrawStaff[sheet, l.staves.staff[i], y, start, length]
ENDLOOP;
END;
DrawKey: PUBLIC PROCEDURE[sheet: SheetPTR, key, oldKey: INTEGER, time: Time] =
BEGIN
n: INTEGER = 8;
oldStaff: Staff ← [0, 0];
x, y, offset: INTEGER;
context: Graphics.Context ← sheet.context;
l: CARDINAL ← Sheet.FindLine[sheet, time];
staves: StavesPTR ← sheet[l].staves;
IF NOT sheet.accidental THEN RETURN;
IF time IN [sheet[l].time-10..sheet[l].time) THEN RETURN;
IF time IN (sheet[l].time..sheet[l].time+15] THEN time ← sheet[l].time;
x ← Sheet.MapHeight[sheet, time, 0].x;
FOR i: CARDINAL IN [0..staves.length) DO
IF staves.staff[i] = oldStaff THEN LOOP;
oldStaff ← staves.staff[i];
offset ← Sheet.Height[sheet, time, oldStaff.pitch, i];
IF Mod[staves.staff[i].pitch, 12] = 3 THEN offset ← offset - 8;
[x, y] ← Sheet.MapHeight[sheet, time, offset];
IF sheet[l].time = time THEN x ← 3;
IF key > 0 THEN SlapDown[context, x , 32+y, sharp];
IF key > 1 THEN SlapDown[context, x+ n, 20+y, sharp];
IF key > 2 THEN SlapDown[context, x+2*n, 36+y, sharp];
IF key > 3 THEN SlapDown[context, x+3*n, 24+y, sharp];
IF key > 4 THEN SlapDown[context, x+4*n, 12+y, sharp];
IF key > 5 THEN SlapDown[context, x+5*n, 28+y, sharp];
IF key > 6 THEN SlapDown[context, x+6*n, 16+y, sharp];
IF key < 0 THEN SlapDown[context, x , 16+y, flat];
IF key < -1 THEN SlapDown[context, x+ n, 28+y, flat];
IF key < -2 THEN SlapDown[context, x+2*n, 12+y, flat];
IF key < -3 THEN SlapDown[context, x+3*n, 24+y, flat];
IF key < -4 THEN SlapDown[context, x+4*n, 8+y, flat];
IF key < -5 THEN SlapDown[context, x+5*n, 20+y, flat];
IF key < -6 THEN SlapDown[context, x+6*n, 4+y, flat];
ENDLOOP;
END;
SlapDown: PROCEDURE[context: Graphics.Context, x, y: INTEGER, a: Accidental] =
INLINE BEGIN
Graphics.SetCP[context, x, y];
SELECT a FROM
flat => Utility.DrawChar[context, 111C];
natural => Utility.DrawChar[context, 112C];
sharp => Utility.DrawChar[context, 113C];
ENDCASE;
END;
DrawStaff: PROCEDURE[sheet: SheetPTR, s: Staff, y, start, length: INTEGER] =
BEGIN
w: REAL ← 0;
i: CARDINAL;
staffY: INTEGER;
texture: CARDINAL;
context: Graphics.Context ← sheet.context;
color: Graphics.Color ← Graphics.GetColor[context];
SELECT color.tag FROM
rgb => texture ← IF color.r = 0 THEN black ELSE white;
ENDCASE => texture ← color.g*256 + color.b;
IF s = oldStaff THEN RETURN;
oldStaff ← s;
y ← y + s.y;
Graphics.SetStipple[context, IF sheet.notehead THEN black ELSE grey];
IF sheet.printing THEN w ← w/2;
staffY ← y;
FOR i IN [0..5) DO
Graphics.DrawBox[context, [start, staffY-w, length, staffY+w]];
staffY ← staffY + 8;
ENDLOOP;
Graphics.SetStipple[context, black];
IF start < 0 THEN [] ← Graphics.SetPaintMode[context, transparent];
IF (s.pitch = 27 OR s.pitch = 15) AND start < 0 THEN {
Graphics.SetCP[context, -21, y+16];
Utility.DrawChar[context, 121C]};
IF (s.pitch = 48 OR s.pitch = 60) AND start < 0 THEN {
Graphics.SetCP[context, -21, y+16];
Utility.DrawChar[context, 120C]};
Graphics.SetStipple[context, texture];
END;
oldStaff: Staff;
d: CARDINAL = 8;
DrawClef: PUBLIC PROCEDURE[sheet: SheetPTR, pitch, staff: INTEGER, time: Time] =
BEGIN
x, y: INTEGER;
l: CARDINAL ← Sheet.FindLine[sheet, time];
IF time IN (sheet.section[l].time..sheet.section[l].time+15] THEN time ← sheet.section[l].time;
[x, y] ← Sheet.Map[sheet, time, , staff];
IF sheet.section[l].time = time THEN x ← -21;
Graphics.SetCP[sheet.context, x, y+16];
IF sheet.voice # noVoice THEN Graphics.SetStipple[sheet.context, light];
SELECT TRUE FROM
x = -21 AND pitch = 27 => Utility.DrawChar[sheet.context, 121C];
x = -21 AND pitch = 48 => Utility.DrawChar[sheet.context, 120C];
pitch = 27 => Utility.DrawChar[sheet.context, 131C];
pitch = 48 => Utility.DrawChar[sheet.context, 130C];
ENDCASE;
END;
DrawOctava: PUBLIC PROCEDURE[sheet: SheetPTR, pitch, staff, height: INTEGER, t1, t2: Time] =
BEGIN
x1, x2, y: INTEGER ← 0;
section1, section2: CARDINAL;
IF t1 > t2 THEN RETURN;
IF pitch # 60 AND pitch # 15 THEN RETURN;
height ← Sheet.OctavaHeight[pitch, height];
Utility.SetFont[sheet.context, text, 12];
section1 ← Sheet.FindSection[sheet, t1];
section2 ← Sheet.FindSection[sheet, t2];
IF sheet.voice # noVoice THEN Graphics.SetStipple[sheet.context, light];
FOR i: CARDINAL IN [section1..section2] DO
IF i+1 = sheet.length THEN EXIT;
IF sheet.section[i].time < sheet.begin THEN LOOP;
IF sheet.section[i].time >= sheet.endTime THEN EXIT;
IF sheet.section[i].time = sheet.section[i+1].time THEN LOOP;
[x1, ] ← Sheet.Map[sheet, MAX[t1, sheet.section[i].time], , staff];
[x2, y] ← Sheet.Map[sheet, MIN[t2, sheet.section[i+1].time-1], , staff];
IF x1 > sheet.width-15 AND i = section1 THEN LOOP;
x2 ← x2+1; y ← y+height;
IF x1 < 10 OR i = section1 THEN BEGIN
Graphics.SetCP[sheet.context, x1, y-8];
Utility.DrawChar[sheet.context, '8];
x1 ← x1+7; END;
IF x1 < x2 THEN DrawDottedLine[sheet, x1, y, x2, y];
IF i = section2 THEN Utility.DrawLine[sheet.context, x2, y, x2, y-(IF height > 0 THEN 7 ELSE -7)];
ENDLOOP;
Utility.SetFont[sheet.context, music, 8];
END;
DrawDottedLine: PROCEDURE[sheet: SheetPTR, x1, y, x2, y2: INTEGER] =
INLINE BEGIN
x: INTEGER;
IF NOT sheet.printing THEN BEGIN
Graphics.SetStipple[sheet.context, 146314B];
Utility.DrawLine[sheet.context, x1, y, x2, y];
Graphics.SetStipple[sheet.context, black];
RETURN; END;
x ← x1;
WHILE x < x2 DO
Utility.DrawLine[sheet.context, x, y, MIN[x+3, x2], y];
x ← x+5;
ENDLOOP;
END;
-- ****************************************************************************
-- changing the view on the sheet
-- ****************************************************************************
-- top: PUBLIC INTEGER ← 0;
SetBegin: PUBLIC PROCEDURE[sheet: SheetPTR, now: Time] =
BEGIN
l: Section ← sheet[Sheet.FindLine[sheet, now]];
sheet.begin ← l.time;
sheet.top ← l.y;
END;
Scroll: PUBLIC PROCEDURE[sheet: SheetPTR, lines: INTEGER] =
BEGIN
section: CARDINAL ← Sheet.FindLine[sheet, sheet.begin];
IF lines < 0
THEN FOR i: CARDINAL IN [0..ABS[lines]) DO
IF section = 0 THEN EXIT;
section ← Sheet.PriorLine[sheet, section];
ENDLOOP
ELSE FOR i: CARDINAL IN [0..ABS[lines]) DO
section ← Sheet.NextLine[sheet, section];
IF section = sheet.length THEN RETURN;
ENDLOOP;
Sheet.SetBegin[sheet, sheet[section].time];
sheet.dirty1 ← 0;
sheet.dirty2 ← LAST[Time];
END;
Scale: PUBLIC PROCEDURE[score: ScorePTR, newScale: INTEGER] =
BEGIN
one: REAL = 1;
two: REAL = 2;
sheet: SheetPTR ← score.sheet;
context: Graphics.Context ← sheet.context;
IF sheet.scale = newScale THEN RETURN;
-- reset to 1
SELECT sheet.scale FROM
2 => Graphics.Scale[context, 3/two, 3/two];
4 => Graphics.Scale[context, 4, 4];
ENDCASE;
sheet.width ← 550;
-- now move to the desired scale
SELECT newScale FROM
1 => {sheet.scale ← 1;
sheet.width ← sheet.width};
-- Graphics.SetLineWidth[context, 1]};
2 => {sheet.scale ← 2; -- actually 3/2
sheet.width ← (3*sheet.width)/2;
Graphics.Scale[context, two/3, two/3]};
-- Graphics.SetLineWidth[context, 2]};
4 => {sheet.scale ← 4;
sheet.width ← 4*sheet.width;
Graphics.Scale[context, one/4, one/4]};
-- Graphics.SetLineWidth[context, 4]};
ENDCASE;
Sheet.Reset[score];
-- Sheet.SetBegin[score, begin];
sheet.dirty1 ← 0;
sheet.dirty2 ← LAST[Time];
END;
END..