kio: implement fonts meta information extractor via Freetype

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-06 20:51:32 +03:00
parent afedc38fec
commit 451301f58f
6 changed files with 181 additions and 1 deletions

View file

@ -248,6 +248,14 @@ set_package_properties(LibSpectre PROPERTIES
PURPOSE "Postscript metadata extraction"
)
kde4_optional_find_package(Freetype)
set_package_properties(Freetype PROPERTIES
DESCRIPTION "Freely available software library to render fonts"
URL "https://www.freetype.org"
TYPE RECOMMENDED
PURPOSE "Fonts metadata extraction"
)
kde4_optional_find_package(OpenSSL)
set_package_properties(OpenSSL PROPERTIES
DESCRIPTION "Robust, commercial-grade, full-featured toolkit for general-purpose cryptography and secure communication"

View file

@ -298,6 +298,7 @@ QString KFileMetaInfo::name(const QString& key)
{ "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#isoSpeedRatings", i18nc("@label EXIF", "ISO Speed Ratings") },
{ "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#meteringMode", i18nc("@label EXIF", "Metering Mode") },
{ "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#whiteBalance", i18nc("@label EXIF", "White Balance") },
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fontFamily", i18nc("@label", "Family") },
// to be used by plugins
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#Email", i18nc("@label", "Creator E-Mail") },
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hashValue", i18nc("@label", "Hash Value") },
@ -327,7 +328,6 @@ QString KFileMetaInfo::name(const QString& key)
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#audioBitDepth", i18nc("@label", "Audio Bit Depth") },
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#videoBitDepth", i18nc("@label", "Video Bit Depth") },
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#aspectRatio", i18nc("@label", "Aspect Ratio") },
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fontFamily", i18nc("@label", "Family") },
{ "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#foundry", i18nc("@label", "Foundry") },
};

View file

@ -125,3 +125,25 @@ if (LIBSPECTRE_FOUND)
DESTINATION ${KDE4_SERVICES_INSTALL_DIR}
)
endif()
if (FREETYPE_FOUND)
include_directories(${FREETYPE_INCLUDE_DIRS})
set(kfilemetadata_freetype_SRCS kfilemetadata_freetype.cpp)
kde4_add_plugin(kfilemetadata_freetype ${kfilemetadata_freetype_SRCS})
target_link_libraries(kfilemetadata_freetype
${KDE4_KIO_LIBS}
${FREETYPE_LIBRARIES}
)
install(
TARGETS kfilemetadata_freetype
DESTINATION ${KDE4_PLUGIN_INSTALL_DIR}
)
install(
FILES kfilemetadata_freetype.desktop
DESTINATION ${KDE4_SERVICES_INSTALL_DIR}
)
endif()

View file

@ -0,0 +1,105 @@
/* This file is part of the KDE libraries
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kfilemetadata_freetype.h"
#include "kpluginfactory.h"
#include "kdebug.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TYPE1_TABLES_H
KFileMetaDataFreetypePlugin::KFileMetaDataFreetypePlugin(QObject* parent, const QVariantList &args)
: KFileMetaDataPlugin(parent)
{
Q_UNUSED(args);
}
KFileMetaDataFreetypePlugin::~KFileMetaDataFreetypePlugin()
{
}
QStringList KFileMetaDataFreetypePlugin::keys() const
{
static const QStringList result = QStringList()
<< QString::fromLatin1("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fontFamily")
<< QString::fromLatin1("http://www.semanticdesktop.org/ontologies/2007/01/19/nie#copyright");
return result;
}
QStringList KFileMetaDataFreetypePlugin::mimeTypes() const
{
static const QStringList result = QStringList()
<< QString::fromLatin1("font/ttf")
<< QString::fromLatin1("application/x-font-ttf")
<< QString::fromLatin1("application/x-font-type1")
<< QString::fromLatin1("font/otf")
<< QString::fromLatin1("application/x-font-otf")
<< QString::fromLatin1("application/x-font-afm")
<< QString::fromLatin1("application/x-font-ttx")
<< QString::fromLatin1("font/woff");
return result;
}
QList<KFileMetaInfoItem> KFileMetaDataFreetypePlugin::metaData(const KUrl &url, const KFileMetaInfo::WhatFlags flags)
{
Q_UNUSED(flags);
QList<KFileMetaInfoItem> result;
const QByteArray urlpath = url.toLocalFile().toLocal8Bit();
FT_Library ftlibrary;
FT_Init_FreeType(&ftlibrary);
if (!ftlibrary) {
kWarning() << "Could not initialize";
return result;
}
FT_Face ftface;
if (FT_New_Face(ftlibrary, urlpath.constData(), 0, &ftface) != 0) {
kWarning() << "Could not open" << urlpath;
FT_Done_FreeType(ftlibrary);
return result;
}
const QString ftfamily = QString::fromUtf8(ftface->family_name);
if (!ftfamily.isEmpty()) {
result.append(
KFileMetaInfoItem(
QString::fromLatin1("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fontFamily"),
ftfamily
)
);
}
PS_FontInfoRec ftfontinfo;
if (FT_Get_PS_Font_Info(ftface, &ftfontinfo) == 0) {
const QString ftnotice = QString::fromUtf8(ftfontinfo.notice);
if (!ftnotice.isEmpty()) {
result.append(
KFileMetaInfoItem(
QString::fromLatin1("http://www.semanticdesktop.org/ontologies/2007/01/19/nie#copyright"),
ftnotice
)
);
}
}
FT_Done_Face(ftface);
FT_Done_FreeType(ftlibrary);
return result;
}
K_PLUGIN_FACTORY(KFileMetaDataFreetypePluginFactory, registerPlugin<KFileMetaDataFreetypePlugin>();)
K_EXPORT_PLUGIN(KFileMetaDataFreetypePluginFactory("kfilemetadata_freetype"))
#include "moc_kfilemetadata_freetype.cpp"

View file

@ -0,0 +1,8 @@
[Desktop Entry]
Type=Service
Name=KFileMetaDataFreetypePlugin
GenericName=Freetype
Comment=Extracts metadata from fonts
X-KDE-Library=kfilemetadata_freetype
X-KDE-ServiceTypes=KFileMetaData/Plugin
InitialPreference=1

View file

@ -0,0 +1,37 @@
/* This file is part of the KDE libraries
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KFILEMETADATA_FREETYPE_H
#define KFILEMETADATA_FREETYPE_H
#include "kfilemetadata.h"
class KFileMetaDataFreetypePlugin : public KFileMetaDataPlugin
{
Q_OBJECT
public:
KFileMetaDataFreetypePlugin(QObject* parent, const QVariantList &args);
~KFileMetaDataFreetypePlugin();
QStringList keys() const final;
QStringList mimeTypes() const final;
QList<KFileMetaInfoItem> metaData(const KUrl &url, const KFileMetaInfo::WhatFlags flags) final;
};
#endif // KFILEMETADATA_FREETYPE_H