G2dImagerGraphCaptureImpl.mesa
Copyright © 1986 by Xerox Corporation. All rights reserved.
Michael Plass, September 18, 1989 5:06:29 pm PDT
Last tweaked by Mike Spreitzer on June 22, 1987 12:49:41 pm PDT
Jules Bloomenthal July 2, 1992 5:42 pm PDT
DIRECTORY Char, G2dImagerGraphCapture, Imager, ImagerDeviceVector, ImagerPrivate, ImagerState, ImagerTransformation, Rope, Scaled,
SF;
G2dImagerGraphCaptureImpl:
CEDAR PROGRAM
IMPORTS Char, Imager, ImagerPrivate, ImagerState, ImagerTransformation, Rope, Scaled
EXPORTS Imager, G2dImagerGraphCapture
~ BEGIN OPEN Imager;
Class: TYPE ~ ImagerPrivate.Class;
ClassRep: PUBLIC TYPE ~ ImagerPrivate.ClassRep; -- export to Imager.ClassRep
State: TYPE ~ ImagerState.State;
StateRep: PUBLIC TYPE ~ ImagerState.StateRep; -- export to Imager.StateRep
Data: TYPE ~ REF DataRep;
DataRep:
TYPE ~
RECORD [
nPoints: NAT ← 0,
point: REF PointSeq,
label: REF RopeSeq ← NIL,
edges: LIST OF EdgeRep ← NIL,
rawLabels: LIST OF LabelRep ← NIL
];
LabelRep:
TYPE ~
RECORD [vec:
VEC, rope:
ROPE];
EdgeRep:
TYPE ~
RECORD [from, to:
INT];
PointSeq:
TYPE ~
RECORD [
SEQUENCE max:
NAT
OF
VEC];
RopeSeq:
TYPE ~
RECORD [
SEQUENCE max:
NAT
OF
ROPE];
AddLabel:
PROC [data: Data, cp:
VEC, rope:
ROPE] ~ {
data.rawLabels ← CONS[[cp, rope], data.rawLabels];
};
tolerance: REAL ← 1.0;
FindPointNumber:
PROC [data: Data, vec:
VEC]
RETURNS [
INT] ~ {
FOR i:
NAT
IN [0..data.nPoints)
DO
p: VEC ~ data.point[i];
IF ABS[vec.x-p.x] <= tolerance AND ABS[vec.y-p.y] <= tolerance THEN RETURN [i];
ENDLOOP;
data.point[data.nPoints] ← vec;
data.nPoints ← data.nPoints + 1;
RETURN [data.nPoints-1]
};
AddEdge:
PROC [data: Data, from, to:
INT] ~ {
data.edges ← CONS[[from, to], data.edges];
};
CreateContext:
PUBLIC
PROC [initalScale:
REAL]
RETURNS [context: Context] ~ {
data: Data ~ NEW[DataRep ← [point: NEW[PointSeq[16300]]]];
state: State ~ ImagerState.CreateState[];
context ← NEW[Imager.ContextRep ← [class: myClass, state: state, data: data]];
Imager.ScaleT[context, initalScale];
Imager.SetGray[context, 1];
};
GetGraph:
PUBLIC
PROC [context: Imager.Context]
RETURNS [G2dImagerGraphCapture.Graph] ~ {
data: Data ~ NARROW[context.data];
graph: G2dImagerGraphCapture.Graph ~ NEW[G2dImagerGraphCapture.GraphRep[data.nPoints]];
FOR i:
NAT
IN [0..data.nPoints)
DO
vertex: G2dImagerGraphCapture.Vertex ← NEW[G2dImagerGraphCapture.VertexRep];
vertex.index ← i;
vertex.pos ← data.point[i];
graph[i] ← vertex;
ENDLOOP;
IF data.nPoints > 0
THEN {
FOR m:
LIST
OF LabelRep ← data.rawLabels, m.rest
UNTIL m =
NIL
DO
d: REAL ← 9999999;
k: NAT ← 0;
vec: VEC ~ m.first.vec;
FOR i:
NAT
IN [0..data.nPoints)
DO
IF graph[i].label =
NIL
THEN {
p: VEC ~ data.point[i];
dd: REAL ~ MAX[ABS[vec.x-p.x], ABS[vec.y-p.y]];
IF dd < d THEN {d ← dd; k ← i};
};
ENDLOOP;
IF d # 9999999 THEN graph[k].label ← m.first.rope;
ENDLOOP;
};
FOR m:
LIST
OF EdgeRep ← data.edges, m.rest
UNTIL m =
NIL
DO
from: G2dImagerGraphCapture.Vertex ~ graph[m.first.from];
to: G2dImagerGraphCapture.Vertex ~ graph[m.first.to];
from.edgesOut ← CONS[to.index, from.edgesOut];
to.edgesIn ← CONS[from.index, to.edgesIn];
ENDLOOP;
RETURN [graph]
};
PlaceLabels:
PROC [data: Data] ~ {
IF data.nPoints > 0
THEN {
data.label ← NEW[RopeSeq[data.nPoints]];
FOR m:
LIST
OF LabelRep ← data.rawLabels, m.rest
UNTIL m =
NIL
DO
d: REAL ← 9999999;
k: NAT ← 0;
vec: VEC ~ m.first.vec;
FOR i:
NAT
IN [0..data.nPoints)
DO
IF data.label[i] =
NIL
THEN {
p: VEC ~ data.point[i];
dd: REAL ~ MAX[ABS[vec.x-p.x], ABS[vec.y-p.y]];
IF dd < d THEN {d ← dd; k ← i};
};
ENDLOOP;
IF d # 9999999 THEN data.label[k] ← m.first.rope;
ENDLOOP;
};
};
Realify:
PROC [dVec: ImagerDeviceVector.DVec] = {
IF dVec.scaled
THEN {
dVec.fv.x ← Scaled.Float[dVec.sv.s];
dVec.fv.y ← Scaled.Float[dVec.sv.f];
dVec.scaled ← FALSE;
};
};
MyShow:
PROC [context: Context, string: XStringProc, xrel:
BOOL] ~ {
state: State ~ context.state;
data: Data ~ NARROW[context.data];
text: REF TEXT ← NEW[TEXT[512]];
charAction:
PROC [char: XChar] ~ {
text[text.length] ← VAL[Char.Code[char]];
text.length ← text.length + 1;
};
string[charAction];
Realify[state.cp];
IF state.np.noImage = 0 THEN AddLabel[data, state.cp.fv, Rope.FromRefText[text]];
};
MyShowBackward:
PROC [context: Context, string: XStringProc] ~ {
state: State ~ context.state;
data: Data ~ NARROW[context.data];
text: REF TEXT ← NEW[TEXT[512]];
charAction:
PROC [char: XChar] ~ {
text[text.length] ← VAL[Char.Code[char]];
text.length ← text.length + 1};
string[charAction];
Realify[state.cp];
IF state.np.noImage = 0 THEN AddLabel[data, state.cp.fv, Rope.FromRefText[text]];
};
MyShowText:
PROC [context: Context, text:
REF
READONLY
TEXT, start, len:
NAT, xrel:
BOOL] ~ {
state: State ~ context.state;
data: Data ~ NARROW[context.data];
i: NAT ← 0;
p: PROC RETURNS [c: CHAR] ~ {c ← text[start+i]; i ← i + 1};
len ← MIN[len, NAT[text.length-start]];
Realify[state.cp];
IF state.np.noImage = 0 THEN AddLabel[data, state.cp.fv, Rope.FromProc[len: len, p: p]];
};
MyMaskFill:
PROC [context: Context, path: PathProc, oddWrap:
BOOL] ~ {};
MyMaskRectangle:
PROC [context: Context, r: Rectangle] ~ {};
MyMaskRectangleI:
PROC [context: Context, x, y, w, h:
INTEGER] ~ {};
MyMaskStroke:
PROC [context: Context, path: PathProc, closed:
BOOL] ~ {
lp: VEC ← [0,0];
p0: VEC ← [0,0];
moveTo: PROC[p: VEC] ~ {IF closed AND lp#p0 THEN MyMaskVector[context, lp, p0]; p0 ← lp ← p};
lineTo: PROC[p1: VEC] ~ {MyMaskVector[context, lp, p1]; lp ← p1};
curveTo: PROC[p1, p2, p3: VEC] ~ {MyMaskVector[context, lp, p3]; lp ← p3};
conicTo: PROC[p1, p2: VEC, r: REAL] ~ {MyMaskVector[context, lp, p2]; lp ← p2};
arcTo: PROC[p1, p2: VEC] ~ {MyMaskVector[context, lp, p2]; lp ← p2};
path[moveTo, lineTo, curveTo, conicTo, arcTo];
IF closed AND lp#p0 THEN MyMaskVector[context, lp, p0];
};
MyMaskDashedStroke:
PROC [context: Context, path: PathProc, patternLen:
NAT, pattern:
PROC [
NAT]
RETURNS [
REAL], offset, length:
REAL] ~ {
MyMaskStroke[context, path, FALSE];
};
MyMaskVector:
PROC [context: Context, p1, p2:
VEC] ~ {
state: State ~ context.state;
data: Data ~ NARROW[context.data];
pn1: INT ~ FindPointNumber[data, ImagerTransformation.Transform[state.clientToDevice, p1]];
pn2: INT ~ FindPointNumber[data, ImagerTransformation.Transform[state.clientToDevice, p2]];
IF state.np.noImage = 0 THEN AddEdge[data, pn1, pn2];
};
MyMaskPixel:
PROC [context: Context, pa: PixelArray] ~ {};
MyMaskBitmap:
PROC [context: Context, bitmap: SampleMap, referencePoint:
SF.Vec, scanMode: ScanMode, position:
VEC] ~ {};
MyDrawBitmap: PROC [context: Context, bitmap: SampleMap, referencePoint: SF.Vec, scanMode: ScanMode, position: VEC] ~ {};
myClass: Class ~
NEW[ClassRep ← [
type: $GraphCapture,
Save: ImagerState.StateSave,
Restore: ImagerState.StateRestore,
SetInt: ImagerState.StateSetInt,
SetReal: ImagerState.StateSetReal,
SetT: ImagerState.StateSetT,
SetFont: ImagerState.StateSetFont,
SetColor: ImagerState.StateSetColor,
SetClipper: ImagerState.StateSetClipper,
GetInt: ImagerState.StateGetInt,
GetReal: ImagerState.StateGetReal,
GetT: ImagerState.StateGetT,
GetFont: ImagerState.StateGetFont,
GetColor: ImagerState.StateGetColor,
GetClipper: ImagerState.StateGetClipper,
ConcatT: ImagerState.StateConcatT,
Scale2T: ImagerState.StateScale2T,
RotateT: ImagerState.StateRotateT,
TranslateT: ImagerState.StateTranslateT,
Move: ImagerState.StateMove,
SetXY: ImagerState.StateSetXY,
SetXYRel: ImagerState.StateSetXYRel,
GetCP: ImagerState.StateGetCP,
StartUnderline: ImagerState.StateStartUnderline,
MaskUnderline: ImagerState.StateMaskUnderline,
CorrectMask: ImagerState.StateCorrectMask,
CorrectSpace: ImagerState.StateCorrectSpace,
Space: ImagerState.StateSpace,
SetCorrectMeasure: ImagerState.StateSetCorrectMeasure,
SetCorrectTolerance: ImagerState.StateSetCorrectTolerance,
Correct: ImagerState.StateCorrect,
DontCorrect: ImagerState.StateDontCorrect,
SetGray: ImagerState.StateSetGray,
SetSampledColor: ImagerState.StateSetSampledColor,
SetSampledBlack: ImagerState.StateSetSampledBlack,
Clip: ImagerState.StateClip,
ClipRectangle: ImagerState.StateClipRectangle,
ClipRectangleI: ImagerState.StateClipRectangleI,
Show: MyShow,
ShowBackward: MyShowBackward,
MaskFill: MyMaskFill,
MaskRectangle: MyMaskRectangle,
MaskStroke: MyMaskStroke,
MaskPixel: MyMaskPixel,
ShowAndFixedXRel: ImagerPrivate.DefaultShowAndFixedXRel,
ShowText: MyShowText,
MaskRectangleI: MyMaskRectangleI,
MaskVector: MyMaskVector,
MaskDashedStroke: MyMaskDashedStroke,
MaskBitmap: MyMaskBitmap,
DrawBitmap: MyDrawBitmap,
DrawPixels: ImagerPrivate.DefaultDrawPixels,
DoIfVisible: ImagerPrivate.DefaultDoIfVisible,
DoWithBuffer: ImagerPrivate.DefaultDoWithBuffer,
DrawObject: ImagerPrivate.DefaultDrawObject,
GetBounds: ImagerPrivate.DefaultGetBounds,
ViewReset: ImagerPrivate.DefaultViewReset,
ViewTranslateI: ImagerPrivate.DefaultViewTranslateI,
ViewClip: ImagerPrivate.DefaultViewClip,
ViewClipRectangleI: ImagerPrivate.DefaultViewClipRectangleI,
GetTransformation: ImagerPrivate.DefaultGetTransformation,
Transform: ImagerPrivate.DefaultTransform,
MoveViewRectangle: ImagerPrivate.DefaultMoveViewRectangle,
TestViewRectangle: ImagerPrivate.DefaultTestViewRectangle,
GetBufferColorOperator: ImagerPrivate.DefaultGetBufferColorOperator,
AccessBuffer: ImagerPrivate.DefaultAccessBuffer,
SaveBuffer: ImagerPrivate.DefaultSaveBuffer,
RestoreBuffer: ImagerPrivate.DefaultRestoreBuffer,
DiscardBuffer: ImagerPrivate.DefaultDiscardBuffer
]];
END.