1 | unit lbc_svg;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils,
|
---|
9 | zint;
|
---|
10 |
|
---|
11 | procedure svg_plot(AStream: TStream; ASymbol: PointerTo_zint_symbol);
|
---|
12 |
|
---|
13 |
|
---|
14 | implementation
|
---|
15 |
|
---|
16 | { Writes the symbol to a stream.
|
---|
17 | To avoid duplication of the bar calculation, the coordinates are taken from
|
---|
18 | the rendered data, i.e. the coordinates are in pixels. We want to draw in mm
|
---|
19 | --> we must divide by GL_CONST. }
|
---|
20 | procedure svg_plot(AStream: TStream; ASymbol: PointerTo_zint_symbol);
|
---|
21 | const
|
---|
22 | factor = 1.0/GL_CONST;
|
---|
23 | var
|
---|
24 | line: PointerTo_zint_render_line;
|
---|
25 | str: PointerTo_zint_render_string;
|
---|
26 | fs: TFormatSettings;
|
---|
27 | L: TStrings;
|
---|
28 | w, h: Single;
|
---|
29 | fgcolor, bgcolor: String;
|
---|
30 | begin
|
---|
31 | if (ASymbol = nil) then
|
---|
32 | raise Exception.Create('[svg_plot] Symbol is nil');
|
---|
33 | if (ASymbol^.rendered = nil) then
|
---|
34 | raise Exception.Create('[svg_plot] Barcode has not yet been rendered.');
|
---|
35 |
|
---|
36 | // rendered size in mm
|
---|
37 | w := ASymbol^.rendered^.exact_width*factor;
|
---|
38 | h := ASymbol^.rendered^.height*factor;
|
---|
39 |
|
---|
40 | // Make sure that floats are written with point as decimal separator
|
---|
41 | fs := FormatSettings;
|
---|
42 | fs.DecimalSeparator := '.';
|
---|
43 |
|
---|
44 | bgcolor := StrPas(ASymbol^.bgColour);
|
---|
45 | fgcolor := StrPas(ASymbol^.fgColour);
|
---|
46 |
|
---|
47 | L := TStringList.Create;
|
---|
48 | try
|
---|
49 | L.Add(
|
---|
50 | '<?xml version="1.0" standalone="no"?>'
|
---|
51 | );
|
---|
52 | L.Add(
|
---|
53 | '<!DOCTYPE svg PUBLIC "-//W3C/DTD SVG 1.1/EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
|
---|
54 | );
|
---|
55 | L.Add(Format(
|
---|
56 | '<svg width="%.3fmm" height="%.3fmm" version="1.1" xmlns="http://www.w3.org/2000/svg">', [w, h], fs));
|
---|
57 |
|
---|
58 | if system.strlen(PChar(@ASymbol^.text[0])) <> 0 then
|
---|
59 | L.Add(' <desc>%s</desc>', [PChar(@ASymbol^.text[0])])
|
---|
60 | else
|
---|
61 | L.Add(' <desc>Symbol generated by Zint and LazBarcodes</desc>');
|
---|
62 | L.Add(Format(
|
---|
63 | ' <rect width="100%%" height="100%%" fill="#%s"/>', [bgcolor]));
|
---|
64 | L.Add(
|
---|
65 | ' <g id="barcode">');
|
---|
66 |
|
---|
67 | line := ASymbol^.rendered^.lines;
|
---|
68 | while Assigned(line) do
|
---|
69 | begin
|
---|
70 | L.Add(Format(
|
---|
71 | ' <rect x="%.3fmm" y="%.3fmm" width="%.3fmm" height="%.3fmm" fill="#%s" />', [
|
---|
72 | line^.x*factor, line^.y*factor,
|
---|
73 | line^.width*factor, line^.length*factor,
|
---|
74 | fgcolor
|
---|
75 | ], fs));
|
---|
76 | line := line^.next;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | str := ASymbol^.rendered^.strings;
|
---|
80 | while Assigned(str) do
|
---|
81 | begin
|
---|
82 | L.Add(Format(
|
---|
83 | ' <text x="%.3fmm" y="%.3fmm" text-anchor="middle" font-family="Helvetica" font-size="%.1f" fill="#%s">%s</text>', [
|
---|
84 | str^.x*factor, (str^.y + ASymbol^.font_height)*factor, 1.0*ASymbol^.font_height,
|
---|
85 | fgcolor,
|
---|
86 | PChar(@ASymbol^.text[0])
|
---|
87 | ], fs));
|
---|
88 | str := str^.next;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | // to do: Add support of rings and hexagons
|
---|
92 |
|
---|
93 | L.Add(' </g>');
|
---|
94 | L.Add('</svg>');
|
---|
95 |
|
---|
96 | L.SaveToStream(AStream);
|
---|
97 | finally
|
---|
98 | L.Free;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | end.
|
---|
103 |
|
---|