Skip to content

Issue in PathFromGlypth #129

Description

@myailabs2020

Below is the code snippet that we have written to generate path from glyph.
GlyphToPath works only for 95% of the characters, For some character, it throws array out of index at Line @46. Kindly let me know if you have better approach get a path from Glyph

` private string GlyphToPath(uint inGlyphIndex, bool inSingleStroke)
{
StringBuilder strPath = new StringBuilder();
string glyphPath = string.Empty;

        int flag;
        int flag2;
        int i = 0;
        int lSegmentCount;
        int total = 0;
        bool bClosed;
        FTVector pointFirst;
        FTVector point1;
        FTVector point2;
        FTVector point3;
        FTVector point4;
        string calculatedPath = string.Empty;
        this.FontFace.LoadGlyph(inGlyphIndex, LoadFlags.NoScale, LoadTarget.Normal);

        for (int c = 0; c < this.FontFace.Glyph.Outline.ContoursCount; c++)
        {
            lSegmentCount = 0;
            bClosed = false;
            total = this.FontFace.Glyph.Outline.Contours[c];

            pointFirst = this.FontFace.Glyph.Outline.Points[i];
            calculatedPath = "M " + pointFirst.X.Value + " " + (-pointFirst.Y.Value + this.FontFace.BBox.Top) + " ";
            strPath.Append(calculatedPath);
            for (; i < total;)
            {
                point1 = this.FontFace.Glyph.Outline.Points[i];
                flag = this.FontFace.Glyph.Outline.Tags[i];
                flag2 = this.FontFace.Glyph.Outline.Tags[i + 1];
                i++;
                if ((flag == 1) && (flag2 == 1))
                {
                    point2 = this.FontFace.Glyph.Outline.Points[i];
                    calculatedPath = "L " + point2.X.Value + " " + (-point2.Y.Value + this.FontFace.BBox.Top) + " ";
                    strPath.Append(calculatedPath);
                    lSegmentCount++;
                }
                else if (flag == 1)
                {
                    point2 = this.FontFace.Glyph.Outline.Points[i];
                    i++;
                    if (i > total)
                    {
                        point3 = new FTVector(0, 0);
                    }
                    else
                    {
                        point3 = this.FontFace.Glyph.Outline.Points[i]; // This is line @ 46 throws array out of index exception some character
                    }
                    i++;
                    if (i <= total)
                    {
                        point4 = this.FontFace.Glyph.Outline.Points[i];
                        calculatedPath = "C " + point2.X.Value + " " + (-point2.Y.Value + this.FontFace.BBox.Top) + " " + point3.X.Value + " " + (-point3.Y.Value + this.FontFace.BBox.Top) + " " + point4.X.Value + " " + (-point4.Y.Value + this.FontFace.BBox.Top) + " ";
                        strPath.Append(calculatedPath);
                        lSegmentCount++;
                    }
                    else
                    {
                        calculatedPath = "C " + point2.X.Value + " " + (-point2.Y.Value + this.FontFace.BBox.Top) + " " + point3.X.Value + " " + (-point3.Y.Value + this.FontFace.BBox.Top) + " " + pointFirst.X.Value + " " + (-pointFirst.Y.Value + this.FontFace.BBox.Top) + " ";
                        strPath.Append(calculatedPath);
                        lSegmentCount++;
                        bClosed = true;
                    }
                }
                else
                {
                    break;
                }
            }
            i = this.FontFace.Glyph.Outline.Contours[c] + 1;
            if ((bClosed == false) && (lSegmentCount > 1) && (inSingleStroke == false))
            {
                calculatedPath = "L " + pointFirst.X.Value + " " + (-pointFirst.Y.Value + this.FontFace.BBox.Top) + " ";
               strPath.Append(calculatedPath);
            }

        }
        strPath.Append("Z");
        glyphPath = strPath.ToString();

        return glyphPath;
    }`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions