forked from rkusa/pdfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.js
59 lines (48 loc) · 1.33 KB
/
word.js
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
'use strict'
var Word = module.exports = function(word, style) {
Word.super_.call(this, require('../pdf/nodes/word'))
if (!style.font) {
throw new TypeError('Cannot create font without specifying a font')
}
this.word = word
this.style = style
this.children = [this]
}
require('../pdf/utils').inherits(Word, require('./base'))
Object.defineProperties(Word.prototype, {
width: {
enumerable: true,
get: function() {
var width = this.children.map(function(word) {
return this.style.font.stringWidth(word.word, word.style.fontSize)
}, this).reduce(function(lhs, rhs) {
return lhs + rhs
}, 0)
return width
}
},
height: {
enumerable: true,
get: function() {
var height = Math.max.apply(Math, this.children.map(function(word) {
return word.style.font.lineHeight(word.style.fontSize, true) * word.style.lineHeight
}, this))
return height
}
},
spacing: {
enumerable: true,
get: function() {
var last = this.children[this.children.length - 1]
var spacing = last.style.font.stringWidth(' ', last.style.fontSize)
return spacing
}
}
})
Word.prototype.toString = function() {
return this.children.map(function(word) {
return word.word
}, this).reduce(function(lhs, rhs) {
return lhs + rhs
}, '')
}