ImagerTester.mesa
Michael Plass, January 17, 1984 10:52 am
DIRECTORY
Basics,
Imager,
ImagerBasic,
ImagerTransform,
RealFns,
Rope,
Scaled,
Font;
ImagerTester: CEDAR PROGRAM
IMPORTS Basics, Imager, ImagerTransform, Font, RealFns, Scaled
= BEGIN
Pair: TYPE ~ Imager.Pair;
Context: TYPE ~ Imager.Context;
FONT: TYPE ~ Imager.FONT;
ROPE: TYPE ~ Rope.ROPE;
mercuryPath: REF ← MercuryPath[ImagerTransform.Rotate[0]];
MercuryPath: PROC [m: ImagerBasic.Transformation] RETURNS [t: Imager.Trajectory] = {
r: ImagerTransform.TransformationRec ← m.Contents;
X: PROC[p: Pair] RETURNS [Pair] ~ {
RETURN [[r.a*p.x + r.b*p.y + r.c, r.d*p.x + r.e*p.y + r.f]]
};
Curve: PROC [a,b,c,d,e,f: REAL] ~ {t ← t.CurveTo[X[[a, b]], X[[c, d]], X[[e, f]]]};
t ← Imager.MoveTo[X[[192.0, 413.353]]];
Curve[237.5415, 425.809, 255.3458, 401.7666, 288.0, 399.5085];
Curve[297.9937, 398.8174, 308.0844, 403.6591, 318.0, 404.5501];
Curve[331.9086, 405.7998, 448.1434, 401.3464, 450.8684, 396.0001];
Curve[453.3393, 391.1521, 435.2951, 383.4805, 431.9999, 383.0218];
Curve[428.5259, 382.538, 410.233, 382.6307, 409.5, 378.0];
Curve[409.8045, 379.9238, 421.3876, 370.5168, 402.0001, 363.6874];
Curve[386.3301, 358.1678, 345.4535, 362.3779, 345.5, 359.9999];
Curve[345.4942, 360.2959, 368.3762, 359.2238, 372.0, 358.0435];
Curve[389.1935, 352.4433, 364.4147, 343.7253, 364.875, 336.0];
Curve[365.5193, 325.1856, 382.3762, 294.0994, 390.0, 285.675];
Curve[408.286, 265.4687, 417.344, 272.0564, 438.0001, 263.8696];
Curve[454.6547, 257.2686, 417.2523, 242.3468, 408.0, 243.5193];
Curve[383.1711, 246.6655, 383.3985, 232.3274, 366.0, 219.439];
Curve[352.0869, 209.1325, 366.6143, 208.1149, 372.0, 208.9024];
Curve[374.2727, 209.2348, 386.2989, 214.7218, 380.8284, 204.0];
Curve[378.6221, 199.6753, 364.5704, 185.5353, 360.0, 184.8125];
Curve[351.8828, 183.5287, 332.7219, 196.0891, 329.1539, 192.0];
Curve[321.1773, 182.8585, 343.6019, 183.1676, 340.0227, 174.0];
Curve[335.0409, 161.2398, 311.5035, 158.2857, 310.4737, 126.0];
Curve[310.1649, 116.3195, 313.5754, 76.22575, 312.75, 72.0];
Curve[310.7936, 61.98325, 261.8854, 53.60412, 252.0, 52.62712];
Curve[238.5165, 51.29451, 155.6176, 53.91593, 152.55, 65.99998];
Curve[146.1258, 91.30679, 146.1964, 75.57425, 114.0, 83.08163];
Curve[101.5249, 85.9905, 85.49357, 98.83845, 93.0, 114.0];
Curve[96.9852, 122.0493, 101.4202, 118.0211, 96.0, 128.775];
Curve[92.91811, 134.8897, 92.05819, 140.964, 90.00001, 147.577];
Curve[85.48869, 162.0718, 88.40478, 162.2188, 86.83928, 168.0];
Curve[84.31547, 177.3202, 68.65387, 174.0181, 70.27779, 186.0];
Curve[71.84946, 197.5966, 93.93316, 251.462, 87.75, 258.0];
Curve[83.8855, 262.0863, 74.20536, 262.4716, 73.98215, 270.0];
Curve[73.80345, 276.027, 80.97349, 281.9097, 79.26, 288.0];
Curve[76.91487, 296.3353, 44.77308, 305.2533, 60.08572, 324.0];
Curve[64.14451, 328.969, 78.96831, 335.1704, 67.77274, 342.0];
Curve[60.79922, 346.254, -5.558746, 358.62, 30.00002, 372.3488];
Curve[45.19923, 378.2171, 84.23178, 361.5336, 114.0, 376.1087];
Curve[140.1327, 388.9037, 163.6664, 405.2124, 192.0, 413.3529];
};
pa: ImagerBasic.PixelArray ← NEW[ImagerBasic.PixelArrayRep ← [
xPixels: 20,
yPixels: 18,
maxSampleValue: 1,
samplesPerPixel: 1,
m: ImagerTransform.Scale[1],
get: PAGet,
data: NIL
]];
checks: ImagerBasic.SampledColor ← NEW[ImagerBasic.ColorRep.sampled ← [sampled[
transparent: FALSE,
pa: pa,
m: ImagerTransform.Scale[2],
colorOperator: $SampledBlack
]]];
PAGet: PROC [
self: ImagerBasic.PixelArray,
buffer: ImagerBasic.PixelBuffer,
object implementation fills this with the requested number of samples.
nSamples: NAT,
number of samples to put in buffer.
layer: INT,
in the range [0..samplesPerPixel)
xStart, yStart: Scaled.Value,
position of first sample.
xDelta, yDelta: Scaled.Value
increment to get to successive samples.
] ~ {
FOR i: NAT IN [0..nSamples) DO
WHILE xStart.Floor < 0 DO xStart ← xStart.PLUS[Scaled.FromInt[self.xPixels]] ENDLOOP;
WHILE yStart.Floor < 0 DO yStart ← yStart.PLUS[Scaled.FromInt[self.yPixels]] ENDLOOP;
WHILE xStart.Floor >= self.xPixels DO xStart ← xStart.MINUS[Scaled.FromInt[self.xPixels]] ENDLOOP;
WHILE yStart.Floor >= self.yPixels DO yStart ← yStart.MINUS[Scaled.FromInt[self.yPixels]] ENDLOOP;
buffer[i] ← Basics.BITAND[1, Basics.BITXOR[xStart.Floor, yStart.Floor]];
xStart ← xStart.PLUS[xDelta];
yStart ← yStart.PLUS[yDelta];
ENDLOOP;
};
RotMerc: PROC [context: Context, data: REFNIL] = {
gray: Imager.Color ← Imager.MakeGray[0.5];
{do: PROC = {
context.ScaleT[3.527777e-4];
context.TranslateT[4.25*72, 5.5*72];
context.ScaleT[0.333];
{action: PROC = {
context.SetColor[Imager.black];
context.MaskFill[mercuryPath];
FOR i: NAT ← 0, i+2 WHILE i<9 DO
context.SetColor[gray];
context.RotateT[-20];
context.MaskFill[mercuryPath];
context.SetColor[Imager.black];
context.RotateT[-20];
context.MaskFill[mercuryPath];
ENDLOOP;
}; context.DoSave[action]};
{action: PROC = {
FOR i: NAT IN [0..2) DO
context.ExcludeOutline[mercuryPath];
context.RotateT[-20];
ENDLOOP;
context.RotateT[-160];
FOR i: NAT ← 10, i+2 WHILE i<18 DO
context.SetColor[gray];
context.RotateT[-20];
context.MaskFill[mercuryPath];
context.SetColor[Imager.black];
context.RotateT[-20];
context.MaskFill[mercuryPath];
ENDLOOP;
}; context.DoSave[action]};
}; context.DoSave[do]};
};
PageCorners: PROC [context: Context, data: REFNIL] = {
bounds: Imager.IntRectangle;
arrowHead: Imager.Trajectory;
theta: REAL;
bounds ← Imager.GetView[context];
theta ← RealFns.ArcTanDeg[bounds.h, bounds.w];
arrowHead ← Imager.MoveTo[[0,0]].CurveTo[[0,.5],[0,.6],[.5,1]].LineTo[[0, .75]].LineTo[[-.5,1]].CurveTo[[0,.6],[0,.5],[0,0]];
{do: PROC = {
context.ScaleT[3.527777e-4];
{action: PROC = {
context.RotateT[theta-90];
context.ScaleT[80];
context.MaskFill[arrowHead];
}; context.DoSave[action]};
{action: PROC = {
context.TranslateT[bounds.w, 0];
context.RotateT[90-theta];
context.ScaleT[80];
context.MaskFill[arrowHead];
}; context.DoSave[action]};
{action: PROC = {
context.TranslateT[0, bounds.h];
context.RotateT[-90-theta];
context.ScaleT[80];
context.MaskFill[arrowHead];
}; context.DoSave[action]};
{action: PROC = {
context.TranslateT[bounds.w, bounds.h];
context.RotateT[90+theta];
context.ScaleT[80];
context.MaskFill[arrowHead];
}; context.DoSave[action]};
context.MaskStroke[Imager.MoveTo[[0, 0]].LineTo[[bounds.w, bounds.h]], 2, butt];
context.MaskStroke[Imager.MoveTo[[bounds.w, 0]].LineTo[[0, bounds.h]], 2, butt];
}; context.DoSave[do]};
};
textSize: REAL ← 12.0;
Times: PROC [size: REAL] RETURNS [FONT] ~ {
RETURN [Font.CreateScaled["Xerox/PressFonts/TimesRoman/MRR", size, $Ideal]]
};
fontName: ROPE ← "Xerox/PressFonts/TimesRoman/MRR";
MaskTxt: PROC[context: Context, data: REF] = {
ropes: LIST OF REFNARROW[data];
row: INT ← 0;
font: FONT ← Font.CreateScaled[fontName, textSize, $Ideal];
{action: PROC = {
context.ScaleT[3.527777e-4];
context.TranslateT[72, 720];
FOR list: LIST OF REF ← ropes, list.rest UNTIL list=NIL DO
row ← row + 1;
context.SetXY[[0, -row * textSize]];
context.ShowCharacters[list.first, font];
ENDLOOP;
}; context.DoSave[action]};
};
MaskText: PROC[context: Context, data: REF] = {
ropes: LIST OF REFNARROW[data];
curfont: FONT ← Font.CreateScaled["Xerox/PressFonts/TimesRoman/MRR", textSize, $Ideal];
altfont: FONT ← Font.CreateScaled["Xerox/PressFonts/Helvetica/MRR", textSize, $Ideal];
row: INT ← 0;
{action: PROC = {
context.ScaleT[3.527777e-4];
context.TranslateT[72, 720];
FOR list: LIST OF REF ← ropes, list.rest UNTIL list=NIL DO
tempfont: FONT ~ curfont;
row ← row + 1;
context.SetXY[[0, -row * textSize]];
context.ShowCharacters[list.first, tempfont];
curfont ← altfont; altfont ← tempfont;
ENDLOOP;
}; context.DoSave[action]};
};
justifiedMeasure: REAL ← 0;
MaskJustifiedText: PROC[context: Context, data: REF] = {
ropes: LIST OF REFNARROW[data];
curfont: FONT ← Font.CreateScaled["Xerox/PressFonts/TimesRoman/MRR", textSize, $Ideal];
altfont: FONT ← Font.CreateScaled["Xerox/PressFonts/Helvetica/MRR", textSize, $Ideal];
row: INT ← 0;
maxWidth: REAL ← 0;
nWidths: INT ← 0;
totalWidths: REAL ← 0;
FOR list: LIST OF REF ← ropes, list.rest UNTIL list=NIL OR list.rest = NIL DO
tempfont: FONT ~ curfont;
width: REAL ~ tempfont.TextWidth[list.first];
maxWidth ← MAX[maxWidth, width];
nWidths ← nWidths + 1;
totalWidths ← totalWidths + width;
curfont ← altfont; altfont ← tempfont;
ENDLOOP;
curfont ← Font.CreateScaled["Xerox/PressFonts/TimesRoman/MRR", textSize, $Ideal];
altfont ← Font.CreateScaled["Xerox/PressFonts/Helvetica/MRR", textSize, $Ideal];
{action: PROC = {
context.ScaleT[3.527777e-4];
context.TranslateT[72, 720];
IF nWidths > 0 THEN context.SetCorrectMeasure[[justifiedMeasure ← (maxWidth+totalWidths/nWidths)/2.0, 0]];
FOR list: LIST OF REF ← ropes, list.rest UNTIL list=NIL DO
tempfont: FONT ~ curfont;
Show: PROC ~ {context.ShowCharacters[list.first, tempfont]};
row ← row + 1;
context.SetXY[[0, -row * textSize]];
IF list.rest = NIL THEN Show[] ELSE context.Correct[Show];
curfont ← altfont; altfont ← tempfont;
ENDLOOP;
}; context.DoSave[action]};
};
Text: PROC RETURNS [ropes: LIST OF REF] = {
new: LIST OF REFNIL;
txt: LIST OF ROPE ← text;
WHILE txt # NIL DO new ← CONS[txt.first, new]; txt ← txt.rest ENDLOOP;
WHILE new # NIL DO tt: LIST OF REF ← new; new ← new.rest; tt.rest ← ropes; ropes ← tt ENDLOOP;
};
text: LIST OF ROPELIST["In olden times when wishing still", "helped one, there lived a king", "whose daughters were all", "beautiful, but the youngest was", "so beautiful that the sun itself,", "which has seen so much, was", "astonished whenever it shone in", "her face. Close by the king's", "castle lay a great dark forest,", "and under an old lime-tree in", "the forest was a well, and when", "the day was very warm, the", "king's child went out into the", "forest and sat down by the side", "of the cool fountain, and when", "she was bored she took a golden", "ball, and threw it up on high", "and caught it, and this ball was", "her favorite plaything."];
END.