コード例 #1
0
ファイル: FontSystem.cs プロジェクト: kiates/SpriteFontPlus
        FontGlyph GetGlyphWithoutBitmap(GlyphCollection collection, int codepoint)
        {
            FontGlyph glyph = null;

            if (collection.Glyphs.TryGetValue(codepoint, out glyph))
            {
                return(glyph);
            }

            Font font;
            var  g = GetCodepointIndex(codepoint, out font);

            if (g == 0)
            {
                collection.Glyphs[codepoint] = null;
                return(null);
            }

            int advance, lsb, x0, y0, x1, y1;

            font.BuildGlyphBitmap(g, FontSize, font.Scale, &advance, &lsb, &x0, &y0, &x1, &y1);

            var pad    = Math.Max(FontGlyph.PadFromBlur(BlurAmount), FontGlyph.PadFromBlur(StrokeAmount));
            var gw     = x1 - x0 + pad * 2;
            var gh     = y1 - y0 + pad * 2;
            var offset = FontGlyph.PadFromBlur(BlurAmount);

            glyph = new FontGlyph {
                Font      = font,
                Codepoint = codepoint,
                Size      = FontSize,
                Index     = g,
                Bounds    = new Rectangle(0, 0, gw, gh),
                XAdvance  = (int)(font.Scale * advance * 10.0f),
                XOffset   = x0 - offset,
                YOffset   = y0 - offset
            };

            collection.Glyphs[codepoint] = glyph;

            return(glyph);
        }
コード例 #2
0
        public void RenderGlyph(GraphicsDevice device, FontGlyph glyph, int blurAmount, int strokeAmount)
        {
            var pad = Math.Max(FontGlyph.PadFromBlur(blurAmount), FontGlyph.PadFromBlur(strokeAmount));

            // Render glyph to byte buffer
            var bufferSize = glyph.Bounds.Width * glyph.Bounds.Height;
            var buffer     = _byteBuffer;

            if ((buffer == null) || (buffer.Length < bufferSize))
            {
                buffer      = new byte[bufferSize];
                _byteBuffer = buffer;
            }
            Array.Clear(buffer, 0, bufferSize);

            var g           = glyph.Index;
            var colorSize   = glyph.Bounds.Width * glyph.Bounds.Height;
            var colorBuffer = _colorBuffer;

            if ((colorBuffer == null) || (colorBuffer.Length < colorSize))
            {
                colorBuffer  = new Color[colorSize];
                _colorBuffer = colorBuffer;
            }

            fixed(byte *dst = &buffer[pad + pad *glyph.Bounds.Width])
            {
                glyph.Font.RenderGlyphBitmap(dst,
                                             glyph.Bounds.Width - pad * 2,
                                             glyph.Bounds.Height - pad * 2,
                                             glyph.Bounds.Width,
                                             g);
            }

            if (strokeAmount > 0)
            {
                var width  = glyph.Bounds.Width;
                var top    = width * strokeAmount;
                var bottom = (glyph.Bounds.Height - strokeAmount) * glyph.Bounds.Width;
                var right  = glyph.Bounds.Width - strokeAmount;
                var left   = strokeAmount;

                byte d;
                for (var i = 0; i < colorSize; ++i)
                {
                    var col   = buffer[i];
                    var black = 0;
                    if (col == 255)
                    {
                        colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = colorBuffer[i].A = 255;
                        continue;
                    }

                    if (i >= top)
                    {
                        black = buffer[i - top];
                    }
                    if (i < bottom)
                    {
                        d     = buffer[i + top];
                        black = ((255 - d) * black + 255 * d) / 255;
                    }
                    if (i % width >= left)
                    {
                        d     = buffer[i - strokeAmount];
                        black = ((255 - d) * black + 255 * d) / 255;
                    }
                    if (i % width < right)
                    {
                        d     = buffer[i + strokeAmount];
                        black = ((255 - d) * black + 255 * d) / 255;
                    }

                    if (black == 0)
                    {
                        if (col == 0)
                        {
                            colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = colorBuffer[i].A = 0; //black transparency to suit stroke
                            continue;
                        }
#if PREMULTIPLIEDALPHA
                        colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = colorBuffer[i].A = col;
#else
                        colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = 255;
                        colorBuffer[i].A = col;
#endif
                    }
                    else
                    {
                        if (col == 0)
                        {
                            colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = 0;
                            colorBuffer[i].A = (byte)black;
                            continue;
                        }

#if PREMULTIPLIEDALPHA
                        var alpha = ((255 - col) * black + 255 * col) / 255;
                        colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = (byte)((alpha * col) / 255);
                        colorBuffer[i].A = (byte)alpha;
#else
                        colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = col;
                        colorBuffer[i].A = (byte)(((255 - col) * black + 255 * col) / 255);
#endif
                    }
                }
            }
            else
            {
                if (blurAmount > 0)
                {
                    fixed(byte *bdst = &buffer[0])
                    {
                        Blur(bdst, glyph.Bounds.Width, glyph.Bounds.Height, glyph.Bounds.Width, blurAmount);
                    }
                }

                for (var i = 0; i < colorSize; ++i)
                {
                    var c = buffer[i];
#if PREMULTIPLIEDALPHA
                    colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = colorBuffer[i].A = c;
#else
                    colorBuffer[i].R = colorBuffer[i].G = colorBuffer[i].B = 255;
                    colorBuffer[i].A = c;
#endif
                }
            }

            // Write to texture
            if (Texture == null)
            {
                Texture = new Texture2D(device, Width, Height);
            }
#if TEXTURESETDATAEXT
            fixed(Color *p = colorBuffer)
#if FNA
            Texture.SetDataPointerEXT(0, glyph.Bounds, (IntPtr)p, colorSize * sizeof(Color));
#else
            Texture.SetDataEXT(0, 0, glyph.Bounds, (IntPtr)p, colorSize * sizeof(Color));
#endif
#else
            Texture.SetData(0, 0, glyph.Bounds, colorBuffer, 0, colorSize);
#endif
        }