summaryrefslogtreecommitdiffstats
path: root/gfx/harfbuzz/src/OT/glyf/path-builder.hh
blob: f5505245033877d89d13d95fa97c5f2523f5412f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef OT_GLYF_PATH_BUILDER_HH
#define OT_GLYF_PATH_BUILDER_HH


#include "../../hb.hh"


namespace OT {
namespace glyf_impl {


struct path_builder_t
{
  hb_font_t *font;
  hb_draw_session_t *draw_session;

  struct optional_point_t
  {
    optional_point_t () {}
    optional_point_t (float x_, float y_) : has_data (true), x (x_), y (y_) {}
    operator bool () const { return has_data; }

    bool has_data = false;
    float x;
    float y;

    optional_point_t mid (optional_point_t p)
    { return optional_point_t ((x + p.x) * 0.5f, (y + p.y) * 0.5f); }
  } first_oncurve, first_offcurve, first_offcurve2, last_offcurve, last_offcurve2;

  path_builder_t (hb_font_t *font_, hb_draw_session_t &draw_session_) :
    font (font_), draw_session (&draw_session_) {}

  /* based on https://github.com/RazrFalcon/ttf-parser/blob/4f32821/src/glyf.rs#L287
     See also:
     * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM01/Chap1.html
     * https://stackoverflow.com/a/20772557
     *
     * Cubic support added. */
  HB_ALWAYS_INLINE
  void consume_point (const contour_point_t &point)
  {
    bool is_on_curve = point.flag & glyf_impl::SimpleGlyph::FLAG_ON_CURVE;
#ifdef HB_NO_CUBIC_GLYF
    bool is_cubic = false;
#else
    bool is_cubic = !is_on_curve && (point.flag & glyf_impl::SimpleGlyph::FLAG_CUBIC);
#endif
    optional_point_t p (font->em_fscalef_x (point.x), font->em_fscalef_y (point.y));
    if (unlikely (!first_oncurve))
    {
      if (is_on_curve)
      {
	first_oncurve = p;
	draw_session->move_to (p.x, p.y);
      }
      else
      {
	if (is_cubic && !first_offcurve2)
	{
	  first_offcurve2 = first_offcurve;
	  first_offcurve = p;
	}
	else if (first_offcurve)
	{
	  optional_point_t mid = first_offcurve.mid (p);
	  first_oncurve = mid;
	  last_offcurve = p;
	  draw_session->move_to (mid.x, mid.y);
	}
	else
	  first_offcurve = p;
      }
    }
    else
    {
      if (last_offcurve)
      {
	if (is_on_curve)
	{
	  if (last_offcurve2)
	  {
	    draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
				    last_offcurve.x, last_offcurve.y,
				    p.x, p.y);
	    last_offcurve2 = optional_point_t ();
	  }
	  else
	    draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
				       p.x, p.y);
	  last_offcurve = optional_point_t ();
	}
	else
	{
	  if (is_cubic && !last_offcurve2)
	  {
	    last_offcurve2 = last_offcurve;
	    last_offcurve = p;
	  }
	  else
	  {
	    optional_point_t mid = last_offcurve.mid (p);

	    if (is_cubic)
	    {
	      draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
				      last_offcurve.x, last_offcurve.y,
				      mid.x, mid.y);
	      last_offcurve2 = optional_point_t ();
	    }
	    else
	      draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
					 mid.x, mid.y);
	    last_offcurve = p;
	  }
	}
      }
      else
      {
	if (is_on_curve)
	  draw_session->line_to (p.x, p.y);
	else
	  last_offcurve = p;
      }
    }

    if (unlikely (point.is_end_point))
    {
      if (first_offcurve && last_offcurve)
      {
	optional_point_t mid = last_offcurve.mid (first_offcurve2 ?
						  first_offcurve2 :
						  first_offcurve);
	if (last_offcurve2)
	  draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
				  last_offcurve.x, last_offcurve.y,
				  mid.x, mid.y);
	else
	  draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
				     mid.x, mid.y);
	last_offcurve = optional_point_t ();
      }
      /* now check the rest */

      if (first_offcurve && first_oncurve)
      {
        if (first_offcurve2)
	  draw_session->cubic_to (first_offcurve2.x, first_offcurve2.y,
				  first_offcurve.x, first_offcurve.y,
				  first_oncurve.x, first_oncurve.y);
	else
	  draw_session->quadratic_to (first_offcurve.x, first_offcurve.y,
				     first_oncurve.x, first_oncurve.y);
      }
      else if (last_offcurve && first_oncurve)
      {
	if (last_offcurve2)
	  draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
				  last_offcurve.x, last_offcurve.y,
				  first_oncurve.x, first_oncurve.y);
	else
	  draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
				     first_oncurve.x, first_oncurve.y);
      }
      else if (first_oncurve)
	draw_session->line_to (first_oncurve.x, first_oncurve.y);
      else if (first_offcurve)
      {
	float x = first_offcurve.x, y = first_offcurve.y;
	draw_session->move_to (x, y);
	draw_session->quadratic_to (x, y, x, y);
      }

      /* Getting ready for the next contour */
      first_oncurve = first_offcurve = last_offcurve = last_offcurve2 = optional_point_t ();
      draw_session->close_path ();
    }
  }
  void points_end () {}

  bool is_consuming_contour_points () { return true; }
  contour_point_t *get_phantoms_sink () { return nullptr; }
};


} /* namespace glyf_impl */
} /* namespace OT */


#endif /* OT_GLYF_PATH_BUILDER_HH */