]> git.sur5r.net Git - ptouch-print/blob - src/ptouch-print.c
Small bugfix on text rendering for UTF-8
[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         NOTE: This does NOT work for some UTF-8 chars like µ
157    -------------------------------------------------------------------- */
158 int find_fontsize(int want_px, char *font, char *text)
159 {
160         int save=0;
161         int brect[8];
162
163         for (int i=4; ; i++) {
164                 if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) {
165                         break;
166                 }
167                 if (brect[1]-brect[5] <= want_px) {
168                         save=i;
169                 } else {
170                         break;
171                 }
172         }
173         if (save == 0) {
174                 return -1;
175         }
176         return save;
177 }
178
179 int needed_width(char *text, char *font, int fsz)
180 {
181         int brect[8];
182
183         if (gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text) != NULL) {
184                 return -1;
185         }
186         return brect[2]-brect[0];
187 }
188
189 gdImage *render_text(char *font, char *line[], int lines, int tape_width)
190 {
191         int brect[8];
192         int i, black, x=0, tmp, fsz=0, ofs;
193         char *p;
194         gdImage *im=NULL;
195
196 //      printf(_("%i lines, font = '%s'\n"), lines, font);
197         if (gdFTUseFontConfig(1) != GD_TRUE) {
198                 printf(_("warning: font config not available\n"));
199         }
200         if (fontsize > 0) {
201                 fsz=fontsize;
202                 printf(_("setting font size=%i\n"), fsz);
203         } else {
204                 for (i=0; i<lines; i++) {
205                         if ((tmp=find_fontsize(tape_width/lines, font, line[i])) < 0) {
206                                 printf(_("could not estimate needed font size\n"));
207                                 return NULL;
208                         }
209                         if ((fsz == 0) || (tmp < fsz)) {
210                                 fsz=tmp;
211                         }
212                 }
213                 printf(_("choosing font size=%i\n"), fsz);
214         }
215         for(i=0; i<lines; i++) {
216                 tmp=needed_width(line[i], font_file, fsz);
217                 if (tmp > x) {
218                         x=tmp;
219                 }
220         }
221         im=gdImageCreatePalette(x, tape_width);
222         gdImageColorAllocate(im, 255, 255, 255);
223         black=gdImageColorAllocate(im, 0, 0, 0);
224         /* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */
225         for (i=0; i<lines; i++) {
226                 if ((p=gdImageStringFT(NULL, &brect[0], -black, font, fsz, 0.0, 0, 0, line[i])) != NULL) {
227                         printf(_("error in gdImageStringFT: %s\n"), p);
228                 }
229                 tmp=brect[1]-brect[5];
230                 ofs=get_baselineoffset(line[i], font_file, fsz);
231 //              printf("line %i height = %ipx, pos = %i\n", i+1, tmp, i*(tape_width/lines)+tmp-ofs-1);
232                 if ((p=gdImageStringFT(im, &brect[0], -black, font, fsz, 0.0, 0, i*(tape_width/lines)+tmp-ofs-1, line[i])) != NULL) {
233                         printf(_("error in gdImageStringFT: %s\n"), p);
234                 }
235         }
236         return im;
237 }
238
239 void usage(char *progname)
240 {
241         printf("usage: %s [options] <print-command(s)>\n", progname);
242         printf("options:\n");
243         printf("\t--font <file>\t\tuse font <file> or <name>\n");
244         printf("\t--writepng <file>\tinstead of printing, write output to png file\n");
245         printf("\t\t\t\tThis currently works only when using\n\t\t\t\tEXACTLY ONE --text statement\n");
246         printf("print-commands:\n");
247         printf("\t--image <file>\t\tprint the given image which must be a 2 color\n");
248         printf("\t\t\t\t(black/white) png\n");
249         printf("\t--text <text>\t\tPrint 1-4 lines of text.\n");
250         printf("\t\t\t\tIf the text contains spaces, use quotation marks\n\t\t\t\taround it.\n");
251         printf("\t--cutmark\t\tPrint a mark where the tape should be cut\n");
252         printf("\t--fontsize\t\tManually set fontsize\n");
253         exit(1);
254 }
255
256 /* here we don't print anything, but just try to catch syntax errors */
257 int parse_args(int argc, char **argv)
258 {
259         int lines, i;
260
261         for (i=1; i<argc; i++) {
262                 if (*argv[i] != '-') {
263                         break;
264                 }
265                 if (strcmp(&argv[i][1], "-font") == 0) {
266                         if (i+1<argc) {
267                                 font_file=argv[++i];
268                         } else {
269                                 usage(argv[0]);
270                         }
271                 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
272                         if (i+1<argc) {
273                                 i++;
274                         } else {
275                                 usage(argv[0]);
276                         }
277                 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
278                         if (i+1<argc) {
279                                 save_png=argv[++i];
280                         } else {
281                                 usage(argv[0]);
282                         }
283                 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
284                         continue;       /* not done here */
285                 } else if (strcmp(&argv[i][1], "-info") == 0) {
286                         continue;       /* not done here */
287                 } else if (strcmp(&argv[i][1], "-image") == 0) {
288                         if (i+1<argc) {
289                                 i++;
290                         } else {
291                                 usage(argv[0]);
292                         }
293                 } else if (strcmp(&argv[i][1], "-text") == 0) {
294                         for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
295                                 if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
296                                         break;
297                                 }
298                                 i++;
299                         }
300                 } else if (strcmp(&argv[i][1], "-version") == 0) {
301                         printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION);
302                         exit(0);
303                 } else {
304                         usage(argv[0]);
305                 }
306         }
307         return i;
308 }
309
310 int main(int argc, char *argv[])
311 {
312         int i, lines, tape_width;
313         char *line[MAX_LINES];
314         gdImage *im=NULL;
315         ptouch_dev ptdev=NULL;
316
317         setlocale(LC_ALL, "");
318         bindtextdomain(PACKAGE, LOCALEDIR);
319         textdomain(PACKAGE);
320         i=parse_args(argc, argv);
321         if (i != argc) {
322                 usage(argv[0]);
323         }
324         if ((ptouch_open(&ptdev)) < 0) {
325                 return 5;
326         }
327         if (ptouch_init(ptdev) != 0) {
328                 printf(_("ptouch_init() failed\n"));
329         }
330         if (ptouch_getstatus(ptdev) != 0) {
331                 printf(_("ptouch_getstatus() failed\n"));
332                 return 1;
333         }
334         tape_width=ptouch_getmaxwidth(ptdev);
335         for (i=1; i<argc; i++) {
336                 if (*argv[i] != '-') {
337                         break;
338                 }
339                 if (strcmp(&argv[i][1], "-font") == 0) {
340                         if (i+1<argc) {
341                                 font_file=argv[++i];
342                         } else {
343                                 usage(argv[0]);
344                         }
345                 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
346                         if (i+1<argc) {
347                                 fontsize=strtol(argv[++i], NULL, 10);
348                         } else {
349                                 usage(argv[0]);
350                         }
351                 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
352                         if (i+1<argc) {
353                                 save_png=argv[++i];
354                         } else {
355                                 usage(argv[0]);
356                         }
357                 } else if (strcmp(&argv[i][1], "-info") == 0) {
358                         printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
359                         exit(0);
360                 } else if (strcmp(&argv[i][1], "-image") == 0) {
361                         im=image_load(argv[++i]);
362                         if (im != NULL) {
363                                 print_img(ptdev, im);
364                                 gdImageDestroy(im);
365                         }
366                 } else if (strcmp(&argv[i][1], "-text") == 0) {
367                         for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
368                                 if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
369                                         break;
370                                 }
371                                 i++;
372                                 line[lines]=argv[i];
373                         }
374                         if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
375                                 printf(_("could not render text\n"));
376                                 return 1;
377                         }
378                         if (save_png != NULL) {
379                                 write_png(im, save_png);
380                         } else {
381                                 print_img(ptdev, im);
382                         }
383                         gdImageDestroy(im);
384                 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
385                         ptouch_cutmark(ptdev);
386                 } else {
387                         usage(argv[0]);
388                 }
389         }
390         if (ptouch_eject(ptdev) != 0) {
391                 printf(_("ptouch_eject() failed\n"));
392                 return -1;
393         }
394         ptouch_close(ptdev);
395         libusb_exit(NULL);
396         return 0;
397 }