Пример #1
0
        // Creates a font out of the given xml file
        public static Font[] fromXMLFile(string fileName, float fontSize)
        {
            if(!File.Exists(fileName))
                throw new Exception("Font file [ "+fileName+" ] not found");

            // Variables
            XmlDocument	document=	new XmlDocument();
            FontGlyphCollection[]	glyphs;
            Font[]	fonts;
            XmlNodeList	xnl, xnl2;
            FileInfo	info=	new FileInfo(fileName);
            int[]	scan=	new int[3];
            int[]	size=	new int[3];
            int[]	temp=	new int[2];
            string[]	splits;

            document.LoadXml(File.ReadAllText(fileName));
            xnl=	document["data"].GetElementsByTagName("font");
            fonts=	new Font[xnl.Count];
            for(int h= 0; h< xnl.Count; h++)
            {
                fonts[h]=	new Font();
                fonts[h].pName=	xnl[h].Attributes["name"].Value;
                fonts[h].pOriginalSize=	fontSize;
                fonts[h].size=	fontSize;
                fonts[h].wordSpacing=	4;
                fonts[h].bitmap=	new Sdx.Bitmap(info.Directory+"\\"+xnl[h]["image"].Attributes["src"].Value);
                if(xnl[h]["defaults"]!= null)
                {
                    if(xnl[h]["defaults"].Attributes["scan"]!= null)
                    {
                        splits=	xnl[h]["defaults"].Attributes["scan"].Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        scan[0]=	int.Parse(splits[0]);
                        scan[1]=	int.Parse(splits[1]);
                        scan[2]=	1;
                    }
                    else
                        scan[2]=	0;
                    if(xnl[h]["defaults"].Attributes["size"]!= null)
                    {
                        splits=	xnl[h]["defaults"].Attributes["size"].Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        size[0]=	int.Parse(splits[0]);
                        size[1]=	int.Parse(splits[1]);
                        size[2]=	1;
                        fonts[h].maxSize=	new Size2(size[0], size[1]);
                    }
                    else
                        size[2]=	0;
                }
                xnl2=	xnl[h]["glyphs"].GetElementsByTagName("add");
                glyphs=	new FontGlyphCollection[xnl2.Count];
                for(int k= 0; k< xnl2.Count; k++)
                {
                    glyphs[k]=	new FontGlyphCollection();

                    glyphs[k].regular.character=	char.Parse(xnl2[k].Attributes["name"].Value);
                    glyphs[k].bold.character=	glyphs[k].regular.character;
                    glyphs[k].italic.character=	glyphs[k].regular.character;
                    glyphs[k].boldItalic.character=	glyphs[k].regular.character;
                    if(size[2]== 1)
                    {
                        glyphs[k].regular.size=	new Size2(size[0], size[1]);
                        glyphs[k].bold.size=	new Size2(size[0], size[1]);
                        glyphs[k].italic.size=	new Size2(size[0], size[1]);
                        glyphs[k].boldItalic.size=	new Size2(size[0], size[1]);
                    }
                    if(scan[2]== 1)
                    {
                        glyphs[k].pRegular=	(xnl2[k].Attributes["regular"]!= null);
                        glyphs[k].pBold=	(xnl2[k].Attributes["bold"]!= null);
                        glyphs[k].pItalic=	(xnl2[k].Attributes["italic"]!= null);
                        glyphs[k].pBoldItalic=	(xnl2[k].Attributes["boldItalic"]!= null);
                        if(glyphs[k].pRegular)
                        {
                            splits=	xnl2[k].Attributes["regular"].Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            glyphs[k].regular.location=	new Point2(int.Parse(splits[0])*scan[0], int.Parse(splits[1])*scan[1]);
                        }
                        if(glyphs[k].pBold)
                        {
                            splits=	xnl2[k].Attributes["bold"].Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            glyphs[k].bold.location=	new Point2(int.Parse(splits[0])*scan[0], int.Parse(splits[1])*scan[1]);
                        }
                        if(glyphs[k].pItalic)
                        {
                            splits=	xnl2[k].Attributes["italic"].Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            glyphs[k].italic.location=	new Point2(int.Parse(splits[0])*scan[0], int.Parse(splits[1])*scan[1]);
                        }
                        if(glyphs[k].pBoldItalic)
                        {
                            splits=	xnl2[k].Attributes["boldItalic"].Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            glyphs[k].boldItalic.location=	new Point2(int.Parse(splits[0])*scan[0], int.Parse(splits[1])*scan[1]);
                        }
                    }
                    glyphs[k].regular.yOffset=	64;
                    glyphs[k].bold.yOffset=	64;
                    glyphs[k].italic.yOffset=	64;
                    glyphs[k].boldItalic.yOffset=	64;
                    glyphs[k].regular.padding=	new Padding(0, 0, 0, 0);
                    glyphs[k].bold.padding=	new Padding(0, 0, 0, 0);
                    glyphs[k].italic.padding=	new Padding(0, 0, 0, 0);
                    glyphs[k].boldItalic.padding=	new Padding(0, 0, 0, 0);
                }
                for(int k= 0; k< glyphs.Length; k++)
                    fonts[h].glyphs.add(glyphs[k].regular.character, glyphs[k]);
                fonts[h].texture=	new Texture(fonts[h].bitmap);

                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.Repeat);
            }

            return fonts;
        }
Пример #2
0
 public TextRenderingHelper(Font font, FontGlyphCollection pmGlyph)
 {
     character=	pmGlyph.regular.character;
     lcoord=	new TexCoord((pmGlyph.regular.location.x)/(font.bitmap.Width), (pmGlyph.regular.location.y)/(font.bitmap.Height));
     rcoord=	new TexCoord((pmGlyph.regular.location.x+pmGlyph.regular.size.width)/(font.bitmap.Width), (pmGlyph.regular.location.y+pmGlyph.regular.size.height)/(font.bitmap.Height));
     glyph=	pmGlyph.regular;
 }