Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/libefont/t1bounds.cc

raw
1 // -*- related-file-name: "../include/efont/t1bounds.hh" -*-
2
3 /* t1bounds.{cc,hh} -- charstring bounding box finder
4 *
5 * Copyright (c) 1998-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/t1bounds.hh>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <math.h>
23
24 namespace Efont {
25
26 CharstringBounds::CharstringBounds()
27 : _lb(UNKDOUBLE, UNKDOUBLE), _rt(UNKDOUBLE, UNKDOUBLE),
28 _last_xf_program(0)
29 {
30 }
31
32 CharstringBounds::CharstringBounds(const Transform& nonfont_xf)
33 : _lb(UNKDOUBLE, UNKDOUBLE), _rt(UNKDOUBLE, UNKDOUBLE),
34 _nonfont_xf(nonfont_xf), _last_xf_program(0)
35 {
36 }
37
38 CharstringBounds::CharstringBounds(const Transform &nonfont_xf, const Vector<double> &weight)
39 : CharstringInterp(weight),
40 _lb(UNKDOUBLE, UNKDOUBLE), _rt(UNKDOUBLE, UNKDOUBLE),
41 _nonfont_xf(nonfont_xf), _last_xf_program(0)
42 {
43 }
44
45 void
46 CharstringBounds::clear()
47 {
48 _lb = _rt = Point(UNKDOUBLE, UNKDOUBLE);
49 _width = Point(0, 0);
50 }
51
52 void
53 CharstringBounds::xf_mark(const Bezier &b)
54 {
55 Bezier b1, b2;
56 b.halve(b1, b2);
57 xf_mark(b1.point(3));
58 if (!xf_controls_inside(b1))
59 xf_mark(b1);
60 if (!xf_controls_inside(b2))
61 xf_mark(b2);
62 }
63
64 void
65 CharstringBounds::act_width(int, const Point &w)
66 {
67 _width = w * _xf;
68 }
69
70 void
71 CharstringBounds::act_line(int, const Point &p0, const Point &p1)
72 {
73 mark(p0);
74 mark(p1);
75 }
76
77 void
78 CharstringBounds::act_curve(int, const Point &p0, const Point &p1, const Point &p2, const Point &p3)
79 {
80 Point q0 = p0 * _xf;
81 Point q1 = p1 * _xf;
82 Point q2 = p2 * _xf;
83 Point q3 = p3 * _xf;
84
85 xf_mark(q0);
86 xf_mark(q3);
87
88 if (!xf_inside(q1) || !xf_inside(q2)) {
89 Bezier b(q0, q1, q2, q3);
90 xf_mark(b);
91 }
92 }
93
94 void
95 CharstringBounds::set_xf(const CharstringProgram *program)
96 {
97 if (_last_xf_program != program) {
98 _last_xf_program = program;
99 double matrix[6];
100 program->font_matrix(matrix);
101 Transform font_xf = Transform(matrix).scaled(program->units_per_em());
102 font_xf.check_null(0.001);
103 _xf = _nonfont_xf * font_xf;
104 }
105 }
106
107 bool
108 CharstringBounds::char_bounds(const CharstringContext &g, bool shift)
109 {
110 set_xf(g.program);
111 CharstringInterp::interpret(g);
112 if (shift) {
113 _xf.raw_translate(_width - _xf.translation());
114 _nonfont_xf.raw_translate(_width - _nonfont_xf.translation());
115 _width = Point(0, 0);
116 }
117 return error() >= 0;
118 }
119
120 void
121 CharstringBounds::translate(double dx, double dy)
122 {
123 _xf.translate(dx, dy);
124 _nonfont_xf.translate(dx, dy);
125 }
126
127 bool
128 CharstringBounds::output(double bb[4], double& width, bool use_cur_width) const
129 {
130 if (!KNOWN(_lb.x))
131 bb[0] = bb[1] = bb[2] = bb[3] = 0;
132 else {
133 bb[0] = _lb.x;
134 bb[1] = _lb.y;
135 bb[2] = _rt.x;
136 bb[3] = _rt.y;
137 }
138 if (use_cur_width)
139 width = _width.x;
140 else {
141 Point p = Point(0, 0) * _xf;
142 width = p.x;
143 }
144 return error() >= 0;
145 }
146
147 bool
148 CharstringBounds::bounds(const Transform& transform, const CharstringContext& g,
149 double bb[4], double& width)
150 {
151 CharstringBounds b(transform);
152 b.char_bounds(g, false);
153 return b.output(bb, width, true);
154 }
155
156 bool
157 CharstringBounds::bounds(const CharstringContext &g,
158 double bb[4], double& width)
159 {
160 CharstringBounds b;
161 b.char_bounds(g, false);
162 return b.output(bb, width, true);
163 }
164
165 } // namespace Efont