bar_code_drawing_20240701.0.0_9f093ffd/src/bar_code_drawing-how-svg.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- A library for drawing bar codes
--
-- Copyright (C) by PragmAda Software Engineering
--
-- Released under the terms of the 3-Clause BSD License. See https://opensource.org/licenses/BSD-3-Clause

with Ada.Characters.Latin_1;
with Ada.Strings.Unbounded;
with PragmARC.Images;

package body Bar_Code_Drawing.How.SVG is
   function Image (Info : in Drawing_Info; Width : in Float := 1.0; Height : in Float := 1.0) return String is
      use Ada.Strings.Unbounded;

      LF : constant Character := Ada.Characters.Latin_1.LF;

      function Image is new PragmARC.Images.Float_Image (Number => Float);

      Unit : constant String := "mm";

      Result : Unbounded_String := To_Unbounded_String ("<?xml version=""1.0"" encoding=""UTF-8""?>" & LF &
                                                        "<!DOCTYPE svg" & LF &
                                                        "  PUBLIC '-//W3C//DTD SVG 1.1//EN'" & LF &
                                                        "  'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>" & LF &
                                                        "<!-- Created by " & Name & " -->" &  LF &
                                                        "<!-- " & URL & " -->" &  LF &
                                                        "<svg height=" & '"' &
                                                        Image ( (if Info.Dim = 1 then Height
                                                                 else Float (Info.Height) * Width), 1, 2, 0) &
                                                        Unit &
                                                        '"' & " width=" & '"' & Image (Float (Info.Width) * Width, 1, 2, 0) & Unit &
                                                        '"' & " version=""1.1"" xmlns=""http://www.w3.org/2000/svg"">" & LF &
                                                        "    <rect height=""100%"" width=""100%"" style=""fill:#FFFFFF""/>" & LF);
                                                        --  White rectangle as background
   begin  -- Image
      if Info.Dim = 1 then -- 1D barcode
         All_X : for X in Info.Bitmap'Range (1) loop
            if Info.Bitmap (X, 0) then
               Append (Source => Result, New_Item => "    <rect style=" & '"' & "fill:#000000;" & '"' &
                                                     " x=" & '"' & Image (Float (X) * Width, 1, 2, 0) & Unit & '"' &
                                                     " y=" & '"' & "0.0" & Unit & '"' &
                                                     " width=" & '"' & Image (Width, 1, 2, 0) & Unit & '"' &
                                                     " height=" & '"' & Image (Height, 1, 2, 0) & Unit & '"' & "/>" & LF);
            end if;
         end loop All_X;
      else
        X_2 : for X in Info.Bitmap'Range (1) loop
            Y_2 : for Y in Info.Bitmap'Range (2) loop
               if Info.Bitmap (X, Y) then
                  Append (Source => Result, New_Item => "    <rect style=" & '"' & "fill:#000000;" & '"' &
                                                        " x=" & '"' & Image (Float (X) * Width, 1, 2, 0) & Unit & '"' &
                                                        " y=" & '"' &
                                                        Image (Float (Info.Width) * Width - Float (Y + 1) * Width, 1, 2, 0) & Unit &
                                                        '"' &
                                                        " width=" & '"' & Image (Width, 1, 2, 0) & Unit & '"' &
                                                        " height=" & '"' & Image (Width, 1, 2, 0) & Unit & '"' & "/>" & LF);
               end if;
            end loop Y_2;
         end loop X_2;
      end if;

      Append (Source => Result, New_Item => "</svg>" & LF);

      return To_String (Result);
   end Image;
end Bar_Code_Drawing.How.SVG;