| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from read_book.globals import current_layout_mode |
| 6 |
from read_book.viewport import scroll_viewport |
| 7 |
|
| 8 |
ignored_tags = { |
| 9 |
'style': True, 'script': True, 'noscript': True, 'title': True, 'meta': True, 'head': True, 'link': True, 'html': True, |
| 10 |
'img': True, 'rt': True, 'rp': True, 'rtc': True, |
| 11 |
} |
| 12 |
|
| 13 |
block_tags_for_tts = { |
| 14 |
'h1': True, |
| 15 |
'h2': True, |
| 16 |
'h3': True, |
| 17 |
'h4': True, |
| 18 |
'h5': True, |
| 19 |
'h6': True, |
| 20 |
'p': True, |
| 21 |
'div': True, |
| 22 |
'table': True, |
| 23 |
'th': True, |
| 24 |
'tr': True, |
| 25 |
'td': True, |
| 26 |
'section': True, |
| 27 |
'article': True, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
def build_text_map(for_tts): |
| 32 |
node_list = [] |
| 33 |
flat_text = '' |
| 34 |
text_node_type = Node.TEXT_NODE |
| 35 |
element_node_type = Node.ELEMENT_NODE |
| 36 |
in_ruby = 0 |
| 37 |
|
| 38 |
def process_node(node): |
| 39 |
nonlocal flat_text, in_ruby |
| 40 |
nt = node.nodeType |
| 41 |
if nt is text_node_type: |
| 42 |
text = node.nodeValue |
| 43 |
if text and text.length: |
| 44 |
if in_ruby: |
| 45 |
rtext = text.trim() |
| 46 |
if rtext.length: |
| 47 |
node_list.push(v"{node: node, offset: flat_text.length, length: rtext.length, offset_in_node: text.length - text.trimStart().length}") |
| 48 |
flat_text += rtext |
| 49 |
else: |
| 50 |
node_list.push(v"{node: node, offset: flat_text.length, length: text.length}") |
| 51 |
flat_text += text |
| 52 |
elif nt is element_node_type: |
| 53 |
if not node.hasChildNodes(): |
| 54 |
return |
| 55 |
tag = node.tagName.toLowerCase() |
| 56 |
if ignored_tags[tag]: |
| 57 |
return |
| 58 |
is_ruby_tag = tag is 'ruby' |
| 59 |
if is_ruby_tag: |
| 60 |
in_ruby += 1 |
| 61 |
children = node.childNodes |
| 62 |
for i in range(children.length): |
| 63 |
process_node(v'children[i]') |
| 64 |
if is_ruby_tag: |
| 65 |
in_ruby -= 1 |
| 66 |
if for_tts and block_tags_for_tts[tag]: |
| 67 |
# add a paragraph separator after block tags so that sentence splitting works |
| 68 |
if flat_text.length and ' \n\t\r'.indexOf(flat_text[-1]) > -1: |
| 69 |
flat_text = flat_text[:-1] + '\u2029' |
| 70 |
elif node_list.length: |
| 71 |
flat_text += '\u2029' |
| 72 |
node_list[-1].length += 1 |
| 73 |
|
| 74 |
process_node(document.body) |
| 75 |
return {'timestamp': window.performance.now(), 'flat_text': flat_text, 'node_list': node_list} |
| 76 |
|
| 77 |
|
| 78 |
def index_for_node(node, node_list): |
| 79 |
for entry in node_list: |
| 80 |
if entry.node.isSameNode(node): |
| 81 |
return entry.offset |
| 82 |
|
| 83 |
|
| 84 |
def tts_word_regex(): |
| 85 |
# Cf is Other, formatting, including soft hyphens zero-width joiners, etc |
| 86 |
return /[\p{Letter}\p{Mark}\p{Number}\p{Punctuation}\p{Cf}]{1,50}/gu |
| 87 |
|
| 88 |
|
| 89 |
def cached_tts_text_map(): |
| 90 |
if not cache.tts_text_map: |
| 91 |
cache.tts_text_map = build_text_map(True) |
| 92 |
return cache.tts_text_map |
| 93 |
|
| 94 |
|
| 95 |
def first_tts_word_after_viewport_start(): |
| 96 |
# Find the first text node (in reading order) positioned at or after the |
| 97 |
# start of the current viewport/page. Used when there is no text visible |
| 98 |
# in the viewport, e.g. a full page image. |
| 99 |
text_map = cached_tts_text_map() |
| 100 |
is_flow = current_layout_mode() is 'flow' |
| 101 |
r = document.createRange() |
| 102 |
for entry in text_map.node_list: |
| 103 |
r.selectNodeContents(entry.node) |
| 104 |
rect = r.getBoundingClientRect() |
| 105 |
if not rect.width and not rect.height: |
| 106 |
continue |
| 107 |
pos = scroll_viewport.rect_block_start(rect) if is_flow else scroll_viewport.rect_inline_start(rect) |
| 108 |
if pos >= 0: |
| 109 |
return entry |
| 110 |
|
| 111 |
|
| 112 |
def tts_data(text_node, offset): |
| 113 |
offset_in_flat_text = offset or 0 |
| 114 |
text_map = cached_tts_text_map() |
| 115 |
if text_node: |
| 116 |
offset_in_flat_text += index_for_node(text_node, text_map.node_list) or 0 |
| 117 |
match = None |
| 118 |
first = True |
| 119 |
last = None |
| 120 |
marked_text = [] |
| 121 |
text = text_map.flat_text[offset_in_flat_text:] |
| 122 |
for v'match of text.matchAll(tts_word_regex())': |
| 123 |
start = match.index |
| 124 |
if first: |
| 125 |
first = False |
| 126 |
if start: |
| 127 |
marked_text.push(text[:start]) |
| 128 |
elif start > last: |
| 129 |
marked_text.push(text[last:start]) |
| 130 |
|
| 131 |
marked_text.push(start + offset_in_flat_text) |
| 132 |
marked_text.push(match[0]) |
| 133 |
last = start + match[0].length |
| 134 |
if last is None: |
| 135 |
marked_text.push(text) |
| 136 |
else: |
| 137 |
trailer = text[last:] |
| 138 |
if trailer: |
| 139 |
marked_text.push(trailer) |
| 140 |
return marked_text |
| 141 |
|
| 142 |
|
| 143 |
def find_node_for_index_binary(node_list, idx_in_flat_text, start): |
| 144 |
# Do a binary search for idx |
| 145 |
start = start or 0 |
| 146 |
end = node_list.length - 1 |
| 147 |
while start <= end: |
| 148 |
mid = Math.floor((start + end) / 2) |
| 149 |
q = node_list[mid] |
| 150 |
limit = q.offset + q.length |
| 151 |
if q.offset <= idx_in_flat_text and limit > idx_in_flat_text: |
| 152 |
start_node = q.node |
| 153 |
start_offset = idx_in_flat_text - q.offset |
| 154 |
return start_node, start_offset + (q.offset_in_node or 0), mid |
| 155 |
if limit <= idx_in_flat_text: |
| 156 |
start = mid + 1 |
| 157 |
else: |
| 158 |
end = mid - 1 |
| 159 |
return None, None, None |
| 160 |
|
| 161 |
|
| 162 |
def get_occurrence_data(node_list, start, end): |
| 163 |
start_node, start_offset, start_pos = find_node_for_index_binary(node_list, start) |
| 164 |
if start_node is not None: |
| 165 |
end_node, end_offset, node_pos = find_node_for_index_binary(node_list, end, start_pos) |
| 166 |
if end_node is not None: |
| 167 |
return { |
| 168 |
'start_node': start_node, 'start_offset': start_offset, 'start_pos': start_pos, |
| 169 |
'end_node': end_node, 'end_offset': end_offset, 'end_pos': node_pos, |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
def find_specific_occurrence(q, num, before_len, after_len, text_map, from_offset): |
| 174 |
if not q or not q.length: |
| 175 |
return |
| 176 |
from_idx = from_offset or 0 |
| 177 |
flat_text = text_map.flat_text |
| 178 |
match_num = -1 |
| 179 |
while True: |
| 180 |
idx = flat_text.indexOf(q, from_idx) |
| 181 |
if idx < 0: |
| 182 |
break |
| 183 |
match_num += 1 |
| 184 |
from_idx = idx + 1 |
| 185 |
if match_num < num: |
| 186 |
continue |
| 187 |
return get_occurrence_data(text_map.node_list, idx + before_len, idx + q.length - after_len) |
| 188 |
|
| 189 |
|
| 190 |
cache = {} |
| 191 |
|
| 192 |
|
| 193 |
def reset_find_caches(): |
| 194 |
nonlocal cache |
| 195 |
cache = {} |
| 196 |
|
| 197 |
|
| 198 |
def select_find_result(match): |
| 199 |
sel = window.getSelection() |
| 200 |
try: |
| 201 |
sel.setBaseAndExtent(match.start_node, match.start_offset, match.end_node, match.end_offset) |
| 202 |
except: # if offset is outside node |
| 203 |
return False |
| 204 |
return bool(sel.rangeCount and sel.toString()) |
| 205 |
|
| 206 |
|
| 207 |
def select_search_result(sr): |
| 208 |
window.getSelection().removeAllRanges() |
| 209 |
if not cache.text_map: |
| 210 |
cache.text_map = build_text_map() |
| 211 |
q = '' |
| 212 |
before_len = after_len = 0 |
| 213 |
if sr.before: |
| 214 |
q = sr.before[-15:] |
| 215 |
before_len = q.length |
| 216 |
q += sr.text |
| 217 |
if sr.after: |
| 218 |
after = sr.after[:15] |
| 219 |
after_len = after.length |
| 220 |
q += after |
| 221 |
match = find_specific_occurrence(q, int(sr.index), before_len, after_len, cache.text_map, sr.from_offset) |
| 222 |
if not match: |
| 223 |
return False |
| 224 |
return select_find_result(match) |
| 225 |
|
| 226 |
|
| 227 |
def find_word_length(text_map, idx): |
| 228 |
r = tts_word_regex() |
| 229 |
r.lastIndex = idx |
| 230 |
match = v'r.exec(text_map.flat_text)' |
| 231 |
word_length = 5 |
| 232 |
if match: |
| 233 |
word_length = match[0]?.length or 5 |
| 234 |
return word_length |
| 235 |
|
| 236 |
|
| 237 |
def select_tts_mark(idx_in_flat_text, last_idx_in_flat_text): |
| 238 |
window.getSelection().removeAllRanges() |
| 239 |
text_map = cached_tts_text_map() |
| 240 |
if idx_in_flat_text is last_idx_in_flat_text: |
| 241 |
match = get_occurrence_data(text_map.node_list, idx_in_flat_text, idx_in_flat_text + find_word_length(text_map, idx_in_flat_text)) |
| 242 |
else: |
| 243 |
match = get_occurrence_data(text_map.node_list, idx_in_flat_text, last_idx_in_flat_text + find_word_length(text_map, last_idx_in_flat_text)) |
| 244 |
if not match: |
| 245 |
return False |
| 246 |
return select_find_result(match) |