1 module qrcode.renderer.text.plain;
2 
3 import qrcode.renderer.rendererinterface;
4 import qrcode.encoder.qrcode;
5 import qrcode.renderer.rendererinterface;
6 import qrcode.renderer.color.colorinterface;
7 
8 /**
9 * Plaintext renderer.
10 */
11 class Plain : RendererInterface
12 {
13 	/**
14 	* Margin around the QR code, also known as quiet zone.
15 	*
16 	* @var integer
17 	*/
18     protected int margin = 1;
19     /**
20 	* Char used for full block.
21 	*
22 	* UTF-8 FULL BLOCK (U+2588)
23 	*
24 	* @var  string
25 	* @link http://www.fileformat.info/info/unicode/char/2588/index.htm
26 	*/
27     protected string fullBlock = "\xE2\x96\x88";
28     /**
29 	* Char used for empty space
30 	*
31 	* @var string
32 	*/
33     protected string emptyBlock = " ";
34 
35 	/**
36 	* Set char used as full block (occupied space, "black").
37 	*
38 	* @param string fullBlock
39 	*/
40     public void setFullBlock(string fullBlock)
41     {
42         this.fullBlock = fullBlock;
43     }
44     /**
45 	* Get char used as full block (occupied space, "black").
46 	*
47 	* @return string
48 	*/
49     public string getFullBlock()
50     {
51         return this.fullBlock;
52     }
53     /**
54 	* Set char used as empty block (empty space, "white").
55 	*
56 	* @param string emptyBlock
57 	*/
58     public void setEmptyBlock(string emptyBlock)
59     {
60         this.emptyBlock = emptyBlock;
61     }
62     /**
63 	* Get char used as empty block (empty space, "white").
64 	*
65 	* @return string
66 	*/
67     public string getEmptyBlock()
68     {
69         return this.emptyBlock;
70     }
71     /**
72 	* Sets the margin around the QR code.
73 	*
74 	* @param  integer margin
75 	* @return AbstractRenderer
76 	* @throws Exception\InvalidArgumentException
77 	*/
78     public RendererInterface setMargin(int margin)
79     {
80         if (margin < 0) {
81             throw new Exception("Margin must be equal to greater than 0");
82         }
83         this.margin = margin;
84         return this;
85     }
86     /**
87 	* Gets the margin around the QR code.
88 	*
89 	* @return integer
90 	*/
91     public int getMargin()
92     {
93         return this.margin;
94     }
95     /**
96 	* render(): defined by RendererInterface.
97 	*
98 	* @see    RendererInterface::render()
99 	* @param  QrCode qrCode
100 	* @return string
101 	*/
102     public string render(QrCode qrCode)
103     {
104 		import std.array;
105         Appender!string result = appender!string();
106         auto matrix = qrCode.matrix();
107         auto width  = matrix.width();
108         // Top margin
109         for (auto x = 0; x < this.margin; x++) {
110             //result .= str_repeat(this.emptyBlock, width + 2 * this.margin)."\n";
111 			result.put(replicate(this.emptyBlock, width + 2 * this.margin));
112 			result.put("\n");
113         }
114         // Body
115         auto array = matrix.getArray();
116         foreach (row ; array) {
117             //result .= str_repeat(this.emptyBlock, this.margin); // left margin
118 			result.put(replicate(this.emptyBlock, this.margin));
119             foreach (_byte ; row) {
120                 //result .= byte ? this.fullBlock : this.emptyBlock;
121 				result.put(_byte ? this.fullBlock : this.emptyBlock);
122             }
123             //result .= str_repeat(this.emptyBlock, this.margin); // right margin
124 			result.put(replicate(this.emptyBlock, this.margin));
125            // result .= "\n";
126 			result.put("\n");
127         }
128         // Bottom margin
129         for (auto x = 0; x < this.margin; x++) {
130 			// result .= str_repeat(this.emptyBlock, width + 2 * this.margin)."\n";
131 			result.put(replicate(this.emptyBlock, width + 2 * this.margin));
132 			result.put("\n");
133         }
134         return result.data;
135     }
136 
137 	public void initRender(){};
138 	public void addColor(string id, ColorInterface color){}
139 	public void drawBackground(string colorId){}
140 	public void drawBlock(int x, int y, string colorId){}
141 	public string getByteStream(){
142 		return string.init;
143 	}
144 }