lukevp / ESC-POS-.NET

Efficient, Easy to Use Thermal Printing & POS (Windows/Linux/OSX, WiFi/BT/USB/Ethernet)
MIT License
522 stars 171 forks source link

Add a command for printing rasterized text #165

Open nickcharlton opened 2 years ago

nickcharlton commented 2 years ago

This allows us to print a line with a custom font, sizes and weights which resolves problems with printing text of unsupported sizes (#132) and unsupported character sets/code pages (#116)

We achieve this by rendering an image with text, sized to fit the text itself and returning byte[] to be compatible with other commands.

Fixes #132, #116


I'm opening this as a draft, as I'm a bit stuck on testing and some feedback on the API itself would be great. I'm using this in production currently and it's working quite well.

On the project which this is extracted from, I'm comparing the rendered text to a fixture using a library I cobbled together from some example code. It uses System.Drawing.Common, rather than ImageSharp so one option would be for me to refactor that then use it here, but I'm open to any thoughts on that.

Another problem with testing is needing a ttf font. A good choice here might be to bundle an open source font for this purpose, but I'm open to ideas.

Finally, it's in the command emitters for images, which felt the most appropriate for what it's doing but it could easily go somewhere else.

lukevp commented 2 years ago

@nickcharlton I merged another PR which bumped imagesharp, could you resolve the merge conflicts?

This is really cool, are you able to set widths and auto-wrap text to ensure it doesn't overflow the printer? or do you just have to rasterize each line individually?

nickcharlton commented 2 years ago

I've just rebased to get up with those changes. I also added a commit to bump ImageSharp.Drawing to the latest beta as there were a few changes there too.

I think we probably could. Could you point me to where I could find out the widths available to us? I'm thinking the nicest solution here is to default to that but still allow overriding.

Also, do you have any thoughts on how we could go about testing this too?

lukevp commented 2 years ago

I think everything you mentioned makes sense! There's not really any automated testing in this project, just a console test, since most of the tests require validation on physical hardware to interpret the commands anyway. I think that would be fine here as well, I don't think you need to have automated tests, just a print command in the console test that will print some rasterized stuff.

For default widths, I don't know what the defaults are, I think it would depend on the printer DPI and how wide it can print. You could just have a conservative setting that works on most epson printers out of the box for a standard receipt size, and then another for the common smaller paper size. I have printers of both sizes that I could test this on.

Regarding system.drawing.common, do you think we could use that instead of imagesharp? I know I picked imagesharp at the onset of the project instead of system.drawing.common for some compatibility reasons, but I'm not certain why. Is it in net standard 2.0?

I've added immediate network printing support as it's an often-requested feature, and am releasing version 3.0, can you update this PR again? my apologies. I would love to bring this in to version 3.1.

lukevp commented 2 years ago

font wise, I think a public domain font would be best, if such a thing exists, since the MIT license may conflict with anything stricter than public domain (eg. I don't know how MIT and CC licenses play out together).

lukevp commented 2 years ago

Do you use this for a line of text at a time, and wrap it manually somehow? or don't wrap?