/** * djvu_text_page_selection: * @page: #DjvuTextPage instance * @p: tree to append * @delimit: character/word/... delimiter * * Walks the tree in @p and appends the text with * djvu_text_page_selection_process() for all s-expressions * between the start and end fields. * * Returns: whether the end was not reached in this subtree */ static gboolean djvu_text_page_selection (DjvuTextPage *page, miniexp_t p, int delimit) { g_return_val_if_fail (miniexp_consp (p) && miniexp_symbolp (miniexp_car (p)), FALSE); if (miniexp_car (p) != page->char_symbol) delimit |= miniexp_car (p) == page->word_symbol ? 1 : 2; miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p)); while (deeper != miniexp_nil) { miniexp_t str = miniexp_car (deeper); if (miniexp_stringp (str)) { if (!djvu_text_page_selection_process (page, p, delimit)) return FALSE; } else { if (!djvu_text_page_selection (page, str, delimit)) return FALSE; } delimit = 0; deeper = miniexp_cdr (deeper); } return TRUE; }
/** * djvu_text_page_sexpr: * @page: #DjvuTextPage instance * @p: tree to append * @start: first s-expression in the selection * @end: last s-expression in the selection * * Walks the tree in @p and extends the rectangle with * djvu_text_page_process() for all s-expressions between @start and @end. * * Returns: whether the end was not reached in this subtree */ static gboolean djvu_text_page_sexpr (DjvuTextPage *page, miniexp_t p, miniexp_t start, miniexp_t end) { g_return_val_if_fail (miniexp_consp (p) && miniexp_symbolp (miniexp_car (p)), FALSE); miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p)); while (deeper != miniexp_nil) { miniexp_t str = miniexp_car (deeper); if (miniexp_stringp (str)) { if (!djvu_text_page_sexpr_process (page, p, start, end)) return FALSE; } else { if (!djvu_text_page_sexpr (page, str, start, end)) return FALSE; } deeper = miniexp_cdr (deeper); } return TRUE; }
//sumatrapdf code int extractText(miniexp_t item, Arraylist list, fz_bbox * target) { miniexp_t type = miniexp_car(item); if (!miniexp_symbolp(type)) return 0; item = miniexp_cdr(item); if (!miniexp_numberp(miniexp_car(item))) return 0; int x0 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item); if (!miniexp_numberp(miniexp_car(item))) return 0; int y0 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item); if (!miniexp_numberp(miniexp_car(item))) return 0; int x1 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item); if (!miniexp_numberp(miniexp_car(item))) return 0; int y1 = miniexp_to_int(miniexp_car(item)); item = miniexp_cdr(item); //RectI rect = RectI::FromXY(x0, y0, x1, y1); fz_bbox rect = {x0 , y0 , x1 , y1}; miniexp_t str = miniexp_car(item); if (miniexp_stringp(str) && !miniexp_cdr(item)) { fz_bbox inters = fz_intersect_bbox(rect, *target); //LOGI("Start text extraction: rectangle=[%d,%d,%d,%d] %s", rect.x0, rect.y0, rect.x1, rect.y1, content); if (!fz_is_empty_bbox(inters)) { const char *content = miniexp_to_str(str); while (*content) { arraylist_add(list, *content++); } // if (value) { // size_t len = str::Len(value); // // TODO: split the rectangle into individual parts per glyph // for (size_t i = 0; i < len; i++) // coords.Append(RectI(rect.x, rect.y, rect.dx, rect.dy)); // extracted.AppendAndFree(value); // } if (miniexp_symbol("word") == type) { arraylist_add(list, ' '); //coords.Append(RectI(rect.x + rect.dx, rect.y, 2, rect.dy)); } else if (miniexp_symbol("char") != type) { arraylist_add(list, '\n'); // extracted.Append(lineSep); // for (size_t i = 0; i < str::Len(lineSep); i++) // coords.Append(RectI()); } } item = miniexp_cdr(item); } while (miniexp_consp(str)) { extractText(str, list, target); item = miniexp_cdr(item); str = miniexp_car(item); } return !item; }
static jboolean miniexp_get_text(JNIEnv * env, miniexp_t exp, jobject stringBuilder, jobject positions, int *state, jclass rectFClass, jmethodID ctor, jmethodID addToList, int pageHeight) { miniexp_t type = miniexp_car(exp); int typenum = parse_text_type(type); miniexp_t r = exp = miniexp_cdr(exp); if (! miniexp_symbolp(type)) return 0; jobject rect = miniexp_get_rect(&r, env, rectFClass, ctor, pageHeight); if (rect == NULL) return 0; miniexp_t s = miniexp_car(r); *state = qMax(*state, typenum); jstring space = (*env)->NewStringUTF(env, " "); jstring newLine = (*env)->NewStringUTF(env, "\n"); if (miniexp_stringp(s) && !miniexp_cdr(r)) { //result += (state >= 2) ? "\n" : (state >= 1) ? " " : ""; if (*state >= 2) { (*env)->CallBooleanMethod(env, positions, addToList, rect); (*env)->CallBooleanMethod(env, stringBuilder, addToList, newLine); } else if (*state >= 1) { (*env)->CallBooleanMethod(env, positions, addToList, rect); (*env)->CallBooleanMethod(env, stringBuilder, addToList, newLine); } else { //add empty? } *state = -1; (*env)->CallBooleanMethod(env, positions, addToList, rect); jstring string = (*env)->NewStringUTF(env, miniexp_to_str(s)); (*env)->CallBooleanMethod(env, stringBuilder, addToList, string); (*env)->DeleteLocalRef(env, string); r = miniexp_cdr(r); } (*env)->DeleteLocalRef(env, space); (*env)->DeleteLocalRef(env, newLine); (*env)->DeleteLocalRef(env, rect); while(miniexp_consp(s)) { miniexp_get_text(env, s, stringBuilder, positions, state, rectFClass, ctor, addToList, pageHeight); r = miniexp_cdr(r); s = miniexp_car(r); } if (r) return 0; *state = qMax(*state, typenum); return 1; }
/** * djvu_text_page_append_search: * @page: #DjvuTextPage instance * @p: tree to append * @case_sensitive: do not ignore case * @delimit: insert spaces because of higher (sentence/paragraph/...) break * * Appends the tree in @p to the internal text string. */ static void djvu_text_page_append_text (DjvuTextPage *page, miniexp_t p, gboolean case_sensitive, gboolean delimit) { char *token_text; miniexp_t deeper; g_return_if_fail (miniexp_consp (p) && miniexp_symbolp (miniexp_car (p))); delimit |= page->char_symbol != miniexp_car (p); deeper = miniexp_cddr (miniexp_cdddr (p)); while (deeper != miniexp_nil) { miniexp_t data = miniexp_car (deeper); if (miniexp_stringp (data)) { DjvuTextLink link; link.position = page->text == NULL ? 0 : strlen (page->text); link.pair = p; g_array_append_val (page->links, link); token_text = (char *) miniexp_to_str (data); if (!case_sensitive) token_text = g_utf8_casefold (token_text, -1); if (page->text == NULL) page->text = g_strdup (token_text); else { char *new_text = g_strjoin (delimit ? " " : NULL, page->text, token_text, NULL); g_free (page->text); page->text = new_text; } if (!case_sensitive) g_free (token_text); } else djvu_text_page_append_text (page, data, case_sensitive, delimit); delimit = FALSE; deeper = miniexp_cdr (deeper); } }
static void djvu_text_page_limits (DjvuTextPage *page, miniexp_t p, EvRectangle *rect) { g_return_if_fail (miniexp_consp (p) && miniexp_symbolp (miniexp_car (p))); miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p)); while (deeper != miniexp_nil) { miniexp_t str = miniexp_car (deeper); if (miniexp_stringp (str)) djvu_text_page_limits_process (page, p, rect); else djvu_text_page_limits (page, str, rect); deeper = miniexp_cdr (deeper); } }
static QString loadText(miniexp_t textExp, const QRect& rect, int pageHeight) { const int textLength = miniexp_length(textExp); if(textLength >= 6 && miniexp_symbolp(miniexp_nth(0, textExp))) { const int xmin = miniexp_to_int(miniexp_nth(1, textExp)); const int ymin = miniexp_to_int(miniexp_nth(2, textExp)); const int xmax = miniexp_to_int(miniexp_nth(3, textExp)); const int ymax = miniexp_to_int(miniexp_nth(4, textExp)); if(rect.intersects(QRect(xmin, pageHeight - ymax, xmax - xmin, ymax - ymin))) { if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "word", 4) == 0) { return QString::fromUtf8(miniexp_to_str(miniexp_nth(5, textExp))); } else { QStringList text; for(int textN = 5; textN < textLength; ++textN) { text.append(loadText(miniexp_nth(textN, textExp), rect, pageHeight)); } if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "line", 4) == 0) { return text.join(" "); } else { return text.join("\n"); } } } } return QString(); }
QList< QRectF > Model::DjVuPage::search(const QString& text, bool matchCase) const { QMutexLocker mutexLocker(&m_parent->m_mutex); miniexp_t pageTextExp; while(true) { pageTextExp = ddjvu_document_get_pagetext(m_parent->m_document, m_index, "word"); if(pageTextExp == miniexp_dummy) { clearMessageQueue(m_parent->m_context, true); } else { break; } } QList< miniexp_t > words; QList< QRectF > results; words.append(pageTextExp); QRectF rect; int index = 0; while(!words.isEmpty()) { miniexp_t textExp = words.takeFirst(); const int textLength = miniexp_length(textExp); if(textLength >= 6 && miniexp_symbolp(miniexp_nth(0, textExp))) { if(qstrncmp(miniexp_to_name(miniexp_nth(0, textExp)), "word", 4) == 0) { const QString word = QString::fromUtf8(miniexp_to_str(miniexp_nth(5, textExp))); if(text.indexOf(word, index, matchCase ? Qt::CaseSensitive : Qt::CaseInsensitive) == index) { const int xmin = miniexp_to_int(miniexp_nth(1, textExp)); const int ymin = miniexp_to_int(miniexp_nth(2, textExp)); const int xmax = miniexp_to_int(miniexp_nth(3, textExp)); const int ymax = miniexp_to_int(miniexp_nth(4, textExp)); rect = rect.united(QRectF(xmin, m_size.height() - ymax, xmax - xmin, ymax - ymin)); index += word.length(); while(text.length() > index && text.at(index).isSpace()) { ++index; } if(text.length() == index) { results.append(rect); rect = QRectF(); index = 0; } } else { rect = QRectF(); index = 0; } } else { for(int textN = 5; textN < textLength; ++textN) { words.append(miniexp_nth(textN, textExp)); } } } } ddjvu_miniexp_release(m_parent->m_document, pageTextExp); QTransform transform = QTransform::fromScale(72.0 / m_resolution, 72.0 / m_resolution); for(int index = 0; index < results.size(); ++index) { results[index] = transform.mapRect(results[index]); } return results; }
QList< Model::Link* > Model::DjVuPage::links() const { QMutexLocker mutexLocker(&m_parent->m_mutex); QList< Link* > links; miniexp_t pageAnnoExp; while(true) { pageAnnoExp = ddjvu_document_get_pageanno(m_parent->m_document, m_index); if(pageAnnoExp == miniexp_dummy) { clearMessageQueue(m_parent->m_context, true); } else { break; } } const int pageAnnoLength = miniexp_length(pageAnnoExp); for(int pageAnnoN = 0; pageAnnoN < pageAnnoLength; ++pageAnnoN) { miniexp_t linkExp = miniexp_nth(pageAnnoN, pageAnnoExp); if(miniexp_length(linkExp) <= 3 || qstrncmp(miniexp_to_name(miniexp_nth(0, linkExp)), "maparea", 7 ) != 0 || !miniexp_symbolp(miniexp_nth(0, miniexp_nth(3, linkExp)))) { continue; } const QString type = QString::fromUtf8(miniexp_to_name(miniexp_nth(0, miniexp_nth(3, linkExp)))); if(type == QLatin1String("rect") || type == QLatin1String("oval") || type == QLatin1String("poly")) { // boundary QPainterPath boundary; miniexp_t areaExp = miniexp_nth(3, linkExp); const int areaLength = miniexp_length( areaExp ); if(areaLength == 5 && (type == QLatin1String("rect") || type == QLatin1String("oval"))) { QPoint p(miniexp_to_int(miniexp_nth(1, areaExp)), miniexp_to_int(miniexp_nth(2, areaExp))); QSize s(miniexp_to_int(miniexp_nth(3, areaExp)), miniexp_to_int(miniexp_nth(4, areaExp))); p.setY(m_size.height() - s.height() - p.y()); const QRectF r(p, s); if(type == QLatin1String("rect")) { boundary.addRect(r); } else { boundary.addEllipse(r); } } else if(areaLength > 0 && areaLength % 2 == 1 && type == QLatin1String("poly")) { QPolygon polygon; for(int areaExpN = 1; areaExpN < areaLength; areaExpN += 2) { QPoint p(miniexp_to_int(miniexp_nth(areaExpN, areaExp)), miniexp_to_int(miniexp_nth(areaExpN + 1, areaExp))); p.setY(m_size.height() - p.y()); polygon << p; } boundary.addPolygon(polygon); } if(boundary.isEmpty()) { continue; } boundary = QTransform::fromScale(1.0 / m_size.width(), 1.0 / m_size.height()).map(boundary); // target QString target; miniexp_t targetExp = miniexp_nth(1, linkExp); if(miniexp_stringp(targetExp)) { target = QString::fromUtf8(miniexp_to_str(miniexp_nth(1, linkExp))); } else if(miniexp_length(targetExp) == 3 && qstrncmp(miniexp_to_name(miniexp_nth(0, targetExp)), "url", 3) == 0) { target = QString::fromUtf8(miniexp_to_str(miniexp_nth(1, targetExp))); } if(target.isEmpty()) { continue; } if(target.at(0) == QLatin1Char('#')) { target.remove(0, 1); bool ok = false; int targetPage = target.toInt(&ok); if(!ok) { if(m_parent->m_indexByName.contains(target)) { targetPage = m_parent->m_indexByName[target] + 1; } else { continue; } } else { targetPage = (target.at(0) == QLatin1Char('+') || target.at(0) == QLatin1Char('-')) ? m_index + targetPage : targetPage; } links.append(new Link(boundary, targetPage)); } else { links.append(new Link(boundary, target)); } } } ddjvu_miniexp_release(m_parent->m_document, pageAnnoExp); return links; }
void djvu_get_djvu_words(SearchHelper& h, jobject list, miniexp_t expr, jstring pattern) { int coords[4]; if (!miniexp_consp(expr)) { return; } miniexp_t head = miniexp_car(expr); expr = miniexp_cdr(expr); if (!miniexp_symbolp(head)) { return; } int i; for (i = 0; i < 4 && miniexp_consp(expr); i++) { head = miniexp_car(expr); expr = miniexp_cdr(expr); if (!miniexp_numberp(head)) { return; } coords[i] = miniexp_to_int(head); } while (miniexp_consp(expr)) { head = miniexp_car(expr); if (miniexp_stringp(head)) { const char* text = miniexp_to_str(head); // DEBUG("%d, %d, %d, %d: %s", coords[0], coords[1], coords[2], coords[3], text); bool add = !pattern; jstring txt = h.str.toString(text); if (pattern) { jstring ltxt = h.str.toLowerCase(txt); add = h.str.indexOf(ltxt, pattern) >= 0; h.str.release(ltxt); } if (add) { // add to list jobject ptb = h.box.create(); h.box.setRect(ptb, coords); h.box.setText(ptb, txt); h.arr.add(list, ptb); } else { h.str.release(txt); } } else if (miniexp_consp(head)) { djvu_get_djvu_words(h, list, head, pattern); } expr = miniexp_cdr(expr); } }