javascript 如何计算jspdf中文本的宽度和高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19109591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate width and height of text in jspdf?
提问by NFE
I'm facing some issues while creating tables in jspdf. I could not find any good related docs, but if anyone knows one, please share.
我在jspdf 中创建表格时遇到了一些问题。我找不到任何好的相关文档,但如果有人知道,请分享。
I found two related methods:
我找到了两个相关的方法:
1) doc.getStringUnitWidth(text, {widths:12,kerning:2});
1) doc.getStringUnitWidth(text, {widths:12,kerning:2});
2) doc.getTextDimensions(text)
2) doc.getTextDimensions(text)
What does widthsand kerningmean in the first method? What should I provide? Which method should I use to get the width and height of text?
第一种方法中的宽度和字距调整是什么意思?我应该提供什么?我应该使用哪种方法来获取文本的宽度和高度?
My goal is to solve some issues related to overlapping cell content, wrapping of text and text overflow relative to the page width.
我的目标是解决一些与重叠单元格内容、文本换行和相对于页面宽度的文本溢出相关的问题。
回答by Simon Bengtsson
You might want to look at the AutoTableplugin for generating tables with jsPDF. I found the built in plugin hard to work with and isn't documented at all.
您可能想查看AutoTable插件以使用 jsPDF 生成表格。我发现内置插件很难使用并且根本没有记录。
The best way I have found to calculate the width is simply doing this:
我发现计算宽度的最好方法就是这样做:
var doc = new jsPDF();
var width = doc.getTextWidth('Text');
console.log(width);
If you really need to calculate the height getDimensions()
might be an option. However, it is not pixel perfect and looking at the source I doubt it will work for anything but pt
. Here is an example:
如果您确实需要计算高度getDimensions()
可能是一种选择。然而,它不是像素完美的,从源头来看,我怀疑它除了pt
. 下面是一个例子:
var doc = new jsPDF('p', 'pt');
var dim = doc.getTextDimensions('Text');
console.log(dim); // Object {w: 24.149968818897642, h: 19.499975433070865}
回答by Studybuffalo
I found this question looking for answers to a similar issue. Unfortunately, the answers provided did not solve my problem, but I figure what I did find out may help other people stumbling onto this question.
我发现这个问题正在寻找类似问题的答案。不幸的是,提供的答案并没有解决我的问题,但我认为我所做的发现可能会帮助其他人偶然发现这个问题。
As already mentioned, the .getStringUnitWidth(text) will return the width of a string (in pt). I could never find any explanation of the options like "kerning" or "widths" in the documentation or in the actual .js file. I also noticed with my own usage that this method was actually underestimating widths by ~10%, which started to add up very quickly. Finally, this did not account for fonts that were bold, italicized or bold and italicized.
如前所述,.getStringUnitWidth(text) 将返回字符串的宽度(以 pt 为单位)。我在文档或实际的 .js 文件中找不到对“字距调整”或“宽度”等选项的任何解释。我还注意到,在我自己的使用中,这种方法实际上低估了约 10% 的宽度,并且开始加起来非常快。最后,这不考虑粗体、斜体或粗体和斜体的字体。
Since I required a high level of accuracy, I ended up calculating the width of each character on my own and using this to calculate a string width (by totaling the width of each character in a string).
由于我需要高度的准确性,因此我最终自己计算了每个字符的宽度,并使用它来计算字符串宽度(通过对字符串中每个字符的宽度求和)。
Below are the font widths at size 1 (in pt) for each character accepted by jsPDF in the 3 default fonts (helvetica, times, courier):
以下是 jsPDF 在 3 种默认字体(helvetica、times、courier)中接受的每个字符的大小为 1(以 pt 为单位)的字体宽度:
helvetica
char unicode normal bold italic bolditalic
32 0.278 0.278 0.278 0.278
! 33 0.278 0.333 0.278 0.333
" 34 0.355 0.474 0.355 0.474
# 35 0.556 0.556 0.556 0.556
$ 36 0.556 0.556 0.556 0.556
% 37 0.889 0.889 0.889 0.889
& 38 0.667 0.722 0.667 0.722
' 39 0.191 0.238 0.191 0.238
( 40 0.333 0.333 0.333 0.333
) 41 0.333 0.333 0.333 0.333
* 42 0.389 0.389 0.389 0.389
+ 43 0.584 0.584 0.584 0.584
, 44 0.278 0.278 0.278 0.278
- 45 0.333 0.333 0.333 0.333
. 46 0.278 0.278 0.278 0.278
/ 47 0.278 0.278 0.278 0.278
0 48 0.556 0.556 0.556 0.556
1 49 0.556 0.556 0.556 0.556
2 50 0.556 0.556 0.556 0.556
3 51 0.556 0.556 0.556 0.556
4 52 0.556 0.556 0.556 0.556
5 53 0.556 0.556 0.556 0.556
6 54 0.556 0.556 0.556 0.556
7 55 0.556 0.556 0.556 0.556
8 56 0.556 0.556 0.556 0.556
9 57 0.556 0.556 0.556 0.556
: 58 0.278 0.333 0.278 0.333
; 59 0.278 0.333 0.278 0.333
< 60 0.584 0.584 0.584 0.584
= 61 0.584 0.584 0.584 0.584
> 62 0.584 0.584 0.584 0.584
? 63 0.556 0.611 0.556 0.611
@ 64 1.015 0.975 1.015 0.975
A 65 0.667 0.722 0.667 0.722
B 66 0.667 0.722 0.667 0.722
C 67 0.722 0.722 0.722 0.722
D 68 0.722 0.722 0.722 0.722
E 69 0.667 0.667 0.667 0.667
F 70 0.611 0.611 0.611 0.611
G 71 0.778 0.778 0.778 0.778
H 72 0.722 0.722 0.722 0.722
I 73 0.278 0.278 0.278 0.278
J 74 0.500 0.556 0.500 0.556
K 75 0.667 0.722 0.667 0.722
L 76 0.556 0.611 0.556 0.611
M 77 0.833 0.833 0.833 0.833
N 78 0.722 0.722 0.722 0.722
O 79 0.778 0.778 0.778 0.778
P 80 0.667 0.667 0.667 0.667
Q 81 0.778 0.778 0.778 0.778
R 82 0.722 0.722 0.722 0.722
S 83 0.667 0.667 0.667 0.667
T 84 0.611 0.611 0.611 0.611
U 85 0.722 0.722 0.722 0.722
V 86 0.667 0.667 0.667 0.667
W 87 0.944 0.944 0.944 0.944
X 88 0.667 0.667 0.667 0.667
Y 89 0.667 0.667 0.667 0.667
Z 90 0.611 0.611 0.611 0.611
[ 91 0.278 0.333 0.278 0.333
\ 92 0.278 0.278 0.278 0.278
] 93 0.278 0.333 0.278 0.333
^ 94 0.469 0.584 0.469 0.584
_ 95 0.556 0.556 0.556 0.556
` 96 0.333 0.333 0.333 0.333
a 97 0.556 0.556 0.556 0.556
b 98 0.556 0.611 0.556 0.611
c 99 0.500 0.556 0.500 0.556
d 100 0.556 0.611 0.556 0.611
e 101 0.556 0.556 0.556 0.556
f 102 0.278 0.333 0.278 0.333
g 103 0.556 0.611 0.556 0.611
h 104 0.556 0.611 0.556 0.611
i 105 0.222 0.278 0.222 0.278
j 106 0.222 0.278 0.222 0.278
k 107 0.500 0.556 0.500 0.556
l 108 0.222 0.278 0.222 0.278
m 109 0.833 0.889 0.833 0.889
n 110 0.556 0.611 0.556 0.611
o 111 0.556 0.611 0.556 0.611
p 112 0.556 0.611 0.556 0.611
q 113 0.556 0.611 0.556 0.611
r 114 0.333 0.389 0.333 0.389
s 115 0.500 0.556 0.500 0.556
t 116 0.278 0.333 0.278 0.333
u 117 0.556 0.611 0.556 0.611
v 118 0.500 0.556 0.500 0.556
w 119 0.722 0.778 0.722 0.778
x 120 0.500 0.556 0.500 0.556
y 121 0.500 0.556 0.500 0.556
z 122 0.500 0.500 0.500 0.500
{ 123 0.334 0.389 0.334 0.389
| 124 0.260 0.280 0.260 0.280
} 125 0.334 0.389 0.334 0.389
~ 126 0.584 0.584 0.584 0.584
128 0.556 0.556 0.556 0.556
? 130 0.222 0.278 0.222 0.278
? 131 0.556 0.556 0.556 0.556
? 132 0.333 0.500 0.333 0.500
… 133 1.000 1.000 1.000 1.000
? 134 0.556 0.556 0.556 0.556
? 135 0.556 0.556 0.556 0.556
? 136 0.333 0.333 0.333 0.333
‰ 137 1.000 1.000 1.000 1.000
? 138 0.667 0.667 0.667 0.667
? 139 0.333 0.333 0.333 0.333
? 140 1.000 1.000 1.000 1.000
? 142 0.611 0.611 0.611 0.611
‘ 145 0.222 0.278 0.222 0.278
' 146 0.222 0.278 0.222 0.278
“ 147 0.333 0.500 0.333 0.500
” 148 0.333 0.500 0.333 0.500
? 149 0.350 0.350 0.350 0.350
– 150 0.556 0.556 0.556 0.556
— 151 1.000 1.000 1.000 1.000
? 152 0.333 0.333 0.333 0.333
? 153 1.000 1.000 1.000 1.000
? 154 0.500 0.556 0.500 0.556
? 155 0.333 0.333 0.333 0.333
? 156 0.944 0.944 0.944 0.944
? 158 0.500 0.500 0.500 0.500
? 159 0.667 0.667 0.667 0.667
160 0.278 0.278 0.278 0.278
? 161 0.333 0.333 0.333 0.333
¢ 162 0.556 0.556 0.556 0.556
£ 163 0.556 0.556 0.556 0.556
¤ 164 0.556 0.556 0.556 0.556
¥ 165 0.556 0.556 0.556 0.556
| 166 0.260 0.280 0.260 0.280
§ 167 0.556 0.556 0.556 0.556
¨ 168 0.333 0.333 0.333 0.333
? 169 0.737 0.737 0.737 0.737
a 170 0.370 0.370 0.370 0.370
? 171 0.556 0.556 0.556 0.556
? 172 0.584 0.584 0.584 0.584
- 173 0.333 0.333 0.333 0.333
? 174 0.737 0.737 0.737 0.737
ˉ 175 0.552 0.552 0.552 0.552
° 176 0.400 0.400 0.400 0.400
± 177 0.549 0.549 0.549 0.549
2 178 0.333 0.333 0.333 0.333
3 179 0.333 0.333 0.333 0.333
′ 180 0.333 0.333 0.333 0.333
μ 181 0.576 0.576 0.576 0.576
? 182 0.537 0.556 0.537 0.556
· 183 0.333 0.333 0.333 0.333
? 184 0.333 0.333 0.333 0.333
1 185 0.333 0.333 0.333 0.333
o 186 0.365 0.365 0.365 0.365
? 187 0.556 0.556 0.556 0.556
? 188 0.834 0.834 0.834 0.834
? 189 0.834 0.834 0.834 0.834
? 190 0.834 0.834 0.834 0.834
? 191 0.611 0.611 0.611 0.611
à 192 0.667 0.722 0.667 0.722
á 193 0.667 0.722 0.667 0.722
? 194 0.667 0.722 0.667 0.722
? 195 0.667 0.722 0.667 0.722
? 196 0.667 0.722 0.667 0.722
? 197 0.667 0.722 0.667 0.722
? 198 1.000 1.000 1.000 1.000
? 199 0.722 0.722 0.722 0.722
è 200 0.667 0.667 0.667 0.667
é 201 0.667 0.667 0.667 0.667
ê 202 0.667 0.667 0.667 0.667
? 203 0.667 0.667 0.667 0.667
ì 204 0.278 0.278 0.278 0.278
í 205 0.278 0.278 0.278 0.278
? 206 0.278 0.278 0.278 0.278
? 207 0.278 0.278 0.278 0.278
D 208 0.722 0.722 0.722 0.722
? 209 0.722 0.722 0.722 0.722
ò 210 0.778 0.778 0.778 0.778
ó 211 0.778 0.778 0.778 0.778
? 212 0.778 0.778 0.778 0.778
? 213 0.778 0.778 0.778 0.778
? 214 0.778 0.778 0.778 0.778
× 215 0.584 0.584 0.584 0.584
? 216 0.778 0.778 0.778 0.778
ù 217 0.722 0.722 0.722 0.722
ú 218 0.722 0.722 0.722 0.722
? 219 0.722 0.722 0.722 0.722
ü 220 0.722 0.722 0.722 0.722
Y 221 0.667 0.667 0.667 0.667
T 222 0.667 0.667 0.667 0.667
? 223 0.611 0.611 0.611 0.611
à 224 0.556 0.556 0.556 0.556
á 225 0.556 0.556 0.556 0.556
a 226 0.556 0.556 0.556 0.556
? 227 0.556 0.556 0.556 0.556
? 228 0.556 0.556 0.556 0.556
? 229 0.556 0.556 0.556 0.556
? 230 0.889 0.889 0.889 0.889
? 231 0.500 0.556 0.500 0.556
è 232 0.556 0.556 0.556 0.556
é 233 0.556 0.556 0.556 0.556
ê 234 0.556 0.556 0.556 0.556
? 235 0.556 0.556 0.556 0.556
ì 236 0.278 0.278 0.278 0.278
í 237 0.278 0.278 0.278 0.278
? 238 0.278 0.278 0.278 0.278
? 239 0.278 0.278 0.278 0.278
e 240 0.556 0.611 0.556 0.611
? 241 0.556 0.611 0.556 0.611
ò 242 0.556 0.611 0.556 0.611
ó 243 0.556 0.611 0.556 0.611
? 244 0.556 0.611 0.556 0.611
? 245 0.556 0.611 0.556 0.611
? 246 0.556 0.611 0.556 0.611
÷ 247 0.549 0.549 0.549 0.549
? 248 0.611 0.611 0.611 0.611
ù 249 0.556 0.611 0.556 0.611
ú 250 0.556 0.611 0.556 0.611
? 251 0.556 0.611 0.556 0.611
ü 252 0.556 0.611 0.556 0.611
y 253 0.500 0.556 0.500 0.556
t 254 0.556 0.611 0.556 0.611
? 255 0.500 0.556 0.500 0.556
times
char unicode normal bold italic bolditalic
32 0.250 0.250 0.250 0.250
! 33 0.333 0.333 0.333 0.389
" 34 0.408 0.555 0.420 0.555
# 35 0.500 0.500 0.500 0.500
$ 36 0.500 0.500 0.500 0.500
% 37 0.833 1.000 0.833 0.833
& 38 0.778 0.833 0.778 0.778
' 39 0.180 0.278 0.214 0.278
( 40 0.333 0.333 0.333 0.333
) 41 0.333 0.333 0.333 0.333
* 42 0.500 0.500 0.500 0.500
+ 43 0.564 0.570 0.675 0.570
, 44 0.250 0.250 0.250 0.250
- 45 0.333 0.333 0.333 0.333
. 46 0.250 0.250 0.250 0.250
/ 47 0.278 0.278 0.278 0.278
0 48 0.500 0.500 0.500 0.500
1 49 0.500 0.500 0.500 0.500
2 50 0.500 0.500 0.500 0.500
3 51 0.500 0.500 0.500 0.500
4 52 0.500 0.500 0.500 0.500
5 53 0.500 0.500 0.500 0.500
6 54 0.500 0.500 0.500 0.500
7 55 0.500 0.500 0.500 0.500
8 56 0.500 0.500 0.500 0.500
9 57 0.500 0.500 0.500 0.500
: 58 0.278 0.333 0.333 0.333
; 59 0.278 0.333 0.333 0.333
< 60 0.564 0.570 0.675 0.570
= 61 0.564 0.570 0.675 0.570
> 62 0.564 0.570 0.675 0.570
? 63 0.444 0.500 0.500 0.500
@ 64 0.921 0.930 0.920 0.832
A 65 0.722 0.722 0.611 0.667
B 66 0.667 0.667 0.611 0.667
C 67 0.667 0.722 0.667 0.667
D 68 0.722 0.722 0.722 0.722
E 69 0.611 0.667 0.611 0.667
F 70 0.556 0.611 0.611 0.667
G 71 0.722 0.778 0.722 0.722
H 72 0.722 0.778 0.722 0.778
I 73 0.333 0.389 0.333 0.389
J 74 0.389 0.500 0.444 0.500
K 75 0.722 0.778 0.667 0.667
L 76 0.611 0.667 0.556 0.611
M 77 0.889 0.944 0.833 0.889
N 78 0.722 0.722 0.667 0.722
O 79 0.722 0.778 0.722 0.722
P 80 0.556 0.611 0.611 0.611
Q 81 0.722 0.778 0.722 0.722
R 82 0.667 0.722 0.611 0.667
S 83 0.556 0.556 0.500 0.556
T 84 0.611 0.667 0.556 0.611
U 85 0.722 0.722 0.722 0.722
V 86 0.722 0.722 0.611 0.667
W 87 0.944 1.000 0.833 0.889
X 88 0.722 0.722 0.611 0.667
Y 89 0.722 0.722 0.556 0.611
Z 90 0.611 0.667 0.556 0.611
[ 91 0.333 0.333 0.389 0.333
\ 92 0.278 0.278 0.278 0.278
] 93 0.333 0.333 0.389 0.333
^ 94 0.469 0.581 0.422 0.570
_ 95 0.500 0.500 0.500 0.500
` 96 0.333 0.333 0.333 0.333
a 97 0.444 0.500 0.500 0.500
b 98 0.500 0.556 0.500 0.500
c 99 0.444 0.444 0.444 0.444
d 100 0.500 0.556 0.500 0.500
e 101 0.444 0.444 0.444 0.444
f 102 0.333 0.333 0.278 0.333
g 103 0.500 0.500 0.500 0.500
h 104 0.500 0.556 0.500 0.556
i 105 0.278 0.278 0.278 0.278
j 106 0.278 0.333 0.278 0.278
k 107 0.500 0.556 0.444 0.500
l 108 0.278 0.278 0.278 0.278
m 109 0.778 0.833 0.722 0.778
n 110 0.500 0.556 0.500 0.556
o 111 0.500 0.500 0.500 0.500
p 112 0.500 0.556 0.500 0.500
q 113 0.500 0.556 0.500 0.500
r 114 0.333 0.444 0.389 0.389
s 115 0.389 0.389 0.389 0.389
t 116 0.278 0.333 0.278 0.278
u 117 0.500 0.556 0.500 0.556
v 118 0.500 0.500 0.444 0.444
w 119 0.722 0.722 0.667 0.667
x 120 0.500 0.500 0.444 0.500
y 121 0.500 0.500 0.444 0.444
z 122 0.444 0.444 0.389 0.389
{ 123 0.480 0.394 0.400 0.348
| 124 0.200 0.220 0.275 0.220
} 125 0.480 0.394 0.400 0.348
~ 126 0.541 0.520 0.541 0.570
128 0.500 0.500 0.500 0.500
? 130 0.333 0.333 0.333 0.333
? 131 0.500 0.500 0.500 0.500
? 132 0.444 0.500 0.556 0.500
… 133 1.000 1.000 0.889 1.000
? 134 0.500 0.500 0.500 0.500
? 135 0.500 0.500 0.500 0.500
? 136 0.333 0.333 0.333 0.333
‰ 137 1.000 1.000 1.000 1.000
? 138 0.556 0.556 0.500 0.556
? 139 0.333 0.333 0.333 0.333
? 140 0.889 1.000 0.944 0.944
? 142 0.611 0.667 0.556 0.611
‘ 145 0.333 0.333 0.333 0.333
' 146 0.333 0.333 0.333 0.333
“ 147 0.444 0.500 0.556 0.500
” 148 0.444 0.500 0.556 0.500
? 149 0.350 0.350 0.350 0.350
– 150 0.500 0.500 0.500 0.500
— 151 1.000 1.000 0.889 1.000
? 152 0.333 0.333 0.333 0.333
? 153 0.980 1.000 0.980 1.000
? 154 0.389 0.389 0.389 0.389
? 155 0.333 0.333 0.333 0.333
? 156 0.722 0.722 0.667 0.722
? 158 0.444 0.444 0.389 0.389
? 159 0.722 0.722 0.556 0.611
160 0.250 0.250 0.250 0.250
? 161 0.333 0.333 0.389 0.389
¢ 162 0.500 0.500 0.500 0.500
£ 163 0.500 0.500 0.500 0.500
¤ 164 0.500 0.500 0.500 0.500
¥ 165 0.500 0.500 0.500 0.500
| 166 0.200 0.220 0.275 0.220
§ 167 0.500 0.500 0.500 0.500
¨ 168 0.333 0.333 0.333 0.333
? 169 0.760 0.747 0.760 0.747
a 170 0.276 0.300 0.276 0.266
? 171 0.500 0.500 0.500 0.500
? 172 0.564 0.570 0.675 0.606
- 173 0.333 0.333 0.333 0.333
? 174 0.760 0.747 0.760 0.747
ˉ 175 0.500 0.500 0.500 0.500
° 176 0.400 0.400 0.400 0.400
± 177 0.549 0.549 0.549 0.549
2 178 0.300 0.300 0.300 0.300
3 179 0.300 0.300 0.300 0.300
′ 180 0.333 0.333 0.333 0.333
μ 181 0.576 0.576 0.576 0.576
? 182 0.453 0.540 0.523 0.500
· 183 0.333 0.333 0.250 0.250
? 184 0.333 0.333 0.333 0.333
1 185 0.300 0.300 0.300 0.300
o 186 0.310 0.330 0.310 0.300
? 187 0.500 0.500 0.500 0.500
? 188 0.750 0.750 0.750 0.750
? 189 0.750 0.750 0.750 0.750
? 190 0.750 0.750 0.750 0.750
? 191 0.444 0.500 0.500 0.500
à 192 0.722 0.722 0.611 0.667
á 193 0.722 0.722 0.611 0.667
? 194 0.722 0.722 0.611 0.667
? 195 0.722 0.722 0.611 0.667
? 196 0.722 0.722 0.611 0.667
? 197 0.722 0.722 0.611 0.667
? 198 0.889 1.000 0.889 0.944
? 199 0.667 0.722 0.667 0.667
è 200 0.611 0.667 0.611 0.667
é 201 0.611 0.667 0.611 0.667
ê 202 0.611 0.667 0.611 0.667
? 203 0.611 0.667 0.611 0.667
ì 204 0.333 0.389 0.333 0.389
í 205 0.333 0.389 0.333 0.389
? 206 0.333 0.389 0.333 0.389
? 207 0.333 0.389 0.333 0.389
D 208 0.722 0.722 0.722 0.722
? 209 0.722 0.722 0.667 0.722
ò 210 0.722 0.778 0.722 0.722
ó 211 0.722 0.778 0.722 0.722
? 212 0.722 0.778 0.722 0.722
? 213 0.722 0.778 0.722 0.722
? 214 0.722 0.778 0.722 0.722
× 215 0.564 0.570 0.675 0.570
? 216 0.722 0.778 0.722 0.722
ù 217 0.722 0.722 0.722 0.722
ú 218 0.722 0.722 0.722 0.722
? 219 0.722 0.722 0.722 0.722
ü 220 0.722 0.722 0.722 0.722
Y 221 0.722 0.722 0.556 0.611
T 222 0.556 0.611 0.611 0.611
? 223 0.500 0.556 0.500 0.500
à 224 0.444 0.500 0.500 0.500
á 225 0.444 0.500 0.500 0.500
a 226 0.444 0.500 0.500 0.500
? 227 0.444 0.500 0.500 0.500
? 228 0.444 0.500 0.500 0.500
? 229 0.444 0.500 0.500 0.500
? 230 0.667 0.722 0.667 0.722
? 231 0.444 0.444 0.444 0.444
è 232 0.444 0.444 0.444 0.444
é 233 0.444 0.444 0.444 0.444
ê 234 0.444 0.444 0.444 0.444
? 235 0.444 0.444 0.444 0.444
ì 236 0.278 0.278 0.278 0.278
í 237 0.278 0.278 0.278 0.278
? 238 0.278 0.278 0.278 0.278
? 239 0.278 0.278 0.278 0.278
e 240 0.500 0.500 0.500 0.500
? 241 0.500 0.556 0.500 0.556
ò 242 0.500 0.500 0.500 0.500
ó 243 0.500 0.500 0.500 0.500
? 244 0.500 0.500 0.500 0.500
? 245 0.500 0.500 0.500 0.500
? 246 0.500 0.500 0.500 0.500
÷ 247 0.549 0.549 0.549 0.549
? 248 0.500 0.500 0.500 0.500
ù 249 0.500 0.556 0.500 0.556
ú 250 0.500 0.556 0.500 0.556
? 251 0.500 0.556 0.500 0.556
ü 252 0.500 0.556 0.500 0.556
y 253 0.500 0.500 0.444 0.444
t 254 0.500 0.556 0.500 0.500
? 255 0.500 0.500 0.444 0.444
courier
All characters in every style are 0.600 (font is monospaced)
Once you have determined the width of your string with the above table you need to convert the answer to your desired unit of measurement.
使用上表确定字符串的宽度后,您需要将答案转换为所需的测量单位。
width (in inches) = (character or string width * font size) / (72 pt/in)
width (in mm) = (character or string width * font size * 25.4 mm/in) / (72 pt/in)
The only caveat to using the above I could think of was if your output pdf was making use of kerning pairs. My output files did not appear to be using kerning pairs, so I did not explore the issue further. If for some reason this is an issue for you, you would need to adjust your widths slightly for each kerning pair as it occurred.
我能想到的使用上述内容的唯一警告是,如果您的输出 pdf 使用字距调整对。我的输出文件似乎没有使用字距调整对,所以我没有进一步探讨这个问题。如果由于某种原因这对您来说是一个问题,您需要在每个字距调整对发生时稍微调整您的宽度。
The calculation of height is much less involved. The height of a single line of text is simply the font size. A line of text at size 10 will take up 10 pt.
高度的计算要少得多。单行文本的高度就是字体大小。一行大小为 10 的文本将占用 10 pt。
If you are wanting to calculate multiple lines of text you will need to account for the "line height" (essentially the amount of whitespace between lines). In my testing I found that jsPDF uses a default line height of 115% (1.15x) of the font size. Thus your calculation of line height would become:
如果要计算多行文本,则需要考虑“行高”(本质上是行之间的空白量)。在我的测试中,我发现 jsPDF 使用字体大小的 115% (1.15x) 的默认行高。因此,您对行高的计算将变为:
height = font size * number of lines * line height
Example:
height = 10 pt * 5 lines * 1.15
height = 57.5 pt
You then simply convert to your desired unit of measurement as above.
然后,您只需按上述方式转换为所需的计量单位。
回答by Igor Barbashin
It's now included in jsPDF:
它现在包含在 jsPDF 中:
doc.getTextWidth(text);