| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from read_book.anchor_visibility import is_anchor_on_screen |
| 6 |
from read_book.globals import get_boss |
| 7 |
|
| 8 |
|
| 9 |
def flatten_seq(seq, par_list): |
| 10 |
if seq.par: |
| 11 |
for par in seq.par: |
| 12 |
if par.anchor: |
| 13 |
par_list.push(par) |
| 14 |
if seq.seq: |
| 15 |
for child in seq.seq: |
| 16 |
flatten_seq(child, par_list) |
| 17 |
|
| 18 |
|
| 19 |
def flatten_smil_map(smil_map): |
| 20 |
anchor_map = {} |
| 21 |
par_list = [] |
| 22 |
if smil_map: |
| 23 |
flatten_seq(smil_map, par_list) |
| 24 |
par_list.sort(def (a, b): return a.num - b.num;) |
| 25 |
for i in range(par_list.length): |
| 26 |
anchor_map[par_list[i].anchor] = i |
| 27 |
return anchor_map, par_list |
| 28 |
|
| 29 |
|
| 30 |
def get_smil_audio_map(smil_map): |
| 31 |
audio_map = {} |
| 32 |
|
| 33 |
def flatten(seq): |
| 34 |
if seq.par: |
| 35 |
for par in seq.par: |
| 36 |
if par.audio: |
| 37 |
a = audio_map[par.audio] |
| 38 |
if not a: |
| 39 |
a = audio_map[par.audio] = [] |
| 40 |
a.push(par) |
| 41 |
if seq.seq: |
| 42 |
for child in seq.seq: |
| 43 |
flatten(child) |
| 44 |
if smil_map: |
| 45 |
flatten(smil_map) |
| 46 |
for v in Object.values(audio_map): |
| 47 |
v.sort(def (a, b): return a.start - b.start;) |
| 48 |
return {'audio_files': audio_map} |
| 49 |
|
| 50 |
|
| 51 |
def get_smil_id_for_timestamp(audio_file_name, timestamp, smil_audio_map, prev_idx): |
| 52 |
pars = smil_audio_map.audio_files[audio_file_name] |
| 53 |
if not pars: |
| 54 |
return None, None |
| 55 |
prev_idx = prev_idx or 0 |
| 56 |
if prev_idx >= pars.length or prev_idx < 0: |
| 57 |
prev_idx = 0 |
| 58 |
for i in range(prev_idx, pars.length): |
| 59 |
if pars[i].start <= timestamp <= pars[i].end: |
| 60 |
return pars[i].anchor, i |
| 61 |
for i in range(0, prev_idx): |
| 62 |
if pars[i].start <= timestamp <= pars[i].end: |
| 63 |
return pars[i].anchor, i |
| 64 |
return None, None |
| 65 |
|
| 66 |
|
| 67 |
def next_audio_file_for_spine_item(audio_file_name, smil_audio_map): |
| 68 |
if audio_file_name: |
| 69 |
keys = Object.keys(smil_audio_map) |
| 70 |
idx = keys.indexOf(audio_file_name) |
| 71 |
if 0 <= idx < keys.length - 1: |
| 72 |
return keys[idx + 1], smil_audio_map[keys[idx + 1]] |
| 73 |
return None, None |
| 74 |
|
| 75 |
|
| 76 |
def first_par(smil_map): |
| 77 |
par_list = flatten_smil_map(smil_map)[1] |
| 78 |
for par in par_list: |
| 79 |
if par.anchor: |
| 80 |
return par |
| 81 |
return None |
| 82 |
|
| 83 |
|
| 84 |
def find_next_audio_in_spine(spine_idx, book_manifest): |
| 85 |
spine = book_manifest.spine |
| 86 |
file_map = book_manifest.files |
| 87 |
|
| 88 |
for i in range(spine_idx + 1, spine.length): |
| 89 |
q = spine[i] |
| 90 |
f = file_map[q] |
| 91 |
if f and f.smil_map: |
| 92 |
par = first_par(f.smil_map) |
| 93 |
if par: |
| 94 |
return q, par |
| 95 |
return None, None |
| 96 |
|
| 97 |
|
| 98 |
def smil_element_at(pos, anchor_map, par_list): |
| 99 |
if pos: |
| 100 |
# first see if we get lucky |
| 101 |
elem = document.elementFromPoint(pos.x, pos.y) |
| 102 |
if elem and elem.id and anchor_map[elem.id]?: |
| 103 |
return par_list[anchor_map[elem.id]] |
| 104 |
# now try to find a par that intersects pos |
| 105 |
af = get_boss().anchor_funcs |
| 106 |
for par in par_list: |
| 107 |
if par.anchor and par.audio: |
| 108 |
elem = document.getElementById(par.anchor) |
| 109 |
if elem: |
| 110 |
br = af.get_bounding_client_rect(elem) |
| 111 |
if br.x <= pos.x <= (br.x + br.width) and br.y <= pos.y <= (br.y + br.height): |
| 112 |
return par |
| 113 |
return None |
| 114 |
else: # use first visible anchor |
| 115 |
for par in par_list: |
| 116 |
if par.anchor and par.audio and is_anchor_on_screen(par.anchor): |
| 117 |
return par |
| 118 |
# just use first par |
| 119 |
for par in par_list: |
| 120 |
if par.audio: |
| 121 |
return par |
| 122 |
return None |
| 123 |
|
| 124 |
|
| 125 |
def mark_smil_element(anchor): |
| 126 |
elem = document.getElementById(anchor) |
| 127 |
if elem: |
| 128 |
sel = window.getSelection() |
| 129 |
sel.selectAllChildren(elem) |
| 130 |
return bool(sel.rangeCount and sel.toString()) |
| 131 |
return False |