-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharu-pdf.r
executable file
·179 lines (165 loc) · 5.29 KB
/
haru-pdf.r
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
REBOL [
]
do %libhpdf.r
pdf-doc: make object! [
doc: none
new: does [ P: make self [] P/doc: hpdf-new 0 "" P ]
add-page: does [ pdf-page/new (hpdf-add-page doc) self ]
get-font: func [ font encod ] [ pdf-font/new hpdf-get-font doc font encod ]
save-to-file: func [ file ] [ hpdf-save-to-file doc to-string file ]
free: does [ hpdf-free doc ]
]
pdf-page: make object! [
page: none
docobj: none
fontobj: none
font-size: 0
new: func [ page- docobj- ] [ P: make self [] P/page: page- P/docobj: docobj- P ]
set-size: func [ size dir ] [ hpdf-page-set-size page size dir ]
get-width: does [ hpdf-page-get-with page ]
get-height: does [ hpdf-page-get-height page ]
set-font: func [ font size ] [
hpdf-page-set-font-and-size page font/font size
fontobj: font
font-size: size
]
begin-text: does [ hpdf-page-begin-text page ]
end-text: does [ hpdf-page-end-text page ]
move-text-pos: func [ x y ] [ hpdf-page-move-text-pos page x y ]
text-width: func [ text ] [ hpdf-page-text-width page text ]
show-text: func [ text ] [ hpdf-page-show-text page text ]
show-text-nl: func [ text ] [ hpdf-page-show-text-next-line page text ]
get-text-pos: func [ /local pos-struct ] [
pos-struct: make struct! [x [float] y [float]] none
hpdf-page-get-curr-text-pos page pos-struct
pos-struct
]
move-to: func [ x y ] [ hpdf-page-move-to page x y ]
line-to: func [ x y ] [ hpdf-page-line-to page x y ]
]
pdf-font: make object! [
font: none
new: func [ font- ] [ P: make self [] P/font: font- P ]
]
pdf-pen: make object! [
page: none
doc: none
w: 0 h: 0 padding: 0 line-h: 30
new: func [ page padding ] [
P: make self [] P/page: page
P/w: page/get-width P/h: page/get-height P/padding: padding
P
]
to-offset: func [ x y ] [ page/move-text-pos (padding + x) (h - padding + y) ]
break: func [ line-h ] [
page/move-text-pos 0 (- line-h)
pos: page/get-text-pos
if lesser? pos/y 50 [
pdf-util/break-page page padding
]
]
move: func [ x y ] [ page/move-text-pos x y ]
]
pdf-util: make object! [
break-page: func [ page padding- /local new-page ] [
page/end-text
new-page: page/docobj/add-page
page/page: new-page/page
page/set-size HPDF-A4 HPDF-PORTRAIT ;;; fix this
page/set-font page/fontobj page/font-size ;;; make it better structured !!!!!!!!!!!!!!!!!!!!
page/begin-text
pen/to-offset 0 (- padding-)
]
]
pdf-table: make object! [
page: none cols: none row-h: 0 breaks-pages: true
new: func [ page cols row-h ] [
P: make self [] P/page: page P/cols: cols P/row-h: row-h
P
]
show-row: func [ vals props /local w fwd i rows ] [
rows: 1
repeat i ((length? cols) - 1) [
switch props/:i [
l [
page/move-text-pos cols/:i 0
page/show-text vals/:i
]
r [
w: page/text-width vals/:i
page/move-text-pos (cols/:i + ( fwd: (cols/(i + 1) - w) )) 0
page/show-text vals/:i
page/move-text-pos (- fwd) 0
]
c [
w: page/text-width vals/:i
c: ((cols/(i + 1)) - cols/:i) / 2
page/move-text-pos (cols/:i + ( fwd: c - (w / 2)) ) 0
page/show-text vals/:i
page/move-text-pos (- fwd) 0
]
lw [
page/move-text-pos cols/:i 0 ; move to this cell
cw: cols/(i + 1) - cols/:i ; cell width
lines: get-wraped-lines vals/:i cw
backup: 0 cellrows: 0
foreach line lines [
page/show-text line
page/move-text-pos 0 (- row-h )
backup: backup + row-h
cellrows: cellrows + 1
]
page/move-text-pos 0 backup
if greater? cellrows rows [ rows: cellrows ]
]
cw [
page/move-text-pos cols/:i 0 ; move to this cell
cw: cols/(i + 1) - cols/:i ; cell width
lines: get-wraped-lines vals/:i cw
backup: 0 cellrows: 0
foreach line lines [
w: page/text-width line
c: ((cols/(i + 1)) - cols/:i) / 2
page/move-text-pos (cols/:i + ( fwd: c - (w / 2)) ) 0
page/show-text line
page/move-text-pos (- fwd) 0
; page/show-text line
page/move-text-pos 0 (- row-h )
backup: backup + row-h
cellrows: cellrows + 1
]
page/move-text-pos 0 backup
if greater? cellrows rows [ rows: cellrows ]
]
]
]
page/move-text-pos (- sum-cols) (- row-h * rows)
pos: page/get-text-pos
if all [ breaks-pages lesser? pos/y 120 ] [
pdf-util/break-page page 80 ;; LATER make it so that line height for all cells in current row is precalculated and page
] ;; break is determined on that
]
show-line: func [ /local posit ] [ ;; just ugly hack until get-text-pos works
page/move-text-pos 0 ( row-h * 0.75 )
page/show-text "____________________________________________________________________________________"
page/move-text-pos 0 (- (row-h * 0.9 ))
]
get-pos: func [ local /posit ] [ posit: page/get-text-pos ] ; pos: page/get-text-pos ?? pos ] ;page/move-to pos/x pos/y page/line-to (pos/x + 100) pos/y ]
sum-cols: func [ /local a i ] [ a: 0 repeat i ((length? cols) - 1) [ a: a + cols/:i ] a ]
get-wraped-lines: func [ text width /local res pointer previous tpart] [
res: copy []
pointer: text
previous: pointer
forall pointer [
if find/match pointer " " [
if (greater? (page/text-width tpart: (copy/part text pointer)) width) [
append res copy/part text previous
text: next previous
]
previous: pointer
]
]
append res copy text
res
]
]