<<>> <> <> <> <> <> <<>> 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 ROPE: TYPE ~ Rope.ROPE; 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.