forked from thenexxuz-zz/cups-epilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcups-epilog.c
1680 lines (1521 loc) · 54.3 KB
/
cups-epilog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file cups-epilog.c - Epilog cups driver */
/* @file cups-epilog.c @verbatim
*========================================================================
* Copyright © 2002-2008 Andrews & Arnold Ltd <info@aaisp.net.uk>
* Copyright 2008 AS220 Labs <brandon@as220.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*========================================================================
*
*
* Author: Andrew & Arnold Ltd and Brandon Edens
*
*
* Description:
* Epilog laser engraver
*
* The Epilog laser engraver comes with a windows printer driver. This works
* well with Corel Draw, and that is about it. There are other windows
* applications, like inkscape, but these rasterise the image before sending to
* the windows printer driver, so there is no way to use them to vector cut!
*
* The cups-epilog app is a cups backend, so build and link/copy to
* /usr/lib/cups/backend/epilog. It allows you to print postscript to the laser
* and both raster and cut. It works well with inkscape.
*
* With this linux driver, vector cutting is recognised by any line or curve in
* 100% red (1.0 0.0 0.0 setrgbcolor).
*
* Create printers using epilog://host/Legend/options where host is the
* hostname or IP of the epilog engraver. The options are as follows. This
* allows you to make a printer for each different type of material.
* af Auto focus (0=no, 1=yes)
* r Resolution 75-1200
* rs Raster speed 1-100
* rp Raster power 0-100
* vs Vector speed 1-100
* vp Vector power 1-100
* vf Vector frequency 10-5000
* sc Photograph screen size in pizels, 0=threshold, +ve=line, -ve=spot, used
* in mono mode, default 8.
* rm Raster mode mono/grey/colour
*
* The mono raster mode uses a line or dot screen on any grey levels or
* colours. This can be controlled with the sc parameter. The default is 8,
* which makes a nice fine line screen on 600dpi engraving. At 600/1200 dpi,
* the image is also lightened to allow for the size of the laser point.
*
* The grey raster mode maps the grey level to power level. The power level is
* scaled to the raster power setting (unlike the windows driver which is
* always 100% in 3D mode).
*
* In colour mode, the primary and secondary colours are processed as separate
* passes, using the grey level of the colour as a power level. The power level
* is scaled to the raster power setting. Note that red is 100% red, and non
* 100% green and blue, etc, so 50% red, 0% green/blue is not counted as red,
* but counts as "grey". 100% red, and 50% green/blue counts as red, half
* power. This means you can make distinct raster areas of the page so that you
* do not waste time moving the head over blank space between them.
*
* Epolog cups driver
* Uses gs to rasterise the postscript input.
* URI is epilog://host/Legend/options
* E.g. epilog://host/Legend/rp=100/rs=100/vp=100/vs=10/vf=5000/rm=mono/flip/af
* Options are as follows, use / to separate :-
* rp Raster power
* rs Raster speed
* vp Vector power
* vs Vector speed
* vf Vector frequency
* w Default image width (pt)
* h Default image height (pt)
* sc Screen (lpi = res/screen, 0=simple threshold)
* r Resolution (dpi)
* af Auto focus
* rm Raster mode (mono/grey/colour)
* flip X flip (for reverse cut)
* Raster modes:-
* mono Screen applied to grey levels
* grey Grey levels are power (scaled to raster power setting)
* colour Each colour grey/red/green/blue/cyan/magenta/yellow plotted
* separately, lightness=power
*
*
* Installation:
* gcc -o epilog `cups-config --cflags` cups-epilog.c `cups-config --libs`
* http://www.cups.org/documentation.php/api-overview.html
*
* Manual testing can be accomplished through execution akin to:
* $ export DEVICE_URI="epilog://epilog-mini/Legend/rp=100/rs=100/vp=100/vs=10/vf=5000/rm=grey"
* # ./epilog job user title copies options
* $ ./epilog 123 jdoe test 1 options < hello-world.ps
*
*/
/*************************************************************************
* includes
*/
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/*************************************************************************
* local defines
*/
/** Default on whether or not auto-focus is enabled. */
#define AUTO_FOCUS (1)
/** Default bed height (y-axis) in pts. */
#define BED_HEIGHT (864)
/** Default bed width (x-axis) in pts. */
#define BED_WIDTH (1728)
/** Number of bytes in the bitmap header. */
#define BITMAP_HEADER_NBYTES (54)
/** Default for debug mode. */
#define DEBUG (1)
/** Basename for files generated by the program. */
#define FILE_BASENAME "epilog"
/** Number of characters allowable for a filename. */
#define FILENAME_NCHARS (128)
/** Default on whether or not the result is supposed to be flipped along the X
* axis.
*/
#define FLIP (0)
/** Maximum allowable hostname characters. */
#define HOSTNAME_NCHARS (1024)
/** Additional offset for the X axis. */
#define HPGLX (0)
/** Additional offset for the Y axis. */
#define HPGLY (0)
/** Whether or not to rotate the incoming PDF 90 degrees clockwise. */
#define PDF_ROTATE_90 (1)
/** Accepted number of points per an inch. */
#define POINTS_PER_INCH (72)
/** Maximum wait before timing out on connecting to the printer (in seconds). */
#define PRINTER_MAX_WAIT (300)
/** Default mode for processing raster engraving (varying power depending upon
* image characteristics).
* Possible values are:
* 'c' = color determines power level
* 'g' = grey-scale levels determine power level
* 'm' = mono mode
* 'n' = no rasterization
*/
#define RASTER_MODE_DEFAULT 'm'
/** Default power level for raster engraving */
#define RASTER_POWER_DEFAULT (40)
/** Whether or not the raster printing is to be repeated. */
#define RASTER_REPEAT (1)
/** Default speed for raster engraving */
#define RASTER_SPEED_DEFAULT (100)
/** Default resolution is 600 DPI */
#define RESOLUTION_DEFAULT (600)
/** Pixel size of screen (0 is threshold).
* FIXME - add more details
*/
#define SCREEN_DEFAULT (8)
/** Number of seconds per a minute. */
#define SECONDS_PER_MIN (60)
/** Temporary directory to store files. */
#define TMP_DIRECTORY "/tmp"
/** FIXME */
#define VECTOR_FREQUENCY_DEFAULT (500)
/** Default power level for vector cutting. */
#define VECTOR_POWER_DEFAULT (50)
/** Default speed level for vector cutting. */
#define VECTOR_SPEED_DEFAULT (30)
/*************************************************************************
* local types
*/
/*************************************************************************
* local variables
*/
/** Temporary buffer for building out strings. */
static char buf[102400];
/** Determines whether or not debug is enabled. */
static char debug = DEBUG;
/** Variable to track auto-focus. */
static int focus = AUTO_FOCUS;
/** Variable to track whether or not the X axis should be flipped. */
static char flip = FLIP;
/** Height of the image (y-axis). By default this is the bed's height. */
static int height = BED_HEIGHT;
/** Job name for the print. */
static char *job_name = "";
/** User name that submitted the print job. */
static char *job_user = "";
/** Title for the job print. */
static char *job_title = "";
/** Number of copies to produce of the print. */
static char *job_copies = "";
/** Printer queue specified options. */
static char *job_options = "";
/** Variable to track the resolution of the print. */
static int resolution = RESOLUTION_DEFAULT;
/** Variable to track the mode for rasterization. One of color 'c', or
* grey-scale 'g', mono 'm', or none 'n'
*/
static char raster_mode = RASTER_MODE_DEFAULT;
/** Variable to track the raster speed. */
static int raster_speed = RASTER_SPEED_DEFAULT;
/** Variable to track the raster power. */
static int raster_power = RASTER_POWER_DEFAULT;
/** Variable to track whether or not a rasterization should be repeated. */
static int raster_repeat = RASTER_REPEAT;
/** FIXME -- pixel size of screen, 0= threshold */
static int screen = SCREEN_DEFAULT;
/** The DEVICE_URI for the printer. */
static char *device_uri = "";
/** Options for the printer. */
static char *queue = "";
/** Variable to track the vector speed. */
static int vector_speed = VECTOR_SPEED_DEFAULT;
/** Variable to track the vector power. */
static int vector_power = VECTOR_POWER_DEFAULT;
/** Variable to track the vector frequency. FIXME */
static int vector_freq = VECTOR_FREQUENCY_DEFAULT;
/** Width of the image (x-axis). By default this is the bed's width. */
static int width = BED_WIDTH; // default bed
/** X re-center (0 = not). */
static int x_center;
/** Track whether or not to repeat X. */
static int x_repeat = 1;
/** Y re-center (0 = not). */
static int y_center;
/** Track whether or not to repeat X. */
static int y_repeat = 1;
/*************************************************************************
* local functions
*/
static int big_to_little_endian(uint8_t *position, int bytes);
static bool execute_ghostscript(char *filename_bitmap, char *filename_eps,
char *filename_vector,
char *bmp_mode, int resolution,
int height, int width);
static bool generate_raster(FILE *pjl_file, FILE *bitmap_file);
static bool generate_vector(FILE *pjl_file, FILE *vector_file);
static bool generate_pjl(FILE *bitmap_file, FILE *pjl_file, FILE *vector_file);
static bool ps_to_eps(FILE *ps_file, FILE *eps_file);
static bool process_job_title_commands(char *title);
static bool process_queue_options(char *queue);
static void range_checks(void);
static int printer_connect(const char *host, const int timeout);
static bool printer_disconnect(int socket_descriptor);
static bool printer_send(const char *host, FILE *pjl_file);
/*************************************************************************/
/**
* Convert a big endian value stored in the array starting at the given pointer
* position to its little endian value.
*
* @param position the starting location for the conversion. Each successive
* unsigned byte is upto nbytes is considered part of the value.
* @param nbytes the number of successive bytes to convert.
*
* @return An integer containing the little endian value of the successive
* bytes.
*/
static int
big_to_little_endian(uint8_t *position, int nbytes)
{
int i;
int result = 0;
for (i = 0; i < nbytes; i++) {
result += *(position + i) << (8 * i);
}
return result;
}
/**
* Execute ghostscript feeding it an ecapsulated postscript file which is then
* converted into a bitmap image. As a byproduct output of the ghostscript
* process is redirected to a .vector file which will contain instructions on
* how to perform a vector cut of lines within the postscript.
*
* @param filename_bitmap the filename to use for the resulting bitmap file.
* @param filename_eps the filename to read in encapsulated postscript from.
* @param filename_vector the filename that will contain the vector
* information.
* @param bmp_mode a string which is one of bmp16m, bmpgray, or bmpmono.
* @param resolution the encapsulated postscript resolution.
* @param height the postscript height in points per inch.
* @param width the postscript width in points per inch.
*
* @return Return true if the execution of ghostscript succeeds, false
* otherwise.
*/
static bool
execute_ghostscript(char *filename_bitmap,
char *filename_eps,
char *filename_vector,
char *bmp_mode, int resolution,
int height, int width)
{
char buf[8192];
sprintf(buf,
"/usr/bin/gs -q -dBATCH -dNOPAUSE -r%d -g%dx%d -sDEVICE=%s \
-sOutputFile=%s %s > %s",
resolution,
(width * resolution) / POINTS_PER_INCH,
(height * resolution) / POINTS_PER_INCH,
bmp_mode,
filename_bitmap,
filename_eps,
filename_vector);
if (debug) {
fprintf(stderr, "%s\n", buf);
}
if (system(buf)) {
return false;
}
return true;
}
/**
*
*/
static bool
generate_raster(FILE *pjl_file, FILE *bitmap_file)
{
int h;
int d;
int offx;
int offy;
int basex = 0;
int basey = 0;
int repeat;
uint8_t bitmap_header[BITMAP_HEADER_NBYTES];
if (x_center) {
basex = x_center - width / 2;
}
if (y_center) {
basey = y_center - height / 2;
}
if (basex < 0) {
basex = 0;
}
if (basey < 0) {
basey = 0;
}
// rasterises
basex = basex * resolution / POINTS_PER_INCH;
basey = basey * resolution / POINTS_PER_INCH;
repeat = raster_repeat;
while (repeat--) {
/* repeated (over printed) */
int pass;
int passes;
long base_offset;
if (raster_mode == 'c') {
passes = 7;
} else {
passes = 1;
}
/* Read in the bitmap header. */
fread(bitmap_header, 1, BITMAP_HEADER_NBYTES, bitmap_file);
/* Re-load width/height from bmp as it is possible that someone used
* setpagedevice or some such
*/
/* Bytes 18 - 21 are the bitmap width (little endian format). */
width = big_to_little_endian(bitmap_header + 18, 4);
/* Bytes 22 - 25 are the bitmap height (little endian format). */
height = big_to_little_endian(bitmap_header + 22, 4);
/* Bytes 10 - 13 base offset for the beginning of the bitmap data. */
base_offset = big_to_little_endian(bitmap_header + 10, 4);
if (raster_mode == 'c' || raster_mode == 'g') {
/* colour/grey are byte per pixel power levels */
h = width;
/* BMP padded to 4 bytes per scan line */
d = (h * 3 + 3) / 4 * 4;
} else {
/* mono */
h = (width + 7) / 8;
/* BMP padded to 4 bytes per scan line */
d = (h + 3) / 4 * 4;
}
if (debug) {
fprintf(stderr, "Width %d Height %d Bytes %d Line %d\n",
width, height, h, d);
}
/* Raster Orientation */
fprintf(pjl_file, "\e*r0F");
/* Raster power */
fprintf(pjl_file, "\e&y%dP",
(raster_mode == 'c' ||
raster_mode == 'g') ? 100 : raster_power);
/* Raster speed */
fprintf(pjl_file, "\e&z%dS", raster_speed);
fprintf(pjl_file, "\e*r%dT", height * y_repeat);
fprintf(pjl_file, "\e*r%dS", width * x_repeat);
/* Raster compression */
fprintf(pjl_file, "\e*b%dM", (raster_mode == 'c' || raster_mode == 'g')
? 7 : 2);
/* Raster direction (1 = up) */
fprintf(pjl_file, "\e&y1O");
if (debug) {
/* Output raster debug information */
fprintf(stderr, "Raster power=%d speed=%d\n",
((raster_mode == 'c' || raster_mode == 'g') ?
100 : raster_power),
raster_speed);
}
/* start at current position */
fprintf(pjl_file, "\e*r1A");
for (offx = width * (x_repeat - 1); offx >= 0; offx -= width) {
for (offy = height * (y_repeat - 1); offy >= 0; offy -= height) {
for (pass = 0; pass < passes; pass++) {
// raster (basic)
int y;
char dir = 0;
fseek(bitmap_file, base_offset, SEEK_SET);
for (y = height - 1; y >= 0; y--) {
int l;
switch (raster_mode) {
case 'c': // colour (passes)
{
char *f = buf;
char *t = buf;
if (d > sizeof (buf)) {
perror("Too wide");
return false;
}
l = fread ((char *)buf, 1, d, bitmap_file);
if (l != d) {
fprintf(stderr, "Bad bit data from gs %d/%d (y=%d)\n", l, d, y);
return false;
}
while (l--) {
// pack and pass check RGB
int n = 0;
int v = 0;
int p = 0;
int c = 0;
for (c = 0; c < 3; c++) {
if (*f > 240) {
p |= (1 << c);
} else {
n++;
v += *f;
}
f++;
}
if (n) {
v /= n;
} else {
p = 0;
v = 255;
}
if (p != pass) {
v = 255;
}
*t++ = 255 - v;
}
}
break;
case 'g': // grey level
{
/* BMP padded to 4 bytes per scan line */
int d = (h + 3) / 4 * 4;
if (d > sizeof (buf)) {
fprintf(stderr, "Too wide\n");
return false;
}
l = fread((char *)buf, 1, d, bitmap_file);
if (l != d) {
fprintf (stderr, "Bad bit data from gs %d/%d (y=%d)\n", l, d, y);
return false;
}
for (l = 0; l < h; l++) {
buf[l] = (255 - (uint8_t)buf[l]);
}
}
break;
default: // mono
{
int d = (h + 3) / 4 * 4; // BMP padded to 4 bytes per scan line
if (d > sizeof (buf))
{
perror("Too wide");
return false;
}
l = fread((char *) buf, 1, d, bitmap_file);
if (l != d)
{
fprintf(stderr, "Bad bit data from gs %d/%d (y=%d)\n", l, d, y);
return false;
}
}
}
if (raster_mode == 'c' || raster_mode == 'g') {
for (l = 0; l < h; l++) {
/* Raster value is multiplied by the
* power scale.
*/
buf[l] = (uint8_t)buf[l] * raster_power / 255;
}
}
/* find left/right of data */
for (l = 0; l < h && !buf[l]; l++) {
;
}
if (l < h) {
/* a line to print */
int r;
int n;
unsigned char pack[sizeof (buf) * 5 / 4 + 1];
for (r = h - 1; r > l && !buf[r]; r--) {
;
}
r++;
fprintf(pjl_file, "\e*p%dY", basey + offy + y);
fprintf(pjl_file, "\e*p%dX", basex + offx +
((raster_mode == 'c' || raster_mode == 'g') ? l : l * 8));
if (dir) {
fprintf(pjl_file, "\e*b%dA", -(r - l));
// reverse bytes!
for (n = 0; n < (r - l) / 2; n++){
unsigned char t = buf[l + n];
buf[l + n] = buf[r - n - 1];
buf[r - n - 1] = t;
}
} else {
fprintf(pjl_file, "\e*b%dA", (r - l));
}
dir = 1 - dir;
// pack
n = 0;
while (l < r) {
int p;
for (p = l; p < r && p < l + 128 && buf[p]
== buf[l]; p++) {
;
}
if (p - l >= 2) {
// run length
pack[n++] = 257 - (p - l);
pack[n++] = buf[l];
l = p;
} else {
for (p = l;
p < r && p < l + 127 &&
(p + 1 == r || buf[p] !=
buf[p + 1]);
p++) {
;
}
pack[n++] = p - l - 1;
while (l < p) {
pack[n++] = buf[l++];
}
}
}
fprintf(pjl_file, "\e*b%dW", (n + 7) / 8 * 8);
r = 0;
while (r < n)
fputc(pack[r++], pjl_file);
while (r & 7)
{
r++;
fputc(0x80, pjl_file);
}
}
}
}
}
}
fprintf(pjl_file, "\e*rC"); // end raster
fputc(26, pjl_file); // some end of file markers
fputc(4, pjl_file);
}
return true;
}
/**
*
*/
static bool
generate_vector(FILE *pjl_file, FILE *vector_file)
{
char up = 1; // output status
char newline = 1; // input status (last was M)
char started = 0;
int sx = 0;
int sy = 0;
int lx = 0;
int ly = 0;
int power = 100;
int offx;
int offy;
int basex = 0;
int basey = 0;
if (x_center) {
basex = x_center - width / 2;
}
if (y_center) {
basey = y_center - height / 2;
}
if (basex < 0) {
basex = 0;
}
if (basey < 0) {
basey = 0;
}
// rasterises
basex = basex * resolution / POINTS_PER_INCH;
basey = basey * resolution / POINTS_PER_INCH;
for (offy = height * (y_repeat - 1); offy >= 0; offy -= height) {
for (offx = width * (x_repeat - 1); offx >= 0; offx -= width) {
char passstart = 0;
rewind(vector_file);
while (fgets((char *) buf, sizeof (buf), vector_file)) {
if (isalpha(*buf)) {
int x,
y;
if (!passstart) {
passstart = 1;
fprintf(pjl_file, "IN;");
fprintf(pjl_file, "XR%04d;", vector_freq);
fprintf(pjl_file, "YP%03d;", vector_power);
fprintf(pjl_file, "ZS%03d;", vector_speed);
}
switch (*buf) {
case 'M': // move
if (sscanf((char *) buf + 1, "%d,%d", &y, &x)
== 2) {
sx = x;
sy = y;
newline = 1;
}
break;
case 'C': // close - only makes sense after an "L"
if (newline == 0 && up == 0 && (lx != sx || ly
!= sy)) {
fprintf(pjl_file, ",%d,%d", basex + offx + sx +
HPGLX, basey + offy + sy + HPGLY);
}
break;
case 'P': // power
if (sscanf((char *)buf + 1, "%d", &x) == 1
&& x != power) {
int epower;
power = x;
if (!started) {
started = 1;
/* XXX disabled as current code path inserts
* this statement AFTER the IN; statement.
*/
/* start HPGL */
/* fprintf(pjl_file, "\e%%1B"); */
}
if (!up) {
fprintf(pjl_file, ";PU");
}
up = 1;
epower = (power * vector_power + 50) / 100;
if (vector_speed && vector_speed < 100) {
int espeed = vector_speed;
int efreq = vector_freq;
if (epower && x < 100) {
int r;
int q;
r = 10000 / x; // power, up to set power level (i.e. x=100)
q = 10000 / espeed;
if (q < r)
r = q;
q = 500000 / efreq;
if (q < r)
r = q;
epower = (50 + epower * r) / 100;
espeed = (50 + espeed * r) / 100;
efreq = (50 + espeed * r) / 100;
}
fprintf(pjl_file, ";ZS%03d;XR%04d;", espeed, efreq);
}
fprintf(pjl_file, ";YP%03d;", epower);
}
break;
case 'L': // line
if (!started) {
started = 1;
//fprintf(pjl_file, "\e%%1B;"); // start HPGL
}
if (newline) {
if (!up)
fprintf(pjl_file, ";");
fprintf(pjl_file, "PU%d,%d",
basex + offx + sx + HPGLX,
basey + offy + sy + HPGLY);
up = 1;
newline = 0;
}
if (up) {
fprintf(pjl_file, ";PD");
} else {
fprintf(pjl_file, ",");
}
up = 0;
if (sscanf ((char *) buf + 1, "%d,%d", &y, &x)
== 2) {
fprintf (pjl_file, "%d,%d", basex + offx + x +
HPGLX, basey + offy + y + HPGLY);
}
lx = x;
ly = y;
break;
}
if (*buf == 'X')
break;
}
}
}
}
if (started) {
if (up == 0)
fprintf(pjl_file, ";");
fprintf(pjl_file, "\e%%0B"); // end HLGL
}
fprintf(pjl_file, "\e%%1BPU"); // start HLGL, and pen up, end
return true;
}
/**
*
*/
static bool
generate_pjl(FILE *bitmap_file, FILE *pjl_file,
FILE *vector_file)
{
int i;
/* Print the printer job language header. */
fprintf(pjl_file, "\e%%-12345X@PJL JOB NAME=%s\r\n", job_title);
fprintf(pjl_file, "\eE@PJL ENTER LANGUAGE=PCL\r\n");
/* Set autofocus on or off. */
fprintf(pjl_file, "\e&y%dA", focus);
/* Left (long-edge) offset registration. Adjusts the position of the
* logical page across the width of the page.
*/
fprintf(pjl_file, "\e&l0U");
/* Top (short-edge) offset registration. Adjusts the position of the
* logical page across the length of the page.
*/
fprintf(pjl_file, "\e&l0Z");
/* Resolution of the print. */
fprintf(pjl_file, "\e&u%dD", resolution);
/* X position = 0 */
fprintf(pjl_file, "\e*p0X");
/* Y position = 0 */
fprintf(pjl_file, "\e*p0Y");
/* PCL resolution. */
fprintf(pjl_file, "\e*t%dR", resolution);
/* If raster power is enabled and raster mode is not 'n' then add that
* information to the print job.
*/
if (raster_power && raster_mode != 'n') {
/* FIXME unknown purpose. */
fprintf(pjl_file, "\e&y0C");
/* We're going to perform a raster print. */
generate_raster(pjl_file, bitmap_file);
}
/* If vector power is > 0 then add vector information to the print job. */
if (vector_power) {
fprintf(pjl_file, "\eE@PJL ENTER LANGUAGE=PCL\r\n");
/* Page Orientation */
fprintf(pjl_file, "\e*r0F");
fprintf(pjl_file, "\e*r%dT", height * y_repeat);
fprintf(pjl_file, "\e*r%dS", width * x_repeat);
fprintf(pjl_file, "\e*r1A");
fprintf(pjl_file, "\e*rC");
fprintf(pjl_file, "\e%%1B");
/* We're going to perform a vector print. */
generate_vector(pjl_file, vector_file);
}
/* Footer for printer job language. */
/* Reset */
fprintf(pjl_file, "\eE");
/* Exit language. */
fprintf(pjl_file, "\e%%-12345X");
/* End job. */
fprintf(pjl_file, "@PJL EOJ \r\n");
/* Pad out the remainder of the file with 0 characters. */
for(i = 0; i < 4096; i++) {
fputc(0, pjl_file);
}
return true;
}
/**
* Convert the given postscript file (ps) converting it to an encapsulated
* postscript file (eps).
*
* @param ps_file a file handle pointing to an opened postscript file that
* is to be converted.
* @param eps_file a file handle pointing to the opened encapsulated
* postscript file to store the result.
*
* @return Return true if the function completes its task, false otherwise.
*/
static bool
ps_to_eps(FILE *ps_file, FILE *eps_file)
{
int xoffset = 0;
int yoffset = 0;
int l;
while (fgets((char *)buf, sizeof (buf), ps_file)) {
fprintf(eps_file, "%s", (char *)buf);
if (*buf != '%') {
break;
}
if (!strncasecmp((char *) buf, "%%PageBoundingBox:", 18)) {
int lower_left_x;
int lower_left_y;
int upper_right_x;
int upper_right_y;
if (sscanf((char *)buf + 14, "%d %d %d %d",
&lower_left_x,
&lower_left_y,
&upper_right_x,
&upper_right_y) == 4) {
xoffset = lower_left_x;
yoffset = lower_left_y;
width = (upper_right_x - lower_left_x);
height = (upper_right_y - lower_left_y);
fprintf(eps_file, "/setpagedevice{pop}def\n"); // use bbox
if (xoffset || yoffset) {
fprintf(eps_file, "%d %d translate\n", -xoffset, -yoffset);
}
if (flip) {
fprintf(eps_file, "%d 0 translate -1 1 scale\n", width);
}
}
}
if (!strncasecmp((char *) buf, "%!", 2)) {
fprintf
(eps_file,
"/==={( )cvs print}def/stroke{currentrgbcolor 0.0 \
eq exch 0.0 eq and exch 0.0 ne and{(P)=== currentrgbcolor pop pop 100 mul \
round cvi = flattenpath{transform(M)=== round cvi ===(,)=== round cvi \
=}{transform(L)=== round cvi ===(,)=== round cvi =}{}{(C)=}pathforall \
newpath}{stroke}ifelse}bind def/showpage{(X)= showpage}bind def\n");
if (raster_mode != 'c' && raster_mode != 'g') {
if (screen == 0) {
fprintf(eps_file, "{0.5 ge{1}{0}ifelse}settransfer\n");
} else {
int s = screen;
if (resolution >= 600) {
// adjust for overprint
fprintf(eps_file,
"{dup 0 ne{%d %d div add}if}settransfer\n",
resolution / 600, s);
}
fprintf(eps_file, "%d 30{%s}setscreen\n", resolution / s,
(screen > 0) ? "pop abs 1 exch sub" :
"180 mul cos exch 180 mul cos add 2 div");
}
}
}
}
while ((l = fread ((char *) buf, 1, sizeof (buf), ps_file)) > 0) {
fwrite ((char *) buf, 1, l, eps_file);
}
return true;
}
/**
* The print job title can affect the functioning of the software. Job titles
* can take three forms:
* xRXxRYx specify that the execution of the job should repeat.
* cCXcCYc specify the center location for the print (in inches).
* nofocus if autofocus is to be disabled.
*
* @param the print job title.
* @return True if the system is able to process the job title, false otherwise.
*/
static bool
process_job_title_commands(char *title)
{
// xRXxRYx for repeat, cCXcCYc is centre (mm)
char *p = title;
char *d;
if (*p++ == 'x') {
if (isdigit(*p)) {
d = p;
while (isdigit(*p)) {
p++;