2 ptouch-print - Print labels with images or text on a Brother P-Touch
4 Copyright (C) 2015-2017 Dominic Radermacher <blip@mockmoon-cybernetics.ch>
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License version 3 as
8 published by the Free Software Foundation
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdio.h> /* printf() */
21 #include <stdlib.h> /* exit(), malloc() */
22 #include <string.h> /* strcmp(), memcmp() */
23 #include <sys/types.h> /* open() */
24 #include <sys/stat.h> /* open() */
25 #include <fcntl.h> /* open() */
28 #include "gettext.h" /* gettext(), ngettext() */
31 #define _(s) gettext(s)
33 #define MAX_LINES 4 /* maybe this should depend on tape size */
35 gdImage *image_load(const char *file);
36 void rasterline_setpixel(uint8_t rasterline[16], int pixel);
37 int get_baselineoffset(char *text, char *font, int fsz);
38 int find_fontsize(int want_px, char *font, char *text);
39 int needed_width(char *text, char *font, int fsz);
40 int print_img(ptouch_dev ptdev, gdImage *im);
41 int write_png(gdImage *im, const char *file);
42 gdImage *render_text(char *font, char *line[], int lines, int tape_width);
43 void usage(char *progname);
44 int parse_args(int argc, char **argv);
46 // char *font_file="/usr/share/fonts/TTF/Ubuntu-M.ttf";
47 // char *font_file="Ubuntu:medium";
48 char *font_file="DejaVuSans";
53 /* --------------------------------------------------------------------
54 -------------------------------------------------------------------- */
56 void rasterline_setpixel(uint8_t rasterline[16], int pixel)
58 rasterline[15-(pixel/8)] |= 1<<(pixel%8);
62 void unsupported_printer(ptouch_dev ptdev)
64 printf(_("your printer unfortunately is not supported by this tool\n"));
65 printf(_("the rasterdata a transferred in some other (unknown) format\n"));
69 int print_img(ptouch_dev ptdev, gdImage *im)
71 int d,i,k,offset,tape_width;
72 uint8_t rasterline[16];
74 if ((ptdev->devinfo->flags & FLAG_UNSUP_RASTER) == FLAG_UNSUP_RASTER) {
75 unsupported_printer(ptdev);
77 tape_width=ptouch_getmaxwidth(ptdev);
78 /* find out whether color 0 or color 1 is darker */
79 d=(gdImageRed(im,1)+gdImageGreen(im,1)+gdImageBlue(im,1) < gdImageRed(im,0)+gdImageGreen(im,0)+gdImageBlue(im,0))?1:0;
80 if (gdImageSY(im) > tape_width) {
81 printf(_("image is too large (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im));
82 printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
85 offset=64-(gdImageSY(im)/2); /* always print centered */
86 if (ptouch_rasterstart(ptdev) != 0) {
87 printf(_("ptouch_rasterstart() failed\n"));
90 for (k=0; k<gdImageSX(im); k+=1) {
91 memset(rasterline, 0, sizeof(rasterline));
92 for (i=0; i<gdImageSY(im); i+=1) {
93 if (gdImageGetPixel(im, k, gdImageSY(im)-1-i) == d) {
94 rasterline_setpixel(rasterline, offset+i);
97 if (ptouch_sendraster(ptdev, rasterline, 16) != 0) {
98 printf(_("ptouch_send() failed\n"));
105 /* --------------------------------------------------------------------
106 Function image_load()
107 Description detect the type of a image and try to load it
108 Last update 2005-10-16
109 Status Working, should add debug info
110 -------------------------------------------------------------------- */
112 gdImage *image_load(const char *file)
114 const uint8_t png[8]={0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
119 if ((f = fopen(file, "rb")) == NULL) { /* error cant open file */
122 if (fread(d, sizeof(d), 1, f) != 1) {
126 if (memcmp(d, png, 8) == 0) {
127 img=gdImageCreateFromPng(f);
133 int write_png(gdImage *im, const char *file)
137 if ((f = fopen(file, "wb")) == NULL) {
138 printf(_("writing image '%s' failed\n"), file);
146 /* --------------------------------------------------------------------
147 Find out the difference in pixels between a "normal" char and one
148 that goes below the font baseline
149 -------------------------------------------------------------------- */
150 int get_baselineoffset(char *text, char *font, int fsz)
154 if (strpbrk(text, "QgjpqyQµ") == NULL) { /* if we have none of these */
155 return 0; /* we don't need an baseline offset */
156 } /* else we need to calculate it */
157 gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "o");
158 int tmp=brect[1]-brect[5];
159 gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "g");
160 return (brect[1]-brect[5])-tmp;
163 /* --------------------------------------------------------------------
164 Find out which fontsize we need for a given font to get a
166 NOTE: This does NOT work for some UTF-8 chars like µ
167 -------------------------------------------------------------------- */
168 int find_fontsize(int want_px, char *font, char *text)
173 for (int i=4; ; i++) {
174 if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) {
177 if (brect[1]-brect[5] <= want_px) {
189 int needed_width(char *text, char *font, int fsz)
193 if (gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text) != NULL) {
196 return brect[2]-brect[0];
199 gdImage *render_text(char *font, char *line[], int lines, int tape_width)
202 int i, black, x=0, tmp, fsz=0, ofs;
206 // printf(_("%i lines, font = '%s'\n"), lines, font);
207 if (gdFTUseFontConfig(1) != GD_TRUE) {
208 printf(_("warning: font config not available\n"));
212 printf(_("setting font size=%i\n"), fsz);
214 for (i=0; i<lines; i++) {
215 if ((tmp=find_fontsize(tape_width/lines, font, line[i])) < 0) {
216 printf(_("could not estimate needed font size\n"));
219 if ((fsz == 0) || (tmp < fsz)) {
223 printf(_("choosing font size=%i\n"), fsz);
225 for(i=0; i<lines; i++) {
226 tmp=needed_width(line[i], font_file, fsz);
231 im=gdImageCreatePalette(x, tape_width);
232 gdImageColorAllocate(im, 255, 255, 255);
233 black=gdImageColorAllocate(im, 0, 0, 0);
234 /* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */
235 for (i=0; i<lines; i++) {
236 if ((p=gdImageStringFT(NULL, &brect[0], -black, font, fsz, 0.0, 0, 0, line[i])) != NULL) {
237 printf(_("error in gdImageStringFT: %s\n"), p);
239 tmp=brect[1]-brect[5];
240 ofs=get_baselineoffset(line[i], font_file, fsz);
241 // printf("line %i height = %ipx, pos = %i\n", i+1, tmp, i*(tape_width/lines)+tmp-ofs-1);
242 if ((p=gdImageStringFT(im, &brect[0], -black, font, fsz, 0.0, 0, i*(tape_width/lines)+tmp-ofs-1, line[i])) != NULL) {
243 printf(_("error in gdImageStringFT: %s\n"), p);
249 void usage(char *progname)
251 printf("usage: %s [options] <print-command(s)>\n", progname);
252 printf("options:\n");
253 printf("\t--font <file>\t\tuse font <file> or <name>\n");
254 printf("\t--writepng <file>\tinstead of printing, write output to png file\n");
255 printf("\t\t\t\tThis currently works only when using\n\t\t\t\tEXACTLY ONE --text statement\n");
256 printf("print-commands:\n");
257 printf("\t--image <file>\t\tprint the given image which must be a 2 color\n");
258 printf("\t\t\t\t(black/white) png\n");
259 printf("\t--text <text>\t\tPrint 1-4 lines of text.\n");
260 printf("\t\t\t\tIf the text contains spaces, use quotation marks\n\t\t\t\taround it.\n");
261 printf("\t--cutmark\t\tPrint a mark where the tape should be cut\n");
262 printf("\t--fontsize\t\tManually set fontsize\n");
266 /* here we don't print anything, but just try to catch syntax errors */
267 int parse_args(int argc, char **argv)
271 for (i=1; i<argc; i++) {
272 if (*argv[i] != '-') {
275 if (strcmp(&argv[i][1], "-font") == 0) {
281 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
287 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
293 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
294 continue; /* not done here */
295 } else if (strcmp(&argv[i][1], "-info") == 0) {
296 continue; /* not done here */
297 } else if (strcmp(&argv[i][1], "-image") == 0) {
303 } else if (strcmp(&argv[i][1], "-text") == 0) {
304 for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
305 if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
310 } else if (strcmp(&argv[i][1], "-version") == 0) {
311 printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION);
320 int main(int argc, char *argv[])
322 int i, lines, tape_width;
323 char *line[MAX_LINES];
325 ptouch_dev ptdev=NULL;
327 setlocale(LC_ALL, "");
328 bindtextdomain(PACKAGE, LOCALEDIR);
330 i=parse_args(argc, argv);
334 if ((ptouch_open(&ptdev)) < 0) {
337 if (ptouch_init(ptdev) != 0) {
338 printf(_("ptouch_init() failed\n"));
340 if (ptouch_getstatus(ptdev) != 0) {
341 printf(_("ptouch_getstatus() failed\n"));
344 tape_width=ptouch_getmaxwidth(ptdev);
345 for (i=1; i<argc; i++) {
346 if (*argv[i] != '-') {
349 if (strcmp(&argv[i][1], "-font") == 0) {
355 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
357 fontsize=strtol(argv[++i], NULL, 10);
361 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
367 } else if (strcmp(&argv[i][1], "-info") == 0) {
368 printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
370 } else if (strcmp(&argv[i][1], "-image") == 0) {
371 im=image_load(argv[++i]);
373 print_img(ptdev, im);
376 } else if (strcmp(&argv[i][1], "-text") == 0) {
377 for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
378 if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
384 if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
385 printf(_("could not render text\n"));
388 if (save_png != NULL) {
389 write_png(im, save_png);
391 print_img(ptdev, im);
394 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
395 ptouch_cutmark(ptdev);
400 if (!save_png && ptouch_eject(ptdev) != 0) {
401 printf(_("ptouch_eject() failed\n"));