Fri, 25 Sep 2009 10:51:24 +0100
test app now a little more interesting -- prints component box labels
src/main.c | file | annotate | diff | revisions |
1.1 --- a/src/main.c Fri Sep 25 10:50:44 2009 +0100 1.2 +++ b/src/main.c Fri Sep 25 10:51:24 2009 +0100 1.3 @@ -58,17 +58,83 @@ 1.4 return -1; 1.5 } 1.6 1.7 + // Figure out image width from DPI 1.8 + // Multiply by 10, that way we can use integer math instead if floating 1.9 + // point for the DPI/width calculations. 1.10 + unsigned long labelLength = ((20.0 * dev->dpiLabel * 10) / 254) - 2; 1.11 + 1.12 + // quick test 1.13 + printf("label of 256 dots = %lf in\n", (256.0 / dev->dpiLabel)); 1.14 + printf("label of 256 dots = %lf mm\n", (256.0 / dev->dpiLabel) * 25.4); 1.15 + printf("label of %ld dots = %lf mm\n", labelLength, (((double)labelLength) / dev->dpiLabel) * 25.4); 1.16 + 1.17 // Create a label 1.18 gdImagePtr im; 1.19 int white, black; 1.20 - im = gdImageCreate(256, dev->pixelWidth); 1.21 + im = gdImageCreate(labelLength, dev->pixelWidth); 1.22 white = gdImageColorAllocate(im, 255, 255, 255); 1.23 black = gdImageColorAllocate(im, 0, 0, 0); 1.24 - gdImageString(im, gdFontGetLarge(), 0, 0, "!!123!! Test label !!ABC!!", black); 1.25 +/* gdImageString(im, gdFontGetLarge(), 0, 0, "!!123!! Test label !!ABC!!", black); 1.26 gdImageString(im, gdFontGetLarge(), 10, 10, "!!123!! Test label !!ABC!!", black); 1.27 gdImageString(im, gdFontGetLarge(), 20, 20, "!!123!! Test label !!ABC!!", black); 1.28 gdImageString(im, gdFontGetLarge(), 30, 30, "!!123!! Test label !!ABC!!", black); 1.29 gdImageString(im, gdFontGetLarge(), 40, 40, "!!123!! Test label !!ABC!!", black); 1.30 +*/ 1.31 + 1.32 +//#define FONT_BLUEHIGHWAY 1.33 + 1.34 +#if defined(FONT_BLUEHIGHWAY) 1.35 + char *font_normal = "./bluehighway/bluehigh.ttf"; 1.36 + char *font_bold = "./bluehighway/bluebold.ttf"; 1.37 +#else 1.38 + char *font_normal = "./Arial.ttf"; 1.39 + char *font_bold = "./Arial_Bold.ttf"; 1.40 +#endif 1.41 + 1.42 + char *pn = "SMD-001/01"; 1.43 + char *strings[] = { 1.44 + "Surface mount resistor", 1.45 + "0805, \u00BCW (SMD-001/01)", 1.46 + "$33\u03A9", 1.47 + }; 1.48 + 1.49 + int curtop = 3; 1.50 + int brect[8]; 1.51 + char *fte; 1.52 + 1.53 + gdFTStringExtra extra; 1.54 + extra.flags = gdFTEX_RESOLUTION | gdFTEX_DISABLE_KERNING; 1.55 + extra.hdpi = dev->dpiLabel; 1.56 + extra.vdpi = dev->dpiPrinthead; 1.57 + 1.58 + //// PART NUMBER 1.59 + // calc bounding box 1.60 + fte = gdImageStringFTEx(NULL, &brect[0], 0, font_bold, 9.0, 0.0, 0, 0, pn, &extra); 1.61 + if (fte != NULL) printf("freetype error: %s\n", fte); 1.62 + 1.63 + // draw P/N 1.64 + fte = gdImageStringFTEx(im, NULL, 0-black, font_bold, 9.0, 0.0, 3+abs(brect[6]), curtop+abs(brect[7]), pn, &extra); 1.65 + if (fte != NULL) printf("freetype error: %s\n", fte); 1.66 + 1.67 + // update Y position 1.68 + curtop += abs(brect[7]) + 2; 1.69 + 1.70 + //// draw description 1.71 + for (int i=0; i<(sizeof(strings)/sizeof(strings[0])); i++) { 1.72 + char *str = strings[i]; 1.73 + char *font = font_normal; 1.74 + float fontsize = 7.0; 1.75 + 1.76 + if (*str == '$') { font = font_bold; fontsize = 8.0; str++; } 1.77 + 1.78 + fte = gdImageStringFTEx(NULL, &brect[0], 0, font, fontsize, 0.0, 0, 0, str, &extra); 1.79 + if (fte != NULL) printf("freetype error: %s\n", fte); 1.80 + 1.81 + fte = gdImageStringFTEx(im, NULL, 0-black, font, fontsize, 0.0, 3+abs(brect[6]), curtop+abs(brect[7]), str, &extra); 1.82 + if (fte != NULL) printf("freetype error: %s\n", fte); 1.83 + 1.84 + curtop += abs(brect[7]) + 1; 1.85 + } 1.86 1.87 // dump the image (for testing purposes) 1.88 FILE *fp = fopen("labeldump.png", "wb");