extern "C" jlong Java_org_ebookdroid_droids_djvu_codec_DjvuOutline_getChild(JNIEnv *env, jclass cls, jlong expr) { // DEBUG("DjvuOutline.getChild(%p)",expr); miniexp_t s = miniexp_car((miniexp_t) expr); if (miniexp_consp(s) && miniexp_consp(miniexp_cdr(s)) && miniexp_stringp(miniexp_car(s)) && miniexp_stringp(miniexp_cadr(s))) return (jlong) miniexp_cddr(s); return 0; }
extern "C" jstring Java_org_ebookdroid_droids_djvu_codec_DjvuOutline_getTitle(JNIEnv *env, jclass cls, jlong expr) { // DEBUG("DjvuOutline.getTitle(%p)",expr); miniexp_t s = miniexp_car((miniexp_t) expr); if (miniexp_consp(s) && miniexp_consp(miniexp_cdr(s)) && miniexp_stringp(miniexp_car(s)) && miniexp_stringp(miniexp_cadr(s))) { const char* buf = miniexp_to_str(miniexp_car(s)); return env->NewStringUTF(buf); } return NULL; }
girara_tree_node_t* djvu_document_index_generate(zathura_document_t* document, djvu_document_t* djvu_document, zathura_error_t* error) { if (document == NULL || djvu_document == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_INVALID_ARGUMENTS; } return NULL; } miniexp_t outline = miniexp_dummy; while ((outline = ddjvu_document_get_outline(djvu_document->document)) == miniexp_dummy) { handle_messages(djvu_document, true); } if (outline == miniexp_dummy) { return NULL; } if (miniexp_consp(outline) == 0 || miniexp_car(outline) != miniexp_symbol("bookmarks")) { ddjvu_miniexp_release(djvu_document->document, outline); return NULL; } girara_tree_node_t* root = girara_node_new(zathura_index_element_new("ROOT")); build_index(djvu_document, miniexp_cdr(outline), root); ddjvu_miniexp_release(djvu_document->document, outline); return root; }
/** * 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; }
extern "C" jobject Java_org_ebookdroid_droids_djvu_codec_DjvuPage_getPageText(JNIEnv *jenv, jclass cls, jlong docHandle, jint pageNumber, jlong contextHandle, jstring pattern) { miniexp_t r = miniexp_nil; while ((r = ddjvu_document_get_pagetext((ddjvu_document_t*) docHandle, pageNumber, "word")) == miniexp_dummy) { waitAndHandleMessages(jenv, contextHandle); } if (r == miniexp_nil || !miniexp_consp(r)) { // DEBUG("getPageLinks(%d): no text on page", pageNumber); return NULL; } // DEBUG("getPageLinks(%d): text on page found", pageNumber); SearchHelper h(jenv); if (!h.valid) { DEBUG("getPageLinks(%d): JNI helper initialization failed", pageNumber); return NULL; } jobject arrayList = h.arr.create(); djvu_get_djvu_words(h, arrayList, r, pattern); return arrayList; }
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_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; }
int buildTOC(miniexp_t expr, list * myList, int level, JNIEnv * env, jclass olClass, jmethodID ctor) { while(miniexp_consp(expr)) { miniexp_t s = miniexp_car(expr); expr = miniexp_cdr(expr); if (miniexp_consp(s) && miniexp_consp(miniexp_cdr(s)) && miniexp_stringp(miniexp_car(s)) && miniexp_stringp(miniexp_cadr(s)) ) { // fill item const char *name = miniexp_to_str(miniexp_car(s)); const char *page = miniexp_to_str(miniexp_cadr(s)); //starts with # int pageno = -1; if (page[0] == '#') { pageno = ddjvu_document_search_pageno(doc, &page[1]); } if (pageno < 0) { LOGI("Page %s", page); } if (name == NULL) {return -1;} OutlineItem * element = (OutlineItem *) malloc(sizeof(OutlineItem)); element->title = name; element->page = pageno; element->level = level; list_item * next = (list_item *) malloc(sizeof(list_item)); next->item = element; next->next = NULL; myList->tail->next = next; myList->tail = next; // recursion buildTOC(miniexp_cddr(s), myList, level+1, env, olClass, ctor); } } return 0; }
void QDjViewOutline::fillItems(QTreeWidgetItem *root, miniexp_t expr) { while(miniexp_consp(expr)) { miniexp_t s = miniexp_car(expr); expr = miniexp_cdr(expr); if (miniexp_consp(s) && miniexp_consp(miniexp_cdr(s)) && miniexp_stringp(miniexp_car(s)) && miniexp_stringp(miniexp_cadr(s)) ) { // fill item const char *name = miniexp_to_str(miniexp_car(s)); const char *link = miniexp_to_str(miniexp_cadr(s)); int pageno = pageNumber(link); QString pagename = (pageno>=0)?djview->pageName(pageno):QString(); QTreeWidgetItem *item = new QTreeWidgetItem(root); QString text = QString::fromUtf8(name); if (name && name[0]) item->setText(0, text.replace(spaces," ")); else if (! pagename.isEmpty()) item->setText(0, tr("Page %1").arg(pagename)); item->setFlags(0); item->setWhatsThis(0, whatsThis()); if (link && link[0]) { QString slink = QString::fromUtf8(link); item->setData(0, Qt::UserRole+1, slink); item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled); item->setToolTip(0, tr("Go: %1").arg(slink)); if (pageno >= 0) item->setData(0, Qt::UserRole, pageno); if (! pagename.isEmpty()) item->setToolTip(0, tr("Go: page %1.").arg(pagename)); } // recurse fillItems(item, miniexp_cddr(s)); } } }
void QDjViewOutline::refresh() { QDjVuDocument *doc = djview->getDocument(); if (doc && !loaded && djview->pageNum()>0) { miniexp_t outline = doc->getDocumentOutline(); if (outline == miniexp_dummy) return; loaded = true; if (outline) { if (!miniexp_consp(outline) || miniexp_car(outline) != miniexp_symbol("bookmarks")) { QString msg = tr("Outline data is corrupted"); qWarning("%s", (const char*)msg.toLocal8Bit()); } tree->clear(); QTreeWidgetItem *root = new QTreeWidgetItem(); fillItems(root, miniexp_cdr(outline)); while (root->childCount() > 0) tree->insertTopLevelItem(tree->topLevelItemCount(), root->takeChild(0) ); if (tree->topLevelItemCount() == 1) tree->topLevelItem(0)->setExpanded(true); delete root; } else { tree->clear(); QTreeWidgetItem *root = new QTreeWidgetItem(tree); root->setText(0, tr("Pages")); root->setFlags(Qt::ItemIsEnabled); root->setData(0, Qt::UserRole, -1); for (int pageno=0; pageno<djview->pageNum(); pageno++) { QTreeWidgetItem *item = new QTreeWidgetItem(root); QString name = djview->pageName(pageno); item->setText(0, tr("Page %1").arg(name)); item->setData(0, Qt::UserRole, pageno); item->setData(0, Qt::UserRole+1, pageno); item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled); item->setToolTip(0, tr("Go: page %1.").arg(name)); item->setWhatsThis(0, whatsThis()); } tree->setItemExpanded(root, true); } pageChanged(djview->getDjVuWidget()->page()); } }
extern "C" jstring Java_org_ebookdroid_droids_djvu_codec_DjvuOutline_getLink(JNIEnv *env, jclass cls, jlong expr, jlong docHandle) { // DEBUG("DjvuOutline.getLinkPage(%p)",expr); miniexp_t s = miniexp_car((miniexp_t) expr); if (miniexp_consp(s) && miniexp_consp(miniexp_cdr(s)) && miniexp_stringp(miniexp_car(s)) && miniexp_stringp(miniexp_cadr(s))) { const char *link = miniexp_to_str(miniexp_cadr(s)); int number = -1; if (link && link[0] == '#') { number = ddjvu_document_search_pageno((ddjvu_document_t*) docHandle, link + 1); if (number >= 0) { char linkbuf[128]; snprintf(linkbuf, 127, "#%d", number + 1); return env->NewStringUTF(linkbuf); } } return env->NewStringUTF(link); } return NULL; }
//Outline extern "C" jlong Java_org_ebookdroid_droids_djvu_codec_DjvuOutline_open(JNIEnv *env, jclass cls, jlong docHandle) { // DEBUG("DjvuOutline.open(%p)",docHandle); miniexp_t outline = ddjvu_document_get_outline((ddjvu_document_t*) docHandle); if (outline && outline != miniexp_dummy) { if (!miniexp_consp(outline) || miniexp_car(outline) != miniexp_symbol("bookmarks")) { ERROR("%s", "Outline data is corrupted"); return 0; } else return (jlong) outline; // debug_outline(outline); } return 0; }
/** * 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 void build_index(djvu_document_t *djvu_document, miniexp_t expression, girara_tree_node_t* root) { if (expression == miniexp_nil || root == NULL) { return; } int fileno = ddjvu_document_get_filenum(djvu_document->document); int curfile = 0; while (miniexp_consp(expression) != 0) { miniexp_t inner = miniexp_car(expression); if (miniexp_consp(inner) && miniexp_consp(miniexp_cdr(inner)) && miniexp_stringp(miniexp_car(inner)) && miniexp_stringp(miniexp_car(inner)) ) { const char* name = miniexp_to_str(miniexp_car(inner)); const char* link = miniexp_to_str(miniexp_cadr(inner)); /* TODO: handle other links? */ if (link == NULL || link[0] != '#') { expression = miniexp_cdr(expression); continue; } zathura_link_type_t type = ZATHURA_LINK_GOTO_DEST; zathura_rectangle_t rect; zathura_link_target_t target = { 0 }; target.destination_type = ZATHURA_LINK_DESTINATION_XYZ; /* Check if link+1 contains a number */ bool number = true; const size_t linklen = strlen(link); for (unsigned int k = 1; k < linklen; k++) { if (!isdigit(link[k])) { number = false; break; } } /* if link starts with a number assume it is a number */ if (number == true) { target.page_number = atoi(link + 1) - 1; } else { /* otherwise assume it is an id for a page */ ddjvu_fileinfo_t info; int f, i; for (i=0; i < fileno; i++) { f = (curfile + i) % fileno; ddjvu_document_get_fileinfo(djvu_document->document, f, &info); if (info.id != NULL && !strcmp(link+1, info.id)) { break; } } /* got a page */ if (i < fileno && info.pageno >= 0) { curfile = (f+1) % fileno; target.page_number = info.pageno; } else { /* give up */ expression = miniexp_cdr(expression); continue; } } zathura_index_element_t* index_element = zathura_index_element_new(name); if (index_element == NULL) { continue; } index_element->link = zathura_link_new(type, rect, target); if (index_element->link == NULL) { zathura_index_element_free(index_element); continue; } girara_tree_node_t* node = girara_node_append_data(root, index_element); /* search recursive */ build_index(djvu_document, miniexp_cddr(inner), node); } expression = miniexp_cdr(expression); } }
extern "C" jboolean Java_org_ebookdroid_droids_djvu_codec_DjvuOutline_expConsp(JNIEnv *env, jclass cls, jlong expr) { // DEBUG("DjvuOutline.expConsp(%p)",expr); return miniexp_consp((miniexp_t) expr); }
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); } }
JNIEXPORT jobjectArray JNICALL Java_universe_constellation_orion_viewer_djvu_DjvuDocument_getOutline(JNIEnv * env, jobject thiz) { miniexp_t outline = ddjvu_document_get_outline(doc); if (outline == miniexp_dummy || outline == NULL) { return NULL; } if (!miniexp_consp(outline) || miniexp_car(outline) != miniexp_symbol("bookmarks")) { LOGI("Outlines is empty"); } list_item * root = (list_item *) malloc(sizeof(list_item)); root->next = NULL; list * myList = (list *) malloc(sizeof(list)); myList->head = root; myList->tail = root; jclass olClass; jmethodID ctor; olClass = (*env)->FindClass(env, "universe/constellation/orion/viewer/outline/OutlineItem"); if (olClass == NULL) return NULL; ctor = (*env)->GetMethodID(env, olClass, "<init>", "(ILjava/lang/String;I)V"); if (ctor == NULL) return NULL; buildTOC(miniexp_cdr(outline), myList, 0, env, olClass, ctor); list_item * next = myList->head; int size = 0; while (next->next != NULL) { next = next->next; size++; } LOGI("Outline has %i entries", size); jobjectArray arr = (*env)->NewObjectArray(env, size, olClass, NULL); if (arr == NULL) { return NULL; } next = root->next; int pos = 0; while (next != NULL) { OutlineItem * item = next->item; jstring title = (*env)->NewStringUTF(env, item->title); //shift pageno to zero based jobject element = (*env)->NewObject(env, olClass, ctor, item->level, title, item->page - 1); (*env)->SetObjectArrayElement(env, arr, pos, element); (*env)->DeleteLocalRef(env, title); (*env)->DeleteLocalRef(env, element); free(item); list_item * next2 = next->next; free(next); next = next2; pos++; } free(root); return arr; }