From 67978f343c69b3020ca3b2b9a38850eb2e6eed27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=AA=8E?= <> Date: Sun, 5 Nov 2017 13:41:40 +0800 Subject: [PATCH] add more options --- add-tts-engine/plugin.py | 156 ++++++++++++++++++++++++-------------- add-tts-engine/plugin.xml | 2 +- 2 files changed, 101 insertions(+), 57 deletions(-) diff --git a/add-tts-engine/plugin.py b/add-tts-engine/plugin.py index 5237dd3..f319fb4 100755 --- a/add-tts-engine/plugin.py +++ b/add-tts-engine/plugin.py @@ -11,10 +11,16 @@ lineEditPrompt1 = 'Language to be process. ie. "zh-HK" is Yue.' defaultInput1 = "zh-HK" -lineEditPrompt2 = 'TTS innerText of Tag. ie. body for Body Tag' -defaultInput2 = "body" +lineEditPrompt2 = 'TTS innerText of Tag. ie. p for Paragraph Tag' +defaultInput2 = "p" lineEditPrompt3 = 'TTS icon add to Tag Name. ie. h1 for Heading1' defaultInput3 = "h1" +lineEditPrompt4 = 'Text Style when Tag is speaking. ie. style="text-decoration:underline"' +defaultInput4 = "text-decoration:underline" +lineEditPrompt5 = 'TTS Pitch. ie. 0.7"' +defaultInput5 = "0.7" +lineEditPrompt6 = 'TTS Speed Rate. ie. 1.1' +defaultInput6 = "1.1" filelist = "Selected file in Book Browser:\n" @@ -68,7 +74,10 @@ def runLXML(bk): items = {lineEditPrompt1: defaultInput1, lineEditPrompt2: defaultInput2, - lineEditPrompt3: defaultInput3} + lineEditPrompt3: defaultInput3, + lineEditPrompt4: defaultInput4, + lineEditPrompt5: defaultInput5, + lineEditPrompt6: defaultInput6} count = 0 for (_, file_id) in bk.selected_iter(): @@ -93,6 +102,9 @@ def runLXML(bk): tts_lang = items[lineEditPrompt1] tts_content_tagname = items[lineEditPrompt2] tts_icon_in_tagname = items[lineEditPrompt3] + tts_on_focus_style = items[lineEditPrompt4] + tts_pitch = items[lineEditPrompt5] + tts_rate = items[lineEditPrompt6] modified = False @@ -111,72 +123,104 @@ def runLXML(bk): encoding="utf-8", xml_declaration=True).decode('utf8')) modified = True + + if bk.id_to_href("tts."+tts_lang) is not None: + print("TTS javascript file is existed.") + return 0 + else: + print("TTS javascript file is added.") # js if modified: jsdata = "\n" jsdata += "var tts_content_tag_name = '"+tts_content_tagname+"';\n" jsdata += "var tts_lang = '"+tts_lang+"';\n" jsdata += "var tts_icon_in_tagname = '"+tts_icon_in_tagname+"';\n" + jsdata += "var tts_on_focus_style = '"+tts_on_focus_style+"';\n" + jsdata += "var tts_pitch = '"+tts_pitch+"';\n" + jsdata += "var tts_rate = '"+tts_rate+"';\n" jsdata += ''' -var tts_pitch = 0.7; -var tts_rate = 1.1; - -var tts = function() { - a = document.createElement('a'); - a.id = 'tts'; - a.innerText = ' ' + String.fromCharCode(55357, 56803); - document.getElementsByTagName(tts_icon_in_tagname)[0].appendChild(a); - document.getElementById('tts').addEventListener('click', speak); -} -function p(){ - speechSynthesis.pause(); - document.getElementById('tts').removeEventListener('click', p); - document.getElementById('tts').addEventListener('click', r); -} -function r(){ - speechSynthesis.resume(); - document.getElementById('tts').removeEventListener('click', r); - document.getElementById('tts').addEventListener('click', p); -} -function speak(text, callback) { + var tts_out_focus_style = ""; + var emojiPlayPaused = String.fromCharCode(9199); + var emojiStop = String.fromCharCode(9209); + var emojiSpeak = String.fromCharCode(55357, 56803); + var tags; + var i; + var utterances=[]; + + var tts = function() { + a = document.createElement('a'); + a.id = 'tts'; + a.innerText = ' ' + emojiSpeak; + document.getElementsByTagName(tts_icon_in_tagname)[0].appendChild(a); + document.getElementById('tts').addEventListener('click', speak); + } + function p(){ + speechSynthesis.pause(); + document.getElementById('tts').removeEventListener('click', p); + document.getElementById('tts').addEventListener('click', r); + } + function r(){ + speechSynthesis.resume(); + document.getElementById('tts').removeEventListener('click', r); + document.getElementById('tts').addEventListener('click', p); + } + + fn_onstart = function (event) { + tts_out_focus_style = tags[i].getAttribute('style'); + if (!tts_out_focus_style){ + tts_out_focus_style = ""; + } + tags[i].setAttribute('style', tts_out_focus_style+";"+tts_on_focus_style); + tags[i].scrollIntoView(); + console.log("started"); + document.getElementById('tts').innerHTML = ' ' + emojiPlayPaused; + }; + + fn_onend = function(event) { + if (!tts_out_focus_style){ + tags[i].removeAttribute('style'); + }else{ + tags[i].setAttribute('style',tts_out_focus_style); + } + i++; + if(i < tags.length){ + console.log('Speak Next :' + i ); + }else{ + i = 0 ; //end of utterances + } + console.log('Finished in ' + event.elapsedTime + ' seconds.'); + document.getElementById('tts').innerHTML = ' ' + emojiStop; + }; + + function speak(text, callback) { tags = document.getElementsByTagName(tts_content_tag_name) - var i = 0; - text = tags[i].innerText; - for(i=1; iAdd TTS Engine for Sigil python2.7 python3.4 -20171030 +20171102 osx,unx,win true