CDVDraw.mesa
Copyright © 1983, 1985 by Xerox Corporation. All rights reserved.
Christian Jacobi, August 5, 1983 11:07 am
last edited by Christian Jacobi, May 1, 1985 10:40:38 am PDT
DIRECTORY
Basics USING [BITSHIFT, bitsPerWord, LongMult],
CD,
CDApplications,
CDBasics,
CDColors,
CDOps,
CDOrient,
CDPrivate,
CDTexts,
CDValue,
CDVPrivate,
CDVScale,
Graphics,
GraphicsBasic,
GraphicsColor,
GraphicsOps,
PrincOps USING [BBptr, BitAddress, BBTable, SrcDesc, BitBltFlags, GrayParm],
PrincOpsUtils USING [BITBLT],
Real,
Rope,
RuntimeError,
Terminal,
TerminalExtras,
ViewerClasses,
ViewerSpecs;
CDVDraw: CEDAR MONITOR
IMPORTS Basics, CD, CDApplications, CDBasics, CDOps, CDOrient, CDValue, CDVPrivate, CDVScale, Graphics, GraphicsOps, PrincOpsUtils, Real, RuntimeError, Terminal, TerminalExtras
EXPORTS CDVPrivate, CD --DrawRef's private fields-- =
BEGIN
ViewerPrivateRep: PUBLIC TYPE = CDVPrivate.MyGraphicRec;
ViewerSaveRep: PUBLIC TYPE = INTEGER; -- XXX placeholder
MyGraphicRef: TYPE = CDVPrivate.MyGraphicRef;
MyGraphicRec: TYPE = CDVPrivate.MyGraphicRec;
maxInfluence: CD.DesignNumber = 0;
virtual: Terminal.Virtual = Terminal.Current[];
defaultFont: Graphics.FontRef = GraphicsOps.DefaultFont[];
errrorReport: RECORD [
text: Rope.ROPE,
me: MyGraphicRef,
r: CD.Rect,
color: REF CDColors.Brick
];
bitBltFlagsForPaint: PrincOps.BitBltFlags = PrincOps.BitBltFlags[
direction: forward,
disjoint: TRUE,
disjointItems: TRUE,
gray: TRUE,
srcFunc: null,
dstFunc: or
];
bitBltFlagsForOutline: PrincOps.BitBltFlags = PrincOps.BitBltFlags[
direction: forward,
disjoint: TRUE,
disjointItems: TRUE,
gray: TRUE,
srcFunc: null,
dstFunc: or
];
bitBltFlagsForClearPattern: PrincOps.BitBltFlags = PrincOps.BitBltFlags[
direction: forward,
disjoint: TRUE,
disjointItems: TRUE,
gray: TRUE,
srcFunc: complement,
dstFunc: and
];
blackBrick: REF CDColors.Brick = NEW[CDColors.Brick←ALL[LAST[CARDINAL]]];
whiteBrick: REF CDColors.Brick = NEW[CDColors.Brick←ALL[0]];
BitBlitDraw: PROC[me: MyGraphicRef, r: CD.Rect, color: REF CDColors.Brick] =
TRUSTED
-- r ALLREADY CLIPPED
INLINE BEGIN
ENABLE {
UNWIND => NULL;
RuntimeError.UNCAUGHT => {
errrorReport.text ← "BitBlitDraw";
errrorReport.me ← me;
errrorReport.r←r;
errrorReport.color𡤌olor;
IF CDVPrivate.catchAny THEN GOTO SomeError ELSE REJECT;
};
};
xBit, x1, x2, y1, y2: CARDINAL;
vr: CD.Rect;
--DONE OUTSIDE r ← CDBasics.Intersection[r, me.deviceDrawRef.interestClip];
IF ~CDBasics.NonEmpty[r] THEN RETURN;
vr ← CDVScale.DesignToViewerRect[me.scale, r];
x1 ← MAX[vr.x1, 0];
y1 ← MAX[vr.y1, 0];
IF vr.x2<=0 OR vr.y2<=0 THEN RETURN;
x2 ← MIN[vr.x2, me.viewer.cw];  
y2 ← MIN[vr.y2, me.viewer.ch];
IF x1>=x2 OR y1>=y2 THEN RETURN;
xBit ← Basics.BITSHIFT[x1+me.vx, me.logbpp];
me.pBBptr.width ← Basics.BITSHIFT[x2-x1, me.logbpp];
me.pBBptr.height ← y2-y1;
y1 ← me.vy-y2;
me.pBBptr.dst ← [
me.screen
+ Basics.LongMult[y1, me.scWidthWords]
+ LONG[(xBit/Basics.bitsPerWord)],,
xBit MOD Basics.bitsPerWord
];
me.pBBptr.src ← [
LOOPHOLE[
LOOPHOLE[color, LONG CARDINAL] +
y1 MOD 4, LONG POINTER],,
xBit MOD Basics.bitsPerWord];
me.pBBptr.srcDesc.gray.yOffset ← y1 MOD 4;
IF me.bpp=1 THEN PrincOpsUtils.BITBLT[me.pBBptr]
ELSE {
TerminalExtras.LockColorFrame[vt: virtual,
xmin: x1+me.vx,
ymin: y1,
xmax: x2+me.vx,
ymax: y1+me.pBBptr.height
];
PrincOpsUtils.BITBLT[me.pBBptr];
TerminalExtras.UnlockColorFrame[virtual]
};
EXITS
SomeError => NULL;
END;
BitBlitSave: ENTRY PROC[me: MyGraphicRef, r: CD.Rect, color: REF CDColors.Brick, clearPattern: REF CDColors.Brick←NIL] =
TRUSTED BEGIN
ENABLE {
UNWIND => NULL;
RuntimeError.UNCAUGHT => {
errrorReport.text ← "BitBlitSave";
errrorReport.me ← me;
errrorReport.r←r;
errrorReport.color𡤌olor;
IF CDVPrivate.catchAny THEN GOTO SomeError ELSE REJECT;
};
};
xBit, x1, x2, y1, y2: CARDINAL;
vr: CD.Rect;
r ← CDBasics.Intersection[r, me.deviceDrawRef.interestClip];
IF ~CDBasics.NonEmpty[r] THEN RETURN;
vr ← CDVScale.DesignToViewerRect[me.scale, r];
x1 ← MAX[vr.x1, 0];
y1 ← MAX[vr.y1, 0];
IF vr.x2<=0 OR vr.y2<=0 THEN RETURN;
x2 ← MIN[vr.x2, me.viewer.cw];
y2 ← MIN[vr.y2, me.viewer.ch];
IF x1>=x2 OR y1>=y2 THEN RETURN;
xBit ← Basics.BITSHIFT[x1+me.vx, me.logbpp];
me.xBBptr.width ← Basics.BITSHIFT[x2-x1, me.logbpp];
me.xBBptr.height ← y2-y1;
y1 ← me.vy-y2; -- now y1 is device top pixel
me.xBBptr.srcDesc ← PrincOps.SrcDesc[gray[PrincOps.GrayParm[
yOffset: y1 MOD 4,
widthMinusOne: 0, --words
heightMinusOne: 3 --lines
]]];
me.xBBptr.dst ← [
me.screen
+ Basics.LongMult[y1, me.scWidthWords]
+ LONG[(xBit/Basics.bitsPerWord)],,
xBit MOD Basics.bitsPerWord
];
IF me.bpp#1 THEN TerminalExtras.LockColorFrame[vt: virtual,
xmin: x1+me.vx,
ymin: y1,
xmax: x2+me.vx,
ymax: y1+me.xBBptr.height
];
IF clearPattern#NIL THEN {
me.xBBptr.flags ← bitBltFlagsForClearPattern;
me.xBBptr.src ← [
LOOPHOLE[
LOOPHOLE[clearPattern, LONG CARDINAL] +
y1 MOD 4, LONG POINTER],,
xBit MOD Basics.bitsPerWord];
PrincOpsUtils.BITBLT[me.xBBptr];
};
me.xBBptr.src ← [
LOOPHOLE[
LOOPHOLE[color, LONG CARDINAL] +
y1 MOD 4, LONG POINTER],,
xBit MOD Basics.bitsPerWord];
me.xBBptr.flags ← bitBltFlagsForOutline;
PrincOpsUtils.BITBLT[me.xBBptr];
IF me.bpp#1 THEN TerminalExtras.UnlockColorFrame[virtual];
EXITS
SomeError => NULL;
END;
BitBlitOutLine: ENTRY PROC[r: CD.Rect, pr: CD.DrawRef] =
BEGIN
ENABLE {
UNWIND => NULL;
RuntimeError.UNCAUGHT => {
errrorReport.text ← "BitBlitOutLine";
errrorReport.me ← pr.viewerPrivate;
errrorReport.r←r;
IF CDVPrivate.catchAny THEN GOTO SomeError ELSE REJECT;
};
};
me: MyGraphicRef ~ pr.viewerPrivate;
vr, clipr: CD.Rect;
DrawBlack: --INTERNAL-- PROC[me: MyGraphicRef, x1, y1, x2, y2: INTEGER] =
--draw exclusive high border pixels
TRUSTED INLINE BEGIN
--no empty test; is guaranteed by caller
xBits: CARDINAL ~ Basics.BITSHIFT[x1+INTEGER[me.vx], me.logbpp];
me.xBBptr.width ← Basics.BITSHIFT[x2-x1, me.logbpp];
me.xBBptr.height ← y2-y1;
me.xBBptr.dst ← [
me.screen
+ Basics.LongMult[INTEGER[me.vy]-y2, me.scWidthWords]
+ LONG[(xBits/Basics.bitsPerWord)],,
xBits MOD Basics.bitsPerWord
];
PrincOpsUtils.BITBLT[me.xBBptr];
END;
--clip to area where
--1) small enogh: it does not crash on scaling later
--2) big enogh: if drawn, it does not draw artificial lines
IF r.x2>me.dClip.x2 THEN r.x2←me.dClip.x2+1;
IF r.y2>me.dClip.y2 THEN r.y2←me.dClip.y2+1;
IF r.x1<me.dClip.x1 THEN r.x1←me.dClip.x1-1;
IF r.y1<me.dClip.y1 THEN r.y1←me.dClip.y1-1;
vr ← CDVScale.DesignToViewerRect[me.scale, r];
clipr ← CDBasics.Intersection[
CDVScale.DesignToViewerRect[me.scale, me.deviceDrawRef.interestClip],
vr
];
clipr.x1 ← MAX[clipr.x1, 0];
clipr.y1 ← MAX[clipr.y1, 0];
clipr.x2 ← MIN[clipr.x2, LONG[me.viewer.cw]];
clipr.y2 ← MIN[clipr.y2, LONG[me.viewer.ch]];
IF clipr.x1>=clipr.x2 OR clipr.y1>=clipr.y2 THEN RETURN;
--harder empty test than CDBasics
--necessary because we add/subtract one
TRUSTED {
me.xBBptr.src ← [LOOPHOLE[blackBrick, LONG POINTER],,0];
me.xBBptr.flags ← bitBltFlagsForOutline;
me.xBBptr.srcDesc ← PrincOps.SrcDesc[gray[PrincOps.GrayParm[
yOffset: 0,
widthMinusOne: 0, --words
heightMinusOne: 0 --lines
]]];
};
IF me.bpp#1 THEN {
TerminalExtras.LockColorFrame[vt: virtual,
xmin: clipr.x1+me.vx,
ymin: me.vy+1-clipr.y2,
xmax: clipr.x2+me.vx-1,
ymax: me.vy-clipr.y1
];
};
IF vr.x1>=clipr.x1 THEN DrawBlack[me, clipr.x1, clipr.y1, clipr.x1+1, clipr.y2]; --left
IF vr.y2<=clipr.y2 THEN DrawBlack[me, clipr.x1, clipr.y2-1, clipr.x2, clipr.y2]; --top
IF vr.x2<=clipr.x2 THEN DrawBlack[me, clipr.x2-1, clipr.y1, clipr.x2, clipr.y2]; --right
IF vr.y1>=clipr.y1 THEN DrawBlack[me, clipr.x1, clipr.y1, clipr.x2, clipr.y1+1]; --bottom
IF me.bpp#1 THEN TerminalExtras.UnlockColorFrame[virtual];
EXITS
SomeError => NULL;
END;
BitBlitDrawRectForViewers: ENTRY PROC[r: CD.Rect, l: CD.Layer, pr: CD.DrawRef] =
BEGIN
ENABLE UNWIND => NULL;
me: MyGraphicRef = pr.viewerPrivate;
BitBlitDraw[me, CDBasics.Intersection[r, pr.interestClip], me.colorTable[l]];
END;
SaveRectForViewers: ENTRY PROCEDURE[r: CD.Rect, l: CD.Layer, pr: CD.DrawRef] =
BEGIN
ENABLE UNWIND => NULL;
me: MyGraphicRef = pr.viewerPrivate;
IF me.saveList=NIL OR me.saveList.first.next>=CDVPrivate.saveListSize THEN
me.saveList ← CONS[CDVPrivate.SavedRectArraySeq[next: 0], me.saveList];
me.saveList.first.x[me.saveList.first.next] ← [r: CDBasics.Intersection[r, pr.interestClip], l: l];
me.saveList.first.next ← me.saveList.first.next+1
END;
SetGround: --CD.SetGroundProc-- PROC [pr: CD.DrawRef, pushedOut: BOOL] =
BEGIN
WITH pr.devicePrivate SELECT FROM
me: MyGraphicRef =>
me.colorTable ← me.personalColors[me.display][IF pushedOut THEN back ELSE normal]
ENDCASE => NULL;
END;
DrawCommentForViewers: PUBLIC PROC[r: CD.DesignRect, comment: Rope.ROPE, pr: CD.DrawRef] =
BEGIN
topToFontLine: NAT ~ 9+2;
fontHeight: NAT ~ 12;
leftMargin: NAT ~ 2;
bothMargin: NAT ~ 2*leftMargin;
me: MyGraphicRef ← NARROW[pr.devicePrivate];
vr: CD.Rect ← CDVScale.DesignToViewerRect[me.scale, r];
IF vr.y2-vr.y1>fontHeight THEN {
xw: REAL ~ Graphics.RopeWidth[font: defaultFont, rope: comment].xw;
IF vr.x2-vr.x1>xw+bothMargin THEN {
me.viewContext.SetColor[Graphics.black];
me.viewContext.SetCP[vr.x1+leftMargin, vr.y2-topToFontLine];
me.viewContext.DrawRope[comment];
}
};
END;
RepaintRectAreaInViewer: PUBLIC PROC[me: MyGraphicRef, rect: CD.DesignRect, eraseFirst: BOOL] =
BEGIN
ENABLE {
RuntimeError.UNCAUGHT => IF CDVPrivate.catchAny THEN GOTO SomeError ELSE REJECT
};
interestRect: CD.DesignRect;
UpdateDrawInformation[me];
me.deviceDrawRef.interestClip ← interestRect ← CDBasics.Intersection[rect, me.dClip];
IF CDBasics.NonEmpty[interestRect] THEN {
[] ← Graphics.SetPaintMode[me.viewContext, transparent];
RepaintBackground[me, interestRect, eraseFirst];
CDOps.QuickDrawDesign[me.actualDesign, me.deviceDrawRef];
[] ← Graphics.SetPaintMode[me.viewContext, opaque];
IF me.saveList#NIL THEN {
l: CDVPrivate.SaveList ← me.saveList;
me.saveList ← NIL;
WHILE l#NIL DO
FOR i: INTEGER IN [0..l.first.next) DO
BitBlitSave[me, l.first.x[i].r, me.colorTable[l.first.x[i].l], me.greyTable[l.first.x[i].l]];
ENDLOOP;
l ← l.rest
ENDLOOP;
};
FOR pl: CDVPrivate.PainterList ← me.painterList, pl.rest WHILE pl#NIL DO
IF CDBasics.Intersect[interestRect, pl.first.rect] THEN
pl.first.proc[me, pl.first, interestRect !
RuntimeError.UNCAUGHT => IF CDVPrivate.catchAny THEN CONTINUE ELSE REJECT
]
ENDLOOP
}
EXITS SomeError=> NULL
END;
RepaintBackground: PUBLIC ENTRY PROC[me: MyGraphicRef, r: CD.DesignRect, eraseFirst: BOOL] =
BEGIN
ENABLE {
UNWIND => NULL;
RuntimeError.UNCAUGHT => {
errrorReport.text ← "background";
errrorReport.me ← me;
errrorReport.r ← r;
IF CDVPrivate.catchAny THEN GOTO SomeError ELSE REJECT
};
};
DrawOutside: --INTERNAL-- PROC [r: CD.DesignRect] =
BEGIN
BitBlitDraw[me, CDBasics.Intersection[r, me.dClip], me.colorTable[CD.backGround]];
END;
--RepaintBackground
IF eraseFirst THEN {
TRUSTED {me.pBBptr.flags.dstFunc ← null};
BitBlitDraw[me, CDBasics.Intersection[r, me.dClip], whiteBrick];
TRUSTED {me.pBBptr.flags.dstFunc ← or};
};
IF me.actualDesign.actual.first.mightReplace#NIL AND me.environment AND
me.actualDesign.actual.first.mightReplace.ob#NIL THEN
CDBasics.DecomposeRect[r: r,
test: CDApplications.ARectO[me.actualDesign.actual.first.mightReplace],
outside: DrawOutside
];
EXITS
SomeError => NULL;
END;
InitForBBLT: PROC [me: MyGraphicRef] =
BEGIN
x, y: REAL;
scWidthBits: INTEGER;
IF me.viewer.column=color THEN TRUSTED {
m: Terminal.ColorMode = Terminal.GetColorMode[virtual];
IF m.full OR m.bitsPerPixelChannelA=0 THEN ERROR CDVPrivate.notSupportedColorMode;
me.bpp ← m.bitsPerPixelChannelA;
me.screen ← virtual.colorBitmapA;
me.scWidth ← virtual.colorWidth; -- pixels
scWidthBits ← virtual.colorWidth*me.bpp;
me.scHeight ← virtual.colorHeight;
IF me.bpp=4 THEN {
me.display ← bit4;
me.logbpp ← 2;
}
ELSE IF me.bpp=8 THEN {
me.display ← bit8;
me.logbpp ← 3;
}
ELSE ERROR;
}
ELSE { -- b+w
me.display ← bw;
me.bpp ← 1;
me.logbpp ← 0;
me.scWidth ← ViewerSpecs.screenW/Basics.bitsPerWord;
scWidthBits ← ViewerSpecs.screenW;
me.scHeight ← ViewerSpecs.screenH;
me.screen ← Terminal.GetBitBltTable[virtual].bbt.dst.word;
};
me.colorTable ← me.personalColors[me.display][normal];
me.greyTable ← me.personalColors[me.display][grey];
[x, y] ← GraphicsOps.UserToDevice[me.viewContext, 0, 0]; -- in device in pixels
me.vx ← Real.RoundI[x]; --distance from left of screen to left most pixel
me.vy ← Real.RoundI[y]; --distance from top of screen to bottom most pixel
me.scWidthWords ← scWidthBits/Basics.bitsPerWord;
----------
--fixed pBBptr initializations
--done in viewerprocess: me.pBBptr ← PrincOpsUtils.AlignedBBTable[@bBTableSpace];
TRUSTED BEGIN
me.pBBptr^ ← PrincOps.BBTable[
dst: TRASH,
dstBpl: scWidthBits,
src: TRASH,
srcDesc: PrincOps.SrcDesc[gray[PrincOps.GrayParm[
yOffset: 0, --is actually trash
widthMinusOne: 0, --words
heightMinusOne: 3 --lines
]]],
width: TRASH,
height: TRASH,
flags: bitBltFlagsForPaint
];
me.xBBptr^ ← me.pBBptr^;
END;
END;
SetupContext: PROC [pr: CD.DrawRef, me: MyGraphicRef] =
BEGIN
s: REAL;
pr.deviceContext ← Graphics.CopyContext[me.viewContext];
s ← CDVScale.DesignToViewerFactor[me.scale];
pr.scaleHint ← s*me.suppressFactorForCells;
pr.b1 ← me.b1;
pr.b2 ← me.b2;
Graphics.Scale[pr.deviceContext, s, s];
Graphics.Translate[pr.deviceContext, -me.scale.off.x, -me.scale.off.y];
END;
InternalCreateDrawInformation: INTERNAL PROC [me: MyGraphicRef] =
BEGIN
pr: CD.DrawRef = CD.CreateDrawRef[me.actualDesign];
me.deviceDrawRef ← pr;
pr.contextFilter ← me.contextFilter ← GetContextFilter[me];
pr.drawRect ← BitBlitDrawRectForViewers;
pr.outLineProc ← BitBlitOutLine;
pr.drawComment ← DrawCommentForViewers;
pr.setGround ← SetGround;
pr.devicePrivate ← me;
pr.stopFlag ← me.stoprequest;
pr.symbolics ← me.symbolics;
pr.environment ← me.environment;
pr.interestClip ← me.dClip ← CDVScale.GetClipRecord[me.scale, me.viewer.cw, me.viewer.ch];
pr.minimalSize ← --XXX--
MIN[(me.dClip.x2-me.dClip.x1)/8, (me.dClip.y2-me.dClip.y1)/8];
InitForBBLT[me];
pr.saveRect ← (IF me.display=bit8 THEN BitBlitDrawRectForViewers ELSE SaveRectForViewers);
pr.viewerPrivate ← me; -- this line last! it tells DummyNotify that the rest is initialized
END;
PROCEDURE NOT USED because Qickdraw gives us no chance
NamedDrawChild: PROC [aptr: CD.ApplicationPtr, pos: CD.DesignPosition, orient: CD.Orientation,
pr: CD.DrawRef] =
BEGIN
Comment: PROC [comment: Rope.ROPE] =
BEGIN
me: MyGraphicRef ← NARROW[pr.devicePrivate];
vpos: CD.Position = CDVScale.DesignToViewerPosition[me.scale, pos];
pr.deviceContext.SetColor[Graphics.black];
pr.deviceContext.SetCP[vpos.x+1, vpos.y+1];
pr.deviceContext.DrawRope[comment];
END;
aptr.ob.p.drawMe[aptr, pos, orient, pr];
IF pr.b2 THEN {
WITH CDProperties.GetPropFromApplication[from: aptr, prop: $SignalName] SELECT FROM
r: Rope.ROPE => Comment[r];
a: ATOM => Comment[Atom.GetPName[a]];
ENDCASE => NULL;
};
END;
UpdateDrawInformation: ENTRY PROC [me: MyGraphicRef] =
BEGIN
ENABLE UNWIND => NULL;
IF me.deviceDrawRef=NIL THEN
InternalCreateDrawInformation[me];
SetupContext[me.deviceDrawRef, me];
END;
CreateDrawInformation: PUBLIC ENTRY PROC [me: MyGraphicRef] =
BEGIN
ENABLE UNWIND => NULL;
InternalCreateDrawInformation[me];
SetupContext[me.deviceDrawRef, me];
END;
GetContextFilter: PROC [me: MyGraphicRef] RETURNS [REF CD.ContextFilter] =
BEGIN
WITH CDValue.Fetch[boundTo: me.actualDesign.technology, key: $ContextFilter, propagation: global] SELECT FROM
rcf: REF CD.ContextFilter => RETURN [rcf];
ENDCASE => RETURN [defaultContextFilter]
END;
defaultContextFilter: REF CD.ContextFilter = NEW[CD.ContextFilter];
FOR l: CD.Layer IN CD.Layer DO
defaultContextFilter[l].paintMode ← transparent;
defaultContextFilter[l].doit ← TRUE;
ENDLOOP;
defaultContextFilter[CD.backGround].doit ← FALSE;
CDValue.EnregisterKey[$ContextFilter];
CDValue.Store[NIL, $ContextFilter, defaultContextFilter];
END.