Skip to content

Commit f4e0d28

Browse files
committedDec 28, 2024·
fix: text and columns aligments
1 parent c5672a5 commit f4e0d28

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed
 

‎android/src/main/java/com/bleprinter/BlePrinterModule.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,14 @@ class BlePrinterModule(reactContext: ReactApplicationContext) :
233233
size: Float = 24f,
234234
promise: Promise
235235
) {
236-
val text = Utils().twoColumns(leftText, rightText, bold, size)
237-
printText(text, bold = bold, size = size, promise = promise)
236+
val text = Utils().twoColumnsBitmap(leftText, rightText, bold, size)
237+
val stream = getStream()
238+
try {
239+
stream?.write(text)
240+
promise.resolve("Printed Text")
241+
} catch (e: Exception) {
242+
promise.resolve(e.message)
243+
}
238244
}
239245

240246
@ReactMethod

‎android/src/main/java/com/bleprinter/Utils.kt

+32-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Utils {
2525
textSize = fontSize
2626
isAntiAlias = true
2727
typeface =
28-
if (bold) Typeface.create(Typeface.DEFAULT, Typeface.BOLD) else Typeface.DEFAULT
28+
if (bold) Typeface.create(Typeface.MONOSPACE, Typeface.BOLD) else Typeface.MONOSPACE
2929
}
3030

3131
if(align != null) {
@@ -115,8 +115,8 @@ public class Utils {
115115
}
116116

117117
val xPosition = when (alignment) {
118-
"LEFT" -> xOffset
119-
"RIGHT" -> xOffset + (width - textWidth).toInt()
118+
"LEFT" -> xOffset + 10f
119+
"RIGHT" -> xOffset + (width - textWidth).toInt() - 10f
120120
"CENTER" -> xOffset + ((width - textWidth) / 2).toInt()
121121
else -> throw IllegalArgumentException("Invalid alignment: $alignment")
122122
}
@@ -203,22 +203,43 @@ public class Utils {
203203
return lines
204204
}
205205

206-
fun twoColumns(
206+
fun twoColumnsBitmap(
207207
leftText: String,
208208
rightText: String,
209209
bold: Boolean = false,
210-
size: Float = 24f
211-
): String {
210+
size: Float = 24f,
211+
): ByteArray {
212+
val paperWidth = PAPER_WIDTH
212213
val paint = getPaint(bold = bold, fontSize = size)
214+
215+
// Calcula as larguras dos textos
213216
val leftWidth = paint.measureText(leftText)
214217
val rightWidth = paint.measureText(rightText)
215218

216-
// Calcula o espaço disponível entre os textos
217-
val totalSpace = 374 - (leftWidth + rightWidth)
219+
// Valida se o texto cabe no papel
220+
if (leftWidth + rightWidth > paperWidth) {
221+
throw IllegalArgumentException("Os textos são muito largos para caber na largura do papel.")
222+
}
223+
224+
// Calcula a posição da coluna da direita
225+
val lineHeight = paint.fontMetrics.bottom - paint.fontMetrics.top
226+
val bitmapHeight = lineHeight.toInt()
227+
228+
// Cria o bitmap
229+
val bitmap = Bitmap.createBitmap(paperWidth.toInt(), bitmapHeight, Bitmap.Config.ARGB_8888)
230+
val canvas = Canvas(bitmap)
231+
canvas.drawColor(Color.WHITE)
232+
233+
// Desenha o texto da esquerda
234+
val leftX = 0f
235+
val leftY = -paint.fontMetrics.top
236+
canvas.drawText(leftText, leftX + 10f, leftY, paint)
237+
238+
// Desenha o texto da direita
239+
val rightX = paperWidth - rightWidth
240+
canvas.drawText(rightText, rightX - 10f, leftY, paint)
218241

219-
// Adiciona espaços entre os textos
220-
val spaces = " ".repeat((totalSpace / paint.measureText(" ")).toInt())
221-
return "$leftText$spaces$rightText"
242+
return convertBitmapToPrinterArray(bitmap)
222243
}
223244

224245
fun drawStyledStroke(

‎example/src/App.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from 'react-native';
1111
import BlePrinter, { type Device } from 'react-native-ble-printer';
1212
import { DeviceItem } from './components/DeviceItem';
13+
import { printReceipt } from './services/printReceipt';
1314

1415
export default function App() {
1516
const [connectedDevice, setConnectedDevice] = useState<Device | null>(null);
@@ -19,7 +20,7 @@ export default function App() {
1920

2021
async function handlePrintReceipt() {
2122
try {
22-
await BlePrinter.printText('Hello world!');
23+
await printReceipt();
2324
} catch (error) {
2425
console.log(error);
2526
}

‎example/src/services/printReceipt.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,44 +36,44 @@ export async function printReceipt() {
3636
await BlePrinter.printStroke(20, 3);
3737
await BlePrinter.printColumns(
3838
['ENTRADA', 'ATUAL', 'ANTERIOR'],
39-
[124, 124, 124],
39+
[128, 128, 128],
4040
['LEFT', 'RIGHT', 'RIGHT'],
4141
{ size: 20, bold: true }
4242
);
4343
await BlePrinter.printStroke(20, 3);
4444
await BlePrinter.printColumns(
4545
[entryTotal.toString(), entryTotal.toString()],
46-
[250, 124],
46+
[256, 128],
4747
['RIGHT', 'RIGHT']
4848
);
4949
await BlePrinter.printText(entryTotal.toFixed(0), { align: 'RIGHT' });
5050
await BlePrinter.printStroke(20, 3);
5151

5252
await BlePrinter.printColumns(
5353
['SAÍDA', 'ATUAL', 'ANTERIOR'],
54-
[124, 124, 124],
54+
[128, 128, 128],
5555
['LEFT', 'RIGHT', 'RIGHT'],
5656
{ size: 20, bold: true }
5757
);
5858
await BlePrinter.printStroke(20, 3);
5959
await BlePrinter.printColumns(
6060
[paymentTotal.toString(), paymentTotal.toString()],
61-
[250, 124],
61+
[250, 128],
6262
['RIGHT', 'RIGHT']
6363
);
6464

6565
await BlePrinter.printText(entryTotal.toFixed(0), { align: 'RIGHT' });
6666
await BlePrinter.printStroke(20, 3);
6767
await BlePrinter.printColumns(
6868
['TOTAL', 'SALDO', 'COMISSÃO'],
69-
[124, 124, 124],
69+
[128, 128, 128],
7070
['LEFT', 'RIGHT', 'RIGHT'],
7171
{ size: 20, bold: true }
7272
);
7373
await BlePrinter.printStroke(20, 3);
7474
await BlePrinter.printColumns(
7575
['1000', '432', '432'],
76-
[124, 124, 124],
76+
[128, 128, 128],
7777
['LEFT', 'RIGHT', 'RIGHT']
7878
);
7979

0 commit comments

Comments
 (0)
Please sign in to comment.