font.dart
2.68 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
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
part of pdf;
class PdfFont extends PdfObject {
/// Thedf type of the font, usually /Type1
final String subtype;
/// The font's real name
String baseFont;
/// Constructs a [PdfFont]. This will attempt to map the font from a known
/// font name to that in Pdf, defaulting to Helvetica if not possible.
///
/// @param name The document name, ie /F1
/// @param subtype The pdf type, ie /Type1
/// @param baseFont The font name, ie /Helvetica
PdfFont(PdfDocument pdfDocument,
{this.subtype = "/Type1", this.baseFont = "/Helvetica"})
: super(pdfDocument, "/Font") {
pdfDocument.fonts.add(this);
}
String get name => "/F$objser";
/// @param os OutputStream to send the object to
@override
void prepare() {
super.prepare();
params["/Subtype"] = PdfStream.string(subtype);
params["/Name"] = PdfStream.string(name);
params["/BaseFont"] = PdfStream.string(baseFont);
params["/Encoding"] = PdfStream.string("/WinAnsiEncoding");
}
double glyphAdvance(int charCode) {
return 0.454;
}
PdfRect glyphBounds(int charCode) {
return const PdfRect(0.0, 0.0, 0.4, 1.0);
}
PdfRect stringBounds(String s) {
var chars = latin1.encode(s);
if (chars.length == 0) return const PdfRect(0.0, 0.0, 0.0, 0.0);
var n = 0;
var c = chars[n];
var r = glyphBounds(c);
var x = r.x;
var y = r.y;
var h = r.h;
var w = n == chars.length - 1 ? r.w : glyphAdvance(c);
while (++n < chars.length) {
c = chars[n];
r = glyphBounds(c);
if (r.y < y) y = r.y;
if (r.h > h) h = r.h;
w += n == chars.length - 1 ? r.w : glyphAdvance(c);
}
return PdfRect(x, y, w, h);
}
PdfPoint stringSize(String s) {
var chars = latin1.encode(s);
var w = 0.0;
var h = 0.0;
for (var c in chars) {
var r = glyphBounds(c);
if (r.h > h) h = r.h;
w += glyphAdvance(c);
}
return PdfPoint(w, h);
}
}