star-micronics / StarXpand-SDK-iOS

StarXpand SDK for iOS is a software development kit for supporting application development for Star Micronics devices.
https://star-m.jp/starxpandsdk-oml.html
Other
27 stars 5 forks source link

TSP100IIILAN / TSP143LAN not printing text, only images #24

Closed davidhole closed 6 months ago

davidhole commented 12 months ago

Description

Whenever actionPrintText is used in a PrinterBuilder, it seems to be ignored in the final printout.

This can be seen using your included example app, with the final printout looking like this: IMG_9100

Your device where the bug occurs

Your printer

Your development environment

ProductName: macOS ProductVersion: 14.1.1 BuildVersion: 23B81

To Reproduce

Steps to reproduce the behavior:

  1. Open the included example app from this repo
  2. Click Discover to find printer MAC
  3. Go back and select Print.
  4. Enter MAC in address (adding in colons)
  5. Press Print
  6. Only images print as part of test print, no text is printed.

Expected behavior

A full printout including all text, as it appears on mPOP and other printers

Screenshots

IMG_9100

mortenbekditlevsen commented 12 months ago

I don't work for Star, but aren't those printer precisely graphics-only printers?

davidhole commented 12 months ago

Hi @mortenbekditlevsen.

No the SDK has all sorts of methods for text, graphics, QR codes, barcodes, etc. All of these methods work well on other printers in the lineup (mPOP) but not on this printer.

It also works fine with these methods on other SDKs (StarPRNT)

mortenbekditlevsen commented 12 months ago

In our POS system we've always been generating bitmaps of receipts for the graphics-only printers like the TSP1000 series. Also when using StarPRNT.

Please refer to similar discussions:

https://github.com/star-micronics/react-native-star-io10/issues/21

From one of the linked sources above:

"In particular, the TSP100 range does not have built-in font data, so is limited to printing graphically, and does not support the actionPrintText() method. Instead, if you require support for the TSP100 range, you will need to build up your document as images and add them to the print job with the actionPrintImage() method."

This is from a discussion about another printer driver, but it's not about the driver, it's about the fact that the printer does not have font data and thus only supports a subset of the available driver commands across all platforms / sdks.

yellowMonkeyMediaDev commented 11 months ago

I'm facing the same issue, is there any solution for this, or should be text edited like image and than printed? :/

mortenbekditlevsen commented 11 months ago

That's what we're doing at my job: we basically have a document format which can be rendered to various destinations: HTML (for emailing), text (with hooks for various receipt printer integrations) and then UIViews which we then render to images and print on graphics-only printers.

YuzoKato commented 11 months ago

@mortenbekditlevsen Thank you for the explanation.

@davidhole @yellowMonkeyMediaDev TSP100III series is a graphic-only printer. You need to use actionPrintImage. The provided project you can build uses actionPrintText only. So, TSP100III cannot print as you expected.
You can refer to Text-to-Graphic sample code. This sample includes a converting function from text to graphic so that you can use text to create printing data.

TSP100IV model supports actionPrintText.

BryanJBryce commented 10 months ago

I made an extension to PrinterBuilder for convenience:


private let maxWidth = 570

extension StarIO10.StarXpandCommand.PrinterBuilder {
    @objc public func actionPrintImageFromText(_ text: String, font: UIFont? = nil) -> Self {
        actionPrintImage(createImageParameterFromText(text, font: font))
    }

    func createImageParameterFromText(_ text: String, font: UIFont?) -> StarXpandCommand.Printer.ImageParameter {
        let width = maxWidth
        let font = font ?? UIFont.monospacedSystemFont(ofSize: 24, weight: .medium)
        let bitmap = createBitmapFromText(text, font: font, width: width)
        return StarXpandCommand.Printer.ImageParameter(image: bitmap, width: width)
    }

    func createBitmapFromText(_ text: String, font: UIFont, width: Int) -> UIImage {
        let attributeDic = NSDictionary(dictionary: [NSAttributedString.Key.font: font])
        let widthCGF = CGFloat(width)
        let stringDrawingOptions: NSStringDrawingOptions = [NSStringDrawingOptions.usesLineFragmentOrigin, NSStringDrawingOptions.truncatesLastVisibleLine]

        let size: CGSize = (text.boundingRect(with: CGSize(width: widthCGF, height: 10_000), options: stringDrawingOptions, attributes: attributeDic as? [NSAttributedString.Key: Any], context: nil)).size

        if UIScreen.main.responds(to: #selector(NSDecimalNumberBehaviors.scale)) {
            if UIScreen.main.scale == 2.0 {
                UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
            } else {
                UIGraphicsBeginImageContext(size)
            }
        } else {
            UIGraphicsBeginImageContext(size)
        }

        let context: CGContext = UIGraphicsGetCurrentContext()!

        UIColor.white.set()

        let rect = CGRect(x: 0, y: 0, width: size.width + 1, height: size.height + 1)

        context.fill(rect)

        let attributes = NSDictionary(dictionary: [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: font])

        text.draw(in: rect, withAttributes: attributes as? [NSAttributedString.Key: Any])

        let imageToPrint: UIImage = UIGraphicsGetImageFromCurrentImageContext()!

        UIGraphicsEndImageContext()

        return imageToPrint
    }
}
Tatsuki-Yamamoto2731 commented 6 months ago

It seems that this issue was resolved, so I am closing this Issue.

MarsYoung commented 2 months ago

I have the same problem, but why can the Star Quick Setup Utility print the text clean and tidy. Converting to image cause ugly text words.

Tatsuki-Yamamoto2731 commented 2 months ago

@MarsYoung This sample shows how to generate graphic data from text data. Does this sample show you a solution? If not, I would like to know the details of the problem. Please open a new Issue and describe the situation as accurately as possible.