<> <> 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.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: BOOLEAN _ TRUE] = 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: BOOL _ FALSE; reposition: BOOLEAN _ FALSE; 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.STREAM _ FS.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: ROPE _ NIL; 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.STREAM _ IO.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.