1
0
mirror of https://git.familie-radermacher.ch/linux/ptouch-print.git synced 2025-05-13 15:22:56 +00:00

for printers with less than 128px printhead, make sure we dont print too much pixels

This commit is contained in:
Dominic Radermacher 2025-05-12 18:29:56 +02:00
parent a6c67df2c9
commit 140bf0a6cc

View File

@ -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);
@ -58,7 +58,7 @@ int verbose = 0;
int fontsize = 0; int fontsize = 0;
bool debug = false; bool debug = false;
bool chain = false; bool chain = false;
int forced_tape_width = 0; int forced_print_width = 0;
/* -------------------------------------------------------------------- /* --------------------------------------------------------------------
-------------------------------------------------------------------- */ -------------------------------------------------------------------- */
@ -200,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);
@ -261,7 +259,7 @@ 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;
@ -279,7 +277,7 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width)
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;
} }
@ -295,7 +293,7 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width)
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) */
@ -314,17 +312,17 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width)
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);
@ -383,12 +381,12 @@ 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;
} }
@ -401,18 +399,18 @@ gdImage *img_cutmark(int tape_width)
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;
} }
@ -475,7 +473,7 @@ int parse_args(int argc, char **argv)
} }
} 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_print_width = strtol(argv[++i], NULL, 10);
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
@ -522,15 +520,15 @@ int parse_args(int argc, char **argv)
usage(argv[0]); usage(argv[0]);
} }
} }
if (forced_tape_width && !save_png) { if (forced_print_width && !save_png) {
forced_tape_width = 0; forced_print_width = 0;
} }
return i; return i;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, lines = 0, tape_width, copies=1; int i, 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;
@ -543,7 +541,9 @@ int main(int argc, char *argv[])
if (i != argc) { if (i != argc) {
usage(argv[0]); usage(argv[0]);
} }
if (!forced_tape_width) { int tape_width = ptouch_get_tape_width(ptdev);
int max_print_width = ptouch_get_max_width(ptdev);
if (!forced_print_width) {
if ((ptouch_open(&ptdev)) < 0) { if ((ptouch_open(&ptdev)) < 0) {
return 5; return 5;
} }
@ -554,9 +554,13 @@ 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 = tape_width;
} else { } else {
tape_width = forced_tape_width; print_width = forced_print_width;
}
// do not try to print more pixels than printhead has
if (print_width > max_print_width) {
print_width = max_print_width;
} }
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (*argv[i] != '-') { if (*argv[i] != '-') {
@ -575,7 +579,7 @@ int main(int argc, char *argv[])
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 (forced_tape_width && save_png) { if (forced_print_width && save_png) {
++i; ++i;
continue; continue;
} else { } else {
@ -585,6 +589,7 @@ 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 printer is %ipx\n"), max_print_width);
printf(_("maximum printing width for this tape is %ipx\n"), tape_width); printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
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);
@ -612,7 +617,7 @@ int main(int argc, char *argv[])
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;
} }
@ -621,13 +626,13 @@ int main(int argc, char *argv[])
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;
@ -658,7 +663,7 @@ int main(int argc, char *argv[])
if (im != NULL) { if (im != NULL) {
gdImageDestroy(im); gdImageDestroy(im);
} }
if (!forced_tape_width) { if (!forced_print_width) {
ptouch_close(ptdev); ptouch_close(ptdev);
} }
libusb_exit(NULL); libusb_exit(NULL);