libetonyek_utils.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/*
3 * This file is part of the libetonyek project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#ifndef LIBETONYEK_UTILS_H_INCLUDED
11#define LIBETONYEK_UTILS_H_INCLUDED
12
13#ifdef HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include <cmath>
18#include <memory>
19#include <string>
20
21#include <boost/cstdint.hpp>
22
23#include <librevenge/librevenge.h>
24#include <librevenge-stream/librevenge-stream.h>
25
26#define ETONYEK_EPSILON 1e-9
27#define ETONYEK_ALMOST_ZERO(x) (std::fabs(x) < ETONYEK_EPSILON)
28
29#define ETONYEK_NUM_ELEMENTS(array) (sizeof(array) / sizeof((array)[0]))
30
31#if defined(HAVE_FUNC_ATTRIBUTE_FORMAT)
32# define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg) __attribute__((__format__(__printf__, fmt, arg)))
33#else
34# define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg)
35#endif
36
37#if defined(HAVE_CLANG_ATTRIBUTE_FALLTHROUGH)
38# define ETONYEK_FALLTHROUGH [[clang::fallthrough]]
39#elif defined(HAVE_GCC_ATTRIBUTE_FALLTHROUGH)
40# define ETONYEK_FALLTHROUGH __attribute__((fallthrough))
41#else
42# define ETONYEK_FALLTHROUGH ((void) 0)
43#endif
44
45// do nothing with debug messages in a release compile
46#ifdef DEBUG
47 namespace libetonyek
48{
49 void debugPrint(const char *format, ...) ETONYEK_ATTRIBUTE_PRINTF(1, 2);
50}
51#define ETONYEK_DEBUG_MSG(M) libetonyek::debugPrint M
52#define ETONYEK_DEBUG(M) M
53#else
54#define ETONYEK_DEBUG_MSG(M)
55#define ETONYEK_DEBUG(M)
56#endif
57
58 namespace libetonyek
59{
60
61 struct IWORKColor;
62 struct IWORKGradient;
63 struct IWORKStroke;
64
65/* Constants */
66 const double etonyek_half_pi(1.57079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107404326e+00);
67 const double etonyek_third_pi(1.04719755119659774615421446109316762806572313312503527365831486410260546876206966620934494178070568932738269550e+00);
68 const double etonyek_pi(3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00);
69 const double etonyek_two_pi(6.28318530717958647692528676655900576839433879875021164194988918461563281257241799725606965068423413596429617303e+00);
70
71 const double etonyek_root_three(1.73205080756887729352744634150587236694280525381038062805580697945193301690880003708114618675724857567562614142e+00);
72 const double etonyek_root_two(1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623e+00);
73
74// Apple Numbers timestamp starts from 01 Jan 2001 00:00:00
75 const unsigned ETONYEK_EPOCH_BEGIN(978307200);
76
77 struct EtonyekDummyDeleter
78{
79 void operator()(void *) {}
80};
81
82 typedef std::shared_ptr<librevenge::RVNGInputStream> RVNGInputStreamPtr_t;
83
84uint8_t readU8(const RVNGInputStreamPtr_t &input, bool = false);
85uint16_t readU16(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
86uint32_t readU32(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
87uint64_t readU64(const RVNGInputStreamPtr_t &input, bool bigEndian=false);
88
89uint64_t readUVar(const RVNGInputStreamPtr_t &input);
90int64_t readSVar(const RVNGInputStreamPtr_t &input);
91
92 double readDouble(const RVNGInputStreamPtr_t &input);
93 float readFloat(const RVNGInputStreamPtr_t &input);
94
95 unsigned long getLength(const RVNGInputStreamPtr_t &input);
96 unsigned long getRemainingLength(const RVNGInputStreamPtr_t &input);
97
104 bool approxEqual(double x, double y, double eps = ETONYEK_EPSILON);
105
106 template<class T>
107 bool approxEqual(const T &left, const T &right, const double eps = ETONYEK_EPSILON)
108{
109 assert(left.length() == right.length());
110
111 for (int i = 0; i != left.length(); ++i)
112 {
113 if (!approxEqual(left[i], right[i], eps))
114 return false;
115 }
116 return true;
117}
118
124 double pt2in(double d);
125
131 double deg2rad(double value);
132
138 double rad2deg(double value);
139
140librevenge::RVNGString makeColor(const IWORKColor &color);
143librevenge::RVNGString makeColor(const IWORKGradient &gradient);
144
145 void writeBorder(const IWORKStroke &stroke, const char *name, librevenge::RVNGPropertyList &props);
146
147std::string detectMimetype(const RVNGInputStreamPtr_t &stream);
148 bool detectImageDimension(const RVNGInputStreamPtr_t &stream, double &width, double &height);
149
150 class EndOfStreamException
151{
152};
153
154 class GenericException
155{
156};
157
158} // namespace libetonyek
159
160#endif // LIBETONYEK_UTILS_H_INCLUDED
161
162/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libetonyek::EndOfStreamException
Definition libetonyek_utils.h:151
libetonyek::GenericException
Definition libetonyek_utils.h:155
ETONYEK_EPSILON
#define ETONYEK_EPSILON
Definition libetonyek_utils.h:26
ETONYEK_ATTRIBUTE_PRINTF
#define ETONYEK_ATTRIBUTE_PRINTF(fmt, arg)
Definition libetonyek_utils.h:34
libetonyek::IWORKToken::value
@ value
Definition IWORKToken.h:631
libetonyek::IWORKToken::y
@ y
Definition IWORKToken.h:643
libetonyek::IWORKToken::format
@ format
Definition IWORKToken.h:537
libetonyek::IWORKToken::height
@ height
Definition IWORKToken.h:558
libetonyek::IWORKToken::left
@ left
Definition IWORKToken.h:575
libetonyek::IWORKToken::x
@ x
Definition IWORKToken.h:641
libetonyek::IWORKToken::color
@ color
Definition IWORKToken.h:155
libetonyek::IWORKToken::width
@ width
Definition IWORKToken.h:639
libetonyek::IWORKToken::stroke
@ stroke
Definition IWORKToken.h:419
libetonyek::IWORKToken::d
@ d
Definition IWORKToken.h:177
libetonyek::IWORKToken::right
@ right
Definition IWORKToken.h:604
libetonyek::IWORKToken::name
@ name
Definition IWORKToken.h:585
libetonyek::KEY1Token::gradient
@ gradient
Definition KEY1Token.h:58
libetonyek::KEY2Token::i
@ i
Definition KEY2Token.h:101
libetonyek
Definition IWORKBezierElement.cpp:21
libetonyek::rad2deg
double rad2deg(double value)
Convert an angle from radians to degrees.
Definition libetonyek_utils.cpp:241
libetonyek::etonyek_pi
const double etonyek_pi(3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00)
libetonyek::readFloat
float readFloat(const RVNGInputStreamPtr_t &input)
Definition libetonyek_utils.cpp:175
libetonyek::makeColor
librevenge::RVNGString makeColor(const IWORKColor &color)
Definition libetonyek_utils.cpp:253
libetonyek::writeBorder
void writeBorder(const IWORKStroke &stroke, const char *const name, librevenge::RVNGPropertyList &props)
Definition libetonyek_utils.cpp:288
libetonyek::etonyek_root_three
const double etonyek_root_three(1.73205080756887729352744634150587236694280525381038062805580697945193301690880003708114618675724857567562614142e+00)
libetonyek::RVNGInputStreamPtr_t
std::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr_t
Definition libetonyek_utils.h:82
libetonyek::deg2rad
double deg2rad(double value)
Convert an angle from degrees to radians.
Definition libetonyek_utils.cpp:232
libetonyek::readUVar
uint64_t readUVar(const RVNGInputStreamPtr_t &input)
Definition libetonyek_utils.cpp:110
libetonyek::approxEqual
bool approxEqual(const IWORKPath &left, const IWORKPath &right, double eps=ETONYEK_EPSILON)
libetonyek::readU8
uint8_t readU8(const RVNGInputStreamPtr_t &input, bool)
Definition libetonyek_utils.cpp:50
libetonyek::readSVar
int64_t readSVar(const RVNGInputStreamPtr_t &input)
Definition libetonyek_utils.cpp:143
libetonyek::etonyek_third_pi
const double etonyek_third_pi(1.04719755119659774615421446109316762806572313312503527365831486410260546876206966620934494178070568932738269550e+00)
libetonyek::readU32
uint32_t readU32(const RVNGInputStreamPtr_t &input, bool bigEndian)
Definition libetonyek_utils.cpp:78
libetonyek::readDouble
double readDouble(const RVNGInputStreamPtr_t &input)
Definition libetonyek_utils.cpp:164
libetonyek::ETONYEK_EPOCH_BEGIN
const unsigned ETONYEK_EPOCH_BEGIN(978307200)
libetonyek::getRemainingLength
unsigned long getRemainingLength(const RVNGInputStreamPtr_t &input)
Definition libetonyek_utils.cpp:217
libetonyek::detectImageDimension
bool detectImageDimension(const RVNGInputStreamPtr_t &stream, double &width, double &height)
Definition libetonyek_utils.cpp:388
libetonyek::getLength
unsigned long getLength(const RVNGInputStreamPtr_t &input)
Definition libetonyek_utils.cpp:186
libetonyek::etonyek_two_pi
const double etonyek_two_pi(6.28318530717958647692528676655900576839433879875021164194988918461563281257241799725606965068423413596429617303e+00)
libetonyek::readU16
uint16_t readU16(const RVNGInputStreamPtr_t &input, bool bigEndian)
Definition libetonyek_utils.cpp:62
libetonyek::detectMimetype
std::string detectMimetype(const RVNGInputStreamPtr_t &stream)
Definition libetonyek_utils.cpp:335
libetonyek::readU64
uint64_t readU64(const RVNGInputStreamPtr_t &input, bool bigEndian)
Definition libetonyek_utils.cpp:94
libetonyek::etonyek_root_two
const double etonyek_root_two(1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623e+00)
libetonyek::pt2in
double pt2in(const double d)
Convert a length from points to inches.
Definition libetonyek_utils.cpp:227
libetonyek::etonyek_half_pi
const double etonyek_half_pi(1.57079632679489661923132169163975144209858469968755291048747229615390820314310449931401741267105853399107404326e+00)
libetonyek::EtonyekDummyDeleter
Definition libetonyek_utils.h:78
libetonyek::EtonyekDummyDeleter::operator()
void operator()(void *)
Definition libetonyek_utils.h:79