-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The library now performs its own text encoding, removing the dependency on platform utilities.
- Loading branch information
Showing
37 changed files
with
4,208 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
escpos4k/src/androidMain/kotlin/cz/multiplatform/escpos4k/core/Encoder.android.kt
This file was deleted.
Oops, something went wrong.
74 changes: 0 additions & 74 deletions
74
escpos4k/src/appleMain/kotlin/cz/multiplatform/escpos4k/core/Encoder.apple.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
escpos4k/src/commonMain/kotlin/cz/multiplatform/escpos4k/core/Encoder.kt
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
escpos4k/src/commonMain/kotlin/cz/multiplatform/escpos4k/core/encoding/Encoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.