Skip to content
Draft
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
23 changes: 23 additions & 0 deletions ESCPOS_NET.UnitTest/EmittersBased/EPSONTests/ImageCommandsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ESCPOS_NET.Emitters;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using Xunit;

namespace ESCPOS_NET.UnitTest.EmittersBased.EPSONTests
{
public class ImageCommandsTests
{
[Fact]
public void RasterisesText_Success()
{
var epson = new EPSON();
FontCollection collection = new FontCollection();
FontFamily family = collection.Install("c://windows/fonts/arial.ttf");

byte[] rawImage = epson.PrintRasterizedText("Hello world!", family, 24);
var renderedImage = Image.Load(rawImage);

// compare to a fixture?
}
}
}
1 change: 1 addition & 0 deletions ESCPOS_NET/ESCPOS_NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.1" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
<PackageReference Include="SuperSimpleTcp" Version="2.4.0" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.4" />
Expand Down
22 changes: 22 additions & 0 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/ImageCommands.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.IO;
using ESCPOS_NET.Emitters.BaseCommandValues;
using ESCPOS_NET.Utilities;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;

namespace ESCPOS_NET.Emitters
{
Expand Down Expand Up @@ -119,5 +123,23 @@ public virtual byte[] PrintImage(byte[] image, bool isHiDPI, bool isLegacy = fal
return ByteSplicer.Combine(SetImageDensity(isHiDPI), BufferImage(image, maxWidth, isLegacy, color), WriteImageFromBuffer());
}
}

public virtual byte[] PrintRasterizedText(string text, FontFamily fontFamily, float size, FontStyle fontStyle = FontStyle.Regular)
{
var font = fontFamily.CreateFont(size, fontStyle);

TextOptions textOptions = new TextOptions(font);
var rect = TextMeasurer.Measure(text, textOptions);

Image image = new Image<Rgba32>((int)rect.Width, (int)rect.Height);
image.Mutate(x => x.DrawText(text, font, SixLabors.ImageSharp.Color.Black, new PointF(0, 0)));

var memoryStream = new MemoryStream();
image.SaveAsPng(memoryStream);
var bytes = memoryStream.ToArray();
memoryStream.Dispose();

return PrintImage(bytes, true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pass in the isHDPI.

}
}
}
4 changes: 4 additions & 0 deletions ESCPOS_NET/Emitters/ICommandEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using SixLabors.Fonts;

namespace ESCPOS_NET.Emitters
{
public interface ICommandEmitter
Expand Down Expand Up @@ -67,6 +69,8 @@ public interface ICommandEmitter

byte[] PrintImage(byte[] image, bool isHiDPI, bool isLegacy = false, int maxWidth = -1, int color = 1);

byte[] PrintRasterizedText(string text, FontFamily fontFamily, float size, FontStyle fontStyle = FontStyle.Regular);

/* Status Commands */
byte[] EnableAutomaticStatusBack();

Expand Down