Exemple #1
0
    //---------------------------------------------------------------------
    void FontManager::parseAttribute(const String& line, FontPtr& pFont)
    {
        vector<String>::type params = StringUtil::split(line);
        String& attrib = params[0];
        StringUtil::toLowerCase(attrib);
        if (attrib == "type")
        {
            // Check params
            if (params.size() != 2)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            StringUtil::toLowerCase(params[1]);
            if (params[1] == "truetype")
            {
                pFont->setType(FT_TRUETYPE);
            }
            else
            {
                pFont->setType(FT_IMAGE);
            }

        }
        else if (attrib == "source")
        {
            // Check params
            if (params.size() != 2)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            pFont->setSource(params[1]);
        }
        else if (attrib == "glyph")
        {
            // Check params
            if (params.size() != 6)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            // Support numeric and character glyph specification
            Font::CodePoint cp;
            if (params[1].at(0) == 'u' && params[1].size() > 1)
            {
                // Unicode glyph spec
                String trimmed = params[1].substr(1);
                cp = StringConverter::parseUnsignedInt(trimmed);
            }
            else
            {
                // Direct character
                cp = params[1].at(0);
            }
            pFont->setGlyphTexCoords(
                cp, 
                StringConverter::parseReal(params[2]),
                StringConverter::parseReal(params[3]),
                StringConverter::parseReal(params[4]),
                StringConverter::parseReal(params[5]), 1.0 ); // assume image is square
        }
        else if (attrib == "size")
        {
            // Check params
            if (params.size() != 2)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            pFont->setTrueTypeSize(
                StringConverter::parseReal(params[1]));
        }
        else if (attrib == "character_spacer")
        {
            // Check params
            if (params.size() != 2)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            pFont->setCharacterSpacer(
                StringConverter::parseUnsignedInt(params[1]));
        }
        else if (attrib == "resolution")
        {
            // Check params
            if (params.size() != 2)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            pFont->setTrueTypeResolution(
                (uint)StringConverter::parseReal(params[1]) );
        }
        else if (attrib == "antialias_colour")
        {
            // Check params
            if (params.size() != 2)
            {
                logBadAttrib(line, pFont);
                return;
            }
            // Set
            pFont->setAntialiasColour(StringConverter::parseBool(params[1]));
        }
        else if (attrib == "code_points")
        {
            for (size_t c = 1; c < params.size(); ++c)
            {
                String& item = params[c];
                StringVector itemVec = StringUtil::split(item, "-");
                if (itemVec.size() == 2)
                {
                    pFont->addCodePointRange(Font::CodePointRange(
                        StringConverter::parseUnsignedInt(itemVec[0]),
                        StringConverter::parseUnsignedInt(itemVec[1])));
                }
            }
        }

    }
void FontTranslator::parseAttribute(ScriptCompiler* compiler, FontPtr& pFont,
                                    PropertyAbstractNode* prop)
{
    String& attrib = prop->name;
    String val;

    if (attrib == "glyph")
    {
        float coords[4];
        if (prop->values.size() != 5 || !getString(prop->values.front(), &val) ||
            !getFloats(++prop->values.begin(), prop->values.end(), coords, 4))
        {
            compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line);
            return;
        }

        // Set
        // Support numeric and character glyph specification
        Font::CodePoint cp;
        if (val.size() > 1 && val[0] == 'u')
        {
            // Unicode glyph spec
            String trimmed = val.substr(1);
            cp = StringConverter::parseUnsignedInt(trimmed);
        }
        else
        {
            // Direct character
            cp = val[0];
        }
        pFont->setGlyphTexCoords(cp, coords[0], coords[1], coords[2], coords[3],
                                 1.0); // assume image is square
    }
    else if (attrib == "antialias_colour")
    {
        bool flag;
        if (prop->values.empty() || !getBoolean(prop->values.front(), &flag))
        {
            compiler->addError(ScriptCompiler::CE_STRINGEXPECTED, prop->file, prop->line);
            return;
        }
        pFont->setAntialiasColour(flag);
    }
    else if (attrib == "code_points")
    {
        if (prop->values.empty())
        {
            compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line);
            return;
        }

        for (auto& v : prop->values)
        {

            bool succ = getString(v, &val);
            StringVector itemVec = StringUtil::split(val, "-");
            if (succ && itemVec.size() == 2)
            {
                pFont->addCodePointRange(
                    Font::CodePointRange(StringConverter::parseUnsignedInt(itemVec[0]),
                                         StringConverter::parseUnsignedInt(itemVec[1])));
            }
        }
    }
    else if (prop->values.empty() || !getString(prop->values.front(), &val) ||
             !pFont->setParameter(attrib, val))
    {
        compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, prop->file, prop->line);
    }
}