File indexing completed on 2022-11-29 20:25:13

0001 /***************************************************************************
0002                       fontplugin.cpp  -  description
0003                              -------------------
0004     begin                : Sun Sep 26th 2010
0005     copyright            : (C) 2010 by Dominik Seichter
0006     email                : domseichter@web.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "fontplugin.h"
0019 #include "batchrenamer.h"
0020 #include "tokenhelpdialog.h"
0021 
0022 #include <ft2build.h>
0023 #include FT_FREETYPE_H
0024 
0025 #include <KLocalizedString>
0026 
0027 FontPlugin::FontPlugin(PluginLoader *loader)
0028     : FilePlugin(loader)
0029 {
0030     const QString prefix("font");
0031 
0032     this->addSupportedToken("fontpostscript");
0033     this->addSupportedToken("fontfamily");
0034     this->addSupportedToken("fontstyle");
0035     m_help.append("[fontPostscript]" + TokenHelpDialog::getTokenSeparator() + i18n("Insert the PostScript name for Type1 and TrueType fonts."));
0036     m_help.append("[fontFamily]" + TokenHelpDialog::getTokenSeparator() + i18n("Insert the (usually English) name of the font family."));
0037     m_help.append("[fontStyle]" + TokenHelpDialog::getTokenSeparator() + i18n("Insert the (usually English) name of the font style."));
0038 
0039     m_name = i18n("Font (FreeType2) Plugin");
0040     m_comment = i18n("<qt>This plugin supports reading tags from "
0041                      "font files.</qt>");
0042 
0043     m_icon = "application-x-font-ttf";
0044 
0045     FT_Error error = FT_Init_FreeType(&m_library);
0046     if (error) {
0047         qDebug("Freetype initialization error %i.", error);
0048         m_library = nullptr;
0049     }
0050 }
0051 
0052 FontPlugin::~FontPlugin()
0053 {
0054     FT_Done_FreeType(m_library);
0055     m_library = nullptr;
0056 
0057 }
0058 
0059 QString FontPlugin::processFile(BatchRenamer *b, int index, const QString &filenameOrToken, EPluginType)
0060 {
0061     QString token(filenameOrToken.toLower());
0062     QString filename = (*b->files())[index].srcUrl().path();
0063 
0064     if (!this->supports(token)) {
0065         return QString("");
0066     }
0067 
0068     if (!m_library) {
0069         return QString("Cannot initialize FreeType");
0070     }
0071 
0072     FT_Face face;
0073     FT_Error error = FT_New_Face(m_library,
0074                                  filename.toUtf8().data(),
0075                                  0,
0076                                  &face);
0077     QString result = QString("");
0078 
0079     if (error == FT_Err_Unknown_File_Format) {
0080         face = nullptr;
0081         result = QString("Unknown font file format error: %1").arg(error);
0082     } else if (error) {
0083         face = nullptr;
0084         result = QString("Unknown error: %1.").arg(error);
0085     } else {
0086         if (token == "fontpostscript") {
0087             result = QString::fromLocal8Bit(FT_Get_Postscript_Name(face));
0088         } else if (token == "fontfamily" && face->family_name) {
0089             result = QString::fromLocal8Bit(face->family_name);
0090         } else if (token == "fontstyle" && face->style_name) {
0091             result = QString::fromLocal8Bit(face->style_name);
0092         }
0093     }
0094 
0095     if (face) {
0096         FT_Done_Face(face);
0097     }
0098 
0099     return result;
0100 }