CDPDPlotImplInDesignCoords.mesa
Copyright © 1983, 1986 by Xerox Corporation. All rights reserved.
History
Black and white versatec plot written by: E. McCreight, August 1, 1983
Changed for color Versatec by: Kim Rachmeler, 1983
Rewritten by: Christian Jacobi, April 11, 1985 3:21:48 pm PST
Last Edited by: Christian Jacobi, December 8, 1986 11:35:18 am PST
DIRECTORY
Atom,
CD,
CDBasics,
CDDefaultProcs,
CDEnvironment,
CDIO,
CDVArrow,
CDCurves,
CDOps,
CDPDPlot,
CDProperties,
CDPropertyTools,
CDSequencer,
CDValue,
GraphicsBasic,
CGArea,
CGClipper,
CGReducer,
Commander,
CStitching,
IO,
PeachPrint,
PDFileFormat,
PDFileWriter,
Process,
Real,
Rope,
RuntimeError USING [UNCAUGHT],
TerminalIO,
ViewerClasses,
ViewerSpecs;
CDPDPlotImpl: CEDAR MONITOR
IMPORTS Atom, CD, CDBasics, CDDefaultProcs, CDEnvironment, CDIO, CDValue, CDVArrow, CDOps, CDCurves, CDProperties, CDPropertyTools, CDSequencer, CGArea, CGClipper, CGReducer, Commander, CStitching, IO, PDFileWriter, PeachPrint, Process, Real, Rope, RuntimeError, TerminalIO
EXPORTS CDPDPlot =
BEGIN
--MONITOR: to protect the stipples
retry: CONDITION;
plottingInProgress: BOOLFALSE;
Enter: ENTRY PROC [d: CD.Design] = {
ENABLE UNWIND => NULL;
BROADCAST retry; --to make others call CheckAborted
WHILE plottingInProgress DO
WAIT retry;
CDSequencer.CheckAborted[d]; --UNWIND IS CATCHED !!!
ENDLOOP;
plottingInProgress ← TRUE
};
Leave: ENTRY PROC [] = {
ENABLE UNWIND => NULL;
plottingInProgress ← FALSE;
BROADCAST retry
};
lastTechPropKey: REF ← $CDxLastTechnology; --a property of the dd.stippleKey
abortPlot: REF BOOLNEW[BOOLFALSE];
--handling colors
Toner: TYPE = PDFileFormat.Toner; -- {black, cyan, magenta, yellow, .. 15};
TonerKeys: TYPE = ARRAY Toner OF REF;
LoadArray: TYPE = ARRAY Toner OF PDFileWriter.LoadReference;
ColorDescription: TYPE = ARRAY CD.Layer OF REF LoadArray ← ALL[NIL];
noColor: PDFileWriter.LoadReference = LAST[PDFileWriter.LoadReference];
-- used to identify tiles without toner so that you can avoid recording them
fullColor: PDFileWriter.LoadReference = noColor-1;
-- used to identify tiles with full toner so that you can avoid recording them
tonerToKeyKeys: REF TonerKeys = NEW[TonerKeys ← ALL[NIL]];
--gives a key to get the keys to get the color stipple as property of the layers
--device description
DeviceDesc: TYPE = CDPDPlot.DeviceDesc;
TaskDesc: TYPE = CDPDPlot.TaskDesc;
PageMode: TYPE = {fixedPage, finiteStripe, anyStripe};
Stipple4: TYPE = ARRAY[0..4) OF [0..16);
Stipple8: TYPE = ARRAY[0..8) OF [0..256);
Stipple16: TYPE = ARRAY[0..16) OF CARDINAL;
fiddleDevice: PUBLIC REF DeviceDesc ← NIL;
error: ERROR = CODE;
MakeDevice: PUBLIC PROC [key: ATOM] RETURNS [dd: REF DeviceDesc] = {
ColorVersatec: PROC [dd: REF DeviceDesc] = {
dd.deviceCode ← last; --does not yet has an assigned value, use "last"
dd.sResolution ← 200; -- resolution (slow directition), pixels / inch
dd.fResolution ← 200; -- resolution (fast direction), pixels / inch
dd.imageFSize ← 8000; --for the wide-bed Versatec, length in the "fast" direction;
dd.toners ← ALL[FALSE];
dd.toners[black] ← TRUE;
dd.toners[cyan] ← TRUE;
dd.toners[magenta] ← TRUE;
dd.toners[yellow] ← TRUE;
dd.bandSSize ← 64; -- number chosen because of buffer-size of versatec
dd.overlap ← 300; -- number of pixels by which to overlap strips
dd.maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
dd.pageMode ← anyStripe;
dd.leftOverMode ← FALSE;
dd.stippleKey ← $CDxVersatec;
dd.name ← "color versatec";
};
InkJetC150: PROC [dd: REF DeviceDesc] = {
dd.deviceCode ← VAL[10];
dd.sResolution ← 120; -- resolution (slow directition), pixels / inch
dd.fResolution ← 120; -- resolution (fast direction), pixels / inch
dd.imageFSize ← 1020;
dd.toners ← ALL[FALSE];
dd.toners[black] ← TRUE;
dd.toners[cyan] ← TRUE;
dd.toners[magenta] ← TRUE;
dd.toners[yellow] ← TRUE;
dd.bandSSize ← 48; --
dd.overlap ← 30; -- number of pixels by which to overlap strips
dd.maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
dd.pageMode ← finiteStripe;
dd.pageSlowSize ← 1020;
dd.leftOverMode ← FALSE;
dd.stippleKey ← $CDxVersatec;
dd.name ← "ink jet";
};
Color400: PROC [dd: REF DeviceDesc] = {
dd.deviceCode ← VAL[9];
dd.sResolution ← 400; -- resolution (slow directition), pixels / inch
dd.fResolution ← 400; -- resolution (fast direction), pixels / inch
dd.imageFSize ← 4096;
dd.toners ← ALL[FALSE];
dd.toners[black] ← TRUE;
dd.toners[cyan] ← TRUE;
dd.toners[magenta] ← TRUE;
dd.toners[yellow] ← TRUE;
dd.bandSSize ← 50; --
dd.overlap ← 30; -- number of pixels by which to overlap strips
dd.maxPixPerLambda ← 240; -- maximum # of pixels to make lambda
dd.pageMode ← fixedPage;
dd.pageSlowSize ← 5500;
dd.leftOverMode ← FALSE;
dd.stippleKey ← $CDxC400;
dd.name ← "Color400";
};
Puffin: PROC [dd: REF DeviceDesc] = {
dd.deviceCode ← PDFileFormat.DeviceCode[puffin];
dd.sResolution ← 384; -- resolution (slow directition), pixels / inch
dd.fResolution ← 384; -- resolution (fast direction), pixels / inch
dd.imageFSize ← dd.fResolution * 17 / 2;
dd.toners ← ALL[FALSE];
dd.toners[black] ← FALSE;
dd.toners[cyan] ← TRUE;
dd.toners[magenta] ← TRUE;
dd.toners[yellow] ← TRUE;
dd.bandSSize ← 16; --
dd.overlap ← 30; -- number of pixels by which to overlap strips
dd.maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
dd.pageMode ← fixedPage;
dd.pageSlowSize ← dd.sResolution*11;
dd.leftOverMode ← TRUE;
dd.stippleKey ← $CDxPuffin;
dd.name ← "Puffin";
};
PlateMaker: PROC [dd: REF DeviceDesc] = {
dd.deviceCode ← PDFileFormat.DeviceCode[last];
dd.sResolution ← 1200; -- resolution (slow directition), pixels / inch
dd.fResolution ← 1200; -- resolution (fast direction), pixels / inch
dd.imageFSize ← dd.fResolution * 17 / 2;
dd.toners ← ALL[FALSE];
dd.toners[black] ← TRUE;
dd.bandSSize ← 16; --
dd.overlap ← 30; -- number of pixels by which to overlap strips
dd.maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
dd.pageMode ← fixedPage;
dd.pageSlowSize ← dd.sResolution*11;
dd.leftOverMode ← TRUE;
dd.stippleKey ← $CDxPDPlotPlatemaker;
dd.name ← "platemaker [color separation]";
};
Raven384: PROC [dd: REF DeviceDesc] = {
dd.deviceCode ← PDFileFormat.DeviceCode[raven];
dd.sResolution ← 384; -- resolution (slow directition), pixels / inch
dd.fResolution ← 384; -- resolution (fast direction), pixels / inch
dd.imageFSize ← dd.fResolution * 17 / 2;
dd.toners ← ALL[FALSE];
dd.toners[black] ← TRUE;
dd.bandSSize ← 16; --
dd.overlap ← 30; -- number of pixels by which to overlap strips
dd.maxPixPerLambda ← 20; -- maximum # of pixels to make lambda
dd.pageMode ← fixedPage;
dd.pageSlowSize ← dd.sResolution*11;
dd.leftOverMode ← TRUE;
dd.stippleKey ← $CDxPDPlotRaven384;
dd.name ← "Raven384 [color separation]";
};
dd ← NEW[DeviceDesc];
dd.contextFilter ← NEW[CD.ContextFilter←ALL[TRUE]];
SELECT key FROM
$Versatec, $PDPlotVersatec => ColorVersatec[dd];
$C150, $PDPlotC150 => InkJetC150[dd];
$Puffin, $PDPlotPuffin => Puffin[dd];
$Platemaker, $PDPlotPlatemaker => PlateMaker[dd];
$Raven384, $PDPlotRaven384 => Raven384[dd];
$Color400, $PDPlotColor400 => Color400[dd];
$UserDevice, $PDPlotUserDevice => dd ← fiddleDevice;
ENDCASE => ERROR error;
};
--state of plot
PlotState: TYPE = REF PlotStateRec;
PlotStateRec: TYPE = RECORD [
tes: ARRAY CD.Layer OF CStitching.Tesselation ← ALL[NIL],
plotScale: REAL ← 1.0, -- pixels per CD.Number
plotClip: CD.Rect ← [0, 0, 0, 0], --in design space touching all geometry in plot
stripeClip: CD.Rect ← [0, 0, 0, 0],--in design space touching all geometry in stripe
bandClip: CD.Rect ← [0, 0, 0, 0],--in design space touching all geometry in band
--plot and stripe areas are defined in integral cd units
--the band is defined in pixel units;
-- bandClip contains at least all area contributing to the real band
pdState: PDFileWriter.PDState,
colorLoads: REF ColorDescription,
anouncedLayer: INT ← -1,
design: CD.Design,
pdy: CARDINAL ← 0,  -- top of band in pixel coords
rightBound: INT 𡤀,
dd: REF DeviceDesc
];
ToDeviceX: PROC[ps: PlotState, cdX: CD.Number] RETURNS [REAL] = INLINE {
--but not clipped!
RETURN [(cdX-ps.stripeClip.x1)*ps.plotScale]
};
ToDeviceY: PROC[ps: PlotState, cdY: CD.Number] RETURNS [REAL] = INLINE {
--Remember that the sign bit changes !!
--but not clipped!
RETURN [(ps.stripeClip.y2-cdY)*ps.plotScale]
};
Plot: PUBLIC PROC [task: REF TaskDesc] = {
ps: PlotState;   
clipSize: CD.Position;
dd: REF DeviceDesc ← task.dd;
design: CD.Design ← task.design;
pageStep: CD.Position;
pageWidth: CD.Position;
dr: CD.DrawRef;
IF design=NIL OR dd=NIL THEN ERROR;
BEGIN
ENABLE { -- for ERRORs
UNWIND => {
CDVArrow.RemoveArrow[design];
TerminalIO.PutRope[" ** plot aborted **\n"];
Leave[];
};
};
Enter[design];
TRUSTED {Process.SetPriority[Process.priorityBackground]};
task.strips ← MAX[1, MIN[10, task.strips]];
IF task.abort=NIL THEN task.abort ← NEW[BOOLFALSE];
IF ~CDBasics.NonEmpty[task.clip] THEN task.clip ← CDOps.BoundingBox[design];
clipSize ← CDBasics.SizeOfRect[task.clip];
dr ← CD.CreateDrawRef[[
design: design,
selections: FALSE,
borders: TRUE,
stopFlag: task.abort,
drawRect: NoteRectangle,
drawChildSel: DontDrawChild,
drawOutLine: DrawOutline,
drawContext: DrawContext,
contextFilter: dd.contextFilter
]];
SetStipples[dd, design.technology];
CheckSpecialScale[task];
--Center the x range of the selected area of the design on the plotter bed, with at most dd.maxPixPerLambda pixels per lambda. If multiple task.strips are called for, overlap adjacent ones by "dd.toners" pixels.
-- total number of pixels across plot = imageFSize+[imageFSize-overlap]*(strips-1))
ps ← NEW[PlotStateRec ← [
plotClip: task.clip,
colorLoads: NEW[ColorDescription ← ALL[NIL]],
design: design,
plotScale: task.scale,
dd: dd
]];
IF ps.plotScale<=0 THEN {
ps.plotScale ← MIN[
REAL[dd.maxPixPerLambda] / design.technology.lambda,
REAL[(dd.imageFSize-dd.overlap)*task.strips+dd.overlap] / clipSize.x
]
};
dd.imageSSize ← Real.RoundC[clipSize.y*ps.plotScale]+1;
IF dd.pageMode=fixedPage THEN {
dd.imageSSize ← MIN[dd.imageSSize, dd.pageSlowSize];
ps.plotScale ← MIN[ps.plotScale, REAL[dd.pageSlowSize]/(clipSize.y+1)];
};
pageStep ← [(clipSize.x+task.strips-1)/task.strips, clipSize.y];
pageWidth ← [Real.Round[dd.imageFSize/ps.plotScale]+1, clipSize.y]; --larger to prevent rounding errors
dr.devicePrivate ← ps;
TerminalIO.PutRopes["start ploting\n"];
FOR strip: INT IN [0..task.strips) DO
fileName: Rope.ROPE ← MakeName[task.fileName, strip, task.strips];
ps.stripeClip ← [ --but not yet intersected with complete design clip
x1: ps.plotClip.x1+strip*pageStep.x,
y1: ps.plotClip.y1,
x2: ps.plotClip.x1+strip*pageStep.x+pageWidth.x,
y2: ps.plotClip.y2
];
ps.pdState ← PDFileWriter.Create[
fileName: fileName,
deviceCode: dd.deviceCode,
sResolution: dd.sResolution,
fResolution: dd.fResolution,
imageSSize: dd.imageSSize,
imageFSize: dd.imageFSize,
bandSSize: dd.bandSSize,
leftOverMode: dd.leftOverMode,
maxLoadWords: dd.maxLoadWords,
copies: dd.copies
];
ps.colorLoads^ ← ALL[NIL];
PDFileWriter.StartImage[pdState: ps.pdState, toners: dd.toners];
-- For each band in the strip
FOR bandYtop: INT ← 0, bandYtop+dd.bandSSize WHILE bandYtop<dd.imageSSize DO
dc: CD.Rect; --task.clip for band in design coordinates; little larger than band
CDSequencer.CheckAborted[design];
-- Determine coordinate transformations
ps.pdy ← bandYtop;
dc ← CDBasics.ReInterpreteRect[[
x1: ps.stripeClip.x1-1,
y1: ps.stripeClip.y2-Real.Fix[(bandYtop+dd.bandSSize)/ps.plotScale]-1,
x2: ps.stripeClip.x1+pageWidth.x+1,
y2: ps.stripeClip.y2-Real.Fix[bandYtop/ps.plotScale]+1
]] ;
dr.interestClip ← ps.bandClip ← CDBasics.Intersection[ps.plotClip, dc];
--Display current band under consideration to pacify user
ps.rightBound ← MIN[Real.Round[ToDeviceX[ps, ps.bandClip.x2]], ps.dd.imageSSize];
CDVArrow.ShowArrow[design, CDBasics.Center[dr.interestClip]];
-- clear previous tessalations
ps.anouncedLayer ← -1;
FOR l: CD.Layer IN CD.Layer DO
IF ps.tes[l]#NIL THEN
CStitching.ChangeRect[plane: ps.tes[l], rect: CDBasics.universe, new: NIL];
ENDLOOP;
CDOps.DrawDesign[design, dr]; -- build tesselations of design rectangles
AnalyzeTesselations[ps]; -- actually draw tesselations
TerminalIO.PutRope["."];
ENDLOOP; -- each band in the strip
PDFileWriter.EndPage[ps.pdState];
PDFileWriter.Close[ps.pdState];
TerminalIO.PutRopes["*\nrecorded strip on file ", fileName, "\n"];
ENDLOOP; -- each strip
TerminalIO.PutRope[" finished plot\n"];
CDVArrow.RemoveArrow[design: design];
Leave[];
END;
};
NoteRectangle: PROC [r: CD.Rect, l: CD.Layer, pr: CD.DrawRef] = {
ps: PlotState = NARROW[pr.devicePrivate];
IF CDBasics.NonEmpty[r] AND ps.dd.contextFilter[l] THEN {
IF ps.tes[l]=NIL THEN ps.tes[l] ← CStitching.NewTesselation[];
ps.tes[l].ChangeRect[rect: r, new: $covered];
};
};
SetStipples: PROC [dd: REF DeviceDesc, technology: CD.Technology] = {
PrepareContextFilter: PROC [dd: REF DeviceDesc] = {
FOR l: CD.Layer IN CD.Layer DO
IF dd.contextFilter^[l] THEN {
color: BOOLFALSE;
FOR toner: Toner IN Toner DO
IF dd.tonerToKey[toner]#NIL THEN
IF CDProperties.GetLayerProp[l, dd.tonerToKey[toner]]#NIL THEN {
color ← TRUE; EXIT
};
ENDLOOP;
dd.contextFilter^[l] ← color
}
ENDLOOP;
};
IF dd.stippleKey=NIL THEN {
TerminalIO.PutRope["device has undefined stipple key\n"];
ERROR ABORTED
};
IF technology.key#CDProperties.GetAtomProp[dd.stippleKey, lastTechPropKey] THEN {
TerminalIO.PutRope["try to load the stipples\n"];
CDEnvironment.ExecFileEntry[Rope.Cat["PDPlot-stipples-", Atom.GetPName[dd.stippleKey], "-", technology.name], NIL, NIL];
TerminalIO.PutRope["stipples loaded; please check whether any errors occured\n"];
};
WITH CDValue.Fetch[technology, dd.stippleKey] SELECT FROM
r: Rope.ROPE => TerminalIO.PutRopes["color stipples used: [", r, "]\n"];
ENDCASE => {
TerminalIO.PutRope["**color stipples are not defined\n"];
ERROR ABORTED
};
IF technology.key#CDProperties.GetAtomProp[dd.stippleKey, lastTechPropKey] THEN {
TerminalIO.PutRope["**Warning: another technology registered in the meantime; the technology independent stipples are redefined\n"];
};
dd.tonerToKey ← NEW[TonerKeys ← ALL[NIL]];
FOR t: PDFileWriter.Toner IN PDFileWriter.Toner DO
dd.tonerToKey^[t] ← CDProperties.GetAtomProp[dd.stippleKey, tonerToKeyKeys[t]];
IF ~dd.toners[t] OR dd.tonerToKey^[t]=NIL THEN {
dd.toners[t] ← FALSE;
dd.tonerToKey^[t] ← NEW[INT];
}
ENDLOOP;
PrepareContextFilter[dd];
};
SetColor: PROC [ps: PlotState, lev: CD.Layer] = {
MakeLoadref: PROC [pattern: REF ANY] RETURNS [loadRef: PDFileWriter.LoadReference] = TRUSTED {
ToTexture: PROC [pattern: REF ANY] RETURNS [texture: Stipple16] = CHECKED {
--tries to convert pattern to a texture stipple
WITH pattern SELECT FROM
s16: REF Stipple16 => texture ← s16^;
s8: REF Stipple8 =>
FOR i: [0..8) IN [0..8) DO
texture[i] ← texture[i+8] ← (256+1)*s8[i];
ENDLOOP;
s4: REF Stipple4 =>
FOR i: [0..4) IN [0..4) DO
texture[i] ← texture[i+4] ← texture[i+8] ← texture[i+12] ← s4[i]*1111H;
ENDLOOP;
ENDCASE => RETURN[Stipple16[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0FFFFH]]; -- error texture
RETURN [texture]
}; -- ToTexture
IF pattern=NIL THEN loadRef ← noColor
ELSE IF pattern=$ink THEN loadRef ← fullColor
ELSE {
texture: Stipple16 ← ToTexture[pattern];
loadRef ← PDFileWriter.LoadContiguousColorTile[pdState: ps.pdState, phase: 0, sMin: 0, fMin: 0, sSize: 16, fSize: 16, bitsPtr: @texture];
IF loadRef>=fullColor THEN ERROR;
};
}; -- MakeLoadref
MakeLoad: PROC [ps: PlotState, lev: CD.Layer] = INLINE {
--makes the load if it does not already exist
IF ps.colorLoads^[lev]=NIL THEN {
ps.colorLoads^[lev] ← NEW[LoadArray];
FOR toner: Toner IN Toner DO
tex: REF ← CDProperties.GetLayerProp[from: lev, prop: ps.dd.tonerToKey[toner]];
IF tex=NIL THEN ps.colorLoads^[lev][toner] ← noColor
ELSE ps.colorLoads^[lev][toner] ← MakeLoadref[tex]
ENDLOOP;
};
}; --MakeLoad
--SetColor
IF ps.anouncedLayer=lev THEN RETURN;
IF ps.colorLoads^[lev]=NIL THEN MakeLoad[ps, lev];
FOR toner: Toner IN Toner DO
IF ps.colorLoads^[lev][toner] = noColor THEN
PDFileWriter.SetColorOff[ps.pdState, toner]
ELSE IF ps.colorLoads^[lev][toner] = fullColor THEN
PDFileWriter.SetColorInk[ps.pdState, toner]
ELSE
PDFileWriter.SetColorTile[ps.pdState, toner, ps.colorLoads^[lev][toner], transparent];
ENDLOOP;
ps.anouncedLayer ← lev
};
PlotPolygon: PROC [ps: PlotState, ob: CD.Object, trans: CD.Transformation] = {
OutputTrapezoid: PROC [xbotL: REAL, xbotR: REAL, ybot: REAL, xtopL: REAL, xtopR: REAL, ytop: REAL] = {
--lower left x, lower right x, lower y, upper left x, upper right x, upper y
--global variable ps
--x-Fast; y-Slow
PDFileWriter.MaskTrapezoid[
pdState: ps.pdState,
sMin: Real.RoundC[ybot],
sSize: Real.RoundC[ytop-ybot],
fMin: Real.RoundC[xbotL],
fSize: Real.RoundC[xbotR-xbotL],
fMinLast: Real.RoundC[xtopL],
fSizeLast: Real.RoundC[xtopR-xtopL]
];
}; --OutputTrapezoid
--PlotPolygon
pp: CDCurves.CurveSpecific = NARROW[ob.specific];
polygon: CGReducer.Ref = CGReducer.New[size: 8];
tiling: CGArea.Ref = CGArea.New[size: 4];
clipRef: CGClipper.Ref = CGClipper.New[size: 4];
clip: GraphicsBasic.Box ← [
xmin: 0,
ymin: ps.pdy,
xmax: MIN[ToDeviceX[ps, ps.bandClip.x2], ps.dd.imageFSize],
ymax: MIN[ps.pdy+ps.dd.bandSSize, ps.dd.imageSSize]
];
CGClipper.SetBox[self: clipRef, box: clip];
CGClipper.Load[self: clipRef, reducer: polygon];
FOR p: LIST OF CD.Position ← pp.points, p.rest WHILE p#NIL DO
at: CD.Position = CDBasics.MapPoint[pointInCell: p.first, cellInWorld: trans];
CGReducer.Vertex[self: polygon, v: [ToDeviceX[ps, at.x], ToDeviceY[ps, at.y]]]
ENDLOOP;
CGReducer.Close[polygon];
CGReducer.Generate[self: polygon, area: tiling];
UNTIL CGArea.Empty[tiling] DO
t: GraphicsBasic.Trap = CGArea.Remove[tiling];
OutputTrapezoid[t.xbotL, t.xbotR, t.ybot, t.xtopL, t.xtopR, t.ytop];
ENDLOOP;
}; --PlotPolygon
DontDrawChild: CD.DrawProc = {};
DrawContext: PROC [pr: CD.DrawRef, proc: CD.DrawContextLayerProc, ob: CD.Object, trans: CD.Transformation, layer: CD.Layer] = {
IF CDCurves.IsPolygon[ob] THEN {
ps: PlotState = NARROW[pr.devicePrivate];
SetColor[ps, layer];
PlotPolygon[ps, ob, trans];
}
ELSE CDDefaultProcs.DrawContext[pr, proc, ob, trans, layer]
};
bugIsStillThere: BOOLTRUE;
DrawOutline: CD.DrawRectProc = { --PROC [r: Rect, l: Layer, pr: DrawRef]
delta: INT = 8;--doesn't cause overflow, plot scale is limited
x1, x2, y1, y2: INT; --in device coords
ps: PlotState = NARROW[pr.devicePrivate];
IF bugIsStillThere THEN RETURN;
--restrict outline to be only slightly larger than device, to prevent arithmetic overflow
r.x1 ← MAX[pr.interestClip.x1-delta, r.x1]; --r in cd Coordinates
r.y1 ← MAX[pr.interestClip.y1-delta, r.y1];
r.x2 ← MIN[pr.interestClip.x2+delta, r.x2];
r.y2 ← MIN[pr.interestClip.y2+delta, r.y2];
--convert to device pixel coordinates
x1 ← Real.Round[ToDeviceX[ps, r.x1]];
x2 ← Real.Round[ToDeviceX[ps, r.x2]];
y1 ← Real.Round[ToDeviceY[ps, r.y2]]; --swap 1-2 points of y coordinate
y2 ← Real.Round[ToDeviceY[ps, r.y1]];
--draw inclusive border pixels
SetColor[ps, l];
MaskRect[ps, x1, y1, x2+1, y1+1];
MaskRect[ps, x1, y2, x2+1, y2+1];
MaskRect[ps, x1, y1, x1+1, y2+1];
MaskRect[ps, x2, y1, x2+1, y2+1];
};
AnalyzeTesselations: PROC [ps: PlotState] = {
FOR lev: CD.Layer IN CD.Layer DO
IF ps.tes[lev]#NIL THEN {
SetColor[ps, lev];
CStitching.EnumerateArea[plane: ps.tes[lev], rect: ps.bandClip, eachTile: ProcessTile, data: ps];
};
ENDLOOP;
};
MaskRect: PROC [ps: PlotState, xStart, yStart, xStop, yStop: INT] = {
--coordinates in devices pixel coordinates;
--xStart = Real.Round[ToDeviceX[ps, r.x1]];
--yStop = Real.Round[ToDeviceY[ps, r.y1]]; swap 1-2 for y coords!
--xStop = Real.Round[ToDeviceX[ps, r.x2]];
--yStart = Real.Round[ToDeviceY[ps, r.y2]];
--prints including start pixels, excluding stop pixels
xStart ← MAX[xStart, 0];
xStop ← MIN[xStop, ps.rightBound];
yStart ← MAX[yStart, ps.pdy];
yStop ← MIN[yStop, ps.pdy+ps.dd.bandSSize, ps.dd.imageSSize];
IF yStop>yStart AND xStop>xStart THEN
PDFileWriter.MaskRectangle[pdState: ps.pdState, sMin: yStart, fMin: xStart, sSize: yStop-yStart, fSize: xStop-xStart];
};
ProcessTile: PROC [tile: CStitching.Tile, data: REF ANY] = {
IF tile.value=$covered THEN {
r: CD.Rect = CStitching.Area[tile];
ps: PlotState = NARROW[data];
MaskRect[ps: ps,
xStart: Real.Round[ToDeviceX[ps, r.x1]],
yStart: Real.Round[ToDeviceY[ps, r.y2]],
xStop: Real.Round[ToDeviceX[ps, r.x2]],
yStop: Real.Round[ToDeviceY[ps, r.y1]]
];
};
};
--command level
CheckSpecialScale: PROC [task: REF TaskDesc] = {
micronsPerInch: REAL ~ 25400;
specialScale: REAL ← -1; --means microns on plotted device per lambda
WITH CDProperties.GetAtomProp[task.dd.stippleKey, $CDxPDPlotScale] SELECT FROM
ri: REF INT => specialScale ← ri^;
rr: REF REAL => specialScale ← rr^;
ENDCASE => specialScale ← -1;
IF specialScale>0 THEN {
size: CD.Position ← CDBasics.SizeOfRect[task.clip];
task.scale ← specialScale*task.dd.sResolution/micronsPerInch/task.design.technology.lambda;
TerminalIO.PutF["plotting at special scale %g microns/lambda [%g pixels/chipndaleUnit]\n", [real[specialScale]], [real[task.scale]]];
IF task.dd.pageMode=fixedPage THEN {
IF task.scale > REAL[task.dd.pageSlowSize]/(size.y+1) THEN
TerminalIO.PutRope["doesn't fit on page; must change scale\n"];
};
};
};
cnt: INT ← 0;
PDColorPlotComm: PROC [comm: CDSequencer.Command] = {
ENABLE error => {
SELECT TerminalIO.RequestSelection["ERROR", LIST["abort plot", "reset and abort plot", "debug"]] FROM
1 => GOTO tobad;
2 => {currTech ← NIL; GOTO tobad};
3 => REJECT;
ENDCASE => GOTO tobad;
};
clip: CD.Rect;
mustSendToDevice: BOOLFALSE;
server: Rope.ROPE; copies: INT ← 1;
task: REF TaskDesc ← NEW[TaskDesc ← [design: comm.design, abort: abortPlot, strips: 1]];
TerminalIO.PutRope["Color plot\n"];
task.dd ← MakeDevice[comm.key];
IF task.dd=NIL THEN {
TerminalIO.PutRope["no device specified\n"];
RETURN
};
TerminalIO.PutRopes[task.dd.name, "\n"];
SELECT TerminalIO.RequestSelection["PD Plot", LIST["plot complete design", "plot drawn rectangle", "specials, strips, servers..."]] FROM
1 => {
clip ← CDOps.BoundingBox[comm.design];
TerminalIO.PutRope["plot all\n"];
};
2 => {
clip ← CDBasics.ToRect[comm.pos, comm.sPos];
TerminalIO.PutRope["plot rectangle\n"];
};
3 => {
default: Rope.ROPENIL;
choice: LIST OF Rope.ROPELIST["no", "type name of server"];
TerminalIO.PutRope["special\n"];
SELECT TerminalIO.RequestSelection["Plot", LIST["plot complete design", "plot drawn rectangle"]] FROM
1 => {
clip ← CDOps.BoundingBox[comm.design];
TerminalIO.PutRope["plot all\n"];
};
2 => {
clip ← CDBasics.ToRect[comm.pos, comm.sPos];
TerminalIO.PutRope["plot rectangle\n"];
};
ENDCASE => ERROR ABORTED;
IF task.dd.pageMode=anyStripe THEN {
IF TerminalIO.Confirm["plot multiple vertical strips", "it's ok not to..."] THEN
task.strips ← TerminalIO.RequestInt["How many vertical strips? [1..10] "];
};
IF TerminalIO.Confirm["ommit comments", "usually not except for lage scale..."] THEN
task.dd.contextFilter[CD.commentLayer] ← FALSE;
IF comm.key=$PDPlotVersatec THEN default ← "Sleepy"
ELSE IF comm.key=$PDPlotColor400 THEN default ← "MtFuji";
IF default#NIL THEN choice.rest.rest ← LIST[default];
SELECT TerminalIO.RequestSelection["send immediately to server", choice] FROM
0, 1 => NULL;
2 => {
mustSendToDevice ← TRUE;
server ← TerminalIO.RequestRope["server name>"];
};
3 => {
mustSendToDevice ← TRUE;
server ← default;
};
ENDCASE => NULL;
IF mustSendToDevice THEN {
IF TerminalIO.Confirm["multiple copies", "istead of a single one..."] THEN
copies ← TerminalIO.RequestInt["copies>"];
IF copies<1 OR copies>10 THEN {
TerminalIO.PutRope["unreasonable number of copies\n"];
ERROR ABORTED
};
};
};
ENDCASE => ERROR ABORTED;
IF clip.x1>=clip.x2 OR clip.y1>=clip.y2 THEN {
TerminalIO.PutRope["**cannot plot empty area\n"];
ERROR ABORTED
};
task.clip ← clip;
IF mustSendToDevice THEN
task.fileName ← IO.PutFR["///temp/chipndale/pdplots/Plot%gX.pd", IO.int[cnt ← cnt+1]];
Plot[task];
IF mustSendToDevice THEN {
FOR strip: INT IN [0..task.strips) DO
PeachPrint.DoPeachPrintCommand[
server: server,
file: MakeName[task.fileName, strip, task.strips],
log: TerminalIO.CreateStream[],
copies: copies,
fork: TRUE
!
PeachPrint.PupAborted => {TerminalIO.PutRope["**Pup aborted plotting\n"]; CONTINUE};
RuntimeError.UNCAUGHT => {TerminalIO.PutRope["**Peach aborted plotting\n"]; CONTINUE};
];
ENDLOOP
};
EXITS tobad => NULL
};
MakeName: PROC [base: Rope.ROPE, strip, strips: INT𡤁] RETURNS [fileName: Rope.ROPE] = {
modifier: Rope.ROPEIF strips>1 THEN IO.PutFR["%d", IO.int[strip+1]] ELSE NIL;
IF Rope.IsEmpty[base] THEN base ← "PDPlot";
fileName ← CDIO.MakeName[wDir: "///temp/", base: base, modifier: modifier, ext: "pd"];
};
WaitPlotFinishedCommand: Commander.CommandProc = {
prio: Process.Priority = Process.GetPriority[];
IF ~plottingInProgress THEN {
IO.PutRope[cmd.out, " waiting for plot to start"];
WHILE ~plottingInProgress DO
Process.Pause[Process.SecondsToTicks[1]];
ENDLOOP;
IO.PutRope[cmd.out, ". plot started...\n"]
};
Process.SetPriority[Process.priorityBackground];
WHILE plottingInProgress DO
Process.Pause[Process.SecondsToTicks[2]];
ENDLOOP;
Process.SetPriority[prio];
IO.PutRope[cmd.out, " finished\n"]
};
--interpreter set up
blackX: REF  ← NIL;
cyanX: REF  ← NIL;
yellowX: REF ← NIL;
magentaX: REF ← NIL;
currTech: CD.Technology ← NIL;
currLayer: CD.Layer ← CD.undefLayer;
Forgett: PROC [l: CD.Layer] = {
IF blackX#NIL THEN CDProperties.PutLayerProp[onto: l, prop: blackX, val: NIL];
IF cyanX#NIL THEN CDProperties.PutLayerProp[onto: l, prop: cyanX, val: NIL];
IF magentaX#NIL THEN CDProperties.PutLayerProp[onto: l, prop: magentaX, val: NIL];
IF yellowX#NIL THEN CDProperties.PutLayerProp[onto: l, prop: yellowX, val: NIL];
};
Start: PUBLIC PROC[tech, device, b, c, m, y: ATOM, text: Rope.ROPENIL, invert: BOOLFALSE] = {
IF currTech#NIL THEN ERROR error;
CDPropertyTools.RemoveProperties[$CDxPDPlotScale];
CDPropertyTools.RemoveProperties[device];
CDProperties.PutAtomProp[device, $CDxPDPlotScale, NIL]; --might be not associated
IF b#NIL THEN CDPropertyTools.RemoveProperties[b];
IF c#NIL THEN CDPropertyTools.RemoveProperties[c];
IF m#NIL THEN CDPropertyTools.RemoveProperties[m];
IF y#NIL THEN CDPropertyTools.RemoveProperties[y];
currTech ← CD.FetchTechnology[tech];
blackX ← b;
cyanX ← c;
yellowX ← y;
magentaX ← m;
CDValue.Store[currTech, device, text];
IF invert THEN {
CDProperties.PutAtomProp[device, tonerToKeyKeys[black], yellowX];
CDProperties.PutAtomProp[device, tonerToKeyKeys[cyan], magentaX];
CDProperties.PutAtomProp[device, tonerToKeyKeys[yellow], blackX];
CDProperties.PutAtomProp[device, tonerToKeyKeys[magenta], cyanX];
}
ELSE {
CDProperties.PutAtomProp[device, tonerToKeyKeys[black], blackX];
CDProperties.PutAtomProp[device, tonerToKeyKeys[cyan], cyanX];
CDProperties.PutAtomProp[device, tonerToKeyKeys[yellow], yellowX];
CDProperties.PutAtomProp[device, tonerToKeyKeys[magenta], magentaX];
};
CDProperties.PutAtomProp[device, $CDxLastTechnology, currTech.key];
FOR l: CD.Layer IN CD.Layer DO Forgett[l] ENDLOOP;
};
End: PUBLIC PROC [] = {
currTech ← NIL
};
Layer: PUBLIC PROC [uniqueKey: ATOM] = {
currLayer ← CD.FetchLayer[t: currTech, uniqueKey: uniqueKey];
Forgett[currLayer]
};
LayerNumber: PUBLIC PROC [layer: NAT] = {
currLayer ← layer;
Forgett[currLayer]
};
Color1: PUBLIC PROC [key: ATOM] = {
CDProperties.PutLayerProp[onto: currLayer, prop: key, val: $ink]
};
Color4: PUBLIC PROC [key: ATOM, i0, i1, i2, i3: [0..16)] = {
CDProperties.PutLayerProp[onto: currLayer, prop: key,
val:
IF i0=0 AND i1=0 AND i2=0 AND i3=0 THEN NIL
ELSE IF i0=15 AND i1=15 AND i2=15 AND i3=15 THEN $ink
ELSE NEW[Stipple4 ← [i0, i1, i2, i3]]
]
};
Color8: PUBLIC PROC [key: ATOM, i0, i1, i2, i3, i4, i5, i6, i7: [0..256)] = {
CDProperties.PutLayerProp[onto: currLayer, prop: key,
val:
IF i0=0 AND i1=0 AND i2=0 AND i3=0 AND i4=0 AND i5=0 AND i6=0 AND i7=0 THEN NIL
ELSE IF i0=255 AND i1=i0 AND i2=i0 AND i3=i0 AND i4=i0 AND i5=i0 AND i6=i0 AND i7=i0 THEN $ink
ELSE NEW[Stipple8 ← [i0, i1, i2, i3, i4, i5, i6, i7]]
]
};
--initializations
Init: PROC [] = {
CDPropertyTools.Associate[$CDxPDPlotScale, $CDxPDPlotScale];
tonerToKeyKeys[black] ← $CDxPDPlotBlack;
tonerToKeyKeys[cyan] ← $CDxPDPlotCyan;
tonerToKeyKeys[magenta] ← $CDxPDPlotMagenta;
tonerToKeyKeys[yellow] ← $CDxPDPlotYellow;
CDSequencer.ImplementCommand[key: $PDPlotVersatec, proc: PDColorPlotComm, queue: doQueue];
CDSequencer.ImplementCommand[key: $PDPlotC150, proc: PDColorPlotComm, queue: doQueue];
CDSequencer.ImplementCommand[key: $PDPlotColor400, proc: PDColorPlotComm, queue: doQueue];
CDSequencer.ImplementCommand[key: $PDPlotUserDevice, proc: PDColorPlotComm, queue: doQueue];
Commander.Register[
key: "///Commands/CDWaitPlotFinished",
proc: WaitPlotFinishedCommand,
doc: "waits until ChipNDale pd plotting is finished once"
];
TerminalIO.PutRope["ChipNDale PD color plot program loaded\n"];
};
Init[];
END.