2 ptouch-print - Print labels with images or text on a Brother P-Touch
4 Copyright (C) 2015 Dominic Radermacher <dominic.radermacher@gmail.com>
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 int print_img(ptouch_dev ptdev, gdImage *im)
64 int d,i,k,offset,tape_width;
65 uint8_t rasterline[16];
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);
75 offset=64-(gdImageSY(im)/2); /* always print centered */
76 if (ptouch_rasterstart(ptdev) != 0) {
77 printf(_("ptouch_rasterstart() failed\n"));
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);
87 if (ptouch_sendraster(ptdev, rasterline, 16) != 0) {
88 printf(_("ptouch_send() failed\n"));
95 /* --------------------------------------------------------------------
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 -------------------------------------------------------------------- */
102 gdImage *image_load(const char *file)
104 const uint8_t png[8]={0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
109 if ((f = fopen(file, "rb")) == NULL) { /* error cant open file */
112 if (fread(d, sizeof(d), 1, f) != 1) {
116 if (memcmp(d, png, 8) == 0) {
117 img=gdImageCreateFromPng(f);
123 int write_png(gdImage *im, const char *file)
127 if ((f = fopen(file, "wb")) == NULL) {
128 printf(_("writing image '%s' failed\n"), file);
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)
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;
153 /* --------------------------------------------------------------------
154 Find out which fontsize we need for a given font to get a
156 NOTE: This does NOT work for some UTF-8 chars like µ
157 -------------------------------------------------------------------- */
158 int find_fontsize(int want_px, char *font, char *text)
163 for (int i=4; ; i++) {
164 if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) {
167 if (brect[1]-brect[5] <= want_px) {
179 int needed_width(char *text, char *font, int fsz)
183 if (gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text) != NULL) {
186 return brect[2]-brect[0];
189 gdImage *render_text(char *font, char *line[], int lines, int tape_width)
192 int i, black, x=0, tmp, fsz=0, ofs;
196 // printf(_("%i lines, font = '%s'\n"), lines, font);
197 if (gdFTUseFontConfig(1) != GD_TRUE) {
198 printf(_("warning: font config not available\n"));
202 printf(_("setting font size=%i\n"), fsz);
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"));
209 if ((fsz == 0) || (tmp < fsz)) {
213 printf(_("choosing font size=%i\n"), fsz);
215 for(i=0; i<lines; i++) {
216 tmp=needed_width(line[i], font_file, fsz);
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);
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);
239 void usage(char *progname)
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");
256 /* here we don't print anything, but just try to catch syntax errors */
257 int parse_args(int argc, char **argv)
261 for (i=1; i<argc; i++) {
262 if (*argv[i] != '-') {
265 if (strcmp(&argv[i][1], "-font") == 0) {
271 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
277 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
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) {
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] == '-')) {
300 } else if (strcmp(&argv[i][1], "-version") == 0) {
301 printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION);
310 int main(int argc, char *argv[])
312 int i, lines, tape_width;
313 char *line[MAX_LINES];
315 ptouch_dev ptdev=NULL;
317 setlocale(LC_ALL, "");
318 bindtextdomain(PACKAGE, LOCALEDIR);
320 i=parse_args(argc, argv);
324 if ((ptouch_open(&ptdev)) < 0) {
327 if (ptouch_init(ptdev) != 0) {
328 printf(_("ptouch_init() failed\n"));
330 if (ptouch_getstatus(ptdev) != 0) {
331 printf(_("ptouch_getstatus() failed\n"));
334 tape_width=ptouch_getmaxwidth(ptdev);
335 for (i=1; i<argc; i++) {
336 if (*argv[i] != '-') {
339 if (strcmp(&argv[i][1], "-font") == 0) {
345 } else if (strcmp(&argv[i][1], "-fontsize") == 0) {
347 fontsize=strtol(argv[++i], NULL, 10);
351 } else if (strcmp(&argv[i][1], "-writepng") == 0) {
357 } else if (strcmp(&argv[i][1], "-info") == 0) {
358 printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
360 } else if (strcmp(&argv[i][1], "-image") == 0) {
361 im=image_load(argv[++i]);
363 print_img(ptdev, im);
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] == '-')) {
374 if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
375 printf(_("could not render text\n"));
378 if (save_png != NULL) {
379 write_png(im, save_png);
381 print_img(ptdev, im);
384 } else if (strcmp(&argv[i][1], "-cutmark") == 0) {
385 ptouch_cutmark(ptdev);
390 if (ptouch_eject(ptdev) != 0) {
391 printf(_("ptouch_eject() failed\n"));