2 * Implements search and copy functionality for Djvu files.
3 * Copyright (C) 2006 Michael Hofmann <mh21@piware.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <libdjvu/miniexp.h>
23 #include "djvu-text-page.h"
27 * djvu_text_page_selection_process:
28 * @page: #DjvuTextPage instance
29 * @p: s-expression to append
30 * @delimit: character/word/... delimiter
32 * Appends the string in @p to the page text.
34 * Returns: whether the end was not reached in this s-expression
37 djvu_text_page_selection_process (DjvuTextPage *page,
41 if (page->text || p == page->start) {
42 char *token_text = (char *) miniexp_to_str (miniexp_nth (5, p));
45 g_strjoin (delimit & 2 ? "\n" :
46 delimit & 1 ? " " : NULL,
47 page->text, token_text,
50 page->text = new_text;
52 page->text = g_strdup (token_text);
60 * djvu_text_page_selection:
61 * @page: #DjvuTextPage instance
63 * @delimit: character/word/... delimiter
65 * Walks the tree in @p and appends the text with
66 * djvu_text_page_selection_process() for all s-expressions
67 * between the start and end fields.
69 * Returns: whether the end was not reached in this subtree
72 djvu_text_page_selection (DjvuTextPage *page,
76 g_return_val_if_fail (miniexp_consp (p) && miniexp_symbolp
77 (miniexp_car (p)), FALSE);
79 if (miniexp_car (p) != page->char_symbol)
80 delimit |= miniexp_car (p) == page->word_symbol ? 1 : 2;
82 miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
83 while (deeper != miniexp_nil) {
84 miniexp_t str = miniexp_car (deeper);
85 if (miniexp_stringp (str)) {
86 if (!djvu_text_page_selection_process
90 if (!djvu_text_page_selection
95 deeper = miniexp_cdr (deeper);
101 djvu_text_page_limits_process (DjvuTextPage *page,
107 current.x1 = miniexp_to_int (miniexp_nth (1, p));
108 current.y1 = miniexp_to_int (miniexp_nth (2, p));
109 current.x2 = miniexp_to_int (miniexp_nth (3, p));
110 current.y2 = miniexp_to_int (miniexp_nth (4, p));
111 if (current.x2 >= rect->x1 && current.y1 <= rect->y2 &&
112 current.x1 <= rect->x2 && current.y2 >= rect->y1) {
113 if (page->start == miniexp_nil)
121 djvu_text_page_limits (DjvuTextPage *page,
127 g_return_if_fail (miniexp_consp (p) &&
128 miniexp_symbolp (miniexp_car (p)));
130 miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
131 while (deeper != miniexp_nil) {
132 miniexp_t str = miniexp_car (deeper);
133 if (miniexp_stringp (str))
134 djvu_text_page_limits_process (page, p, rect);
136 djvu_text_page_limits (page, str, rect);
138 deeper = miniexp_cdr (deeper);
143 djvu_text_page_copy (DjvuTextPage *page,
144 EvRectangle *rectangle)
148 page->start = miniexp_nil;
149 page->end = miniexp_nil;
150 djvu_text_page_limits (page, page->text_structure, rectangle);
151 djvu_text_page_selection (page, page->text_structure, 0);
153 /* Do not free the string */
161 * djvu_text_page_position:
162 * @page: #DjvuTextPage instance
163 * @position: index in the page text
165 * Returns the closest s-expression that contains the given position in
168 * Returns: closest s-expression
171 djvu_text_page_position (DjvuTextPage *page,
174 GArray *links = page->links;
176 int hi = links->len - 1;
179 g_return_val_if_fail (hi >= 0, miniexp_nil);
181 /* Shamelessly copied from GNU classpath */
183 mid = (low + hi) >> 1;
185 &g_array_index (links, DjvuTextLink, mid);
186 if (link->position == position)
188 else if (link->position > position)
194 return g_array_index (page->links, DjvuTextLink, mid).pair;
198 * djvu_text_page_union:
199 * @target: first rectangle and result
200 * @source: second rectangle
202 * Calculates the bounding box of two rectangles and stores the reuslt
206 djvu_text_page_union (EvRectangle *target,
209 if (source->x1 < target->x1)
210 target->x1 = source->x1;
211 if (source->x2 > target->x2)
212 target->x2 = source->x2;
213 if (source->y1 < target->y1)
214 target->y1 = source->y1;
215 if (source->y2 > target->y2)
216 target->y2 = source->y2;
220 * djvu_text_page_sexpr_process:
221 * @page: #DjvuTextPage instance
222 * @p: s-expression to append
223 * @start: first s-expression in the selection
224 * @end: last s-expression in the selection
226 * Appends the rectangle defined by @p to the internal bounding box rectangle.
228 * Returns: whether the end was not reached in this s-expression
231 djvu_text_page_sexpr_process (DjvuTextPage *page,
236 if (page->bounding_box || p == start) {
237 EvRectangle *new_rectangle = g_new (EvRectangle, 1);
238 new_rectangle->x1 = miniexp_to_int (miniexp_nth (1, p));
239 new_rectangle->y1 = miniexp_to_int (miniexp_nth (2, p));
240 new_rectangle->x2 = miniexp_to_int (miniexp_nth (3, p));
241 new_rectangle->y2 = miniexp_to_int (miniexp_nth (4, p));
242 if (page->bounding_box) {
243 djvu_text_page_union (page->bounding_box,
245 g_free (new_rectangle);
247 page->bounding_box = new_rectangle;
255 * djvu_text_page_sexpr:
256 * @page: #DjvuTextPage instance
258 * @start: first s-expression in the selection
259 * @end: last s-expression in the selection
261 * Walks the tree in @p and extends the rectangle with
262 * djvu_text_page_process() for all s-expressions between @start and @end.
264 * Returns: whether the end was not reached in this subtree
267 djvu_text_page_sexpr (DjvuTextPage *page,
272 g_return_val_if_fail (miniexp_consp (p) && miniexp_symbolp
273 (miniexp_car (p)), FALSE);
275 miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
276 while (deeper != miniexp_nil) {
277 miniexp_t str = miniexp_car (deeper);
278 if (miniexp_stringp (str)) {
279 if (!djvu_text_page_sexpr_process
280 (page, p, start, end))
283 if (!djvu_text_page_sexpr
284 (page, str, start, end))
287 deeper = miniexp_cdr (deeper);
293 * djvu_text_page_box:
294 * @page: #DjvuTextPage instance
295 * @start: first s-expression in the selection
296 * @end: last s-expression in the selection
298 * Builds a rectangle that contains all s-expressions in the given range.
301 djvu_text_page_box (DjvuTextPage *page,
305 page->bounding_box = NULL;
306 djvu_text_page_sexpr (page, page->text_structure, start, end);
307 return page->bounding_box;
311 * djvu_text_page_append_search:
312 * @page: #DjvuTextPage instance
314 * @case_sensitive: do not ignore case
315 * @delimit: insert spaces because of higher (sentence/paragraph/...) break
317 * Appends the tree in @p to the internal text string.
320 djvu_text_page_append_text (DjvuTextPage *page,
322 gboolean case_sensitive,
327 g_return_if_fail (miniexp_consp (p) &&
328 miniexp_symbolp (miniexp_car (p)));
330 delimit |= page->char_symbol != miniexp_car (p);
332 miniexp_t deeper = miniexp_cddr (miniexp_cdddr (p));
333 while (deeper != miniexp_nil) {
334 miniexp_t data = miniexp_car (deeper);
335 if (miniexp_stringp (data)) {
337 link.position = page->text == NULL ? 0 :
340 g_array_append_val (page->links, link);
342 token_text = (char *) miniexp_to_str (data);
344 token_text = g_utf8_casefold (token_text, -1);
345 if (page->text == NULL)
346 page->text = g_strdup (token_text);
349 g_strjoin (delimit ? " " : NULL,
350 page->text, token_text,
353 page->text = new_text;
358 djvu_text_page_append_text (page, data,
359 case_sensitive, delimit);
361 deeper = miniexp_cdr (deeper);
366 * djvu_text_page_search:
367 * @page: #DjvuTextPage instance
368 * @text: text to search
370 * Searches the page for the given text. The results list has to be
371 * externally freed afterwards.
374 djvu_text_page_search (DjvuTextPage *page,
377 char *haystack = page->text;
380 if (page->links->len == 0)
383 search_len = strlen (text);
384 while ((haystack = strstr (haystack, text)) != NULL) {
385 int start_p = haystack - page->text;
386 miniexp_t start = djvu_text_page_position (page, start_p);
387 int end_p = start_p + search_len - 1;
388 miniexp_t end = djvu_text_page_position (page, end_p);
389 result = djvu_text_page_box (page, start, end);
391 page->results = g_list_prepend (page->results, result);
392 haystack = haystack + search_len;
394 page->results = g_list_reverse (page->results);
399 * djvu_text_page_prepare_search:
400 * @page: #DjvuTextPage instance
401 * @case_sensitive: do not ignore case
403 * Indexes the page text and prepares the page for subsequent searches.
406 djvu_text_page_prepare_search (DjvuTextPage *page,
407 gboolean case_sensitive)
409 djvu_text_page_append_text (page, page->text_structure,
410 case_sensitive, FALSE);
414 * djvu_text_page_new:
415 * @text: S-expression of the page text
417 * Creates a new page to search.
419 * Returns: new #DjvuTextPage instance
422 djvu_text_page_new (miniexp_t text)
426 page = g_new0 (DjvuTextPage, 1);
427 page->links = g_array_new (FALSE, FALSE, sizeof (DjvuTextLink));
428 page->char_symbol = miniexp_symbol ("char");
429 page->word_symbol = miniexp_symbol ("word");
430 page->text_structure = text;
435 * djvu_text_page_free:
436 * @page: #DjvuTextPage instance
438 * Frees the given #DjvuTextPage instance.
441 djvu_text_page_free (DjvuTextPage *page)
444 g_array_free (page->links, TRUE);