1 /*
2 * Copyright © 2011 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27 #ifndef VIEW_CAIRO_HH
28 #define VIEW_CAIRO_HH
29
30 #include "view-options.hh"
31 #include "output-options.hh"
32 #include "helper-cairo.hh"
33
34 struct view_cairo_t : view_options_t, output_options_t<>
35 {
~view_cairo_tview_cairo_t36 ~view_cairo_t ()
37 {
38 cairo_debug_reset_static_data ();
39 }
40
add_optionsview_cairo_t41 void add_options (option_parser_t *parser)
42 {
43 parser->set_summary ("View text with given font.");
44 view_options_t::add_options (parser);
45 output_options_t::add_options (parser, helper_cairo_supported_formats);
46 }
47
initview_cairo_t48 void init (hb_buffer_t *buffer, const font_options_t *font_opts)
49 {
50 lines = g_array_new (false, false, sizeof (helper_cairo_line_t));
51 subpixel_bits = font_opts->subpixel_bits;
52 }
new_lineview_cairo_t53 void new_line () {}
consume_textview_cairo_t54 void consume_text (hb_buffer_t *buffer,
55 const char *text,
56 unsigned int text_len,
57 hb_bool_t utf8_clusters) {}
errorview_cairo_t58 void error (const char *message)
59 { g_printerr ("%s: %s\n", g_get_prgname (), message); }
consume_glyphsview_cairo_t60 void consume_glyphs (hb_buffer_t *buffer,
61 const char *text,
62 unsigned int text_len,
63 hb_bool_t utf8_clusters)
64 {
65 direction = hb_buffer_get_direction (buffer);
66 helper_cairo_line_t l (text, text_len, buffer, utf8_clusters, subpixel_bits);
67 g_array_append_val (lines, l);
68 }
finishview_cairo_t69 void finish (hb_buffer_t *buffer, const font_options_t *font_opts)
70 {
71 render (font_opts);
72
73 for (unsigned int i = 0; i < lines->len; i++) {
74 helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
75 line.finish ();
76 }
77 #if GLIB_CHECK_VERSION (2, 22, 0)
78 g_array_unref (lines);
79 #else
80 g_array_free (lines, TRUE);
81 #endif
82 }
83
84 protected:
85
86 void render (const font_options_t *font_opts);
87
88 hb_direction_t direction = HB_DIRECTION_INVALID; // Remove this, make segment_properties accessible
89 GArray *lines = nullptr;
90 unsigned subpixel_bits = 0;
91 };
92
93 inline void
render(const font_options_t * font_opts)94 view_cairo_t::render (const font_options_t *font_opts)
95 {
96 bool vertical = HB_DIRECTION_IS_VERTICAL (direction);
97 int vert = vertical ? 1 : 0;
98 int horiz = vertical ? 0 : 1;
99
100 int x_sign = font_opts->font_size_x < 0 ? -1 : +1;
101 int y_sign = font_opts->font_size_y < 0 ? -1 : +1;
102
103 hb_font_t *font = font_opts->font;
104
105 if (!have_font_extents)
106 {
107 hb_font_extents_t hb_extents;
108 hb_font_get_extents_for_direction (font, direction, &hb_extents);
109 font_extents.ascent = scalbn ((double) hb_extents.ascender, - (int) subpixel_bits);
110 font_extents.descent = -scalbn ((double) hb_extents.descender, - (int) subpixel_bits);
111 font_extents.line_gap = scalbn ((double) hb_extents.line_gap, - (int) subpixel_bits);
112 have_font_extents = true;
113 }
114
115 double ascent = y_sign * font_extents.ascent;
116 double descent = y_sign * font_extents.descent;
117 double line_gap = y_sign * font_extents.line_gap + line_space;
118 double leading = ascent + descent + line_gap;
119
120 /* Calculate surface size. */
121 double w = 0, h = 0;
122 (vertical ? w : h) = (int) lines->len * leading - (font_extents.line_gap + line_space);
123 (vertical ? h : w) = 0;
124 for (unsigned int i = 0; i < lines->len; i++) {
125 helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
126 double x_advance, y_advance;
127 line.get_advance (&x_advance, &y_advance);
128 if (vertical)
129 h = MAX (h, y_sign * y_advance);
130 else
131 w = MAX (w, x_sign * x_advance);
132 }
133
134 cairo_scaled_font_t *scaled_font = helper_cairo_create_scaled_font (font_opts,
135 this);
136
137 /* See if font needs color. */
138 cairo_content_t content = CAIRO_CONTENT_ALPHA;
139 if (helper_cairo_scaled_font_has_color (scaled_font))
140 content = CAIRO_CONTENT_COLOR;
141
142 /* Create surface. */
143 cairo_t *cr = helper_cairo_create_context (w + margin.l + margin.r,
144 h + margin.t + margin.b,
145 this,
146 this,
147 content);
148 cairo_set_scaled_font (cr, scaled_font);
149
150 /* Setup coordinate system. */
151 cairo_translate (cr, margin.l, margin.t);
152 if (vertical)
153 cairo_translate (cr,
154 w - ascent, /* We currently always stack lines right to left */
155 y_sign < 0 ? h : 0);
156 else
157 {
158 cairo_translate (cr,
159 x_sign < 0 ? w : 0,
160 y_sign < 0 ? descent : ascent);
161 }
162
163 /* Draw. */
164 cairo_translate (cr, +vert * leading, -horiz * leading);
165 for (unsigned int i = 0; i < lines->len; i++)
166 {
167 helper_cairo_line_t &l = g_array_index (lines, helper_cairo_line_t, i);
168
169 cairo_translate (cr, -vert * leading, +horiz * leading);
170
171 if (show_extents)
172 {
173 cairo_save (cr);
174
175 cairo_set_source_rgba (cr, 1., 0., 0., .5);
176 cairo_set_line_width (cr, 10);
177 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
178 for (unsigned i = 0; i < l.num_glyphs; i++) {
179 cairo_move_to (cr, l.glyphs[i].x, l.glyphs[i].y);
180 cairo_rel_line_to (cr, 0, 0);
181 }
182 cairo_stroke (cr);
183
184 cairo_restore (cr);
185 cairo_save (cr);
186
187 cairo_set_source_rgba (cr, 1., 0., 1., .5);
188 cairo_set_line_width (cr, 3);
189 for (unsigned i = 0; i < l.num_glyphs; i++)
190 {
191 hb_glyph_extents_t hb_extents;
192 hb_font_get_glyph_extents (font, l.glyphs[i].index, &hb_extents);
193 double x1 = scalbn ((double) hb_extents.x_bearing, - (int) subpixel_bits);
194 double y1 = -scalbn ((double) hb_extents.y_bearing, - (int) subpixel_bits);
195 double width = scalbn ((double) hb_extents.width, - (int) subpixel_bits);
196 double height = -scalbn ((double) hb_extents.height, - (int) subpixel_bits);
197
198 cairo_rectangle (cr, l.glyphs[i].x + x1, l.glyphs[i].y + y1, width, height);
199 }
200 cairo_stroke (cr);
201
202 cairo_restore (cr);
203 }
204
205 // https://github.com/harfbuzz/harfbuzz/issues/4378
206 #if CAIRO_VERSION >= 11705
207 if (l.num_clusters)
208 cairo_show_text_glyphs (cr,
209 l.utf8, l.utf8_len,
210 l.glyphs, l.num_glyphs,
211 l.clusters, l.num_clusters,
212 l.cluster_flags);
213 else
214 #endif
215 cairo_show_glyphs (cr, l.glyphs, l.num_glyphs);
216 }
217
218 /* Clean up. */
219 helper_cairo_destroy_context (cr);
220 cairo_scaled_font_destroy (scaled_font);
221 }
222
223 #endif
224