-- $Date: 2004/02/23 08:37:51 $
-- $Revision: 1.6 $
-- $Author: jcrocholl $

with Readers; use Readers;
with Token_Readers; use Token_Readers;

with Writers; use Writers;
with Indent_Writers; use Indent_Writers;

with Real_Vectors; use Real_Vectors;
with Real_Strings; use Real_Strings;
with Integer_Strings; use Integer_Strings;
with Messages; use Messages;

with Lines; use Lines;
with Outlines; use Outlines;
with Outlines.SVG; use Outlines.SVG;
with Glyphs; use Glyphs;

package body Glyphs.SVG is

   -- Read outlines from an XML reader.
   procedure Read_Outlines
     (XML  : access XML_Reader-- Use this reader.
      This : access Glyph) is   -- Add outlines to this glyph.
   begin
      -- Debug("reading outlines");
      Skip_Whitespace(XML); Assert_Char(XML, '=');
      Skip_Whitespace(XML); Assert_Char(XML, '"');
      loop
         Add_Outline(This, Read(XML));
         exit when Found_Char(XML, 'Z');
      end loop;
      Get(XML);
      Assert_Char(XML, '"');
   end Read_Outlines;

   -- Read bounds from an XML reader.
   procedure Read_Bounds
     (XML  : access XML_Reader-- Use this reader.
      This : access Glyph)      -- Add bounds to this glyph.
   is
      Left, Bottom, Right, Top : Real;
   begin
      -- Debug("reading bounds");
      Skip_Whitespace(XML); Assert_Char(XML, '=');
      Skip_Whitespace(XML); Assert_Char(XML, '"');
      Left := Read_Real(XML);
      Bottom := Read_Real(XML);
      Right := Read_Real(XML);
      Top := Read_Real(XML);
      Set_Bounds(This, Left, Bottom, Right, Top);
      Assert_Char(XML, '"');
   end Read_Bounds;

   -- Read a glyph from an XML reader.
   function Read
     (XML : access XML_Reader-- Use this reader.
     return Glyph_Access       -- The newly created glyph.
   is
      Result : Glyph_Access := Create;
   begin
      -- Debug("reading glyph");
      -- Set_Bounds(Result, Left, Bottom, Right, Top);

      while not End_Of_Tag(XML) loop
         Read_Attribute_Name(XML);
         if Found(XML, "transform") then
            Read_Attribute_Value(XML);
         elsif Found(XML, "roemer:bounds") then
            Read_Bounds(XML, Result);
         elsif Found(XML, "d") then
            Read_Outlines(XML, Result);
         else
            XML_Expect_Error(XML, "", "transform" / "roemer:bounds" / "d");
         end if;
      end loop;
      Exit_Tag(XML);

      return Result;
   end Read;

   -- Write a glyph to an XML writer.
   procedure Write
     (XML       : access XML_Writer-- Use this writer.
      This      : access Glyph;      -- Write this glyph.
      Tolerance : in Real;           -- Tolerance level.
      Translate : in Vector)         -- Output offset.
   is
      use Glyphs.Outline_Lists;
      Outlines : Outline_List_Access;
      Outline  : Outline_Access;
      Bounds   : Rectangle := Get_Bounds(This);
   begin
      Start_Tag(XML, "path");
      Write_Attribute(XML, "transform", "translate(" &
        To_String(Translate.X - (Bounds.Left + Bounds.Right) / 2.0, Tolerance) & " " &
        To_String(Translate.Y - (Bounds.Bottom + Bounds.Top) / 2.0, Tolerance) & ")");
      Write_Attribute(XML, "roemer:bounds", To_String(Bounds, Tolerance));

      Write_Attribute_Name(XML, "d");
      Put_Line(XML, "=""");
      Outlines := Get_Outlines(This);
      Reset(Outlines);
      while Next(Outlines) loop
         Outline := Current(Outlines);
         Write(XML, Outline, Tolerance);
      end loop;
      Close_Tag(XML, "Z"" /");
      New_Line(XML);
   end Write;

end Glyphs.SVG;