/src/skia/third_party/externals/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 | | void init () |
40 | 0 | { |
41 | 0 | path_open = false; |
42 | 0 | min_x.set_int (INT_MAX); |
43 | 0 | min_y.set_int (INT_MAX); |
44 | 0 | max_x.set_int (INT_MIN); |
45 | 0 | max_y.set_int (INT_MIN); |
46 | 0 | } |
47 | | |
48 | 0 | void start_path () { path_open = true; } |
49 | 0 | void end_path () { path_open = false; } |
50 | 0 | bool is_path_open () const { return path_open; } |
51 | | |
52 | | void update_bounds (const point_t &pt) |
53 | 0 | { |
54 | 0 | if (pt.x < min_x) min_x = pt.x; |
55 | 0 | if (pt.x > max_x) max_x = pt.x; |
56 | 0 | if (pt.y < min_y) min_y = pt.y; |
57 | 0 | if (pt.y > max_y) max_y = pt.y; |
58 | 0 | } |
59 | | |
60 | | bool path_open; |
61 | | number_t min_x; |
62 | | number_t min_y; |
63 | | number_t max_x; |
64 | | number_t max_y; |
65 | | }; |
66 | | |
67 | | struct cff2_path_procs_extents_t : path_procs_t<cff2_path_procs_extents_t, cff2_cs_interp_env_t, cff2_extents_param_t> |
68 | | { |
69 | | static void moveto (cff2_cs_interp_env_t &env, cff2_extents_param_t& param, const point_t &pt) |
70 | 0 | { |
71 | 0 | param.end_path (); |
72 | 0 | env.moveto (pt); |
73 | 0 | } |
74 | | |
75 | | static void line (cff2_cs_interp_env_t &env, cff2_extents_param_t& param, const point_t &pt1) |
76 | 0 | { |
77 | 0 | if (!param.is_path_open ()) |
78 | 0 | { |
79 | 0 | param.start_path (); |
80 | 0 | param.update_bounds (env.get_pt ()); |
81 | 0 | } |
82 | 0 | env.moveto (pt1); |
83 | 0 | param.update_bounds (env.get_pt ()); |
84 | 0 | } |
85 | | |
86 | | static void curve (cff2_cs_interp_env_t &env, cff2_extents_param_t& param, const point_t &pt1, const point_t &pt2, const point_t &pt3) |
87 | 0 | { |
88 | 0 | if (!param.is_path_open ()) |
89 | 0 | { |
90 | 0 | param.start_path (); |
91 | 0 | param.update_bounds (env.get_pt ()); |
92 | 0 | } |
93 | | /* include control points */ |
94 | 0 | param.update_bounds (pt1); |
95 | 0 | param.update_bounds (pt2); |
96 | 0 | env.moveto (pt3); |
97 | 0 | param.update_bounds (env.get_pt ()); |
98 | 0 | } |
99 | | }; |
100 | | |
101 | | struct cff2_cs_opset_extents_t : cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, cff2_path_procs_extents_t> {}; |
102 | | |
103 | | bool OT::cff2::accelerator_t::get_extents (hb_font_t *font, |
104 | | hb_codepoint_t glyph, |
105 | | hb_glyph_extents_t *extents) const |
106 | 0 | { |
107 | | #ifdef HB_NO_OT_FONT_CFF |
108 | | /* XXX Remove check when this code moves to .hh file. */ |
109 | | return true; |
110 | | #endif |
111 | |
|
112 | 0 | if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; |
113 | | |
114 | 0 | unsigned int fd = fdSelect->get_fd (glyph); |
115 | 0 | cff2_cs_interpreter_t<cff2_cs_opset_extents_t, cff2_extents_param_t> interp; |
116 | 0 | const byte_str_t str = (*charStrings)[glyph]; |
117 | 0 | interp.env.init (str, *this, fd, font->coords, font->num_coords); |
118 | 0 | cff2_extents_param_t param; |
119 | 0 | param.init (); |
120 | 0 | if (unlikely (!interp.interpret (param))) return false; |
121 | | |
122 | 0 | if (param.min_x >= param.max_x) |
123 | 0 | { |
124 | 0 | extents->width = 0; |
125 | 0 | extents->x_bearing = 0; |
126 | 0 | } |
127 | 0 | else |
128 | 0 | { |
129 | 0 | extents->x_bearing = font->em_scalef_x (param.min_x.to_real ()); |
130 | 0 | extents->width = font->em_scalef_x (param.max_x.to_real ()) - extents->x_bearing; |
131 | 0 | } |
132 | 0 | if (param.min_y >= param.max_y) |
133 | 0 | { |
134 | 0 | extents->height = 0; |
135 | 0 | extents->y_bearing = 0; |
136 | 0 | } |
137 | 0 | else |
138 | 0 | { |
139 | 0 | extents->y_bearing = font->em_scalef_y (param.max_y.to_real ()); |
140 | 0 | extents->height = font->em_scalef_y (param.min_y.to_real ()) - extents->y_bearing; |
141 | 0 | } |
142 | |
|
143 | 0 | return true; |
144 | 0 | } |
145 | | |
146 | | #ifdef HB_EXPERIMENTAL_API |
147 | | struct cff2_path_param_t |
148 | | { |
149 | | cff2_path_param_t (hb_font_t *font_, draw_helper_t &draw_helper_) |
150 | | { |
151 | | draw_helper = &draw_helper_; |
152 | | font = font_; |
153 | | } |
154 | | |
155 | | void move_to (const point_t &p) |
156 | | { draw_helper->move_to (font->em_scalef_x (p.x.to_real ()), font->em_scalef_y (p.y.to_real ())); } |
157 | | |
158 | | void line_to (const point_t &p) |
159 | | { draw_helper->line_to (font->em_scalef_x (p.x.to_real ()), font->em_scalef_y (p.y.to_real ())); } |
160 | | |
161 | | void cubic_to (const point_t &p1, const point_t &p2, const point_t &p3) |
162 | | { |
163 | | draw_helper->cubic_to (font->em_scalef_x (p1.x.to_real ()), font->em_scalef_y (p1.y.to_real ()), |
164 | | font->em_scalef_x (p2.x.to_real ()), font->em_scalef_y (p2.y.to_real ()), |
165 | | font->em_scalef_x (p3.x.to_real ()), font->em_scalef_y (p3.y.to_real ())); |
166 | | } |
167 | | |
168 | | protected: |
169 | | draw_helper_t *draw_helper; |
170 | | hb_font_t *font; |
171 | | }; |
172 | | |
173 | | struct cff2_path_procs_path_t : path_procs_t<cff2_path_procs_path_t, cff2_cs_interp_env_t, cff2_path_param_t> |
174 | | { |
175 | | static void moveto (cff2_cs_interp_env_t &env, cff2_path_param_t& param, const point_t &pt) |
176 | | { |
177 | | param.move_to (pt); |
178 | | env.moveto (pt); |
179 | | } |
180 | | |
181 | | static void line (cff2_cs_interp_env_t &env, cff2_path_param_t& param, const point_t &pt1) |
182 | | { |
183 | | param.line_to (pt1); |
184 | | env.moveto (pt1); |
185 | | } |
186 | | |
187 | | static void curve (cff2_cs_interp_env_t &env, cff2_path_param_t& param, const point_t &pt1, const point_t &pt2, const point_t &pt3) |
188 | | { |
189 | | param.cubic_to (pt1, pt2, pt3); |
190 | | env.moveto (pt3); |
191 | | } |
192 | | }; |
193 | | |
194 | | struct cff2_cs_opset_path_t : cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, cff2_path_procs_path_t> {}; |
195 | | |
196 | | bool OT::cff2::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph, draw_helper_t &draw_helper) const |
197 | | { |
198 | | #ifdef HB_NO_OT_FONT_CFF |
199 | | /* XXX Remove check when this code moves to .hh file. */ |
200 | | return true; |
201 | | #endif |
202 | | |
203 | | if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; |
204 | | |
205 | | unsigned int fd = fdSelect->get_fd (glyph); |
206 | | cff2_cs_interpreter_t<cff2_cs_opset_path_t, cff2_path_param_t> interp; |
207 | | const byte_str_t str = (*charStrings)[glyph]; |
208 | | interp.env.init (str, *this, fd, font->coords, font->num_coords); |
209 | | cff2_path_param_t param (font, draw_helper); |
210 | | if (unlikely (!interp.interpret (param))) return false; |
211 | | return true; |
212 | | } |
213 | | #endif |
214 | | |
215 | | #endif |