| 1 |
#include <cups/cups.h> |
| 2 |
#include <cups/ppd.h> |
| 3 |
#include <string> |
| 4 |
#include <stdio.h> |
| 5 |
#include <map> |
| 6 |
#include <cairo/cairo.h> |
| 7 |
#include <exception> |
| 8 |
#include "CairoUtils.h" |
| 9 |
|
| 10 |
using namespace std; |
| 11 |
|
| 12 |
const char* PNGFileName = "label.png"; |
| 13 |
|
| 14 |
class Error: public exception |
| 15 |
{ |
| 16 |
public: |
| 17 |
Error(const string& Message): exception(), Message_(Message) {} |
| 18 |
virtual ~Error() throw() {} |
| 19 |
virtual const char* what() const throw() { return Message_.c_str(); } |
| 20 |
private: |
| 21 |
string Message_; |
| 22 |
}; |
| 23 |
|
| 24 |
map <string, string> gPaperNames; |
| 25 |
typedef pair<string, string> str_pair; |
| 26 |
|
| 27 |
static void |
| 28 |
FindPapersOptions(ppd_option_t& o) |
| 29 |
{ |
| 30 |
if (!strcmp(o.keyword, "PageSize")) |
| 31 |
{ |
| 32 |
for (int i = 0; i < o.num_choices; ++i) |
| 33 |
{ |
| 34 |
ppd_choice_t& c = o.choices[i]; |
| 35 |
|
| 36 |
gPaperNames.insert(str_pair(c.choice, c.text)); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
FindPapersGroup(ppd_group_t& g) |
| 43 |
{ |
| 44 |
for (int i = 0; i < g.num_options; ++i) |
| 45 |
FindPapersOptions(g.options[i]); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
FindPapers(ppd_group_t* group, int num_groups) |
| 50 |
{ |
| 51 |
for (int i = 0; i < num_groups; ++i) |
| 52 |
FindPapersGroup(group[i]); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
void |
| 57 |
CreateLabelImage(int Width, int Height) |
| 58 |
{ |
| 59 |
CairoSurfacePtr Surface(cairo_image_surface_create(CAIRO_FORMAT_RGB24, Width, Height)); |
| 60 |
if (!*Surface) |
| 61 |
throw Error("Unable to create cairo surface"); |
| 62 |
|
| 63 |
|
| 64 |
CairoPtr c(cairo_create(Surface)); |
| 65 |
if (!*c) |
| 66 |
throw Error("Unable to create cairo_t"); |
| 67 |
|
| 68 |
// setup cairo |
| 69 |
cairo_set_antialias(c, CAIRO_ANTIALIAS_NONE); |
| 70 |
|
| 71 |
// clear image |
| 72 |
cairo_set_source_rgb(c, 1, 1, 1); |
| 73 |
cairo_paint(c); |
| 74 |
|
| 75 |
// draw boundrect |
| 76 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 77 |
cairo_set_line_width(c, 5); |
| 78 |
cairo_rectangle(c, 5, 5, Width - 10, Height - 10); |
| 79 |
cairo_stroke(c); |
| 80 |
|
| 81 |
// load an image from the file |
| 82 |
cairo_save(c); |
| 83 |
CairoSurfacePtr Image(cairo_image_surface_create_from_png("tel.png")); |
| 84 |
if (!*Image) |
| 85 |
throw Error("Unable to load image file"); |
| 86 |
|
| 87 |
cairo_scale(c, 0.5, 0.5); |
| 88 |
cairo_set_source_surface(c, Image, 30, 30); |
| 89 |
cairo_paint(c); |
| 90 |
cairo_restore(c); |
| 91 |
|
| 92 |
// draw text |
| 93 |
cairo_select_font_face(c, "courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); |
| 94 |
cairo_set_font_size(c, 50); |
| 95 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 96 |
cairo_set_line_width(c, 1); |
| 97 |
const char* lines[4] = { |
| 98 |
"Jim Best", |
| 99 |
"Good, Bedder, and Best", |
| 100 |
"900 Park Ave", |
| 101 |
"New York, NY 10021-0231" }; |
| 102 |
|
| 103 |
cairo_text_extents_t te; |
| 104 |
double x = 250; |
| 105 |
double y = 50; |
| 106 |
for(int i = 0; i < 4; ++i) |
| 107 |
{ |
| 108 |
cairo_text_extents(c, lines[i], &te); |
| 109 |
|
| 110 |
x = 1080 - te.width; |
| 111 |
cairo_move_to(c, x, y); |
| 112 |
cairo_show_text(c, lines[i]); |
| 113 |
cairo_stroke(c); |
| 114 |
|
| 115 |
y += te.height + 10; |
| 116 |
} |
| 117 |
|
| 118 |
// draw horiz line |
| 119 |
cairo_set_line_width(c, 5); |
| 120 |
cairo_move_to(c, 30, 200); |
| 121 |
cairo_line_to(c, 1080, 200); |
| 122 |
cairo_stroke(c); |
| 123 |
|
| 124 |
// draw barcode - load from fle also |
| 125 |
cairo_save(c); |
| 126 |
CairoSurfacePtr Barcode(cairo_image_surface_create_from_png("barcode.png")); |
| 127 |
if (!*Barcode) |
| 128 |
throw Error("Unable to load barcode file"); |
| 129 |
|
| 130 |
cairo_set_source_surface(c, Barcode, 30, 230); |
| 131 |
cairo_paint(c); |
| 132 |
cairo_restore(c); |
| 133 |
|
| 134 |
|
| 135 |
// draw address 2 |
| 136 |
cairo_select_font_face(c, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| 137 |
cairo_set_font_size(c, 50); |
| 138 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 139 |
cairo_set_line_width(c, 1); |
| 140 |
const char* lines2[4] = { |
| 141 |
"Dr. Robert J. Block", |
| 142 |
"Alta Mira Hospital", |
| 143 |
"200 Madison Ave", |
| 144 |
"Montgomery, AL 36104-3626" }; |
| 145 |
|
| 146 |
x = 30; |
| 147 |
y = 320; |
| 148 |
for(int i = 0; i < 4; ++i) |
| 149 |
{ |
| 150 |
cairo_text_extents(c, lines2[i], &te); |
| 151 |
|
| 152 |
cairo_move_to(c, x, y); |
| 153 |
cairo_show_text(c, lines2[i]); |
| 154 |
cairo_stroke(c); |
| 155 |
|
| 156 |
y += te.height; |
| 157 |
} |
| 158 |
|
| 159 |
// draw a photo |
| 160 |
cairo_save(c); |
| 161 |
CairoSurfacePtr Photo(cairo_image_surface_create_from_png("photo.png")); |
| 162 |
if (!*Photo) |
| 163 |
throw Error("Unable to load photo file"); |
| 164 |
|
| 165 |
//cairo_scale(c, 0.5, 0.5); |
| 166 |
cairo_set_source_surface(c, Photo, 850, 330); |
| 167 |
cairo_paint(c); |
| 168 |
cairo_restore(c); |
| 169 |
|
| 170 |
// draw small description |
| 171 |
cairo_select_font_face(c, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| 172 |
cairo_set_font_size(c, 20); |
| 173 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 174 |
cairo_set_line_width(c, 1); |
| 175 |
cairo_move_to(c, 30, 640); |
| 176 |
cairo_show_text(c, "Printed on Linux using DYMO CUPS drivers and Cairo graphics library"); |
| 177 |
cairo_stroke(c); |
| 178 |
|
| 179 |
|
| 180 |
// save to file |
| 181 |
if (cairo_surface_write_to_png(Surface, PNGFileName) != CAIRO_STATUS_SUCCESS) |
| 182 |
throw Error("Unable to write to PNG file"); |
| 183 |
} |
| 184 |
|
| 185 |
int main(int argc, char** argv) |
| 186 |
{ |
| 187 |
try |
| 188 |
{ |
| 189 |
if (argc < 2) |
| 190 |
throw Error("Usage: test_label <PrinterName>"); |
| 191 |
|
| 192 |
|
| 193 |
printf("Please Insert '30256 Shipping' paper. Press 'c' to continue, 'a' to abort: "); |
| 194 |
int ch = 0; |
| 195 |
ch = getchar(); |
| 196 |
|
| 197 |
if (ch == 'a') |
| 198 |
return 0; |
| 199 |
|
| 200 |
int Width = 1112; |
| 201 |
int Height = 664; |
| 202 |
|
| 203 |
CreateLabelImage(Width, Height); |
| 204 |
|
| 205 |
int num_options = 0; |
| 206 |
cups_option_t* options = NULL; |
| 207 |
|
| 208 |
num_options = cupsAddOption("PageSize", "w167h288", num_options, &options); |
| 209 |
num_options = cupsAddOption("scaling", "100", num_options, &options); |
| 210 |
//num_options = cupsAddOption("landscape", "yes", num_options, &options); |
| 211 |
num_options = cupsAddOption("orientation-requested", "4", num_options, &options); |
| 212 |
num_options = cupsAddOption("DymoHalftoning", "ErrorDiffusion", num_options, &options); |
| 213 |
num_options = cupsAddOption("DymoPrintQuality", "Graphics", num_options, &options); |
| 214 |
// num_options = cupsAddOption("orientation-requested", "1", num_options, &options); |
| 215 |
|
| 216 |
|
| 217 |
cupsPrintFile(argv[1], PNGFileName, "Test print with Cairo", num_options, options); |
| 218 |
cupsFreeOptions(num_options, options); |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
catch(std::exception& e) |
| 223 |
{ |
| 224 |
fprintf(stderr, e.what()); |
| 225 |
fprintf(stderr, "\n"); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
} |