mirror of
https://git.familie-radermacher.ch/linux/ptouch-print.git
synced 2025-05-14 07:42:55 +00:00
Compare commits
No commits in common. "master" and "v1.6" have entirely different histories.
@ -1,7 +1,7 @@
|
||||
/*
|
||||
libptouch - functions to help accessing a brother ptouch
|
||||
|
||||
Copyright (C) 2013-2025 Dominic Radermacher <dominic@familie-radermacher.ch>
|
||||
Copyright (C) 2013-2023 Dominic Radermacher <dominic@familie-radermacher.ch>
|
||||
|
||||
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
|
||||
@ -45,10 +45,9 @@ struct _pt_tape_info tape_info[]= {
|
||||
};
|
||||
|
||||
struct _pt_dev_info ptdevs[] = {
|
||||
{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, 0x2011, "PT-2450PC", 128, 180, FLAG_RASTER_PACKBITS},
|
||||
{0x04f9, 0x2019, "PT-1950", 112, 180, FLAG_RASTER_PACKBITS}, /* 180dpi, apparently 112px printhead ?, maximum tape width 18mm - unconfirmed if it works */
|
||||
{0x04f9, 0x2019, "PT-1950", 128, 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, 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
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
ptouch-print - Print labels with images or text on a Brother P-Touch
|
||||
|
||||
Copyright (C) 2015-2025 Dominic Radermacher <dominic@familie-radermacher.ch>
|
||||
Copyright (C) 2015-2024 Dominic Radermacher <dominic@familie-radermacher.ch>
|
||||
|
||||
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
|
||||
@ -44,8 +44,8 @@ int needed_width(char *text, char *font, int fsz);
|
||||
int print_img(ptouch_dev ptdev, gdImage *im, int chain);
|
||||
int write_png(gdImage *im, const char *file);
|
||||
gdImage *img_append(gdImage *in_1, gdImage *in_2);
|
||||
gdImage *img_cutmark(int print_width);
|
||||
gdImage *render_text(char *font, char *line[], int lines, int print_width);
|
||||
gdImage *img_cutmark(int tape_width);
|
||||
gdImage *render_text(char *font, char *line[], int lines, int tape_width);
|
||||
void unsupported_printer(ptouch_dev ptdev);
|
||||
void usage(char *progname);
|
||||
int parse_args(int argc, char **argv);
|
||||
@ -58,7 +58,7 @@ int verbose = 0;
|
||||
int fontsize = 0;
|
||||
bool debug = false;
|
||||
bool chain = false;
|
||||
int forced_print_width = 0;
|
||||
int forced_tape_width = 0;
|
||||
|
||||
/* --------------------------------------------------------------------
|
||||
-------------------------------------------------------------------- */
|
||||
@ -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
|
||||
// if (pixel > ptdev->devinfo->device_max_px) {
|
||||
if ((pixel < 0) || (pixel >= (int)(size*8))) {
|
||||
if (pixel > (int)(size*8)) {
|
||||
return;
|
||||
}
|
||||
rasterline[(size-1)-(pixel/8)] |= (uint8_t)(1<<(pixel%8));
|
||||
@ -76,23 +76,24 @@ void rasterline_setpixel(uint8_t* rasterline, size_t size, int pixel)
|
||||
|
||||
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];
|
||||
|
||||
if (!im) {
|
||||
printf(_("nothing to print\n"));
|
||||
return -1;
|
||||
}
|
||||
int tape_width = ptouch_get_tape_width(ptdev);
|
||||
size_t max_pixels = ptouch_get_max_width(ptdev);
|
||||
tape_width=ptouch_get_tape_width(ptdev);
|
||||
/* find out whether color 0 or color 1 is darker */
|
||||
int d = (gdImageRed(im,1) + gdImageGreen(im,1) + gdImageBlue(im,1) < gdImageRed(im,0) + gdImageGreen(im,0) + gdImageBlue(im,0))?1:0;
|
||||
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) {
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
printf(_("image size (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im));
|
||||
int offset = ((int)max_pixels / 2) - (gdImageSY(im)/2); /* always print centered */
|
||||
//offset=64-(gdImageSY(im)/2); /* always print centered */
|
||||
size_t max_pixels=ptouch_get_max_width(ptdev);
|
||||
offset=((int)max_pixels / 2)-(gdImageSY(im)/2); /* always print centered */
|
||||
printf("max_pixels=%ld, offset=%d\n", max_pixels, offset);
|
||||
if ((ptdev->devinfo->flags & FLAG_RASTER_PACKBITS) == FLAG_RASTER_PACKBITS) {
|
||||
if (debug) {
|
||||
@ -128,14 +129,14 @@ int print_img(ptouch_dev ptdev, gdImage *im, int chain)
|
||||
printf(_("send precut command\n"));
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < gdImageSX(im); ++k) {
|
||||
for (k=0; k<gdImageSX(im); k+=1) {
|
||||
memset(rasterline, 0, sizeof(rasterline));
|
||||
for (int i = 0; i < gdImageSY(im); ++i) {
|
||||
for (i=0; i<gdImageSY(im); i+=1) {
|
||||
if (gdImageGetPixel(im, k, gdImageSY(im)-1-i) == d) {
|
||||
rasterline_setpixel(rasterline, sizeof(rasterline), offset+i);
|
||||
}
|
||||
}
|
||||
if (ptouch_sendraster(ptdev, rasterline, (ptdev->devinfo->max_px / 8)) != 0) {
|
||||
if (ptouch_sendraster(ptdev, rasterline, 16) != 0) {
|
||||
printf(_("ptouch_sendraster() failed\n"));
|
||||
return -1;
|
||||
}
|
||||
@ -200,12 +201,14 @@ int write_png(gdImage *im, const char *file)
|
||||
int get_baselineoffset(char *text, char *font, int fsz)
|
||||
{
|
||||
int brect[8];
|
||||
int o_offset;
|
||||
int text_offset;
|
||||
|
||||
/* NOTE: This assumes that 'o' is always on the baseline */
|
||||
gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "o");
|
||||
int o_offset = brect[1];
|
||||
o_offset=brect[1];
|
||||
gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text);
|
||||
int text_offset = brect[1];
|
||||
text_offset=brect[1];
|
||||
if (debug) {
|
||||
printf(_("debug: o baseline offset - %d\n"), o_offset);
|
||||
printf(_("debug: text baseline offset - %d\n"), text_offset);
|
||||
@ -259,7 +262,7 @@ int offset_x(char *text, char *font, int fsz)
|
||||
return -brect[0];
|
||||
}
|
||||
|
||||
gdImage *render_text(char *font, char *line[], int lines, int print_width)
|
||||
gdImage *render_text(char *font, char *line[], int lines, int tape_width)
|
||||
{
|
||||
int brect[8];
|
||||
int i, black, x=0, tmp=0, fsz=0;
|
||||
@ -277,7 +280,7 @@ gdImage *render_text(char *font, char *line[], int lines, int print_width)
|
||||
printf(_("setting font size=%i\n"), fsz);
|
||||
} else {
|
||||
for (i=0; i<lines; ++i) {
|
||||
if ((tmp = find_fontsize(print_width/lines, font, line[i])) < 0) {
|
||||
if ((tmp=find_fontsize(tape_width/lines, font, line[i])) < 0) {
|
||||
printf(_("could not estimate needed font size\n"));
|
||||
return NULL;
|
||||
}
|
||||
@ -293,7 +296,7 @@ gdImage *render_text(char *font, char *line[], int lines, int print_width)
|
||||
x=tmp;
|
||||
}
|
||||
}
|
||||
im = gdImageCreatePalette(x, print_width);
|
||||
im=gdImageCreatePalette(x, tape_width);
|
||||
gdImageColorAllocate(im, 255, 255, 255);
|
||||
black=gdImageColorAllocate(im, 0, 0, 0);
|
||||
/* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */
|
||||
@ -312,17 +315,17 @@ gdImage *render_text(char *font, char *line[], int lines, int print_width)
|
||||
if (debug) {
|
||||
printf("debug: needed (max) height is %ipx\n", max_height);
|
||||
}
|
||||
if ((max_height * lines) > print_width) {
|
||||
if ((max_height * lines) > tape_width) {
|
||||
printf("Font size %d too large for %d lines\n", fsz, lines);
|
||||
return NULL;
|
||||
}
|
||||
/* calculate unused pixels */
|
||||
int unused_px = print_width - (max_height * lines);
|
||||
int unused_px = tape_width - (max_height * lines);
|
||||
/* now render lines */
|
||||
for (i=0; i<lines; ++i) {
|
||||
int ofs=get_baselineoffset(line[i], font_file, fsz);
|
||||
//int pos = ((i)*(print_width/(lines)))+(max_height)-ofs-1;
|
||||
int pos = ((i)*(print_width/(lines)))+(max_height)-ofs;
|
||||
//int pos=((i)*(tape_width/(lines)))+(max_height)-ofs-1;
|
||||
int pos=((i)*(tape_width/(lines)))+(max_height)-ofs;
|
||||
pos += (unused_px/lines) / 2;
|
||||
if (debug) {
|
||||
printf("debug: line %i pos=%i ofs=%i\n", i+1, pos, ofs);
|
||||
@ -381,12 +384,12 @@ gdImage *img_append(gdImage *in_1, gdImage *in_2)
|
||||
return out;
|
||||
}
|
||||
|
||||
gdImage *img_cutmark(int print_width)
|
||||
gdImage *img_cutmark(int tape_width)
|
||||
{
|
||||
gdImage *out=NULL;
|
||||
int style_dashed[6];
|
||||
|
||||
out = gdImageCreatePalette(9, print_width);
|
||||
out=gdImageCreatePalette(9, tape_width);
|
||||
if (out == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@ -399,18 +402,18 @@ gdImage *img_cutmark(int print_width)
|
||||
style_dashed[4]=black;
|
||||
style_dashed[5]=black;
|
||||
gdImageSetStyle(out, style_dashed, 6);
|
||||
gdImageLine(out, 5, 0, 5, print_width - 1, gdStyled);
|
||||
gdImageLine(out, 5, 0, 5, tape_width-1, gdStyled);
|
||||
return out;
|
||||
}
|
||||
|
||||
gdImage *img_padding(int print_width, int length)
|
||||
gdImage *img_padding(int tape_width, int length)
|
||||
{
|
||||
gdImage *out=NULL;
|
||||
|
||||
if ((length < 1) || (length > 256)) {
|
||||
length=1;
|
||||
}
|
||||
out = gdImageCreatePalette(length, print_width);
|
||||
out=gdImageCreatePalette(length, tape_width);
|
||||
if (out == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@ -473,7 +476,7 @@ int parse_args(int argc, char **argv)
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-force-tape-width") == 0) {
|
||||
if (i+1<argc) {
|
||||
forced_print_width = strtol(argv[++i], NULL, 10);
|
||||
forced_tape_width=strtol(argv[++i], NULL, 10);
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
@ -520,15 +523,15 @@ int parse_args(int argc, char **argv)
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
if (forced_print_width && !save_png) {
|
||||
forced_print_width = 0;
|
||||
if (forced_tape_width && !save_png) {
|
||||
forced_tape_width = 0;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i, lines = 0, copies = 1, print_width = 0;
|
||||
int i, lines = 0, tape_width, copies=1;
|
||||
char *line[MAX_LINES];
|
||||
gdImage *im=NULL;
|
||||
gdImage *out=NULL;
|
||||
@ -541,9 +544,7 @@ int main(int argc, char *argv[])
|
||||
if (i != argc) {
|
||||
usage(argv[0]);
|
||||
}
|
||||
int tape_width = ptouch_get_tape_width(ptdev);
|
||||
int max_print_width = ptouch_get_max_width(ptdev);
|
||||
if (!forced_print_width) {
|
||||
if (!forced_tape_width) {
|
||||
if ((ptouch_open(&ptdev)) < 0) {
|
||||
return 5;
|
||||
}
|
||||
@ -554,13 +555,9 @@ int main(int argc, char *argv[])
|
||||
printf(_("ptouch_getstatus() failed\n"));
|
||||
return 1;
|
||||
}
|
||||
print_width = tape_width;
|
||||
tape_width=ptouch_get_tape_width(ptdev);
|
||||
} else {
|
||||
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;
|
||||
tape_width = forced_tape_width;
|
||||
}
|
||||
for (i=1; i<argc; ++i) {
|
||||
if (*argv[i] != '-') {
|
||||
@ -579,7 +576,7 @@ int main(int argc, char *argv[])
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-force-tape-width") == 0) {
|
||||
if (forced_print_width && save_png) {
|
||||
if (forced_tape_width && save_png) {
|
||||
++i;
|
||||
continue;
|
||||
} else {
|
||||
@ -589,7 +586,6 @@ int main(int argc, char *argv[])
|
||||
i++;
|
||||
continue;
|
||||
} 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("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);
|
||||
@ -617,7 +613,7 @@ int main(int argc, char *argv[])
|
||||
line[lines]=argv[i];
|
||||
}
|
||||
if (lines) {
|
||||
if ((im = render_text(font_file, line, lines, print_width)) == NULL) {
|
||||
if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
|
||||
printf(_("could not render text\n"));
|
||||
return 1;
|
||||
}
|
||||
@ -626,13 +622,13 @@ int main(int argc, char *argv[])
|
||||
im = NULL;
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-cutmark") == 0) {
|
||||
im = img_cutmark(print_width);
|
||||
im=img_cutmark(tape_width);
|
||||
out=img_append(out, im);
|
||||
gdImageDestroy(im);
|
||||
im = NULL;
|
||||
} else if (strcmp(&argv[i][1], "-pad") == 0) {
|
||||
int length=strtol(argv[++i], NULL, 10);
|
||||
im = img_padding(print_width, length);
|
||||
im=img_padding(tape_width, length);
|
||||
out=img_append(out, im);
|
||||
gdImageDestroy(im);
|
||||
im = NULL;
|
||||
@ -663,7 +659,7 @@ int main(int argc, char *argv[])
|
||||
if (im != NULL) {
|
||||
gdImageDestroy(im);
|
||||
}
|
||||
if (!forced_print_width) {
|
||||
if (!forced_tape_width) {
|
||||
ptouch_close(ptdev);
|
||||
}
|
||||
libusb_exit(NULL);
|
||||
|
@ -1,5 +1,4 @@
|
||||
# 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} == "2011", MODE="0660", TAG+="uaccess"
|
||||
SUBSYSTEM == "usb", ATTRS{idVendor} == "04f9", ATTRS{idProduct} == "2019", MODE="0660", TAG+="uaccess"
|
||||
|
Loading…
Reference in New Issue
Block a user