summaryrefslogtreecommitdiffstats
path: root/kviewshell/renderedDocumentPage.cpp
blob: 6bfc454cee05566fb2323bc78b77d2781c0e14b5 (plain)
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
//
// Class: RenderedDocumentPage
//
// Widget for displaying TeX DVI files.
// Part of KDVI- A previewer for TeX DVI files.
//
// (C) 2004 Stefan Kebekus. Distributed under the GPL.

#include <config.h>

#include <kdebug.h>

#include "renderedDocumentPage.h"
#include "hyperlink.h"
#include "selection.h"
#include "textBox.h"


RenderedDocumentPage::RenderedDocumentPage()
{
  textBoxList.reserve(250);
  pageNr = 0;
  isEmpty = true;
  pageText = TQString();
}


RenderedDocumentPage::~RenderedDocumentPage()
{
  ;
}


void RenderedDocumentPage::setPageNumber(const PageNumber& pnr)
{
  pageNr = pnr;
  clear();
}


void RenderedDocumentPage::clear()
{
#ifdef DEBUG_DOCUMENTPAGE
  kdDebug(1223) << "RenderedDocumentPage::clear() called for page #" << pageNumber << endl;
#endif

  textBoxList.clear();
  hyperLinkList.clear();
  pageText = TQString();

  isEmpty = true;
}


TQRegion RenderedDocumentPage::selectedRegion(const TextSelection& selection)
{
  if (selection.isEmpty() || selection.getPageNumber() != pageNr)
    return TQRegion();

  int startIndex = selection.getSelectedTextStart();
  int endIndex = selection.getSelectedTextEnd();

  TQValueVector<TQRect> wordBox;

  TQRect currentWordBox;
  //unsigned int currentBaseline = 0;

  // Merge character boxes into boxes containing complete words.
  // Note: A word in this context is defined as a string of boxes
  // with the same baseline.
  for (int i = startIndex; i <= endIndex; i++)
  {
    if (i == 0)
    {
      // start first word
      currentWordBox = textBoxList[i].box;
      //currentBaseline = textBoxList[i].baseline;
      continue;
    }

    /*if (currentBaseline == textBoxList[i].baseline)
    {
      currentWordBox = currentWordBox.unite(textBoxList[i].box);
    }
    else*/
    {
      // start next word
      wordBox.push_back(currentWordBox);
      currentWordBox = textBoxList[i].box;
      //currentBaseline = textBoxList[i].baseline;
    }
  }
  // we still need to store the last word
  wordBox.push_back(currentWordBox);

  TQValueVector<TQRect> lineBox;

  // Merge word boxes into boxes containing whole lines.
  // We start a new line if we encounter a wordbox which does not
  // vertically overlap which the current lineBox.
  TQRect currentLineBox;

  for (unsigned int i = 0; i < wordBox.size(); i++)
  {
    if (!currentLineBox.isValid())
    {
      // start first line
      currentLineBox = wordBox[i];
      continue;
    }

    // check if there is vertical overlap
    if (wordBox[i].top() <= currentLineBox.bottom() && wordBox[i].bottom() >= currentLineBox.top())
    {
      // the word belongs to the current line
      currentLineBox = currentLineBox.unite(wordBox[i]);
    }
    else
    {
      // start next line
      //kdDebug() << "push line (" << currentLineBox.top() << ", " << currentLineBox.bottom() << ")" << endl;
      lineBox.push_back(currentLineBox);
      currentLineBox = wordBox[i];
    }
  }
  // we still need to store the last line
  //kdDebug() << "push line (" << currentLineBox.top() << ", " << currentLineBox.bottom() << ")" << endl;
  lineBox.push_back(currentLineBox);

  //kdDebug() << "Number of lineboxes = " << lineBox.size() << endl;

  // Now we increase the height of the lines if necessary to obtain a connected region
  // for our selection.
  for (unsigned int i = 0; i < lineBox.size() - 1; i++)
  {
      if (lineBox[i+1].top() >= lineBox[i].bottom())
      {
        int midPoint = (lineBox[i].bottom() + lineBox[i+1].top()) / 2;

        lineBox[i].setBottom(midPoint);
        lineBox[i+1].setTop(midPoint+1);
      }
  }

  // Add the lineboxes to a Region
  TQRegion selectedRegion;
  for (unsigned int i = 0; i < lineBox.size(); i++)
  {
    selectedRegion += TQRegion(lineBox[i]);
  }

  return selectedRegion;
}


TextSelection RenderedDocumentPage::select(const TQRect& selectedRectangle)
{
  int selectedTextStart = -1;
  int selectedTextEnd   = -1;

  // Find the smallest and biggest index for which the corresponding
  // textBoxList entry intersects the selected rectangle.
  for (unsigned int i=0; i<textBoxList.size(); i++)
  {
    if (selectedRectangle.intersects(textBoxList[i].box))
    {
      if (selectedTextStart == -1)
        selectedTextStart = i;
      selectedTextEnd = i;
    }
  }

  TextSelection selection;

  TQString selectedText;

  if (selectedTextStart != -1)
  {
    for (int i = selectedTextStart; (i <= selectedTextEnd) && (i < (int)textBoxList.size()); i++)
    {
      selectedText += textBoxList[i].text;
    }
    selection.set(pageNr, selectedTextStart, selectedTextEnd, selectedText);
    return selection;
  }
  // return empty selection
  return selection;
}

TextSelection RenderedDocumentPage::select(const TQPoint& point)
{
  int selectedTextStart = -1;
  int selectedTextEnd   = -1;

  for (unsigned int i=0; i<textBoxList.size(); i++)
  {
    if (textBoxList[i].box.contains(point))
    {
      selectedTextStart = i;
      selectedTextEnd = i;
      break;
    }
  }

  TextSelection selection;

  TQString selectedText;

  if (selectedTextStart != -1)
  {
    selectedText = textBoxList[selectedTextStart].text;
    selection.set(pageNr, selectedTextStart, selectedTextEnd, selectedText);
    return selection;
  }
  // return empty selection
  return selection;
}

TextSelection RenderedDocumentPage::find(const TQString& str, int index, bool caseSensitive)
{
  if (pageText.isNull())
  {
    // Create the pageText by joining all entries of textBoxList.
    for (TQValueVector<TextBox>::Iterator i = textBoxList.begin(); i != textBoxList.end(); i++)
    {
      pageText = pageText + i->text;
    }
  }

  // Create empty selection;
  TextSelection selection;

  // If the page contains no searchable text
  if (pageText.isNull())
    return selection;

  // Compute the corresponding pageText index
  unsigned int subIndex = 0;
  for (int i = 0; i < index; i++)
  {
    subIndex += textBoxList[i].text.length();
  }

  int textIndex = pageText.find(str, subIndex, caseSensitive);

  if (textIndex == -1)
    return selection;

  // Because a single Hyperlink structure can possible store more then
  // one character we now have to search for the Indices in the
  // textBoxList Vector which corresponds to the found index in the
  // String pageText.  FIXME: It would be faster to search directly in
  // the textBoxList.
  int counter = 0;
  int startIndex = 0;
  while (counter < textIndex)
  {
    counter += textBoxList[startIndex].text.length();
    // If the string we searched for starts in the middle of some text element we better return a
    // selection that it somewhat bigger.
    if (counter > textIndex)
      break;

    startIndex++;

    // safety check
    if (startIndex >= (int)textBoxList.size())
      return selection;
  }

  // Search for the end index.
  // TODO: This algorithm is not entirely correct if str does not start exactly at the beginning of some text element.
  counter = 0;
  int endIndex = startIndex;
  while (counter < (int)str.length())
  {
    counter += textBoxList[endIndex].text.length();
    if (counter >= (int)str.length())
      break;

    endIndex++;

    // safety check
    if (endIndex >= (int)textBoxList.size())
      return selection;
  }

  // Set the selection
  selection.set(pageNr, startIndex, endIndex, str);
  return selection;
}


TextSelection RenderedDocumentPage::findRev(const TQString& str, int index, bool caseSensitive)
{
  // Negative index means we start the search at the end of the text.
  if (index < 0)
  {
    index = textBoxList.size();
  }

  if (pageText.isNull())
  {
    // Create the pageText by joining all entries of textBoxList.
    for (TQValueVector<TextBox>::Iterator i = textBoxList.begin(); i != textBoxList.end(); i++)
    {
      pageText = pageText + i->text;
    }
  }

  // Create empty selection;
  TextSelection selection;

  // If the page contains no searchable text
  if (pageText.isNull())
    return selection;

  // Compute the corresponding pageText index
  unsigned int subIndex = 0;
  for (int i = 0; i < index; i++)
  {
    subIndex += textBoxList[i].text.length();
  }

  int textIndex = pageText.findRev(str, subIndex, caseSensitive);

  if (textIndex == -1)
    return selection;

  // Because a single Hyperlink structure can possible store more then
  // one character we now have to search for the Indices in the
  // textBoxList Vector which corresponds to the found index in the
  // String pageText.  FIXME: It would be faster to search directly in
  // the textBoxList.
  int counter = 0;
  int startIndex = 0;
  while (counter < textIndex)
  {
    counter += textBoxList[startIndex].text.length();
    // If the string we searched for starts in the middle of some text element we better return a
    // selection that it somewhat bigger.
    if (counter > textIndex)
      break;

    startIndex++;

    // safety check
    if (startIndex >= (int)textBoxList.size())
      return selection;
  }

  // Search for the end index.
  // TODO: This algorithm is not entirely correct if str does not start exactly at the beginning of some text element.
  counter = 0;
  int endIndex = startIndex;
  while (counter < (int)str.length())
  {
    counter += textBoxList[endIndex].text.length();
    if (counter >= (int)str.length())
      break;

    endIndex++;

    // safety check
    if (endIndex >= (int)textBoxList.size())
      return selection;
  }

  // Set the selection
  selection.set(pageNr, startIndex, endIndex, str);
  return selection;
}

#include "renderedDocumentPage.moc"