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 #include "view-cairo.hh"
28
29 #include <assert.h>
30
31
32 void
render(const font_options_t * font_opts)33 view_cairo_t::render (const font_options_t *font_opts)
34 {
35 bool vertical = HB_DIRECTION_IS_VERTICAL (direction);
36 int vert = vertical ? 1 : 0;
37 int horiz = vertical ? 0 : 1;
38
39 int x_sign = font_opts->font_size_x < 0 ? -1 : +1;
40 int y_sign = font_opts->font_size_y < 0 ? -1 : +1;
41
42 hb_font_t *font = font_opts->get_font();
43
44 view_options_t::font_extents_t extents = view_options.font_extents;
45 if (!view_options.have_font_extents)
46 {
47 hb_font_extents_t hb_extents;
48 hb_font_get_extents_for_direction (font, direction, &hb_extents);
49 extents.ascent = scalbn ((double) hb_extents.ascender, scale_bits);
50 extents.descent = -scalbn ((double) hb_extents.descender, scale_bits);
51 extents.line_gap = scalbn ((double) hb_extents.line_gap, scale_bits);
52 }
53
54 double ascent = y_sign * extents.ascent;
55 double descent = y_sign * extents.descent;
56 double line_gap = y_sign * extents.line_gap + view_options.line_space;
57 double leading = ascent + descent + line_gap;
58
59 /* Calculate surface size. */
60 double w = 0, h = 0;
61 (vertical ? w : h) = (int) lines->len * leading - (extents.line_gap + view_options.line_space);
62 (vertical ? h : w) = 0;
63 for (unsigned int i = 0; i < lines->len; i++) {
64 helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
65 double x_advance, y_advance;
66 line.get_advance (&x_advance, &y_advance);
67 if (vertical)
68 h = MAX (h, y_sign * y_advance);
69 else
70 w = MAX (w, x_sign * x_advance);
71 }
72
73 cairo_scaled_font_t *scaled_font = helper_cairo_create_scaled_font (font_opts);
74
75 /* See if font needs color. */
76 cairo_content_t content = CAIRO_CONTENT_ALPHA;
77 if (helper_cairo_scaled_font_has_color (scaled_font))
78 content = CAIRO_CONTENT_COLOR;
79
80 /* Create surface. */
81 cairo_t *cr = helper_cairo_create_context (w + view_options.margin.l + view_options.margin.r,
82 h + view_options.margin.t + view_options.margin.b,
83 &view_options, &output_options, content);
84 cairo_set_scaled_font (cr, scaled_font);
85
86 /* Setup coordinate system. */
87 cairo_translate (cr, view_options.margin.l, view_options.margin.t);
88 if (vertical)
89 cairo_translate (cr,
90 w - ascent, /* We currently always stack lines right to left */
91 y_sign < 0 ? h : 0);
92 else
93 {
94 cairo_translate (cr,
95 x_sign < 0 ? w : 0,
96 y_sign < 0 ? descent : ascent);
97 }
98
99 /* Draw. */
100 cairo_translate (cr, +vert * leading, -horiz * leading);
101 for (unsigned int i = 0; i < lines->len; i++)
102 {
103 helper_cairo_line_t &l = g_array_index (lines, helper_cairo_line_t, i);
104
105 cairo_translate (cr, -vert * leading, +horiz * leading);
106
107 if (view_options.annotate) {
108 cairo_save (cr);
109
110 /* Draw actual glyph origins */
111 cairo_set_source_rgba (cr, 1., 0., 0., .5);
112 cairo_set_line_width (cr, 5);
113 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
114 for (unsigned i = 0; i < l.num_glyphs; i++) {
115 cairo_move_to (cr, l.glyphs[i].x, l.glyphs[i].y);
116 cairo_rel_line_to (cr, 0, 0);
117 }
118 cairo_stroke (cr);
119
120 cairo_restore (cr);
121 }
122
123 if (0 && cairo_surface_get_type (cairo_get_target (cr)) == CAIRO_SURFACE_TYPE_IMAGE) {
124 /* cairo_show_glyphs() doesn't support subpixel positioning */
125 cairo_glyph_path (cr, l.glyphs, l.num_glyphs);
126 cairo_fill (cr);
127 } else if (l.num_clusters)
128 cairo_show_text_glyphs (cr,
129 l.utf8, l.utf8_len,
130 l.glyphs, l.num_glyphs,
131 l.clusters, l.num_clusters,
132 l.cluster_flags);
133 else
134 cairo_show_glyphs (cr, l.glyphs, l.num_glyphs);
135 }
136
137 /* Clean up. */
138 helper_cairo_destroy_context (cr);
139 cairo_scaled_font_destroy (scaled_font);
140 }
141