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

Add a command for printing rasterized text #165

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nickcharlton
Copy link
Contributor

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
Copy link
Owner

lukevp commented Jul 8, 2022

@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?

This allows us to print a line with a custom font, sizes and weights
which resolves problems with printing text of unsupported sizes (lukevp#132)
and unsupported character sets/code pages (lukevp#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 lukevp#132, lukevp#116
@nickcharlton
Copy link
Contributor Author

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
Copy link
Owner

lukevp commented Aug 18, 2022

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
Copy link
Owner

lukevp commented Aug 18, 2022

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
Copy link
Owner

lukevp commented Aug 18, 2022

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Font size bigger than PrintStyle.DoubleHeight | PrintStyle.DoubleWidth
4 participants