1 /* -*- c++ -*-
2 FILE: PostScriptWriter.cpp
3 RCS REVISION: $Revision: 1.3 $
4 
5 COPYRIGHT: (c) 1999 -- 2003 Melinda Green, Don Hatch, and Jay Berkenbilt - Superliminal Software
6 
7 LICENSE: Free to use and modify for non-commercial purposes as long as the
8     following conditions are adhered to:
9     1) Obvious credit for the source of this code and the designs it embodies
10        are clearly made, and
11     2) Ports and derived versions of 4D Magic Cube programs are not distributed
12        without the express written permission of the authors.
13 
14 DESCRIPTION:
15     Implementation of the PostScriptWriter class
16 */
17 
18 #include "PostScriptWriter.h"
19 
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 
24 #include "Puzzlest.h"
25 #include "PSProlog.h"
26 #include "Preferences.h"
27 
PostScriptWriter(Preferences & prefs,PuzzleState & ps,struct frame & uframe)28 PostScriptWriter::PostScriptWriter(Preferences& prefs,
29                                    PuzzleState& ps,
30                                    struct frame& uframe) :
31     preferences(prefs),
32     puzzle_state(ps),
33     frame(uframe)
34 {
35 }
36 
37 bool
generateOutput(char const * filename)38 PostScriptWriter::generateOutput(char const* filename)
39 {
40     FILE* file = fopen(filename, "w");
41     if (file == NULL)
42     {
43         fprintf(stderr, "unable to open %s: %s\n", filename, strerror(errno));
44         return false;
45     }
46 
47     // Generate image inside a 6'' square.
48 
49     double const sidelength = 72.0 * 6.0;
50 
51     fputs(psp_before, file);
52 
53     // Draw background
54     fprintf(file, "drawBackground\n");
55 
56     // Draw polygons
57 
58     bool do_outline = preferences.getBoolProperty(M4D_OUTLINE);
59     fprintf(file, "/outlines %s def\n", (do_outline ? "true" : "false"));
60 
61     int i;
62     for (i = 0; i < frame.nquads; ++i)
63     {
64         int j;
65         double x[4];
66         double y[4];
67 
68         for (j = 0; j < 4; ++j)
69         {
70             x[j] = sidelength * frame.verts[frame.quads[i][j]][X] / 65536.0;
71             y[j] = sidelength * frame.verts[frame.quads[i][j]][Y] / 65536.0;
72         }
73 
74         real brightness = frame.brightnesses[i];
75         int color = puzzle_state.idToColor((int)frame.quadids[i] / 6);
76 
77         fprintf(file, "%d %.2f ", color, brightness);
78         for (j = 0; j < 4; ++j)
79         {
80             fprintf(file, "%.2f %.2f ", x[j], y[j]);
81         }
82         fprintf(file, "drawPolygon\n");
83     }
84 
85     fputs(psp_after, file);
86 
87     fclose(file);
88 
89     return true;
90 }
91 
92 // Local Variables:
93 // c-basic-offset: 4
94 // c-comment-only-line-offset: 0
95 // c-file-offsets: ((defun-block-intro . +) (block-open . 0) (substatement-open . 0) (statement-cont . +) (statement-case-open . +4) (arglist-intro . +) (arglist-close . +) (inline-open . 0))
96 // indent-tabs-mode: nil
97 // End:
98