Commit ada6ac69 authored by Marcel Wiesweg's avatar Marcel Wiesweg
Browse files

Fix detection of true 7bit ASCII.

The Qt Latin1 codec will only tell us if the characters are in the 8bit ISO-8859-1 range.
Use the C function isascii to test if the characters are in the true 7bit ASCII range.

BUG: 205824
parent 80df5cc1
......@@ -29,6 +29,10 @@
#include "kexiv2_p.h"
#include "rotationmatrix.h"
// C++ includes
#include <cctype>
// KDE includes
#include <klocale.h>
......@@ -240,6 +244,19 @@ QString KExiv2::getExifComment() const
return QString();
}
static bool is7BitAscii(const QByteArray& s)
{
const int size = s.size();
for (int i=0; i<size; i++)
{
if (!isascii(s[i]))
{
return false;
}
}
return true;
}
bool KExiv2::setExifComment(const QString& comment, bool setProgramName) const
{
if (!setProgramId(setProgramName))
......@@ -258,20 +275,23 @@ bool KExiv2::setExifComment(const QString& comment, bool setProgramName) const
QTextCodec* latin1Codec = QTextCodec::codecForName("iso8859-1");
if (latin1Codec->canEncode(comment))
{
// write as ASCII
std::string exifComment("charset=\"Ascii\" ");
exifComment += comment.toLatin1().constData();
d->exifMetadata()["Exif.Photo.UserComment"] = exifComment;
}
else
{
// write as Unicode (UCS-2)
std::string exifComment("charset=\"Unicode\" ");
exifComment += comment.toUtf8().constData();
d->exifMetadata()["Exif.Photo.UserComment"] = exifComment;
// We know it's in the ISO-8859-1 8bit range.
// Check if it's in the ASCII 7bit range
if (is7BitAscii(comment.toLatin1()))
{
// write as ASCII
std::string exifComment("charset=\"Ascii\" ");
exifComment += comment.toLatin1().constData();
d->exifMetadata()["Exif.Photo.UserComment"] = exifComment;
return true;
}
}
// write as Unicode (UCS-2)
std::string exifComment("charset=\"Unicode\" ");
exifComment += comment.toUtf8().constData();
d->exifMetadata()["Exif.Photo.UserComment"] = exifComment;
return true;
}
return true;
}
catch( Exiv2::Error& e )
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment