bar_code_drawing_20240701.0.0_9f093ffd/src/bar_code_drawing-how-pbm.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
-- 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;

package body Bar_Code_Drawing.How.PBM is
   function Image (Info : in Drawing_Info; Scale : in Positive := 1) return String is
      use Ada.Strings.Unbounded;

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

      Result : Unbounded_String := To_Unbounded_String ("P1" & LF &
                                                        "# Created by " & Name & LF &
                                                        "# " & URL & LF &
                                                        Integer'Image (Scale * Info.Width) &
                                                        Integer'Image ( (if Info.Dim = 1 then 1 else Scale) * Info.Height) & LF);
      Line   : Unbounded_String;
   begin -- Image
      if Info.Dim = 1 then -- 1D code
         Make_Line : for X in Info.Bitmap'Range (1) loop
            Apply_Scale : for J in 1 .. Scale loop
               Append (Source => Line, New_Item => Boolean'Pos (Info.Bitmap (X, 0) )'Image);
            end loop Apply_Scale;
         end loop Make_Line;

         Append (Source => Line, New_Item => LF);

         All_Lines : for Y in 1 .. Info.Height loop
            Append (Source => Result, New_Item => Line);
         end loop All_Lines;
      else -- 2D code
         All_Y : for Y in reverse Info.Bitmap'Range (2) loop
            Line := Null_Unbounded_String;

            All_X : for X in Info.Bitmap'Range (1) loop
               Scale_X : for I in 1 .. Scale loop
                  Append (Source => Line, New_Item => Boolean'Pos (Info.Bitmap (X, Y) )'Image);
               end loop Scale_X;
            end loop All_X;

            Append (Source => Line, New_Item => LF);

            Scale_Y : for I in 1 .. Scale loop
               Append (Source => Result, New_Item => Line);
            end loop Scale_Y;
         end loop All_Y;
      end if;

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