gdevpxut.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpxut.c,v 1.8 2005/07/11 22:08:30 stefan Exp $ */
  14. /* Utilities for PCL XL generation */
  15. #include "math_.h"
  16. #include "string_.h"
  17. #include "gx.h"
  18. #include "stream.h"
  19. #include "gxdevcli.h"
  20. #include "gdevpxat.h"
  21. #include "gdevpxen.h"
  22. #include "gdevpxop.h"
  23. #include "gdevpxut.h"
  24. #include <assert.h>
  25. /* ---------------- High-level constructs ---------------- */
  26. /* Write the file header, including the resolution. */
  27. int
  28. px_write_file_header(stream *s, const gx_device *dev)
  29. {
  30. static const char *const enter_pjl_header =
  31. "\033%-12345X@PJL SET RENDERMODE=";
  32. static const char *const rendermode_gray = "GRAYSCALE";
  33. static const char *const rendermode_color = "COLOR";
  34. static const char *const file_header =
  35. "\n@PJL ENTER LANGUAGE = PCLXL\n\
  36. ) HP-PCL XL;1;1;Comment Copyright artofcode LLC 2005\000\n";
  37. static const byte stream_header[] = {
  38. DA(pxaUnitsPerMeasure),
  39. DUB(0), DA(pxaMeasure),
  40. DUB(eBackChAndErrPage), DA(pxaErrorReport),
  41. pxtBeginSession,
  42. DUB(0), DA(pxaSourceType),
  43. DUB(eBinaryLowByteFirst), DA(pxaDataOrg),
  44. pxtOpenDataSource
  45. };
  46. px_put_bytes(s, (const byte *)enter_pjl_header,
  47. strlen(enter_pjl_header));
  48. if (dev->color_info.num_components == 1)
  49. px_put_bytes(s, (const byte *)rendermode_gray,
  50. strlen(rendermode_gray));
  51. else
  52. px_put_bytes(s, (const byte *)rendermode_color,
  53. strlen(rendermode_color));
  54. /* We have to add 2 to the strlen because the next-to-last */
  55. /* character is a null. */
  56. px_put_bytes(s, (const byte *)file_header,
  57. strlen(file_header) + 2);
  58. px_put_usp(s, (uint) (dev->HWResolution[0] + 0.5),
  59. (uint) (dev->HWResolution[1] + 0.5));
  60. PX_PUT_LIT(s, stream_header);
  61. return 0;
  62. }
  63. /* Write the page header, including orientation. */
  64. int
  65. px_write_page_header(stream *s, const gx_device *dev)
  66. {
  67. static const byte page_header_1[] = {
  68. DUB(ePortraitOrientation), DA(pxaOrientation)
  69. };
  70. PX_PUT_LIT(s, page_header_1);
  71. return 0;
  72. }
  73. /* Write the media selection command if needed, updating the media size. */
  74. int
  75. px_write_select_media(stream *s, const gx_device *dev,
  76. pxeMediaSize_t *pms, byte *media_source)
  77. {
  78. #define MSD(ms, res, w, h)\
  79. { ms, (float)((w) * 1.0 / (res)), (float)((h) * 1.0 / res) },
  80. static const struct {
  81. pxeMediaSize_t ms;
  82. float width, height;
  83. } media_sizes[] = {
  84. px_enumerate_media(MSD)
  85. { pxeMediaSize_next }
  86. };
  87. #undef MSD
  88. float w = dev->width / dev->HWResolution[0],
  89. h = dev->height / dev->HWResolution[1];
  90. int i;
  91. pxeMediaSize_t size;
  92. byte tray = eAutoSelect;
  93. /* The default is eLetterPaper, media size 0. */
  94. for (i = countof(media_sizes) - 2; i > 0; --i)
  95. if (fabs(media_sizes[i].width - w) < 5.0 / 72 &&
  96. fabs(media_sizes[i].height - h) < 5.0 / 72
  97. )
  98. break;
  99. size = media_sizes[i].ms;
  100. /*
  101. * According to the PCL XL documentation, MediaSize must always
  102. * be specified, but MediaSource is optional.
  103. */
  104. px_put_uba(s, (byte)size, pxaMediaSize);
  105. if (media_source != NULL)
  106. tray = *media_source;
  107. px_put_uba(s, tray, pxaMediaSource);
  108. if (pms)
  109. *pms = size;
  110. return 0;
  111. }
  112. /*
  113. * Write the file trailer. Note that this takes a FILE *, not a stream *,
  114. * since it may be called after the stream is closed.
  115. */
  116. int
  117. px_write_file_trailer(FILE *file)
  118. {
  119. static const byte file_trailer[] = {
  120. pxtCloseDataSource,
  121. pxtEndSession,
  122. 033, '%', '-', '1', '2', '3', '4', '5', 'X'
  123. };
  124. fwrite(file_trailer, 1, sizeof(file_trailer), file);
  125. return 0;
  126. }
  127. /* ---------------- Low-level data output ---------------- */
  128. /* Write a sequence of bytes. */
  129. void
  130. px_put_bytes(stream * s, const byte * data, uint count)
  131. {
  132. uint used;
  133. sputs(s, data, count, &used);
  134. }
  135. /* Utilities for writing data values. */
  136. /* H-P printers only support little-endian data, so that's what we emit. */
  137. void
  138. px_put_a(stream * s, px_attribute_t a)
  139. {
  140. sputc(s, pxt_attr_ubyte);
  141. sputc(s, (byte)a);
  142. }
  143. void
  144. px_put_ac(stream *s, px_attribute_t a, px_tag_t op)
  145. {
  146. px_put_a(s, a);
  147. sputc(s, (byte)op);
  148. }
  149. void
  150. px_put_ub(stream * s, byte b)
  151. {
  152. sputc(s, pxt_ubyte);
  153. sputc(s, b);
  154. }
  155. void
  156. px_put_uba(stream *s, byte b, px_attribute_t a)
  157. {
  158. px_put_ub(s, b);
  159. px_put_a(s, a);
  160. }
  161. void
  162. px_put_s(stream * s, uint i)
  163. {
  164. sputc(s, (byte) i);
  165. sputc(s, (byte) (i >> 8));
  166. }
  167. void
  168. px_put_us(stream * s, uint i)
  169. {
  170. sputc(s, pxt_uint16);
  171. px_put_s(s, i);
  172. }
  173. void
  174. px_put_usa(stream *s, uint i, px_attribute_t a)
  175. {
  176. px_put_us(s, i);
  177. px_put_a(s, a);
  178. }
  179. void
  180. px_put_u(stream * s, uint i)
  181. {
  182. if (i <= 255)
  183. px_put_ub(s, (byte)i);
  184. else
  185. px_put_us(s, i);
  186. }
  187. void
  188. px_put_usp(stream * s, uint ix, uint iy)
  189. {
  190. spputc(s, pxt_uint16_xy);
  191. px_put_s(s, ix);
  192. px_put_s(s, iy);
  193. }
  194. void
  195. px_put_usq_fixed(stream * s, fixed x0, fixed y0, fixed x1, fixed y1)
  196. {
  197. spputc(s, pxt_uint16_box);
  198. px_put_s(s, fixed2int(x0));
  199. px_put_s(s, fixed2int(y0));
  200. px_put_s(s, fixed2int(x1));
  201. px_put_s(s, fixed2int(y1));
  202. }
  203. void
  204. px_put_ss(stream * s, int i)
  205. {
  206. sputc(s, pxt_sint16);
  207. px_put_s(s, (uint) i);
  208. }
  209. void
  210. px_put_ssp(stream * s, int ix, int iy)
  211. {
  212. sputc(s, pxt_sint16_xy);
  213. px_put_s(s, (uint) ix);
  214. px_put_s(s, (uint) iy);
  215. }
  216. void
  217. px_put_l(stream * s, ulong l)
  218. {
  219. px_put_s(s, (uint) l);
  220. px_put_s(s, (uint) (l >> 16));
  221. }
  222. void
  223. px_put_r(stream * s, floatp r)
  224. { /* Convert to single-precision IEEE float. */
  225. int exp;
  226. long mantissa = (long)(frexp(r, &exp) * 0x1000000);
  227. if (exp < -126)
  228. mantissa = 0, exp = 0; /* unnormalized */
  229. if (mantissa < 0)
  230. exp += 128, mantissa = -mantissa;
  231. /* All quantities are little-endian. */
  232. spputc(s, (byte) mantissa);
  233. spputc(s, (byte) (mantissa >> 8));
  234. spputc(s, (byte) (((exp + 127) << 7) + ((mantissa >> 16) & 0x7f)));
  235. spputc(s, (byte) ((exp + 127) >> 1));
  236. }
  237. void
  238. px_put_rl(stream * s, floatp r)
  239. {
  240. spputc(s, pxt_real32);
  241. px_put_r(s, r);
  242. }
  243. void
  244. px_put_data_length(stream * s, uint num_bytes)
  245. {
  246. if (num_bytes > 255) {
  247. spputc(s, pxt_dataLength);
  248. px_put_l(s, (ulong) num_bytes);
  249. } else {
  250. spputc(s, pxt_dataLengthByte);
  251. spputc(s, (byte) num_bytes);
  252. }
  253. }