| 375 | | |
| | 375 | Printing from application code. |
| | 376 | |
| | 377 | {{{ |
| | 378 | #include <stdio.h> |
| | 379 | #include <cairo.h> |
| | 380 | #include <cairo-ps.h> |
| | 381 | #include <cups/cups.h> |
| | 382 | |
| | 383 | #include "cairobmp.h" |
| | 384 | |
| | 385 | // A4 width, height in points, from GhostView manual: |
| | 386 | // http://www.gnu.org/software/gv/manual/html_node/Paper-Keywords-and-paper-size-in-points.html |
| | 387 | // #define WIDTH 595 |
| | 388 | // #define HEIGHT 842 |
| | 389 | // #define WIDTH 320 |
| | 390 | #define WIDTH 480 |
| | 391 | #define HEIGHT 500 |
| | 392 | |
| | 393 | |
| | 394 | int main(int argc, char **argv) { |
| | 395 | |
| | 396 | int n; |
| | 397 | |
| | 398 | char tmpFilepath[] = "/tmp/tktprtXXXXXX"; |
| | 399 | |
| | 400 | // mkstemp(tmpFilepath); |
| | 401 | strcpy(tmpFilepath, "/tmp/tkt.bmp"); // TODO Easier name just for testing |
| | 402 | printf("tmpFilename [%s]\n", tmpFilepath); |
| | 403 | |
| | 404 | // cairo_surface_t* surface = cairo_ps_surface_create(tmpfilename, WIDTH, HEIGHT); |
| | 405 | cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_A1, WIDTH, HEIGHT); |
| | 406 | printf("after surface create %d\n", cairo_surface_get_type(surface)); |
| | 407 | cairo_t *context = cairo_create(surface); |
| | 408 | |
| | 409 | LoadBMPToSurface(/*"/usr/images/bird.bmp"*/"/usr/images/cardinal_logo.bmp", surface, 0, 0); |
| | 410 | |
| | 411 | // draw some text |
| | 412 | cairo_select_font_face(context, "Arial Black", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| | 413 | cairo_set_font_size(context, 30); |
| | 414 | int x = 20; |
| | 415 | int y = 140; |
| | 416 | cairo_move_to(context, x, y); |
| | 417 | cairo_show_text(context, "Item 1"); // the text we got as a parameter |
| | 418 | y += 30; |
| | 419 | cairo_move_to(context, x, y); |
| | 420 | cairo_show_text(context, "Item 2"); // the text we got as a parameter |
| | 421 | y += 30; |
| | 422 | cairo_move_to(context, x, y); |
| | 423 | cairo_show_text(context, "Item 3"); // the text we got as a parameter |
| | 424 | |
| | 425 | // draw a dotted box |
| | 426 | const double pattern[] = { 15.0, 10.0 }; |
| | 427 | cairo_set_dash(context, pattern, 2, 0); |
| | 428 | cairo_set_line_width(context, 5); |
| | 429 | cairo_rectangle(context, 5, 105, /*280*/ 400, 300); |
| | 430 | cairo_stroke(context); |
| | 431 | |
| | 432 | // finish up |
| | 433 | cairo_show_page(context); |
| | 434 | cairo_destroy(context); |
| | 435 | cairo_surface_flush(surface); |
| | 436 | |
| | 437 | CairoSurfaceToBMP(surface, tmpFilepath); |
| | 438 | |
| | 439 | cairo_surface_destroy(surface); |
| | 440 | |
| | 441 | const char *printerName = cupsGetDefault(); |
| | 442 | printf("printer [%s]\n", printerName); |
| | 443 | |
| | 444 | // cupsGetDefault is returning NULL for some reason, set printerName manually |
| | 445 | printerName = "STARSK1_311"; |
| | 446 | n = cupsPrintFile(printerName, tmpFilepath, "cairo PS", 0, NULL); |
| | 447 | // return value is job ID |
| | 448 | printf("cupsPrintFile result %d\n", n); |
| | 449 | |
| | 450 | //unlink(tmpFilepath); // TODO don't delete file to allow testing |
| | 451 | |
| | 452 | return 0; |
| | 453 | } |
| | 454 | }}} |
| | 455 | |
| | 456 | |
| | 457 | |