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

Commonize text encoding #34

Merged
merged 34 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bca0509
Commonize character encoding
okarmazin Jan 28, 2024
79909bc
Delete the old expect/actual fun encode
okarmazin Sep 18, 2024
78789fd
Move Charset to package encoding.charset
okarmazin Sep 18, 2024
65d5f8c
Move Encoder.kt to package encoding
okarmazin Sep 18, 2024
4418754
Extract CP437 to its own file
okarmazin Sep 18, 2024
cacc080
Extract CP850 to its own file
okarmazin Sep 18, 2024
5ecd9cc
Rename CP437 to IBM437
okarmazin Sep 18, 2024
f52916b
Rename CP850 to IBM850
okarmazin Sep 18, 2024
d955445
Extract CP860 to its own file
okarmazin Sep 18, 2024
d9e1359
Extract CP863 to its own file
okarmazin Sep 18, 2024
c45222f
Extract CP865 to its own file
okarmazin Sep 18, 2024
d687bd5
Extract CP857 to its own file
okarmazin Sep 18, 2024
640f33b
Extract ISO_8859_7 to its own file
okarmazin Sep 18, 2024
924053c
Extract Windows1252 to its own file
okarmazin Sep 18, 2024
8a1ea69
Extract CP866 to its own file
okarmazin Sep 18, 2024
4e59e2c
Extract CP852 to its own file
okarmazin Sep 18, 2024
5915b77
Extract CP858 to its own file
okarmazin Sep 18, 2024
1810b79
Extract CP775 to its own file
okarmazin Sep 18, 2024
bf20998
Extract CP855 to its own file
okarmazin Sep 18, 2024
4296181
Extract CP861 to its own file
okarmazin Sep 18, 2024
e08dadf
Extract CP862 to its own file
okarmazin Sep 18, 2024
2b1070d
Extract CP864 to its own file
okarmazin Sep 18, 2024
c7f3f2e
Extract CP869 to its own file
okarmazin Sep 18, 2024
5d5e68d
Extract ISO_8859_2 to its own file
okarmazin Sep 18, 2024
164de0c
Extract ISO_8859_15 to its own file
okarmazin Sep 18, 2024
f670889
Extract Windows1250 to its own file
okarmazin Sep 18, 2024
a8bb52f
Extract Windows1251 to its own file
okarmazin Sep 18, 2024
635340e
Extract Windows1253 to its own file
okarmazin Sep 18, 2024
26015d7
Extract Windows1254 to its own file
okarmazin Sep 18, 2024
f4c5fb3
Extract Windows1255 to its own file
okarmazin Sep 18, 2024
d8bfd7a
Extract Windows1256 to its own file
okarmazin Sep 18, 2024
d4ce4e6
Extract Windows1257 to its own file
okarmazin Sep 18, 2024
9c98cde
Extract Windows1258 to its own file
okarmazin Sep 18, 2024
5821008
Rename the rest of CPxxx charsets to IBMxxx
okarmazin Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,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