1
1
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0
2
+ using QRCoder . Extensions ;
2
3
using System ;
3
4
using System . Collections ;
5
+ using System . Collections . Generic ;
4
6
using System . Drawing ;
5
7
using System . Text ;
8
+ using System . Text . RegularExpressions ;
6
9
using static QRCoder . QRCodeGenerator ;
7
10
using static QRCoder . SvgQRCode ;
8
11
@@ -69,7 +72,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
69
72
for (int xi = 0; xi < drawableModulesCount; xi += 1)
70
73
{
71
74
matrix[yi, xi] = 0;
72
- if (bitArray[xi+offset] && ! IsBlockedByLogo((xi+offset)*pixelsPerModule, (yi+offset) * pixelsPerModule, logoAttr, pixelsPerModule))
75
+ if (bitArray[xi+offset] && (logo == null || !logo.FillLogoBackground() || ! IsBlockedByLogo((xi+offset)*pixelsPerModule, (yi+offset) * pixelsPerModule, logoAttr, pixelsPerModule) ))
73
76
{
74
77
if(x0 == -1)
75
78
{
@@ -121,7 +124,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
121
124
122
125
// Output SVG rectangles
123
126
double x = xi * pixelsPerModule;
124
- if (!IsBlockedByLogo(x, y, logoAttr, pixelsPerModule))
127
+ if (logo == null || !logo.FillLogoBackground() || !IsBlockedByLogo(x, y, logoAttr, pixelsPerModule))
125
128
svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(xL * pixelsPerModule)}"" height=""{CleanSvgVal(yL * pixelsPerModule)}"" fill=""{darkColorHex}"" />");
126
129
127
130
}
@@ -131,8 +134,23 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
131
134
//Render logo, if set
132
135
if (logo != null)
133
136
{
134
- svgFile.AppendLine($@"<svg width=""100%"" height=""100%"" version=""1.1"" xmlns = ""http://www.w3.org/2000/svg"">");
135
- svgFile.AppendLine($@"<image x=""{CleanSvgVal(logoAttr.Value.X)}"" y=""{CleanSvgVal(logoAttr.Value.Y)}"" width=""{CleanSvgVal(logoAttr.Value.Width)}"" height=""{CleanSvgVal(logoAttr.Value.Height)}"" xlink:href=""{logo.GetDataUri()}"" />");
137
+
138
+ if (logo.GetMediaType() == SvgLogo.MediaType.PNG)
139
+ {
140
+ svgFile.AppendLine($@"<svg width=""100%"" height=""100%"" version=""1.1"" xmlns = ""http://www.w3.org/2000/svg"">");
141
+ svgFile.AppendLine($@"<image x=""{CleanSvgVal(logoAttr.Value.X)}"" y=""{CleanSvgVal(logoAttr.Value.Y)}"" width=""{CleanSvgVal(logoAttr.Value.Width)}"" height=""{CleanSvgVal(logoAttr.Value.Height)}"" xlink:href=""{logo.GetDataUri()}"" />");
142
+ }
143
+ else if (logo.GetMediaType() == SvgLogo.MediaType.SVG)
144
+ {
145
+ svgFile.AppendLine($@"<svg x=""{CleanSvgVal(logoAttr.Value.X)}"" y=""{CleanSvgVal(logoAttr.Value.Y)}"" width=""{CleanSvgVal(logoAttr.Value.Width)}"" height=""{CleanSvgVal(logoAttr.Value.Height)}"" version=""1.1"" xmlns = ""http://www.w3.org/2000/svg"">");
146
+ var rawLogo = (string)logo.GetRawLogo();
147
+ //Remove some attributes from logo, because it would lead to wrong sizing inside our svg wrapper
148
+ new List<string>() { "width", "height", "x", "y" }.ForEach(attr =>
149
+ {
150
+ rawLogo = Regex.Replace(rawLogo, $@"(?!=<svg[^>]*?) +{attr}=(""[^""]+""|'[^']+')(?=[^>]*>)", "");
151
+ });
152
+ svgFile.Append(rawLogo);
153
+ }
136
154
svgFile.AppendLine(@"</svg>");
137
155
}
138
156
@@ -185,9 +203,11 @@ public enum SizingMode
185
203
public class SvgLogo
186
204
{
187
205
private string _logoData;
188
- private string _mediaType;
206
+ private MediaType _mediaType;
189
207
private int _iconSizePercent;
190
208
private bool _fillLogoBackground;
209
+ private object _logoRaw;
210
+
191
211
192
212
/// <summary>
193
213
/// Create a logo object to be used in SvgQRCode renderer
@@ -205,8 +225,9 @@ public SvgLogo(Bitmap iconRasterized, int iconSizePercent = 15, bool fillLogoBac
205
225
_logoData = Convert.ToBase64String(ms.GetBuffer(), Base64FormattingOptions.None);
206
226
}
207
227
}
208
- _mediaType = "image/png" ;
228
+ _mediaType = MediaType.PNG ;
209
229
_fillLogoBackground = fillLogoBackground;
230
+ _logoRaw = iconRasterized;
210
231
}
211
232
212
233
/// <summary>
@@ -218,13 +239,24 @@ public SvgLogo(string iconVectorized, int iconSizePercent = 15, bool fillLogoBac
218
239
{
219
240
_iconSizePercent = iconSizePercent;
220
241
_logoData = Convert.ToBase64String(Encoding.UTF8.GetBytes(iconVectorized), Base64FormattingOptions.None);
221
- _mediaType = "image/svg+xml" ;
242
+ _mediaType = MediaType.SVG ;
222
243
_fillLogoBackground = fillLogoBackground;
244
+ _logoRaw = iconVectorized;
245
+ }
246
+
247
+ public object GetRawLogo()
248
+ {
249
+ return _logoRaw;
250
+ }
251
+
252
+ public MediaType GetMediaType()
253
+ {
254
+ return _mediaType;
223
255
}
224
256
225
257
public string GetDataUri()
226
258
{
227
- return $"data:{_mediaType};base64,{_logoData}";
259
+ return $"data:{_mediaType.GetStringValue() };base64,{_logoData}";
228
260
}
229
261
230
262
public int GetIconSizePercent()
@@ -235,6 +267,14 @@ public bool FillLogoBackground()
235
267
{
236
268
return _fillLogoBackground;
237
269
}
270
+
271
+ public enum MediaType : int
272
+ {
273
+ [StringValue("image/png")]
274
+ PNG = 0,
275
+ [StringValue("image/svg+xml")]
276
+ SVG = 1
277
+ }
238
278
}
239
279
}
240
280
0 commit comments