1 /**
2  * Describes fonts objects
3  */
4 module harud.font;
5 
6 import harud.c.capi;
7 import harud.c.consts;
8 import harud.haruobject;
9 import harud.types;
10 import std.conv;
11 import std.string;
12 
13 /**
14  * Font class
15  */
16 class Font : IHaruObject {
17    protected HPDF_Font _font;
18 
19    this(HPDF_Font font) {
20       _font = font;
21    }
22 
23    /**
24     * Gets the name of the font.
25     *
26     * Returns:
27     *   the font name on success. Otherwise, returns null.
28     */
29    string getFontName() {
30       return to!string(HPDF_Font_GetFontName(this._font));
31    }
32 
33    /**
34     * Gets the encoding name of the font.
35     *
36     * Returns:
37     *   the encoding name of the font on success. Otherwise, returns null.
38     */
39    string getEncodingName() {
40       return to!string(HPDF_Font_GetEncodingName(this._font));
41    }
42 
43    /**
44     * Gets the width of a Unicode character in a specific font.
45     *
46     * Actual width of the character on the page can be calculated as follows:
47     *
48     * Examples:
49     * --------------------
50     *   char_width = font.getUnicodeWidth(UNICODE);
51     *   float actual_width = char_width * FONT_SIZE / 1000;
52     * --------------------
53     *
54     * Params:
55     *   code = A Unicode character.
56     *
57     * Returns:
58     *   the character width on success. Otherwise, returns null.
59     */
60    int getUnicodeWidth(HPDF_UNICODE code) {
61       return HPDF_Font_GetUnicodeWidth(this._font, code);
62    }
63 
64    /**
65     * Gets the bounding box of the font.
66     *
67     * Returns:
68     *   On success, returns Rect struct specifying the font bounding box.
69     *   Otherwise, returns a HaruBox struct of {0, 0, 0, 0}.
70     */
71    Rect getBBox() {
72       return HPDF_Font_GetBBox(this._font);
73    }
74 
75    /**
76     * Gets the vertical ascent of the font.
77     *
78     * Returns:
79     *   the font vertical ascent on success. Otherwise, returns 0.
80     */
81    int getAscent() {
82       return HPDF_Font_GetAscent(this._font);
83    }
84 
85    /**
86     * Gets the vertical descent of the font.
87     *
88     * Returns:
89     *   the font vertical descent on success. Otherwise, returns 0.
90     */
91    int getDescent() {
92       return HPDF_Font_GetDescent(this._font);
93    }
94 
95    /**
96     * Gets the distance from the baseline of lowercase letters.
97     *
98     * Returns:
99     *   the font x-height value on success. Otherwise, returns 0.
100     */
101    uint getXHeight() {
102       return HPDF_Font_GetXHeight(this._font);
103    }
104 
105    /**
106     * Gets the distance from the baseline of uppercase letters.
107     *
108     * Returns:
109     *   the font cap height on success. Otherwise, returns 0.
110     */
111    uint getCapHeight() {
112       return HPDF_Font_GetCapHeight(this._font);
113    }
114 
115    /**
116     * Gets total width of the text, number of characters, and number of words.
117     *
118     * Params:
119     *   text = The text to get width.
120     *   len = The byte length of the text.
121     *
122     * Returns:
123     *   On success, returns a TextWidth struct including calculation result.<br />
124     *   Otherwise, returns TextWidth struct whose attributes are all 0.
125     */
126    TextWidth textWidth(string text, uint len) {
127       return HPDF_Font_TextWidth(this._font, text.toStringz(), len);
128    }
129 
130    /**
131     * Calculates the byte length which can be included within the specified width.
132     *
133     * Params:
134     *  text = The text to use for calculation.
135     *  len = The length of the text.
136     *  width = The width of the area to put the text.
137     *  fontSize = The size of the font.
138     *  charSpace = The character spacing.
139     *  wordSpace = The word spacing.
140     *  wordWrap = Suppose there are three words: "ABCDE", "FGH", and "IJKL". Also, suppose the substring until "J" can be included within the width (12 bytes). If word_wrap is HPDF_FALSE the function returns 12. If word_wrap parameter is HPDF_TRUE, it returns 10 (the end of the previous word).
141     *  realWidth = If not NULL, parameter is set to the real width of the text. If NULL, parameter is ignored.
142     *
143     * Returns:
144     *   On success, returns byte length which can be included within specified width. Otherwise, returns 0.
145     */
146    uint measureText(string text, uint len, float width, float fontSize, float charSpace, float wordSpace,
147          bool wordWrap, float* realWidth) {
148       return HPDF_Font_MeasureText(this._font, text.toStringz, len, width, fontSize, charSpace, wordSpace, wordWrap
149             ? HPDF_TRUE : HPDF_FALSE, realWidth);
150    }
151 
152    HPDF_HANDLE getHandle() {
153       return _font;
154    }
155 }