| 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 |
|
| 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 |
|
| 25 |
static int |
| 26 |
GetPrinterResolution(ppd_group_t* group, int num_groups) |
| 27 |
{ |
| 28 |
for (int i = 0; i < num_groups; ++i) |
| 29 |
{ |
| 30 |
ppd_group_t g = group[i]; |
| 31 |
for (int j = 0; j < g.num_options; ++j) |
| 32 |
{ |
| 33 |
ppd_option_t o = g.options[j]; |
| 34 |
|
| 35 |
if (!strcmp(o.keyword, "Resolution")) |
| 36 |
{ |
| 37 |
ppd_choice_t c = o.choices[0]; |
| 38 |
|
| 39 |
return atoi(c.choice); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
CreateBoundsImage(int Width, int Height, const string& Text, string& FileName) |
| 49 |
{ |
| 50 |
FileName = "test.png"; |
| 51 |
|
| 52 |
CairoSurfacePtr Surface(cairo_image_surface_create(CAIRO_FORMAT_RGB24, Width, Height)); |
| 53 |
if (!*Surface) |
| 54 |
throw Error("Unable to create cairo surface"); |
| 55 |
|
| 56 |
|
| 57 |
CairoPtr c(cairo_create(Surface)); |
| 58 |
if (!*c) |
| 59 |
throw Error("Unable to create cairo_t"); |
| 60 |
|
| 61 |
//setup Cairo |
| 62 |
cairo_set_antialias(c, CAIRO_ANTIALIAS_NONE); |
| 63 |
|
| 64 |
|
| 65 |
// clear image |
| 66 |
cairo_set_source_rgb(c, 1, 1, 1); |
| 67 |
cairo_paint(c); |
| 68 |
|
| 69 |
// draw rect |
| 70 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 71 |
cairo_set_line_width(c, 3); |
| 72 |
cairo_rectangle(c, 3, 3, Width - 6, Height - 6); |
| 73 |
cairo_move_to(c, 3, 3); |
| 74 |
cairo_line_to(c, Width - 3, Height - 3); |
| 75 |
cairo_move_to(c, 3, Height - 3); |
| 76 |
cairo_line_to(c, Width - 3, 3); |
| 77 |
cairo_stroke(c); |
| 78 |
|
| 79 |
|
| 80 |
// draw text |
| 81 |
cairo_select_font_face(c, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| 82 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 83 |
cairo_set_line_width(c, 1); |
| 84 |
|
| 85 |
cairo_text_extents_t te; |
| 86 |
for(int FontSize = 100; ;FontSize -= 10) |
| 87 |
{ |
| 88 |
cairo_set_font_size(c, FontSize); |
| 89 |
|
| 90 |
cairo_text_extents(c, Text.c_str(), &te); |
| 91 |
|
| 92 |
if (Width > te.width) |
| 93 |
break; |
| 94 |
} |
| 95 |
|
| 96 |
//cairo_move_to(c, 10, 10); |
| 97 |
//cairo_show_text(c, "Hello"); |
| 98 |
cairo_move_to(c, (Width - te.width) / 2, (Height + te.height) / 2); |
| 99 |
cairo_text_path(c, Text.c_str()); |
| 100 |
|
| 101 |
cairo_stroke(c); |
| 102 |
|
| 103 |
// save to file |
| 104 |
if (cairo_surface_write_to_png(Surface, FileName.c_str()) != CAIRO_STATUS_SUCCESS) |
| 105 |
throw Error("Unable to write to PNG file"); |
| 106 |
} |
| 107 |
|
| 108 |
int main(int argc, char** argv) |
| 109 |
{ |
| 110 |
try |
| 111 |
{ |
| 112 |
if (argc < 2) |
| 113 |
throw Error("Usage: customPaper <PrinterName>"); |
| 114 |
|
| 115 |
|
| 116 |
const char* ppdFileName = cupsGetPPD(argv[1]); |
| 117 |
if (!ppdFileName) |
| 118 |
throw Error(string("Unknown printer '") + argv[1] + "'"); |
| 119 |
|
| 120 |
ppd_file_t* ppd = ppdOpenFile(ppdFileName); |
| 121 |
if (!ppd) |
| 122 |
throw Error(string("Unable to open ppd file '") + ppdFileName + "'"); |
| 123 |
|
| 124 |
|
| 125 |
int Resolution = GetPrinterResolution(ppd->groups, ppd->num_groups); |
| 126 |
|
| 127 |
// custom paper definition |
| 128 |
// use 12 mm paper size in points with 5 inch long |
| 129 |
float CustomPaperPrintableWidth = 28.8; |
| 130 |
float CustomPaperPrintableHeight = 5 * 72; |
| 131 |
|
| 132 |
int Width = int(CustomPaperPrintableWidth * Resolution / 72); |
| 133 |
int Height = int(CustomPaperPrintableHeight * Resolution / 72); |
| 134 |
bool Landscape = false; |
| 135 |
|
| 136 |
string FileName; |
| 137 |
if (Height > Width) |
| 138 |
{ |
| 139 |
Landscape = true; |
| 140 |
int t = Width; |
| 141 |
Width = Height; |
| 142 |
Height = t; |
| 143 |
} |
| 144 |
CreateBoundsImage(Width, Height, "Custom paper", FileName); |
| 145 |
|
| 146 |
int num_options = 0; |
| 147 |
cups_option_t* options = NULL; |
| 148 |
|
| 149 |
// PageSize value for custom paper |
| 150 |
char PageSizeValue[250]; |
| 151 |
sprintf(PageSizeValue, "Custom.%fx%f", CustomPaperPrintableWidth, CustomPaperPrintableHeight); |
| 152 |
|
| 153 |
|
| 154 |
num_options = cupsAddOption("PageSize", PageSizeValue, num_options, &options); |
| 155 |
num_options = cupsAddOption("MediaType", "12mm", num_options, &options); |
| 156 |
num_options = cupsAddOption("scaling", "100", num_options, &options); |
| 157 |
if (Landscape) |
| 158 |
num_options = cupsAddOption("landscape", "yes", num_options, &options); |
| 159 |
|
| 160 |
|
| 161 |
cupsPrintFile(argv[1], FileName.c_str(), "Test print with Cairo", num_options, options); |
| 162 |
cupsFreeOptions(num_options, options); |
| 163 |
|
| 164 |
|
| 165 |
ppdClose(ppd); |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
catch(std::exception& e) |
| 170 |
{ |
| 171 |
fputs(e.what(), stderr); |
| 172 |
fputs("\n", stderr); |
| 173 |
return 1; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|