-- $Date: 2004/01/13 03:32:30 $
-- $Revision: 1.9 $
-- $Author: jcrocholl $

with Ada.Text_IO; use Ada.Text_IO;

with Parsers; use Parsers;
with Messages; use Messages;
with Real_Strings; use Real_Strings;
with Real_Vectors; use Real_Vectors;
with Lines; use Lines;
with Outlines; use Outlines;
with Glyphs; use Glyphs;

package body EPS is

   ----------------------------------
   -- Reading Encapsulated PostScript
   ----------------------------------

   -- Read an outline from a file.
   function Read
     (This : in Parser-- Read from this parser.
     return Outline is  -- The newly created glyph.
   begin
      return Read(This);
   end Read;

   -- Read a glyph from a file.
   function Read
     (From : in Parser-- Read from this parser.
     return Glyph       -- The newly created glyph.
   is
      Result : Glyph := Create("");
   begin
      while Next_Line(From) loop
         exit when not Found(From, "%");
         Debug(Rest_Of_Line(From));
         -- if Skip(From, "%%BoundingBox: ");
      end loop;
      -- Start := Read_Real_Vector(From);
      return Result;
   end Read;

   -- Read a glyph from a file.
   function Read
     (Filename : in String-- Read from this file.
     return Glyph           -- The newly created glyph.
   is
      From   : Parser;
      Result : Glyph;
   begin
      From := Open(Filename);
      Result := Read(From);
      Close(From);
      return Result;
   end Read;

   -- Read a glyph from current input.
   function Read
     return Glyph is -- The newly created glyph.
   begin
      return Read(Current_Input);
   end Read;

   ----------------------------------
   -- Writing Encapsulated PostScript
   ----------------------------------

   -- Write an outline to a file.
   procedure Write
     (This  : in Outline;          -- Write this outline.
      File  : in File_Type;        -- Write to this file.
      Debug : in Boolean := False) -- Add red boxes?
   is
      use Outlines.Line_Lists;
   begin
      Put_Line(File, Postscript(Last(This).To) & " moveto");
      Reset(This);
      while Line_Lists.Next(This) loop
         Put_Line(File, Postscript(Current(This)));
         if Debug then
            Put_Line(File, Postscript(Current(This).To - (2.0, 2.0)) & " 4 4 rectfill");
         end if;
      end loop;
   end Write;

   -- Write a glyph to a file.
   procedure Write
     (This  : in Glyph;            -- Write this glyph.
      File  : in File_Type;        -- Write to this file.
      Debug : in Boolean := False) -- Add red boxes?
   is
      use Glyphs.Outline_Lists;
      Outlines        : Outline_List;
      Current_Outline : Outline;
      Bounds          : Box := Get_Bounds(This);
   begin
      Put_Line(File, "%!PS-Adobe-2.0 EPSF-2.0");
      -- Put_Line(File, "%%Creator: roemer");
      -- Put_Line(File, "%%Pages: 1");
      Put_Line(File, "%%BoundingBox: " &
        Postscript(Get_Bottom_Left(Bounds)) & " " &
        Postscript(Get_Top_Right(Bounds)));
      Put_Line(File, "%%EndComments");

      if Debug then
         Put_Line(File, "1 0 0 setrgbcolor");
      end if;
      Outlines := Get_Outlines(This);
      Reset(Outlines);
      while Next(Outlines) loop
         Current_Outline := Current(Outlines);
         Write(Current_Outline, File, Debug);
      end loop;
      Put_Line(File, "0 0 0 setrgbcolor");
      if Debug
      then Put_Line(File, "stroke");
      else Put_Line(File, "fill");
      end if;
   end Write;

   -- Write a glyph to a file.
   procedure Write
     (This     : in Glyph;            -- Write this glyph.
      Filename : in String;           -- Write to this file.
      Debug    : in Boolean := False) -- Add red boxes?
   is
      File : File_Type;
   begin
      Create(File, Out_File, Filename);
      Write(This, File, Debug);
      Close(File);
   end Write;

   -- Write a glyph to current output.
   procedure Write
     (This  : in Glyph;               -- Write this glyph.
      Debug : in Boolean := False) is -- Add red boxes?
   begin
      Write(This, Current_Output, Debug);
   end Write;

end EPS;