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 
34 using namespace CFF;
35 
36 struct cff2_extents_param_t
37 {
initcff2_extents_param_t38   void init ()
39   {
40     path_open = false;
41     min_x.set_int (INT_MAX);
42     min_y.set_int (INT_MAX);
43     max_x.set_int (INT_MIN);
44     max_y.set_int (INT_MIN);
45   }
46 
start_pathcff2_extents_param_t47   void   start_path ()       { path_open = true; }
end_pathcff2_extents_param_t48   void     end_path ()       { path_open = false; }
is_path_opencff2_extents_param_t49   bool is_path_open () const { return path_open; }
50 
update_boundscff2_extents_param_t51   void update_bounds (const point_t &pt)
52   {
53     if (pt.x < min_x) min_x = pt.x;
54     if (pt.x > max_x) max_x = pt.x;
55     if (pt.y < min_y) min_y = pt.y;
56     if (pt.y > max_y) max_y = pt.y;
57   }
58 
59   bool  path_open;
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, cff2_extents_param_t>
67 {
movetocff2_path_procs_extents_t68   static void moveto (cff2_cs_interp_env_t &env, cff2_extents_param_t& param, const point_t &pt)
69   {
70     param.end_path ();
71     env.moveto (pt);
72   }
73 
linecff2_path_procs_extents_t74   static void line (cff2_cs_interp_env_t &env, cff2_extents_param_t& param, const point_t &pt1)
75   {
76     if (!param.is_path_open ())
77     {
78       param.start_path ();
79       param.update_bounds (env.get_pt ());
80     }
81     env.moveto (pt1);
82     param.update_bounds (env.get_pt ());
83   }
84 
curvecff2_path_procs_extents_t85   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)
86   {
87     if (!param.is_path_open ())
88     {
89       param.start_path ();
90       param.update_bounds (env.get_pt ());
91     }
92     /* include control points */
93     param.update_bounds (pt1);
94     param.update_bounds (pt2);
95     env.moveto (pt3);
96     param.update_bounds (env.get_pt ());
97   }
98 };
99 
100 struct cff2_cs_opset_extents_t : cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, cff2_path_procs_extents_t> {};
101 
get_extents(hb_font_t * font,hb_codepoint_t glyph,hb_glyph_extents_t * extents) const102 bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
103 					   hb_codepoint_t glyph,
104 					   hb_glyph_extents_t *extents) const
105 {
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   if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false;
112 
113   unsigned int fd = fdSelect->get_fd (glyph);
114   cff2_cs_interpreter_t<cff2_cs_opset_extents_t, cff2_extents_param_t> interp;
115   const byte_str_t str = (*charStrings)[glyph];
116   interp.env.init (str, *this, fd, font->coords, font->num_coords);
117   cff2_extents_param_t  param;
118   param.init ();
119   if (unlikely (!interp.interpret (param))) return false;
120 
121   if (param.min_x >= param.max_x)
122   {
123     extents->width = 0;
124     extents->x_bearing = 0;
125   }
126   else
127   {
128     extents->x_bearing = font->em_scalef_x (param.min_x.to_real ());
129     extents->width = font->em_scalef_x (param.max_x.to_real () - param.min_x.to_real ());
130   }
131   if (param.min_y >= param.max_y)
132   {
133     extents->height = 0;
134     extents->y_bearing = 0;
135   }
136   else
137   {
138     extents->y_bearing = font->em_scalef_y (param.max_y.to_real ());
139     extents->height = font->em_scalef_y (param.min_y.to_real () - param.max_y.to_real ());
140   }
141 
142   return true;
143 }
144 
145 
146 #endif
147