Skip to content

Commit

Permalink
Commonize text encoding
Browse files Browse the repository at this point in the history
The library now performs its own text encoding, removing the dependency on platform utilities.
  • Loading branch information
okarmazin authored Sep 18, 2024
2 parents 6a67e81 + 5821008 commit 33cbf3a
Show file tree
Hide file tree
Showing 37 changed files with 4,208 additions and 285 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ suspend fun awaitPrint(): MyError? {
val libraryError: PrintError? = connection.print(configuration) { // (2)
// MULTIPLE TEXT ALIGNMENTS PER LINE
line("Famous bridges:")
charset(Charset.CP865) // Can encode Ø, but not ů
charset(IBM865) // Can encode Ø, but not ů
segmentedLine(
LineSegment("Øresundsbroen", TextAlignment.LEFT),
LineSegment("7845m", TextAlignment.RIGHT),
)
charset(Charset.CP852) // Can encode ů, but not Ø
charset(IBM852) // Can encode ů, but not Ø
segmentedLine(
LineSegment("Karlův most", TextAlignment.LEFT),
LineSegment("515m", TextAlignment.RIGHT),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package cz.multiplatform.escpos4k.core

import cz.multiplatform.escpos4k.core.encoding.charset.Charset
import cz.multiplatform.escpos4k.core.encoding.encode

internal sealed class Command {
abstract fun bytes(): ByteArray

class Text(private val text: String, charset: Charset) : Command() {
private val encodedBytes: ByteArray by lazy { encode(text, charset) }
private val encodedBytes: ByteArray by lazy { text.encode(charset) }

override fun bytes(): ByteArray = encodedBytes.copyOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import arrow.core.Nel
import arrow.core.nonEmptyListOf
import arrow.core.toNonEmptyListOrNull
import cz.multiplatform.escpos4k.core.LineDistributionStrategy.Companion.SpaceEvenly
import cz.multiplatform.escpos4k.core.encoding.charset.Charset

/**
* The central class for building the content to send to the printer. The class provides a variety
Expand All @@ -43,9 +44,9 @@ import cz.multiplatform.escpos4k.core.LineDistributionStrategy.Companion.SpaceEv
* val builder = CommandBuilder(defaultConfig) {
* // Charsets
* line("Famous bridges:")
* charset(Charset.CP865) // Can encode Ø, but not ů
* charset(IBM865) // Can encode Ø, but not ů
* line("Øresundsbroen: 7845m")
* charset(Charset.CP852) // Can encode ů, but not Ø
* charset(IBM852) // Can encode ů, but not Ø
* line("Karlův most: 515m")
*
* // Text style - temporary style builder
Expand Down Expand Up @@ -162,10 +163,10 @@ public class CommandBuilder(
* printer.print {
* line("Famous bridges:")
*
* charset(Charset.CP865) // Can encode Ø, but not ů
* charset(IBM865) // Can encode Ø, but not ů
* line("Øresundsbroen: 7845m")
*
* charset(Charset.CP852) // Can encode ů, but not Ø
* charset(IBM852) // Can encode ů, but not Ø
* line("Karlův most: 515m")
* }
* ```
Expand All @@ -192,10 +193,10 @@ public class CommandBuilder(
* line("Famous bridges")
*
* // 865 can encode Ø, but not ů
* charset(Charset.CP865)
* charset(IBM865)
* line("Øresundsbroen: 7845m")
*
* withCharset(Charset.CP852) {
* withCharset(IBM852) {
* // 852 can encode ů, but not Ø
* line("Karlův most: 515m")
* }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024 Ondřej Karmazín
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cz.multiplatform.escpos4k.core.encoding

import cz.multiplatform.escpos4k.core.encoding.charset.Charset

private const val replacement = '?'
private const val replacementByte = replacement.code.toByte()

internal fun String.encode(charset: Charset): ByteArray {
val output = ByteArray(length)

for ((index, char) in this.withIndex()) {
val byte: Byte =
when {
// 0..31 are control characters, always replaced
char.code < 32 -> {
log("Replacing control character '${char.code}' at index $index with '$replacement'.")
replacementByte
}

// Printable ASCII up to 127
char.code < 128 -> {
char.code.toByte()
}

// Page-specific characters
else -> {
val byte = charset.mapping[char]?.toByte()
if (byte == null) {
log(
"[${charset.ianaName}] Replacing unmappable code unit '${char.code}' at index $index with '$replacement'")
}
byte ?: replacementByte
}
}

output[index] = byte
}

return output
}

private fun log(msg: String) {
val debug = false
if (debug) {
println(msg)
}
}
Loading

0 comments on commit 33cbf3a

Please sign in to comment.