ArtworkInterpressImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Michael Plass, September 25, 1985 11:25:51 am PDT
Doug Wyatt, June 24, 1985 6:46:40 pm PDT
Pier, October 9, 1985 2:39:15 pm PDT
DIRECTORY Convert, TEditFormat, Interpress, Imager, ViewerOps, Commander, Rope, NodeStyle, RuntimeError, Scaled, TextEdit, Real, IO, NodeProps, FSRope, FunctionCache, ImagerMemory;
ArtworkInterpressImpl: CEDAR PROGRAM
IMPORTS Convert, TEditFormat, Interpress, Imager, ViewerOps, Commander, Rope, NodeStyle, RuntimeError, Scaled, TextEdit, Real, IO, NodeProps, FSRope, FunctionCache, ImagerMemory
~ BEGIN
ROPE: TYPE ~ Rope.ROPE;
GetBounds: PROC [prop: REF, nodeStyle: NodeStyle.Ref] RETURNS [box: Imager.Box ← [0,0,0,0]] ~ {
rope: ROPE ~ WITH prop SELECT FROM r: ROPE => r, ENDCASE => NIL;
size: INT ~ Rope.Size[rope];
IF size # 0 THEN {
i: INT ← -1;
Getc: PROC RETURNS [CHAR] ~ {i←i+1; RETURN [IF i<size THEN rope.Fetch[i] ELSE ' ]};
c: CHAR ← Getc[];
Invariant: c = (IF i<size THEN rope.Fetch[i] ELSE ' )
startTok: INT ← 0;
sizeTok: INT ← 0;
GetTok: PROC ~ {
startTok ← i;
UNTIL c = ' DO c ← Getc[] ENDLOOP;
sizeTok ← i-startTok;
WHILE c = ' AND i<size DO c ← Getc[] ENDLOOP;
};
Match: PROC [key: ROPE] RETURNS [BOOL] ~ {
k: INT ~ Rope.Size[key];
RETURN [sizeTok = k AND Rope.Run[rope, startTok, key, 0, FALSE] = k]
};
WHILE c = ' AND i<size DO c ← Getc[] ENDLOOP;
UNTIL i>=size DO
r: REAL ← 1.0;
IF c = '- THEN {r ← -r; c ← Getc[]};
GetTok[];
r ← r*Convert.RealFromLiteral[rope, startTok];
GetTok[];
SELECT TRUE FROM
Match["mm"] => r ← r*0.001;
Match["cm"] => r ← r*0.01;
Match["in"] => r ← r*0.0254;
Match["pt"] => r ← r*0.0254/72.27;
Match["bp"] => r ← r*0.0254/72;
ENDCASE => ERROR;
r ← r*pointsPerMeter;
GetTok[];
SELECT TRUE FROM
Match["xmin"] => box.xmin ← r;
Match["xmax"] => box.xmax ← r;
Match["ymin"] => box.ymin ← r;
Match["ymax"] => box.ymax ← r;
ENDCASE => ERROR;
ENDLOOP;
}
ELSE {
box.ymax ← NodeStyle.GetTopLeading[nodeStyle];
box.ymin ← -NodeStyle.GetBottomLeading[nodeStyle];
box.xmax ← NodeStyle.GetLineLength[nodeStyle];
};
};
Floor: PROC [r: REAL] RETURNS [i: INT] ~ {
i ← Real.Round[r];
IF i > r THEN i ← i-1;
};
Ceiling: PROC [r: REAL] RETURNS [i: INT] ~ {
i ← Real.Round[r];
IF i < r THEN i ← i+1;
};
Format: TEditFormat.FormatProc ~ {
inner: PROC ~ {
extraIndent: REAL ← nodeStyle.GetFirstIndent;
leftIndent: REAL ← nodeStyle.GetLeftIndent + extraIndent;
rightIndent: REAL ← nodeStyle.GetRightIndent;
trimmedLineWidth: REAL ← lineWidth.Float-leftIndent-rightIndent;
box: Imager.Box ← GetBounds[NodeProps.GetProp[n: node, name: $Bounds], nodeStyle];
lineInfo.xmin ← Floor[box.xmin];
lineInfo.ymin ← Floor[box.ymin];
lineInfo.xmax ← Ceiling[box.xmax];
lineInfo.ymax ← Ceiling[box.ymax];
SELECT nodeStyle.GetLineFormatting[] FROM
FlushLeft => {lineInfo.xOffset ← Scaled.FromReal[leftIndent]};
FlushRight => {lineInfo.xOffset ← Scaled.FromReal[MAX[lineWidth.Float-leftIndent-box.xmax, 0]]};
Centered, Justified => {lineInfo.xOffset ← Scaled.FromReal[leftIndent+(trimmedLineWidth-box.xmax)/2]};
ENDCASE => ERROR;
};
lineInfo.startPos ← [node, 0];
lineInfo.nextPos ← [node, TextEdit.Size[node]];
lineInfo.nChars ← TextEdit.Size[node];
inner[ ! RuntimeError.UNCAUGHT => {IF NOT debug THEN {ViewerOps.BlinkDisplay[]; CONTINUE}}];
};
Resolve: TEditFormat.ResolveProc ~ {
loc ← lineInfo.startPos;
xmin ← lineInfo.xmin+lineInfo.xOffset.integerPart;
width ← lineInfo.xmax;
rightOfLine ← FALSE;
};
CharPosition: TEditFormat.CharPositionProc ~ {
x ← lineInfo.xmin+lineInfo.xOffset.integerPart;
width ← lineInfo.xmax;
};
BoundingBox: TEditFormat.BoundingBoxProc ~ {
RETURN [[lineInfo.xmin, lineInfo.ymin, lineInfo.xmax, lineInfo.ymax]]
};
debug: BOOLFALSE;
useFSRope: BOOLTRUE;
pointsPerMeter: REAL ← 72/0.0254;
memoryCache: FunctionCache.Cache ← FunctionCache.GlobalCache[];
Paint: TEditFormat.PaintProc ~ {
box: Imager.Box ← BoundingBox[lineInfo, lineInfo.startPos.where, lineInfo.nChars];
ipRope: ROPENARROW[NodeProps.GetProp[lineInfo.startPos.node, $Interpress]];
compare: FunctionCache.CompareProc ~ {good ← argument = ipRope};
memory: Imager.Context ← NARROW[FunctionCache.Lookup[memoryCache, compare, $ArtwIP].value];
IF memory = NIL THEN {
proc: PROC ~ {
ipMaster: Interpress.OpenMaster ~ Interpress.FromStream[ipStream, NIL, NIL];
Imager.Trans[memory];
Imager.ScaleT[memory, pointsPerMeter];
Interpress.DoPage[ipMaster, 1, memory];
};
ipStream: IO.STREAMIF useFSRope THEN FSRope.StreamFromRope[ipRope] ELSE IO.RIS[ipRope];
memory ← ImagerMemory.NewMemoryContext[];
Imager.DoSave[memory, proc ! RuntimeError.UNCAUGHT => {IF NOT debug THEN {ViewerOps.BlinkDisplay[]; CONTINUE}}];
FunctionCache.Insert[memoryCache, ipRope, memory, ImagerMemory.GetContextSize[memory], $ArtwIP];
};
ImagerMemory.Replay[c: memory, into: context];
};
class: TEditFormat.ArtworkClass ~ NEW[TEditFormat.ArtworkClassRep ← [
name: $Interpress,
format: Format,
paint: Paint,
resolve: Resolve,
charPosition: CharPosition,
boundingBox: BoundingBox
]];
Command: Commander.CommandProc ~ {
IF Rope.Match["*on*", cmd.commandLine, FALSE] THEN TEditFormat.RegisterArtwork[class]
ELSE IF Rope.Match["*off*", cmd.commandLine, FALSE] THEN TEditFormat.UnRegisterArtwork[$Interpress]
ELSE {cmd.out.PutRope["Please say on or off.\n"]; RETURN};
ViewerOps.PaintEverything[];
};
Commander.Register["ArtworkInterpress", Command, "Turn ArtworkInterpress on or off"];
END.