-- $Date: 2004/01/13 03:34:14 $
-- $Revision: 1.14 $
-- $Author: jcrocholl $

with Ada.Command_Line; use Ada.Command_Line;

with Real_Numbers; use Real_Numbers;
with Real_Strings; use Real_Strings;
with Messages; use Messages;
with Glyphs; use Glyphs;
with Read_PBM; use Read_PBM;
with Stairs; use Stairs;
with Curves; use Curves;
with Transforms; use Transforms;
with EPS; use EPS;

procedure PBM2Glyph is
   Index    : Positive-- Command line argument index.
   Exit_Now : Boolean;  -- Exit program instantly?

   Staff_Height : Real-- Height of staff in pixels.
   Center_X     : Real-- Horizontal coordinate of center pixel.
   Center_Y     : Real-- Vertical coordinate of center pixel.

   Pixels   : Boolean := False; -- Keep pixel stairs.
   Straight : Boolean := False; -- Don't make cubic bezier curves.
   Debug    : Boolean := False; -- Debug postscript output.

   G : Glyph-- The glyph that is being created.
begin
   User_Friendly("pbm2glyph", " ",
     (
     -"Read a PBM (Portable Bitmap) from standard input.",
     -"Create an EPS (Encapsulated PostScript) from it.",
     -"Perform some optimization, write to standard output."),
     (
     -" Staff height in pixels.",
     -" Pixels from left to center.",
     -" Pixels from top to center."),
     (
     -"-p, --pixels Don't remove pixel stairs, implies -s.",
     -"-s, --straight Don't create cubic bezier curves.",
     -"-d, --debug Add debugging markers in EPS output."),
     Index, Exit_Now);
   if Exit_Now then return; end if;

   while Index <= Argument_Count - 3 loop
      -- Debug("arg: " & Argument(Index));
      if Argument(Index) = "-q" or Argument(Index) = "--quiet" then null;
      elsif Argument(Index) = "-p" or Argument(Index) = "--pixels" then Pixels := True;
      elsif Argument(Index) = "-s" or Argument(Index) = "--straight" then Straight := True;
      elsif Argument(Index) = "-d" or Argument(Index) = "--debug" then Debug := True;
      else
         Invalid_Option(Index);
         return;
      end if;
      Index := Index + 1;
   end loop;

   if Index > Argument_Count - 2 then
      Too_Few_Arguments;
      return;
   end if;

   Staff_Height := To_Number(Argument(Index));
   Center_X := To_Number(Argument(Index + 1));
   Center_Y := To_Number(Argument(Index + 2));

   Message("reading PBM from standard input");
   G := Read_Current_Input;

   if Outline_Lists.Count(Get_Outlines(G)) = 0 then
      Message("no black pixels found");
      Set_Exit_Status(Failure);
      return;
   end if;

   if not Pixels then
      Message("removing pixel stairs");
      Stairs.Remove_Stairs(G, 2.5);
      if not Straight then
         Message("making cubic bezier curves");
         Curves.Make_Cubic(G, 0.1);
         Curves.Make_Cubic(G, 0.2);
         Curves.Make_Cubic(G, 0.3);
         Curves.Make_Cubic(G, 0.4);
         Curves.Make_Cubic(G, 0.5);
         -- Curves.Make_Cubic(G, 0.6);
         -- Curves.Make_Cubic(G, 1.0);
         -- Curves.Make_Cubic(G, 2.0);
      end if;
   end if;

   Message("translating to center");
   Translate(G, (-Center_X, Center_Y - Get_Height(G)));

   Message("scaling to interline space");
   Scale(G, 400.0 / Staff_Height);

   Message("writing EPS to standard output");
   Write(G, Debug);

   Message("finished successfully");
end PBM2Glyph;