Coverage Report

Created: 2025-11-11 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.05/xpdf/PDFCore.h
Line
Count
Source
1
//========================================================================
2
//
3
// PDFCore.h
4
//
5
// Copyright 2004-2014 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#ifndef PDFCORE_H
10
#define PDFCORE_H
11
12
#include <aconf.h>
13
14
#include <stdlib.h>
15
#include <atomic>
16
#include "SplashTypes.h"
17
#include "CharTypes.h"
18
#include "DisplayState.h"
19
#include "TextOutputDev.h"
20
21
class GString;
22
class GList;
23
class SplashBitmap;
24
class SplashPattern;
25
class BaseStream;
26
class PDFDoc;
27
class Links;
28
class LinkDest;
29
class LinkAction;
30
class Annot;
31
class AcroFormField;
32
class TextPage;
33
class HighlightFile;
34
class OptionalContentGroup;
35
class TileMap;
36
class TileCache;
37
class TileCompositor;
38
class PDFCore;
39
40
//------------------------------------------------------------------------
41
// PDFHistory
42
//------------------------------------------------------------------------
43
44
struct PDFHistory {
45
#ifdef _WIN32
46
  wchar_t *fileName;
47
#else
48
  GString *fileName;
49
#endif
50
  int page;
51
};
52
53
#define pdfHistorySize 50
54
55
//------------------------------------------------------------------------
56
// SelectMode
57
//------------------------------------------------------------------------
58
59
enum SelectMode {
60
  selectModeBlock,
61
  selectModeLinear
62
};
63
64
//------------------------------------------------------------------------
65
// FindResult
66
//------------------------------------------------------------------------
67
68
struct FindResult {
69
  FindResult(int pageA, double xMinA, double yMinA, double xMaxA, double yMaxA)
70
0
    : page(pageA), xMin(xMinA), yMin(yMinA), xMax(xMaxA), yMax(yMaxA) {}
71
  int page;
72
  double xMin, yMin, xMax, yMax;
73
};
74
75
//------------------------------------------------------------------------
76
// AsyncFindAll
77
//------------------------------------------------------------------------
78
79
class AsyncFindAll {
80
public:
81
82
0
  AsyncFindAll(PDFCore *coreA): core(coreA), canceled(gFalse) {}
83
84
0
  void reset() { canceled = false; }
85
86
  // Run the search, returning a list of FindResults -- same as
87
  // PDFCore::findAll().  This can be run on a separate thread.  If
88
  // cancel() is called while the search is running, this function
89
  // returns null.
90
  GList *run(PDFDoc *doc, Unicode *u, int len, GBool caseSensitive,
91
       GBool wholeWord, int firstPage, int lastPage);
92
93
  // Cancel a running search, causing run() to return null.
94
0
  void cancel() { canceled = true; }
95
96
private:
97
98
  PDFCore *core;
99
  std::atomic<bool> canceled;
100
};
101
102
//------------------------------------------------------------------------
103
// PDFCore
104
//------------------------------------------------------------------------
105
106
class PDFCore {
107
public:
108
109
  PDFCore(SplashColorMode colorMode, int bitmapRowPad,
110
    GBool reverseVideo, SplashColorPtr paperColor);
111
  virtual ~PDFCore();
112
113
  //----- loadFile / displayPage / displayDest
114
115
  // Load a new file.  Returns pdfOk or error code.
116
  virtual int loadFile(GString *fileName, GString *ownerPassword = NULL,
117
           GString *userPassword = NULL);
118
119
#ifdef _WIN32
120
  // Load a new file.  Returns pdfOk or error code.
121
  virtual int loadFile(wchar_t *fileName, int fileNameLen,
122
           GString *ownerPassword = NULL,
123
           GString *userPassword = NULL);
124
#endif
125
126
  // Load a new file, via a Stream instead of a file name.  Returns
127
  // pdfOk or error code.
128
  virtual int loadFile(BaseStream *stream, GString *ownerPassword = NULL,
129
           GString *userPassword = NULL);
130
131
  // Load an already-created PDFDoc object.
132
  virtual void loadDoc(PDFDoc *docA);
133
134
  // Reload the current file.  This only works if the PDF was loaded
135
  // via a file.  Returns pdfOk or error code.
136
  virtual int reload();
137
138
  // Clear out the current document, if any.
139
  virtual void clear();
140
141
  // Same as clear(), but returns the PDFDoc object instead of
142
  // deleting it.
143
  virtual PDFDoc *takeDoc(GBool redraw);
144
145
  // Display (or redisplay) the specified page.  If <scrollToTop> is
146
  // set, the window is vertically scrolled to the top; if
147
  // <scrollToBottom> is set, the window is vertically scrolled to the
148
  // bottom; otherwise, no scrolling is done.  If <addToHist> is set,
149
  // this page change is added to the history list.
150
  virtual void displayPage(int page, GBool scrollToTop,
151
         GBool scrollToBottom, GBool addToHist = gTrue);
152
153
  // Display a link destination.
154
  virtual void displayDest(LinkDest *dest);
155
156
  // Called before any update is started.
157
  virtual void startUpdate();
158
159
  // Called after any update is complete.  Subclasses can check for
160
  // changes in the display parameters here.
161
  virtual void finishUpdate(GBool addToHist, GBool checkForChangedFile);
162
163
  //----- page/position changes
164
165
  virtual GBool gotoNextPage(int inc, GBool top);
166
  virtual GBool gotoPrevPage(int dec, GBool top, GBool bottom);
167
  virtual GBool gotoNamedDestination(GString *dest);
168
  virtual GBool goForward();
169
  virtual GBool goBackward();
170
  virtual void scrollLeft(int nCols = 16);
171
  virtual void scrollRight(int nCols = 16);
172
  virtual void scrollUp(int nLines = 16, GBool snapToPage = gFalse);
173
  virtual void scrollUpPrevPage(int nLines = 16);
174
  virtual void scrollDown(int nLines = 16, GBool snapToPage = gFalse);
175
  virtual void scrollDownNextPage(int nLines = 16);
176
  virtual void scrollPageUp();
177
  virtual void scrollPageDown();
178
  virtual void scrollTo(int x, int y, GBool snapToPage = gFalse);
179
  virtual void scrollToLeftEdge();
180
  virtual void scrollToRightEdge();
181
  virtual void scrollToTopEdge();
182
  virtual void scrollToBottomEdge();
183
  virtual void scrollToTopLeft();
184
  virtual void scrollToBottomRight();
185
  // Scroll so that (page, x, y) is centered in the window.
186
  virtual void scrollToCentered(int page, double x, double y);
187
  virtual void setZoom(double zoom);
188
  virtual void zoomToRect(int page, double ulx, double uly,
189
        double lrx, double lry);
190
  virtual void zoomCentered(double zoom);
191
  virtual void zoomToCurrentWidth();
192
  virtual void setRotate(int rotate);
193
  virtual void setDisplayMode(DisplayMode mode);
194
  virtual void setOCGState(OptionalContentGroup *ocg, GBool ocgState);
195
196
  //----- selection
197
198
  // Selection mode.
199
0
  SelectMode getSelectMode() { return selectMode; }
200
  void setSelectMode(SelectMode mode);
201
202
  // Selection color.
203
  SplashColorPtr getSelectionColor();
204
  void setSelectionColor(SplashColor color);
205
206
  // Modify the selection.  These functions use device coordinates.
207
  void setSelection(int page, int x0, int y0, int x1, int y1);
208
  void setLinearSelection(int page, TextPosition *pos0, TextPosition *pos1);
209
  void clearSelection();
210
  void startSelectionDrag(int pg, int x, int y);
211
  void moveSelectionDrag(int pg, int x, int y);
212
  void finishSelectionDrag();
213
  void selectWord(int pg, int x, int y);
214
  void selectLine(int pg, int x, int y);
215
216
  // Retrieve the current selection.  This function uses user
217
  // coordinates.  Returns false if there is no selection.
218
  GBool getSelection(int *pg, double *ulx, double *uly,
219
         double *lrx, double *lry);
220
  GBool hasSelection();
221
222
  // Text extraction.
223
  void setTextExtractionMode(TextOutputMode mode);
224
  GBool getDiscardDiagonalText();
225
  void setDiscardDiagonalText(GBool discard);
226
  GString *extractText(int pg, double xMin, double yMin,
227
           double xMax, double yMax);
228
  GString *getSelectedText();
229
230
  //----- find
231
232
  virtual GBool find(char *s, GBool caseSensitive, GBool next, GBool backward,
233
         GBool wholeWord, GBool onePageOnly);
234
  virtual GBool findU(Unicode *u, int len, GBool caseSensitive,
235
          GBool next, GBool backward, GBool wholeWord,
236
          GBool onePageOnly);
237
  GList *findAll(Unicode *u, int len, GBool caseSensitive,
238
     GBool wholeWord, int firstPage, int lastPage);
239
240
241
  //----- coordinate conversion
242
243
  // user space: per-page, as defined by PDF file; unit = point
244
  // device space: (0,0) is upper-left corner of a page; unit = pixel
245
  // window space: (0,0) is upper-left corner of drawing area; unit = pixel
246
247
  GBool cvtWindowToUser(int xw, int yw, int *pg, double *xu, double *yu);
248
  GBool cvtWindowToDev(int xw, int yw, int *pg, int *xd, int *yd);
249
  GBool cvtUserToWindow(int pg, double xy, double yu, int *xw, int *yw);
250
  void cvtUserToDev(int pg, double xu, double yu, int *xd, int *yd);
251
  GBool cvtDevToWindow(int pg, int xd, int yd, int *xw, int *yw);
252
  void cvtDevToUser(int pg, int xd, int yd, double *xu, double *yu);
253
  void getWindowPageRange(int x, int y, int w, int h,
254
        int *firstPage, int *lastPage);
255
256
  //----- password dialog
257
258
0
  virtual GString *getPassword() { return NULL; }
259
260
  //----- misc access
261
262
0
  PDFDoc *getDoc() { return doc; }
263
  int getPageNum();
264
  int getMidPageNum();
265
  double getZoom();
266
  double getZoomDPI(int page);
267
  int getRotate();
268
  DisplayMode getDisplayMode();
269
  virtual void setPaperColor(SplashColorPtr paperColor);
270
  virtual void setMatteColor(SplashColorPtr matteColor);
271
  virtual void setReverseVideo(GBool reverseVideo);
272
0
  GBool canGoBack() { return historyBLen > 1; }
273
0
  GBool canGoForward() { return historyFLen > 0; }
274
  int getScrollX();
275
  int getScrollY();
276
  int getWindowWidth();
277
  int getWindowHeight();
278
  virtual void setBusyCursor(GBool busy) = 0;
279
  LinkAction *findLink(int pg, double x, double y);
280
  Annot *findAnnot(int pg, double x, double y);
281
  int findAnnotIdx(int pg, double x, double y);
282
  AcroFormField *findFormField(int pg, double x, double y);
283
  int findFormFieldIdx(int pg, double x, double y);
284
  AcroFormField *getFormField(int idx);
285
  GBool overText(int pg, double x, double y);
286
  void forceRedraw();
287
  void setTileDoneCbk(void (*cbk)(void *data), void *data);
288
289
protected:
290
291
  //--- calls from PDFCore subclass
292
293
  // Set the window size (when the window is resized).
294
  void setWindowSize(int winWidthA, int winHeightA);
295
296
  // Get the current window bitmap.  If <wholeWindow> is true, the
297
  // full window is being redrawn -- this is used to end incremental
298
  // updates when the rasterization is done.
299
  SplashBitmap *getWindowBitmap(GBool wholeWindow);
300
301
  // Returns true if the last call to getWindowBitmap() returned a
302
  // finished bitmap; or false if the bitmap was still being
303
  // rasterized.
304
0
  GBool isBitmapFinished() { return bitmapFinished; }
305
306
  // This should be called periodically (typically every ~0.1 seconds)
307
  // to do incremental updates.  If an update is required, it will
308
  // trigger a call to invalidate().
309
  virtual void tick();
310
311
  //--- callbacks to PDFCore subclass
312
313
  // Subclasses can return true here to force PDFCore::finishUpdate()
314
  // to always invalidate the window.  This is necessary to avoid
315
  // flickering on some backends.
316
0
  virtual GBool alwaysInvalidateOnUpdate() { return gFalse; }
317
318
  // Invalidate the specified rectangle (in window coordinates).
319
  virtual void invalidate(int x, int y, int w, int h) = 0;
320
321
  // Update the scrollbars.
322
  virtual void updateScrollbars() = 0;
323
324
  // This returns true if the PDF file has changed on disk (if it can
325
  // be checked).
326
0
  virtual GBool checkForNewFile() { return gFalse; }
327
328
  // This is called just before a PDF file is loaded.
329
0
  virtual void preLoad() {}
330
331
  // This is called just after a PDF file is loaded.
332
0
  virtual void postLoad() {}
333
334
  // This is called just before deleting the PDFDoc.  The PDFCore
335
  // subclass must shut down any secondary threads that are using the
336
  // PDFDoc pointer.
337
0
  virtual void aboutToDeleteDoc() {}
338
339
  //--- internal
340
341
  int loadFile2(PDFDoc *newDoc);
342
  void addToHistory();
343
  void clearPage();
344
  void loadLinks(int pg);
345
  void loadText(int pg);
346
  void getSelectionBBox(int *wxMin, int *wyMin, int *wxMax, int *wyMax);
347
  void getSelectRectListBBox(GList *rects, int *wxMin, int *wyMin,
348
           int *wxMax, int *wyMax);
349
  void checkInvalidate(int x, int y, int w, int h);
350
  void invalidateWholeWindow();
351
352
  friend class AsyncFindAll;
353
354
  PDFDoc *doc;
355
356
  int linksPage;    // cached links for one page
357
  Links *links;
358
359
  int textPage;     // cached extracted text for one page
360
  double textDPI;
361
  int textRotate;
362
  TextOutputControl textOutCtrl;
363
  TextPage *text;
364
365
  DisplayState *state;
366
  TileMap *tileMap;
367
  TileCache *tileCache;
368
  TileCompositor *tileCompositor;
369
  GBool bitmapFinished;
370
371
  SelectMode selectMode;
372
  int selectPage;   // page of current selection
373
  int selectStartX,   // for block mode: start point of current
374
      selectStartY;   //   selection, in device coords
375
  TextPosition selectStartPos;  // for linear mode: start position of
376
        //   current selection
377
378
  PDFHistory      // page history queue
379
    history[pdfHistorySize];
380
  int historyCur;               // currently displayed page
381
  int historyBLen;              // number of valid entries backward from
382
                                //   current entry
383
  int historyFLen;              // number of valid entries forward from
384
                                //   current entry
385
};
386
387
#endif