Ejemplo n.º 1
0
    private void UpdateSprite(CCFont.Char c, int index, Vector3 position)
    {
        // vertex order is
        // 01
        // 32
        Vector2 corner;

        corner.x      = c.uMin;
        corner.y      = c.vMax;
        uv[index]     = corner;
        corner.x      = c.uMax;
        uv[index + 1] = corner;
        corner.y      = c.vMin;
        uv[index + 2] = corner;
        corner.x      = c.uMin;
        uv[index + 3] = corner;

        position.x += c.xOffset;
        position.y += c.yOffset;

        vertices[index]     = position;
        position.x         += c.width;
        vertices[index + 1] = position;
        position.y         -= c.height;
        vertices[index + 2] = position;
        position.x         -= c.width;
        vertices[index + 3] = position;
    }
Ejemplo n.º 2
0
    private static CCFont.Char ParseGlyph(int width, int height, float scale)
    {
        ReadNextLine();
        CCFont.Char c = new CCFont.Char();
        c.kerningIds = new int[0];
        c.id         = currentLine["id"];
        float
            w = currentLine["width"],
            h = currentLine["height"];

        c.uMin = (currentLine["x"] + 0.5f) / width;
        c.uMax = c.uMin + w / width;
        c.vMin = (height - h - currentLine["y"] - 0.5f) / height;
        c.vMax = c.vMin + h / height;

        c.xOffset = currentLine["xoffset"] * scale;
        c.width   = w * scale;
        c.yOffset = -currentLine["yoffset"] * scale;
        c.height  = h * scale;

        c.advance = currentLine["xadvance"] * scale;

        return(c);
    }
Ejemplo n.º 3
0
    private void Import(string filePath)
    {
        lines            = File.ReadAllLines(filePath);
        currentLine      = new Dictionary <string, int>();
        currentLineIndex = 0;
        do
        {
            ReadNextLine();
        }while(currentLineType != null && currentLineType != "common");

        int
            pixelLineHeight = currentLine["lineHeight"],
            width           = currentLine["scaleW"],
            height          = currentLine["scaleH"];

        float
            baseline     = (float)currentLine["base"] / pixelLineHeight,
            pixelScale   = 1f / pixelLineHeight,
            leftMargin   = 0f,
            rightMargin  = 0f,
            topMargin    = 0f,
            bottomMargin = 0f;

        CCFont.Char[]      asciiGlyphs = new CCFont.Char['~' - ' '];
        List <CCFont.Char> otherGlyphs = new List <CCFont.Char>();

        // read character data
        do
        {
            ReadNextLine();
        }while(currentLineType != null && currentLineType != "chars");
        for (int i = 0, l = currentLine["count"]; i < l; i++)
        {
            CCFont.Char c = ParseGlyph(width, height, pixelScale);
            if (c.xOffset < leftMargin)
            {
                leftMargin = c.xOffset;
            }
            if (c.xOffset + c.width - c.advance > rightMargin)
            {
                rightMargin = c.xOffset + c.width - c.advance;
            }
            if (c.yOffset > topMargin)
            {
                topMargin = c.yOffset;
            }
            if (c.height - c.yOffset > bottomMargin)
            {
                bottomMargin = c.height - c.yOffset;
            }

            if (c.id == ' ')
            {
                // set space advance
                font.spaceAdvance = c.advance;
            }
            else if ('!' <= c.id && c.id <= '~')
            {
                asciiGlyphs[c.id - '!'] = c;
            }
            else if (c.id > 127)
            {
                // add everything else that isn't a control character
                otherGlyphs.Add(c);
            }
        }

        // set missing ASCII characters
        CCFont.Char missing = new CCFont.Char();
        missing.kerningIds = new int[0];
        for (int i = 0; i < '~' - ' '; i++)
        {
            if (asciiGlyphs[i] == null)
            {
                asciiGlyphs[i] = missing;
            }
        }

        // set font data
        font.pixelLineHeight = pixelLineHeight;
        font.baseline        = baseline;
        font.pixelScale      = pixelScale;
        font.leftMargin      = -leftMargin;
        font.rightMargin     = rightMargin;
        font.topMargin       = topMargin;
        font.bottomMargin    = bottomMargin - 1f;
        font.asciiChars      = asciiGlyphs;
        font.otherChars      = otherGlyphs.ToArray();
        Array.Sort(font.otherChars, new CharComparer());

        // add kernings
        do
        {
            ReadNextLine();
        }while(currentLineType != null && currentLineType != "kernings");
        if (currentLineType != null && currentLine["count"] > 0)
        {
            font.supportsKerning = true;
            int l = currentLine["count"];
            ReadNextLine();
            int          currentCharId = currentLine["first"];
            List <int>   kerningIds    = new List <int>();
            List <float> kernings      = new List <float>();
            for (int i = 0; i < l; i++)
            {
                if (currentCharId != currentLine["first"])
                {
                    CCFont.Char c = font[(char)currentCharId];
                    c.kerningIds = kerningIds.ToArray();
                    c.kernings   = kernings.ToArray();
                    Array.Sort(c.kerningIds, c.kernings);
                    kernings.Clear();
                    kerningIds.Clear();
                    currentCharId = currentLine["first"];
                }
                kerningIds.Add(currentLine["second"]);
                kernings.Add(currentLine["amount"] * pixelScale);
                ReadNextLine();
            }
        }
        else
        {
            font.supportsKerning = false;
        }

        currentLine = null;
    }