#c #android-ndk #mupdf
#c #android-ndk #mupdf
Вопрос:
Я работаю над библиотекой ndk, которая разработана на языке C. Я хочу добавить к нему функцию. для этого мне потребовалась одна простая функция, похожая на нижеприведенную функцию (она уже есть в библиотеке). нижеприведенная функция работает для поиска слов из заданных текстов, и если найдены совпадения, она вернет результаты в виде массива. аналогично, мне нужен массив предложений из заданных текстов страниц.
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(amp;alh, env) || !PageTextBoxHelper_init(amp;ptbh, env)|| !CharacterHelper_init(amp;ch, env))
{
DEBUG("search(): JNI helper initialization failed"); //, pagehandle);
return NULL;
}
jobject arrayList = ArrayListHelper_create(amp;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, amp;rect);
sheet = fz_new_stext_sheet(doc->ctx);
pagetext = fz_new_stext_page(doc->ctx, amp;rect);
dev = fz_new_stext_device(doc->ctx, sheet, pagetext, NULL);
fz_run_page(doc->ctx, page->page, dev, amp;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, amp;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(amp;rr, amp;tmp_rr);
}
if (!fz_is_empty_rect(amp;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(amp;ptbh);
if (ptb)
{
// DEBUG("MuPdfPage(%p).search(%p, %p): rect %p", thiz, doc, page, ptb);
PageTextBoxHelper_setRect(amp;ptbh, ptb, coords);
// PageTextBoxHelper_setText(amp;ptbh, ptb, txt);
// DEBUG("MuPdfPage(%p).search(%p, %p): add rect %p to array %p", thiz, doc, page, ptb, arrayList);
ArrayListHelper_add(amp;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;
}