1
0
mirror of https://git.familie-radermacher.ch/linux/ptouch-print.git synced 2025-07-13 02:03:44 +00:00

Compare commits

..

7 Commits
v1.6 ... master

Author SHA1 Message Date
Dominic Radermacher
1f450d9124 Add support for PT-E310BT (thanks to Christian Radin) 2025-06-22 08:13:08 +02:00
Marcus Müller
1a39fe1284 udev uaccess rules must have lower order than 73-seat-late.rules
see, for example, https://github.com/bigbigmdm/IMSProg/issues/98 . If the rule granting uaccess tag to a device is loaded after the seat-late rule, seat-late can't grant the user access to the device.

Signed-off-by: Marcus Müller <mueller@baseband.digital>
2025-06-01 20:15:29 +02:00
Dominic Radermacher
2c9828af87 add support for PT-9200DX, thanks Ralf Lieb for reporting 2025-06-01 20:12:20 +02:00
Dominic Radermacher
2f18522602 fix segfault for last commit 2025-05-19 13:33:55 +02:00
Dominic Radermacher
140bf0a6cc for printers with less than 128px printhead, make sure we dont print too much pixels 2025-05-12 18:29:56 +02:00
Dominic Radermacher
a6c67df2c9 cosmetics 2025-05-12 17:59:45 +02:00
Dominic Radermacher
ec923ed579 Add support for PT-2300 and fix 112px width centering (thanks to Bradley Erickson) 2025-05-12 17:47:00 +02:00
4 changed files with 163 additions and 113 deletions

View File

@ -71,6 +71,6 @@ install(TARGETS ${PROJECT_NAME} DESTINATION bin)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/ptouch-print.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/ptouch-print.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
if(EXISTS /etc/udev/rules.d) if(EXISTS /etc/udev/rules.d)
install(FILES udev/90-usb-ptouch-permissions.rules DESTINATION /etc/udev/rules.d) install(FILES udev/20-usb-ptouch-permissions.rules DESTINATION /etc/udev/rules.d)
install(CODE "execute_process(COMMAND udevadm control --reload-rules)") install(CODE "execute_process(COMMAND udevadm control --reload-rules)")
endif() endif()

View File

@ -1,7 +1,7 @@
/* /*
libptouch - functions to help accessing a brother ptouch libptouch - functions to help accessing a brother ptouch
Copyright (C) 2013-2023 Dominic Radermacher <dominic@familie-radermacher.ch> Copyright (C) 2013-2025 Dominic Radermacher <dominic@familie-radermacher.ch>
This program is free software; you can redistribute it and/or modify it This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3 as under the terms of the GNU General Public License version 3 as
@ -45,9 +45,11 @@ struct _pt_tape_info tape_info[]= {
}; };
struct _pt_dev_info ptdevs[] = { struct _pt_dev_info ptdevs[] = {
{0x04f9, 0x2001, "PT-9200DX", 384, 360, FLAG_RASTER_PACKBITS|FLAG_HAS_PRECUT}, /* 360dpi, maximum 128px, max tape width 36mm */
{0x04f9, 0x2004, "PT-2300", 112, 180, FLAG_RASTER_PACKBITS|FLAG_HAS_PRECUT}, /* 180dpi, 112px printhead */
{0x04f9, 0x2007, "PT-2420PC", 128, 180, FLAG_RASTER_PACKBITS}, /* 180dpi, 128px, maximum tape width 24mm, must send TIFF compressed pixel data */ {0x04f9, 0x2007, "PT-2420PC", 128, 180, FLAG_RASTER_PACKBITS}, /* 180dpi, 128px, maximum tape width 24mm, must send TIFF compressed pixel data */
{0x04f9, 0x2011, "PT-2450PC", 128, 180, FLAG_RASTER_PACKBITS}, {0x04f9, 0x2011, "PT-2450PC", 128, 180, FLAG_RASTER_PACKBITS},
{0x04f9, 0x2019, "PT-1950", 128, 180, FLAG_RASTER_PACKBITS}, /* 180dpi, apparently 112px printhead ?, maximum tape width 18mm - unconfirmed if it works */ {0x04f9, 0x2019, "PT-1950", 112, 180, FLAG_RASTER_PACKBITS}, /* 180dpi, apparently 112px printhead ?, maximum tape width 18mm - unconfirmed if it works */
{0x04f9, 0x201f, "PT-2700", 128, 180, FLAG_HAS_PRECUT}, {0x04f9, 0x201f, "PT-2700", 128, 180, FLAG_HAS_PRECUT},
{0x04f9, 0x202c, "PT-1230PC", 128, 180, FLAG_NONE}, /* 180dpi, supports tapes up to 12mm - I don't know how much pixels it can print! */ {0x04f9, 0x202c, "PT-1230PC", 128, 180, FLAG_NONE}, /* 180dpi, supports tapes up to 12mm - I don't know how much pixels it can print! */
/* Notes about the PT-1230PC: While it is true that this printer supports /* Notes about the PT-1230PC: While it is true that this printer supports
@ -79,6 +81,11 @@ struct _pt_dev_info ptdevs[] = {
{0x04f9, 0x20e1, "PT-D610BT", 128, 180, FLAG_P700_INIT|FLAG_USE_INFO_CMD|FLAG_HAS_PRECUT|FLAG_D460BT_MAGIC}, {0x04f9, 0x20e1, "PT-D610BT", 128, 180, FLAG_P700_INIT|FLAG_USE_INFO_CMD|FLAG_HAS_PRECUT|FLAG_D460BT_MAGIC},
//{0x04f9, 0x200d, "PT-3600", 384, 360, FLAG_RASTER_PACKBITS}, //{0x04f9, 0x200d, "PT-3600", 384, 360, FLAG_RASTER_PACKBITS},
{0x04f9, 0x20af, "PT-P710BT", 128, 180, FLAG_RASTER_PACKBITS|FLAG_HAS_PRECUT}, {0x04f9, 0x20af, "PT-P710BT", 128, 180, FLAG_RASTER_PACKBITS|FLAG_HAS_PRECUT},
/* added by Christian, PT-E310BT (aka PT-E310BTVP) requires these flags, otherwise not returning from libusb_bulk_transfer-call */
/* printhead 128px, 180 dpi resolution */
/* 3,5/6/9/12/18 mm TZe Tapes, 12mm and 18mm tested */
/* 5,2/9/11,2 mm HSe heat shrink tubes not tested, probably requiring extension of struct _pt_tape_info */
{0x04f9, 0x2201, "PT-E310BT", 128, 180, FLAG_P700_INIT|FLAG_USE_INFO_CMD|FLAG_D460BT_MAGIC},
{0,0,"",0,0,0} {0,0,"",0,0,0}
}; };
@ -162,6 +169,9 @@ int ptouch_open(ptouch_dev *ptdev)
int ptouch_close(ptouch_dev ptdev) int ptouch_close(ptouch_dev ptdev)
{ {
if (!ptdev) {
return -1;
}
libusb_release_interface(ptdev->h, 0); libusb_release_interface(ptdev->h, 0);
libusb_close(ptdev->h); libusb_close(ptdev->h);
return 0; return 0;
@ -171,7 +181,11 @@ int ptouch_send(ptouch_dev ptdev, uint8_t *data, size_t len)
{ {
int r, tx; int r, tx;
if ((ptdev == NULL) || (len > 128)) { if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_send() with NULL ptdev\n"));
return -1;
}
if (len > 128) {
return -1; return -1;
} }
if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, (int)len, &tx, 0)) != 0) { if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, (int)len, &tx, 0)) != 0) {
@ -227,6 +241,11 @@ int ptouch_enable_packbits(ptouch_dev ptdev)
/* print information command */ /* print information command */
int ptouch_info_cmd(ptouch_dev ptdev, int size_x) int ptouch_info_cmd(ptouch_dev ptdev, int size_x)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_info_cmd() with NULL ptdev\n"));
return -1;
}
/* 1B 69 7A {n1} {n2} {n3} {n4} {n5} {n6} {n7} {n8} {n9} {n10} */ /* 1B 69 7A {n1} {n2} {n3} {n4} {n5} {n6} {n7} {n8} {n9} {n10} */
uint8_t cmd[] = "\x1b\x69\x7a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; uint8_t cmd[] = "\x1b\x69\x7a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
@ -264,6 +283,10 @@ int ptouch_send_precut_cmd(ptouch_dev ptdev, int precut)
int ptouch_rasterstart(ptouch_dev ptdev) int ptouch_rasterstart(ptouch_dev ptdev)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_rasterstart() with NULL ptdev\n"));
return -1;
}
/* 1B 69 52 01 = Select graphics transfer mode = Raster */ /* 1B 69 52 01 = Select graphics transfer mode = Raster */
char cmd[] = "\x1b\x69\x52\x01"; char cmd[] = "\x1b\x69\x52\x01";
/* 1B 69 61 01 = switch mode (0=esc/p, 1=raster mode) */ /* 1B 69 61 01 = switch mode (0=esc/p, 1=raster mode) */
@ -291,6 +314,11 @@ int ptouch_ff(ptouch_dev ptdev)
/* finish print and either cut or leave tape in machine */ /* finish print and either cut or leave tape in machine */
int ptouch_finalize(ptouch_dev ptdev, int chain) int ptouch_finalize(ptouch_dev ptdev, int chain)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_finalize() with NULL ptdev\n"));
return -1;
}
char cmd_eject[]="\x1a"; /* Print command with feeding */ char cmd_eject[]="\x1a"; /* Print command with feeding */
char cmd_chain[]="\x0c"; /* Print command (no cut) */ char cmd_chain[]="\x0c"; /* Print command (no cut) */
@ -320,6 +348,11 @@ int ptouch_getstatus(ptouch_dev ptdev)
int i, r, tx=0, tries=0; int i, r, tx=0, tries=0;
struct timespec w; struct timespec w;
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_getstatus() with NULL ptdev\n"));
return -1;
}
ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd)); ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
while (tx == 0) { while (tx == 0) {
w.tv_sec=0; w.tv_sec=0;
@ -371,11 +404,19 @@ int ptouch_getstatus(ptouch_dev ptdev)
size_t ptouch_get_tape_width(ptouch_dev ptdev) size_t ptouch_get_tape_width(ptouch_dev ptdev)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_get_tape_width() with NULL ptdev\n"));
return 0;
}
return ptdev->tape_width_px; return ptdev->tape_width_px;
} }
size_t ptouch_get_max_width(ptouch_dev ptdev) size_t ptouch_get_max_width(ptouch_dev ptdev)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_get_max_width() with NULL ptdev\n"));
return 0;
}
return ptdev->devinfo->max_px; return ptdev->devinfo->max_px;
} }
@ -384,6 +425,10 @@ int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, size_t len)
uint8_t buf[64]; uint8_t buf[64];
int rc; int rc;
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_sendraster() with NULL ptdev\n"));
return -1;
}
if (len > (size_t)(ptdev->devinfo->max_px / 8)) { if (len > (size_t)(ptdev->devinfo->max_px / 8)) {
return -1; return -1;
} }

View File

@ -1,7 +1,7 @@
/* /*
ptouch-print - Print labels with images or text on a Brother P-Touch ptouch-print - Print labels with images or text on a Brother P-Touch
Copyright (C) 2015-2024 Dominic Radermacher <dominic@familie-radermacher.ch> Copyright (C) 2015-2025 Dominic Radermacher <dominic@familie-radermacher.ch>
This program is free software; you can redistribute it and/or modify it This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3 as under the terms of the GNU General Public License version 3 as
@ -44,8 +44,8 @@ int needed_width(char *text, char *font, int fsz);
int print_img(ptouch_dev ptdev, gdImage *im, int chain); int print_img(ptouch_dev ptdev, gdImage *im, int chain);
int write_png(gdImage *im, const char *file); int write_png(gdImage *im, const char *file);
gdImage *img_append(gdImage *in_1, gdImage *in_2); gdImage *img_append(gdImage *in_1, gdImage *in_2);
gdImage *img_cutmark(int tape_width); gdImage *img_cutmark(int print_width);
gdImage *render_text(char *font, char *line[], int lines, int tape_width); gdImage *render_text(char *font, char *line[], int lines, int print_width);
void unsupported_printer(ptouch_dev ptdev); void unsupported_printer(ptouch_dev ptdev);
void usage(char *progname); void usage(char *progname);
int parse_args(int argc, char **argv); int parse_args(int argc, char **argv);
@ -67,7 +67,7 @@ void rasterline_setpixel(uint8_t* rasterline, size_t size, int pixel)
{ {
// TODO: pixel should be unsigned, since we can't have negative // TODO: pixel should be unsigned, since we can't have negative
// if (pixel > ptdev->devinfo->device_max_px) { // if (pixel > ptdev->devinfo->device_max_px) {
if (pixel > (int)(size*8)) { if ((pixel < 0) || (pixel >= (int)(size*8))) {
return; return;
} }
rasterline[(size-1)-(pixel/8)] |= (uint8_t)(1<<(pixel%8)); rasterline[(size-1)-(pixel/8)] |= (uint8_t)(1<<(pixel%8));
@ -76,24 +76,23 @@ void rasterline_setpixel(uint8_t* rasterline, size_t size, int pixel)
int print_img(ptouch_dev ptdev, gdImage *im, int chain) int print_img(ptouch_dev ptdev, gdImage *im, int chain)
{ {
int d,i,k,offset,tape_width;
uint8_t rasterline[(ptdev->devinfo->max_px)/8]; uint8_t rasterline[(ptdev->devinfo->max_px)/8];
if (!im) { if (!im) {
printf(_("nothing to print\n")); printf(_("nothing to print\n"));
return -1; return -1;
} }
tape_width=ptouch_get_tape_width(ptdev); int tape_width = ptouch_get_tape_width(ptdev);
size_t max_pixels = ptouch_get_max_width(ptdev);
/* find out whether color 0 or color 1 is darker */ /* find out whether color 0 or color 1 is darker */
d=(gdImageRed(im,1)+gdImageGreen(im,1)+gdImageBlue(im,1) < gdImageRed(im,0)+gdImageGreen(im,0)+gdImageBlue(im,0))?1:0; int d = (gdImageRed(im,1) + gdImageGreen(im,1) + gdImageBlue(im,1) < gdImageRed(im,0) + gdImageGreen(im,0) + gdImageBlue(im,0))?1:0;
if (gdImageSY(im) > tape_width) { if (gdImageSY(im) > tape_width) {
printf(_("image is too large (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im)); printf(_("image is too large (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im));
printf(_("maximum printing width for this tape is %ipx\n"), tape_width); printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
return -1; return -1;
} }
//offset=64-(gdImageSY(im)/2); /* always print centered */ printf(_("image size (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im));
size_t max_pixels=ptouch_get_max_width(ptdev); int offset = ((int)max_pixels / 2) - (gdImageSY(im)/2); /* always print centered */
offset=((int)max_pixels / 2)-(gdImageSY(im)/2); /* always print centered */
printf("max_pixels=%ld, offset=%d\n", max_pixels, offset); printf("max_pixels=%ld, offset=%d\n", max_pixels, offset);
if ((ptdev->devinfo->flags & FLAG_RASTER_PACKBITS) == FLAG_RASTER_PACKBITS) { if ((ptdev->devinfo->flags & FLAG_RASTER_PACKBITS) == FLAG_RASTER_PACKBITS) {
if (debug) { if (debug) {
@ -129,14 +128,14 @@ int print_img(ptouch_dev ptdev, gdImage *im, int chain)
printf(_("send precut command\n")); printf(_("send precut command\n"));
} }
} }
for (k=0; k<gdImageSX(im); k+=1) { for (int k = 0; k < gdImageSX(im); ++k) {
memset(rasterline, 0, sizeof(rasterline)); memset(rasterline, 0, sizeof(rasterline));
for (i=0; i<gdImageSY(im); i+=1) { for (int i = 0; i < gdImageSY(im); ++i) {
if (gdImageGetPixel(im, k, gdImageSY(im)-1-i) == d) { if (gdImageGetPixel(im, k, gdImageSY(im) - 1 - i) == d) {
rasterline_setpixel(rasterline, sizeof(rasterline), offset+i); rasterline_setpixel(rasterline, sizeof(rasterline), offset+i);
} }
} }
if (ptouch_sendraster(ptdev, rasterline, 16) != 0) { if (ptouch_sendraster(ptdev, rasterline, (ptdev->devinfo->max_px / 8)) != 0) {
printf(_("ptouch_sendraster() failed\n")); printf(_("ptouch_sendraster() failed\n"));
return -1; return -1;
} }
@ -153,10 +152,10 @@ int print_img(ptouch_dev ptdev, gdImage *im, int chain)
gdImage *image_load(const char *file) gdImage *image_load(const char *file)
{ {
const uint8_t png[8]={0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a}; const uint8_t png[8] = {0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
char d[10]; char d[10];
FILE *f; FILE *f;
gdImage *img=NULL; gdImage *img = NULL;
if (!strcmp(file, "-")) { if (!strcmp(file, "-")) {
f = stdin; f = stdin;
@ -167,14 +166,14 @@ gdImage *image_load(const char *file)
return NULL; return NULL;
} }
if (fseek(f, 0L, SEEK_SET)) { /* file is not seekable. eg 'stdin' */ if (fseek(f, 0L, SEEK_SET)) { /* file is not seekable. eg 'stdin' */
img=gdImageCreateFromPng(f); img = gdImageCreateFromPng(f);
} else { } else {
if (fread(d, sizeof(d), 1, f) != 1) { if (fread(d, sizeof(d), 1, f) != 1) {
return NULL; return NULL;
} }
rewind(f); rewind(f);
if (memcmp(d, png, 8) == 0) { if (memcmp(d, png, 8) == 0) {
img=gdImageCreateFromPng(f); img = gdImageCreateFromPng(f);
} }
} }
fclose(f); fclose(f);
@ -201,14 +200,12 @@ int write_png(gdImage *im, const char *file)
int get_baselineoffset(char *text, char *font, int fsz) int get_baselineoffset(char *text, char *font, int fsz)
{ {
int brect[8]; int brect[8];
int o_offset;
int text_offset;
/* NOTE: This assumes that 'o' is always on the baseline */ /* NOTE: This assumes that 'o' is always on the baseline */
gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "o"); gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "o");
o_offset=brect[1]; int o_offset = brect[1];
gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text); gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text);
text_offset=brect[1]; int text_offset = brect[1];
if (debug) { if (debug) {
printf(_("debug: o baseline offset - %d\n"), o_offset); printf(_("debug: o baseline offset - %d\n"), o_offset);
printf(_("debug: text baseline offset - %d\n"), text_offset); printf(_("debug: text baseline offset - %d\n"), text_offset);
@ -223,7 +220,7 @@ int get_baselineoffset(char *text, char *font, int fsz)
-------------------------------------------------------------------- */ -------------------------------------------------------------------- */
int find_fontsize(int want_px, char *font, char *text) int find_fontsize(int want_px, char *font, char *text)
{ {
int save=0; int save = 0;
int brect[8]; int brect[8];
for (int i=4; ; ++i) { for (int i=4; ; ++i) {
@ -231,7 +228,7 @@ int find_fontsize(int want_px, char *font, char *text)
break; break;
} }
if (brect[1]-brect[5] <= want_px) { if (brect[1]-brect[5] <= want_px) {
save=i; save = i;
} else { } else {
break; break;
} }
@ -262,12 +259,12 @@ int offset_x(char *text, char *font, int fsz)
return -brect[0]; return -brect[0];
} }
gdImage *render_text(char *font, char *line[], int lines, int tape_width) gdImage *render_text(char *font, char *line[], int lines, int print_width)
{ {
int brect[8]; int brect[8];
int i, black, x=0, tmp=0, fsz=0; int i, black, x = 0, tmp = 0, fsz = 0;
char *p; char *p;
gdImage *im=NULL; gdImage *im = NULL;
if (debug) { if (debug) {
printf(_("render_text(): %i lines, font = '%s'\n"), lines, font); printf(_("render_text(): %i lines, font = '%s'\n"), lines, font);
@ -276,11 +273,11 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width)
printf(_("warning: font config not available\n")); printf(_("warning: font config not available\n"));
} }
if (fontsize > 0) { if (fontsize > 0) {
fsz=fontsize; fsz = fontsize;
printf(_("setting font size=%i\n"), fsz); printf(_("setting font size=%i\n"), fsz);
} else { } else {
for (i=0; i<lines; ++i) { for (i = 0; i < lines; ++i) {
if ((tmp=find_fontsize(tape_width/lines, font, line[i])) < 0) { if ((tmp = find_fontsize(print_width/lines, font, line[i])) < 0) {
printf(_("could not estimate needed font size\n")); printf(_("could not estimate needed font size\n"));
return NULL; return NULL;
} }
@ -290,48 +287,48 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width)
} }
printf(_("choosing font size=%i\n"), fsz); printf(_("choosing font size=%i\n"), fsz);
} }
for(i=0; i<lines; ++i) { for(i = 0; i < lines; ++i) {
tmp=needed_width(line[i], font_file, fsz); tmp = needed_width(line[i], font_file, fsz);
if (tmp > x) { if (tmp > x) {
x=tmp; x = tmp;
} }
} }
im=gdImageCreatePalette(x, tape_width); im = gdImageCreatePalette(x, print_width);
gdImageColorAllocate(im, 255, 255, 255); gdImageColorAllocate(im, 255, 255, 255);
black=gdImageColorAllocate(im, 0, 0, 0); black = gdImageColorAllocate(im, 0, 0, 0);
/* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */ /* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */
/* find max needed line height for ALL lines */ /* find max needed line height for ALL lines */
int max_height=0; int max_height=0;
for (i=0; i<lines; ++i) { for (i = 0; i < lines; ++i) {
if ((p=gdImageStringFT(NULL, &brect[0], -black, font, fsz, 0.0, 0, 0, line[i])) != NULL) { if ((p = gdImageStringFT(NULL, &brect[0], -black, font, fsz, 0.0, 0, 0, line[i])) != NULL) {
printf(_("error in gdImageStringFT: %s\n"), p); printf(_("error in gdImageStringFT: %s\n"), p);
} }
//int ofs=get_baselineoffset(line[i], font_file, fsz); //int ofs = get_baselineoffset(line[i], font_file, fsz);
int lineheight=brect[1]-brect[5]; int lineheight = brect[1]-brect[5];
if (lineheight > max_height) { if (lineheight > max_height) {
max_height=lineheight; max_height = lineheight;
} }
} }
if (debug) { if (debug) {
printf("debug: needed (max) height is %ipx\n", max_height); printf("debug: needed (max) height is %ipx\n", max_height);
} }
if ((max_height * lines) > tape_width) { if ((max_height * lines) > print_width) {
printf("Font size %d too large for %d lines\n", fsz, lines); printf("Font size %d too large for %d lines\n", fsz, lines);
return NULL; return NULL;
} }
/* calculate unused pixels */ /* calculate unused pixels */
int unused_px = tape_width - (max_height * lines); int unused_px = print_width - (max_height * lines);
/* now render lines */ /* now render lines */
for (i=0; i<lines; ++i) { for (i = 0; i < lines; ++i) {
int ofs=get_baselineoffset(line[i], font_file, fsz); int ofs = get_baselineoffset(line[i], font_file, fsz);
//int pos=((i)*(tape_width/(lines)))+(max_height)-ofs-1; //int pos = ((i)*(print_width/(lines)))+(max_height)-ofs-1;
int pos=((i)*(tape_width/(lines)))+(max_height)-ofs; int pos = ((i)*(print_width/(lines)))+(max_height)-ofs;
pos += (unused_px/lines) / 2; pos += (unused_px/lines) / 2;
if (debug) { if (debug) {
printf("debug: line %i pos=%i ofs=%i\n", i+1, pos, ofs); printf("debug: line %i pos=%i ofs=%i\n", i+1, pos, ofs);
} }
int off_x = offset_x(line[i], font_file, fsz); int off_x = offset_x(line[i], font_file, fsz);
if ((p=gdImageStringFT(im, &brect[0], -black, font, fsz, 0.0, off_x, pos, line[i])) != NULL) { if ((p = gdImageStringFT(im, &brect[0], -black, font, fsz, 0.0, off_x, pos, line[i])) != NULL) {
printf(_("error in gdImageStringFT: %s\n"), p); printf(_("error in gdImageStringFT: %s\n"), p);
} }
} }
@ -340,27 +337,27 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width)
gdImage *img_append(gdImage *in_1, gdImage *in_2) gdImage *img_append(gdImage *in_1, gdImage *in_2)
{ {
gdImage *out=NULL; gdImage *out = NULL;
int width=0; int width = 0;
int i_1_x=0; int i_1_x = 0;
int length=0; int length = 0;
if (in_1 != NULL) { if (in_1 != NULL) {
width=gdImageSY(in_1); width = gdImageSY(in_1);
length=gdImageSX(in_1); length = gdImageSX(in_1);
i_1_x=gdImageSX(in_1); i_1_x = gdImageSX(in_1);
} }
if (in_2 != NULL) { if (in_2 != NULL) {
length += gdImageSX(in_2); length += gdImageSX(in_2);
/* width should be the same, but let's be sure */ /* width should be the same, but let's be sure */
if (gdImageSY(in_2) > width) { if (gdImageSY(in_2) > width) {
width=gdImageSY(in_2); width = gdImageSY(in_2);
} }
} }
if ((width == 0) || (length == 0)) { if ((width == 0) || (length == 0)) {
return NULL; return NULL;
} }
out=gdImageCreatePalette(length, width); out = gdImageCreatePalette(length, width);
if (out == NULL) { if (out == NULL) {
return NULL; return NULL;
} }
@ -384,36 +381,36 @@ gdImage *img_append(gdImage *in_1, gdImage *in_2)
return out; return out;
} }
gdImage *img_cutmark(int tape_width) gdImage *img_cutmark(int print_width)
{ {
gdImage *out=NULL; gdImage *out = NULL;
int style_dashed[6]; int style_dashed[6];
out=gdImageCreatePalette(9, tape_width); out = gdImageCreatePalette(9, print_width);
if (out == NULL) { if (out == NULL) {
return NULL; return NULL;
} }
gdImageColorAllocate(out, 255, 255, 255); gdImageColorAllocate(out, 255, 255, 255);
int black=gdImageColorAllocate(out, 0, 0, 0); int black = gdImageColorAllocate(out, 0, 0, 0);
style_dashed[0]=gdTransparent; style_dashed[0] = gdTransparent;
style_dashed[1]=gdTransparent; style_dashed[1] = gdTransparent;
style_dashed[2]=gdTransparent; style_dashed[2] = gdTransparent;
style_dashed[3]=black; style_dashed[3] = black;
style_dashed[4]=black; style_dashed[4] = black;
style_dashed[5]=black; style_dashed[5] = black;
gdImageSetStyle(out, style_dashed, 6); gdImageSetStyle(out, style_dashed, 6);
gdImageLine(out, 5, 0, 5, tape_width-1, gdStyled); gdImageLine(out, 5, 0, 5, print_width - 1, gdStyled);
return out; return out;
} }
gdImage *img_padding(int tape_width, int length) gdImage *img_padding(int print_width, int length)
{ {
gdImage *out=NULL; gdImage *out = NULL;
if ((length < 1) || (length > 256)) { if ((length < 1) || (length > 256)) {
length=1; length=1;
} }
out=gdImageCreatePalette(length, tape_width); out = gdImageCreatePalette(length, print_width);
if (out == NULL) { if (out == NULL) {
return NULL; return NULL;
} }
@ -452,62 +449,62 @@ int parse_args(int argc, char **argv)
{ {
int lines, i; int lines, i;
for (i=1; i<argc; ++i) { for (i = 1; i < argc; ++i) {
if (*argv[i] != '-') { if (*argv[i] != '-') {
break; break;
} }
if (strcmp(&argv[i][1], "-font") == 0) { if (strcmp(&argv[i][1], "-font") == 0) {
if (i+1<argc) { if (i+1 < argc) {
font_file=argv[++i]; font_file = argv[++i];
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-fontsize") == 0) { } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
if (i+1<argc) { if (i+1 < argc) {
++i; ++i;
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-writepng") == 0) { } else if (strcmp(&argv[i][1], "-writepng") == 0) {
if (i+1<argc) { if (i+1 < argc) {
save_png=argv[++i]; save_png = argv[++i];
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-force-tape-width") == 0) { } else if (strcmp(&argv[i][1], "-force-tape-width") == 0) {
if (i+1<argc) { if (i+1 < argc) {
forced_tape_width=strtol(argv[++i], NULL, 10); forced_tape_width = strtol(argv[++i], NULL, 10);
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-cutmark") == 0) { } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
continue; /* not done here */ continue; /* not done here */
} else if (strcmp(&argv[i][1], "-chain") == 0) { } else if (strcmp(&argv[i][1], "-chain") == 0) {
chain=true; chain = true;
} else if (strcmp(&argv[i][1], "-debug") == 0) { } else if (strcmp(&argv[i][1], "-debug") == 0) {
debug=true; debug = true;
} else if (strcmp(&argv[i][1], "-info") == 0) { } else if (strcmp(&argv[i][1], "-info") == 0) {
continue; /* not done here */ continue; /* not done here */
} else if (strcmp(&argv[i][1], "-copies") == 0) { } else if (strcmp(&argv[i][1], "-copies") == 0) {
if (i+1<argc) { if (i+1 < argc) {
++i; ++i;
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-image") == 0) { } else if (strcmp(&argv[i][1], "-image") == 0) {
if (i+1<argc) { if (i+1 < argc) {
++i; ++i;
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-pad") == 0) { } else if (strcmp(&argv[i][1], "-pad") == 0) {
if (i+1<argc) { if (i+1 < argc) {
++i; ++i;
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-text") == 0) { } else if (strcmp(&argv[i][1], "-text") == 0) {
for (lines=0; (lines < MAX_LINES) && (i < argc); ++lines) { for (lines = 0; (lines < MAX_LINES) && (i < argc); ++lines) {
if ((i+1 >= argc) || (argv[i+1][0] == '-')) { if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
break; break;
} }
@ -531,16 +528,16 @@ int parse_args(int argc, char **argv)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, lines = 0, tape_width, copies=1; int lines = 0, copies = 1, print_width = 0;
char *line[MAX_LINES]; char *line[MAX_LINES];
gdImage *im=NULL; gdImage *im = NULL;
gdImage *out=NULL; gdImage *out = NULL;
ptouch_dev ptdev=NULL; ptouch_dev ptdev = NULL;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
bindtextdomain("ptouch-print", "/usr/share/locale/"); bindtextdomain("ptouch-print", "/usr/share/locale/");
textdomain("ptouch-print"); textdomain("ptouch-print");
i=parse_args(argc, argv); int i = parse_args(argc, argv);
if (i != argc) { if (i != argc) {
usage(argv[0]); usage(argv[0]);
} }
@ -555,23 +552,28 @@ int main(int argc, char *argv[])
printf(_("ptouch_getstatus() failed\n")); printf(_("ptouch_getstatus() failed\n"));
return 1; return 1;
} }
tape_width=ptouch_get_tape_width(ptdev); print_width = ptouch_get_tape_width(ptdev);
} else { int max_print_width = ptouch_get_max_width(ptdev);
tape_width = forced_tape_width; // do not try to print more pixels than printhead has
if (print_width > max_print_width) {
print_width = max_print_width;
}
} else { // --forced_tape_width together with --writepng
print_width = forced_tape_width;
} }
for (i=1; i<argc; ++i) { for (i = 1; i < argc; ++i) {
if (*argv[i] != '-') { if (*argv[i] != '-') {
break; break;
} }
if (strcmp(&argv[i][1], "-font") == 0) { if (strcmp(&argv[i][1], "-font") == 0) {
if (i+1<argc) { if (i+1 < argc) {
font_file=argv[++i]; font_file = argv[++i];
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-fontsize") == 0) { } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
if (i+1<argc) { if (i+1 < argc) {
fontsize=strtol(argv[++i], NULL, 10); fontsize = strtol(argv[++i], NULL, 10);
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
@ -586,7 +588,8 @@ int main(int argc, char *argv[])
i++; i++;
continue; continue;
} else if (strcmp(&argv[i][1], "-info") == 0) { } else if (strcmp(&argv[i][1], "-info") == 0) {
printf(_("maximum printing width for this tape is %ipx\n"), tape_width); printf(_("maximum printing width for this printer is %ldpx\n"), ptouch_get_max_width(ptdev));
printf(_("maximum printing width for this tape is %ldpx\n"), ptouch_get_tape_width(ptdev));
printf("media type = %02x (%s)\n", ptdev->status->media_type, pt_mediatype(ptdev->status->media_type)); printf("media type = %02x (%s)\n", ptdev->status->media_type, pt_mediatype(ptdev->status->media_type));
printf("media width = %d mm\n", ptdev->status->media_width); printf("media width = %d mm\n", ptdev->status->media_width);
printf("tape color = %02x (%s)\n", ptdev->status->tape_color, pt_tapecolor(ptdev->status->tape_color)); printf("tape color = %02x (%s)\n", ptdev->status->tape_color, pt_tapecolor(ptdev->status->tape_color));
@ -597,39 +600,39 @@ int main(int argc, char *argv[])
} }
exit(0); exit(0);
} else if (strcmp(&argv[i][1], "-image") == 0) { } else if (strcmp(&argv[i][1], "-image") == 0) {
if ((im=image_load(argv[++i])) == NULL) { if ((im = image_load(argv[++i])) == NULL) {
printf(_("failed to load image file\n")); printf(_("failed to load image file\n"));
return 1; return 1;
} }
out=img_append(out, im); out = img_append(out, im);
gdImageDestroy(im); gdImageDestroy(im);
im = NULL; im = NULL;
} else if (strcmp(&argv[i][1], "-text") == 0) { } else if (strcmp(&argv[i][1], "-text") == 0) {
for (lines=0; (lines < MAX_LINES) && (i < argc); ++lines) { for (lines = 0; (lines < MAX_LINES) && (i < argc); ++lines) {
if ((i+1 >= argc) || (argv[i+1][0] == '-')) { if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
break; break;
} }
++i; ++i;
line[lines]=argv[i]; line[lines] = argv[i];
} }
if (lines) { if (lines) {
if ((im=render_text(font_file, line, lines, tape_width)) == NULL) { if ((im = render_text(font_file, line, lines, print_width)) == NULL) {
printf(_("could not render text\n")); printf(_("could not render text\n"));
return 1; return 1;
} }
out=img_append(out, im); out = img_append(out, im);
gdImageDestroy(im); gdImageDestroy(im);
im = NULL; im = NULL;
} }
} else if (strcmp(&argv[i][1], "-cutmark") == 0) { } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
im=img_cutmark(tape_width); im = img_cutmark(print_width);
out=img_append(out, im); out = img_append(out, im);
gdImageDestroy(im); gdImageDestroy(im);
im = NULL; im = NULL;
} else if (strcmp(&argv[i][1], "-pad") == 0) { } else if (strcmp(&argv[i][1], "-pad") == 0) {
int length=strtol(argv[++i], NULL, 10); int length=strtol(argv[++i], NULL, 10);
im=img_padding(tape_width, length); im = img_padding(print_width, length);
out=img_append(out, im); out = img_append(out, im);
gdImageDestroy(im); gdImageDestroy(im);
im = NULL; im = NULL;
} else if (strcmp(&argv[i][1], "-chain") == 0) { } else if (strcmp(&argv[i][1], "-chain") == 0) {
@ -646,7 +649,7 @@ int main(int argc, char *argv[])
if (save_png) { if (save_png) {
write_png(out, save_png); write_png(out, save_png);
} else { } else {
for (i=0; i<copies; ++i) { for (i = 0; i < copies; ++i) {
print_img(ptdev, out, chain); print_img(ptdev, out, chain);
if (ptouch_finalize(ptdev, ( chain || (i < copies-1) ) ) != 0) { if (ptouch_finalize(ptdev, ( chain || (i < copies-1) ) ) != 0) {
printf(_("ptouch_finalize(%d) failed\n"), chain); printf(_("ptouch_finalize(%d) failed\n"), chain);

View File

@ -1,4 +1,5 @@
# Enable non-root access for known ptouch printers # Enable non-root access for known ptouch printers
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2004", MODE="0660", TAG+="uaccess"
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2007", MODE="0660", TAG+="uaccess" SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2007", MODE="0660", TAG+="uaccess"
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2011", MODE="0660", TAG+="uaccess" SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2011", MODE="0660", TAG+="uaccess"
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2019", MODE="0660", TAG+="uaccess" SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2019", MODE="0660", TAG+="uaccess"
@ -16,3 +17,4 @@ SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20af", MODE=
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20df", MODE="0660", TAG+="uaccess" SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20df", MODE="0660", TAG+="uaccess"
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20e0", MODE="0660", TAG+="uaccess" SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20e0", MODE="0660", TAG+="uaccess"
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20e1", MODE="0660", TAG+="uaccess" SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "20e1", MODE="0660", TAG+="uaccess"
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2201", MODE="0660", TAG+="uaccess"