]> git.sur5r.net Git - ptouch-print/blob - src/ptouch-print.c
ead7c2bfbc4c6a18698a5acbc3a5961755b3cce0
[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 Dominic Radermacher <dominic.radermacher@gmail.com>
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 int print_img(ptouch_dev ptdev, gdImage *im)
63 {
64         int d,i,k,offset,tape_width;
65         uint8_t rasterline[16];
66
67         tape_width=ptouch_getmaxwidth(ptdev);
68         /* find out whether color 0 or color 1 is darker */
69         d=(gdImageRed(im,1)+gdImageGreen(im,1)+gdImageBlue(im,1) < gdImageRed(im,0)+gdImageGreen(im,0)+gdImageBlue(im,0))?1:0;
70         if (gdImageSY(im) > tape_width) {
71                 printf(_("image is too large (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im));
72                 printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
73                 return -1;
74         }
75         offset=64-(gdImageSY(im)/2);    /* always print centered  */
76         if (ptouch_rasterstart(ptdev) != 0) {
77                 printf(_("ptouch_rasterstart() failed\n"));
78                 return -1;
79         }
80         for (k=0; k<gdImageSX(im); k+=1) {
81                 memset(rasterline, 0, sizeof(rasterline));
82                 for (i=0; i<gdImageSY(im); i+=1) {
83                         if (gdImageGetPixel(im, k, gdImageSY(im)-1-i) == d) {
84                                 rasterline_setpixel(rasterline, offset+i);
85                         }
86                 }
87                 if (ptouch_sendraster(ptdev, rasterline, 16) != 0) {
88                         printf(_("ptouch_send() failed\n"));
89                         return -1;
90                 }
91         }
92         return 0;
93 }
94
95 /* --------------------------------------------------------------------
96         Function        image_load()
97         Description     detect the type of a image and try to load it
98         Last update     2005-10-16
99         Status          Working, should add debug info
100    -------------------------------------------------------------------- */
101
102 gdImage *image_load(const char *file)
103 {
104         const uint8_t png[8]={0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
105         char d[10];
106         FILE *f;
107         gdImage *img=NULL;
108
109         if ((f = fopen(file, "rb")) == NULL) {  /* error cant open file */
110                 return NULL;
111         }
112         if (fread(d, sizeof(d), 1, f) != 1) {
113                 return NULL;
114         }
115         rewind(f);
116         if (memcmp(d, png, 8) == 0) {
117                 img=gdImageCreateFromPng(f);
118         }
119         fclose(f);
120         return img;
121 }
122
123 int write_png(gdImage *im, const char *file)
124 {
125         FILE *f;
126
127         if ((f = fopen(file, "wb")) == NULL) {
128                 printf(_("writing image '%s' failed\n"), file);
129                 return -1;
130         }
131         gdImagePng(im, f);
132         fclose(f);
133         return 0;
134 }
135
136 /* --------------------------------------------------------------------
137         Find out the difference in pixels between a "normal" char and one
138         that goes below the font baseline
139    -------------------------------------------------------------------- */
140 int get_baselineoffset(char *text, char *font, int fsz)
141 {
142         int brect[8];
143
144         if (strpbrk(text, "QgjpqyQ") == NULL) { /* if we have none of these */
145                 return 0;               /* we don't need an baseline offset */
146         }                               /* else we need to calculate it */
147         gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "o");
148         int tmp=brect[1]-brect[5];
149         gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "g");
150         return (brect[1]-brect[5])-tmp;
151 }
152
153 /* --------------------------------------------------------------------
154         Find out which fontsize we need for a given font to get a
155         specified pixel size
156    -------------------------------------------------------------------- */
157 int find_fontsize(int want_px, char *font, char *text)
158 {
159         int save=0;
160         int brect[8];
161
162         for (int i=4; ; i++) {
163                 if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) {
164                         break;
165                 }
166                 if (brect[1]-brect[5] <= want_px) {
167                         save=i;
168                 } else {
169                         break;
170                 }
171         }
172         if (save == 0) {
173                 return -1;
174         }
175         return save;
176 }
177
178 int needed_width(char *text, char *font, int fsz)
179 {
180         int brect[8];
181
182         if (gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text) != NULL) {
183                 return -1;
184         }
185         return brect[2]-brect[0];
186 }
187
188 gdImage *render_text(char *font, char *line[], int lines, int tape_width)
189 {
190         int brect[8];
191         int i, black, x=0, tmp, fsz=0, ofs;
192         char *p;
193         gdImage *im=NULL;
194
195 //      printf(_("%i lines, font = '%s'\n"), lines, font);
196         if (gdFTUseFontConfig(1) != GD_TRUE) {
197                 printf(_("warning: font config not available\n"));
198         }
199         if (fontsize > 0) {
200                 fsz=fontsize;
201                 printf(_("setting font size=%i\n"), fsz);
202         } else {
203                 for (i=0; i<lines; i++) {
204                         if ((tmp=find_fontsize(tape_width/lines, font, line[i])) < 0) {
205                                 printf(_("could not estimate needed font size\n"));
206                                 return NULL;
207                         }
208                         if ((fsz == 0) || (tmp < fsz)) {
209                                 fsz=tmp;
210                         }
211                 }
212                 printf(_("choosing font size=%i\n"), fsz);
213         }
214         for(i=0; i<lines; i++) {
215                 tmp=needed_width(line[i], font_file, fsz);
216                 if (tmp > x) {
217                         x=tmp;
218                 }
219         }
220         im=gdImageCreatePalette(x, tape_width);
221         gdImageColorAllocate(im, 255, 255, 255);
222         black=gdImageColorAllocate(im, 0, 0, 0);
223         /* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */
224         for (i=0; i<lines; i++) {
225                 if ((p=gdImageStringFT(NULL, &brect[0], -black, font, fsz, 0.0, 0, 0, line[i])) != NULL) {
226                         printf(_("error in gdImageStringFT: %s\n"), p);
227                 }
228                 tmp=brect[1]-brect[5];
229                 ofs=get_baselineoffset(line[i], font_file, fsz);
230 //              printf("line %i height = %ipx, pos = %i\n", i+1, tmp, i*(tape_width/lines)+tmp-ofs-1);
231                 if ((p=gdImageStringFT(im, &brect[0], -black, font, fsz, 0.0, 0, i*(tape_width/lines)+tmp-ofs-1, line[i])) != NULL) {
232                         printf(_("error in gdImageStringFT: %s\n"), p);
233                 }
234         }
235         return im;
236 }
237
238 void usage(char *progname)
239 {
240         printf("usage: %s [options] <print-command(s)>\n", progname);
241         printf("options:\n");
242         printf("\t--font <file>\t\tuse font <file> or <name>\n");
243         printf("\t--writepng <file>\tinstead of printing, write output to png file\n");
244         printf("\t\t\t\tThis currently works only when using\n\t\t\t\tEXACTLY ONE --text statement\n");
245         printf("print-commands:\n");
246         printf("\t--image <file>\t\tprint the given image which must be a 2 color\n");
247         printf("\t\t\t\t(black/white) png\n");
248         printf("\t--text <text>\t\tPrint 1-4 lines of text.\n");
249         printf("\t\t\t\tIf the text contains spaces, use quotation marks\n\t\t\t\taround it.\n");
250         printf("\t--cutmark\t\tPrint a mark where the tape should be cut\n");
251         printf("\t--fontsize\t\tManually set fontsize\n");
252         exit(1);
253 }
254
255 /* here we don't print anything, but just try to catch syntax errors */
256 int parse_args(int argc, char **argv)
257 {
258         int lines, i;
259
260         for (i=1; i<argc; i++) {
261                 if (*argv[i] != '-') {
262                         break;
263                 }
264                 if (strcmp(&argv[i][1], "-font") == 0) {
265                         if (i+1<argc) {
266                                 font_file=argv[++i];
267                         } else {
268                                 usage(argv[0]);
269                         }
270                 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
271                         if (i+1<argc) {
272                                 i++;
273                         } else {
274                                 usage(argv[0]);
275                         }
276                 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
277                         if (i+1<argc) {
278                                 save_png=argv[++i];
279                         } else {
280                                 usage(argv[0]);
281                         }
282                 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
283                         continue;       /* not done here */
284                 } else if (strcmp(&argv[i][1], "-info") == 0) {
285                         continue;       /* not done here */
286                 } else if (strcmp(&argv[i][1], "-image") == 0) {
287                         if (i+1<argc) {
288                                 i++;
289                         } else {
290                                 usage(argv[0]);
291                         }
292                 } else if (strcmp(&argv[i][1], "-text") == 0) {
293                         for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
294                                 if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
295                                         break;
296                                 }
297                                 i++;
298                         }
299                 } else if (strcmp(&argv[i][1], "-version") == 0) {
300                         printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION);
301                         exit(0);
302                 } else {
303                         usage(argv[0]);
304                 }
305         }
306         return i;
307 }
308
309 int main(int argc, char *argv[])
310 {
311         int i, lines, tape_width;
312         char *line[MAX_LINES];
313         gdImage *im=NULL;
314         ptouch_dev ptdev=NULL;
315
316         setlocale(LC_ALL, "");
317         bindtextdomain(PACKAGE, LOCALEDIR);
318         textdomain(PACKAGE);
319         i=parse_args(argc, argv);
320         if (i != argc) {
321                 usage(argv[0]);
322         }
323         if ((ptouch_open(&ptdev)) < 0) {
324                 return 5;
325         }
326         if (ptouch_init(ptdev) != 0) {
327                 printf(_("ptouch_init() failed\n"));
328         }
329         if (ptouch_getstatus(ptdev) != 0) {
330                 printf(_("ptouch_getstatus() failed\n"));
331                 return 1;
332         }
333         tape_width=ptouch_getmaxwidth(ptdev);
334         for (i=1; i<argc; i++) {
335                 if (*argv[i] != '-') {
336                         break;
337                 }
338                 if (strcmp(&argv[i][1], "-font") == 0) {
339                         if (i+1<argc) {
340                                 font_file=argv[++i];
341                         } else {
342                                 usage(argv[0]);
343                         }
344                 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
345                         if (i+1<argc) {
346                                 fontsize=strtol(argv[++i], NULL, 10);
347                         } else {
348                                 usage(argv[0]);
349                         }
350                 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
351                         if (i+1<argc) {
352                                 save_png=argv[++i];
353                         } else {
354                                 usage(argv[0]);
355                         }
356                 } else if (strcmp(&argv[i][1], "-info") == 0) {
357                         printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
358                         exit(0);
359                 } else if (strcmp(&argv[i][1], "-image") == 0) {
360                         im=image_load(argv[++i]);
361                         if (im != NULL) {
362                                 print_img(ptdev, im);
363                                 gdImageDestroy(im);
364                         }
365                 } else if (strcmp(&argv[i][1], "-text") == 0) {
366                         for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
367                                 if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
368                                         break;
369                                 }
370                                 i++;
371                                 line[lines]=argv[i];
372                         }
373                         if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
374                                 printf(_("could not render text\n"));
375                                 return 1;
376                         }
377                         if (save_png != NULL) {
378                                 write_png(im, save_png);
379                         } else {
380                                 print_img(ptdev, im);
381                         }
382                         gdImageDestroy(im);
383                 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
384                         ptouch_cutmark(ptdev);
385                 } else {
386                         usage(argv[0]);
387                 }
388         }
389         if (ptouch_eject(ptdev) != 0) {
390                 printf(_("ptouch_eject() failed\n"));
391                 return -1;
392         }
393         ptouch_close(ptdev);
394         libusb_exit(NULL);
395         return 0;
396 }