ImagerDisplayMaskImpl.mesa
Last Edited by: Crow, July 15, 1983 11:36 am
DIRECTORY
Environment   USING [LongNumber],
Inline     USING [BITOR, BITAND, HighHalf],
ImagerBasic   USING [Path, IntPair, IntRectangle, PixelArray,
         Transformation, StrokeEnds],
ImagerTransform USING [IntTransform],
ImagerDisplay  USING [Mask],
ImagerDisplayPrivate USING [ContextData],
Imager    USING [Context];
ImagerDisplayMaskImpl: CEDAR PROGRAM
IMPORTS ImagerTransform, Inline
EXPORTS Imager
= BEGIN OPEN ImagerBasic;
NotImplementedYet: PUBLIC SIGNAL = CODE;
Client Procedures (exported to Imager)
MaskStroke: PUBLIC PROC [context: Imager.Context, path: Path, width: REAL, strokeEnds: StrokeEnds] = {
Form a stencil of "width" thickness around a spine defined by "path".
Connected path segments will be mitered together. If the first and last points of a trajectory coincide, the path will be closed and mitered.
displayContext: ImagerDisplayPrivate.ContextData = NARROW[context.data];
};
MaskFill: PUBLIC PROC [context: Imager.Context, path: Path] = {
Use the path to outline an area to be filled with the source
displayContext: ImagerDisplayPrivate.ContextData = NARROW[context.data];
};
MaskPixel: PUBLIC PROC [context: Imager.Context, pixelArray: PixelArray] = {
Use the supplied pixel array as the stencil. The origin of the pixel array will be placed at the origin of the client space. The samples will be assumed to be placed at one unit intervals in the client space.
displayContext: ImagerDisplayPrivate.ContextData = NARROW[context.data];
};
Fast Track. The width of lines, etc. is device dependent, approximately .1% page height.
MoveTo: PUBLIC PROC [context: Imager.Context, p: IntPair] = {
displayContext: ImagerDisplayPrivate.ContextData = NARROW[context.data];
displayContext.currentIntPosition ←
        ImagerTransform.IntTransform[p, displayContext.transform];
Imager.SetIntCP[context, p];
};
DrawTo: PUBLIC PROC [context: Imager.Context, p: IntPair] = {
Draw line using source for color
displayContext: ImagerDisplayPrivate.ContextData = NARROW[context.data];
q: IntPair ← p;    -- hold onto untransformed point
p ← ImagerTransform.IntTransform[p, displayContext.transform];
IF displayContext.noClip
THEN { OPEN displayContext;
  deviceProcs.drawLine[currentIntPosition, p, currentPxlValue]; }
ELSE IF displayContext.clipper.refRep = NIL       -- rectangular?
THEN { in: BOOLEAN; a, b: IntPair;
[in, a, b] ← ClipEdgeByRectangle[displayContext.clipper,
          displayContext.currentIntPosition, p];
IF in THEN displayContext.deviceProcs.drawLine[a, b,
           displayContext.currentPxlValue];
}
ELSE ERROR NotImplementedYet[];    -- until paths are worked out - M. Plass?
displayContext.currentIntPosition ← p;
};
DrawPath: PUBLIC PROC [context: Imager.Context, path: Path] = {
Draw whole path using source for color
};
FillRectangle: PUBLIC PROC [context: Imager.Context, area: IntRectangle] = {
Fill rectangular area with source
};
Internal Procedures
ClipEdgeByRectangle: PROC [clipper: ImagerDisplay.Mask, a, b: IntPair]
        RETURNS [BOOLEAN, IntPair, IntPair] = {
Out: TYPE = RECORD[bottom, top, left, right: BOOLEAN];
noneOut: Out = [FALSE, FALSE, FALSE, FALSE];
Code: PROC [x, y: INTEGER] RETURNS [Out] = INLINE {
OPEN clipper;
out: Out ← noneOut;
IF x < fMin THEN out.left ← TRUE;  IF x > fMin + fSize THEN out.right ← TRUE;
IF y < sMin THEN out.bottom ← TRUE; IF y > sMin + sSize THEN out.top ← TRUE;
RETURN [ out ];
};
ClipY: PROC [a, b: IntPair, xPos: INTEGER] RETURNS [INTEGER] = INLINE {
t: Environment.LongNumber;
t.high ← LOOPHOLE[xPos - a.x, UNSPECIFIED];  t.low ← LOOPHOLE[0, UNSPECIFIED];
RETURN[ a.y + LOOPHOLE[
Inline.HighHalf[ LOOPHOLE[t, LONG INTEGER] / (b.x - a.x) * (b.y - a.y)], INTEGER] ];
};
ClipX: PROC [a, b: IntPair, yPos: INTEGER] RETURNS [INTEGER] = INLINE {
t: Environment.LongNumber;
t.high ← LOOPHOLE[yPos - a.y, UNSPECIFIED];  t.low ← LOOPHOLE[0, UNSPECIFIED];
RETURN[ a.x + LOOPHOLE[
Inline.HighHalf[ LOOPHOLE[t, LONG INTEGER] / (b.y - a.y) * (b.x - a.x)], INTEGER] ];
};
aCode, bCode, out: Out;
c: IntPair;
WHILE TRUE DO
aCode ← Code[a.x, a.y];  bCode ← Code[b.x, b.y];
Trivial acceptance test
IF Inline.BITOR[LOOPHOLE[aCode, UNSPECIFIED], LOOPHOLE[bCode, UNSPECIFIED]]
  = LOOPHOLE[0, UNSPECIFIED]
THEN RETURN[TRUE, a, b];
Trivial rejection test
IF Inline.BITAND[LOOPHOLE[aCode, UNSPECIFIED], LOOPHOLE[bCode, UNSPECIFIED]]
  # LOOPHOLE[0, UNSPECIFIED]
THEN RETURN[ FALSE, a, b];
Actual clipping necessary
out ← IF aCode # noneOut THEN aCode ELSE bCode;
SELECT TRUE FROM
out.left => { c.x ← clipper.fMin;
     c.y ← ClipY[a, b, c.x]; };
out.right => {  c.x ← clipper.fMin + clipper.fSize;
     c.y ← ClipY[a, b, c.x]; };
out.bottom => { c.y ← clipper.sMin;
     c.x ← ClipX[a, b, c.y]; };
out.top => { c.y ← clipper.sMin + clipper.sSize;
     c.x ← ClipX[a, b, c.y]; }
ENDCASE;
IF aCode = out THEN a ← c ELSE b ← c;
ENDLOOP;
ERROR;     -- can't get here, supposedly
};
END.