| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
FUNCTIONS = 'x y scroll_to scroll_into_view reset_globals __reset_transforms window_scroll_pos content_size'.split(' ') |
| 6 |
|
| 7 |
from read_book.globals import get_boss, viewport_mode_changer |
| 8 |
from utils import document_height, document_width, ios_major_version |
| 9 |
|
| 10 |
|
| 11 |
class ScrollViewport: |
| 12 |
def __init__(self): |
| 13 |
self.set_mode('flow') |
| 14 |
self.window_width_from_parent = self.window_height_from_parent = None |
| 15 |
# In RTL mode, we hide the fact that we are scrolling to the left by negating the |
| 16 |
# current X position and the requested X scroll position, which fools the reader |
| 17 |
# code into thinking that it's always scrolling in positive X. |
| 18 |
self.rtl = False |
| 19 |
self.ltr = True |
| 20 |
self.vertical_writing_mode = False |
| 21 |
self.horizontal_writing_mode = True |
| 22 |
|
| 23 |
def set_mode(self, mode): |
| 24 |
prefix = ('flow' if mode is 'flow' else 'paged') + '_' |
| 25 |
for attr in FUNCTIONS: |
| 26 |
self[attr] = self[prefix + attr] |
| 27 |
|
| 28 |
def initialize_on_layout(self, body_style): |
| 29 |
self.horizontal_writing_mode = True |
| 30 |
self.vertical_writing_mode = False |
| 31 |
self.ltr = True |
| 32 |
self.rtl = False |
| 33 |
if body_style.direction is 'rtl': |
| 34 |
self.rtl = True |
| 35 |
self.ltr = False |
| 36 |
|
| 37 |
css_vertical_rl = body_style.getPropertyValue('writing-mode') is 'vertical-rl' |
| 38 |
if css_vertical_rl: |
| 39 |
self.vertical_writing_mode = True |
| 40 |
self.horizontal_writing_mode = False |
| 41 |
self.rtl = True |
| 42 |
self.ltr = False |
| 43 |
else: |
| 44 |
css_vertical_lr = body_style.getPropertyValue('writing-mode') is 'vertical-lr' |
| 45 |
if css_vertical_lr: |
| 46 |
self.vertical_writing_mode = True |
| 47 |
self.horizontal_writing_mode = False |
| 48 |
self.ltr = True |
| 49 |
self.rtl = False |
| 50 |
|
| 51 |
def flow_x(self): |
| 52 |
if self.rtl: |
| 53 |
return -window.pageXOffset |
| 54 |
return window.pageXOffset |
| 55 |
|
| 56 |
def flow_y(self): |
| 57 |
return window.pageYOffset |
| 58 |
|
| 59 |
def flow_window_scroll_pos(self): |
| 60 |
return window.pageXOffset, window.pageYOffset |
| 61 |
|
| 62 |
def inline_pos(self): |
| 63 |
if self.vertical_writing_mode: |
| 64 |
return self.y() |
| 65 |
return self.x() |
| 66 |
|
| 67 |
def block_pos(self): |
| 68 |
if self.horizontal_writing_mode: |
| 69 |
return self.y() |
| 70 |
return self.x() |
| 71 |
|
| 72 |
def flow_scroll_to(self, x, y): |
| 73 |
if self.rtl: |
| 74 |
window.scrollTo(-x, y) |
| 75 |
else: |
| 76 |
window.scrollTo(x, y) |
| 77 |
|
| 78 |
def scroll_to_in_inline_direction(self, pos, preserve_other): |
| 79 |
# Lines flow vertically, so inline is vertical. |
| 80 |
if self.vertical_writing_mode: |
| 81 |
self.scroll_to(self.x() if preserve_other else 0, pos) |
| 82 |
else: |
| 83 |
self.scroll_to(pos, self.y() if preserve_other else 0) |
| 84 |
|
| 85 |
def scroll_to_in_block_direction(self, pos, preserve_other): |
| 86 |
# In horizontal modes, the block direction is vertical. |
| 87 |
if self.horizontal_writing_mode: |
| 88 |
self.scroll_to(self.x() if preserve_other else 0, pos) |
| 89 |
# In vertical modes, the block direction is horizontal. |
| 90 |
else: |
| 91 |
self.scroll_to(pos, self.y() if preserve_other else 0) |
| 92 |
|
| 93 |
def flow_scroll_into_view(self, elem): |
| 94 |
elem.scrollIntoView() |
| 95 |
|
| 96 |
def scroll_by(self, x, y): |
| 97 |
if self.ltr: |
| 98 |
window.scrollBy(x, y) |
| 99 |
# Swap inline direction if in RTL mode. |
| 100 |
else: |
| 101 |
window.scrollBy(-x, y) |
| 102 |
|
| 103 |
def scroll_by_in_inline_direction(self, offset): |
| 104 |
# Same logic as scroll_to_in_inline_direction |
| 105 |
if self.vertical_writing_mode: |
| 106 |
self.scroll_by(0, offset) |
| 107 |
else: |
| 108 |
self.scroll_by(offset, 0) |
| 109 |
|
| 110 |
def scroll_by_in_block_direction(self, offset): |
| 111 |
# Same logic as scroll_to_in_block_direction |
| 112 |
if self.horizontal_writing_mode: |
| 113 |
self.scroll_by(0, offset) |
| 114 |
else: |
| 115 |
self.scroll_by(offset, 0) |
| 116 |
|
| 117 |
def flow_reset_globals(self): |
| 118 |
pass |
| 119 |
|
| 120 |
def flow___reset_transforms(self): |
| 121 |
pass |
| 122 |
|
| 123 |
def flow_content_size(self): |
| 124 |
return document.documentElement.scrollWidth, document.documentElement.scrollHeight |
| 125 |
|
| 126 |
def paged_content_inline_size(self): |
| 127 |
w, h = self.content_size() |
| 128 |
return w if self.horizontal_writing_mode else h |
| 129 |
|
| 130 |
def paged_content_block_size(self): |
| 131 |
w, h = self.content_size() |
| 132 |
return h if self.horizontal_writing_mode else w |
| 133 |
|
| 134 |
def inline_size(self): |
| 135 |
if self.horizontal_writing_mode: |
| 136 |
return self.width() |
| 137 |
return self.height() |
| 138 |
|
| 139 |
def block_size(self): |
| 140 |
if self.horizontal_writing_mode: |
| 141 |
return self.height() |
| 142 |
return self.width() |
| 143 |
|
| 144 |
def update_window_size(self, w, h): |
| 145 |
self.window_width_from_parent = w |
| 146 |
self.window_height_from_parent = h |
| 147 |
|
| 148 |
def width(self): |
| 149 |
return window.innerWidth |
| 150 |
|
| 151 |
def height(self): |
| 152 |
return window.innerHeight |
| 153 |
|
| 154 |
def document_inline_size(self): |
| 155 |
if self.horizontal_writing_mode: |
| 156 |
return document_width() |
| 157 |
return document_height() |
| 158 |
|
| 159 |
def document_block_size(self): |
| 160 |
if self.horizontal_writing_mode: |
| 161 |
return document_height() |
| 162 |
return document_width() |
| 163 |
|
| 164 |
# Assure that the viewport position returned is corrected for the RTL |
| 165 |
# mode of ScrollViewport. |
| 166 |
def viewport_to_document(self, x, y, doc): |
| 167 |
# Convert x, y from the viewport (window) coordinate system to the |
| 168 |
# document (body) coordinate system |
| 169 |
doc = doc or window.document |
| 170 |
topdoc = window.document |
| 171 |
while doc is not topdoc and doc.defaultView: |
| 172 |
# We are in a frame |
| 173 |
frame = doc.defaultView.frameElement |
| 174 |
rect = frame.getBoundingClientRect() |
| 175 |
x += rect.left |
| 176 |
y += rect.top |
| 177 |
doc = frame.ownerDocument |
| 178 |
wx, wy = self.window_scroll_pos() |
| 179 |
x += wx |
| 180 |
y += wy |
| 181 |
if self.rtl: |
| 182 |
x *= -1 |
| 183 |
return x, y |
| 184 |
|
| 185 |
def rect_inline_start(self, rect): |
| 186 |
# Lines start on the left in LTR mode, right in RTL mode |
| 187 |
if self.horizontal_writing_mode: |
| 188 |
return rect.left if self.ltr else rect.right |
| 189 |
# Only top-to-bottom vertical writing is supported |
| 190 |
return rect.top |
| 191 |
|
| 192 |
def rect_inline_end(self, rect): |
| 193 |
# Lines end on the right in LTR mode, left in RTL mode |
| 194 |
if self.horizontal_writing_mode: |
| 195 |
return rect.right if self.ltr else rect.left |
| 196 |
# In top-to-bottom (the only vertical mode supported), bottom: |
| 197 |
return rect.bottom |
| 198 |
|
| 199 |
def rect_block_start(self, rect): |
| 200 |
# Block flows top to bottom in horizontal modes |
| 201 |
if self.horizontal_writing_mode: |
| 202 |
return rect.top |
| 203 |
# Block flows either left or right in vertical modes |
| 204 |
return rect.left if self.ltr else rect.right |
| 205 |
|
| 206 |
def rect_block_end(self, rect): |
| 207 |
# Block flows top to bottom in horizontal modes |
| 208 |
if self.horizontal_writing_mode: |
| 209 |
return rect.bottom |
| 210 |
# Block flows either left or right in vertical modes |
| 211 |
return rect.right if self.ltr else rect.left |
| 212 |
|
| 213 |
def rect_inline_size(self, rect): |
| 214 |
# Lines go horizontally in horizontal writing, so use width |
| 215 |
if self.horizontal_writing_mode: |
| 216 |
return rect.width |
| 217 |
return rect.height |
| 218 |
|
| 219 |
def rect_block_size(self, rect): |
| 220 |
# The block is vertical in horizontal writing, so use height |
| 221 |
if self.horizontal_writing_mode: |
| 222 |
return rect.height |
| 223 |
return rect.width |
| 224 |
|
| 225 |
# Returns document inline coordinate (1 value) at viewport inline coordinate |
| 226 |
def viewport_to_document_inline(self, pos, doc): |
| 227 |
# Lines flow horizontally in horizontal mode |
| 228 |
if self.horizontal_writing_mode: |
| 229 |
return self.viewport_to_document(pos, 0, doc)[0] |
| 230 |
# Inline is vertical in vertical mode |
| 231 |
return self.viewport_to_document(0, pos, doc)[1] |
| 232 |
|
| 233 |
# Returns document block coordinate (1 value) at viewport block coordinate |
| 234 |
def viewport_to_document_block(self, pos, doc): |
| 235 |
# Block is vertical in horizontal mode |
| 236 |
if self.horizontal_writing_mode: |
| 237 |
return self.viewport_to_document(0, pos, doc)[1] |
| 238 |
# Horizontal in vertical mode |
| 239 |
return self.viewport_to_document(pos, 0, doc)[0] |
| 240 |
|
| 241 |
def viewport_to_document_inline_block(self, inline, block, doc): |
| 242 |
if self.horizontal_writing_mode: |
| 243 |
return self.viewport_to_document(inline, block, doc) |
| 244 |
return self.viewport_to_document(block, inline, doc) |
| 245 |
|
| 246 |
|
| 247 |
class IOSScrollViewport(ScrollViewport): |
| 248 |
def width(self): |
| 249 |
return self.window_width_from_parent or window.innerWidth |
| 250 |
|
| 251 |
def height(self): |
| 252 |
return self.window_height_from_parent or window.innerHeight |
| 253 |
|
| 254 |
def _scroll_implementation(self, x, y): |
| 255 |
if x is 0 and y is 0: |
| 256 |
document.documentElement.style.transform = 'none' |
| 257 |
else: |
| 258 |
x *= -1 |
| 259 |
y *= -1 |
| 260 |
document.documentElement.style.transform = f'translateX({x}px) translateY({y}px)' |
| 261 |
|
| 262 |
def paged_scroll_to(self, x, y): |
| 263 |
x = x or 0 |
| 264 |
y = y or 0 |
| 265 |
if self.ltr: |
| 266 |
self._scroll_implementation(x, y) |
| 267 |
else: |
| 268 |
self._scroll_implementation(-x, y) |
| 269 |
boss = get_boss() |
| 270 |
if boss: |
| 271 |
boss.onscroll() |
| 272 |
|
| 273 |
def transform_val(self, y): |
| 274 |
raw = document.documentElement.style.transform |
| 275 |
if not raw or raw is 'none': |
| 276 |
return 0 |
| 277 |
idx = raw.lastIndexOf('(') if y else raw.indexOf('(') |
| 278 |
raw = raw[idx + 1:] |
| 279 |
ans = parseInt(raw) |
| 280 |
if isNaN(ans): |
| 281 |
ans = 0 |
| 282 |
return ans |
| 283 |
|
| 284 |
def paged_x(self): |
| 285 |
ans = self.transform_val() |
| 286 |
if self.ltr: |
| 287 |
ans *= -1 |
| 288 |
return ans |
| 289 |
|
| 290 |
def paged_y(self): |
| 291 |
return -1 * self.transform_val(True) |
| 292 |
|
| 293 |
def paged_scroll_into_view(self, elem): |
| 294 |
left = elem.offsetLeft |
| 295 |
if left is None: |
| 296 |
return # element has display: none |
| 297 |
elem.scrollIntoView() |
| 298 |
window.scrollTo(0, 0) |
| 299 |
p = elem.offsetParent |
| 300 |
while p: |
| 301 |
left += p.offsetLeft |
| 302 |
p = p.offsetParent |
| 303 |
# left -= window_width() // 2 |
| 304 |
self._scroll_implementation(max(0, left), 0) |
| 305 |
|
| 306 |
def paged_window_scroll_pos(self): |
| 307 |
return self.transform_val(), self.transform_val(True) |
| 308 |
|
| 309 |
def paged_content_size(self): |
| 310 |
w = document.documentElement.scrollWidth |
| 311 |
h = document.documentElement.scrollHeight |
| 312 |
return w - self.transform_val(), h - self.transform_val(True) |
| 313 |
|
| 314 |
def paged___reset_transforms(self): |
| 315 |
s = document.documentElement.style |
| 316 |
if s.transform is not 'none': |
| 317 |
s.transform = 'none' |
| 318 |
|
| 319 |
def paged_reset_globals(self): |
| 320 |
self.__reset_transforms() |
| 321 |
|
| 322 |
|
| 323 |
if 1 < ios_major_version < 15: |
| 324 |
scroll_viewport = IOSScrollViewport() |
| 325 |
else: |
| 326 |
scroll_viewport = ScrollViewport() |
| 327 |
for attr in FUNCTIONS: |
| 328 |
if not scroll_viewport['paged_' + attr]: |
| 329 |
scroll_viewport['paged_' + attr] = scroll_viewport[attr] |
| 330 |
viewport_mode_changer(scroll_viewport.set_mode) |
| 331 |
|
| 332 |
|
| 333 |
def get_unit_size_in_pixels(unit): |
| 334 |
d = document.createElement('span') |
| 335 |
d.style.position = 'absolute' |
| 336 |
d.style.visibility = 'hidden' |
| 337 |
d.style.width = f'1{unit}' |
| 338 |
d.style.fontSize = f'1{unit}' |
| 339 |
d.style.paddingTop = d.style.paddingBottom = d.style.paddingLeft = d.style.paddingRight = '0' |
| 340 |
d.style.marginTop = d.style.marginBottom = d.style.marginLeft = d.style.marginRight = '0' |
| 341 |
d.style.borderStyle = 'none' |
| 342 |
document.body.appendChild(d) |
| 343 |
ans = d.clientWidth |
| 344 |
document.body.removeChild(d) |
| 345 |
return ans |
| 346 |
|
| 347 |
|
| 348 |
def rem_size(reset): |
| 349 |
if reset: |
| 350 |
rem_size.ans = None |
| 351 |
return |
| 352 |
if not rem_size.ans: |
| 353 |
d = document.createElement('span') |
| 354 |
d.style.position = 'absolute' |
| 355 |
d.style.visibility = 'hidden' |
| 356 |
d.style.width = '1rem' |
| 357 |
d.style.fontSize = '1rem' |
| 358 |
d.style.paddingTop = d.style.paddingBottom = d.style.paddingLeft = d.style.paddingRight = '0' |
| 359 |
d.style.marginTop = d.style.marginBottom = d.style.marginLeft = d.style.marginRight = '0' |
| 360 |
d.style.borderStyle = 'none' |
| 361 |
document.body.appendChild(d) |
| 362 |
rem_size.ans = max(2, d.clientWidth) |
| 363 |
document.body.removeChild(d) |
| 364 |
return rem_size.ans |
| 365 |
|
| 366 |
|
| 367 |
def line_height(reset): |
| 368 |
if reset: |
| 369 |
line_height.ans = None |
| 370 |
return |
| 371 |
if not line_height.ans: |
| 372 |
ds = window.getComputedStyle(document.body) |
| 373 |
try: |
| 374 |
# will fail if line-height = "normal" |
| 375 |
lh = float(ds.lineHeight) |
| 376 |
except: |
| 377 |
try: |
| 378 |
lh = 1.2 * float(ds.fontSize) |
| 379 |
except: |
| 380 |
lh = 15 |
| 381 |
line_height.ans = max(5, lh) |
| 382 |
return line_height.ans |