I am working on ndk lib which is developed in the C language. I want to add a feature to it. for that, I required one simple function which is similar to the bellow function(it's already in lib). bellow function is work for search words from given texts and if matches found that it will return the results in an array. similarly, I want an array of sentences from those given page texts.
JNIEXPORT jobjectArray JNICALL
JNI_FN(MuPdfPage_search)(JNIEnv * env, jobject thiz, jlong dochandle, jlong pagehandle,
jstring text)
{
renderdocument_t *doc = (renderdocument_t*) (long) dochandle;
renderpage_t *page = (renderpage_t*) (long) pagehandle;
// DEBUG("MuPdfPage(%p).search(%p, %p)", thiz, doc, page);
if (!doc || !page)
{
return NULL;
}
const char *str = (*env)->GetStringUTFChars(env, text, NULL);
if (str == NULL)
{
return NULL;
}
ArrayListHelper alh;
PageTextBoxHelper ptbh;
CharacterHelper ch;
if (!ArrayListHelper_init(&alh, env) || !PageTextBoxHelper_init(&ptbh, env)|| !CharacterHelper_init(&ch, env))
{
DEBUG("search(): JNI helper initialization failed"); //, pagehandle);
return NULL;
}
jobject arrayList = ArrayListHelper_create(&alh);
// DEBUG("MuPdfPage(%p).search(%p, %p): array: %p", thiz, doc, page, arrayList);
if (!arrayList)
{
return NULL;
}
fz_rect *hit_bbox = NULL;
fz_stext_sheet *sheet = NULL;
fz_stext_page *pagetext = NULL;
fz_device *dev = NULL;
int pos;
int len;
int i, n;
int hit_count = 0;
fz_try(doc->ctx)
{
fz_rect rect;
// DEBUG("MuPdfPage(%p).search(%p, %p): load page text", thiz, doc, page);
fz_bound_page(doc->ctx, page->page, &rect);
sheet = fz_new_stext_sheet(doc->ctx);
pagetext = fz_new_stext_page(doc->ctx, &rect);
dev = fz_new_stext_device(doc->ctx, sheet, pagetext, NULL);
fz_run_page(doc->ctx, page->page, dev, &fz_identity, NULL);
// DEBUG("MuPdfPage(%p).search(%p, %p): free text device", thiz, doc, page);
fz_close_device(doc->ctx, dev);
fz_drop_device(doc->ctx, dev);
dev = NULL;
len = textlen(pagetext);
// DEBUG("MuPdfPage(%p).search(%p, %p): text length: %d", thiz, doc, page, len);
for (pos = 0; pos < len; pos++)
{
fz_rect rr = fz_empty_rect;
// DEBUG("MuPdfPage(%p).search(%p, %p): match %d", thiz, doc, page, pos);
n = match(doc->ctx, &ch, pagetext, str, pos);
if (n > 0)
{
// DEBUG("MuPdfPage(%p).search(%p, %p): match found: %d, %d", thiz, doc, page, pos, n);
for (i = 0; i < n; i++)
{
fz_rect tmp_rr = bboxcharat(doc->ctx, pagetext, pos + i);
rr = *fz_union_rect(&rr, &tmp_rr);
}
if (!fz_is_empty_rect(&rr))
{
int coords[4];
coords[0] = (rr.x0);
coords[1] = (rr.y0);
coords[2] = (rr.x1);
coords[3] = (rr.y1);
// DEBUG("MuPdfPage(%p).search(%p, %p): found rectangle (%d, %d - %d, %d)", thiz, doc, page, coords[0], coords[1], coords[2], coords[3]);
jobject ptb = PageTextBoxHelper_create(&ptbh);
if (ptb)
{
// DEBUG("MuPdfPage(%p).search(%p, %p): rect %p", thiz, doc, page, ptb);
PageTextBoxHelper_setRect(&ptbh, ptb, coords);
// PageTextBoxHelper_setText(&ptbh, ptb, txt);
// DEBUG("MuPdfPage(%p).search(%p, %p): add rect %p to array %p", thiz, doc, page, ptb, arrayList);
ArrayListHelper_add(&alh, arrayList, ptb);
}
}
}
}
} fz_always(doc->ctx)
{
// DEBUG("MuPdfPage(%p).search(%p, %p): free resources", thiz, doc, page);
if (pagetext)
{
fz_drop_stext_page(doc->ctx, pagetext);
}
if (sheet)
{
fz_drop_stext_sheet(doc->ctx, sheet);
}
if (dev)
{
fz_drop_device(doc->ctx, dev);
}
}fz_catch(doc->ctx)
{
jclass cls;
(*env)->ReleaseStringUTFChars(env, text, str);
cls = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
if (cls != NULL)
{
(*env)->ThrowNew(env, cls, "Out of memory in MuPDFCore_searchPage");
}
(*env)->DeleteLocalRef(env, cls);
return NULL;
}
(*env)->ReleaseStringUTFChars(env, text, str);
return arrayList;
}