Пример #1
0
        public static Texture CreateTexture()
        {
            Texture tex = TextureManager.Instance.GetByName("Axiom/LightingTexture");

            if (tex == null)
            {
                byte[] fotbuf = new byte[128 * 128 * 4];
                for (int y = 0; y < 128; y++)
                {
                    for (int x = 0; x < 128; x++)
                    {
                        byte  alpha  = 0;
                        float radius = (x - 64) * (x - 64) + (y - 64) * (y - 64);
                        radius = 4000 - radius;
                        if (radius > 0)
                        {
                            alpha = (byte)((radius / 4000) * 255);
                        }
                        fotbuf[y * 128 * 4 + x * 4]     = 255;
                        fotbuf[y * 128 * 4 + x * 4 + 1] = 255;
                        fotbuf[y * 128 * 4 + x * 4 + 2] = 255;
                        fotbuf[y * 128 * 4 + x * 4 + 3] = alpha;
                    }
                }

                System.IO.MemoryStream stream = new System.IO.MemoryStream(fotbuf);
                Axiom.Media.Image      img    = Axiom.Media.Image.FromRawStream(stream, 128, 128, Axiom.Media.PixelFormat.A8R8G8B8);
                TextureManager.Instance.LoadImage("Axiom/LightingTexture", img, TextureType.TwoD, 0, 1, 1);

                tex = TextureManager.Instance.GetByName("Axiom/LightingTexture");
            }

            return(tex);
        }
Пример #2
0
        //protected System.Drawing.Font GetFontBySpacing(float lineSpacingPixel, FontStyle fontStyle) {
        //    // Compute size based on the regular font.  If we are using bold
        //    // and italic inline with the normal font, I don't want to switch
        //    // font sizes mid-line.
        //    int emHeight = fontFamily.GetEmHeight(FontStyle.Regular);
        //    int lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

        //    // this is the largest emSize that will fit
        //    float emSize = lineSpacingPixel * ((float)emHeight / lineSpacing);
        //    return GetFont(emSize, fontFlags);
        //}

        protected void CreateFontGlyphSet(string chars, int size, FontStyle fontStyle)
        {
            glyphData = new GlyphData[chars.Length];

            // used for calculating position in the image for rendering the characters
            float x = 0, y = 0;

            StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();

            format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;

            int emHeight    = fontFamily.GetEmHeight(FontStyle.Regular);
            int lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);
            int descent     = fontFamily.GetCellDescent(FontStyle.Regular);

            this.font = new System.Drawing.Font(fontFamily, size, fontStyle, GraphicsUnit.Pixel);
            log.InfoFormat("Code page for {0} = {1}", font, font.GdiCharSet);

            float lineSpacingPixel = font.Size * (float)lineSpacing / emHeight;
            float descentPixel     = font.Size * (float)descent / emHeight;

            this.cellDescent = descentPixel;
            this.ySpacing    = (float)Math.Ceiling(lineSpacingPixel);

            float height = ySpacing;

            int descentPixelCeil = (int)Math.Ceiling(descentPixel);

            int bitmapHeight = 512;
            int bitmapWidth  = 512;

            int charIndex = 0;
            int glyphSet  = 0;

            while (charIndex < chars.Length)
            {
                Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                // get a handles to the graphics context of the bitmap
                Graphics g = Graphics.FromImage(bitmap);
                g.PageUnit = GraphicsUnit.Pixel;
                // Ideally, gdi would handle alpha correctly, but it doesn't seem to
                g.Clear(System.Drawing.Color.FromArgb(0, 1, 0, 0));
                // these fonts better look good!
                // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.None;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                // g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;

                // Initialize up our x and y offset in this image
                x = 0;
                y = descentPixel;
                int firstChar = charIndex;

                Dictionary <char, Rect> glyphRects = new Dictionary <char, Rect>();
                // loop through each character in the glyph string and draw it to the bitmap
                while (charIndex < chars.Length)
                {
                    char c = chars[charIndex];

                    SizeF metrics = g.MeasureString(c.ToString(), font, 1024, format);
                    float width   = (float)Math.Ceiling(metrics.Width);

                    // are we gonna wrap?
                    if (x + width > bitmapWidth)
                    {
                        // increment the y coord and reset x to move to the beginning of next line
                        y += (height + InterGlyphPadSpace);
                        x  = 0;
                    }

                    if (y + height > bitmapHeight)
                    {
                        // need to break to a new image
                        break;
                    }

                    // draw the character
                    g.DrawString(c.ToString(), font, Brushes.White, x, y, format);

                    Rect rect = new Rect();

                    // calculate the texture coords for the character
                    // I think that these rectangles are not inclusive,
                    rect.Left   = x;
                    rect.Right  = rect.Left + width;
                    rect.Top    = y;
                    rect.Bottom = rect.Top + height;

                    // Stash the area that we drew into, so that we can
                    // use it later to construct the atlas.
                    glyphRects[c] = rect;

                    // increment X by the width of the current char
                    x += (width + InterGlyphPadSpace);
                    charIndex++;
                }
                // Ok, we are done with this texture
#if DEBUG_FONT_GLYPH
                {
                    // Save this for debugging
                    System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                    string     debugFileName = string.Format("{0}_{1}_{2}.{3}", fontFamily.Name, size, glyphSet, imgformat.ToString().ToLower());
                    FileStream debugStream   = new FileStream(debugFileName, FileMode.OpenOrCreate);
                    bitmap.Save(debugStream, imgformat);
                    debugStream.Close();
                    log.InfoFormat("Saved font file: {0}: {1}x{2}", debugFileName, bitmap.Width, bitmap.Height);
                }
#endif
                // save the image to a memory stream
                Stream stream = new MemoryStream();
                // flip the image
                bitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY);
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

                // destroy the bitmap
                bitmap.Dispose();

                // Bitmap headers are 54 bytes.. skip them
                stream.Position = BitmapHeaderSize;

                Axiom.Media.Image image       = Axiom.Media.Image.FromRawStream(stream, bitmapWidth, bitmapHeight, Axiom.Media.PixelFormat.A8R8G8B8);
                string            imgName     = String.Format("_font_glyphs_{0}_{1}", this.name, glyphSet);
                Texture           fontTexture = TextureManager.Instance.LoadImage(imgName, image);

                // Construct and populate the atlas and glyph data structures
                TextureAtlas glyphAtlas = AtlasManager.Instance.CreateAtlas(imgName, fontTexture);
                glyphAtlasList.Add(glyphAtlas);
                for (int i = firstChar; i < charIndex; i++)
                {
                    Rect        rect        = glyphRects[chars[i]];
                    TextureInfo textureInfo = glyphAtlas.DefineImage(chars[i].ToString(), rect);
                    glyphData[i].TextureInfo       = textureInfo;
                    glyphData[i].HorizontalAdvance = rect.Width;
                    glyphData[i].Character         = chars[i];
                    glyphMap[chars[i]]             = i;
                }
                glyphSet++;
            }

#if DEBUG_FONT_GLYPH
            {
                Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                // get a handles to the graphics context of the bitmap
                Graphics g = Graphics.FromImage(bitmap);
                g.PageUnit = GraphicsUnit.Pixel;
                // Ideally, gdi would handle alpha correctly, but it doesn't seem to
                g.Clear(System.Drawing.Color.FromArgb(0, 1, 0, 0));
                // these fonts better look good!
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.None;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                string           test_text = "\u0031\u0644\u0032\u0644\u0627\u0645\u0020\u0639\u0644\u064a\u0028\u0643\u0029\u0645\u002e\u0020\u0643\u064a\u0641\u0020\u0627\u0644\u062d\u0627\u0644\u0039";
                CharacterRange[] ranges    = new CharacterRange[test_text.Length];
                for (int i = 0; i < test_text.Length; ++i)
                {
                    ranges[i] = new CharacterRange(i, 1);
                }

                // format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DirectionRightToLeft;
                format.SetMeasurableCharacterRanges(ranges);
                System.Drawing.Region[] regions = g.MeasureCharacterRanges(test_text, font, new RectangleF(0, 0, 600, 400), format);
                foreach (System.Drawing.Region region in regions)
                {
                    log.InfoFormat("Region: {0}", region.GetBounds(g));
                }

                g.DrawString(test_text, font, Brushes.White, 100, 0, format);
                // save the image to a memory stream
                Stream stream = new MemoryStream();
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

                // Save this for debugging
                System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                string     debugFileName = string.Format("{0}_{1}_{2}.{3}", fontFamily.Name, size, glyphSet, imgformat.ToString().ToLower());
                FileStream debugStream   = new FileStream(debugFileName, FileMode.OpenOrCreate);
                bitmap.Save(debugStream, imgformat);
                debugStream.Close();
                log.InfoFormat("Saved font file: {0}: {1}x{2}", debugFileName, bitmap.Width, bitmap.Height);

                // destroy the bitmap
                bitmap.Dispose();
                stream.Close();
            }
#endif
        }