Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for drawing to an external SKSurface. #256

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading