/src/harfbuzz/src/hb-ot-cff2-table.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2018 Adobe 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 | | * Adobe Author(s): Michiharu Ariza |
25 | | */ |
26 | | |
27 | | #include "hb.hh" |
28 | | |
29 | | #ifndef HB_NO_OT_FONT_CFF |
30 | | |
31 | | #include "hb-ot-cff2-table.hh" |
32 | | #include "hb-cff2-interp-cs.hh" |
33 | | #include "hb-draw.hh" |
34 | | |
35 | | using namespace CFF; |
36 | | |
37 | | struct cff2_extents_param_t |
38 | | { |
39 | | cff2_extents_param_t () |
40 | 35.4k | { |
41 | 35.4k | min_x.set_int (INT_MAX); |
42 | 35.4k | min_y.set_int (INT_MAX); |
43 | 35.4k | max_x.set_int (INT_MIN); |
44 | 35.4k | max_y.set_int (INT_MIN); |
45 | 35.4k | } |
46 | | |
47 | 235k | void start_path () { path_open = true; } |
48 | 276k | void end_path () { path_open = false; } |
49 | 8.42M | bool is_path_open () const { return path_open; } |
50 | | |
51 | | void update_bounds (const point_t &pt) |
52 | 16.4M | { |
53 | 16.4M | if (pt.x < min_x) min_x = pt.x; |
54 | 16.4M | if (pt.x > max_x) max_x = pt.x; |
55 | 16.4M | if (pt.y < min_y) min_y = pt.y; |
56 | 16.4M | if (pt.y > max_y) max_y = pt.y; |
57 | 16.4M | } |
58 | | |
59 | | bool path_open = false; |
60 | | number_t min_x; |
61 | | number_t min_y; |
62 | | number_t max_x; |
63 | | number_t max_y; |
64 | | }; |
65 | | |
66 | | struct cff2_path_procs_extents_t : path_procs_t<cff2_path_procs_extents_t, cff2_cs_interp_env_t<number_t>, cff2_extents_param_t> |
67 | | { |
68 | | static void moveto (cff2_cs_interp_env_t<number_t> &env, cff2_extents_param_t& param, const point_t &pt) |
69 | 276k | { |
70 | 276k | param.end_path (); |
71 | 276k | env.moveto (pt); |
72 | 276k | } |
73 | | |
74 | | static void line (cff2_cs_interp_env_t<number_t> &env, cff2_extents_param_t& param, const point_t &pt1) |
75 | 4.51M | { |
76 | 4.51M | if (!param.is_path_open ()) |
77 | 158k | { |
78 | 158k | param.start_path (); |
79 | 158k | param.update_bounds (env.get_pt ()); |
80 | 158k | } |
81 | 4.51M | env.moveto (pt1); |
82 | 4.51M | param.update_bounds (env.get_pt ()); |
83 | 4.51M | } |
84 | | |
85 | | static void curve (cff2_cs_interp_env_t<number_t> &env, cff2_extents_param_t& param, const point_t &pt1, const point_t &pt2, const point_t &pt3) |
86 | 3.91M | { |
87 | 3.91M | if (!param.is_path_open ()) |
88 | 77.1k | { |
89 | 77.1k | param.start_path (); |
90 | 77.1k | param.update_bounds (env.get_pt ()); |
91 | 77.1k | } |
92 | | /* include control points */ |
93 | 3.91M | param.update_bounds (pt1); |
94 | 3.91M | param.update_bounds (pt2); |
95 | 3.91M | env.moveto (pt3); |
96 | 3.91M | param.update_bounds (env.get_pt ()); |
97 | 3.91M | } |
98 | | }; |
99 | | |
100 | | struct cff2_cs_opset_extents_t : cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, number_t, cff2_path_procs_extents_t> {}; |
101 | | |
102 | | bool OT::cff2::accelerator_t::get_extents (hb_font_t *font, |
103 | | hb_codepoint_t glyph, |
104 | | hb_glyph_extents_t *extents) const |
105 | 591k | { |
106 | | #ifdef HB_NO_OT_FONT_CFF |
107 | | /* XXX Remove check when this code moves to .hh file. */ |
108 | | return true; |
109 | | #endif |
110 | | |
111 | 591k | if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; |
112 | | |
113 | 35.4k | unsigned int fd = fdSelect->get_fd (glyph); |
114 | 35.4k | const hb_ubytes_t str = (*charStrings)[glyph]; |
115 | 35.4k | cff2_cs_interp_env_t<number_t> env (str, *this, fd, font->coords, font->num_coords); |
116 | 35.4k | cff2_cs_interpreter_t<cff2_cs_opset_extents_t, cff2_extents_param_t, number_t> interp (env); |
117 | 35.4k | cff2_extents_param_t param; |
118 | 35.4k | if (unlikely (!interp.interpret (param))) return false; |
119 | | |
120 | 14.7k | if (param.min_x >= param.max_x) |
121 | 2.06k | { |
122 | 2.06k | extents->width = 0; |
123 | 2.06k | extents->x_bearing = 0; |
124 | 2.06k | } |
125 | 12.6k | else |
126 | 12.6k | { |
127 | 12.6k | extents->x_bearing = roundf (param.min_x.to_real ()); |
128 | 12.6k | extents->width = roundf (param.max_x.to_real () - extents->x_bearing); |
129 | 12.6k | } |
130 | 14.7k | if (param.min_y >= param.max_y) |
131 | 2.02k | { |
132 | 2.02k | extents->height = 0; |
133 | 2.02k | extents->y_bearing = 0; |
134 | 2.02k | } |
135 | 12.7k | else |
136 | 12.7k | { |
137 | 12.7k | extents->y_bearing = roundf (param.max_y.to_real ()); |
138 | 12.7k | extents->height = roundf (param.min_y.to_real () - extents->y_bearing); |
139 | 12.7k | } |
140 | | |
141 | 14.7k | font->scale_glyph_extents (extents); |
142 | | |
143 | 14.7k | return true; |
144 | 35.4k | } |
145 | | |
146 | | bool OT::cff2::accelerator_t::paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data, hb_color_t foreground) const |
147 | 0 | { |
148 | 0 | funcs->push_clip_glyph (data, glyph, font); |
149 | 0 | funcs->color (data, true, foreground); |
150 | 0 | funcs->pop_clip (data); |
151 | |
|
152 | 0 | return true; |
153 | 0 | } |
154 | | |
155 | | struct cff2_path_param_t |
156 | | { |
157 | | cff2_path_param_t (hb_font_t *font_, hb_draw_session_t &draw_session_) |
158 | 643k | { |
159 | 643k | draw_session = &draw_session_; |
160 | 643k | font = font_; |
161 | 643k | } |
162 | | |
163 | | void move_to (const point_t &p) |
164 | 779k | { draw_session->move_to (font->em_fscalef_x (p.x.to_real ()), font->em_fscalef_y (p.y.to_real ())); } |
165 | | |
166 | | void line_to (const point_t &p) |
167 | 3.88M | { draw_session->line_to (font->em_fscalef_x (p.x.to_real ()), font->em_fscalef_y (p.y.to_real ())); } |
168 | | |
169 | | void cubic_to (const point_t &p1, const point_t &p2, const point_t &p3) |
170 | 7.60M | { |
171 | 7.60M | draw_session->cubic_to (font->em_fscalef_x (p1.x.to_real ()), font->em_fscalef_y (p1.y.to_real ()), |
172 | 7.60M | font->em_fscalef_x (p2.x.to_real ()), font->em_fscalef_y (p2.y.to_real ()), |
173 | 7.60M | font->em_fscalef_x (p3.x.to_real ()), font->em_fscalef_y (p3.y.to_real ())); |
174 | 7.60M | } |
175 | | |
176 | | protected: |
177 | | hb_draw_session_t *draw_session; |
178 | | hb_font_t *font; |
179 | | }; |
180 | | |
181 | | struct cff2_path_procs_path_t : path_procs_t<cff2_path_procs_path_t, cff2_cs_interp_env_t<number_t>, cff2_path_param_t> |
182 | | { |
183 | | static void moveto (cff2_cs_interp_env_t<number_t> &env, cff2_path_param_t& param, const point_t &pt) |
184 | 779k | { |
185 | 779k | param.move_to (pt); |
186 | 779k | env.moveto (pt); |
187 | 779k | } |
188 | | |
189 | | static void line (cff2_cs_interp_env_t<number_t> &env, cff2_path_param_t& param, const point_t &pt1) |
190 | 3.88M | { |
191 | 3.88M | param.line_to (pt1); |
192 | 3.88M | env.moveto (pt1); |
193 | 3.88M | } |
194 | | |
195 | | static void curve (cff2_cs_interp_env_t<number_t> &env, cff2_path_param_t& param, const point_t &pt1, const point_t &pt2, const point_t &pt3) |
196 | 7.60M | { |
197 | 7.60M | param.cubic_to (pt1, pt2, pt3); |
198 | 7.60M | env.moveto (pt3); |
199 | 7.60M | } |
200 | | }; |
201 | | |
202 | | struct cff2_cs_opset_path_t : cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, number_t, cff2_path_procs_path_t> {}; |
203 | | |
204 | | bool OT::cff2::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session) const |
205 | 2.04M | { |
206 | | #ifdef HB_NO_OT_FONT_CFF |
207 | | /* XXX Remove check when this code moves to .hh file. */ |
208 | | return true; |
209 | | #endif |
210 | | |
211 | 2.04M | if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; |
212 | | |
213 | 643k | unsigned int fd = fdSelect->get_fd (glyph); |
214 | 643k | const hb_ubytes_t str = (*charStrings)[glyph]; |
215 | 643k | cff2_cs_interp_env_t<number_t> env (str, *this, fd, font->coords, font->num_coords); |
216 | 643k | cff2_cs_interpreter_t<cff2_cs_opset_path_t, cff2_path_param_t, number_t> interp (env); |
217 | 643k | cff2_path_param_t param (font, draw_session); |
218 | 643k | if (unlikely (!interp.interpret (param))) return false; |
219 | 345k | return true; |
220 | 643k | } |
221 | | |
222 | | #endif |