summaryrefslogtreecommitdiff
path: root/Build/source/libs/xpdf/xpdf-src/xpdf/PDFCore.h
blob: 56ecd958c2197a0812578b9333e28cf781d587c4 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//========================================================================
//
// PDFCore.h
//
// Copyright 2004-2014 Glyph & Cog, LLC
//
//========================================================================

#ifndef PDFCORE_H
#define PDFCORE_H

#include <aconf.h>

#include <stdlib.h>
#ifndef PDF_PARSER_ONLY
#include <atomic>
#endif
#include "SplashTypes.h"
#include "CharTypes.h"
#include "DisplayState.h"
#include "TextOutputDev.h"

class GString;
class GList;
class SplashBitmap;
class SplashPattern;
class BaseStream;
class PDFDoc;
class Links;
class LinkDest;
class LinkAction;
class Annot;
class AcroFormField;
class TextPage;
class HighlightFile;
class OptionalContentGroup;
class TileMap;
class TileCache;
class TileCompositor;
class PDFCore;

//------------------------------------------------------------------------
// PDFHistory
//------------------------------------------------------------------------

struct PDFHistory {
#ifdef _WIN32
  wchar_t *fileName;
#else
  GString *fileName;
#endif
  int page;
};

#define pdfHistorySize 50

//------------------------------------------------------------------------
// SelectMode
//------------------------------------------------------------------------

enum SelectMode {
  selectModeBlock,
  selectModeLinear
};

//------------------------------------------------------------------------
// FindResult
//------------------------------------------------------------------------

struct FindResult {
  FindResult(int pageA, double xMinA, double yMinA, double xMaxA, double yMaxA)
    : page(pageA), xMin(xMinA), yMin(yMinA), xMax(xMaxA), yMax(yMaxA) {}
  int page;
  double xMin, yMin, xMax, yMax;
};

//------------------------------------------------------------------------
// AsyncFindAll
//------------------------------------------------------------------------
#ifndef PDF_PARSER_ONLY
class AsyncFindAll {
public:

  AsyncFindAll(PDFCore *coreA): core(coreA), canceled(gFalse) {}

  void reset() { canceled = false; }

  // Run the search, returning a list of FindResults -- same as
  // PDFCore::findAll().  This can be run on a separate thread.  If
  // cancel() is called while the search is running, this function
  // returns null.
  GList *run(PDFDoc *doc, Unicode *u, int len, GBool caseSensitive,
	     GBool wholeWord, int firstPage, int lastPage);

  // Cancel a running search, causing run() to return null.
  void cancel() { canceled = true; }

private:

  PDFCore *core;
  std::atomic<bool> canceled;
};
#endif

//------------------------------------------------------------------------
// PDFCore
//------------------------------------------------------------------------

class PDFCore {
public:

  PDFCore(SplashColorMode colorMode, int bitmapRowPad,
	  GBool reverseVideo, SplashColorPtr paperColor);
  virtual ~PDFCore();

  //----- loadFile / displayPage / displayDest

  // Load a new file.  Returns pdfOk or error code.
  virtual int loadFile(GString *fileName, GString *ownerPassword = NULL,
		       GString *userPassword = NULL);

#ifdef _WIN32
  // Load a new file.  Returns pdfOk or error code.
  virtual int loadFile(wchar_t *fileName, int fileNameLen,
		       GString *ownerPassword = NULL,
		       GString *userPassword = NULL);
#endif

  // Load a new file, via a Stream instead of a file name.  Returns
  // pdfOk or error code.
  virtual int loadFile(BaseStream *stream, GString *ownerPassword = NULL,
		       GString *userPassword = NULL);

  // Load an already-created PDFDoc object.
  virtual void loadDoc(PDFDoc *docA);

  // Reload the current file.  This only works if the PDF was loaded
  // via a file.  Returns pdfOk or error code.
  virtual int reload();

  // Clear out the current document, if any.
  virtual void clear();

  // Same as clear(), but returns the PDFDoc object instead of
  // deleting it.
  virtual PDFDoc *takeDoc(GBool redraw);

  // Display (or redisplay) the specified page.  If <scrollToTop> is
  // set, the window is vertically scrolled to the top; if
  // <scrollToBottom> is set, the window is vertically scrolled to the
  // bottom; otherwise, no scrolling is done.  If <addToHist> is set,
  // this page change is added to the history list.
  virtual void displayPage(int page, GBool scrollToTop,
			   GBool scrollToBottom, GBool addToHist = gTrue);

  // Display a link destination.
  virtual void displayDest(LinkDest *dest);

  // Called before any update is started.
  virtual void startUpdate();

  // Called after any update is complete.  Subclasses can check for
  // changes in the display parameters here.
  virtual void finishUpdate(GBool addToHist, GBool checkForChangedFile);

  //----- page/position changes

  virtual GBool gotoNextPage(int inc, GBool top);
  virtual GBool gotoPrevPage(int dec, GBool top, GBool bottom);
  virtual GBool gotoNamedDestination(GString *dest);
  virtual GBool goForward();
  virtual GBool goBackward();
  virtual void scrollLeft(int nCols = 16);
  virtual void scrollRight(int nCols = 16);
  virtual void scrollUp(int nLines = 16, GBool snapToPage = gFalse);
  virtual void scrollUpPrevPage(int nLines = 16);
  virtual void scrollDown(int nLines = 16, GBool snapToPage = gFalse);
  virtual void scrollDownNextPage(int nLines = 16);
  virtual void scrollPageUp();
  virtual void scrollPageDown();
  virtual void scrollTo(int x, int y, GBool snapToPage = gFalse);
  virtual void scrollToLeftEdge();
  virtual void scrollToRightEdge();
  virtual void scrollToTopEdge();
  virtual void scrollToBottomEdge();
  virtual void scrollToTopLeft();
  virtual void scrollToBottomRight();
  // Scroll so that (page, x, y) is centered in the window.
  virtual void scrollToCentered(int page, double x, double y);
  virtual void setZoom(double zoom);
  virtual void zoomToRect(int page, double ulx, double uly,
			  double lrx, double lry);
  virtual void zoomCentered(double zoom);
  virtual void zoomToCurrentWidth();
  virtual void setRotate(int rotate);
  virtual void setDisplayMode(DisplayMode mode);
  virtual void setOCGState(OptionalContentGroup *ocg, GBool ocgState);

  //----- selection

  // Selection mode.
  SelectMode getSelectMode() { return selectMode; }
  void setSelectMode(SelectMode mode);

  // Selection color.
  SplashColorPtr getSelectionColor();
  void setSelectionColor(SplashColor color);

  // Modify the selection.  These functions use device coordinates.
  void setSelection(int page, int x0, int y0, int x1, int y1);
  void setLinearSelection(int page, TextPosition *pos0, TextPosition *pos1);
  void clearSelection();
  void startSelectionDrag(int pg, int x, int y);
  void moveSelectionDrag(int pg, int x, int y);
  void finishSelectionDrag();
  void selectWord(int pg, int x, int y);
  void selectLine(int pg, int x, int y);

  // Retrieve the current selection.  This function uses user
  // coordinates.  Returns false if there is no selection.
  GBool getSelection(int *pg, double *ulx, double *uly,
		     double *lrx, double *lry);
  GBool hasSelection();

  // Text extraction.
  void setTextExtractionMode(TextOutputMode mode);
  GBool getDiscardDiagonalText();
  void setDiscardDiagonalText(GBool discard);
  GString *extractText(int pg, double xMin, double yMin,
		       double xMax, double yMax);
  GString *getSelectedText();

  //----- find

  virtual GBool find(char *s, GBool caseSensitive, GBool next, GBool backward,
		     GBool wholeWord, GBool onePageOnly);
  virtual GBool findU(Unicode *u, int len, GBool caseSensitive,
		      GBool next, GBool backward, GBool wholeWord,
		      GBool onePageOnly);
  GList *findAll(Unicode *u, int len, GBool caseSensitive,
		 GBool wholeWord, int firstPage, int lastPage);


  //----- coordinate conversion

  // user space: per-page, as defined by PDF file; unit = point
  // device space: (0,0) is upper-left corner of a page; unit = pixel
  // window space: (0,0) is upper-left corner of drawing area; unit = pixel

  GBool cvtWindowToUser(int xw, int yw, int *pg, double *xu, double *yu);
  GBool cvtWindowToDev(int xw, int yw, int *pg, int *xd, int *yd);
  GBool cvtUserToWindow(int pg, double xy, double yu, int *xw, int *yw);
  void cvtUserToDev(int pg, double xu, double yu, int *xd, int *yd);
  GBool cvtDevToWindow(int pg, int xd, int yd, int *xw, int *yw);
  void cvtDevToUser(int pg, int xd, int yd, double *xu, double *yu);
  void getWindowPageRange(int x, int y, int w, int h,
			  int *firstPage, int *lastPage);

  //----- password dialog

  virtual GString *getPassword() { return NULL; }

  //----- misc access

  PDFDoc *getDoc() { return doc; }
  int getPageNum();
  int getMidPageNum();
  double getZoom();
  double getZoomDPI(int page);
  int getRotate();
  DisplayMode getDisplayMode();
  virtual void setPaperColor(SplashColorPtr paperColor);
  virtual void setMatteColor(SplashColorPtr matteColor);
  virtual void setReverseVideo(GBool reverseVideo);
  GBool canGoBack() { return historyBLen > 1; }
  GBool canGoForward() { return historyFLen > 0; }
  int getScrollX();
  int getScrollY();
  int getWindowWidth();
  int getWindowHeight();
  virtual void setBusyCursor(GBool busy) = 0;
  LinkAction *findLink(int pg, double x, double y);
  Annot *findAnnot(int pg, double x, double y);
  int findAnnotIdx(int pg, double x, double y);
  AcroFormField *findFormField(int pg, double x, double y);
  int findFormFieldIdx(int pg, double x, double y);
  AcroFormField *getFormField(int idx);
  GBool overText(int pg, double x, double y);
  void forceRedraw();
  void setTileDoneCbk(void (*cbk)(void *data), void *data);

protected:

  //--- calls from PDFCore subclass

  // Set the window size (when the window is resized).
  void setWindowSize(int winWidthA, int winHeightA);

  // Get the current window bitmap.  If <wholeWindow> is true, the
  // full window is being redrawn -- this is used to end incremental
  // updates when the rasterization is done.
  SplashBitmap *getWindowBitmap(GBool wholeWindow);

  // Returns true if the last call to getWindowBitmap() returned a
  // finished bitmap; or false if the bitmap was still being
  // rasterized.
  GBool isBitmapFinished() { return bitmapFinished; }

  // This should be called periodically (typically every ~0.1 seconds)
  // to do incremental updates.  If an update is required, it will
  // trigger a call to invalidate().
  virtual void tick();

  //--- callbacks to PDFCore subclass

  // Subclasses can return true here to force PDFCore::finishUpdate()
  // to always invalidate the window.  This is necessary to avoid
  // flickering on some backends.
  virtual GBool alwaysInvalidateOnUpdate() { return gFalse; }

  // Invalidate the specified rectangle (in window coordinates).
  virtual void invalidate(int x, int y, int w, int h) = 0;

  // Update the scrollbars.
  virtual void updateScrollbars() = 0;

  // This returns true if the PDF file has changed on disk (if it can
  // be checked).
  virtual GBool checkForNewFile() { return gFalse; }

  // This is called just before a PDF file is loaded.
  virtual void preLoad() {}

  // This is called just after a PDF file is loaded.
  virtual void postLoad() {}

  // This is called just before deleting the PDFDoc.  The PDFCore
  // subclass must shut down any secondary threads that are using the
  // PDFDoc pointer.
  virtual void aboutToDeleteDoc() {}

  //--- internal

  int loadFile2(PDFDoc *newDoc);
  void addToHistory();
  void clearPage();
  void loadLinks(int pg);
  void loadText(int pg);
  void getSelectionBBox(int *wxMin, int *wyMin, int *wxMax, int *wyMax);
  void getSelectRectListBBox(GList *rects, int *wxMin, int *wyMin,
			     int *wxMax, int *wyMax);
  void checkInvalidate(int x, int y, int w, int h);
  void invalidateWholeWindow();

  friend class AsyncFindAll;

  PDFDoc *doc;

  int linksPage;		// cached links for one page
  Links *links;

  int textPage;			// cached extracted text for one page
  double textDPI;
  int textRotate;
  TextOutputControl textOutCtrl;
  TextPage *text;

  DisplayState *state;
  TileMap *tileMap;
  TileCache *tileCache;
  TileCompositor *tileCompositor;
  GBool bitmapFinished;

  SelectMode selectMode;
  int selectPage;	 	// page of current selection
  int selectStartX,		// for block mode: start point of current
      selectStartY;		//   selection, in device coords
  TextPosition selectStartPos;	// for linear mode: start position of
				//   current selection

  PDFHistory			// page history queue
    history[pdfHistorySize];
  int historyCur;               // currently displayed page
  int historyBLen;              // number of valid entries backward from
                                //   current entry
  int historyFLen;              // number of valid entries forward from
                                //   current entry
};

#endif