(DEFINE-FILE-INFO PACKAGE "INTERLISP" READTABLE "INTERLISP" BASE 10) (FILECREATED " 9-Oct-2023 00:42:25" {DSK}frank>il>medley>gmedley>library>PDFSTREAM.;2 14029 :CHANGES-TO (FNS SEE-PDF OPEN-PDF-STREAM PS-TO-PDF PDFCONVERTER) (VARS PDFSTREAMCOMS) :PREVIOUS-DATE " 1-Oct-2023 20:53:05" {DSK}frank>il>medley>gmedley>library>PDFSTREAM.;1 ) (PRETTYCOMPRINT PDFSTREAMCOMS) (RPAQQ PDFSTREAMCOMS ((FILES (SYSLOAD) POSTSCRIPTSTREAM) [COMS (* ; "Hook into hardcopy interface") [ADDVARS [PRINTERTYPES ((PDF) (CANPRINT (PDF)) (STATUS TRUE) (PROPERTIES NILL) (SEND POSTSCRIPTSEND) (BITMAPSCALE POSTSCRIPT.BITMAPSCALE) (BITMAPFILE (PDF.HARDCOPYW FILE BITMAP SCALEFACTOR REGION ROTATION TITLE] [PRINTFILETYPES (PDF (TEST PDFFILEP) (EXTENSION (PDF)) (CONVERSION (TEXT PDF.TEXT TEDIT PDF.TEDIT] (IMAGESTREAMTYPES (PDF (OPENSTREAM OPEN-PDF-STREAM) (FONTCREATE POSTSCRIPT.FONTCREATE) (FONTSAVAILABLE POSTSCRIPT.FONTSAVAILABLE) (CREATECHARSET \CREATECHARSET.PSC] (VARS (DEFAULTPRINTERTYPE 'PDF)) (FNS PDFFILEP PDF.HARDCOPYW PDF.TEXT PDF.TEDIT) (P (FONTPROFILE.ADDDEVICE 'PDF 'POSTSCRIPT] (* ;; "") (* ;; "Implementation of PDF streams") (INITVARS (PDFCONVERTER NIL)) (* ; "Mac with ghostscript?") (ALISTS (PDF-CONVERTER-TEMPLATES ps2pdf pstopdf)) (GLOBALVARS PDFCONVERTER PDF-CONVERTER-TEMPLATES) (FNS OPEN-PDF-STREAM CLOSE-PDF-STREAM PS-TO-PDF) (FNS SEE-PDF) (FNS PDFCONVERTER))) (FILESLOAD (SYSLOAD) POSTSCRIPTSTREAM) (* ; "Hook into hardcopy interface") (ADDTOVAR PRINTERTYPES ((PDF) (CANPRINT (PDF)) (STATUS TRUE) (PROPERTIES NILL) (SEND POSTSCRIPTSEND) (BITMAPSCALE POSTSCRIPT.BITMAPSCALE) (BITMAPFILE (PDF.HARDCOPYW FILE BITMAP SCALEFACTOR REGION ROTATION TITLE)))) (ADDTOVAR PRINTFILETYPES (PDF (TEST PDFFILEP) (EXTENSION (PDF)) (CONVERSION (TEXT PDF.TEXT TEDIT PDF.TEDIT)))) (ADDTOVAR IMAGESTREAMTYPES (PDF (OPENSTREAM OPEN-PDF-STREAM) (FONTCREATE POSTSCRIPT.FONTCREATE) (FONTSAVAILABLE POSTSCRIPT.FONTSAVAILABLE) (CREATECHARSET \CREATECHARSET.PSC))) (RPAQQ DEFAULTPRINTERTYPE PDF) (DEFINEQ (PDFFILEP [LAMBDA (FILE) (* ; "Edited 23-Jun-2023 14:43 by rmk") (* ; "Edited 5-Mar-93 21:40 by rmk:") (* ; "Edited 14-Jan-93 10:56 by jds") (OR (CL:MEMBER (UNPACKFILENAME.STRING FILE 'EXTENSION) '("PDF") :TEST (FUNCTION STRING-EQUAL)) (CL:WHEN (STREAMP FILE) (SETFILEPTR FILE 0) (PROG1 (AND (EQ (BIN FILE) (CHARCODE %%)) (EQ (BIN FILE) (CHARCODE P)) (EQ (BIN FILE) (CHARCODE D)) (EQ (BIN FILE) (CHARCODE F))) (SETFILEPTR FILE 0)))]) (PDF.HARDCOPYW [LAMBDA (PDFFILE BITMAP SCALEFACTOR REGION Landscape? TITLE) (* ; "Edited 24-Jul-2023 10:37 by rmk") (* ; "Edited 23-Jun-2023 13:28 by rmk") (* ; "Edited 6-Mar-2023 22:43 by rmk") (LET ((PSTTMP (PACKFILENAME 'EXTENSION 'TMPPS 'BODY PDFFILE))) (PS-TO-PDF (POSTSCRIPT.HARDCOPYW PSTTMP BITMAP SCALEFACTOR REGION Landscape? TITLE) PDFFILE]) (PDF.TEXT [LAMBDA (FILE PDFFILE FONTS HEADING TABS) (* ; "Edited 1-Oct-2023 15:24 by rmk") (* ; "Edited 23-Jun-2023 13:23 by rmk") (* ; "Edited 7-Mar-2023 08:39 by rmk") (TEXTTOIMAGEFILE FILE PDFFILE 'PDF FONTS HEADING TABS `(REGION ,POSTSCRIPT.DEFAULT.PAGEREGION ROTATION ,(NOT (NOT POSTSCRIPT.TEXTFILE.LANDSCAPE ]) (PDF.TEDIT [LAMBDA (FILE PDFFILE) (* ; "Edited 23-Jun-2023 13:22 by rmk") (* ; "Edited 7-Mar-2023 08:39 by rmk") (LET ((TSTREAM (OPENTEXTSTREAM FILE))) (TEDIT.FORMAT.HARDCOPY FILE PDFFILE T NIL NIL NIL 'PDF) (CLOSEF TSTREAM]) ) (FONTPROFILE.ADDDEVICE 'PDF 'POSTSCRIPT) (* ;; "") (* ;; "Implementation of PDF streams") (RPAQ? PDFCONVERTER NIL) (* ; "Mac with ghostscript?") (ADDTOVAR PDF-CONVERTER-TEMPLATES (ps2pdf " " PSFILE " " PDFFILE " 2> " ERRORFILE) (pstopdf " " PSFILE " -o " PDFFILE " 2> " ERRORFILE)) (DECLARE%: DOEVAL@COMPILE DONTCOPY (GLOBALVARS PDFCONVERTER PDF-CONVERTER-TEMPLATES) ) (DEFINEQ (OPEN-PDF-STREAM [LAMBDA (FILE OPTIONS) (* ; "Edited 23-Sep-2023 15:38 by rmk") (* ; "Edited 22-Sep-2023 11:04 by rmk") (* ; "Edited 24-Jun-2023 14:49 by rmk") (* ;; "Open a temporary PS file, but set it up so that at closing it gets converted to PDF using an operating-system utility (if available), and then gets renamed to the original intended filename.") (* ;; "We have to stash the original filename someplace. We could put it in the tmp filename and then parse it out, but then we would have to worry about how unix filenames might parse against our {, }, etc. ") (* ;;  "Simplest thing for now is to just add an extra field at the end of the \POSTSCRIPTDATA record.") (* ;; "") (if [AND NIL (EQ 'LPT (FILENAMEFIELD FILE 'HOST] then (* ;; "If FILE is on the LPT device, we could just ssume that it can be printed directly, no point in converting. But then we would alo have to lie and give it a PDF extension so it thinks that we are heading to a PDF printer.") (OPENPOSTSCRIPTSTREAM FILE OPTIONS) else (CL:UNLESS (ASSOC (PDFCONVERTER) PDF-CONVERTER-TEMPLATES) (ERROR "A specified POSTSCRIPT-to-PDF converter cannot be found")) (SETQ FILE (OR (AND (NEQ FILE T) (OUTFILEP FILE)) (ERROR "PDF target file not found" FILE))) (LET ((PSSTREAM (OPENPOSTSCRIPTSTREAM (CONCAT "{UNIX}/tmp/medley-pdf-" (IDATE) "-" (RAND) ".ps") OPTIONS))) (STREAMPROP PSSTREAM 'AFTERCLOSE (CONS (FUNCTION CLOSE-PDF-STREAM))) (STREAMPROP PSSTREAM 'PDFTARGETINFO FILE) PSSTREAM]) (CLOSE-PDF-STREAM [LAMBDA (PSSTREAM) (* ; "Edited 22-Sep-2023 11:18 by rmk") (* ; "Edited 24-Jul-2023 10:37 by rmk") (* ; "Edited 17-Jul-2023 22:32 by rmk") (* ; "Edited 24-Jun-2023 13:57 by rmk") (* ;; "PSSTREAM is a postscript (maybe in tmp) rendition of what is intended to end up as a pdf. If we are going directly to a printer, we can probably just pass it along without worrying about conversion. In fact, in that case we probably should not have bothered even setting up the PDF stream.") (* ;; "But for a file we execute the PDFCONVERTER as a shell command to make a pdf, and then we rename it to the intended filename") (STREAMPROP PSSTREAM 'AFTERCLOSE NIL) (* ;  "Maybe just remove only CLOSE-PDF-STREAMfrom the list?") (LET ((TARGETINFO (STREAMPROP PSSTREAM 'PDFTARGETINFO NIL))) (CL:IF TARGETINFO (RENAMEFILE (PS-TO-PDF PSSTREAM) TARGETINFO) PSSTREAM)]) (PS-TO-PDF [LAMBDA (PSFILE PDFFILE DONTDELETE) (* ; "Edited 1-Oct-2023 15:18 by rmk") (* ; "Edited 23-Sep-2023 22:54 by rmk") (* ; "Edited 23-Jul-2023 22:30 by rmk") (* ; "Edited 24-Jun-2023 15:01 by rmk") (* ; "Edited 16-Jul-2022 13:06 by rmk") (* ; "Edited 8-Jul-2022 10:20 by rmk") (* ; "Edited 7-May-2022 22:40 by rmk") (* ; "Edited 7-Oct-2021 11:15 by rmk:") (* ;; "PSFILE is the name of a closed PS file on a DSK/UNIX device. This function uses the PDFCONVERTER utility to convert that to a parallel pdf file, which is then renamed to PDFFILE. ") (* ;; "DONTDELETE is just for debugging, keeps the /tmp/ files") (SETQ PSFILE (FULLNAME (TRUEFILENAME PSFILE))) (CL:UNLESS (INFILEP PSFILE) (ERROR "NO PS FILE TO CONVERT")) (CL:UNLESS (ASSOC (PDFCONVERTER) PDF-CONVERTER-TEMPLATES) (ERROR "A specified POSTSCRIPT-to-PDF converter cannot be found")) (SETQ PDFFILE (if PDFFILE then (TRUEFILENAME PDFFILE) else (PACKFILENAME 'EXTENSION 'pdf 'BODY PSFILE))) (LET ((ERRORFILE (PACKFILENAME 'EXTENSION 'error 'BODY PSFILE)) COMPLETIONCODE) (* ;; "PROCESS-COMMAND is currently from GITFNS. Not sure whether ShellCommand in UNIXUTILS is appropriate.") (* ;;  "We have to map the filenames down to Unix conventions: (not pseudohost or host, slashes, etc.") [SETQ COMPLETIONCODE (PROCESS-COMMAND (CONCATLIST (SUBLIS `((PSFILE \, (SLASHIT (TRUEFILENAME PSFILE) NIL T)) (PDFFILE \, (SLASHIT (TRUEFILENAME PDFFILE) NIL T)) (ERRORFILE \, (SLASHIT (TRUEFILENAME ERRORFILE) NIL T))) (ASSOC (PDFCONVERTER) PDF-CONVERTER-TEMPLATES] (* ;; "Now use Medley names") (CLOSEF? PSFILE) (CL:UNLESS DONTDELETE (DELFILE PSFILE)) (CLOSEF? ERRORFILE) (CL:WHEN (INFILEP ERRORFILE) (CL:WHEN (IGREATERP (PROG1 (GETFILEINFO ERRORFILE 'LENGTH) (CL:UNLESS DONTDELETE (DELFILE ERRORFILE))) 0) (ERROR "Cannot create PDF file for " PDFFILE))) (CL:WHEN (IGREATERP COMPLETIONCODE 0) (ERROR "Cannot create PDF file for " PDFFILE)) PDFFILE]) ) (DEFINEQ (SEE-PDF [LAMBDA (PDFFILE) (* ; "Edited 1-Oct-2023 20:47 by rmk") (* ; "Edited 26-Sep-2023 16:52 by rmk") (* ;; "Use the ShellOpener for this machine to open the PDF file outside of Medley") (ShellOpen (PACKFILENAME 'BODY PDFFILE 'EXTENSION 'PDF]) ) (DEFINEQ (PDFCONVERTER [LAMBDA NIL (SETQ PDFCONVERTER (OR PDFCONVERTER (MKATOM (UNIX-GETENV "MEDLEY-PDFCONVERTER")) (CAR (for TEMPLATE in PDF-CONVERTER-TEMPLATES thereis (ShellWhich (CAR TEMPLATE]) ) (DECLARE%: DONTCOPY (FILEMAP (NIL (3208 5822 (PDFFILEP 3218 . 4132) (PDF.HARDCOPYW 4134 . 4732) (PDF.TEXT 4734 . 5451) ( PDF.TEDIT 5453 . 5820)) (6262 13322 (OPEN-PDF-STREAM 6272 . 8408) (CLOSE-PDF-STREAM 8410 . 9697) ( PS-TO-PDF 9699 . 13320)) (13323 13721 (SEE-PDF 13333 . 13719)) (13722 14006 (PDFCONVERTER 13732 . 14004))))) STOP