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

Font Issue #538

Open
harsh8088 opened this issue Sep 16, 2024 · 0 comments
Open

Font Issue #538

harsh8088 opened this issue Sep 16, 2024 · 0 comments

Comments

@harsh8088
Copy link

harsh8088 commented Sep 16, 2024

Printing different font sizes:
@DantSu ,

when multiLineBranch is
-short text(single line, first print), printing as expected.
-long text(2 line text, second print), printing different font.

Code:

 fun printContent() {
    try {
        branch = "ABCDEFGHIJKL MNOPQRSTUV GHjgdjhgajgd hdkhsdwd dkh d"
        val printer = EscPosPrinter(UsbConnection(usbManager, usbDevice), 203, 80f, 32)

        println("PrinterDetails")
        println("printerCharSizeWidthPx:${printer.printerCharSizeWidthPx}")
        println("printerWidthMM:${printer.printerWidthMM}")
        println("printerWidthPx:${printer.printerWidthPx}")
        println("printerDpi:${printer.printerDpi}")
        println("printerNbrCharactersPerLine:${printer.printerNbrCharactersPerLine}")
        timeString = centerAlignText(timeString, 49)

        val multiLineBranch = centerMultiLineBranch(branch, 48)

        val tokenText = centerAlignText("TOKEN NUMBER", 49)
        token = centerAlignText(token, 25)
        println("printer timeString:${timeString}:${timeString.length}")
        println("printer branch:${branch}:${branch.length}")
        println("printer tokenText:${tokenText}:${tokenText.length}")
        println("printer token:${token}:${token.length}")

        printer.printFormattedTextAndCut(
            "[C]${timeString}\n" +
                    "[L]\n" +
                    "$multiLineBranch\n" +
                    "[C]------------------------------------------------\n" +
                    "[L]\n" +
                    "[L]\n" +
                    "[C]<b>${tokenText}</b>\n" +
                    "[L]\n" +
                    "[C]<font size='big'><b>${token}</b></font>\n" +
                    "[L]\n" +
                    "[L]\n" +
//                        "[L][C] PLEASE PROCEED TO BILLING[R]\n" +
                    "[L]\n" +
                    "[L]\n" +
                    "[C]------------------------------------------------\n" +
                    "", 5.0f
        )
        dismiss()
    } catch (e: Exception) {
        e.printStackTrace()
        Toast.makeText(context, "Printer Error!!!", Toast.LENGTH_LONG).show()
    }
}

private fun centerAlignText(text: String, stringLength: Int): String {
    try {
        val paddingLength = (stringLength - text.length) / 2
        val padding = " ".repeat(paddingLength)
        return padding + text + padding
    } catch (e: Exception) {
        return text
    }
}

private fun centerMultiLineBranch(text: String, stringLength: Int): String {
    val paddingLength = max(0, (stringLength - text.length) / 2)
    val padding = " ".repeat(paddingLength)

    // Check if the text is too long
    if (text.length > stringLength) {
        // Find the last space before the middle point
        val middleIndex = stringLength / 2
        var lastSpaceIndex = middleIndex - 1
        while (lastSpaceIndex > 0 && text[lastSpaceIndex] != ' ') {
            lastSpaceIndex--
        }

        // Split the text at the last space before the middle point
        val firstPart = text.substring(0, lastSpaceIndex + 1)
        val secondPart = text.substring(lastSpaceIndex + 1)

        // Center align each part
        val centeredFirstPart = padding + firstPart + padding
        val centeredSecondPart = padding + secondPart + padding
        println("centeredFirstPart,${centeredFirstPart.length}: " + centeredFirstPart)
        println("centeredSecondPart,${centeredSecondPart.length}: " + centeredSecondPart)
        return centerAlignText(centeredFirstPart, 48) +"\n"+
                centerAlignText(centeredSecondPart, 48)
    } else {
        return "[C]<font size='tall'>${padding + text + padding}</font>"
    }
}

Console:

printerCharSizeWidthPx:19 printerWidthMM:80.0 printerWidthPx:646 printerDpi:203 printerNbrCharactersPerLine:32

Suggestions are welcome!

Media (1)

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

No branches or pull requests

1 participant