-- 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..