File indexing completed on 2024-04-21 05:51:42

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2010 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "fontplugin.h"
0005 #include "batchrenamer.h"
0006 #include "tokenhelpdialog.h"
0007 
0008 #include <ft2build.h>
0009 #include FT_FREETYPE_H
0010 
0011 #include <KLocalizedString>
0012 
0013 FontPlugin::FontPlugin(PluginLoader *loader)
0014     : FilePlugin(loader)
0015 {
0016     this->addSupportedToken("fontpostscript");
0017     this->addSupportedToken("fontfamily");
0018     this->addSupportedToken("fontstyle");
0019     m_help.append("[fontPostscript]" + TokenHelpDialog::getTokenSeparator() + i18n("Insert the PostScript name for Type1 and TrueType fonts."));
0020     m_help.append("[fontFamily]" + TokenHelpDialog::getTokenSeparator() + i18n("Insert the (usually English) name of the font family."));
0021     m_help.append("[fontStyle]" + TokenHelpDialog::getTokenSeparator() + i18n("Insert the (usually English) name of the font style."));
0022 
0023     m_name = i18n("Font (FreeType2) Plugin");
0024     m_comment = i18n("<qt>This plugin supports reading tags from "
0025                      "font files.</qt>");
0026 
0027     m_icon = "application-x-font-ttf";
0028 
0029     FT_Error error = FT_Init_FreeType(&m_library);
0030     if (error) {
0031         qDebug("Freetype initialization error %i.", error);
0032         m_library = nullptr;
0033     }
0034 }
0035 
0036 FontPlugin::~FontPlugin()
0037 {
0038     FT_Done_FreeType(m_library);
0039     m_library = nullptr;
0040 
0041 }
0042 
0043 QString FontPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType)
0044 {
0045     QString token(filenameOrToken.toLower());
0046     QString filename = (*b->files())[index].srcUrl().path();
0047 
0048     if (!this->supports(token)) {
0049         return QString("");
0050     }
0051 
0052     if (!m_library) {
0053         return QString("Cannot initialize FreeType");
0054     }
0055 
0056     FT_Face face;
0057     FT_Error error = FT_New_Face(m_library,
0058                                  filename.toUtf8().data(),
0059                                  0,
0060                                  &face);
0061     QString result = QString("");
0062 
0063     if (error == FT_Err_Unknown_File_Format) {
0064         face = nullptr;
0065         result = QString("Unknown font file format error: %1").arg(error);
0066     } else if (error) {
0067         face = nullptr;
0068         result = QString("Unknown error: %1.").arg(error);
0069     } else {
0070         if (token == "fontpostscript") {
0071             result = QString::fromLocal8Bit(FT_Get_Postscript_Name(face));
0072         } else if (token == "fontfamily" && face->family_name) {
0073             result = QString::fromLocal8Bit(face->family_name);
0074         } else if (token == "fontstyle" && face->style_name) {
0075             result = QString::fromLocal8Bit(face->style_name);
0076         }
0077     }
0078 
0079     if (face) {
0080         FT_Done_Face(face);
0081     }
0082 
0083     return result;
0084 }