Fri, 25 Sep 2009 10:51:24 +0100
test app now a little more interesting -- prints component box labels
1 /**************************
2 * P-Touch PT-2450DX printer driver
3 *
4 * P. Pemberton, 2009
5 *
6 * Specs:
7 * Printer head is 128 dots, 180dpi, for a total print area of ~18mm vertical
8 * by however long your TZ label tape is.
9 * Printhead size is (128/180)=0.711[.] inches, or 18.0622[.] mm
10 * Each dot is (18.062/128) = 0.1411[.] mm
11 * Printable area is (numdots * 0.1411) mm
12 *
13 * Tape width Margins Printable area
14 * mm dots mm dots mm dots
15 * 6mm 42 1.0mm 7 4mm 28
16 * 9mm 63 1.0mm 7 7mm 49
17 * 12mm 85 2.0mm 14 8mm 57
18 * 18mm 127 3.0mm 21 12mm 85
19 * 24mm 170 3.0mm 128 18mm 128 ***
20 *
21 * 24mm is slightly odd. Because the printhead is only 128 dots (18mm), the
22 * margins are enforced by this, and not the driver software. It is impossible
23 * to print right to the edge of a 24mm label in a PT-2450DX.
24 *
25 **************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <gd.h>
30 #include <gdfonts.h>
31 #include <gdfontl.h>
32 #include "ptouch.h"
34 /****************************************************************************/
36 int main(int argc, char **argv)
37 {
38 pt_Device *dev;
40 // check command line args
41 if (argc < 2) {
42 printf("ERROR: must specify device name\n");
43 return -1;
44 }
46 // Open and initialise the printer
47 dev = pt_Initialise(argv[1]);
49 if (dev == NULL) {
50 printf("Error opening printer device.\n");
51 return -1;
52 }
54 // Get printer status
55 int err;
56 if ((err = pt_GetStatus(dev)) != PT_ERR_SUCCESS) {
57 printf("getstatus error %d\n", err);
58 return -1;
59 }
61 // Figure out image width from DPI
62 // Multiply by 10, that way we can use integer math instead if floating
63 // point for the DPI/width calculations.
64 unsigned long labelLength = ((20.0 * dev->dpiLabel * 10) / 254) - 2;
66 // quick test
67 printf("label of 256 dots = %lf in\n", (256.0 / dev->dpiLabel));
68 printf("label of 256 dots = %lf mm\n", (256.0 / dev->dpiLabel) * 25.4);
69 printf("label of %ld dots = %lf mm\n", labelLength, (((double)labelLength) / dev->dpiLabel) * 25.4);
71 // Create a label
72 gdImagePtr im;
73 int white, black;
74 im = gdImageCreate(labelLength, dev->pixelWidth);
75 white = gdImageColorAllocate(im, 255, 255, 255);
76 black = gdImageColorAllocate(im, 0, 0, 0);
77 /* gdImageString(im, gdFontGetLarge(), 0, 0, "!!123!! Test label !!ABC!!", black);
78 gdImageString(im, gdFontGetLarge(), 10, 10, "!!123!! Test label !!ABC!!", black);
79 gdImageString(im, gdFontGetLarge(), 20, 20, "!!123!! Test label !!ABC!!", black);
80 gdImageString(im, gdFontGetLarge(), 30, 30, "!!123!! Test label !!ABC!!", black);
81 gdImageString(im, gdFontGetLarge(), 40, 40, "!!123!! Test label !!ABC!!", black);
82 */
84 //#define FONT_BLUEHIGHWAY
86 #if defined(FONT_BLUEHIGHWAY)
87 char *font_normal = "./bluehighway/bluehigh.ttf";
88 char *font_bold = "./bluehighway/bluebold.ttf";
89 #else
90 char *font_normal = "./Arial.ttf";
91 char *font_bold = "./Arial_Bold.ttf";
92 #endif
94 char *pn = "SMD-001/01";
95 char *strings[] = {
96 "Surface mount resistor",
97 "0805, \u00BCW (SMD-001/01)",
98 "$33\u03A9",
99 };
101 int curtop = 3;
102 int brect[8];
103 char *fte;
105 gdFTStringExtra extra;
106 extra.flags = gdFTEX_RESOLUTION | gdFTEX_DISABLE_KERNING;
107 extra.hdpi = dev->dpiLabel;
108 extra.vdpi = dev->dpiPrinthead;
110 //// PART NUMBER
111 // calc bounding box
112 fte = gdImageStringFTEx(NULL, &brect[0], 0, font_bold, 9.0, 0.0, 0, 0, pn, &extra);
113 if (fte != NULL) printf("freetype error: %s\n", fte);
115 // draw P/N
116 fte = gdImageStringFTEx(im, NULL, 0-black, font_bold, 9.0, 0.0, 3+abs(brect[6]), curtop+abs(brect[7]), pn, &extra);
117 if (fte != NULL) printf("freetype error: %s\n", fte);
119 // update Y position
120 curtop += abs(brect[7]) + 2;
122 //// draw description
123 for (int i=0; i<(sizeof(strings)/sizeof(strings[0])); i++) {
124 char *str = strings[i];
125 char *font = font_normal;
126 float fontsize = 7.0;
128 if (*str == '$') { font = font_bold; fontsize = 8.0; str++; }
130 fte = gdImageStringFTEx(NULL, &brect[0], 0, font, fontsize, 0.0, 0, 0, str, &extra);
131 if (fte != NULL) printf("freetype error: %s\n", fte);
133 fte = gdImageStringFTEx(im, NULL, 0-black, font, fontsize, 0.0, 3+abs(brect[6]), curtop+abs(brect[7]), str, &extra);
134 if (fte != NULL) printf("freetype error: %s\n", fte);
136 curtop += abs(brect[7]) + 1;
137 }
139 // dump the image (for testing purposes)
140 FILE *fp = fopen("labeldump.png", "wb");
141 gdImagePng(im, fp);
142 fclose(fp);
144 // Set job options
145 pt_SetOption(dev, PT_OPTION_AUTOCUT, 1);
146 pt_SetOption(dev, PT_OPTION_MIRROR, 1);
147 pt_SetOption(dev, PT_OPTION_SEPARATOR, 1);
149 // Print the label
150 printf("Print state code: %d\n", pt_Print(dev, &im, 1));
152 gdImageDestroy(im);
154 // Close the printer device
155 pt_Close(dev);
157 return 0;
158 }