2 * Copyright (c) 2001-2002 artofcode LLC.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include "ijs_server.h"
33 typedef struct _ExampleParamList ExampleParamList;
35 struct _ExampleParamList {
36 ExampleParamList *next;
43 example_status_cb (void *status_cb_data,
51 example_list_cb (void *list_cb_data,
57 const char *param_list = "OutputFile,DeviceManufacturer,DeviceModel,PageImageFormat,Dpi,Width,Height,BitsPerSample,ColorSpace,NumChan,PaperSize,PrintableArea,PrintableTopLeft,TopLeft";
58 int size = strlen (param_list);
60 fprintf (stderr, "example_list_cb\n");
65 memcpy (val_buf, param_list, size);
70 example_enum_cb (void *enum_cb_data,
77 const char *val = NULL;
78 if (!strcmp (key, "ColorSpace"))
79 val = "DeviceRGB,DeviceGray,DeviceCMYK";
80 else if (!strcmp (key, "DeviceManufacturer"))
81 val = "IJS Distribution";
82 else if (!strcmp (key, "DeviceModel"))
83 val = "ijs_server_example";
84 else if (!strcmp (key, "PageImageFormat"))
91 int size = strlen (val);
95 memcpy (val_buf, val, size);
100 /* A C implementation of /^(\d\.+\-eE)+x(\d\.+\-eE)+$/ */
102 example_parse_wxh (const char *val, int size,
103 double *pw, double *ph)
109 for (i = 0; i < size; i++)
116 if (i >= sizeof(buf))
119 memcpy (buf, val, i);
121 *pw = strtod (buf, &tail);
125 if (size - i > sizeof(buf))
128 memcpy (buf, val + i + 1, size - i - 1);
129 buf[size - i - 1] = 0;
130 *ph = strtod (buf, &tail);
138 * example_find_key: Search parameter list for key.
140 * @key: key to look up
142 * Return value: ExampleParamList entry matching @key, or NULL.
144 static ExampleParamList *
145 example_find_key (ExampleParamList *pl, const char *key)
147 ExampleParamList *curs;
149 for (curs = pl; curs != NULL; curs = curs->next)
151 if (!strcmp (curs->key, key))
158 * @printable: An array in which to store the printable area.
160 * On return, @printable = PrintableArea[0:1] + TopLeft[0:1]
163 example_compute_printable (ExampleParamList *pl, double printable[4])
165 ExampleParamList *curs;
166 double width, height;
170 curs = example_find_key (pl, "PaperSize");
173 code = example_parse_wxh (curs->value, curs->value_size, &width, &height);
177 printable[0] = width - 2 * margin;
178 printable[1] = height - 2 * margin;
179 printable[2] = margin;
180 printable[3] = margin;
187 example_compute_offset (ExampleParamList *pl, IjsPageHeader *ph,
188 double *px0, double *py0)
190 ExampleParamList *curs;
191 double width, height;
198 curs = example_find_key (pl, "PaperSize");
202 code = example_parse_wxh (curs->value, curs->value_size, &width, &height);
206 curs = example_find_key (pl, "TopLeft");
209 code = example_parse_wxh (curs->value, curs->value_size,
216 code = example_compute_printable (pl, printable);
228 *py0 = height - ph->height / ph->yres - top;
235 example_get_cb (void *get_cb_data,
242 ExampleParamList *pl = *(ExampleParamList **)get_cb_data;
243 ExampleParamList *curs;
248 fprintf (stderr, "example_get_cb: %s\n", key);
249 curs = example_find_key (pl, key);
252 if (curs->value_size > val_size)
254 memcpy (val_buf, curs->value, curs->value_size);
255 return curs->value_size;
258 if (!strcmp (key, "PrintableArea") || !strcmp (key, "PrintableTopLeft"))
261 int off = !strcmp (key, "PrintableArea") ? 0 : 2;
263 code = example_compute_printable (pl, printable);
266 sprintf (buf, "%gx%g", printable[off + 0], printable[off + 1]);
271 if (!strcmp (key, "DeviceManufacturer"))
272 val = "IJS Distribution";
273 else if (!strcmp (key, "DeviceModel"))
274 val = "ijs_server_example";
275 else if (!strcmp (key, "PageImageFormat"))
279 return IJS_EUNKPARAM;
282 int size = strlen (val);
286 memcpy (val_buf, val, size);
292 example_set_cb (void *set_cb_data, IjsServerCtx *ctx, IjsJobId job_id,
293 const char *key, const char *value, int value_size)
295 ExampleParamList **ppl = (ExampleParamList **)set_cb_data;
296 ExampleParamList *pl;
297 int key_len = strlen (key);
300 fprintf (stderr, "example_set_cb: %s=", key);
302 if (!strcmp (key, "PaperSize"))
304 double width, height;
306 code = example_parse_wxh (value, value_size, &width, &height);
311 fwrite (value, 1, value_size, stderr);
312 fputs ("\n", stderr);
314 pl = example_find_key (*ppl, key);
318 pl = (ExampleParamList *)malloc (sizeof (ExampleParamList));
320 pl->key = malloc (key_len + 1);
321 memcpy (pl->key, key, key_len + 1);
329 pl->value = malloc (value_size);
330 memcpy (pl->value, value, value_size);
331 pl->value_size = value_size;
336 * Finds a parameter in the param list, and allocates a null terminated
337 * string with the value.
340 find_param (ExampleParamList *pl, const char *key)
342 ExampleParamList *curs;
345 curs = example_find_key (pl, key);
349 result = malloc (curs->value_size + 1);
350 memcpy (result, curs->value, curs->value_size);
351 result[curs->value_size] = 0;
356 free_param_list (ExampleParamList *pl)
358 ExampleParamList *next;
360 for (; pl != NULL; pl = next)
370 main (int argc, char **argv)
376 char hexbuf[BUF_SIZE * 3];
379 double xscale, yscale;
381 ExampleParamList *pl = NULL;
383 ctx = ijs_server_init ();
386 ijs_server_install_status_cb (ctx, example_status_cb, &pl);
387 ijs_server_install_list_cb (ctx, example_list_cb, &pl);
388 ijs_server_install_enum_cb (ctx, example_enum_cb, &pl);
389 ijs_server_install_set_cb (ctx, example_set_cb, &pl);
390 ijs_server_install_get_cb (ctx, example_get_cb, &pl);
394 int total_bytes, bytes_left;
395 ExampleParamList *curs;
397 status = ijs_server_get_page_header (ctx, &ph);
399 fprintf (stderr, "got page header, %d x %d\n",
400 ph.width, ph.height);
404 fn = find_param (pl, "OutputFile");
405 /* todo: check error! */
409 fn = find_param (pl, "OutputFD");
412 f = fdopen (atoi (fn), "w");
421 fprintf (stderr, "can't open output file %s\n", fn);
430 fprintf (f, "%%!PS-Adobe-2.0\n");
432 example_compute_offset (pl, &ph, &x0, &y0);
434 xscale = 72.0 / ph.xres;
435 yscale = 72.0 / ph.yres;
437 fprintf (f, "%%%%BoundingBox: %d %d %d %d\n",
438 (int)(x0 * 72), (int)(y0 * 72),
439 (int)(x0 * 72 + xscale * ph.width + 0.999),
440 (int)(y0 * 72 + yscale * ph.height + 0.999));
442 fprintf (f, "/rhex { currentfile exch readhexstring pop } bind def\n");
443 fprintf (f, "/picstr %d string def\n", ph.width);
445 for (curs = pl; curs != NULL; curs = curs->next)
447 fprintf (f, "%% IJS parameter: %s = ", curs->key);
448 fwrite (curs->value, 1, curs->value_size, f);
457 "[ %d 0 0 %d 0 %d ]\n",
459 xscale * ph.width, yscale * ph.height,
460 ph.width, ph.height, ph.bps,
461 ph.width, -ph.height, ph.height);
463 fprintf (f, "{ picstr rhex } image\n");
466 fprintf (f, "{ picstr rhex }\n"
467 "false %d colorimage\n", ph.n_chan);
469 total_bytes = ((ph.n_chan * ph.bps * ph.width + 7) >> 3) * ph.height;
470 bytes_left = total_bytes;
473 int n_bytes = bytes_left;
476 if (n_bytes > sizeof(buf))
477 n_bytes = sizeof(buf);
479 fprintf (stderr, "%d bytes left, reading %d\n", bytes_left, n_bytes);
481 status = ijs_server_get_data (ctx, buf, n_bytes);
484 fprintf (stderr, "page aborted!\n");
488 for (i = 0; i < n_bytes; i++)
490 const char hex[16] = "0123456789AbCdEf";
491 unsigned char c = ((unsigned char *)buf)[i];
493 hexbuf[j++] = hex[c >> 4];
494 hexbuf[j++] = hex[c & 0xf];
498 if ((n_bytes & 31) != 0)
500 fwrite (hexbuf, 1, j, f);
501 bytes_left -= n_bytes;
503 fprintf (f, "grestore\nshowpage\n");
507 if (status > 0) status = 0; /* normal exit */
511 ijs_server_done (ctx);
513 free_param_list (pl);
516 fprintf (stderr, "server exiting with status %d\n", status);