! EDWIN PDF DECODE PROGRAM

%include "ECSC17.IMP77SPECS"

%own %integer XL = 0;      !Origin of virtual window (Left edge)
%own %integer XR = 1023;   !(Right edge)
%own %integer YB = 0;      !(Bottom edge)
%own %integer YT = 1023;   !(Top edge)
%own %integer A, B

%const %string (15) %array COM (0:15) = "Line abs", "Move abs", "Marker abs",
   "Line rel", "Move rel", "Marker rel", "Instance", "End sub", "Window",
   "Character", "", "Newframe", "Terminate EDWIN", "Unknown call" (2), ""

%const %string (31) %array ATT (0:15) = "Set colour", "Set line style",
   "Set char size", "Set char rot", "Set char quality", "Set char font",
   "Set char slant", "Set intensity", "Set speed",  "Set unknown att" (6),
   "Aspect ratioing"

%routine PHEX (%integer I)
   %const %byte %integer %array A(0:15) = '0', '1', '2', '3', '4',
    '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
   %integer L

   SPACE
   %for L=12,-4,0 %cycle
        PRINT SYMBOL (A(I>>L&15))
   %repeat
%end


%routine INTERPRET
   ! PC=0 => PDF read from the input stream, else read from the array.
   !Interpret instructions in display file starting
   !at (relative) PC until an END instruction is found
   !For more information on display file layout, see GRAFIX.PDFDOC.
   !Codes are   0 LINEA   1 MOVEA   2 MARKERA
   !            3 LINER   4 MOVER   5 MARKERR
   !            6 SUBPIC  7 old END 8 WINDOW
   !            9 CHAR   10 ATTRIBUTES  11  NEWFRAME   12  END
   %integer WORD,CODE,R,X,Y,Z,P
   %switch C(0:15)

   %routine GET (%integername N)
      %on 3,4 %start
          PRINT STRING ("PDFDEC fails - Corrupt PDF")
          NEWLINE
          %stop
      %finish
      READ(N)
   %end

   %cycle
      GET (WORD)
      CODE=WORD&15
      %if CODE<=5 %start;  !Draw, Move, Point
          GET(X)
          A = X
          %if WORD&16=0 %start; !Long form
              GET(Y)
          %finish%else %start
              Y=X&255; X=X>>8&255
              X=X!!(\255) %if X&128#0
              Y=Y!!(\255) %if Y&128#0
          %finish
    %finish
    ->C(CODE)


C(0): C(1): C(2): C(3): C(4): C(5): ! MOVE LINE and MARKERs
      PHEX (WORD)
      PHEX (A)
      %if WORD&16=0 %then PHEX (Y) %else SPACES (5)
      SPACES (20)
      PRINT STRING (COM(CODE)." (")
      WRITE (WORD>>12&15,0) %and PRINT STRING (", ") %if CODE=2 %or CODE=5
      WRITE (X,0);   PRINT SYMBOL (',')
      WRITE (Y,1);   PRINT SYMBOL (')')
      NEWLINE
      %continue

C(6): GET(A)
      R=WORD>>4&7
      GET(Z); ! Sub pic size
      PHEX (WORD);   PHEX (A);   PHEX (Z);   SPACES (20)
      PRINT STRING (COM(CODE)." (")
      WRITE (A,0);   PRINT STRING (", ")
      WRITE (Z,0);   PRINT STRING (", ")
      WRITE (R,0);   PRINT STRING (", 0, 0)")
      NEWLINE
      %continue

C(8): ! SET new WINDOW
      GET(XL);  GET(XR);  GET(CODE);  GET (YB);  GET (YT)
      PHEX (WORD);  PHEX (XL);  PHEX (XR);  PHEX (CODE);  PHEX (YB);  PHEX (YT)
      SPACES (5)
      PRINT STRING (COM(8)." (")
      WRITE (XL,0);   PRINT SYMBOL (',')
      WRITE (XR,1);   PRINT SYMBOL (',')
      WRITE (YB,1);   PRINT SYMBOL (',')
      WRITE (YT,1);   PRINT SYMBOL (')')
      NEWLINE
      %continue

C(7): ! End sub
C(9): ! Character
C(11): ! Newframe
C(12): ! Terminate
      PHEX (WORD)
      SPACES (30)
      PRINT STRING (COM(CODE))
      %if CODE=9 %start
          PRINT STRING (" (")
          A = WORD >> 4
          %if 32<= A <= 127 %start
              PRINT SYMBOL ('''')
              PRINT SYMBOL (A)
              PRINT SYMBOL ('''')
          %finish %else WRITE (A,0)
          PRINT SYMBOL (')')
      %finish
      NEWLINE
      %continue

C(15):
C(10): %if CODE=10 %then A=WORD>>4&255 %else GET(A)
       B = WORD >> 12 & 15
       PHEX (WORD)
       %if CODE=10 %then SPACES(5) %else PHEX (A)
       SPACES (25)
       PRINT STRING (COM (CODE).ATT(B)." (")
       WRITE (A,0)
       PRINT SYMBOL (')')
       NEWLINE

  %repeat

C(*): PHEX (WORD)
      SPACES (30)
      PRINT STRING ("Unknown PDF command")
      NEWLINE
      PRINT STRING ("******   ANALYSIS  ABANDONED  *******")
      NEWLINE
      SELECT OUTPUT (0)
      PRINT STRING ("******   Analysis  abandoned  *******")
      NEWLINE
      %stop

%end



%external %routine PDFDEC (%string (255) PARM)
   %external %routine %spec SET DEFAULT (%string (7) PARM)
   %string (127) IN, OUT

   %on 4,9 %start
      %if EVENT_SUB>=2 %start
          PRINT STRING ("PDFDEC fails - Cannot open file ".PARM)
          NEWLINE
      %finish
      %stop
   %finish

   %if PARM = "" %start
       SELECT INPUT (0)
       PROMPT ("Parameters:")
       SKIP SYMBOL %while NEXT SYMBOL <= 32
       PARM = PARM . TO STRING (NEXT SYMBOL) %and SKIP SYMBOL %until NEXT SYMBOL <= 32
   %finish

   IN = PARM %and OUT = ".OUT" %unless PARM -> IN.("/").OUT
   SET DEFAULT ("#PDF")
   OPEN INPUT (1,IN)
   SET DEFAULT ("#DEC")
   OPEN OUTPUT (2,OUT)

   SELECT INPUT (1)
   SELECT OUTPUT (2)
   INTERPRET
%end

%end %of %file

