/
model.go
252 lines (219 loc) · 4.7 KB
/
model.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
// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"github.com/hailiang/color"
"go-mupdf/fz"
// "sync"
// "time"
"unsafe"
)
type RenderOption struct {
Width int
Dark bool
Brightness float64
}
type Model struct {
ctx *fz.Context
doc *fz.Document
// render cache
RenderOption
height int
heights []int
cache []Pixel
cached []bool
stop bool
// mu sync.Mutex // cache lock
// wg sync.WaitGroup
}
func newModel() (*Model, error) {
ctx := fz.AllocDefault.NewContext(nil, fz.StoreDefault)
return &Model{
ctx: ctx,
RenderOption: RenderOption{
Dark: true,
Brightness: 0.45,
},
}, nil
}
func (m *Model) Open(file string) error {
if m.doc != nil {
m.doc.Close()
m.doc = nil
}
m.doc = m.ctx.OpenDocument(file)
return nil
}
func (m *Model) PageCount() int {
return m.doc.CountPages()
}
func (m *Model) Height() int {
return len(m.cache) / m.RenderOption.Width
}
func (m *Model) setWidth(width int) {
if width != m.Width {
m.Width = width
m.heights = make([]int, m.PageCount())
m.height = 0
for i := 0; i < m.PageCount(); i++ {
page := m.doc.LoadPage(i)
var bounds fz.Rect
m.doc.BoundPage(page, &bounds)
zoom := float32(width) / bounds.X1
var transform fz.Matrix
transform.Scale(zoom, zoom)
bounds.Transform(&transform)
var bbox fz.Irect
bbox.RoundRect(&bounds)
m.height += int(bbox.Y1)
m.heights[i] = int(bbox.Y1)
m.doc.FreePage(page)
}
m.cache = make([]Pixel, m.Width*m.height)
m.cached = make([]bool, m.PageCount())
}
}
func (m *Model) FlipDarkMode() {
m.Dark = !m.Dark
m.cached = make([]bool, m.PageCount())
}
/*
func (m *Model) stopEagerLoad() {
m.mu.Lock()
defer m.mu.Unlock()
m.stop = true
m.wg.Wait()
m.stop = false
}
func (m *Model) eagerLoad() {
m.stopEagerLoad()
m.wg.Add(1)
go func() {
for i := 0; i < m.PageCount(); i++ {
if m.stop {
break
}
if m.cached[i] {
continue
}
m.renderPage(i)
time.Sleep(10 * time.Millisecond)
}
m.wg.Done()
}()
}
*/
func (m *Model) renderPage(pageIndex int) {
if m.cached[pageIndex] {
return
}
pageStart := 0
for i := 0; i < pageIndex; i++ {
pageStart += m.heights[i]
}
renderPage(m.ctx, m.doc, pageIndex, &m.RenderOption, m.cache[m.Width*pageStart:])
m.cached[pageIndex] = true
}
func (m *Model) Get(y, h, w int) []Pixel {
m.setWidth(w)
pageStart := 0
for i := 0; i < m.PageCount(); i++ {
pageEnd := pageStart + m.heights[i]
if !m.cached[i] {
if pageEnd >= y && pageStart <= y+h {
m.renderPage(i)
}
}
pageStart = pageEnd
}
srcStart := m.Width * y
srcEnd := srcStart + m.Width*h
if srcEnd > len(m.cache) {
srcEnd = len(m.cache)
}
return m.cache[srcStart:srcEnd]
}
func renderPage(ctx *fz.Context, doc *fz.Document, pageIndex int, opt *RenderOption, buf []Pixel) {
page := doc.LoadPage(pageIndex)
defer doc.FreePage(page)
var bounds fz.Rect
doc.BoundPage(page, &bounds)
zoom := float32(opt.Width) / bounds.X1
var transform fz.Matrix
transform.Scale(zoom, zoom)
bounds.Transform(&transform)
var bbox fz.Irect
bbox.RoundRect(&bounds)
pix := ctx.NewPixmapWithBboxAndData(ctx.DeviceRgb(), &bbox,
(*byte)(unsafe.Pointer(&buf[0])))
ctx.ClearPixmapWithValue(pix, 0xFF)
dev := fz.NewDeviceHook(ctx.NewDrawDevice(pix))
defer dev.Free()
var imgRects Rects
dev.ImageHook = func(image *fz.Image, ctm *fz.Matrix, alpha float32) {
pt := fz.Point{0, 0}
pt.Transform(ctm)
x, y := int(pt.X), int(pt.Y)
w := round(sqrtf(ctm.A*ctm.A+ctm.B*ctm.B) + 1)
h := round(sqrtf(ctm.C*ctm.C+ctm.D*ctm.D) + 1)
imgRects = append(imgRects, &Rect{x, y, w, h})
}
doc.RunPage(page, dev.ToDevice(), &transform, nil)
if opt.Dark {
pixels := buf
off := 0
b := opt.Brightness
bb := b * b
for y := 0; y < int(pix.H); y++ {
for x := 0; x < int(pix.W); x++ {
pixel := pixels[off+x]
rgb := color.RGBFromBytes(pixel.GetRGB())
if !imgRects.In(x, y) {
hcl := rgb.ToHCL()
hcl.L = 1 - hcl.L
rgb = hcl.ToRGB()
rgb.Times(b)
} else {
rgb.Times(bb)
}
pixels[off+x].SetRGB(rgb.ToBytes())
}
off += int(pix.W)
}
}
}
func (m *Model) disposePageCache() {
m.cached = nil
m.cache = nil
}
func (m *Model) Dispose() {
m.disposePageCache()
if m.doc != nil {
m.doc.Close()
m.doc = nil
}
if m.ctx != nil {
m.ctx.FreeContext()
m.ctx = nil
}
}
type Rect struct {
X int
Y int
Width int
Height int
}
func (r *Rect) In(x, y int) bool {
return x >= r.X && x < r.X+r.Width &&
y >= r.Y && y < r.Y+r.Height
}
type Rects []*Rect
func (rs Rects) In(x, y int) bool {
for _, r := range rs {
if r.In(x, y) {
return true
}
}
return false
}