1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/*
* Implements search and copy functionality for Djvu files.
* Copyright (C) 2006 Michael Hofmann <mh21@piware.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <string.h>
#include <glib.h>
#include <libdjvu/miniexp.h>
#include "djvu-text-page.h"
/**
* djvu_text_page_selection_process:
* @page: #DjvuTextPage instance
* @p: s-expression to append
* @delimit: character/word/... delimiter
*
* Appends the string in @p to the page text.
*
* Returns: whether the end was not reached in this s-expression
*/
static gboolean
djvu_text_page_selection_process (DjvuTextPage *page,
miniexp_t p,
int delimit)
{
if (page->text || p == page->start) {
char *token_text = (char *) miniexp_to_str (miniexp_nth (5, p));
if (page->text) {
char *new_text =
g_strjoin ((delimit & 2) ? "\n" :
(delimit & 1) ? " " : NULL,
page->text, token_text,
NULL);
g_free (page->text);
page->text = new_text;
} else
page->text = g_strdup (token_text);
if (p == page->end)
return FALSE;
}
return TRUE;
}
/**
* 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;
}
static void
djvu_text_page_limits_process (DjvuTextPage *page,
miniexp_t p,
EvRectangle *rect)
{
EvRectangle current;
current.x1 = miniexp_to_int (miniexp_nth (1, p));
current.y1 = miniexp_to_int (miniexp_nth (2, p));
current.x2 = miniexp_to_int (miniexp_nth (3, p));
current.y2 = miniexp_to_int (miniexp_nth (4, p));
if (current.x2 >= rect->x1 && current.y1 <= rect->y2 &&
current.x1 <= rect->x2 && current.y2 >= rect->y1) {
if (page->start == miniexp_nil)
page->start = p;
page->end = p;
}
}
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);
}
}
char *
djvu_text_page_copy (DjvuTextPage *page,
EvRectangle *rectangle)
{
char* text;
page->start = miniexp_nil;
page->end = miniexp_nil;
djvu_text_page_limits (page, page->text_structure, rectangle);
djvu_text_page_selection (page, page->text_structure, 0);
/* Do not free the string */
text = page->text;
page->text = NULL;
return text;
}
/**
* djvu_text_page_position:
* @page: #DjvuTextPage instance
* @position: index in the page text
*
* Returns the closest s-expression that contains the given position in
* the page text.
*
* Returns: closest s-expression
*/
static miniexp_t
djvu_text_page_position (DjvuTextPage *page,
int position)
{
GArray *links = page->links;
int low = 0;
int hi = links->len - 1;
int mid = 0;
g_return_val_if_fail (hi >= 0, miniexp_nil);
/* Shamelessly copied from GNU classpath */
while (low <= hi) {
mid = (low + hi) >> 1;
DjvuTextLink *link =
&g_array_index (links, DjvuTextLink, mid);
if (link->position == position)
break;
else if (link->position > position)
hi = --mid;
else
low = mid + 1;
}
return g_array_index (page->links, DjvuTextLink, mid).pair;
}
/**
* djvu_text_page_union:
* @target: first rectangle and result
* @source: second rectangle
*
* Calculates the bounding box of two rectangles and stores the reuslt
* in the first.
*/
static void
djvu_text_page_union (EvRectangle *target,
EvRectangle *source)
{
if (source->x1 < target->x1)
target->x1 = source->x1;
if (source->x2 > target->x2)
target->x2 = source->x2;
if (source->y1 < target->y1)
target->y1 = source->y1;
if (source->y2 > target->y2)
target->y2 = source->y2;
}
/**
* djvu_text_page_sexpr_process:
* @page: #DjvuTextPage instance
* @p: s-expression to append
* @start: first s-expression in the selection
* @end: last s-expression in the selection
*
* Appends the rectangle defined by @p to the internal bounding box rectangle.
*
* Returns: whether the end was not reached in this s-expression
*/
static gboolean
djvu_text_page_sexpr_process (DjvuTextPage *page,
miniexp_t p,
miniexp_t start,
miniexp_t end)
{
if (page->bounding_box || p == start) {
EvRectangle *new_rectangle = ev_rectangle_new ();
new_rectangle->x1 = miniexp_to_int (miniexp_nth (1, p));
new_rectangle->y1 = miniexp_to_int (miniexp_nth (2, p));
new_rectangle->x2 = miniexp_to_int (miniexp_nth (3, p));
new_rectangle->y2 = miniexp_to_int (miniexp_nth (4, p));
if (page->bounding_box) {
djvu_text_page_union (page->bounding_box,
new_rectangle);
g_free (new_rectangle);
} else
page->bounding_box = new_rectangle;
if (p == end)
return FALSE;
}
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;
}
/**
* djvu_text_page_box:
* @page: #DjvuTextPage instance
* @start: first s-expression in the selection
* @end: last s-expression in the selection
*
* Builds a rectangle that contains all s-expressions in the given range.
*/
static EvRectangle *
djvu_text_page_box (DjvuTextPage *page,
miniexp_t start,
miniexp_t end)
{
page->bounding_box = NULL;
djvu_text_page_sexpr (page, page->text_structure, start, end);
return page->bounding_box;
}
/**
* 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;
g_return_if_fail (miniexp_consp (p) &&
miniexp_symbolp (miniexp_car (p)));
delimit |= page->char_symbol != miniexp_car (p);
miniexp_t 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);
}
}
/**
* djvu_text_page_search:
* @page: #DjvuTextPage instance
* @text: text to search
* @case_sensitive: do not ignore case
*
* Searches the page for the given text. The results list has to be
* externally freed afterwards.
*/
void
djvu_text_page_search (DjvuTextPage *page,
const char *text,
gboolean case_sensitive)
{
char *haystack = page->text;
char *search_text;
int search_len;
EvRectangle *result;
if (page->links->len == 0)
return;
search_len = strlen (text);
if (case_sensitive)
search_text = g_strdup (text);
else
search_text = g_utf8_casefold (text, search_len);
while ((haystack = strstr (haystack, search_text)) != NULL) {
int start_p = haystack - page->text;
miniexp_t start = djvu_text_page_position (page, start_p);
int end_p = start_p + search_len - 1;
miniexp_t end = djvu_text_page_position (page, end_p);
result = djvu_text_page_box (page, start, end);
g_assert (result);
page->results = g_list_prepend (page->results, result);
haystack = haystack + search_len;
}
page->results = g_list_reverse (page->results);
g_free (search_text);
}
/**
* djvu_text_page_prepare_search:
* @page: #DjvuTextPage instance
* @case_sensitive: do not ignore case
*
* Indexes the page text and prepares the page for subsequent searches.
*/
void
djvu_text_page_prepare_search (DjvuTextPage *page,
gboolean case_sensitive)
{
djvu_text_page_append_text (page, page->text_structure,
case_sensitive, FALSE);
}
/**
* djvu_text_page_new:
* @text: S-expression of the page text
*
* Creates a new page to search.
*
* Returns: new #DjvuTextPage instance
*/
DjvuTextPage *
djvu_text_page_new (miniexp_t text)
{
DjvuTextPage *page;
page = g_new0 (DjvuTextPage, 1);
page->links = g_array_new (FALSE, FALSE, sizeof (DjvuTextLink));
page->char_symbol = miniexp_symbol ("char");
page->word_symbol = miniexp_symbol ("word");
page->text_structure = text;
return page;
}
/**
* djvu_text_page_free:
* @page: #DjvuTextPage instance
*
* Frees the given #DjvuTextPage instance.
*/
void
djvu_text_page_free (DjvuTextPage *page)
{
g_free (page->text);
g_array_free (page->links, TRUE);
g_free (page);
}
|