2020-01-18 11:38:54 +01:00
|
|
|
// Copyright (C) 2019-2020 Jakub Melka
|
2019-02-24 17:48:37 +01:00
|
|
|
//
|
|
|
|
// This file is part of PdfForQt.
|
|
|
|
//
|
|
|
|
// PdfForQt is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// PdfForQt is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#ifndef PDFUTILS_H
|
|
|
|
#define PDFUTILS_H
|
|
|
|
|
2019-05-10 19:48:52 +02:00
|
|
|
#include "pdfglobal.h"
|
|
|
|
|
2019-12-29 17:25:18 +01:00
|
|
|
#include <QRectF>
|
2020-01-28 19:17:45 +01:00
|
|
|
#include <QColor>
|
2019-05-10 19:48:52 +02:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QDataStream>
|
|
|
|
|
2020-06-27 17:28:03 +02:00
|
|
|
#include <set>
|
2019-08-13 11:45:36 +02:00
|
|
|
#include <vector>
|
2019-09-20 18:19:21 +02:00
|
|
|
#include <iterator>
|
2020-03-07 17:38:50 +01:00
|
|
|
#include <functional>
|
2019-08-13 11:45:36 +02:00
|
|
|
|
2019-02-24 17:48:37 +01:00
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Class for easy storing of cached item. This class is not thread safe,
|
|
|
|
/// and for this reason, access function are not constant (they can modify the
|
|
|
|
/// object).
|
|
|
|
template<typename T>
|
|
|
|
class PDFCachedItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit inline PDFCachedItem() :
|
|
|
|
m_dirty(true),
|
|
|
|
m_object()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cached object. If object is dirty, then cached object is refreshed.
|
|
|
|
/// \param holder Holder object, which owns the cached item
|
|
|
|
/// \param function Refresh function
|
|
|
|
template<typename H>
|
|
|
|
inline const T& get(const H* holder, T(H::* function)(void) const)
|
|
|
|
{
|
|
|
|
if (m_dirty)
|
|
|
|
{
|
|
|
|
m_object = (holder->*function)();
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
|
2019-10-02 19:37:19 +02:00
|
|
|
/// Returns the cached object. If object is dirty, then cached object is refreshed.
|
|
|
|
/// \param holder Holder object, which owns the cached item
|
|
|
|
/// \param function Refresh function
|
|
|
|
template<typename H>
|
|
|
|
inline const T& get(H* holder, T(H::* function)(void))
|
|
|
|
{
|
|
|
|
if (m_dirty)
|
|
|
|
{
|
|
|
|
m_object = (holder->*function)();
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
|
2020-03-07 17:38:50 +01:00
|
|
|
/// Returns the cached object. If object is dirty, then cached object is refreshed.
|
|
|
|
/// \param function Refresh function
|
|
|
|
inline const T& get(const std::function<T(void)>& function)
|
|
|
|
{
|
|
|
|
if (m_dirty)
|
|
|
|
{
|
|
|
|
m_object = function();
|
|
|
|
m_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
|
2019-02-24 17:48:37 +01:00
|
|
|
/// Invalidates the cached item, so it must be refreshed from the cache next time,
|
|
|
|
/// if it is accessed.
|
|
|
|
inline void dirty()
|
|
|
|
{
|
|
|
|
m_dirty = true;
|
|
|
|
m_object = T();
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:02:49 +02:00
|
|
|
/// Returns true, if cache is dirty
|
|
|
|
inline bool isDirty() const { return m_dirty; }
|
|
|
|
|
2019-02-24 17:48:37 +01:00
|
|
|
private:
|
|
|
|
bool m_dirty;
|
|
|
|
T m_object;
|
|
|
|
};
|
|
|
|
|
2019-05-10 19:48:52 +02:00
|
|
|
/// Bit-reader, which can read n-bit unsigned integers from the stream.
|
|
|
|
/// Number of bits can be set in the constructor and is constant.
|
2019-10-29 15:34:09 +01:00
|
|
|
class PDFFORQTLIBSHARED_EXPORT PDFBitReader
|
2019-05-10 19:48:52 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Value = uint64_t;
|
|
|
|
|
2019-09-15 16:50:34 +02:00
|
|
|
explicit PDFBitReader(const QByteArray* stream, Value bitsPerComponent);
|
2019-05-10 19:48:52 +02:00
|
|
|
|
2019-10-26 18:02:37 +02:00
|
|
|
PDFBitReader(const PDFBitReader&) = default;
|
|
|
|
PDFBitReader(PDFBitReader&&) = default;
|
|
|
|
|
|
|
|
PDFBitReader& operator=(const PDFBitReader&) = default;
|
|
|
|
PDFBitReader& operator=(PDFBitReader&&) = default;
|
|
|
|
|
2019-05-10 19:48:52 +02:00
|
|
|
/// Returns maximal value of n-bit unsigned integer.
|
|
|
|
Value max() const { return m_maximalValue; }
|
|
|
|
|
|
|
|
/// Reads single n-bit value from the stream. If stream hasn't enough data,
|
|
|
|
/// then exception is thrown.
|
2019-09-15 16:50:34 +02:00
|
|
|
Value read() { return read(m_bitsPerComponent); }
|
|
|
|
|
|
|
|
/// Reads single n-bit value from the stream. If stream hasn't enough data,
|
|
|
|
/// then exception is thrown.
|
|
|
|
Value read(Value bits);
|
2019-05-10 19:48:52 +02:00
|
|
|
|
2019-10-12 18:10:25 +02:00
|
|
|
/// Reads single n-bit value from the stream. If stream hasn't enough data,
|
|
|
|
/// then exception is thrown. State of the stream is not changed, i.e., read
|
|
|
|
/// bits are reverted back.
|
|
|
|
Value look(Value bits) const;
|
|
|
|
|
2019-05-10 19:48:52 +02:00
|
|
|
/// Seeks the desired position in the data stream. If position can't be seeked,
|
|
|
|
/// then exception is thrown.
|
|
|
|
void seek(qint64 position);
|
|
|
|
|
2019-10-26 18:02:37 +02:00
|
|
|
/// Skips desired number of bytes
|
|
|
|
void skipBytes(Value bytes);
|
|
|
|
|
2019-09-15 16:50:34 +02:00
|
|
|
/// Seeks data to the byte boundary (number of processed bits is divisible by 8)
|
|
|
|
void alignToBytes();
|
|
|
|
|
2019-09-22 13:18:42 +02:00
|
|
|
/// Returns true, if we are at the end of the data stream (no more data can be read)
|
|
|
|
bool isAtEnd() const;
|
|
|
|
|
2019-10-26 18:02:37 +02:00
|
|
|
/// Returns position in the data stream (byte position, not bit position, so
|
|
|
|
/// result of this function is sometimes inaccurate)
|
|
|
|
int getPosition() const { return m_position; }
|
|
|
|
|
2019-10-27 15:52:50 +01:00
|
|
|
/// Reads signed 32-bit integer from the stream
|
|
|
|
int32_t readSignedInt();
|
|
|
|
|
2019-10-28 17:39:22 +01:00
|
|
|
/// Reads signed 8-bit integer from the stream
|
|
|
|
int8_t readSignedByte();
|
|
|
|
|
2019-10-27 15:52:50 +01:00
|
|
|
/// Reads unsigned 32-bit integer from the stream
|
|
|
|
uint32_t readUnsignedInt() { return read(32); }
|
|
|
|
|
|
|
|
/// Reads unsigned 16-bit integer from the stream
|
|
|
|
uint16_t readUnsignedWord() { return read(16); }
|
|
|
|
|
|
|
|
/// Reads unsigned 8-bit integer from the stream
|
|
|
|
uint8_t readUnsignedByte() { return read(8); }
|
|
|
|
|
2019-10-28 17:39:22 +01:00
|
|
|
/// Return underlying byte stream
|
|
|
|
const QByteArray* getStream() const { return m_stream; }
|
|
|
|
|
2019-10-30 17:44:04 +01:00
|
|
|
/// Reads substream from current stream. This function works only on byte boundary,
|
|
|
|
/// otherwise exception is thrown.
|
|
|
|
/// \param length Length of the substream. Can be -1, in this case, all remaining data is read.
|
|
|
|
QByteArray readSubstream(int length);
|
|
|
|
|
2019-05-10 19:48:52 +02:00
|
|
|
private:
|
2019-09-15 16:50:34 +02:00
|
|
|
const QByteArray* m_stream;
|
|
|
|
int m_position;
|
2019-05-10 19:48:52 +02:00
|
|
|
|
2019-10-26 18:02:37 +02:00
|
|
|
Value m_bitsPerComponent;
|
|
|
|
Value m_maximalValue;
|
2019-05-10 19:48:52 +02:00
|
|
|
|
|
|
|
Value m_buffer;
|
|
|
|
Value m_bitsInBuffer;
|
|
|
|
};
|
|
|
|
|
2019-10-05 17:38:15 +02:00
|
|
|
/// Bit writer
|
|
|
|
class PDFBitWriter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Value = uint64_t;
|
|
|
|
|
|
|
|
explicit PDFBitWriter(Value bitsPerComponent);
|
|
|
|
|
|
|
|
/// Writes value to the output stream
|
|
|
|
void write(Value value);
|
|
|
|
|
|
|
|
/// Finish line - align to byte boundary
|
|
|
|
void finishLine() { flush(true); }
|
|
|
|
|
|
|
|
/// Returns the result byte array
|
|
|
|
QByteArray takeByteArray() { return qMove(m_outputByteArray); }
|
|
|
|
|
|
|
|
/// Reserve memory in buffer
|
|
|
|
void reserve(int size) { m_outputByteArray.reserve(size); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void flush(bool alignToByteBoundary);
|
|
|
|
|
|
|
|
QByteArray m_outputByteArray;
|
|
|
|
|
|
|
|
Value m_bitsPerComponent;
|
|
|
|
Value m_mask;
|
|
|
|
Value m_buffer;
|
|
|
|
Value m_bitsInBuffer;
|
|
|
|
};
|
|
|
|
|
2019-08-25 18:16:37 +02:00
|
|
|
/// Simple class guard, for properly saving/restoring new/old value. In the constructor,
|
|
|
|
/// new value is stored in the pointer (old one is being saved), and in the destructor,
|
|
|
|
/// old value is restored. This object assumes, that value is not a null pointer.
|
|
|
|
template<typename Value>
|
|
|
|
class PDFTemporaryValueChange
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
/// \param value Value pointer (must not be a null pointer)
|
|
|
|
/// \param newValue New value to be set to the pointer
|
|
|
|
explicit inline PDFTemporaryValueChange(Value* valuePointer, Value newValue) :
|
|
|
|
m_oldValue(qMove(*valuePointer)),
|
|
|
|
m_value(valuePointer)
|
|
|
|
{
|
|
|
|
*valuePointer = qMove(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ~PDFTemporaryValueChange()
|
|
|
|
{
|
|
|
|
*m_value = qMove(m_oldValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Value m_oldValue;
|
|
|
|
Value* m_value;
|
|
|
|
};
|
|
|
|
|
2019-09-20 18:19:21 +02:00
|
|
|
/// Implements range for range based for cycles
|
|
|
|
template<typename T>
|
|
|
|
class PDFIntegerRange
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit inline constexpr PDFIntegerRange(T begin, T end) : m_begin(begin), m_end(end) { }
|
|
|
|
|
|
|
|
struct Iterator : public std::iterator<std::random_access_iterator_tag, T, ptrdiff_t, T*, T&>
|
|
|
|
{
|
|
|
|
inline Iterator() : value(T(0)) { }
|
|
|
|
inline Iterator(T value) : value(value) { }
|
|
|
|
|
|
|
|
inline bool operator==(const Iterator& other) const { return value == other.value; }
|
|
|
|
inline bool operator!=(const Iterator& other) const { return value != other.value; }
|
|
|
|
|
|
|
|
inline T operator*() const { return value; }
|
|
|
|
inline Iterator& operator+=(ptrdiff_t movement) { value += T(movement); return *this; }
|
|
|
|
inline Iterator& operator-=(ptrdiff_t movement) { value -= T(movement); return *this; }
|
|
|
|
inline Iterator operator+(ptrdiff_t movement) const { return Iterator(value + T(movement)); }
|
|
|
|
inline ptrdiff_t operator-(const Iterator& other) const { return ptrdiff_t(value - other.value); }
|
|
|
|
|
|
|
|
inline Iterator& operator++()
|
|
|
|
{
|
|
|
|
++value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Iterator operator++(int)
|
|
|
|
{
|
|
|
|
Iterator copy(*this);
|
|
|
|
++value;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Iterator& operator--()
|
|
|
|
{
|
|
|
|
--value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Iterator operator--(int)
|
|
|
|
{
|
|
|
|
Iterator copy(*this);
|
|
|
|
--value;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
T value = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterator begin() const { return Iterator(m_begin); }
|
|
|
|
Iterator end() const { return Iterator(m_end); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_begin;
|
|
|
|
T m_end;
|
|
|
|
};
|
|
|
|
|
2019-09-15 16:50:34 +02:00
|
|
|
template<typename T>
|
|
|
|
bool contains(T value, std::initializer_list<T> list)
|
|
|
|
{
|
|
|
|
return (std::find(list.begin(), list.end(), value) != list.end());
|
|
|
|
}
|
|
|
|
|
2019-06-15 17:40:22 +02:00
|
|
|
/// Performs linear mapping of value x in interval [x_min, x_max] to the interval [y_min, y_max].
|
|
|
|
/// \param x Value to be linearly remapped from interval [x_min, x_max] to the interval [y_min, y_max].
|
|
|
|
/// \param x_min Start of the input interval
|
|
|
|
/// \param x_max End of the input interval
|
|
|
|
/// \param y_min Start of the output interval
|
|
|
|
/// \param y_max End of the output interval
|
|
|
|
static inline constexpr PDFReal interpolate(PDFReal x, PDFReal x_min, PDFReal x_max, PDFReal y_min, PDFReal y_max)
|
|
|
|
{
|
|
|
|
return y_min + (x - x_min) * (y_max - y_min) / (x_max - x_min);
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:45:36 +02:00
|
|
|
inline
|
|
|
|
std::vector<uint8_t> convertByteArrayToVector(const QByteArray& data)
|
|
|
|
{
|
|
|
|
return std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(data.constData()), reinterpret_cast<const uint8_t*>(data.constData()) + data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
const unsigned char* convertByteArrayToUcharPtr(const QByteArray& data)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const unsigned char*>(data.constData());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
unsigned char* convertByteArrayToUcharPtr(QByteArray& data)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<unsigned char*>(data.data());
|
|
|
|
}
|
|
|
|
|
2019-11-01 16:47:30 +01:00
|
|
|
/// This function computes ceil of log base 2 of value. The algorithm is taken
|
|
|
|
/// from: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn.
|
|
|
|
/// License for this function is public domain.
|
|
|
|
inline constexpr uint8_t log2ceil(uint32_t value)
|
|
|
|
{
|
2019-11-05 17:34:07 +01:00
|
|
|
const uint32_t originalValue = value;
|
2019-11-01 16:47:30 +01:00
|
|
|
constexpr uint8_t MULTIPLY_DE_BRUIJN_BIT_POSITION[32] =
|
|
|
|
{
|
|
|
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
|
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
|
|
};
|
|
|
|
|
|
|
|
value |= value >> 1;
|
|
|
|
value |= value >> 2;
|
|
|
|
value |= value >> 4;
|
|
|
|
value |= value >> 8;
|
|
|
|
value |= value >> 16;
|
|
|
|
|
|
|
|
uint8_t logarithm = MULTIPLY_DE_BRUIJN_BIT_POSITION[static_cast<uint32_t>((value * 0x07C4ACDDU) >> 27)];
|
|
|
|
|
|
|
|
// Ceil
|
2019-11-05 17:34:07 +01:00
|
|
|
if ((1U << logarithm) < originalValue)
|
2019-11-01 16:47:30 +01:00
|
|
|
{
|
|
|
|
++logarithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
return logarithm;
|
|
|
|
}
|
|
|
|
|
2019-11-10 14:17:54 +01:00
|
|
|
struct PDFFORQTLIBSHARED_EXPORT PDFDependentLibraryInfo
|
|
|
|
{
|
|
|
|
Q_DECLARE_TR_FUNCTIONS(pdf::PDFDependentLibraryInfo)
|
|
|
|
|
|
|
|
public:
|
|
|
|
QString library;
|
|
|
|
QString version;
|
|
|
|
QString license;
|
|
|
|
QString url;
|
|
|
|
|
|
|
|
static std::vector<PDFDependentLibraryInfo> getLibraryInfo();
|
|
|
|
};
|
|
|
|
|
2019-12-28 19:21:29 +01:00
|
|
|
/// Union-find algorithm, which uses path compression optimization. It can run in time
|
|
|
|
/// O(n + f * (1 + log(n)/log(2 + f/n)), where n is number of unions (resp. size of the
|
|
|
|
/// array) and f is number of find operations.
|
|
|
|
template<typename T>
|
|
|
|
class PDFUnionFindAlgorithm
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit PDFUnionFindAlgorithm(T size)
|
|
|
|
{
|
|
|
|
m_indices.resize(size, T(0));
|
|
|
|
std::iota(m_indices.begin(), m_indices.end(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
T find(T index)
|
|
|
|
{
|
|
|
|
// Use path compression optimization. We assume we will not
|
|
|
|
// have long paths, so we will use simple recursion and
|
|
|
|
// not while cycle.
|
|
|
|
if (m_indices[index] != index)
|
|
|
|
{
|
|
|
|
m_indices[index] = find(m_indices[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_indices[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void unify(T x, T y)
|
|
|
|
{
|
|
|
|
T xRoot = find(x);
|
|
|
|
T yRoot = find(y);
|
|
|
|
|
|
|
|
if (xRoot < yRoot)
|
|
|
|
{
|
|
|
|
m_indices[yRoot] = xRoot;
|
|
|
|
}
|
|
|
|
else if (xRoot > yRoot)
|
|
|
|
{
|
|
|
|
m_indices[xRoot] = yRoot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> m_indices;
|
|
|
|
};
|
|
|
|
|
2019-12-29 17:25:18 +01:00
|
|
|
template<typename T>
|
|
|
|
constexpr bool isIntervalOverlap(T x1_min, T x1_max, T x2_min, T x2_max)
|
|
|
|
{
|
|
|
|
// We have two situations, where intervals doesn't overlap:
|
|
|
|
// 1) |--------| |---------|
|
|
|
|
// x1_min x1_max x2_min x2_max
|
|
|
|
// 2) |--------| |---------|
|
|
|
|
// x2_min x2_max x1_min x1_max
|
|
|
|
if (x1_max < x2_min || x2_max < x1_min)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool isRectangleHorizontallyOverlapped(const QRectF& r1, const QRectF& r2)
|
|
|
|
{
|
|
|
|
return isIntervalOverlap(r1.left(), r1.right(), r2.left(), r2.right());
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:17:45 +01:00
|
|
|
inline QColor invertColor(QColor color)
|
|
|
|
{
|
|
|
|
qreal r = 0.0;
|
|
|
|
qreal g = 0.0;
|
|
|
|
qreal b = 0.0;
|
|
|
|
qreal a = 0.0;
|
|
|
|
|
|
|
|
color.getRgbF(&r, &g, &b, &a);
|
|
|
|
|
|
|
|
r = 1.0 - r;
|
|
|
|
g = 1.0 - g;
|
|
|
|
b = 1.0 - b;
|
|
|
|
return QColor::fromRgbF(r, g, b, a);
|
|
|
|
}
|
|
|
|
|
2020-02-18 20:21:18 +01:00
|
|
|
/// Performs linear interpolation of interval [x1, x2] to interval [y1, y2],
|
|
|
|
/// using formula y = y1 + (x - x1) * (y2 - y1) / (x2 - x1), transformed
|
|
|
|
/// to formula y = k * x + q, where q = y1 - x1 * k and
|
|
|
|
/// k = (y2 - y1) / (x2 - x1).
|
|
|
|
template<typename T>
|
|
|
|
class PDFLinearInterpolation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
constexpr inline PDFLinearInterpolation(T x1, T x2, T y1, T y2) :
|
|
|
|
m_k((y2 - y1) / (x2 - x1)),
|
|
|
|
m_q(y1 - x1 * m_k)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps value from x interval to y interval
|
|
|
|
constexpr inline T operator()(T x) const
|
|
|
|
{
|
|
|
|
return m_k * x + m_q;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_k;
|
|
|
|
T m_q;
|
|
|
|
};
|
|
|
|
|
2020-02-28 18:56:28 +01:00
|
|
|
/// Fuzzy compares two points, with given tolerance (so, if points are at lower distance
|
|
|
|
/// from each other than squared tolerance, they are considered as same and function returns true).
|
|
|
|
/// \param p1 First point
|
|
|
|
/// \param p2 Second point
|
|
|
|
/// \param squaredTolerance Squared tolerance
|
|
|
|
static inline bool isFuzzyComparedPointsSame(const QPointF& p1, const QPointF& p2, PDFReal squaredTolerance)
|
|
|
|
{
|
|
|
|
QPointF dp = p2 - p1;
|
|
|
|
const qreal squaredDistance = QPointF::dotProduct(dp, dp);
|
|
|
|
return squaredDistance < squaredTolerance;
|
|
|
|
}
|
|
|
|
|
2020-03-22 15:30:34 +01:00
|
|
|
/// Storage for result of some operation. Stores, if operation was successful, or not and
|
|
|
|
/// also error message, why operation has failed. Can be converted explicitly to bool.
|
|
|
|
class PDFOperationResult
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline PDFOperationResult(bool success) :
|
|
|
|
m_success(success)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline PDFOperationResult(QString message) :
|
|
|
|
m_success(false),
|
|
|
|
m_errorMessage(qMove(message))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const { return m_success; }
|
|
|
|
|
|
|
|
const QString& getErrorMessage() const { return m_errorMessage; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_success;
|
|
|
|
QString m_errorMessage;
|
|
|
|
};
|
|
|
|
|
2020-06-20 14:10:46 +02:00
|
|
|
/// Set of closed intervals
|
|
|
|
class PDFClosedIntervalSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit inline PDFClosedIntervalSet() = default;
|
|
|
|
|
|
|
|
bool operator ==(const PDFClosedIntervalSet&) const = default;
|
|
|
|
|
|
|
|
using ClosedInterval = std::pair<PDFInteger, PDFInteger>;
|
|
|
|
|
|
|
|
/// Adds closed interval, where \p low is lower bound
|
|
|
|
/// of the closed interval, and high is upper bound
|
|
|
|
/// of closed interval.
|
|
|
|
/// \param low Lower bound of interval
|
|
|
|
/// \param high Upper bound of interval
|
|
|
|
void addInterval(PDFInteger low, PDFInteger high);
|
|
|
|
|
|
|
|
/// Adds a single value to the interval (closed interval
|
|
|
|
/// of single value)
|
|
|
|
/// \param value Value
|
|
|
|
void addValue(PDFInteger value) { addInterval(value, value); }
|
|
|
|
|
|
|
|
/// Returns true, if given closed range is subset of
|
|
|
|
/// this interval set.
|
|
|
|
bool isCovered(PDFInteger low, PDFInteger high);
|
|
|
|
|
|
|
|
/// Returns sum of interval lengths
|
|
|
|
PDFInteger getTotalLength() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Normalizes interval ranges - merges adjacent intervals
|
|
|
|
void normalize();
|
|
|
|
|
|
|
|
/// Returns true, if interval overlaps, or is adjacent to the other one
|
|
|
|
/// \param a First interval
|
|
|
|
/// \param b Second interval
|
|
|
|
static bool overlapsOrAdjacent(ClosedInterval a, ClosedInterval b);
|
|
|
|
|
|
|
|
std::vector<ClosedInterval> m_intervals;
|
|
|
|
};
|
|
|
|
|
2020-06-27 17:28:03 +02:00
|
|
|
template<typename T>
|
|
|
|
QDataStream& operator>>(QDataStream& stream, std::vector<T>& vector)
|
|
|
|
{
|
|
|
|
std::vector<T>::size_type size = 0;
|
|
|
|
stream >> size;
|
|
|
|
vector.resize(size);
|
|
|
|
for (T& item : vector)
|
|
|
|
{
|
|
|
|
stream >> item;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QDataStream& operator<<(QDataStream& stream, const std::vector<T>& vector)
|
|
|
|
{
|
|
|
|
stream << vector.size();
|
|
|
|
for (const T& item : vector)
|
|
|
|
{
|
|
|
|
stream << item;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t Size>
|
|
|
|
QDataStream& operator>>(QDataStream& stream, std::array<T, Size>& array)
|
|
|
|
{
|
|
|
|
std::array<T, Size>::size_type size = 0;
|
|
|
|
stream >> size;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (i < array.size())
|
|
|
|
{
|
|
|
|
stream >> array[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
T item;
|
|
|
|
stream >> item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If array size was changed, then fill in empty objects
|
|
|
|
for (size_t i = size; i < array.size(); ++i)
|
|
|
|
{
|
|
|
|
array[i] = T();
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t Size>
|
|
|
|
QDataStream& operator<<(QDataStream& stream, const std::array<T, Size>& array)
|
|
|
|
{
|
|
|
|
stream << array.size();
|
|
|
|
for (const T& item : array)
|
|
|
|
{
|
|
|
|
stream << item;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QDataStream& operator>>(QDataStream& stream, std::set<T>& set)
|
|
|
|
{
|
|
|
|
std::set<T>::size_type size = 0;
|
|
|
|
stream >> size;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
T item;
|
|
|
|
stream >> item;
|
|
|
|
set.insert(set.end(), qMove(item));
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QDataStream& operator<<(QDataStream& stream, const std::set<T>& set)
|
|
|
|
{
|
|
|
|
stream << set.size();
|
|
|
|
for (const T& item : set)
|
|
|
|
{
|
|
|
|
stream << item;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2019-02-24 17:48:37 +01:00
|
|
|
} // namespace pdf
|
|
|
|
|
|
|
|
#endif // PDFUTILS_H
|