Search in sources :

Example 1 with TextWord

use of org.ebookdroid.droids.mupdf.codec.TextWord in project LibreraReader by foobnix.

the class MultyDocSearchDialog method searchInThePDF.

public static int searchInThePDF(String path, String text, final Handler update1) {
    try {
        text = text.toLowerCase(Locale.US);
        CodecContext ctx = BookType.getCodecContextByPath(path);
        CodecDocument openDocument = null;
        CacheZipUtils.cacheLock.lock();
        try {
            String zipPath = CacheZipUtils.extracIfNeed(path, CacheDir.ZipApp).unZipPath;
            openDocument = ctx.openDocument(zipPath, "");
            LOG.d("searchInThePDF", openDocument, zipPath);
        } finally {
            CacheZipUtils.cacheLock.unlock();
        }
        if (!Model.get().isSearcingRun) {
            openDocument.recycle();
            ctx.recycle();
            return -1;
        }
        int pageCount = openDocument.getPageCount();
        Model.get().currentPagesCount = pageCount;
        Model.get().currentDoc = new File(path).getName();
        LOG.d("searchInThePDF", "pageCount", Model.get().currentPagesCount, Model.get().currentDoc);
        int emptyCount = 0;
        for (int i = 0; i < pageCount; i++) {
            if (!Model.get().isSearcingRun) {
                openDocument.recycle();
                ctx.recycle();
                return -1;
            }
            CodecPage page = openDocument.getPage(i);
            TextWord[][] textPage = page.getText();
            LOG.d("searchInThePDF", "getText", textPage != null ? textPage.length : "null");
            if (textPage == null || textPage.length <= 1) {
                emptyCount++;
                if (emptyCount >= 5) {
                    LOG.d("searchInThePDF", "Page is empty", emptyCount);
                    break;
                }
            }
            List<TextWord> findText = Page.findText(text, textPage);
            page.recycle();
            page = null;
            Model.get().currentPage = i;
            update1.sendEmptyMessage(0);
            if (!findText.isEmpty()) {
                openDocument.recycle();
                ctx.recycle();
                findText = null;
                openDocument = null;
                ctx = null;
                return i;
            }
            findText = null;
        }
        openDocument.recycle();
        ctx.recycle();
        openDocument = null;
        ctx = null;
        System.gc();
    } catch (Exception e) {
        LOG.e(e);
    }
    return -1;
}
Also used : CodecContext(org.ebookdroid.core.codec.CodecContext) CodecPage(org.ebookdroid.core.codec.CodecPage) File(java.io.File) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord) CodecDocument(org.ebookdroid.core.codec.CodecDocument)

Example 2 with TextWord

use of org.ebookdroid.droids.mupdf.codec.TextWord in project LibreraReader by foobnix.

the class HorizontalModeController method doSearch.

@Override
public void doSearch(final String text, final com.foobnix.android.utils.ResultResponse<Integer> result) {
    if (searchTask != null && searchTask.getStatus() != CopyAsyncTask.Status.FINISHED) {
        return;
    }
    searchTask = new CopyAsyncTask() {

        @Override
        protected Object doInBackground(Object... params) {
            try {
                PageImageState.get().cleanSelectedWords();
                String textLowCase = text.toLowerCase(Locale.US);
                String bookPath = getBookPath();
                int prev = -1;
                boolean nextWorld = false;
                String firstPart = "";
                TextWord firstWord = null;
                for (int i = 0; i < getPageCount(); i++) {
                    if (!TempHolder.isSeaching) {
                        result.onResultRecive(Integer.MAX_VALUE);
                        return null;
                    }
                    if (isClosed) {
                        TempHolder.isSeaching = false;
                        return null;
                    }
                    if (i > 1) {
                        result.onResultRecive(i * -1);
                    }
                    TextWord[][] pageText = getPageText(i);
                    recyclePage(i);
                    if (pageText == null) {
                        continue;
                    }
                    int index = 0;
                    List<TextWord> find = new ArrayList<TextWord>();
                    for (TextWord[] line : pageText) {
                        find.clear();
                        index = 0;
                        for (TextWord word : line) {
                            if (AppState.get().selectingByLetters) {
                                String it = String.valueOf(textLowCase.charAt(index));
                                if (word.w.toLowerCase(Locale.US).equals(it)) {
                                    index++;
                                    find.add(word);
                                } else {
                                    index = 0;
                                    find.clear();
                                }
                                if (index == text.length()) {
                                    index = 0;
                                    if (prev != i) {
                                        result.onResultRecive(i);
                                        prev = i;
                                    }
                                    for (TextWord t : find) {
                                        PageImageState.get().addWord(i, t);
                                    }
                                }
                            } else if (word.w.toLowerCase(Locale.US).contains(textLowCase)) {
                                LOG.d("Contains 1", word.w);
                                if (prev != i) {
                                    result.onResultRecive(i);
                                    prev = i;
                                }
                                PageImageState.get().addWord(i, word);
                            } else if (word.w.length() >= 3 && word.w.endsWith("-")) {
                                nextWorld = true;
                                firstWord = word;
                                firstPart = word.w.replace("-", "");
                            } else if (nextWorld && (firstPart + word.w.toLowerCase(Locale.US)).contains(text)) {
                                LOG.d("Contains 2", firstPart, word.w, text);
                                PageImageState.get().addWord(i, firstWord);
                                PageImageState.get().addWord(i, word);
                                nextWorld = false;
                                firstWord = null;
                                firstPart = "";
                                if (prev != i) {
                                    result.onResultRecive(i);
                                    prev = i;
                                }
                            } else if (nextWorld && TxtUtils.isNotEmpty(word.w)) {
                                nextWorld = false;
                                firstWord = null;
                            }
                        }
                    }
                }
                result.onResultRecive(-1);
            } catch (Exception e) {
                result.onResultRecive(-1);
            }
            TempHolder.isSeaching = false;
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            EventBus.getDefault().post(new InvalidateMessage());
        }
    }.execute();
}
Also used : CopyAsyncTask(com.foobnix.pdf.CopyAsyncTask) ArrayList(java.util.ArrayList) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord) InvalidateMessage(com.foobnix.pdf.search.activity.msg.InvalidateMessage)

Example 3 with TextWord

use of org.ebookdroid.droids.mupdf.codec.TextWord in project LibreraReader by foobnix.

the class PageImaveView method onDraw.

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    try {
        if (AppState.get().isOLED && !AppState.get().isDayNotInvert) /* && MagicHelper.getBgColor() == Color.BLACK */
        {
            canvas.drawColor(Color.BLACK);
        } else {
            canvas.drawColor(MagicHelper.ligtherColor(MagicHelper.getBgColor()));
        }
        final int saveCount = canvas.getSaveCount();
        canvas.save();
        canvas.concat(imageMatrix());
        if (imageDrawable != null) {
            imageDrawable.draw(canvas);
        }
        if (PageImageState.get().isShowCuttingLine && AppState.get().isCut == false) {
            int offset = drawableWidth * AppState.get().cutP / 100;
            canvas.drawLine(offset, 0, offset, drawableHeight, paintWrods);
        }
        List<TextWord> selectedWords = PageImageState.get().getSelectedWords(pageNumber);
        if (selectedWords != null) {
            for (TextWord tw : selectedWords) {
                drawWord(canvas, tw);
            }
        }
        if (AppState.get().isOLED && !AppState.get().isDayNotInvert) /* && !TempHolder.get().isTextFormat */
        {
            canvas.drawRect(-dp1, 0, drawableWidth + dp1, drawableHeight, rect);
        }
        canvas.restoreToCount(saveCount);
    } catch (Exception e) {
        LOG.e(e);
    }
}
Also used : Paint(android.graphics.Paint) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord)

Example 4 with TextWord

use of org.ebookdroid.droids.mupdf.codec.TextWord in project LibreraReader by foobnix.

the class AbstractViewController method processLongTap.

public final String processLongTap(boolean single, final MotionEvent e1, final MotionEvent e2, boolean draw) {
    if (e1 != null) {
        xLong = e1.getX();
        yLong = e1.getY();
    }
    float x2 = e2.getX();
    float y2 = e2.getY();
    final float zoom = base.getZoomModel().getZoom();
    final RectF tapRect = new RectF(xLong, yLong, x2, y2);
    if (yLong > y2) {
        tapRect.sort();
    }
    tapRect.offset(getScrollX(), getScrollY());
    StringBuilder build = new StringBuilder();
    boolean isHyphenWorld = false;
    LOG.d("Add Word page", "----", firstVisiblePage, lastVisiblePage + 1);
    for (final Page page : model.getPages(firstVisiblePage, lastVisiblePage + 1)) {
        if (draw)
            page.selectedText.clear();
        LOG.d("Add Word page", page.hashCode());
        final RectF bounds = page.getBounds(zoom);
        TextWord prevWord = null;
        if (RectF.intersects(bounds, tapRect)) {
            if (LengthUtils.isNotEmpty(page.texts)) {
                for (final TextWord[] lines : page.texts) {
                    final TextWord[] current = lines;
                    for (final TextWord line : current) {
                        if (line.left <= 0 || line.top <= 0) {
                            continue;
                        }
                        RectF wordRect = page.getPageRegion(bounds, line);
                        if (wordRect == null) {
                            continue;
                        }
                        if (isHyphenWorld || (single && RectF.intersects(wordRect, tapRect))) {
                            if (prevWord != null && prevWord.w.endsWith("-") && !isHyphenWorld) {
                                build.append(prevWord.w.replace("-", ""));
                                if (draw)
                                    page.selectedText.add(prevWord);
                            }
                            build.append(line.w + " ");
                            if (!isHyphenWorld) {
                                if (draw)
                                    page.selectedText.add(line);
                            }
                            LOG.d("Add Word", line.w);
                            if (isHyphenWorld && TxtUtils.isNotEmpty(line.w)) {
                                if (draw)
                                    page.selectedText.add(line);
                                isHyphenWorld = false;
                            }
                            if (line.w.endsWith("-")) {
                                isHyphenWorld = true;
                            }
                            // get links
                            if (LengthUtils.isNotEmpty(page.links)) {
                                for (final PageLink link : page.links) {
                                    final RectF linkRect = page.getLinkSourceRect(bounds, link);
                                    if (linkRect == null) {
                                        continue;
                                    }
                                    if (RectF.intersects(linkRect, wordRect)) {
                                        TempHolder.get().linkPage = link.targetPage;
                                    }
                                }
                            }
                        } else if (!single) {
                            if (y2 > yLong) {
                                if (wordRect.top < tapRect.top && wordRect.bottom > tapRect.top && wordRect.right > tapRect.left) {
                                    if (draw)
                                        page.selectedText.add(line);
                                    build.append(line.w + TxtUtils.space());
                                    LOG.d("Add Word", line.w);
                                } else if (wordRect.top < tapRect.bottom && wordRect.bottom > tapRect.bottom && wordRect.left < tapRect.right) {
                                    if (draw)
                                        page.selectedText.add(line);
                                    build.append(line.w + TxtUtils.space());
                                    LOG.d("Add Word", line.w);
                                } else if (wordRect.top > tapRect.top && wordRect.bottom < tapRect.bottom) {
                                    if (draw)
                                        page.selectedText.add(line);
                                    build.append(line.w + TxtUtils.space());
                                    LOG.d("Add Word", line.w);
                                }
                            } else if (RectF.intersects(wordRect, tapRect)) {
                                if (draw)
                                    page.selectedText.add(line);
                                if (AppState.get().selectingByLetters) {
                                    build.append(line.w);
                                } else {
                                    build.append(line.w.trim() + " ");
                                }
                                LOG.d("Add Word", line.w);
                            }
                        }
                        if (TxtUtils.isNotEmpty(line.w)) {
                            prevWord = line;
                        }
                    }
                    String k;
                    if (AppState.get().selectingByLetters && current.length >= 2 && !(k = current[current.length - 1].getWord()).equals(" ") && !k.equals("-")) {
                        build.append(" ");
                    }
                }
            }
        }
    }
    if (build.length() > 0) {
        redrawView();
        String filterString = TxtUtils.filterString(build.toString());
        LOG.d("Add Word SELECT TEXT", filterString);
        return filterString;
    }
    return null;
}
Also used : RectF(android.graphics.RectF) PageLink(org.ebookdroid.core.codec.PageLink) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord)

Example 5 with TextWord

use of org.ebookdroid.droids.mupdf.codec.TextWord in project LibreraReader by foobnix.

the class DecodeServiceBase method searchText.

@Override
public void searchText(final String text, final Page[] pages, final ResultResponse<Integer> response, final Runnable finish) {
    Thread t = new Thread() {

        @Override
        public void run() {
            for (Page page : pages) {
                if (!TempHolder.isSeaching) {
                    response.onResultRecive(Integer.MAX_VALUE);
                    finish.run();
                    return;
                }
                if (page.index.docIndex > 1) {
                    response.onResultRecive(page.index.docIndex * -1);
                }
                if (isRecycled.get()) {
                    TempHolder.isSeaching = false;
                    return;
                }
                if (page.texts == null) {
                    final CodecPage page2 = codecDocument.getPage(page.index.docIndex);
                    page.texts = page2.getText();
                    if (!page2.isRecycled()) {
                        executor.addAny(new Task(0) {

                            @Override
                            public void run() {
                                page2.recycle();
                            }
                        });
                    }
                }
                page.selectedText = new ArrayList<TextWord>();
                List<TextWord> findText = page.findText(text);
                if (findText != null && !findText.isEmpty()) {
                    page.selectedText = findText;
                    response.onResultRecive(page.index.docIndex);
                    LOG.d("Find on page1", page.index.docIndex, text);
                }
            }
            response.onResultRecive(-1);
            finish.run();
            TempHolder.isSeaching = false;
        }
    };
    t.start();
}
Also used : CodecPage(org.ebookdroid.core.codec.CodecPage) CodecPage(org.ebookdroid.core.codec.CodecPage) TextWord(org.ebookdroid.droids.mupdf.codec.TextWord)

Aggregations

TextWord (org.ebookdroid.droids.mupdf.codec.TextWord)9 Paint (android.graphics.Paint)3 RectF (android.graphics.RectF)3 ArrayList (java.util.ArrayList)3 CodecPage (org.ebookdroid.core.codec.CodecPage)2 PointF (android.graphics.PointF)1 CopyAsyncTask (com.foobnix.pdf.CopyAsyncTask)1 InvalidateMessage (com.foobnix.pdf.search.activity.msg.InvalidateMessage)1 File (java.io.File)1 List (java.util.List)1 Page (org.ebookdroid.core.Page)1 Annotation (org.ebookdroid.core.codec.Annotation)1 CodecContext (org.ebookdroid.core.codec.CodecContext)1 CodecDocument (org.ebookdroid.core.codec.CodecDocument)1 PageLink (org.ebookdroid.core.codec.PageLink)1