示例#1
0
        public BDFGlyph getGlyph(char ch)
        {
            BDFGlyph g = glyphMapper[(ushort)ch];

            if (g == null)
            {
                g = NotDefGlyph;
            }
            return(g);
        }
示例#2
0
        public int charWidth(char ch)
        {
            BDFGlyph g = font.getGlyph(ch);

            if (g != null)
            {
                BDFParser.Rectangle r = g.getBbx();
                r.width = (r.width + 1) & ~1;
                return(r.width);
            }
            return(0);
        }
示例#3
0
        public int[] charsWidths(char[] chars, int start, int length)
        {
            int[] advances = new int[length];
            int   adv_idx  = 0;

            for (int i = start; i < start + length; i++)
            {
                BDFGlyph glyph = font.getGlyph(chars[i]);
                if (adv_idx == 0)
                {
                    advances[adv_idx++] = glyph.getDWidth().width;
                }
                else
                {
                    advances[adv_idx++] = advances[adv_idx - 1] + glyph.getDWidth().width;
                }
            }

            return(advances);
        }
示例#4
0
 public void setGlyphs(BDFGlyph[] glyphs)
 {
     this.glyphs = glyphs;
     for (int i = 0; i < glyphs.Length; i++)
     {
         glyphs[i].init(this);
         if (glyphs[i].encoding != -1)
         {
             glyphMapper[glyphs[i].encoding] = glyphs[i];
         }
         else
         {
             if (glyphs[i].name.ToLower() == ".notdef")
             {
                 NotDefGlyph = glyphs[i];
             }
             else
             {
                 UnknownGlyphs.Add(glyphs[i].name, glyphs[i]);
             }
         }
     }
 }
示例#5
0
 public void setGlyphs(BDFGlyph[] glyphs)
 {
     this.glyphs = glyphs;
     for (int i = 0; i < glyphs.Length; i++)
     {
         glyphs[i].init(this);
         if (glyphs[i].encoding != -1)
         {
             glyphMapper[glyphs[i].encoding] = glyphs[i];
         }
         else
         {
             if (glyphs[i].name.ToLower() == ".notdef")
             {
                 NotDefGlyph = glyphs[i];
             }
             else
             {
                 UnknownGlyphs.Add(glyphs[i].name, glyphs[i]);
             }
         }
     }
 }
示例#6
0
文件: BDFParser.cs 项目: zer09/Cosmos
        public BDFFontContainer LoadFont()
        {
            BDFFontContainer font = null;

            int[]         version  = null;
            List <String> comments = new List <string>();

            String[]        fontName       = null;
            int[]           size           = null;
            int[]           bBox           = null;
            int             contentVersion = 0;
            int             metrics        = 0;
            List <String>   properties     = new List <string>();
            List <BDFGlyph> chars          = new List <BDFGlyph>();
            FontStyle       style          = FontStyle.Normal;
            string          s = "";

            s = inStream.ReadLine();
            if (s.Substring(0, 9).ToLower() == "startfont")
            {
                version    = new int[2];
                version[0] = int.Parse(s.Substring(10, 1));
                version[1] = int.Parse(s.Substring(12, 1));
            }
            else
            {
                throw new Exception("Unable to find start of the font");
            }
            while (!inStream.EndOfStream)
            {
                System.GC.Collect();
                s = inStream.ReadLine();
                if (s.Length > 4)
                {
                    if (s.Substring(0, 4).ToLower() == "size")
                    {
                        string[] s2 = s.Substring(5).Split(new char[] { ' ' });
                        size = new int[4];
                        if (s2.Length == 3)
                        {
                            size[3] = 1;
                        }
                        else
                        {
                            size[3] = int.Parse(s2[3]);
                        }
                        size[0] = int.Parse(s2[0]);
                        size[1] = int.Parse(s2[1]);
                        size[2] = int.Parse(s2[2]);
                        continue;
                    }
                    else if (s.Substring(0, 4).ToLower() == "font")
                    {
                        if (s.Substring(4, 1) == " ") // font command
                        {
                            fontName = new string[14];
                            String[] split = s.Trim().Split(new char[] { '-' });
                            Array.Copy(split, 1, fontName, 0, split.Length - 2);
                            continue;
                        }
                        else if (s.Length > 15)
                        {
                            if (s.Substring(0, 15).ToLower() == "fontboundingbox")
                            {
                                string[] s2 = s.Substring(16).Split(new char[] { ' ' });
                                bBox = new int[4];
                                if (s2.Length != 4)
                                {
                                    throw new Exception("Unknown formatting!");
                                }
                                bBox[0] = int.Parse(s2[0]);
                                bBox[1] = int.Parse(s2[1]);
                                bBox[2] = int.Parse(s2[2]);
                                bBox[3] = int.Parse(s2[3]);
                                continue;
                            }
                        }
                    }
                    else if (s.Length > 5)
                    {
                        if (s.Substring(0, 5).ToLower() == "chars")
                        {
                            int           chrs = int.Parse(s.Substring(6));
                            StringBuilder sb;

                            for (uint i = 0; i < chrs; i++)
                            {
                                System.GC.Collect();
                                s = inStream.ReadLine();
                                BDFGlyph g = null;

                                if (s.Length < 9 || s.Substring(0, 9).ToLower() != "startchar")
                                {
                                    throw new Exception("Expected StartChar, but didn't get it");
                                }
                                g = new BDFGlyph(s.Substring(10));
                                while (true)
                                {
                                    s = inStream.ReadLine();
                                    if (s.Length > 3)
                                    {
                                        if (s.Substring(0, 3).ToLower() == "bbx")
                                        {
                                            string[] strs = s.Substring(4).Split(new char[] { ' ' });
                                            if (strs.Length != 4)
                                            {
                                                throw new Exception("Error when loading bbx!");
                                            }
                                            g.setBBX(int.Parse(strs[2]), int.Parse(strs[3]), int.Parse(strs[0]), int.Parse(strs[1]));
                                            continue;
                                        }
                                        else if (s.Length >= 6)
                                        {
                                            if (s.Substring(0, 6).ToLower() == "swidth")
                                            {
                                                string[] s2 = s.Substring(7).Split(new char[] { ' ' });
                                                if (s2.Length != 2)
                                                {
                                                    throw new Exception("Error when loading swidth!");
                                                }
                                                g.setSWidth(int.Parse(s2[0]), int.Parse(s2[1]));
                                                continue;
                                            }
                                            else if (s.Substring(0, 6).ToLower() == "dwidth")
                                            {
                                                string[] s2 = s.Substring(7).Split(new char[] { ' ' });
                                                if (s2.Length != 2)
                                                {
                                                    throw new Exception("Error when loading dwidth!");
                                                }
                                                g.setDWidth(int.Parse(s2[0]), int.Parse(s2[1]));
                                                continue;
                                            }
                                            else if (s.Substring(0, 6).ToLower() == "bitmap")
                                            {
                                                s  = inStream.ReadLine();
                                                sb = new StringBuilder(200);
                                                if (s.Length == 2)
                                                {
                                                    while (s.Length == 2)
                                                    {
                                                        sb.Append(s);
                                                        s = inStream.ReadLine();
                                                    }
                                                }
                                                else if (s.Length == 4)
                                                {
                                                    while (s.Length == 4)
                                                    {
                                                        sb.Append(s);
                                                        s = inStream.ReadLine();
                                                    }
                                                }
                                                else
                                                {
                                                    throw new Exception("Unknown depth");
                                                }

                                                g.setRawData(sb.ToString());
                                                if (s.ToLower() != "endchar")
                                                {
                                                    throw new Exception("Unknown end to the bitmap");
                                                }
                                                break;
                                            }
                                            else if (s.Length > 7)
                                            {
                                                if (s.Substring(0, 7).ToLower() == "vvector")
                                                {
                                                    // We don't take this into account.
                                                    continue;
                                                }
                                                else if (s.Substring(0, 7).ToLower() == "dwidth1")
                                                {
                                                    // We don't take this into account.
                                                    continue;
                                                }
                                                else if (s.Substring(0, 7).ToLower() == "swidth1")
                                                {
                                                    // We don't take this into account.
                                                    continue;
                                                }
                                                else if (s.Substring(0, 7).ToLower() == "endchar")
                                                {
                                                    throw new Exception("ERROR: Character didn't include a bitmap!");
                                                }
                                                else if (s.Length > 8)
                                                {
                                                    if (s.Substring(0, 8).ToLower() == "encoding")
                                                    {
                                                        g.encoding = int.Parse(s.Substring(9));
                                                        continue;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    throw new Exception("Unknown Property");
                                }
                                chars.Add(g);
                            }

                            if (inStream.ReadLine().ToLower() != "endfont")
                            {
                                throw new Exception("Error when loading chars, missing endfont message.");
                            }
                            break;
                        }
                        else if (s.Length > 7)
                        {
                            if (s.Substring(0, 7).ToLower() == "comment")
                            {
                                comments.Add(s.Substring(9, (s.Length - 10)));
                                continue;
                            }
                            else if (s.Length > 10)
                            {
                                if (s.Substring(0, 10).ToLower() == "metricset")
                                {
                                    metrics = int.Parse(s.Substring(11, 1));
                                    continue;
                                }
                                else if (s.Length > 14)
                                {
                                    if (s.Substring(0, 14).ToLower() == "contentversion")
                                    {
                                        contentVersion = int.Parse(s.Substring(15));
                                        continue;
                                    }
                                    else if (s.Length > 15)
                                    {
                                        if (s.Substring(0, 15).ToLower() == "startproperties")
                                        {
                                            int props = int.Parse(s.Substring(16));
                                            for (uint i = 0; i < props; i++)
                                            {
                                                properties.Add(inStream.ReadLine());
                                            }
                                            s = inStream.ReadLine();
                                            if (s.ToLower() != "endproperties")
                                            {
                                                throw new Exception("An error occured while loading properties!");
                                            }
                                            continue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                throw new Exception("Unknown command");
            }

            if ("i".Equals(fontName[BDFFontContainer.SLANT], StringComparison.InvariantCultureIgnoreCase) ||
                "o".Equals(fontName[BDFFontContainer.SLANT], StringComparison.InvariantCultureIgnoreCase))
            {
                style = FontStyle.Italic;
            }
            if ("bold".Equals(fontName[BDFFontContainer.WEIGHT], StringComparison.InvariantCultureIgnoreCase))
            {
                style |= FontStyle.Bold;
            }

            font = new BDFFontContainer(fontName, style, size[0]);
            font.setBoundingBox(bBox[0], bBox[1], bBox[2], bBox[3]);
            font.setResolution(size[1], size[2]);
            font.setComments(comments.ToArray());
            font.setProperties(properties.ToArray());

            if (size != null && size.Length == 4)
            {
                font.setDepth(size[3]);
            }

            font.setGlyphs(chars.ToArray());
            return(font);
        }
示例#7
0
        public BDFFontContainer LoadFont()
        {
            BDFFontContainer font = null;
            int[] version = null;
            List<String> comments = new List<string>();
            String[] fontName = null;
            int[] size = null;
            int[] bBox = null;
            int contentVersion = 0;
            int metrics = 0;
            List<String> properties = new List<string>();
            List<BDFGlyph> chars = new List<BDFGlyph>();
            FontStyle style = FontStyle.Normal;
            string s = "";

            s = inStream.ReadLine();
            if (s.Substring(0, 9).ToLower() == "startfont")
            {
                version = new int[2];
                version[0] = int.Parse(s.Substring(10, 1));
                version[1] = int.Parse(s.Substring(12, 1));
            }
            else
            {
                throw new Exception("Unable to find start of the font");
            }
            while (!inStream.EndOfStream)
            {
                System.GC.Collect();
                s = inStream.ReadLine();
                if (s.Length > 4)
                {
                    if (s.Substring(0, 4).ToLower() == "size")
                    {
                        string[] s2 = s.Substring(5).Split(new char[] { ' ' });
                        size = new int[4];
                        if (s2.Length == 3)
                        {
                            size[3] = 1;
                        }
                        else
                        {
                            size[3] = int.Parse(s2[3]);
                        }
                        size[0] = int.Parse(s2[0]);
                        size[1] = int.Parse(s2[1]);
                        size[2] = int.Parse(s2[2]);
                        continue;
                    }
                    else if (s.Substring(0, 4).ToLower() == "font")
                    {
                        if (s.Substring(4, 1) == " ") // font command
                        {
                            fontName = new string[14];
                            String[] split = s.Trim().Split(new char[] { '-' });
                            Array.Copy(split, 1, fontName, 0, split.Length - 2);
                            continue;
                        }
                        else if (s.Length > 15)
                        {
                            if (s.Substring(0, 15).ToLower() == "fontboundingbox")
                            {
                                string[] s2 = s.Substring(16).Split(new char[] { ' ' });
                                bBox = new int[4];
                                if (s2.Length != 4)
                                {
                                    throw new Exception("Unknown formatting!");
                                }
                                bBox[0] = int.Parse(s2[0]);
                                bBox[1] = int.Parse(s2[1]);
                                bBox[2] = int.Parse(s2[2]);
                                bBox[3] = int.Parse(s2[3]);
                                continue;
                            }
                        }
                    }
                    else if (s.Length > 5)
                    {
                        if (s.Substring(0, 5).ToLower() == "chars")
                        {
                            int chrs = int.Parse(s.Substring(6));
                            StringBuilder sb;

                            for (uint i = 0; i < chrs; i++)
                            {
                                System.GC.Collect();
                                s = inStream.ReadLine();
                                BDFGlyph g = null;
                                
                                if (s.Length < 9 || s.Substring(0, 9).ToLower() != "startchar")
                                {
                                    throw new Exception("Expected StartChar, but didn't get it");
                                }
                                g = new BDFGlyph(s.Substring(10));
                                while (true)
                                {
                                    s = inStream.ReadLine();
                                    if (s.Length > 3)
                                    {
                                        if (s.Substring(0, 3).ToLower() == "bbx")
                                        {
                                            string[] strs = s.Substring(4).Split(new char[] { ' ' });
                                            if (strs.Length != 4)
                                            {
                                                throw new Exception("Error when loading bbx!");
                                            }
                                            g.setBBX(int.Parse(strs[2]), int.Parse(strs[3]), int.Parse(strs[0]), int.Parse(strs[1]));
                                            continue;
                                        }
                                        else if (s.Length >= 6)
                                        {
                                            if (s.Substring(0, 6).ToLower() == "swidth")
                                            {
                                                string[] s2 = s.Substring(7).Split(new char[] { ' ' });
                                                if (s2.Length != 2)
                                                {
                                                    throw new Exception("Error when loading swidth!");
                                                }
                                                g.setSWidth(int.Parse(s2[0]), int.Parse(s2[1]));
                                                continue;
                                            }
                                            else if (s.Substring(0, 6).ToLower() == "dwidth")
                                            {
                                                string[] s2 = s.Substring(7).Split(new char[] { ' ' });
                                                if (s2.Length != 2)
                                                {
                                                    throw new Exception("Error when loading dwidth!");
                                                }
                                                g.setDWidth(int.Parse(s2[0]), int.Parse(s2[1]));
                                                continue;
                                            }
                                            else if (s.Substring(0, 6).ToLower() == "bitmap")
                                            {
                                                s = inStream.ReadLine();
                                                sb = new StringBuilder(200);
                                                if (s.Length == 2)
                                                {
                                                    while (s.Length == 2)
                                                    {
                                                        sb.Append(s);
                                                        s = inStream.ReadLine();
                                                    }
                                                }
                                                else if (s.Length == 4)
                                                {
                                                    while (s.Length == 4)
                                                    {
                                                        sb.Append(s);
                                                        s = inStream.ReadLine();
                                                    }
                                                }
                                                else
                                                {
                                                    throw new Exception("Unknown depth");
                                                }

                                                g.setRawData(sb.ToString());
                                                if (s.ToLower() != "endchar")
                                                {
                                                    throw new Exception("Unknown end to the bitmap");
                                                }
                                                break;
                                            }
                                            else if (s.Length > 7)
                                            {
                                                if (s.Substring(0, 7).ToLower() == "vvector")
                                                {
                                                    // We don't take this into account.
                                                    continue; 
                                                }
                                                else if (s.Substring(0, 7).ToLower() == "dwidth1")
                                                {
                                                    // We don't take this into account.
                                                    continue; 
                                                }
                                                else if (s.Substring(0, 7).ToLower() == "swidth1")
                                                {
                                                    // We don't take this into account.
                                                    continue; 
                                                }
                                                else if (s.Substring(0, 7).ToLower() == "endchar")
                                                {
                                                    throw new Exception("ERROR: Character didn't include a bitmap!");
                                                }
                                                else if (s.Length > 8)
                                                {
                                                    if (s.Substring(0, 8).ToLower() == "encoding")
                                                    {
                                                        g.encoding = int.Parse(s.Substring(9));
                                                        continue;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    throw new Exception("Unknown Property");
                                }
                                chars.Add(g);
                            }

                            if (inStream.ReadLine().ToLower() != "endfont")
                            {
                                throw new Exception("Error when loading chars, missing endfont message.");
                            }
                            break;
                        }
                        else if (s.Length > 7)
                        {
                            if (s.Substring(0, 7).ToLower() == "comment")
                            {
                                comments.Add(s.Substring(9, (s.Length - 10)));
                                continue;
                            }
                            else if (s.Length > 10)
                            {
                                if (s.Substring(0, 10).ToLower() == "metricset")
                                {
                                    metrics = int.Parse(s.Substring(11,1));
                                    continue;
                                }
                                else if (s.Length > 14)
                                {
                                    if (s.Substring(0, 14).ToLower() == "contentversion")
                                    {
                                        contentVersion = int.Parse(s.Substring(15));
                                        continue;
                                    }
                                    else if (s.Length > 15)
                                    {
                                        if (s.Substring(0, 15).ToLower() == "startproperties")
                                        {
                                            int props = int.Parse(s.Substring(16));
                                            for (uint i = 0; i < props; i++)
                                            {
                                                properties.Add(inStream.ReadLine());
                                            }
                                            s = inStream.ReadLine();
                                            if (s.ToLower() != "endproperties")
                                            {
                                                throw new Exception("An error occured while loading properties!");
                                            }
                                            continue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                throw new Exception("Unknown command");
            }

            if ("i".Equals(fontName[BDFFontContainer.SLANT], StringComparison.InvariantCultureIgnoreCase) ||
                "o".Equals(fontName[BDFFontContainer.SLANT], StringComparison.InvariantCultureIgnoreCase))
            {
                style = FontStyle.Italic;
            }
            if ("bold".Equals(fontName[BDFFontContainer.WEIGHT], StringComparison.InvariantCultureIgnoreCase))
                style |= FontStyle.Bold;

            font = new BDFFontContainer(fontName, style, size[0]);
            font.setBoundingBox(bBox[0], bBox[1], bBox[2], bBox[3]);
            font.setResolution(size[1], size[2]);
            font.setComments(comments.ToArray());
            font.setProperties(properties.ToArray());

            if (size != null && size.Length == 4)
                font.setDepth(size[3]);

            font.setGlyphs(chars.ToArray());
            return font;
        }