1 module ithox.qrcode.renderer.image.eps;
2 
3 import ithox.qrcode.renderer.image.abstractrender;
4 import ithox.qrcode.renderer.color.colorinterface;
5 import ithox.qrcode.renderer.color.cmyk;
6 import ithox.qrcode.renderer.color.rgb;
7 import ithox.qrcode.renderer.color.gray;
8 
9 import std.format, std.conv;
10 
11 /**
12 * EPS backend.
13 */
14 class Eps : AbstractRenderer
15 {
16 	/**
17 	* EPS string.
18 	*
19 	* @var string
20 	*/
21     protected string eps;
22     /**
23 	* Colors used for drawing.
24 	*
25 	* @var array
26 	*/
27     protected ColorInterface[string] colors ;
28 
29     /**
30 	* Current color.
31 	*
32 	* @var string
33 	*/
34     protected string currentColor;
35 
36 	/**
37 	* init(): defined by RendererInterface.
38 	*
39 	* @see    ImageRendererInterface::init()
40 	* @return void
41 	*/
42     public override void initRender()
43     {
44         this.eps = "%!PS-Adobe-3.0 EPSF-3.0\n"
45 			~ "%%BoundingBox: 0 0 " ~ to!string(this.finalWidth) ~ " " ~ to!string(this.finalHeight) ~ "\n"
46 				~ "/F { rectfill } def\n";
47     }
48 
49 	/**
50 	* addColor(): defined by RendererInterface.
51 	*
52 	* @see    ImageRendererInterface::addColor()
53 	* @param  string         id
54 	* @param  ColorInterface color
55 	* @return void
56 	*/
57     public override void addColor(string id, ColorInterface color)
58     {
59         if (
60             typeid(color) != typeid(Rgb)
61             && typeid(color) != typeid(Cmyk)
62             && typeid(color) !=  typeid(Gray)
63 			) {
64 				color = color.toCmyk();
65 			}
66         this.colors[id] = color;
67     }
68 
69 	/**
70 	* drawBackground(): defined by RendererInterface.
71 	*
72 	* @see    ImageRendererInterface::drawBackground()
73 	* @param  string $colorId
74 	* @return void
75 	*/
76     public override void drawBackground(string colorId)
77     {
78         this.setColor(colorId);
79         this.eps ~= "0 0 " ~ to!string(this.finalWidth) ~ " " ~ to!string(this.finalHeight) ~ " F\n";
80     }
81 
82 	/**
83 	* drawBlock(): defined by RendererInterface.
84 	*
85 	* @see    ImageRendererInterface::drawBlock()
86 	* @param  integer x
87 	* @param  integer y
88 	* @param  string  colorId
89 	* @return void
90 	*/
91     public override void drawBlock(int x, int y, string colorId)
92     {
93         this.setColor(colorId);
94         //this.eps ~= x  " " . (this.finalHeight - y) . " " . this.blockSize . " " . this.blockSize . " F\n";
95 		this.eps ~= format("%s %s %s %s F\n", x, this.finalHeight - y, this.blockSize, this.blockSize);
96     }
97 
98 
99     /**
100 	* getByteStream(): defined by RendererInterface.
101 	*
102 	* @see    ImageRendererInterface::getByteStream()
103 	* @return string
104 	*/
105     public override string getByteStream()
106     {
107         return this.eps;
108     }
109 
110 	/**
111 	* Sets color to use.
112 	*
113 	* @param  string colorId
114 	* @return void
115 	*/
116     protected void setColor(string colorId)
117     {
118         if (colorId != this.currentColor) 
119 		{
120             auto color = colorId in this.colors;
121 			if(color is null)
122 			{
123 				throw new Exception("Color id is not set "~colorId);
124 			}
125             if (typeid(*color) == typeid(Rgb)) {
126 				auto tmp = cast(Rgb)(*color);
127                 this.eps ~= format(
128 									"%F %F %F setrgbcolor\n",
129 									tmp.getRed() / 100,
130 									tmp.getGreen() / 100,
131 									tmp.getBlue() / 100
132 									);
133             } else if (typeid(*color) == typeid(Cmyk)) {
134 				auto tmp = cast(Cmyk)(*color);
135                 this.eps ~= format(
136 									"%F %F %F %F setcmykcolor\n",
137 									tmp.getCyan() / 100,
138 									tmp.getMagenta() / 100,
139 									tmp.getYellow() / 100,
140 									tmp.getBlack() / 100
141 									);
142             } else if (typeid(*color) == typeid(Gray)) {
143 				auto tmp = cast(Gray)(*color);
144                 this.eps ~= format(
145 									"%F setgray\n",
146 									tmp.getGray() / 100
147 									);
148             }
149             this.currentColor = colorId;
150         }
151     }
152 }