コード例 #1
0
ファイル: hgBMFont.cs プロジェクト: NNNIC/hug-unity-browser
    void Parse()
    {
        bm = new BMFONTDATA();

        Func <string, int> ip = (val) => {
            return(int.Parse(val));
        };

        int pagecnt = 0;
        int charcnt = 0;
        int kerncnt = 0;
        var rs      = new ReadText(m_bmFontTxt.ToString());

        while (true)
        {
            var line = rs.GetLine();
            if (string.IsNullOrEmpty(line))
            {
                break;
            }
            var parts = line.Split(' ');
            if (parts == null || parts.Length == 0)
            {
                continue;
            }
            if (parts[0] == "info")
            {
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    switch (k)
                    {
                    case "face": bm.face = v; break;

                    case "size": bm.size = ip(v); break;

                    case "padding":
                    {
                        bm.padding = new int[4];
                        var es = v.Split(',');
                        for (int i = 0; i < 4; i++)
                        {
                            bm.padding[i] = int.Parse(es[i]);
                        }
                        break;
                    }

                    case "spacing":
                    {
                        bm.spacing = new int[2];
                        var es = v.Split(',');
                        for (int i = 0; i < 2; i++)
                        {
                            bm.spacing[i] = int.Parse(es[i]);
                        }
                        break;
                    }

                    case "outline": bm.outline = ip(v); break;
                    }
                }
            }
            if (parts[0] == "common")
            {
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    switch (k)
                    {
                    case "lineHieht": bm.lineHieht = ip(v); break;

                    case "base": bm._base = ip(v); break;

                    case "scaleW": bm.scaleW = ip(v); break;

                    case "scaleH": bm.scaleH = ip(v); break;

                    case "pages":
                        bm._pages = ip(v); break;

                    case "packed": bm.packed = ip(v); break;

                    case "alphaChnl": bm.alphaChnl = ip(v); break;

                    case "redChnl": bm.redChnl = ip(v); break;

                    case "greenChnl": bm.greenChnl = ip(v); break;

                    case "blueChnl": bm.blueChnl = ip(v); break;
                    }
                }
            }
            if (parts[0] == "page")
            {
                if (bm.pages == null || bm.pages.Length == 0)
                {
                    bm.pages = new BMFONTDATA.PAGE[bm._pages];
                }

                var np = new BMFONTDATA.PAGE();
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    switch (k)
                    {
                    case "id": np.id = ip(v); break;

                    case "file": np.file = v;     break;
                    }
                }
                Debug.Log(line);
                bm.pages[pagecnt++] = np;
            }
            if (parts[0] == "chars")
            {
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    if (k == "count")
                    {
                        int num = ip(v);
                        bm.chars = new BMFONTDATA.CHAR[num];
                    }
                }
            }
            if (parts[0] == "char")
            {
                var nc = new BMFONTDATA.CHAR();
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    switch (k)
                    {
                    case "id": nc.id = ip(v); break;

                    case "x": nc.x = ip(v); break;

                    case "y": nc.y = ip(v); break;

                    case "width": nc.width = ip(v); break;

                    case "height": nc.height = ip(v); break;

                    case "xoffset": nc.xoffset = ip(v); break;

                    case "yoffset": nc.yoffset = ip(v); break;

                    case "xadvance": nc.xadvance = ip(v); break;

                    case "page": nc.page = ip(v); break;

                    case "chnl": nc.chnl = ip(v); break;
                    }
                }
                bm.chars[charcnt++] = nc;
            }
            if (parts[0] == "kernings")
            {
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    if (k == "count")
                    {
                        int num = ip(v);
                        bm.kernings = new BMFONTDATA.KERNING[num];
                    }
                }
            }
            if (parts[0] == "kerning")
            {
                var nk = new BMFONTDATA.KERNING();
                foreach (var p in parts)
                {
                    var kv = p.Split('=');
                    if (kv.Length != 2)
                    {
                        continue;
                    }
                    var k = kv[0]; var v = kv[1];
                    switch (k)
                    {
                    case "first": nk.first = ip(v); break;

                    case "second": nk.second = ip(v); break;

                    case "amount": nk.amount = ip(v); break;
                    }
                }
                bm.kernings[kerncnt++] = nk;
            }
        }
    }