File indexing completed on 2024-05-05 04:21:07

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #include "kpDocumentMetaInfo.h"
0030 
0031 #include <cmath>
0032 
0033 #include <QPoint>
0034 
0035 #include "kpLogCategories.h"
0036 
0037 #include "kpDefs.h"
0038 
0039 
0040 //
0041 // Constants which "ought to be enough for anybody"
0042 // LOTODO: Maybe there are some QImage constants somewhere?
0043 //
0044 
0045 // public static
0046 
0047 // (round up to guarantee at least 1 dot per inch)
0048 const int kpDocumentMetaInfo::MinDotsPerMeter =
0049     int (std::ceil (1/*single dot per inch - a very low DPI*/ * KP_INCHES_PER_METER) + 0.1);
0050 
0051 const int kpDocumentMetaInfo::MaxDotsPerMeter =
0052     int ((600 * 100)/*a lot of DPI*/ * KP_INCHES_PER_METER);
0053 
0054 // public static
0055 const int kpDocumentMetaInfo::MaxOffset = (4000/*big image*/ * 100)/*a very big image*/;
0056 const int kpDocumentMetaInfo::MinOffset = -kpDocumentMetaInfo::MaxOffset;
0057 
0058 //---------------------------------------------------------------------
0059 
0060 struct kpDocumentMetaInfoPrivate
0061 {
0062     int m_dotsPerMeterX{}, m_dotsPerMeterY{};
0063     QPoint m_offset;
0064 
0065     QMap <QString, QString> m_textMap;
0066 };
0067 
0068 //---------------------------------------------------------------------
0069 
0070 // public
0071 kpDocumentMetaInfo::kpDocumentMetaInfo ()
0072     : d (new kpDocumentMetaInfoPrivate ())
0073 {
0074     d->m_dotsPerMeterX = 0;
0075     d->m_dotsPerMeterY = 0;
0076     d->m_offset = QPoint (0, 0);
0077 }
0078 
0079 //---------------------------------------------------------------------
0080 
0081 kpDocumentMetaInfo::kpDocumentMetaInfo (const kpDocumentMetaInfo &rhs)
0082     : d (new kpDocumentMetaInfoPrivate ())
0083 {
0084     d->m_dotsPerMeterX = rhs.dotsPerMeterX ();
0085     d->m_dotsPerMeterY = rhs.dotsPerMeterY ();
0086     d->m_offset = rhs.offset ();
0087     d->m_textMap = rhs.textMap ();
0088 }
0089 
0090 //---------------------------------------------------------------------
0091 
0092 // public
0093 kpDocumentMetaInfo::~kpDocumentMetaInfo ()
0094 {
0095     delete d;
0096 }
0097 
0098 //---------------------------------------------------------------------
0099 
0100 // public
0101 bool kpDocumentMetaInfo::operator== (const kpDocumentMetaInfo &rhs) const
0102 {
0103     return (d->m_dotsPerMeterX == rhs.d->m_dotsPerMeterX &&
0104             d->m_dotsPerMeterY == rhs.d->m_dotsPerMeterY &&
0105             d->m_offset == rhs.d->m_offset &&
0106             d->m_textMap == rhs.d->m_textMap);
0107 }
0108 
0109 //---------------------------------------------------------------------
0110 
0111 // public
0112 bool kpDocumentMetaInfo::operator!= (const kpDocumentMetaInfo &rhs) const
0113 {
0114     return !(*this == rhs);
0115 }
0116 
0117 //---------------------------------------------------------------------
0118 
0119 // public
0120 kpDocumentMetaInfo &kpDocumentMetaInfo::operator= (const kpDocumentMetaInfo &rhs)
0121 {
0122     if (this == &rhs) {
0123         return *this;
0124     }
0125 
0126     d->m_dotsPerMeterX = rhs.dotsPerMeterX ();
0127     d->m_dotsPerMeterY = rhs.dotsPerMeterY ();
0128     d->m_offset = rhs.offset ();
0129     d->m_textMap = rhs.textMap ();
0130 
0131     return *this;
0132 }
0133 
0134 //---------------------------------------------------------------------
0135 
0136 // public
0137 void kpDocumentMetaInfo::printDebug (const QString &prefix) const
0138 {
0139     const QString usedPrefix = !prefix.isEmpty() ? QString(prefix + QLatin1String(":")) : QString();
0140 
0141     qCDebug(kpLogImagelib) << usedPrefix;
0142 
0143     qCDebug(kpLogImagelib) << "dotsPerMeter X=" << dotsPerMeterX ()
0144                << " Y=" << dotsPerMeterY ()
0145                << " offset=" << offset ();
0146 
0147     const QStringList keys = textKeys();
0148     for (const QString &key : keys)
0149       qCDebug(kpLogImagelib) << "key=" << key << " text=" << text(key);
0150 
0151     qCDebug(kpLogImagelib) << usedPrefix << "ENDS";
0152 }
0153 
0154 //---------------------------------------------------------------------
0155 
0156 // public
0157 kpCommandSize::SizeType kpDocumentMetaInfo::size () const
0158 {
0159     kpCommandSize::SizeType ret = 0;
0160 
0161     for (const auto &key : d->m_textMap.keys ())
0162     {
0163         ret += kpCommandSize::StringSize (key) +
0164                kpCommandSize::StringSize (d->m_textMap [key]);
0165     }
0166 
0167     // We don't know what the QMap size overhead is so overestimate the size
0168     // rather than underestimating it.
0169     // LOTODO: Find the proper size in bytes.
0170     return ret * 3;
0171 }
0172 
0173 //---------------------------------------------------------------------
0174 
0175 // public
0176 int kpDocumentMetaInfo::dotsPerMeterX () const
0177 {
0178     return d->m_dotsPerMeterX;
0179 }
0180 
0181 //---------------------------------------------------------------------
0182 
0183 // public
0184 void kpDocumentMetaInfo::setDotsPerMeterX (int val)
0185 {
0186     // Unspecified resolution?
0187     if (val == 0)
0188     {
0189         d->m_dotsPerMeterX = 0;
0190         return;
0191     }
0192 
0193     d->m_dotsPerMeterX = qBound (MinDotsPerMeter, val, MaxDotsPerMeter);
0194 }
0195 
0196 //---------------------------------------------------------------------
0197 
0198 // public
0199 int kpDocumentMetaInfo::dotsPerMeterY () const
0200 {
0201     return d->m_dotsPerMeterY;
0202 }
0203 
0204 //---------------------------------------------------------------------
0205 
0206 // public
0207 void kpDocumentMetaInfo::setDotsPerMeterY (int val)
0208 {
0209     // Unspecified resolution?
0210     if (val == 0)
0211     {
0212         d->m_dotsPerMeterY = 0;
0213         return;
0214     }
0215 
0216     d->m_dotsPerMeterY = qBound (MinDotsPerMeter, val, MaxDotsPerMeter);
0217 }
0218 
0219 //---------------------------------------------------------------------
0220 
0221 // public
0222 QPoint kpDocumentMetaInfo::offset () const
0223 {
0224     return d->m_offset;
0225 }
0226 
0227 //---------------------------------------------------------------------
0228 
0229 // public
0230 void kpDocumentMetaInfo::setOffset (const QPoint &point)
0231 {
0232     const int x = qBound (MinOffset, point.x (), MaxOffset);
0233     const int y = qBound (MinOffset, point.y (), MaxOffset);
0234 
0235     d->m_offset = QPoint (x, y);
0236 }
0237 
0238 //---------------------------------------------------------------------
0239 
0240 // public
0241 QMap <QString, QString> kpDocumentMetaInfo::textMap () const
0242 {
0243     return d->m_textMap;
0244 }
0245 
0246 //---------------------------------------------------------------------
0247 
0248 // public
0249 QList <QString> kpDocumentMetaInfo::textKeys () const
0250 {
0251     return d->m_textMap.keys ();
0252 }
0253 
0254 //---------------------------------------------------------------------
0255 
0256 // public
0257 QString kpDocumentMetaInfo::text (const QString &key) const
0258 {
0259     if (key.isEmpty ()) {
0260         return {};
0261     }
0262 
0263     return d->m_textMap [key];
0264 }
0265 
0266 //---------------------------------------------------------------------
0267 
0268 // public
0269 void kpDocumentMetaInfo::setText (const QString &key, const QString &value)
0270 {
0271     if (key.isEmpty ()) {
0272         return;
0273     }
0274 
0275     d->m_textMap [key] = value;
0276 }
0277 
0278 //---------------------------------------------------------------------