-
Notifications
You must be signed in to change notification settings - Fork 29
/
page.go
160 lines (141 loc) · 3.78 KB
/
page.go
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
package poppler
// #cgo pkg-config: poppler-glib
// #include <poppler.h>
// #include <glib.h>
// #include <cairo.h>
import "C"
import "unsafe"
import "github.com/ungerik/go-cairo"
//import "fmt"
type Page struct {
p *C.struct__PopplerPage
}
func (p *Page) Text() string {
return C.GoString(C.poppler_page_get_text(p.p))
}
func (p *Page) TextAttributes() (results []TextAttributes) {
a := C.poppler_page_get_text_attributes(p.p)
defer C.poppler_page_free_text_attributes(a)
var attr *C.PopplerTextAttributes
results = make([]TextAttributes, 0)
el := C.g_list_first(a)
for el != nil {
attr = (*C.PopplerTextAttributes)(el.data)
fn := attr.font_name
result := TextAttributes{
FontName: toString(fn),
FontSize: float64(attr.font_size),
IsUnderlined: toBool(attr.is_underlined),
StartIndex: int(attr.start_index),
EndIndex: int(attr.end_index),
Color: Color{
R: int(attr.color.red),
G: int(attr.color.green),
B: int(attr.color.blue),
},
}
results = append(results, result)
el = el.next
}
return
}
func (p *Page) Size() (width, height float64) {
var w, h C.double
C.poppler_page_get_size(p.p, &w, &h)
return float64(w), float64(h)
}
func (p *Page) Index() int {
return int(C.poppler_page_get_index(p.p))
}
func (p *Page) Label() string {
return toString(C.poppler_page_get_label(p.p))
}
func (p *Page) Duration() float64 {
return float64(C.poppler_page_get_duration(p.p))
}
func (p *Page) Images() (results []Image) {
l := C.poppler_page_get_image_mapping(p.p)
defer C.poppler_page_free_image_mapping(l)
results = make([]Image, 0)
var im *C.PopplerImageMapping
for el := C.g_list_first(l); el != nil; el = el.next {
im = (*C.PopplerImageMapping)(el.data)
result := Image{
Id: int(im.image_id),
Area: Rectangle{
X1: float64(im.area.x1),
Y1: float64(im.area.y1),
X2: float64(im.area.x2),
Y2: float64(im.area.y2),
},
p: p.p,
}
results = append(results, result)
}
return
}
func (p *Page) TextLayout() (layouts []Rectangle) {
var rect *C.PopplerRectangle
var n C.guint
if toBool(C.poppler_page_get_text_layout(p.p, &rect, &n)) {
defer C.g_free((C.gpointer)(rect))
layouts = make([]Rectangle, int(n))
r := (*[1 << 30]C.PopplerRectangle)(unsafe.Pointer(rect))[:n:n]
for i := 0; i < int(n); i++ {
layouts[i] = Rectangle{
X1: float64(r[i].x1),
Y1: float64(r[i].y1),
X2: float64(r[i].x2),
Y2: float64(r[i].y2),
}
}
}
return
}
func (p *Page) TextLayoutAndAttrs() (result []TextEl) {
text := p.Text()
attrs := p.TextAttributes()
layout := p.TextLayout()
result = make([]TextEl, len(layout))
attrsRef := make([]*TextAttributes, len(attrs))
for i, a := range attrs {
attr := a
attrsRef[i] = &attr
}
i := 0
for _, t := range text {
var a *TextAttributes
for _, a = range attrsRef {
if i >= a.StartIndex && i <= a.EndIndex {
break
}
}
result[i] = TextEl{
Text: string(t),
Attrs: a,
Rect: layout[i],
}
i++
}
return
}
func (p *Page) Close() {
C.g_object_unref(C.gpointer(p.p))
}
// Converts a page into SVG and saves to file.
// Inspired by https://github.com/dawbarton/pdf2svg
func (p *Page) ConvertToSVG(filename string){
width, height := p.Size()
// Open the SVG file
surface := cairo.NewSVGSurface( filename, width, height, cairo.SVG_VERSION_1_2 )
// TODO Can be improved by using cairo_svg_surface_create_for_stream() instead of
// cairo_svg_surface_create() for stream processing instead of file processing.
// However, this needs to be changed in github.com/ungerik/go-cairo/surface.go
// Get cairo context pointer
_, drawcontext := surface.Native()
// Render the PDF file into the SVG file
C.poppler_page_render_for_printing(p.p, (*C.cairo_t)(unsafe.Pointer(drawcontext)) );
// Close the SVG file
surface.ShowPage()
surface.Destroy()
}