2020-01-03 18:11:03 +01:00
|
|
|
// Copyright (C) 2019-2020 Jakub Melka
|
2019-12-28 19:21:29 +01:00
|
|
|
//
|
2020-12-20 19:03:58 +01:00
|
|
|
// This file is part of Pdf4Qt.
|
2019-12-28 19:21:29 +01:00
|
|
|
//
|
2020-12-20 19:03:58 +01:00
|
|
|
// Pdf4Qt is free software: you can redistribute it and/or modify
|
2019-12-28 19:21:29 +01:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-20 19:03:58 +01:00
|
|
|
// Pdf4Qt is distributed in the hope that it will be useful,
|
2019-12-28 19:21:29 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2020-12-20 19:03:58 +01:00
|
|
|
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-28 19:21:29 +01:00
|
|
|
|
|
|
|
#ifndef PDFTEXTLAYOUT_H
|
|
|
|
#define PDFTEXTLAYOUT_H
|
|
|
|
|
|
|
|
#include "pdfglobal.h"
|
2020-01-08 19:02:29 +01:00
|
|
|
#include "pdfutils.h"
|
2019-12-28 19:21:29 +01:00
|
|
|
|
2020-01-05 18:13:43 +01:00
|
|
|
#include <QColor>
|
2019-12-31 17:39:31 +01:00
|
|
|
#include <QDataStream>
|
2019-12-28 19:21:29 +01:00
|
|
|
#include <QPainterPath>
|
|
|
|
|
|
|
|
#include <set>
|
2020-01-03 18:11:03 +01:00
|
|
|
#include <compare>
|
2019-12-28 19:21:29 +01:00
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
2020-01-03 18:11:03 +01:00
|
|
|
class PDFTextLayout;
|
2020-01-08 19:02:29 +01:00
|
|
|
class PDFTextLayoutStorage;
|
2019-12-28 19:21:29 +01:00
|
|
|
|
|
|
|
struct PDFTextCharacterInfo
|
|
|
|
{
|
|
|
|
/// Character
|
|
|
|
QChar character;
|
|
|
|
|
|
|
|
/// Character path
|
|
|
|
QPainterPath outline;
|
|
|
|
|
|
|
|
/// Do we use a vertical writing system?
|
|
|
|
bool isVerticalWritingSystem = false;
|
|
|
|
|
|
|
|
/// Advance (in character space, it must be translated
|
|
|
|
/// into device space), for both vertical/horizontal modes.
|
|
|
|
PDFReal advance = 0.0;
|
|
|
|
|
|
|
|
/// Font size (in character space, it must be translated
|
|
|
|
/// into device space)
|
|
|
|
PDFReal fontSize = 0.0;
|
|
|
|
|
|
|
|
/// Transformation matrix from character space to device space
|
|
|
|
QMatrix matrix;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PDFTextLayoutSettings
|
|
|
|
{
|
|
|
|
/// Number of samples for 'docstrum' algorithm, i.e. number of
|
|
|
|
/// nearest characters. By default, 5 characters should fit.
|
|
|
|
size_t samples = 5;
|
|
|
|
|
|
|
|
/// Distance sensitivity to determine, if characters are close enough.
|
|
|
|
/// Maximal distance is computed as current character advance multiplied
|
|
|
|
/// by this constant.
|
|
|
|
PDFReal distanceSensitivity = 4.0;
|
|
|
|
|
|
|
|
/// Maximal vertical distance, in portion of font size, of two characters
|
|
|
|
/// to be considered they lie on same line.
|
|
|
|
PDFReal charactersOnLineSensitivity = 0.25;
|
|
|
|
|
|
|
|
/// Maximal ratio between font size of characters to be considered
|
|
|
|
/// that they lie on same line.
|
|
|
|
PDFReal fontSensitivity = 2.0;
|
|
|
|
|
|
|
|
/// Maximal space ratio between two lines of block. Default coefficient
|
|
|
|
/// means, that height ratio limit is (height1 + height2)
|
|
|
|
PDFReal blockVerticalSensitivity = 1.5;
|
|
|
|
|
|
|
|
/// Minimal horizontal overlap for two lines considered to be in one block
|
|
|
|
PDFReal blockOverlapSensitivity = 0.3;
|
2019-12-31 17:39:31 +01:00
|
|
|
|
|
|
|
friend QDataStream& operator<<(QDataStream& stream, const PDFTextLayoutSettings& settings);
|
|
|
|
friend QDataStream& operator>>(QDataStream& stream, PDFTextLayoutSettings& settings);
|
2019-12-28 19:21:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Represents character in device space coordinates. All values (dimensions,
|
|
|
|
/// bounding box, etc. are in device space coordinates).
|
|
|
|
struct TextCharacter
|
|
|
|
{
|
|
|
|
QChar character;
|
|
|
|
QPointF position;
|
|
|
|
PDFReal angle = 0.0;
|
|
|
|
PDFReal fontSize = 0.0;
|
|
|
|
PDFReal advance = 0.0;
|
|
|
|
QPainterPath boundingBox;
|
2019-12-29 13:50:00 +01:00
|
|
|
|
2020-01-17 19:29:21 +01:00
|
|
|
size_t index = 0; // Just temporary index, it is not serialized, just for text layout algorithm
|
|
|
|
|
2019-12-29 13:50:00 +01:00
|
|
|
void applyTransform(const QMatrix& matrix);
|
2019-12-31 17:39:31 +01:00
|
|
|
|
|
|
|
friend QDataStream& operator<<(QDataStream& stream, const TextCharacter& character);
|
|
|
|
friend QDataStream& operator>>(QDataStream& stream, TextCharacter& character);
|
2019-12-28 19:21:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using TextCharacters = std::vector<TextCharacter>;
|
|
|
|
|
|
|
|
/// Represents text line consisting of set of characters and line bounding box.
|
|
|
|
class PDFTextLine
|
|
|
|
{
|
|
|
|
public:
|
2019-12-31 17:39:31 +01:00
|
|
|
explicit inline PDFTextLine() = default;
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
/// Construct new line from characters. Characters are sorted in x-coordinate
|
|
|
|
/// and bounding box is computed.
|
|
|
|
/// \param characters
|
|
|
|
explicit PDFTextLine(TextCharacters characters);
|
|
|
|
|
|
|
|
const TextCharacters& getCharacters() const { return m_characters; }
|
|
|
|
const QPainterPath& getBoundingBox() const { return m_boundingBox; }
|
2019-12-29 17:25:18 +01:00
|
|
|
const QPointF& getTopLeft() const { return m_topLeft; }
|
2019-12-28 19:21:29 +01:00
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
/// Get angle inclination of block
|
|
|
|
PDFReal getAngle() const;
|
|
|
|
|
2019-12-29 13:50:00 +01:00
|
|
|
void applyTransform(const QMatrix& matrix);
|
|
|
|
|
2019-12-31 17:39:31 +01:00
|
|
|
friend QDataStream& operator<<(QDataStream& stream, const PDFTextLine& line);
|
|
|
|
friend QDataStream& operator>>(QDataStream& stream, PDFTextLine& line);
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
private:
|
|
|
|
TextCharacters m_characters;
|
|
|
|
QPainterPath m_boundingBox;
|
2019-12-29 17:25:18 +01:00
|
|
|
QPointF m_topLeft;
|
2019-12-28 19:21:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using PDFTextLines = std::vector<PDFTextLine>;
|
|
|
|
|
|
|
|
/// Represents text block consisting of set of lines and block bounding box.
|
|
|
|
class PDFTextBlock
|
|
|
|
{
|
|
|
|
public:
|
2019-12-31 17:39:31 +01:00
|
|
|
explicit inline PDFTextBlock() = default;
|
2019-12-28 19:21:29 +01:00
|
|
|
explicit inline PDFTextBlock(PDFTextLines textLines);
|
|
|
|
|
|
|
|
const PDFTextLines& getLines() const { return m_lines; }
|
|
|
|
const QPainterPath& getBoundingBox() const { return m_boundingBox; }
|
2019-12-29 17:25:18 +01:00
|
|
|
const QPointF& getTopLeft() const { return m_topLeft; }
|
2019-12-28 19:21:29 +01:00
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
/// Get angle inclination of block
|
|
|
|
PDFReal getAngle() const;
|
|
|
|
|
2019-12-29 13:50:00 +01:00
|
|
|
void applyTransform(const QMatrix& matrix);
|
|
|
|
|
2019-12-31 17:39:31 +01:00
|
|
|
friend QDataStream& operator<<(QDataStream& stream, const PDFTextBlock& block);
|
|
|
|
friend QDataStream& operator>>(QDataStream& stream, PDFTextBlock& block);
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
private:
|
|
|
|
PDFTextLines m_lines;
|
|
|
|
QPainterPath m_boundingBox;
|
2019-12-29 17:25:18 +01:00
|
|
|
QPointF m_topLeft;
|
2019-12-28 19:21:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using PDFTextBlocks = std::vector<PDFTextBlock>;
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
/// Character pointer points to some character in text layout.
|
|
|
|
/// It also has page index to decide, which page the pointer points to.
|
|
|
|
struct PDFCharacterPointer
|
|
|
|
{
|
|
|
|
auto operator<=>(const PDFCharacterPointer&) const = default;
|
|
|
|
|
|
|
|
/// Returns true, if character pointer is valid and points to the correct location
|
|
|
|
bool isValid() const { return pageIndex > -1; }
|
|
|
|
|
2020-01-04 17:58:55 +01:00
|
|
|
/// Returns true, if character belongs to same block
|
|
|
|
bool hasSameBlock(const PDFCharacterPointer& other) const;
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
/// Returns true, if character belongs to same line
|
|
|
|
bool hasSameLine(const PDFCharacterPointer& other) const;
|
|
|
|
|
2020-01-26 17:06:50 +01:00
|
|
|
PDFInteger pageIndex = -1;
|
2020-01-03 18:11:03 +01:00
|
|
|
size_t blockIndex = 0;
|
|
|
|
size_t lineIndex = 0;
|
|
|
|
size_t characterIndex = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
using PDFTextSelectionItem = std::pair<PDFCharacterPointer, PDFCharacterPointer>;
|
|
|
|
using PDFTextSelectionItems = std::vector<PDFTextSelectionItem>;
|
|
|
|
|
2020-01-05 18:13:43 +01:00
|
|
|
struct PDFTextSelectionColoredItem
|
|
|
|
{
|
|
|
|
explicit inline PDFTextSelectionColoredItem() = default;
|
|
|
|
explicit inline PDFTextSelectionColoredItem(PDFCharacterPointer start, PDFCharacterPointer end, QColor color) :
|
|
|
|
start(start),
|
|
|
|
end(end),
|
|
|
|
color(color)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-25 17:36:25 +01:00
|
|
|
bool operator==(const PDFTextSelectionColoredItem&) const = default;
|
|
|
|
bool operator!=(const PDFTextSelectionColoredItem&) const = default;
|
|
|
|
|
2020-01-05 18:13:43 +01:00
|
|
|
inline bool operator<(const PDFTextSelectionColoredItem& other) const { return std::tie(start, end) < std::tie(other.start, other.end); }
|
|
|
|
|
|
|
|
PDFCharacterPointer start;
|
|
|
|
PDFCharacterPointer end;
|
|
|
|
QColor color;
|
|
|
|
};
|
|
|
|
using PDFTextSelectionColoredItems = std::vector<PDFTextSelectionColoredItem>;
|
|
|
|
|
|
|
|
/// Text selection, can be used across multiple pages. Also defines color
|
|
|
|
/// for each text selection.
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextSelection
|
2020-01-03 18:11:03 +01:00
|
|
|
{
|
|
|
|
public:
|
2020-01-05 18:13:43 +01:00
|
|
|
explicit PDFTextSelection() = default;
|
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
using iterator = PDFTextSelectionColoredItems::const_iterator;
|
|
|
|
|
2020-01-25 17:36:25 +01:00
|
|
|
bool operator==(const PDFTextSelection&) const = default;
|
|
|
|
bool operator!=(const PDFTextSelection&) const = default;
|
|
|
|
|
2020-01-05 18:13:43 +01:00
|
|
|
/// Adds text selection items to selection
|
|
|
|
/// \param items Items
|
|
|
|
/// \param color Color for items (must include alpha channel)
|
|
|
|
void addItems(const PDFTextSelectionItems& items, QColor color);
|
|
|
|
|
|
|
|
/// Builds text selection, so it is prepared for rendering. Text selection,
|
|
|
|
/// which is not build, can't be used for rendering.
|
|
|
|
void build();
|
2020-01-03 18:11:03 +01:00
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
/// Returns iterator to start of page range
|
|
|
|
iterator begin(PDFInteger pageIndex) const;
|
|
|
|
|
|
|
|
/// Returns iterator to end of page range
|
|
|
|
iterator end(PDFInteger pageIndex) const;
|
|
|
|
|
2020-01-26 17:48:38 +01:00
|
|
|
/// Returns iterator to next page range
|
|
|
|
iterator nextPageRange(iterator currentPageRange) const;
|
|
|
|
|
2020-01-25 17:36:25 +01:00
|
|
|
/// Returns true, if text selection is empty
|
|
|
|
bool isEmpty() const { return m_items.empty(); }
|
|
|
|
|
2020-01-26 17:48:38 +01:00
|
|
|
iterator begin() const { return m_items.cbegin(); }
|
|
|
|
iterator end() const { return m_items.cend(); }
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
private:
|
2020-01-05 18:13:43 +01:00
|
|
|
PDFTextSelectionColoredItems m_items;
|
2020-01-03 18:11:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PDFFindResult
|
|
|
|
{
|
2020-01-04 17:58:55 +01:00
|
|
|
bool operator<(const PDFFindResult& other) const;
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
/// Matched string during search
|
|
|
|
QString matched;
|
|
|
|
|
|
|
|
/// Context (text before and after match)
|
|
|
|
QString context;
|
|
|
|
|
|
|
|
/// Matched selection (can be multiple items, if selection
|
|
|
|
/// is spanned between multiple blocks)
|
|
|
|
PDFTextSelectionItems textSelectionItems;
|
|
|
|
};
|
|
|
|
using PDFFindResults = std::vector<PDFFindResult>;
|
|
|
|
|
|
|
|
class PDFTextFlow;
|
|
|
|
using PDFTextFlows = std::vector<PDFTextFlow>;
|
|
|
|
|
|
|
|
/// This class represents a portion of continuous text on the page. It can
|
|
|
|
/// consists of multiple blocks (which follow reading order).
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextFlow
|
2020-01-03 18:11:03 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum FlowFlag
|
|
|
|
{
|
|
|
|
None = 0x0000,
|
|
|
|
SeparateBlocks = 0x0001, ///< Create flow for each block
|
|
|
|
RemoveSoftHyphen = 0x0002, ///< Removes 'soft hyphen' unicode character from end-of-line (character 0x00AD)
|
|
|
|
AddLineBreaks = 0x0004, ///< Add line break characters to the end of line
|
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(FlowFlags, FlowFlag)
|
|
|
|
|
|
|
|
/// Finds simple text in current text flow. All text occurences are returned.
|
|
|
|
/// \param text Text to be found
|
|
|
|
/// \param caseSensitivity Case sensitivity
|
|
|
|
PDFFindResults find(const QString& text, Qt::CaseSensitivity caseSensitivity) const;
|
|
|
|
|
2020-01-04 17:58:55 +01:00
|
|
|
/// Finds regular expression matches in current text flow. All text occurences are returned.
|
|
|
|
/// \param expression Regular expression to be matched
|
|
|
|
PDFFindResults find(const QRegularExpression& expression) const;
|
|
|
|
|
2020-02-18 20:21:18 +01:00
|
|
|
/// Returns whole text for this text flow
|
|
|
|
QString getText() const { return m_text; }
|
|
|
|
|
2020-01-26 17:48:38 +01:00
|
|
|
/// Returns text form character pointers
|
|
|
|
/// \param begin Begin character
|
|
|
|
/// \param end End character
|
|
|
|
QString getText(const PDFCharacterPointer& begin, const PDFCharacterPointer& end) const;
|
|
|
|
|
2020-01-03 18:11:03 +01:00
|
|
|
/// Merge data from \p next flow (i.e. connect two consecutive flows)
|
|
|
|
void merge(const PDFTextFlow& next);
|
|
|
|
|
|
|
|
/// Creates text flows from text layout, according to creation flags.
|
|
|
|
/// \param layout Layout, from which is text flow created
|
|
|
|
/// \param flags Flow creation flags
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
static PDFTextFlows createTextFlows(const PDFTextLayout& layout, FlowFlags flags, PDFInteger pageIndex);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Returns text selection from index and length. Returned text selection can also
|
|
|
|
/// be empty (for example, if only single space character is selected, which has
|
|
|
|
/// no counterpart in real text)
|
|
|
|
/// \param index Index of text selection subrange
|
|
|
|
/// \param length Length of text selection
|
|
|
|
PDFTextSelectionItems getTextSelectionItems(size_t index, size_t length) const;
|
|
|
|
|
|
|
|
/// Returns context for text selection (or empty string, if text selection is empty)
|
|
|
|
/// \param index Index of text selection subrange
|
|
|
|
/// \param length Length of text selection
|
|
|
|
QString getContext(size_t index, size_t length) const;
|
|
|
|
|
|
|
|
QString m_text;
|
|
|
|
std::vector<PDFCharacterPointer> m_characterPointers;
|
|
|
|
};
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
/// Text layout of single page. Can handle various fonts, various angles of lines
|
|
|
|
/// and vertically oriented text. It performs the "docstrum" algorithm.
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextLayout
|
2019-12-28 19:21:29 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit PDFTextLayout();
|
|
|
|
|
|
|
|
/// Adds character to the layout
|
|
|
|
void addCharacter(const PDFTextCharacterInfo& info);
|
|
|
|
|
2020-01-26 17:06:50 +01:00
|
|
|
/// Performs text layout algorithm
|
2019-12-28 19:21:29 +01:00
|
|
|
void perform();
|
|
|
|
|
|
|
|
/// Optimizes layout memory allocation to contain less space
|
|
|
|
void optimize();
|
|
|
|
|
|
|
|
/// Returns estimate of number of bytes, which this mesh occupies in memory
|
|
|
|
qint64 getMemoryConsumptionEstimate() const;
|
|
|
|
|
2019-12-29 17:25:18 +01:00
|
|
|
/// Returns recognized text blocks
|
|
|
|
const PDFTextBlocks& getTextBlocks() const { return m_blocks; }
|
|
|
|
|
2020-01-25 17:36:25 +01:00
|
|
|
/// Returns true, if given point is pointing to some text block
|
|
|
|
bool isHoveringOverTextBlock(const QPointF& point) const;
|
|
|
|
|
2020-01-26 17:06:50 +01:00
|
|
|
/// Creates text selection. This function needs to modify the layout contents,
|
|
|
|
/// so do not use this function from multiple threads (it is not thread-safe).
|
|
|
|
/// Text selection is created from rectangle using two points.
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
/// \param point1 First point
|
|
|
|
/// \param point2 Second point
|
|
|
|
PDFTextSelection createTextSelection(PDFInteger pageIndex, const QPointF& point1, const QPointF& point2);
|
|
|
|
|
2020-01-26 17:48:38 +01:00
|
|
|
/// Returns string from text selection
|
|
|
|
/// \param itBegin Iterator (begin range)
|
|
|
|
/// \param itEnd Iterator (end range)
|
|
|
|
/// \param pageIndex Index of the page
|
|
|
|
QString getTextFromSelection(PDFTextSelection::iterator itBegin, PDFTextSelection::iterator itEnd, PDFInteger pageIndex) const;
|
|
|
|
|
|
|
|
/// Returns string from text selection
|
|
|
|
/// \param selection Text selection
|
|
|
|
/// \param pageIndex Index of the page
|
|
|
|
QString getTextFromSelection(const PDFTextSelection& selection, PDFInteger pageIndex) const;
|
|
|
|
|
2019-12-31 17:39:31 +01:00
|
|
|
friend QDataStream& operator<<(QDataStream& stream, const PDFTextLayout& layout);
|
|
|
|
friend QDataStream& operator>>(QDataStream& stream, PDFTextLayout& layout);
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
private:
|
|
|
|
/// Makes layout for particular angle
|
|
|
|
void performDoLayout(PDFReal angle);
|
|
|
|
|
|
|
|
/// Returns a list of characters for particular angle. Exact match is used
|
|
|
|
/// for angle, even if angle is floating point number.
|
|
|
|
/// \param angle Angle
|
|
|
|
TextCharacters getCharactersForAngle(PDFReal angle) const;
|
|
|
|
|
|
|
|
/// Applies transform to text characters (positions and bounding boxes)
|
|
|
|
/// \param characters Characters
|
|
|
|
/// \param matrix Transform matrix
|
2020-01-26 17:06:50 +01:00
|
|
|
static void applyTransform(TextCharacters& characters, const QMatrix& matrix);
|
2019-12-28 19:21:29 +01:00
|
|
|
|
|
|
|
TextCharacters m_characters;
|
|
|
|
std::set<PDFReal> m_angles;
|
|
|
|
PDFTextLayoutSettings m_settings;
|
2019-12-29 13:50:00 +01:00
|
|
|
PDFTextBlocks m_blocks;
|
2019-12-28 19:21:29 +01:00
|
|
|
};
|
|
|
|
|
2020-12-11 18:59:39 +01:00
|
|
|
/// Cache for storing single text layout
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextLayoutCache
|
2020-12-11 18:59:39 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit PDFTextLayoutCache(std::function<PDFTextLayout(PDFInteger)> textLayoutGetter);
|
|
|
|
|
|
|
|
/// Clears the cache
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/// Returns text layout. This function always succeeds. If compiler is not active,
|
|
|
|
/// then empty layout is returned.
|
|
|
|
/// \param compiler Text layout compiler
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
const PDFTextLayout& getTextLayout(PDFInteger pageIndex);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<PDFTextLayout(PDFInteger)> m_textLayoutGetter;
|
|
|
|
PDFInteger m_pageIndex;
|
|
|
|
PDFTextLayout m_layout;
|
|
|
|
};
|
|
|
|
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextLayoutGetter
|
2020-12-11 18:59:39 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit inline PDFTextLayoutGetter(PDFTextLayoutCache* cache, PDFInteger pageIndex) :
|
|
|
|
m_cache(cache),
|
|
|
|
m_pageIndex(pageIndex)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast operator, casts to constant reference to PDFTextLayout
|
|
|
|
operator const PDFTextLayout&()
|
|
|
|
{
|
|
|
|
return m_cache->getTextLayout(m_pageIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PDFTextLayoutCache* m_cache;
|
|
|
|
PDFInteger m_pageIndex;
|
|
|
|
};
|
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
/// Lazy getter for text layouts from storage. This is used, when we do not want to
|
|
|
|
/// get text layout each time, because it is time expensive. If text layout is not needed,
|
|
|
|
/// then nothing happens. Text layout is returned only, if conversion operator is used.
|
2020-12-11 18:59:39 +01:00
|
|
|
class PDFTextLayoutStorageGetter
|
2020-01-08 19:02:29 +01:00
|
|
|
{
|
|
|
|
public:
|
2020-12-11 18:59:39 +01:00
|
|
|
explicit PDFTextLayoutStorageGetter(const PDFTextLayoutStorage* storage, PDFInteger pageIndex) :
|
2020-01-08 19:02:29 +01:00
|
|
|
m_storage(storage),
|
|
|
|
m_pageIndex(pageIndex)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cast operator, casts to constant reference to PDFTextLayout
|
|
|
|
operator const PDFTextLayout&()
|
|
|
|
{
|
2020-12-11 18:59:39 +01:00
|
|
|
return m_textLayout.get(this, &PDFTextLayoutStorageGetter::getTextLayoutImpl);
|
2020-01-08 19:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PDFTextLayout getTextLayoutImpl() const;
|
|
|
|
|
|
|
|
const PDFTextLayoutStorage* m_storage;
|
|
|
|
PDFInteger m_pageIndex;
|
|
|
|
PDFCachedItem<PDFTextLayout> m_textLayout;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Paints text selection on various pages using page to device point matrix
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextSelectionPainter
|
2020-01-08 19:02:29 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit inline PDFTextSelectionPainter(const PDFTextSelection* selection) :
|
|
|
|
m_selection(selection)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draws text selection on the painter, using text layout and matrix. If current text selection
|
|
|
|
/// doesn't contain items from active page, then text layout is not accessed.
|
|
|
|
/// \param painter Painter
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
/// \param textLayoutGetter Text layout getter
|
|
|
|
/// \param matrix Matrix which translates from page space to device space
|
|
|
|
void draw(QPainter* painter, PDFInteger pageIndex, PDFTextLayoutGetter& textLayoutGetter, const QMatrix& matrix);
|
|
|
|
|
2020-12-12 18:08:37 +01:00
|
|
|
/// Prepares geometry for text selection drawing, using text layout and matrix. If current text selection
|
|
|
|
/// doesn't contain items from active page, then text layout is not accessed.
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
/// \param textLayoutGetter Text layout getter
|
|
|
|
/// \param matrix Matrix which translates from page space to device space
|
|
|
|
QPainterPath prepareGeometry(PDFInteger pageIndex, PDFTextLayoutGetter& textLayoutGetter, const QMatrix& matrix, QPolygonF* quadrilaterals);
|
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
private:
|
2020-01-11 16:14:38 +01:00
|
|
|
static constexpr const PDFReal HEIGHT_INCREASE_FACTOR = 0.40;
|
2020-01-08 19:02:29 +01:00
|
|
|
static constexpr const PDFReal SELECTION_ALPHA = 0.25;
|
|
|
|
|
|
|
|
const PDFTextSelection* m_selection;
|
|
|
|
};
|
|
|
|
|
2019-12-31 17:39:31 +01:00
|
|
|
/// Storage for text layouts. For reading and writing, this object is thread safe.
|
|
|
|
/// For writing, mutex is used to synchronize asynchronous writes, for reading
|
|
|
|
/// no mutex is used at all. For this reason, both reading/writing at the same time
|
|
|
|
/// is prohibited, it is not thread safe.
|
2020-12-20 19:03:58 +01:00
|
|
|
class Pdf4QtLIBSHARED_EXPORT PDFTextLayoutStorage
|
2019-12-31 17:39:31 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit inline PDFTextLayoutStorage() = default;
|
|
|
|
explicit inline PDFTextLayoutStorage(PDFInteger pageCount) :
|
|
|
|
m_offsets(pageCount, 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns text layout for particular page. If page index is invalid,
|
|
|
|
/// then empty text layout is returned. Function is not thread safe, if
|
|
|
|
/// function \p setTextLayout is called from another thread.
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
PDFTextLayout getTextLayout(PDFInteger pageIndex) const;
|
|
|
|
|
2020-01-08 19:02:29 +01:00
|
|
|
/// Returns text layout for particular page. If page index is invalid,
|
|
|
|
/// then empty text layout is returned. Function is not thread safe, if
|
|
|
|
/// function \p setTextLayout is called from another thread.
|
|
|
|
/// \param pageIndex Page index
|
2020-12-11 18:59:39 +01:00
|
|
|
PDFTextLayoutStorageGetter getTextLayoutLazy(PDFInteger pageIndex) const { return PDFTextLayoutStorageGetter(this, pageIndex); }
|
2020-01-08 19:02:29 +01:00
|
|
|
|
2019-12-31 17:39:31 +01:00
|
|
|
/// Sets text layout to the particular index. Index must be valid and from
|
|
|
|
/// range 0 to \p pageCount - 1. Function is not thread safe.
|
|
|
|
/// \param pageIndex Page index
|
|
|
|
/// \param layout Text layout
|
|
|
|
/// \param mutex Mutex for locking (calls of setTextLayout from multiple threads)
|
|
|
|
void setTextLayout(PDFInteger pageIndex, const PDFTextLayout& layout, QMutex* mutex);
|
|
|
|
|
2020-01-04 17:58:55 +01:00
|
|
|
/// Finds simple text in all pages. All text occurences are returned.
|
|
|
|
/// \param text Text to be found
|
|
|
|
/// \param caseSensitivity Case sensitivity
|
|
|
|
/// \param flowFlags Text flow flags
|
|
|
|
PDFFindResults find(const QString& text, Qt::CaseSensitivity caseSensitivity, PDFTextFlow::FlowFlags flowFlags) const;
|
|
|
|
|
|
|
|
/// Finds regular expression matches in current text flow. All text occurences are returned.
|
|
|
|
/// \param expression Regular expression to be matched
|
|
|
|
/// \param flowFlags Text flow flags
|
|
|
|
PDFFindResults find(const QRegularExpression& expression, PDFTextFlow::FlowFlags flowFlags) const;
|
|
|
|
|
2020-01-26 17:06:50 +01:00
|
|
|
/// Returns number of pages
|
|
|
|
size_t getCount() const { return m_offsets.size(); }
|
|
|
|
|
2019-12-31 17:39:31 +01:00
|
|
|
private:
|
|
|
|
std::vector<int> m_offsets;
|
|
|
|
QByteArray m_textLayouts;
|
|
|
|
};
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
} // namespace pdf
|
|
|
|
|
2020-02-18 20:21:18 +01:00
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(pdf::PDFTextFlow::FlowFlags)
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
#endif // PDFTEXTLAYOUT_H
|