gdevokii.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Copyright (C) 1989, 1992, 1995 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: gdevokii.c,v 1.7 2004/08/10 13:02:36 stefan Exp $*/
  14. /*
  15. * Okidata IBM compatible dot-matrix printer driver for Ghostscript.
  16. *
  17. * This device is for the Okidata Microline IBM compatible 9 pin dot
  18. * matrix printers. It is derived from the Epson 9 pin printer driver
  19. * using the standard 1/72" vertical pin spacing and the 60/120/240
  20. * dpi horizontal resolutions. The vertical feed resolution however
  21. * is 1/144" and the Okidata implements the standard 1/216" requests
  22. * through "scaling":
  23. *
  24. * (power on)
  25. * "\033J\001" (vertical feed 1/216") => Nothing happens
  26. * "\033J\001" (vertical feed 1/216") => Advance 1/144"
  27. * "\033J\001" (vertical feed 1/216") => Advance 1/144"
  28. * "\033J\001" (vertical feed 1/216") => Nothing happens
  29. * (and so on)
  30. *
  31. * The simple minded accounting used here keep track of when the
  32. * page actually advances assumes the printer starts in a "power on"
  33. * state.
  34. *
  35. * Supported resolutions are:
  36. *
  37. * 60x72 60x144
  38. * 120x72 120x144
  39. * 240x72 240x144
  40. *
  41. */
  42. #include "gdevprn.h"
  43. /*
  44. * Valid values for X_DPI:
  45. *
  46. * 60, 120, 240
  47. *
  48. * The value specified at compile time is the default value used if the
  49. * user does not specify a resolution at runtime.
  50. */
  51. #ifndef X_DPI
  52. # define X_DPI 120
  53. #endif
  54. /*
  55. * Valid values for Y_DPI:
  56. *
  57. * 72, 144
  58. *
  59. * The value specified at compile time is the default value used if the
  60. * user does not specify a resolution at runtime.
  61. */
  62. #ifndef Y_DPI
  63. # define Y_DPI 72
  64. #endif
  65. /* The device descriptor */
  66. private dev_proc_print_page(okiibm_print_page);
  67. /* Okidata IBM device */
  68. const gx_device_printer far_data gs_okiibm_device =
  69. prn_device(prn_std_procs, "okiibm",
  70. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  71. X_DPI, Y_DPI,
  72. 0.25, 0.0, 0.25, 0.0, /* margins */
  73. 1, okiibm_print_page);
  74. /* ------ Internal routines ------ */
  75. /* Forward references */
  76. private void okiibm_output_run(byte *, int, int, char, FILE *, int);
  77. /* Send the page to the printer. */
  78. private int
  79. okiibm_print_page1(gx_device_printer *pdev, FILE *prn_stream, int y_9pin_high,
  80. const char *init_string, int init_length,
  81. const char *end_string, int end_length)
  82. {
  83. static const char graphics_modes_9[5] =
  84. {
  85. -1, 0 /*60*/, 1 /*120*/, -1, 3 /*240*/
  86. };
  87. int in_y_mult = (y_9pin_high ? 2 : 1);
  88. int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  89. /* Note that in_size is a multiple of 8. */
  90. int in_size = line_size * (8 * in_y_mult);
  91. byte *buf1 = (byte *)gs_malloc(pdev->memory, in_size, 1, "okiibm_print_page(buf1)");
  92. byte *buf2 = (byte *)gs_malloc(pdev->memory, in_size, 1, "okiibm_print_page(buf2)");
  93. byte *in = buf1;
  94. byte *out = buf2;
  95. int out_y_mult = 1;
  96. int x_dpi = pdev->x_pixels_per_inch;
  97. char start_graphics = graphics_modes_9[x_dpi / 60];
  98. int first_pass = (start_graphics == 3 ? 1 : 0);
  99. int last_pass = first_pass * 2;
  100. int y_passes = (y_9pin_high ? 2 : 1);
  101. int skip = 0, lnum = 0, pass, ypass;
  102. int y_step = 0;
  103. /* Check allocations */
  104. if ( buf1 == 0 || buf2 == 0 )
  105. { if ( buf1 )
  106. gs_free(pdev->memory, (char *)buf1, in_size, 1, "okiibm_print_page(buf1)");
  107. if ( buf2 )
  108. gs_free(pdev->memory, (char *)buf2, in_size, 1, "okiibm_print_page(buf2)");
  109. return_error(gs_error_VMerror);
  110. }
  111. /* Initialize the printer. */
  112. fwrite(init_string, 1, init_length, prn_stream);
  113. /* Print lines of graphics */
  114. while ( lnum < pdev->height )
  115. {
  116. byte *in_data;
  117. byte *inp;
  118. byte *in_end;
  119. byte *out_end;
  120. int lcnt;
  121. /* Copy 1 scan line and test for all zero. */
  122. gdev_prn_get_bits(pdev, lnum, in, &in_data);
  123. if ( in_data[0] == 0 &&
  124. !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
  125. )
  126. {
  127. lnum++;
  128. skip += 2 / in_y_mult;
  129. continue;
  130. }
  131. /*
  132. * Vertical tab to the appropriate position.
  133. * The skip count is in 1/144" steps. If total
  134. * vertical request is not a multiple od 1/72"
  135. * we need to make sure the page is actually
  136. * going to advance.
  137. */
  138. if ( skip & 1 )
  139. {
  140. int n = 1 + (y_step == 0 ? 1 : 0);
  141. fprintf(prn_stream, "\033J%c", n);
  142. y_step = (y_step + n) % 3;
  143. skip -= 1;
  144. }
  145. skip = skip / 2 * 3;
  146. while ( skip > 255 )
  147. {
  148. fputs("\033J\377", prn_stream);
  149. skip -= 255;
  150. }
  151. if ( skip )
  152. {
  153. fprintf(prn_stream, "\033J%c", skip);
  154. }
  155. /* Copy the the scan lines. */
  156. lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
  157. if ( lcnt < 8 * in_y_mult )
  158. { /* Pad with lines of zeros. */
  159. memset(in + lcnt * line_size, 0,
  160. in_size - lcnt * line_size);
  161. }
  162. if ( y_9pin_high )
  163. { /* Shuffle the scan lines */
  164. byte *p;
  165. int i;
  166. static const char index[] =
  167. { 0, 2, 4, 6, 8, 10, 12, 14,
  168. 1, 3, 5, 7, 9, 11, 13, 15
  169. };
  170. for ( i = 0; i < 16; i++ )
  171. {
  172. memcpy( out + (i * line_size),
  173. in + (index[i] * line_size),
  174. line_size);
  175. }
  176. p = in;
  177. in = out;
  178. out = p;
  179. }
  180. for ( ypass = 0; ypass < y_passes; ypass++ )
  181. {
  182. for ( pass = first_pass; pass <= last_pass; pass++ )
  183. {
  184. /* We have to 'transpose' blocks of 8 pixels x 8 lines, */
  185. /* because that's how the printer wants the data. */
  186. if ( pass == first_pass )
  187. {
  188. out_end = out;
  189. inp = in;
  190. in_end = inp + line_size;
  191. for ( ; inp < in_end; inp++, out_end += 8 )
  192. {
  193. gdev_prn_transpose_8x8(inp + (ypass * 8 * line_size),
  194. line_size, out_end, 1);
  195. }
  196. /* Remove trailing 0s. */
  197. while ( out_end > out && out_end[-1] == 0 )
  198. {
  199. out_end--;
  200. }
  201. }
  202. /* Transfer whatever is left and print. */
  203. if ( out_end > out )
  204. {
  205. okiibm_output_run(out, (int)(out_end - out),
  206. out_y_mult, start_graphics,
  207. prn_stream, pass);
  208. }
  209. fputc('\r', prn_stream);
  210. }
  211. if ( ypass < y_passes - 1 )
  212. {
  213. int n = 1 + (y_step == 0 ? 1 : 0);
  214. fprintf(prn_stream, "\033J%c", n);
  215. y_step = (y_step + n) % 3;
  216. }
  217. }
  218. skip = 16 - y_passes + 1; /* no skip on last Y pass */
  219. lnum += 8 * in_y_mult;
  220. }
  221. /* Reinitialize the printer. */
  222. fwrite(end_string, 1, end_length, prn_stream);
  223. fflush(prn_stream);
  224. gs_free(pdev->memory, (char *)buf2, in_size, 1, "okiibm_print_page(buf2)");
  225. gs_free(pdev->memory, (char *)buf1, in_size, 1, "okiibm_print_page(buf1)");
  226. return 0;
  227. }
  228. /* Output a single graphics command. */
  229. /* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
  230. private void
  231. okiibm_output_run(byte *data, int count, int y_mult,
  232. char start_graphics, FILE *prn_stream, int pass)
  233. {
  234. int xcount = count / y_mult;
  235. fputc(033, prn_stream);
  236. fputc("KLYZ"[start_graphics], prn_stream);
  237. fputc(xcount & 0xff, prn_stream);
  238. fputc(xcount >> 8, prn_stream);
  239. if ( !pass )
  240. {
  241. fwrite(data, 1, count, prn_stream);
  242. }
  243. else
  244. {
  245. /* Only write every other column of y_mult bytes. */
  246. int which = pass;
  247. register byte *dp = data;
  248. register int i, j;
  249. for ( i = 0; i < xcount; i++, which++ )
  250. {
  251. for ( j = 0; j < y_mult; j++, dp++ )
  252. {
  253. putc(((which & 1) ? *dp : 0), prn_stream);
  254. }
  255. }
  256. }
  257. }
  258. /* The print_page procedures are here, to avoid a forward reference. */
  259. private const char okiibm_init_string[] = { 0x18 };
  260. private const char okiibm_end_string[] = { 0x0c };
  261. private const char okiibm_one_direct[] = { 0x1b, 0x55, 0x01 };
  262. private const char okiibm_two_direct[] = { 0x1b, 0x55, 0x00 };
  263. private int
  264. okiibm_print_page(gx_device_printer *pdev, FILE *prn_stream)
  265. {
  266. char init_string[16], end_string[16];
  267. int init_length, end_length;
  268. init_length = sizeof(okiibm_init_string);
  269. memcpy(init_string, okiibm_init_string, init_length);
  270. end_length = sizeof(okiibm_end_string);
  271. memcpy(end_string, okiibm_end_string, end_length);
  272. if ( pdev->y_pixels_per_inch > 72 &&
  273. pdev->x_pixels_per_inch > 60 )
  274. {
  275. /* Unidirectional printing for the higher resolutions. */
  276. memcpy( init_string + init_length, okiibm_one_direct,
  277. sizeof(okiibm_one_direct) );
  278. init_length += sizeof(okiibm_one_direct);
  279. memcpy( end_string + end_length, okiibm_two_direct,
  280. sizeof(okiibm_two_direct) );
  281. end_length += sizeof(okiibm_two_direct);
  282. }
  283. return okiibm_print_page1( pdev, prn_stream,
  284. pdev->y_pixels_per_inch > 72 ? 1 : 0,
  285. init_string, init_length,
  286. end_string, end_length );
  287. }