Skip to content

Commit bda8ec5

Browse files
authored
Merge pull request #256 from danzk/develop
Adding support for drawing to an external SKSurface.
2 parents 4a039ce + 556198b commit bda8ec5

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeDrawerBase.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ protected void DrawBarcode(
3737

3838
if (matrix != SKMatrix.Empty)
3939
{
40-
this._skCanvas.SetMatrix(matrix);
40+
var currentMatrix = _skCanvas.TotalMatrix;
41+
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
42+
this._skCanvas.SetMatrix(concatMatrix);
4143
}
4244

4345
this._skCanvas.DrawBitmap(SKBitmap.Decode(barcodeImageData), x, y);
@@ -65,7 +67,9 @@ protected void DrawInterpretationLine(
6567

6668
if (matrix != SKMatrix.Empty)
6769
{
68-
this._skCanvas.SetMatrix(matrix);
70+
var currentMatrix = _skCanvas.TotalMatrix;
71+
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
72+
this._skCanvas.SetMatrix(concatMatrix);
6973
}
7074

7175
var textBounds = new SKRect();

src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeEAN13ElementDrawer.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ private void DrawEAN13InterpretationLine(
9090

9191
if (matrix != SKMatrix.Empty)
9292
{
93-
this._skCanvas.SetMatrix(matrix);
93+
var currentMatrix = _skCanvas.TotalMatrix;
94+
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
95+
this._skCanvas.SetMatrix(concatMatrix);
9496
}
9597

9698
var textBounds = new SKRect();

src/BinaryKits.Zpl.Viewer/ElementDrawers/FieldBlockElementDrawer.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
128128

129129
if (matrix != SKMatrix.Empty)
130130
{
131-
this._skCanvas.SetMatrix(matrix);
131+
var currentMatrix = _skCanvas.TotalMatrix;
132+
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
133+
this._skCanvas.SetMatrix(concatMatrix);
132134
}
133135

134136
foreach (var textLine in textLines)

src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
108108

109109
if (matrix != SKMatrix.Empty)
110110
{
111-
this._skCanvas.SetMatrix(matrix);
111+
var currentMatrix = _skCanvas.TotalMatrix;
112+
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
113+
this._skCanvas.SetMatrix(concatMatrix);
112114
}
113115

114116
if (textField.FieldTypeset == null)

src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs

+76
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,82 @@ public List<byte[]> DrawMulti(
238238
return result;
239239
}
240240

241+
/// <summary>
242+
/// Draw the label on the provided surface
243+
/// </summary>
244+
/// <param name="surface">Skia Surface</param>
245+
/// <param name="elements">Zpl elements</param>
246+
/// <param name="labelWidth">Label width in millimeter</param>
247+
/// <param name="labelHeight">Label height in millimeter</param>
248+
/// <param name="printDensityDpmm">Dots per millimeter</param>
249+
/// <returns></returns>
250+
public void DrawSurface(SKSurface surface,
251+
ZplElementBase[] elements,
252+
double labelWidth = 101.6,
253+
double labelHeight = 152.4,
254+
int printDensityDpmm = 8)
255+
{
256+
var result = new List<byte[]>();
257+
var imageHistory = new List<SKImage>();
258+
var labelImageWidth = Convert.ToInt32(labelWidth * printDensityDpmm);
259+
var labelImageHeight = Convert.ToInt32(labelHeight * printDensityDpmm);
260+
261+
var skCanvas = surface.Canvas;
262+
//This has an issue with AvaloniaUI making the window transparent.
263+
skCanvas.Clear(SKColors.Transparent);
264+
265+
foreach (var element in elements)
266+
{
267+
var drawer = this._elementDrawers.SingleOrDefault(o => o.CanDraw(element));
268+
if (drawer == null)
269+
{
270+
continue;
271+
}
272+
273+
try
274+
{
275+
if (drawer.IsReverseDraw(element))
276+
{
277+
//basically only ZplGraphicBox/Circle depending on requirements
278+
using var skBitmapInvert = new SKBitmap(labelImageWidth, labelImageHeight);
279+
using var skCanvasInvert = new SKCanvas(skBitmapInvert);
280+
skCanvasInvert.Clear(SKColors.Transparent);
281+
282+
drawer.Prepare(this._printerStorage, skCanvasInvert);
283+
drawer.Draw(element, _drawerOptions);
284+
285+
//use color inversion on an reverse draw white element
286+
if (drawer.IsWhiteDraw(element))
287+
{
288+
this.InvertDrawWhite(skCanvas, skBitmapInvert);
289+
}
290+
else
291+
{
292+
this.InvertDraw(skCanvas, skBitmapInvert);
293+
}
294+
295+
continue;
296+
}
297+
298+
drawer.Prepare(this._printerStorage, skCanvas);
299+
drawer.Draw(element, _drawerOptions);
300+
301+
continue;
302+
}
303+
catch (Exception ex)
304+
{
305+
if (element is ZplBarcode barcodeElement)
306+
throw new Exception($"Error on zpl element \"{barcodeElement.Content}\": {ex.Message}", ex);
307+
else if (element is ZplDataMatrix dataMatrixElement)
308+
throw new Exception($"Error on zpl element \"{dataMatrixElement.Content}\": {ex.Message}", ex);
309+
else
310+
{
311+
throw;
312+
}
313+
}
314+
}
315+
}
316+
241317
/**
242318
* PDF transparency and SKBlendMode are not very good friends, SKBlendMode.Xor behaves as SKBlendMode.SrcOver.
243319
*

0 commit comments

Comments
 (0)