]> git.sur5r.net Git - ptouch-print/blob - src/ptouch-print.c
e99e6ae609f383271359ec75fd0e195d832b330a
[ptouch-print] / src / ptouch-print.c
1 /*
2         ptouch-print - Print labels with images or text on a Brother P-Touch
3         
4         Copyright (C) 2015-2017 Dominic Radermacher <blip@mockmoon-cybernetics.ch>
5
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
9         
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.
14         
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
18 */
19
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() */
26 #include <gd.h>
27 #include "config.h"
28 #include "gettext.h"    /* gettext(), ngettext() */
29 #include "ptouch.h"
30
31 #define _(s) gettext(s)
32
33 #define MAX_LINES 4     /* maybe this should depend on tape size */
34
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);
45
46 // char *font_file="/usr/share/fonts/TTF/Ubuntu-M.ttf";
47 // char *font_file="Ubuntu:medium";
48 char *font_file="DejaVuSans";
49 char *save_png=NULL;
50 int verbose=0;
51 int fontsize=0;
52
53 /* --------------------------------------------------------------------
54    -------------------------------------------------------------------- */
55
56 void rasterline_setpixel(uint8_t rasterline[16], int pixel)
57 {
58         rasterline[15-(pixel/8)] |= 1<<(pixel%8);
59         return;
60 }
61
62 void unsupported_printer(ptouch_dev ptdev)
63 {
64         printf(_("your printer unfortunately is not supported by this tool\n"));
65         printf(_("the rasterdata a transferred in some other (unknown) format\n"));
66         exit(1);
67 }
68
69 int print_img(ptouch_dev ptdev, gdImage *im)
70 {
71         int d,i,k,offset,tape_width;
72         uint8_t rasterline[16];
73
74         if ((ptdev->devinfo->flags & FLAG_UNSUP_RASTER) == FLAG_UNSUP_RASTER) {
75                 unsupported_printer(ptdev);
76         }
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);
83                 return -1;
84         }
85         offset=64-(gdImageSY(im)/2);    /* always print centered  */
86         if (ptouch_rasterstart(ptdev) != 0) {
87                 printf(_("ptouch_rasterstart() failed\n"));
88                 return -1;
89         }
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);
95                         }
96                 }
97                 if (ptouch_sendraster(ptdev, rasterline, 16) != 0) {
98                         printf(_("ptouch_send() failed\n"));
99                         return -1;
100                 }
101         }
102         return 0;
103 }
104
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    -------------------------------------------------------------------- */
111
112 gdImage *image_load(const char *file)
113 {
114         const uint8_t png[8]={0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
115         char d[10];
116         FILE *f;
117         gdImage *img=NULL;
118
119         if ((f = fopen(file, "rb")) == NULL) {  /* error cant open file */
120                 return NULL;
121         }
122         if (fread(d, sizeof(d), 1, f) != 1) {
123                 return NULL;
124         }
125         rewind(f);
126         if (memcmp(d, png, 8) == 0) {
127                 img=gdImageCreateFromPng(f);
128         }
129         fclose(f);
130         return img;
131 }
132
133 int write_png(gdImage *im, const char *file)
134 {
135         FILE *f;
136
137         if ((f = fopen(file, "wb")) == NULL) {
138                 printf(_("writing image '%s' failed\n"), file);
139                 return -1;
140         }
141         gdImagePng(im, f);
142         fclose(f);
143         return 0;
144 }
145
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)
151 {
152         int brect[8];
153
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;
161 }
162
163 /* --------------------------------------------------------------------
164         Find out which fontsize we need for a given font to get a
165         specified pixel size
166         NOTE: This does NOT work for some UTF-8 chars like µ
167    -------------------------------------------------------------------- */
168 int find_fontsize(int want_px, char *font, char *text)
169 {
170         int save=0;
171         int brect[8];
172
173         for (int i=4; ; i++) {
174                 if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) {
175                         break;
176                 }
177                 if (brect[1]-brect[5] <= want_px) {
178                         save=i;
179                 } else {
180                         break;
181                 }
182         }
183         if (save == 0) {
184                 return -1;
185         }
186         return save;
187 }
188
189 int needed_width(char *text, char *font, int fsz)
190 {
191         int brect[8];
192
193         if (gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text) != NULL) {
194                 return -1;
195         }
196         return brect[2]-brect[0];
197 }
198
199 gdImage *render_text(char *font, char *line[], int lines, int tape_width)
200 {
201         int brect[8];
202         int i, black, x=0, tmp, fsz=0, ofs;
203         char *p;
204         gdImage *im=NULL;
205
206 //      printf(_("%i lines, font = '%s'\n"), lines, font);
207         if (gdFTUseFontConfig(1) != GD_TRUE) {
208                 printf(_("warning: font config not available\n"));
209         }
210         if (fontsize > 0) {
211                 fsz=fontsize;
212                 printf(_("setting font size=%i\n"), fsz);
213         } else {
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"));
217                                 return NULL;
218                         }
219                         if ((fsz == 0) || (tmp < fsz)) {
220                                 fsz=tmp;
221                         }
222                 }
223                 printf(_("choosing font size=%i\n"), fsz);
224         }
225         for(i=0; i<lines; i++) {
226                 tmp=needed_width(line[i], font_file, fsz);
227                 if (tmp > x) {
228                         x=tmp;
229                 }
230         }
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);
238                 }
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);
244                 }
245         }
246         return im;
247 }
248
249 void usage(char *progname)
250 {
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");
263         exit(1);
264 }
265
266 /* here we don't print anything, but just try to catch syntax errors */
267 int parse_args(int argc, char **argv)
268 {
269         int lines, i;
270
271         for (i=1; i<argc; i++) {
272                 if (*argv[i] != '-') {
273                         break;
274                 }
275                 if (strcmp(&argv[i][1], "-font") == 0) {
276                         if (i+1<argc) {
277                                 font_file=argv[++i];
278                         } else {
279                                 usage(argv[0]);
280                         }
281                 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
282                         if (i+1<argc) {
283                                 i++;
284                         } else {
285                                 usage(argv[0]);
286                         }
287                 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
288                         if (i+1<argc) {
289                                 save_png=argv[++i];
290                         } else {
291                                 usage(argv[0]);
292                         }
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) {
298                         if (i+1<argc) {
299                                 i++;
300                         } else {
301                                 usage(argv[0]);
302                         }
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] == '-')) {
306                                         break;
307                                 }
308                                 i++;
309                         }
310                 } else if (strcmp(&argv[i][1], "-version") == 0) {
311                         printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION);
312                         exit(0);
313                 } else {
314                         usage(argv[0]);
315                 }
316         }
317         return i;
318 }
319
320 int main(int argc, char *argv[])
321 {
322         int i, lines, tape_width;
323         char *line[MAX_LINES];
324         gdImage *im=NULL;
325         ptouch_dev ptdev=NULL;
326
327         setlocale(LC_ALL, "");
328         bindtextdomain(PACKAGE, LOCALEDIR);
329         textdomain(PACKAGE);
330         i=parse_args(argc, argv);
331         if (i != argc) {
332                 usage(argv[0]);
333         }
334         if ((ptouch_open(&ptdev)) < 0) {
335                 return 5;
336         }
337         if (ptouch_init(ptdev) != 0) {
338                 printf(_("ptouch_init() failed\n"));
339         }
340         if (ptouch_getstatus(ptdev) != 0) {
341                 printf(_("ptouch_getstatus() failed\n"));
342                 return 1;
343         }
344         tape_width=ptouch_getmaxwidth(ptdev);
345         for (i=1; i<argc; i++) {
346                 if (*argv[i] != '-') {
347                         break;
348                 }
349                 if (strcmp(&argv[i][1], "-font") == 0) {
350                         if (i+1<argc) {
351                                 font_file=argv[++i];
352                         } else {
353                                 usage(argv[0]);
354                         }
355                 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
356                         if (i+1<argc) {
357                                 fontsize=strtol(argv[++i], NULL, 10);
358                         } else {
359                                 usage(argv[0]);
360                         }
361                 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
362                         if (i+1<argc) {
363                                 save_png=argv[++i];
364                         } else {
365                                 usage(argv[0]);
366                         }
367                 } else if (strcmp(&argv[i][1], "-info") == 0) {
368                         printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
369                         exit(0);
370                 } else if (strcmp(&argv[i][1], "-image") == 0) {
371                         im=image_load(argv[++i]);
372                         if (im != NULL) {
373                                 print_img(ptdev, im);
374                                 gdImageDestroy(im);
375                         }
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] == '-')) {
379                                         break;
380                                 }
381                                 i++;
382                                 line[lines]=argv[i];
383                         }
384                         if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
385                                 printf(_("could not render text\n"));
386                                 return 1;
387                         }
388                         if (save_png != NULL) {
389                                 write_png(im, save_png);
390                         } else {
391                                 print_img(ptdev, im);
392                         }
393                         gdImageDestroy(im);
394                 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
395                         ptouch_cutmark(ptdev);
396                 } else {
397                         usage(argv[0]);
398                 }
399         }
400         if (ptouch_eject(ptdev) != 0) {
401                 printf(_("ptouch_eject() failed\n"));
402                 return -1;
403         }
404         ptouch_close(ptdev);
405         libusb_exit(NULL);
406         return 0;
407 }