Search in sources :

Example 6 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class TTSService method playPage.

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private void playPage(String preText, int pageNumber, String anchor) {
    if (pageNumber != -1) {
        EventBus.getDefault().post(new MessagePageNumber(pageNumber));
        AppState.get().lastBookPage = pageNumber;
        CodecDocument dc = getDC();
        if (dc == null) {
            LOG.d(TAG, "CodecDocument", "is NULL");
            return;
        }
        int pageCount = dc.getPageCount();
        LOG.d(TAG, "CodecDocument PageCount", pageNumber, pageCount);
        if (pageNumber >= pageCount) {
            TempHolder.get().timerFinishTime = 0;
            Vibro.vibrate(1000);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            TTSEngine.get().getTTS().setOnUtteranceCompletedListener(null);
            TTSEngine.get().speek(LibreraApp.context.getString(R.string.the_book_is_over));
            EventBus.getDefault().post(new TtsStatus());
            return;
        }
        CodecPage page = dc.getPage(pageNumber);
        String pageHTML = page.getPageHTML();
        page.recycle();
        pageHTML = TxtUtils.replaceHTMLforTTS(pageHTML);
        if (TxtUtils.isNotEmpty(anchor)) {
            int indexOf = pageHTML.indexOf(anchor);
            if (indexOf > 0) {
                pageHTML = pageHTML.substring(indexOf);
                LOG.d("find anchor new text", pageHTML);
            }
        }
        LOG.d(TAG, pageHTML);
        if (TxtUtils.isEmpty(pageHTML)) {
            LOG.d("empty page play next one", emptyPageCount);
            emptyPageCount++;
            if (emptyPageCount < 3) {
                playPage("", AppState.get().lastBookPage + 1, null);
            }
            return;
        }
        emptyPageCount = 0;
        String[] parts = TxtUtils.getParts(pageHTML);
        String firstPart = parts[0];
        final String secondPart = parts[1];
        if (TxtUtils.isNotEmpty(preText)) {
            preText = TxtUtils.replaceLast(preText, "-", "");
            firstPart = preText + firstPart;
        }
        if (Build.VERSION.SDK_INT >= 15) {
            TTSEngine.get().getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onStart(String utteranceId) {
                }

                @Override
                public void onError(String utteranceId) {
                    TTSEngine.get().stop();
                }

                @Override
                public void onDone(String utteranceId) {
                    LOG.d(TAG, "onUtteranceCompleted");
                    if (TempHolder.get().timerFinishTime != 0 && System.currentTimeMillis() > TempHolder.get().timerFinishTime) {
                        LOG.d(TAG, "Timer");
                        TempHolder.get().timerFinishTime = 0;
                        return;
                    }
                    playPage(secondPart, AppState.get().lastBookPage + 1, null);
                    SettingsManager.updateTempPage(AppState.get().lastBookPath, AppState.get().lastBookPage + 1);
                }
            });
        } else {
            TTSEngine.get().getTTS().setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(String utteranceId) {
                    LOG.d(TAG, "onUtteranceCompleted");
                    if (TempHolder.get().timerFinishTime != 0 && System.currentTimeMillis() > TempHolder.get().timerFinishTime) {
                        LOG.d(TAG, "Timer");
                        TempHolder.get().timerFinishTime = 0;
                        return;
                    }
                    playPage(secondPart, AppState.get().lastBookPage + 1, null);
                    SettingsManager.updateTempPage(AppState.get().lastBookPath, AppState.get().lastBookPage + 1);
                }
            });
        }
        TTSNotification.show(AppState.get().lastBookPath, pageNumber + 1);
        TTSEngine.get().speek(firstPart);
        EventBus.getDefault().post(new TtsStatus());
        savePage();
    }
}
Also used : OnUtteranceCompletedListener(android.speech.tts.TextToSpeech.OnUtteranceCompletedListener) UtteranceProgressListener(android.speech.tts.UtteranceProgressListener) CodecPage(org.ebookdroid.core.codec.CodecPage) CodecDocument(org.ebookdroid.core.codec.CodecDocument) TargetApi(android.annotation.TargetApi)

Example 7 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class HorizontalModeController method getTextForPage.

@Override
public String getTextForPage(int page) {
    try {
        CodecPage codecPage = codeDocument.getPage(page);
        if (!codecPage.isRecycled()) {
            String pageHTML = codecPage.getPageHTML();
            pageHTML = TxtUtils.replaceHTMLforTTS(pageHTML);
            return pageHTML;
        }
    } catch (Exception e) {
        LOG.e(e);
    }
    return "";
}
Also used : CodecPage(org.ebookdroid.core.codec.CodecPage)

Example 8 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class HorizontalModeController method recyclePage.

@Override
public void recyclePage(int number) {
    if (codeDocument == null) {
        return;
    }
    try {
        CodecPage page = codeDocument.getPage(number);
        page.recycle();
    } catch (Exception e) {
        LOG.e(e);
    }
}
Also used : CodecPage(org.ebookdroid.core.codec.CodecPage)

Example 9 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class MuPdfDocument method documentToHtml.

@Override
public String documentToHtml() {
    StringBuilder out = new StringBuilder();
    int pages = getPageCount();
    for (int i = 0; i < pages; i++) {
        CodecPage pageCodec = getPage(i);
        String pageHTML = pageCodec.getPageHTML();
        out.append(pageHTML);
    }
    return out.toString();
}
Also used : CodecPage(org.ebookdroid.core.codec.CodecPage)

Example 10 with CodecPage

use of org.ebookdroid.core.codec.CodecPage in project LibreraReader by foobnix.

the class ExtUtils method openPDFInTextReflowAsync.

public static File openPDFInTextReflowAsync(Activity a, final File file, Handler dialog) {
    try {
        File bookTempRoot = new File(AppState.get().downlodsPath, file.getName());
        if (!bookTempRoot.exists()) {
            bookTempRoot.mkdirs();
        } else {
            CacheZipUtils.removeFiles(bookTempRoot.listFiles());
        }
        String pwd = "";
        try {
            pwd = a.getIntent().getStringExtra(HorizontalModeController.EXTRA_PASSWORD);
            if (pwd == null) {
                pwd = "";
            }
        } catch (Exception e) {
            LOG.e(e);
        }
        CodecDocument doc = BookType.getCodecContextByPath(file.getPath()).openDocument(file.getPath(), pwd);
        List<OutlineLink> outline = doc.getOutline();
        final File fileReflowHtml = new File(bookTempRoot, "temp" + REFLOW_HTML);
        try {
            FileWriter fout = new FileWriter(fileReflowHtml);
            BufferedWriter out = new BufferedWriter(fout);
            out.write("<html>");
            out.write("<head><meta charset=\"utf-8\"/></head>");
            out.write("<body>");
            int pages = doc.getPageCount();
            int imgCount = 0;
            for (int i = 0; i < pages; i++) {
                LOG.d("Extract page", i);
                CodecPage pageCodec = doc.getPage(i);
                String html = pageCodec.getPageHTMLWithImages();
                out.write("<a id=\"" + i + "\"></a>");
                html = TxtUtils.replaceEndLine(html);
                int startImage = html.indexOf(IMAGE_BEGIN);
                while (startImage >= 0) {
                    if (!TempHolder.get().isConverting) {
                        CacheZipUtils.removeFiles(bookTempRoot.listFiles());
                        bookTempRoot.delete();
                        break;
                    }
                    imgCount++;
                    LOG.d("Extract image", imgCount);
                    int endImage = html.indexOf(IMAGE_END, startImage);
                    String mime = html.substring(startImage + IMAGE_BEGIN.length(), endImage);
                    String format;
                    if (mime.startsWith(IMAGE_JPEG_BASE64)) {
                        format = ".jpg";
                        mime = mime.replace(IMAGE_JPEG_BASE64, "");
                    } else if (mime.startsWith(IMAGE_PNG_BASE64)) {
                        format = ".png";
                        mime = mime.replace(IMAGE_PNG_BASE64, "");
                    } else {
                        format = ".none";
                    }
                    // FileOutputStream mimeOut = new FileOutputStream(new File(bookTempRoot, "mime"
                    // + imgCount + ".mime"));
                    // mimeOut.write(mime.getBytes());
                    // mimeOut.close();
                    byte[] decode = Base64.decode(mime, Base64.DEFAULT);
                    String imageName = imgCount + format;
                    LOG.d("Extract-mime", mime.substring(mime.length() - 10, mime.length()));
                    FileOutputStream imgStream = new FileOutputStream(new File(bookTempRoot, imageName));
                    imgStream.write(decode);
                    imgStream.close();
                    html = html.substring(0, startImage) + "<img src=\"" + imageName + "\"/>" + html.substring(endImage + IMAGE_END.length());
                    startImage = html.indexOf(IMAGE_BEGIN);
                    LOG.d("startImage", startImage);
                }
                // out.write(TextUtils.htmlEncode(html));
                // html = html.replace("< ", "&lt; ");
                // html = html.replace("> ", "&gt; ");
                // html = html.replace("&", "&amp;");
                out.write(html);
                pageCodec.recycle();
                LOG.d("Extract page end1", i);
                dialog.sendEmptyMessage(((i + 1) * 100) / pages);
                if (!TempHolder.get().isConverting) {
                    CacheZipUtils.removeFiles(bookTempRoot.listFiles());
                    bookTempRoot.delete();
                    break;
                }
            }
            doc.recycle();
            out.write("</body></html>");
            out.flush();
            out.close();
            fout.close();
        } catch (Exception e) {
            LOG.e(e);
            return null;
        }
        File epubOutpub = new File(AppState.get().downlodsPath, file.getName() + REFLOW_EPUB);
        if (epubOutpub.isFile()) {
            epubOutpub.delete();
        }
        FileMeta meta = AppDB.get().getOrCreate(file.getPath());
        Fb2Extractor.convertFolderToEpub(bookTempRoot, epubOutpub, meta.getAuthor(), meta.getTitle(), outline);
        CacheZipUtils.removeFiles(bookTempRoot.listFiles());
        bookTempRoot.delete();
        if (!TempHolder.get().isConverting) {
            epubOutpub.delete();
            LOG.d("Delete temp file", fileReflowHtml.getPath());
        }
        LOG.d("openPDFInTextReflow", fileReflowHtml.getPath());
        return epubOutpub;
    } catch (RuntimeException e) {
        LOG.e(e);
        return null;
    }
}
Also used : OutlineLink(org.ebookdroid.core.codec.OutlineLink) FileWriter(java.io.FileWriter) CodecPage(org.ebookdroid.core.codec.CodecPage) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileMeta(com.foobnix.dao2.FileMeta) CodecDocument(org.ebookdroid.core.codec.CodecDocument)

Aggregations

CodecPage (org.ebookdroid.core.codec.CodecPage)10 CodecDocument (org.ebookdroid.core.codec.CodecDocument)4 Rect (android.graphics.Rect)2 RectF (android.graphics.RectF)2 File (java.io.File)2 BitmapRef (org.ebookdroid.common.bitmaps.BitmapRef)2 TextWord (org.ebookdroid.droids.mupdf.codec.TextWord)2 TargetApi (android.annotation.TargetApi)1 Bitmap (android.graphics.Bitmap)1 Matrix (android.graphics.Matrix)1 OnUtteranceCompletedListener (android.speech.tts.TextToSpeech.OnUtteranceCompletedListener)1 UtteranceProgressListener (android.speech.tts.UtteranceProgressListener)1 FileMeta (com.foobnix.dao2.FileMeta)1 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 RawBitmap (org.ebookdroid.common.bitmaps.RawBitmap)1 CodecContext (org.ebookdroid.core.codec.CodecContext)1 CodecPageHolder (org.ebookdroid.core.codec.CodecPageHolder)1 CodecPageInfo (org.ebookdroid.core.codec.CodecPageInfo)1