-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBuilder.cc
More file actions
456 lines (405 loc) · 8.59 KB
/
TextBuilder.cc
File metadata and controls
456 lines (405 loc) · 8.59 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include "TextBuilder.hh"
#include <qpdf/QPDF.hh>
#include <qpdf/QUtil.hh>
#include <sstream>
#include <locale>
StringOrDouble::StringOrDouble(std::string const& str) :
str(str),
d(0.0),
is_string(true)
{
}
StringOrDouble::StringOrDouble(double d) :
d(d),
is_string(false)
{
}
bool
StringOrDouble::isString() const
{
return this->is_string;
}
double
StringOrDouble::getDouble() const
{
return this->d;
}
std::string const&
StringOrDouble::getString() const
{
return this->str;
}
std::string
StringOrDouble::getValue() const
{
if (is_string)
{
return QPDFObjectHandle::newString(str).unparse();
}
else
{
return QUtil::double_to_string(d, 2);
}
}
TextBuilder::NoLineSaver::NoLineSaver(int& no_lines) :
no_lines(no_lines)
{
++this->no_lines;
}
TextBuilder::NoLineSaver::~NoLineSaver()
{
--this->no_lines;
}
TextBuilder::StateSaver::StateSaver(State& state) :
state(state),
saved_state(state)
{
}
TextBuilder::StateSaver::~StateSaver()
{
state = saved_state;
}
TextBuilder::TextBuilder(QPDF& qpdf, QPDFObjectHandle const& fonts) :
qpdf(qpdf),
fonts(fonts),
no_line_count(0)
{
}
TextBuilder::State::State() :
T_c(0.0),
T_w(0.0),
T_h(1.0),
T_l(0.0),
T_fs(0.0),
T_rise(0.0)
{
}
std::string
TextBuilder::getContents()
{
std::string result = "BT\n";
for (auto const& line: state.lines)
{
result += " " + line + "\n";
}
result += "ET\n";
return result;
}
void
TextBuilder::appendLine(std::string const& line)
{
if (this->no_line_count == 0)
{
state.lines.push_back(line);
}
}
void
TextBuilder::appendLine(std::function<void(std::ostringstream&)> fn)
{
if (this->no_line_count == 0)
{
std::ostringstream buf;
buf.imbue(std::locale::classic());
fn(buf);
state.lines.push_back(buf.str());
}
}
void
TextBuilder::withNoLines(std::function<void()> fn)
{
NoLineSaver n(this->no_line_count);
fn();
}
void
TextBuilder::saveState(std::function<void()> fn)
{
// XXX StateSaver copies lines, which is more expensive than
// necessary. We could avoid this by using a NoLineSaver, pulling
// lines out of state, and handling lines separately, but this
// won't work if we add transactional (commit/rollback)
// capability later.
StateSaver s(this->state);
fn();
}
void
TextBuilder::assertFont()
{
if (! state.T_f.isInitialized())
{
throw std::runtime_error("no font has been set");
}
}
void
TextBuilder::getGlobalFontFields(double& ascent, double& descent)
{
assertFont();
auto font_descriptor = state.T_f.getKey("/FontDescriptor");
if (! font_descriptor.isDictionary())
{
throw std::runtime_error(
"unable to get FontDescriptor for current font");
}
auto ascent_oh = font_descriptor.getKey("/Ascent");
auto descent_oh = font_descriptor.getKey("/Descent");
if (! (ascent_oh.isNumber() && descent_oh.isNumber()))
{
throw std::runtime_error(
"unable to get fields for current font");
}
ascent = ascent_oh.getNumericValue() / 1000.0;
descent = descent_oh.getNumericValue() / 1000.0;
}
double
TextBuilder::getCharWidth(char ch)
{
assertFont();
auto first_char_oh = state.T_f.getKey("/FirstChar");
auto last_char_oh = state.T_f.getKey("/LastChar");
auto widths = state.T_f.getKey("/Widths");
if (! (first_char_oh.isInteger() &&
last_char_oh.isInteger() &&
widths.isArray()))
{
throw std::runtime_error("unable to get width information from font");
}
int first_char = first_char_oh.getIntValueAsInt();
int last_char = last_char_oh.getIntValueAsInt();
int uch = static_cast<int>(static_cast<unsigned char>(ch));
if ((uch < first_char) || (uch > last_char))
{
throw std::runtime_error("no width information for character value " +
QUtil::int_to_string(uch));
}
return widths.getArrayItem(uch - first_char).getNumericValue() / 1000.0;
}
double
TextBuilder:: getT_c() const
{
return state.T_c;
}
double
TextBuilder:: getT_w() const
{
return state.T_w;
}
double
TextBuilder:: getT_h() const
{
return state.T_h;
}
double
TextBuilder:: getT_l() const
{
return state.T_l;
}
QPDFObjectHandle
TextBuilder:: getT_f() const
{
return state.T_f;
}
double
TextBuilder:: getT_fs() const
{
return state.T_fs;
}
double
TextBuilder:: getT_rise() const
{
return state.T_rise;
}
QPDFMatrix
TextBuilder:: getT_m() const
{
return state.T_m;
}
QPDFMatrix
TextBuilder::getT_lm() const
{
return state.T_lm;
}
void
TextBuilder::Tc(double tc)
{
state.T_c = tc;
appendLine([&](auto& buf) {
buf << tc << " Tc";
});
}
void
TextBuilder::Tw(double tw)
{
state.T_w = tw;
appendLine([&](auto& buf) {
buf << tw << " Tw";
});
}
void
TextBuilder::Tz(double tz)
{
state.T_h = tz / 100.0;
appendLine([&](auto& buf) {
buf << tz << " Tz";
});
}
void
TextBuilder::TL(double tl)
{
state.T_l = tl;
appendLine([&](auto& buf) {
buf << tl << " TL";
});
}
void
TextBuilder::Tf(std::string const& name, double size)
{
QPDFObjectHandle font;
if (fonts.isDictionary())
{
font = fonts.getKey(name);
if (! font.isDictionary())
{
font = QPDFObjectHandle();
}
}
state.T_f = font;
if (! font.isInitialized())
{
throw std::runtime_error("font " + name + " is not known");
}
state.T_fs = size;
appendLine([&](auto& buf) {
buf << name << " " << size << " Tf";
});
}
void
TextBuilder::Tr(int tr)
{
appendLine([&](auto& buf) {
buf << tr << " Tr";
});
}
void
TextBuilder::Ts(double ts)
{
state.T_rise = ts;
appendLine([&](auto& buf) {
buf << ts << " Ts";
});
}
void
TextBuilder::Td(double x, double y)
{
state.T_lm.translate(x, y);
state.T_m = state.T_lm;
appendLine([&](auto& buf) {
buf << x << " " << y << " Td";
});
}
void
TextBuilder::TD(double x, double y)
{
withNoLines([&]() {
TL(-y);
Td(x, y);
});
appendLine([&](auto& buf) {
buf << x << " " << y << " TD";
});
}
void
TextBuilder::Tm(QPDFMatrix const& tm)
{
state.T_lm = tm;
state.T_m = state.T_lm;
appendLine([&](auto& buf) {
buf << tm.unparse() << " Tm";
});
}
void
TextBuilder::Tstar()
{
withNoLines([&]() {
Td(0, -state.T_l);
});
appendLine("T*");
}
QPDFObjectHandle::Rectangle
TextBuilder::Tj(std::string const& str)
{
QPDFObjectHandle::Rectangle bbox;
withNoLines([&]() {
bbox = TJ({{str}});
});
appendLine([&](auto& buf) {
buf << QPDFObjectHandle::newString(str).unparse() << " Tj";
});
return bbox;
}
QPDFObjectHandle::Rectangle
TextBuilder::apos(std::string const& str)
{
QPDFObjectHandle::Rectangle bbox;
withNoLines([&]() {
Tstar();
bbox = Tj(str);
});
appendLine([&](auto& buf) {
buf << QPDFObjectHandle::newString(str).unparse() << " '";
});
return bbox;
}
QPDFObjectHandle::Rectangle
TextBuilder::quot(double aw, double ac, std::string const& str)
{
QPDFObjectHandle::Rectangle bbox;
withNoLines([&]() {
Tw(aw);
Tc(ac);
bbox = apos(str);
});
appendLine([&](auto& buf) {
buf << aw << " " << ac << " "
<< QPDFObjectHandle::newString(str).unparse() << " \"";
});
return bbox;
}
QPDFObjectHandle::Rectangle
TextBuilder::TJ(std::vector<StringOrDouble> const& tj)
{
assertFont();
// 9.4.4
double width = 0.0;
for (auto const& tj_iter: tj)
{
if (tj_iter.isString())
{
for (auto const& i: tj_iter.getString())
{
double cw = getCharWidth(i);
width += ((cw * state.T_fs) + state.T_c +
(i == ' ' ? state.T_w : 0)) * state.T_h;
}
}
else
{
double T_j = tj_iter.getDouble() / 1000.0;
width -= (T_j * state.T_fs) * state.T_h;
}
}
double ascent, descent;
getGlobalFontFields(ascent, descent);
double height = state.T_fs * ascent;
auto bbox = state.T_m.transformRectangle(
QPDFObjectHandle::Rectangle(0, state.T_rise + (descent * state.T_fs),
width, height + state.T_rise));
state.T_m.translate(width, 0);
appendLine([&](auto& buf) {
buf << "[";
for (auto const& tj_iter: tj)
{
buf << tj_iter.getValue() << " ";
}
buf << "] TJ";
});
return bbox;
}