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