Subversion Repositories tpanel

Rev

Rev 11 | Blame | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

#include "tfont.h"
#include "treadxml.h"
#include "tconfig.h"
#include "terror.h"

#include <include/core/SkFontStyle.h>

using std::string;
using std::map;
using std::pair;

TFont::TFont()
{
    DECL_TRACER("TFont::TFont()");
    initialize();
}

TFont::~TFont()
{
    DECL_TRACER("TFont::~TFont()");
}

void TFont::initialize()
{
    DECL_TRACER("TFont::initialize()");

    if (mFonts.size() > 0)
        mFonts.clear();

    // System fonts first
    FONT_T font;

    font.number = 1;
    font.faceIndex = 1;
    font.fullName = "Courier New";
    font.name = "Courier New";
    font.size = 9;
    font.subfamilyName = "normal";
    font.fileSize = 0;
    font.usageCount = 0;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 2;
    font.faceIndex = 2;
    font.size = 12;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 3;
    font.faceIndex = 3;
    font.size = 18;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 4;
    font.faceIndex = 4;
    font.size = 26;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 5;
    font.faceIndex = 5;
    font.size = 32;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 6;
    font.faceIndex = 6;
    font.size = 18;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 7;
    font.faceIndex = 7;
    font.size = 26;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 8;
    font.faceIndex = 8;
    font.size = 34;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 9;
    font.faceIndex = 9;
    font.fullName = "AMX Bold";
    font.name = "AMX Bold";
    font.size = 14;
    font.subfamilyName = "bold";
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 10;
    font.faceIndex = 10;
    font.size = 20;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 11;
    font.faceIndex = 11;
    font.size = 36;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 19;
    font.faceIndex = 19;
    font.fullName = "Arial";
    font.name = "Arial";
    font.size = 9;
    font.subfamilyName = "normal";
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 20;
    font.faceIndex = 20;
    font.size = 10;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 21;
    font.faceIndex = 21;
    font.size = 12;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 22;
    font.faceIndex = 22;
    font.size = 14;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 23;
    font.faceIndex = 23;
    font.size = 16;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 24;
    font.faceIndex = 24;
    font.size = 18;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 25;
    font.faceIndex = 25;
    font.size = 20;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 26;
    font.faceIndex = 26;
    font.size = 24;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 27;
    font.faceIndex = 27;
    font.size = 36;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 28;
    font.faceIndex = 28;
    font.fullName = "Arial Bold";
    font.name = "Arial Bold";
    font.size = 10;
    font.subfamilyName = "bold";
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    font.number = 29;
    font.faceIndex = 29;
    font.size = 8;
    mFonts.insert(pair<int, FONT_T>(font.number, font));

    // read the individual fonts from file
    TError::clear();
    string path = makeFileName(TConfig::getProjectPath(), "fnt.xma");

    if (!isValidFile())
    {
        MSG_ERROR("File " << path << " doesn't exist or is not readable!");
        TError::setError();
        return;
    }

    TReadXML reader(path);

    if (TError::isError())
    {
        MSG_ERROR("Stopped scanning file " << path << " because of previous errors!");
        return;
    }

    reader.findElement("fontList");

    if (!reader.success())
    {
        MSG_DEBUG("File does not contain the element \"fontList\"!");
        TError::setError();
        return;
    }

    mxml_node_t *node = reader.getFirstChild();

    while (node)
    {
        string ename = reader.getElementName(node);

        if (ename.compare("font") == 0)
        {
            mxml_node_t *n = reader.getFirstChild(node);
            string snum;
            FONT_T ft;

            if (n)
            {
                snum = reader.getAttributeFromNode(node, "number");

                if (!snum.empty())
                    ft.number = atoi(snum.c_str());
                else
                {
                    MSG_ERROR("Element " << ename << " contains no or invalid attribute!");
                    TError::setError();
                    return;
                }

                while (n)
                {
                    string e = reader.getElementName(n);

                    if (e.compare("file") == 0)
                        ft.file = reader.getTextFromNode(n);
                    else if (e.compare("fileSize") == 0)
                        ft.fileSize = reader.getIntFromNode(n);
                    else if (e.compare("faceIndex") == 0)
                        ft.faceIndex = reader.getIntFromNode(n);
                    else if (e.compare("name") == 0)
                        ft.name = reader.getTextFromNode(n);
                    else if (e.compare("subfamilyName") == 0)
                        ft.subfamilyName = reader.getTextFromNode(n);
                    else if (e.compare("fullName") == 0)
                        ft.fullName = reader.getTextFromNode(n);
                    else if (e.compare("size") == 0)
                        ft.size = reader.getIntFromNode(n);
                    else if (e.compare("usageCount") == 0)
                        ft.usageCount = reader.getIntFromNode(n);

                    n = reader.getNextChild(n);
                }

                mFonts.insert(pair<int, FONT_T>(ft.number, ft));
            }
        }

        node = reader.getNextChild();
    }
}

FONT_T TFont::getFont(int number)
{
    DECL_TRACER("TFont::getFont(int number)");

    if (mFonts.size() == 0)
    {
        MSG_WARNING("No fonts found!");
        return FONT_T();
    }

    map<int, FONT_T>::iterator iter = mFonts.find(number);

    if (iter == mFonts.end())
        return FONT_T();

    return iter->second;
}

FONT_STYLE TFont::getStyle(int number)
{
    DECL_TRACER("TFont::getStyle(int number)");

    map<int, FONT_T>::iterator iter = mFonts.find(number);

    if (iter == mFonts.end())
        return FONT_NONE;

    if (iter->second.subfamilyName.compare("Regular") == 0)
        return FONT_NORMAL;
    else if (iter->second.subfamilyName.compare("Italic") == 0)
        return FONT_ITALIC;
    else if (iter->second.subfamilyName.compare("Bold") == 0)
        return FONT_BOLD;
    else if (iter->second.subfamilyName.compare("Bold Italic") == 0)
        return FONT_BOLD_ITALIC;

    return FONT_NORMAL;
}

FONT_STYLE TFont::getStyle(FONT_T& font)
{
    DECL_TRACER("TFont::getStyle(int number)");

    if (font.subfamilyName.compare("Regular") == 0)
        return FONT_NORMAL;
    else if (font.subfamilyName.compare("Italic") == 0)
        return FONT_ITALIC;
    else if (font.subfamilyName.compare("Bold") == 0)
        return FONT_BOLD;
    else if (font.subfamilyName.compare("Bold Italic") == 0)
        return FONT_BOLD_ITALIC;

    return FONT_NORMAL;
}


#define MAX_FACES   10

sk_sp<SkTypeface> TFont::getTypeFace(int number)
{
    DECL_TRACER("TFont::getTypeFace(int number)");

    map<int, FONT_T>::iterator iter = mFonts.find(number);

    if (iter == mFonts.end())
    {
        MSG_ERROR("No font with index " << number << " found!");
        TError::setError();
        return sk_sp<SkTypeface>();
    }

    string path = TConfig::getProjectPath() + "/fonts/" + iter->second.file;
    sk_sp<SkTypeface> tf;
    MSG_TRACE("Loading font \"" << path << "\" ...");
    tf = SkTypeface::MakeFromFile(path.c_str(), iter->second.faceIndex);

    if (!tf)
    {
        MSG_ERROR("Error loading font \"" << path << "\"");
        TError::setError();
        return tf;
    }
    else
    {
        MSG_TRACE("Font \"" << path << "\" was loaded successfull.");
    }

    SkString sname;
    tf->getFamilyName(&sname);
    MSG_DEBUG("Found font name \"" << sname.c_str() << "\" with attributes: bold=" << ((tf->isBold())?"TRUE":"FALSE") << ", italic=" << ((tf->isItalic())?"TRUE":"FALSE") << ", fixed=" << ((tf->isFixedPitch())?"TRUE":"FALSE"));

    if (iter->second.name.compare(sname.c_str()) != 0)
    {
        MSG_WARNING("The loaded font \"" << sname.c_str() << "\" is not the wanted font \"" << iter->second.name << "\"!");
    }

    FONT_STYLE style = getStyle(iter->second);

    if (style == FONT_BOLD && tf->isBold())
        return tf;
    else if (style == FONT_ITALIC && tf->isItalic())
        return tf;
    else if (style == FONT_BOLD_ITALIC && tf->isBold() && tf->isItalic())
        return tf;
    else if (style == FONT_NORMAL && !tf->isBold() && !tf->isItalic())
        return tf;

    MSG_WARNING("The wanted font style " << iter->second.subfamilyName << " was not found!");
    return tf;
}