1 /**
2  * This module contains some common utilities used by harud
3  */
4 module harud.util;
5 
6 import harud.doc : Doc;
7 import harud.font : Font;
8 import harud.types : Rect, HaruCMYKColor;
9 
10 /**
11  * Creates a rectangle starting from the top left corner.
12  *
13  * Params:
14  *  xLeft = X coordinates of left up corner
15  *  yTop = Y coordinates of left up corner
16  *  width = Width of rectangle
17  *  height = Height of rectangle
18  *
19  * Examples:
20  * --------------------
21  * (x, y)
22  *    *----------+
23  *    |          | height
24  *    +----------+
25  *       width
26  * --------------------
27  */
28 Rect createTopLeftRect(float xLeft, float yTop, float width, float height) {
29    return Rect(xLeft, yTop - height, xLeft + width, yTop);
30 }
31 
32 unittest {
33    Rect r = createTopLeftRect(25, 500, 150, 50);
34    assert(r.left == 25);
35    assert(r.top == 500);
36    assert(r.right == 25 + 150);
37    assert(r.bottom == 500 - 50);
38 }
39 
40 /**
41  * Creates a rectangle with the pdf convention.
42  *
43  * When describing a rectangle in PDF syntax, an array of four numbers is used.
44  * The order of the numbers is: left, bottom, width, height.
45  *
46  *
47  * Params:
48  *  xLeft = X coordinates of left up corner
49  *  yBottom = Y coordinates of left bottom corner
50  *  width = Width of rectangle
51  *  height = Height of rectangle
52  *
53  * Examples:
54  * --------------------
55  *             width
56  *         +----------+
57  *         |          | height
58  *         *----------+
59  * (left, bottom)
60  * --------------------
61  */
62 Rect createPdfRect(float xLeft, float yBottom, float width, float height) {
63    return Rect(xLeft, yBottom, xLeft + width, yBottom + height);
64 }
65 
66 unittest {
67    Rect r = createPdfRect(25, 200, 150, 50);
68    assert(r.left == 25);
69    assert(r.bottom == 200);
70    assert(r.right == 25 + 150);
71    assert(r.top == 200 + 50);
72 }
73 
74 Font getFont(Doc pdf, StandardFont font) {
75    final switch (font) with (StandardFont) {
76       case courier:
77          return pdf.getFont("Courier");
78       case courierBold:
79          return pdf.getFont("Courier-Bold");
80       case courierOblique:
81          return pdf.getFont("Courier-Oblique");
82       case courierBoldOblique:
83          return pdf.getFont("Courier-BoldOblique");
84       case helvetica:
85          return pdf.getFont("Helvetica");
86       case helveticaBold:
87          return pdf.getFont("Helvetica-Bold");
88       case helveticaOblique:
89          return pdf.getFont("Helvetica-Oblique");
90       case helveticaBoldOblique:
91          return pdf.getFont("Helvetica-BoldOblique");
92       case timesRoman:
93          return pdf.getFont("Times-Roman");
94       case timesBold:
95          return pdf.getFont("Times-Bold");
96       case timesItalic:
97          return pdf.getFont("Times-Italic");
98       case timesBoldItalic:
99          return pdf.getFont("Times-BoldItalic");
100       case symbol:
101          return pdf.getFont("Symbol");
102       case zapfDingbats:
103          return pdf.getFont("ZapfDingbats");
104 
105    }
106 }
107 
108 enum StandardFont {
109    courier,
110    courierBold,
111    courierOblique,
112    courierBoldOblique,
113    helvetica,
114    helveticaBold,
115    helveticaOblique,
116    helveticaBoldOblique,
117    timesRoman,
118    timesBold,
119    timesItalic,
120    timesBoldItalic,
121    symbol,
122    zapfDingbats
123 }
124 
125 enum MaterializeColor {
126    red
127 }
128 
129 HaruCMYKColor getColor(string c)() {
130    switch (c) {
131       case "red":
132          return HaruCMYKColor(0, 0.73, 0.78, 0.04);
133       case "pink":
134          return HaruCMYKColor(0, 0.87, 0.58, 0.09); //#e91e63":
135       case "purple":
136          return HaruCMYKColor(0.11, 0.78, 0., 0.31); //#9c27b0":
137       case "grey":
138          return HaruCMYKColor(0., 0., 0., 0.38);
139       case "grey-lighten-5":
140          return HaruCMYKColor(0., 0., 0., 0.02);
141       case "grey-lighten-4":
142          return HaruCMYKColor(0., 0., 0., 0.04);
143       case "grey-lighten-3":
144          return HaruCMYKColor(0., 0., 0., 0.07);
145       case "grey-lighten-2":
146          return HaruCMYKColor(0., 0., 0., 0.12);
147       case "grey-lighten-1":
148          return HaruCMYKColor(0., 0., 0., 0.26);
149       case "grey-darken-1":
150          return HaruCMYKColor(0., 0., 0., 0.54);
151       case "grey-darken-2":
152          return HaruCMYKColor(0., 0., 0., 0.62);
153       case "grey-darken-3":
154          return HaruCMYKColor(0., 0., 0., 0.74);
155       case "grey-darken-4":
156          return HaruCMYKColor(0., 0., 0., 0.87);
157       case "blue":
158          return HaruCMYKColor(0.86, 0.71, 0.0, 0.05); //#2196F3,
159       case "blue-lighten-5":
160          return HaruCMYKColor(0.10, 0.04, 0.0, 0.01); //#E3F2FD,
161       case "blue-lighten-4":
162          return HaruCMYKColor(0.25, 0.12, 0.0, 0.02); //#BBDEFB,
163       case "blue-lighten-3":
164          return HaruCMYKColor(0.42, 0.19, 0.0, 0.02); //#90CAF9,
165       case "blue-lighten-2":
166          return HaruCMYKColor(0.59, 0.26, 0.0, 0.04); //#64B5F6,
167       case "blue-lighten-1":
168          return HaruCMYKColor(0.73, 0.33, 0.0, 0.04); //#42A5F5,
169       case "blue-darken-1":
170          return HaruCMYKColor(0.87, 0.41, 0.0, 0.10); //#1E88E5,
171       case "blue-darken-2":
172          return HaruCMYKColor(0.88, 0.44, 0.0, 0.18); //#1976D2,
173       case "blue-darken-3":
174          return HaruCMYKColor(0.89, 0.47, 0.0, 0.25); //#1565C0,
175       case "blue-darken-4":
176          return HaruCMYKColor(0.92, 0.56, 0.0, 0.37); //#0D47A1,
177       case "blue-accent-1":
178          return HaruCMYKColor(0.49, 0.31, 0.0, 0.0); //#82B1FF,
179       case "blue-accent-2":
180          return HaruCMYKColor(0.73, 0.46, 0.0, 0.0); //#448AFF
181       case "blue-accent-3":
182          return HaruCMYKColor(0.85, 0.53, 0.0, 0.0); //#2979FF
183       case "blue-accent-4":
184          return HaruCMYKColor(0.84, 0.62, 0.0, 0.0); //#2962FF
185 
186       default:
187          assert(false);
188    }
189 }
190 
191 unittest {
192    assert(getColor!("red").yellow == cast(float)0.78);
193    assert(getColor!("grey-darken-2").key == cast(float)0.62);
194 }