Skip to content

Commit

Permalink
Merge pull request #256 from danzk/develop
Browse files Browse the repository at this point in the history
Adding support for drawing to an external SKSurface.
  • Loading branch information
primo-ppcg authored Jun 19, 2024
2 parents 4a039ce + 556198b commit bda8ec5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeDrawerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ protected void DrawBarcode(

if (matrix != SKMatrix.Empty)
{
this._skCanvas.SetMatrix(matrix);
var currentMatrix = _skCanvas.TotalMatrix;
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
this._skCanvas.SetMatrix(concatMatrix);
}

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

if (matrix != SKMatrix.Empty)
{
this._skCanvas.SetMatrix(matrix);
var currentMatrix = _skCanvas.TotalMatrix;
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
this._skCanvas.SetMatrix(concatMatrix);
}

var textBounds = new SKRect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ private void DrawEAN13InterpretationLine(

if (matrix != SKMatrix.Empty)
{
this._skCanvas.SetMatrix(matrix);
var currentMatrix = _skCanvas.TotalMatrix;
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
this._skCanvas.SetMatrix(concatMatrix);
}

var textBounds = new SKRect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public override void Draw(ZplElementBase element, DrawerOptions options)

if (matrix != SKMatrix.Empty)
{
this._skCanvas.SetMatrix(matrix);
var currentMatrix = _skCanvas.TotalMatrix;
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
this._skCanvas.SetMatrix(concatMatrix);
}

foreach (var textLine in textLines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public override void Draw(ZplElementBase element, DrawerOptions options)

if (matrix != SKMatrix.Empty)
{
this._skCanvas.SetMatrix(matrix);
var currentMatrix = _skCanvas.TotalMatrix;
var concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
this._skCanvas.SetMatrix(concatMatrix);
}

if (textField.FieldTypeset == null)
Expand Down
76 changes: 76 additions & 0 deletions src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,82 @@ public List<byte[]> DrawMulti(
return result;
}

/// <summary>
/// Draw the label on the provided surface
/// </summary>
/// <param name="surface">Skia Surface</param>
/// <param name="elements">Zpl elements</param>
/// <param name="labelWidth">Label width in millimeter</param>
/// <param name="labelHeight">Label height in millimeter</param>
/// <param name="printDensityDpmm">Dots per millimeter</param>
/// <returns></returns>
public void DrawSurface(SKSurface surface,
ZplElementBase[] elements,
double labelWidth = 101.6,
double labelHeight = 152.4,
int printDensityDpmm = 8)
{
var result = new List<byte[]>();
var imageHistory = new List<SKImage>();
var labelImageWidth = Convert.ToInt32(labelWidth * printDensityDpmm);
var labelImageHeight = Convert.ToInt32(labelHeight * printDensityDpmm);

var skCanvas = surface.Canvas;
//This has an issue with AvaloniaUI making the window transparent.
skCanvas.Clear(SKColors.Transparent);

foreach (var element in elements)
{
var drawer = this._elementDrawers.SingleOrDefault(o => o.CanDraw(element));
if (drawer == null)
{
continue;
}

try
{
if (drawer.IsReverseDraw(element))
{
//basically only ZplGraphicBox/Circle depending on requirements
using var skBitmapInvert = new SKBitmap(labelImageWidth, labelImageHeight);
using var skCanvasInvert = new SKCanvas(skBitmapInvert);
skCanvasInvert.Clear(SKColors.Transparent);

drawer.Prepare(this._printerStorage, skCanvasInvert);
drawer.Draw(element, _drawerOptions);

//use color inversion on an reverse draw white element
if (drawer.IsWhiteDraw(element))
{
this.InvertDrawWhite(skCanvas, skBitmapInvert);
}
else
{
this.InvertDraw(skCanvas, skBitmapInvert);
}

continue;
}

drawer.Prepare(this._printerStorage, skCanvas);
drawer.Draw(element, _drawerOptions);

continue;
}
catch (Exception ex)
{
if (element is ZplBarcode barcodeElement)
throw new Exception($"Error on zpl element \"{barcodeElement.Content}\": {ex.Message}", ex);
else if (element is ZplDataMatrix dataMatrixElement)
throw new Exception($"Error on zpl element \"{dataMatrixElement.Content}\": {ex.Message}", ex);
else
{
throw;
}
}
}
}

/**
* PDF transparency and SKBlendMode are not very good friends, SKBlendMode.Xor behaves as SKBlendMode.SrcOver.
*
Expand Down

0 comments on commit bda8ec5

Please sign in to comment.