PressRescueImpl.mesa
Michael Plass, April 25, 1984 12:15:33 pm PST
DIRECTORY
Commander, FS, IO, PressReader, Rope, Real, PressPrinter, PressFontReader;
PressRescueImpl: CEDAR PROGRAM
IMPORTS Commander, FS, IO, PressReader, Rope, Real, PressPrinter, PressFontReader ~ BEGIN
ROPE: TYPE ~ Rope.ROPE;
bytesPerPage: NAT ~ 512; -- as per press file format.
micasPerPoint: REAL = 2540.0/72.0;
pointsPerMica: REAL = 72.0/2540.0;
pressFontFile: PressFontReader.Handle ← PressFontReaderFromFile["[Indigo]<Fonts>Fonts.Widths"];
pressFonts: LIST OF PressFontReader.Font ← PressFontReaderListFonts[pressFontFile];
PressFontReaderFromFile: PROC [rope: ROPE] RETURNS [PressFontReader.Handle]
~ TRUSTED {RETURN [PressFontReader.FromFile[rope]]};
PressFontReaderListFonts: PROC [pressFontFile: PressFontReader.Handle] RETURNS [LIST OF PressFontReader.Font]
~ TRUSTED {RETURN [PressFontReader.ListFonts[pressFontFile]]};
RescuePressPage: PROC [stream: IO.STREAM, pressFile: PressReader.Handle, pageNumber: INT, fontTable: REF ARRAY [0..6*16) OF PressReader.FontDirectoryEntry] RETURNS [ok: BOOLEANTRUE] = TRUSTED {
PageProc: PressReader.PageProc = TRUSTED {
EntityProc: PressReader.EntityProc = TRUSTED { -- Xe, Ye, fontSet
xe: INT ← entityTrailer.Xe;
ye: INT ← entityTrailer.Ye;
x: INT ← xe;
y: INT ← ye;
lastShowX, lastShowY: INT ← -1;
currentFont: PressReader.FontDirectoryEntry; -- will be established by setFont
currentPressFont: PressFontReader.Font ← NIL; -- will be established by setFont
currentMicaSize: NAT ← 0; -- will be established by setFont
currentSpaceX, currentSpaceY: INT; -- will be established by setFont
hue: REAL ← 0.0;
saturation: REAL ← 1.0;
brightness: REAL ← 0.0;
skipAlternative, inAlternative: BOOLFALSE;
reposition: BOOLEANFALSE;
showCharactersProc: PressReader.ShowCharactersProc = TRUSTED {
xw, yw: INT ← 0;
IF skipAlternative THEN RETURN;
IF lastShowY # y THEN {
stream.PutChar['\n];
lastShowX ← 2540;
};
IF ABS[x - lastShowX] < 50 THEN NULL
ELSE FOR i: INT ← x - lastShowX, i - 175 UNTIL i <= 0 DO
stream.PutChar[' ]
ENDLOOP;
FOR i: INT IN [0..text.Length[]) DO
c: CHAR ← text.Fetch[i];
IF c = ' THEN {
xw ← xw+currentSpaceX;
yw ← yw+currentSpaceY;
}
ELSE IF currentPressFont # NIL THEN {
charInfo: PressFontReader.CharInfo ← PressFontReader.GetCharInfo[currentPressFont, c];
xw ← xw+Real.RoundLI[charInfo.widthX*currentMicaSize];
yw ← yw+Real.RoundLI[charInfo.widthY*currentMicaSize];
}
ENDLOOP;
stream.PutRope[text];
x ← x + xw;
y ← y + yw;
lastShowX ← x;
lastShowY ← y;
}; -- showCharactersProc
fontProc: PressReader.FontProc = TRUSTED {
IF skipAlternative THEN RETURN;
currentFont ← fontTable[entityTrailer.fontSet*16+font];
currentMicaSize ← IF currentFont.size < 0 THEN -currentFont.size ELSE Real.RoundLI[currentFont.size/72.0*2540.0];
FOR l: LIST OF PressFontReader.Font ← pressFonts, l.rest UNTIL l = NIL DO
fontRec: PressFontReader.FontInfoRec ← l.first.info;
IF Rope.Equal[fontRec.family, currentFont.family, FALSE]
AND (fontRec.face = currentFont.face.encoding)
AND ((fontRec.size = 0.0) OR (ABS[fontRec.size - currentMicaSize*(1.0E-5)] < 4.0E-5))
AND ((fontRec.rotation = currentFont.rotation) OR ((fontRec.rotation = 0) AND (currentFont.rotation MOD 5400 = 0))) THEN {
currentPressFont ← l.first;
EXIT;
};
ENDLOOP;
};
positionProc: PressReader.PositionProc = TRUSTED {
IF skipAlternative THEN RETURN;
IF opCode = setX THEN x ← xe + value
ELSE y ← ye + value;
reposition ← TRUE;
};
spacingProc: PressReader.SpacingProc = TRUSTED {
IF skipAlternative THEN RETURN;
SELECT opCode FROM
setSpaceX, setSpaceXShort => currentSpaceX ← value;
setSpaceY, setSpaceYShort => currentSpaceY ← value;
resetSpace => {
currentSpaceX ← 200;
currentSpaceY ← 0;
};
ENDCASE => ERROR;
IF opCode = resetSpace THEN NULL ELSE NULL;
};
spaceProc: PressReader.SpaceProc = TRUSTED {
showCharactersProc[showCharacterImmediate, 1, " "];
};
colorProc: PressReader.ColorProc = TRUSTED {
IF skipAlternative THEN RETURN;
SELECT opCode FROM
setHue => NULL;
setSaturation => NULL;
setBrightness => NULL;
ENDCASE => ERROR;
};
showRectangleProc: PressReader.ShowRectangleProc = TRUSTED {
IF skipAlternative THEN RETURN;
NULL;
};
alternativeProc: PressReader.AlternativeProc = TRUSTED {
IF (types = 0) AND (elBytes = 0) AND (dlBytes = 0) THEN
inAlternative ← skipAlternative ← FALSE
ELSE IF inAlternative THEN skipAlternative ← TRUE
ELSE inAlternative ← TRUE;
};
showObjectProc: PressReader.ShowObjectProc = TRUSTED {
NULL;
}; -- showObjectProc
showDotsProc: PressReader.ShowDotsProc = TRUSTED {
NULL;
}; -- showDotsProc
fontProc[0];
pressFile.GetCommands[PressReader.CommandProcs[
showCharactersProc: showCharactersProc,
fontProc: fontProc,
positionProc: positionProc,
spacingProc: spacingProc,
spaceProc: spaceProc,
colorProc: colorProc,
showRectangleProc: showRectangleProc,
alternativeProc: alternativeProc,
showObjectProc: showObjectProc,
showDotsProc: showDotsProc
]];
}; -- EntityProc
pressFile.GetPage[EntityProc];
stream.PutRope["\n\n------------------------------------------------------------------------"];
}; -- PageProc
SkipFonts: PressReader.FontDirectoryProc = TRUSTED {
ok ← FALSE;
};
pressFile.GetParts[pageNumber, PageProc, SkipFonts];
};
Rescue: PROC [outputName, inputName1: ROPE] ~ TRUSTED {
pressFile1: PressReader.Handle ← PressReader.OpenPressFile[inputName1];
documentDirectory1: PressReader.DocumentDirectory ← pressFile1.GetDocumentDirectory;
pageNumber1: INT ← 1;
fontTable: REF ARRAY [0..6*16) OF PressReader.FontDirectoryEntry ← NEW[ARRAY [0..6*16) OF PressReader.FontDirectoryEntry];
FontInitProc1: PressReader.FontEntryProc = { -- build a list of needed fonts
fontTable[fontDirectoryEntry.fontSet*16 + fontDirectoryEntry.font] ← fontDirectoryEntry;
}; -- FontInitProc
stream: IO.STREAMFS.StreamOpen[outputName, $create];
pressFile1.GetFonts[FontInitProc1];
WHILE RescuePressPage[stream, pressFile1, pageNumber1, fontTable] DO
pageNumber1 ← pageNumber1 + 1;
ENDLOOP;
pressFile1.ClosePressFile[];
stream.Close;
};
Break: PROC [char: CHAR] RETURNS [IO.CharClass] = {
IF char = '← THEN RETURN [break];
IF char = ' OR char = '  OR char = ', OR char = '; OR char = '\n THEN RETURN [sepr];
RETURN [other];
};
FindFullName: PROC [inputName: ROPE] RETURNS [ROPE] ~ {
fullFName: ROPENIL;
fullFName ← FS.FileInfo[inputName ! FS.Error => CONTINUE].fullFName;
IF fullFName = NIL OR NOT PressPrinter.IsAPressFile[fullFName] THEN {
IF inputName.Find[".press", 0, FALSE] = -1 THEN {
inputName ← inputName.Concat[".press"];
};
fullFName ← FS.FileInfo[inputName].fullFName;
};
RETURN [fullFName]
};
PressRescueCommand: Commander.CommandProc ~ {
stream: IO.STREAMIO.RIS[cmd.commandLine];
GetToken: PROC RETURNS [rope: ROPE] = {
rope ← NIL;
rope ← stream.GetTokenRope[Break ! IO.EndOfStream => CONTINUE].token;
};
outputName: ROPE ← GetToken[];
gets: ROPE ← GetToken[];
inputName1: ROPE ← GetToken[];
IF NOT gets.Equal["←"] OR inputName1 = NIL THEN {cmd.out.PutRope["Specify output ← input, please"]; RETURN};
inputName1 ← FindFullName[inputName1 ! FS.Error => {
IF error.group = user THEN {cmd.out.PutRope[error.explanation]; cmd.out.PutChar['\n]; GOTO Quit} ELSE REJECT
}];
Rescue[outputName, inputName1];
cmd.out.PutRope[outputName];
cmd.out.PutRope[" written.\n"];
IF GetToken[].Length # 0 THEN {cmd.out.PutRope["Ignored: "]; cmd.out.PutRope[cmd.commandLine.Substr[stream.GetIndex]]; cmd.out.PutChar['\n]};
EXITS Quit => NULL
};
Commander.Register["PressRescue", PressRescueCommand, "Rescue the text portions of a press file (output ← inputRescue)"];
END.