forked from gen2brain/go-fitz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitz.go
833 lines (642 loc) · 18.9 KB
/
fitz.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
// Package fitz provides wrapper for the [MuPDF](http://mupdf.com/) fitz library
// that can extract pages from PDF and EPUB documents as images, text, html or svg.
package fitz
/*
#cgo CFLAGS: -I./include
#include <mupdf/fitz.h>
#include <mupdf/fitz/structured-text.h>
#include <mupdf/pdf/page.h>
#include <mupdf/pdf/object.h>
#include <stdlib.h>
#include <pthread.h>
#include "quad.h"
const char *fz_version = FZ_VERSION;
fz_document *open_document(fz_context *ctx, const char *filename) {
fz_document *doc;
fz_try(ctx) {
doc = fz_open_document(ctx, filename);
}
fz_catch(ctx) {
return NULL;
}
return doc;
}
fz_document *open_document_with_stream(fz_context *ctx, const char *magic, fz_stream *stream) {
fz_document *doc;
fz_try(ctx) {
doc = fz_open_document_with_stream(ctx, magic, stream);
}
fz_catch(ctx) {
return NULL;
}
return doc;
}
void fail(char *msg) {
fprintf(stderr, "%s\n", msg);
abort();
}
void lock_mutex(void *user, int lock) {
pthread_mutex_t *mutex = (pthread_mutex_t *) user;
if (pthread_mutex_lock(&mutex[lock]) != 0)
fail("pthread_mutex_lock()");
}
void unlock_mutex(void *user, int lock) {
pthread_mutex_t *mutex = (pthread_mutex_t *) user;
if (pthread_mutex_unlock(&mutex[lock]) != 0)
fail("pthread_mutex_unlock()");
}
pthread_mutex_t* new_mutex() {
pthread_mutex_t* mutex = malloc(sizeof(pthread_mutex_t) * FZ_LOCK_MAX);
for (int i = 0; i < FZ_LOCK_MAX; i++) {
if (pthread_mutex_init(&mutex[i], NULL) != 0)
fail("pthread_mutex_init()");
}
return mutex;
}
typedef fz_locks_context fz_locks_context_t;
fz_locks_context_t* create_fz_locks_context(pthread_mutex_t* mutex) {
fz_locks_context* lock_ctx = malloc(sizeof(fz_locks_context));
lock_ctx->user = mutex;
lock_ctx->lock = lock_mutex;
lock_ctx->unlock = unlock_mutex;
return lock_ctx;
}
// This struct uses `type` as a member so jumping back into c to grab the
// value.
int stext_block_type(fz_stext_block *block) {
return block->type;
}
// Need to be in C since this call requires a macro to run
int page_rotation(fz_context *ctx, pdf_page *page) {
int rotate = 0;
fz_try(ctx) {
rotate = pdf_to_int(ctx,
pdf_dict_get_inheritable(ctx, page->obj, PDF_NAME(Rotate)));
}
fz_catch(ctx) return -1;
return rotate;
}
*/
import "C"
import (
"errors"
"image"
"io"
"os"
"path/filepath"
"sync"
"unsafe"
)
// Errors.
var (
ErrNoSuchFile = errors.New("fitz: no such file")
ErrCreateContext = errors.New("fitz: cannot create context")
ErrOpenDocument = errors.New("fitz: cannot open document")
ErrOpenMemory = errors.New("fitz: cannot open memory")
ErrPageMissing = errors.New("fitz: page missing")
ErrCreatePixmap = errors.New("fitz: cannot create pixmap")
ErrPixmapSamples = errors.New("fitz: cannot get pixmap samples")
ErrNeedsPassword = errors.New("fitz: document needs password")
ErrLoadOutline = errors.New("fitz: cannot load outline")
)
var (
// needed for multithreading
locks *C.fz_locks_context_t
mutexs *C.pthread_mutex_t
rootCtx *C.struct_fz_context
)
func init() {
mutexs = C.new_mutex()
locks = C.create_fz_locks_context(mutexs)
rootCtx = (*C.struct_fz_context)(unsafe.Pointer(C.fz_new_context_imp(nil, locks, C.FZ_STORE_UNLIMITED, C.fz_version)))
}
// Document represents fitz document.
type Document struct {
ctx *C.struct_fz_context
data []byte // binds data to the Document lifecycle avoiding premature GC
doc *C.struct_fz_document
mtx sync.Mutex
stream *C.fz_stream
}
// Outline type.
type Outline struct {
// Hierarchy level of the entry (starting from 1).
Level int
// Title of outline item.
Title string
// Destination in the document to be displayed when this outline item is activated.
URI string
// The page number of an internal link.
Page int
// Top.
Top float64
}
// Link type.
type Link struct {
URI string
}
// New returns new fitz document.
func New(filename string) (f *Document, err error) {
f = &Document{}
filename, err = filepath.Abs(filename)
if err != nil {
return
}
if _, e := os.Stat(filename); e != nil {
err = ErrNoSuchFile
return
}
f.ctx = C.fz_clone_context(rootCtx)
if f.ctx == nil {
err = ErrCreateContext
return
}
C.fz_register_document_handlers(f.ctx)
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
f.doc = C.open_document(f.ctx, cfilename)
if f.doc == nil {
err = ErrOpenDocument
return
}
ret := C.fz_needs_password(f.ctx, f.doc)
v := int(ret) != 0
if v {
err = ErrNeedsPassword
}
return
}
// NewFromMemory returns new fitz document from byte slice.
func NewFromMemory(b []byte) (f *Document, err error) {
f = &Document{}
f.ctx = C.fz_clone_context(rootCtx)
if f.ctx == nil {
err = ErrCreateContext
return
}
C.fz_register_document_handlers(f.ctx)
stream := C.fz_open_memory(f.ctx, (*C.uchar)(&b[0]), C.size_t(len(b)))
f.stream = C.fz_keep_stream(f.ctx, stream)
if f.stream == nil {
err = ErrOpenMemory
return
}
magic := contentType(b)
if magic == "" {
err = ErrOpenMemory
return
}
f.data = b
cmagic := C.CString(magic)
defer C.free(unsafe.Pointer(cmagic))
f.doc = C.open_document_with_stream(f.ctx, cmagic, f.stream)
if f.doc == nil {
err = ErrOpenDocument
}
ret := C.fz_needs_password(f.ctx, f.doc)
v := int(ret) != 0
if v {
err = ErrNeedsPassword
}
return
}
// NewFromReader returns new fitz document from io.Reader.
func NewFromReader(r io.Reader) (f *Document, err error) {
b, e := io.ReadAll(r)
if e != nil {
err = e
return
}
f, err = NewFromMemory(b)
return
}
// NumPage returns total number of pages in document.
func (f *Document) NumPage() int {
return int(C.fz_count_pages(f.ctx, f.doc))
}
// Image returns image for given page number.
func (f *Document) Image(pageNumber int) (image.Image, error) {
return f.ImageDPI(pageNumber, 300.0)
}
// ImageDPI returns image for given page number and DPI.
func (f *Document) ImageDPI(pageNumber int, dpi float64) (image.Image, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
img := image.RGBA{}
if pageNumber >= f.NumPage() {
return nil, ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var bounds C.fz_rect
bounds = C.fz_bound_page(f.ctx, page)
var ctm C.fz_matrix
ctm = C.fz_scale(C.float(dpi/72), C.float(dpi/72))
var bbox C.fz_irect
bounds = C.fz_transform_rect(bounds, ctm)
bbox = C.fz_round_rect(bounds)
pixmap := C.fz_new_pixmap_with_bbox(f.ctx, C.fz_device_rgb(f.ctx), bbox, nil, 1)
if pixmap == nil {
return nil, ErrCreatePixmap
}
C.fz_clear_pixmap_with_value(f.ctx, pixmap, C.int(0xff))
//defer C.fz_drop_pixmap(f.ctx, pixmap)
device := C.fz_new_draw_device(f.ctx, ctm, pixmap)
C.fz_enable_device_hints(f.ctx, device, C.FZ_NO_CACHE)
defer C.fz_drop_device(f.ctx, device)
drawMatrix := C.fz_identity
C.fz_run_page(f.ctx, page, device, drawMatrix, nil)
C.fz_close_device(f.ctx, device)
pixels := C.fz_pixmap_samples(f.ctx, pixmap)
if pixels == nil {
return nil, ErrPixmapSamples
}
defer C.free(unsafe.Pointer(pixels))
img.Pix = C.GoBytes(unsafe.Pointer(pixels), C.int(4*bbox.x1*bbox.y1))
img.Rect = image.Rect(int(bbox.x0), int(bbox.y0), int(bbox.x1), int(bbox.y1))
img.Stride = 4 * img.Rect.Max.X
return &img, nil
}
// ImagePNG returns image for given page number as PNG bytes.
func (f *Document) ImagePNG(pageNumber int, dpi float64) ([]byte, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return nil, ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var bounds C.fz_rect
bounds = C.fz_bound_page(f.ctx, page)
var ctm C.fz_matrix
ctm = C.fz_scale(C.float(dpi/72), C.float(dpi/72))
var bbox C.fz_irect
bounds = C.fz_transform_rect(bounds, ctm)
bbox = C.fz_round_rect(bounds)
pixmap := C.fz_new_pixmap_with_bbox(f.ctx, C.fz_device_rgb(f.ctx), bbox, nil, 1)
if pixmap == nil {
return nil, ErrCreatePixmap
}
C.fz_clear_pixmap_with_value(f.ctx, pixmap, C.int(0xff))
defer C.fz_drop_pixmap(f.ctx, pixmap)
device := C.fz_new_draw_device(f.ctx, ctm, pixmap)
C.fz_enable_device_hints(f.ctx, device, C.FZ_NO_CACHE)
defer C.fz_drop_device(f.ctx, device)
drawMatrix := C.fz_identity
C.fz_run_page(f.ctx, page, device, drawMatrix, nil)
C.fz_close_device(f.ctx, device)
buf := C.fz_new_buffer_from_pixmap_as_png(f.ctx, pixmap, C.fz_default_color_params)
defer C.fz_drop_buffer(f.ctx, buf)
size := C.fz_buffer_storage(f.ctx, buf, nil)
str := C.GoStringN(C.fz_string_from_buffer(f.ctx, buf), C.int(size))
return []byte(str), nil
}
// Links returns slice of links for given page number.
func (f *Document) Links(pageNumber int) ([]Link, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return nil, ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
links := C.fz_load_links(f.ctx, page)
defer C.fz_drop_link(f.ctx, links)
linkCount := 0
for currLink := links; currLink != nil; currLink = currLink.next {
linkCount++
}
if linkCount == 0 {
return nil, nil
}
gLinks := make([]Link, linkCount)
currLink := links
for i := 0; i < linkCount; i++ {
gLinks[i] = Link{
URI: C.GoString(currLink.uri),
}
currLink = currLink.next
}
return gLinks, nil
}
// Text returns text for given page number.
func (f *Document) Text(pageNumber int) (string, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return "", ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var bounds C.fz_rect
bounds = C.fz_bound_page(f.ctx, page)
var ctm C.fz_matrix
ctm = C.fz_scale(C.float(72.0/72), C.float(72.0/72))
text := C.fz_new_stext_page(f.ctx, bounds)
defer C.fz_drop_stext_page(f.ctx, text)
var opts C.fz_stext_options
opts.flags = 0
device := C.fz_new_stext_device(f.ctx, text, &opts)
C.fz_enable_device_hints(f.ctx, device, C.FZ_NO_CACHE)
defer C.fz_drop_device(f.ctx, device)
var cookie C.fz_cookie
C.fz_run_page(f.ctx, page, device, ctm, &cookie)
C.fz_close_device(f.ctx, device)
buf := C.fz_new_buffer_from_stext_page(f.ctx, text)
defer C.fz_drop_buffer(f.ctx, buf)
str := C.GoString(C.fz_string_from_buffer(f.ctx, buf))
return str, nil
}
// HTML returns html for given page number.
func (f *Document) HTML(pageNumber int, header bool) (string, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return "", ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var bounds C.fz_rect
bounds = C.fz_bound_page(f.ctx, page)
var ctm C.fz_matrix
ctm = C.fz_scale(C.float(72.0/72), C.float(72.0/72))
text := C.fz_new_stext_page(f.ctx, bounds)
defer C.fz_drop_stext_page(f.ctx, text)
var opts C.fz_stext_options
opts.flags = C.FZ_STEXT_PRESERVE_IMAGES
device := C.fz_new_stext_device(f.ctx, text, &opts)
C.fz_enable_device_hints(f.ctx, device, C.FZ_NO_CACHE)
defer C.fz_drop_device(f.ctx, device)
var cookie C.fz_cookie
C.fz_run_page(f.ctx, page, device, ctm, &cookie)
C.fz_close_device(f.ctx, device)
buf := C.fz_new_buffer(f.ctx, 1024)
defer C.fz_drop_buffer(f.ctx, buf)
out := C.fz_new_output_with_buffer(f.ctx, buf)
defer C.fz_drop_output(f.ctx, out)
if header {
C.fz_print_stext_header_as_html(f.ctx, out)
}
C.fz_print_stext_page_as_html(f.ctx, out, text, C.int(pageNumber))
if header {
C.fz_print_stext_trailer_as_html(f.ctx, out)
}
str := C.GoString(C.fz_string_from_buffer(f.ctx, buf))
return str, nil
}
// SVG returns svg document for given page number.
func (f *Document) SVG(pageNumber int) (string, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return "", ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var bounds C.fz_rect
bounds = C.fz_bound_page(f.ctx, page)
var ctm C.fz_matrix
ctm = C.fz_scale(C.float(72.0/72), C.float(72.0/72))
bounds = C.fz_transform_rect(bounds, ctm)
buf := C.fz_new_buffer(f.ctx, 1024)
defer C.fz_drop_buffer(f.ctx, buf)
out := C.fz_new_output_with_buffer(f.ctx, buf)
defer C.fz_drop_output(f.ctx, out)
device := C.fz_new_svg_device(f.ctx, out, bounds.x1-bounds.x0, bounds.y1-bounds.y0, C.FZ_SVG_TEXT_AS_PATH, 1)
C.fz_enable_device_hints(f.ctx, device, C.FZ_NO_CACHE)
defer C.fz_drop_device(f.ctx, device)
var cookie C.fz_cookie
C.fz_run_page(f.ctx, page, device, ctm, &cookie)
C.fz_close_device(f.ctx, device)
str := C.GoString(C.fz_string_from_buffer(f.ctx, buf))
return str, nil
}
// ToC returns the table of contents (also known as outline).
func (f *Document) ToC() ([]Outline, error) {
data := make([]Outline, 0)
outline := C.fz_load_outline(f.ctx, f.doc)
if outline == nil {
return nil, ErrLoadOutline
}
defer C.fz_drop_outline(f.ctx, outline)
var walk func(outline *C.fz_outline, level int)
walk = func(outline *C.fz_outline, level int) {
for outline != nil {
res := Outline{}
res.Level = level
res.Title = C.GoString(outline.title)
res.URI = C.GoString(outline.uri)
res.Page = int(outline.page.page)
res.Top = float64(outline.y)
data = append(data, res)
if outline.down != nil {
walk(outline.down, level+1)
}
outline = outline.next
}
}
walk(outline, 1)
return data, nil
}
// Metadata returns the map with standard metadata.
func (f *Document) Metadata() map[string]string {
data := make(map[string]string)
lookup := func(key string) string {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
buf := make([]byte, 256)
C.fz_lookup_metadata(f.ctx, f.doc, ckey, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)))
return string(buf)
}
data["format"] = lookup("format")
data["encryption"] = lookup("encryption")
data["title"] = lookup("info:Title")
data["author"] = lookup("info:Author")
data["subject"] = lookup("info:Subject")
data["keywords"] = lookup("info:Keywords")
data["creator"] = lookup("info:Creator")
data["producer"] = lookup("info:Producer")
data["creationDate"] = lookup("info:CreationDate")
data["modDate"] = lookup("info:modDate")
return data
}
// Bound gives the Bounds of a given Page in the document.
func (f *Document) Bound(pageNumber int) (image.Rectangle, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return image.Rectangle{}, ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var bounds C.fz_rect
bounds = C.fz_bound_page(f.ctx, page)
return image.Rect(int(bounds.x0), int(bounds.y0), int(bounds.x1), int(bounds.y1)), nil
}
type BoundingBox struct {
X0, X1, Y0, Y1 float32
}
type Span struct {
Text string `json:"text"`
BBox BoundingBox `json:"bbox"`
}
type Line struct {
BBox BoundingBox `json:"bbox"`
Spans []Span `json:"spans"`
}
type Block struct {
Number int `json:"number"`
Lines []Line `json:"line"`
}
func (f *Document) WordBlocks(pageNumber int) ([]Block, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return nil, ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
var rect C.fz_rect
rect = C.fz_bound_page(f.ctx, page)
tpage := C.fz_new_stext_page(f.ctx, rect)
defer C.fz_drop_stext_page(f.ctx, tpage)
var opts C.fz_stext_options
device := C.fz_new_stext_device(f.ctx, tpage, &opts)
defer C.fz_drop_device(f.ctx, device)
drawMatrix := C.fz_identity
C.fz_run_page(f.ctx, page, device, drawMatrix, nil)
C.fz_close_device(f.ctx, device)
pageRect := tpage.mediabox
var blocks []Block
buf := C.fz_new_buffer(f.ctx, 1024)
defer C.fz_drop_buffer(f.ctx, buf)
i := -1
for block := tpage.first_block; block != nil; block = block.next {
i++
if C.fz_contains_rect(pageRect, block.bbox) == 0 &&
C.fz_is_infinite_rect(pageRect) == 0 {
continue
}
if C.fz_is_empty_rect(C.fz_intersect_rect(pageRect, block.bbox)) != 0 &&
C.fz_is_infinite_rect(pageRect) == 0 {
continue
}
if C.stext_block_type(block) == C.FZ_STEXT_BLOCK_IMAGE {
continue
}
blockResult := Block{
Number: i,
}
for line := C.first_text_line_from_stest_block(block); line != nil; line = line.next {
if C.fz_is_empty_rect(C.fz_intersect_rect(pageRect, line.bbox)) != 0 &&
C.fz_is_infinite_rect(pageRect) == 0 {
continue
}
blockResult.Lines = append(
blockResult.Lines,
f.loadLine(line, buf, pageRect),
)
}
blocks = append(blocks, blockResult)
}
return blocks, nil
}
func rectToBBox(rect C.fz_rect) BoundingBox {
return BoundingBox{
X0: float32(rect.x0),
X1: float32(rect.x1),
Y0: float32(rect.y0),
Y1: float32(rect.y1),
}
}
func (f *Document) loadLine(
line *C.fz_stext_line,
buf *C.fz_buffer,
pageRect C.fz_rect,
) Line {
C.fz_clear_buffer(f.ctx, buf)
lineRect := C.fz_empty_rect
spanRect := C.fz_empty_rect
var goLine Line
type Style struct {
Font string
Size float64
Color int
}
oldStyle := Style{
Size: -1,
Color: -1,
}
for ch := line.first_char; ch != nil; ch = ch.next {
rect := C.fz_rect_from_quad(C.char_quad(f.ctx, line, ch))
if C.fz_contains_rect(pageRect, rect) == 0 &&
C.fz_is_infinite_rect(pageRect) == 0 {
continue
}
style := Style{
Font: C.GoString(C.fz_font_name(f.ctx, ch.font)),
Color: int(ch.color),
Size: float64(ch.size),
}
if oldStyle.Size != style.Size ||
oldStyle.Font != style.Font ||
oldStyle.Color != style.Color {
if oldStyle.Size >= 0 && C.fz_is_empty_rect(spanRect) == 0 {
size := C.fz_buffer_storage(f.ctx, buf, nil)
text := C.GoStringN(C.fz_string_from_buffer(f.ctx, buf), C.int(size))
span := Span{
Text: text,
BBox: rectToBBox(spanRect),
}
C.fz_clear_buffer(f.ctx, buf)
lineRect = C.fz_union_rect(lineRect, spanRect)
goLine.Spans = append(goLine.Spans, span)
}
spanRect = rect
oldStyle = style
}
spanRect = C.fz_union_rect(spanRect, rect)
C.append_rune(f.ctx, buf, ch.c)
}
if C.fz_is_empty_rect(spanRect) == 0 {
size := C.fz_buffer_storage(f.ctx, buf, nil)
text := C.GoStringN(C.fz_string_from_buffer(f.ctx, buf), C.int(size))
span := Span{
Text: text,
BBox: rectToBBox(spanRect),
}
C.fz_clear_buffer(f.ctx, buf)
lineRect = C.fz_union_rect(lineRect, spanRect)
goLine.Spans = append(goLine.Spans, span)
}
goLine.BBox = rectToBBox(lineRect)
return goLine
}
func (f *Document) PageRotation(pageNumber int) (int, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if pageNumber >= f.NumPage() {
return 0, ErrPageMissing
}
page := C.fz_load_page(f.ctx, f.doc, C.int(pageNumber))
defer C.fz_drop_page(f.ctx, page)
pdfPage := C.pdf_page_from_fz_page(f.ctx, page)
if pdfPage == nil {
return 0, errors.New("Document isn't PDF")
}
rotation := int(C.page_rotation(f.ctx, pdfPage))
if rotation < 0 {
return rotation, errors.New("Failed to pull rotation")
}
return rotation, nil
}
// Close closes the underlying fitz document.
func (f *Document) Close() error {
if f.stream != nil {
C.fz_drop_stream(f.ctx, f.stream)
}
C.fz_drop_document(f.ctx, f.doc)
C.fz_drop_context(f.ctx)
f.data = nil
return nil
}