예제 #1
0
파일: BFont.cs 프로젝트: Phyyl/BFont
        private static void AddPadding(RenderGlyph renderGlyph, int padding)
        {
            if (padding == 0)
            {
                return;
            }

            int width  = Math.Max(0, renderGlyph.Bitmap.Width + padding * 2);
            int height = Math.Max(0, renderGlyph.Bitmap.Height + padding * 2);

            BBitmap result = new BBitmap(width, height);

            result.Draw(renderGlyph.Bitmap, padding, padding);

            renderGlyph.Info.XOffset += padding;
            renderGlyph.Info.YOffset  = height;

            renderGlyph.Bitmap = result;
        }
예제 #2
0
파일: BFont.cs 프로젝트: Phyyl/BFont
        private static void GenerateSignedDistanceField(RenderGlyph renderGlyph, int spread)
        {
            if (spread == 0)
            {
                return;
            }

            int width  = renderGlyph.Bitmap.Width;
            int height = renderGlyph.Bitmap.Height;

            int bufferLength = renderGlyph.Bitmap.Buffer.Length;

            uint[] input  = renderGlyph.Bitmap.Buffer;
            uint[] output = new uint[renderGlyph.Bitmap.Buffer.Length];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    uint color = input[x + y * width];

                    bool inside          = (color >> 24 & 0xff) != 0;
                    int  distanceSquared = int.MaxValue;

                    for (int dx = -spread; dx <= spread; dx++)
                    {
                        for (int dy = -spread; dy <= spread; dy++)
                        {
                            int sx = x + dx;
                            int sy = y + dy;

                            if (dx == 0 || dy == 0 || sx < 0 || sx >= width || sy < 0 || sy >= height)
                            {
                                continue;
                            }

                            uint compareColor  = input[sx + sy * width];
                            bool compareInside = (compareColor >> 24 & 0xff) != 0;

                            if (compareInside ^ inside)
                            {
                                int compareDistanceSquared = dx * dx + dy * dy;

                                if (compareDistanceSquared < distanceSquared)
                                {
                                    distanceSquared = compareDistanceSquared;
                                }
                            }
                        }
                    }

                    float distance = inside ? 0 : (float)Math.Sqrt(distanceSquared);
                    distance = MathHelper.Clamp(distance, 0, spread);
                    byte alpha = (byte)(255 - (distance / spread) * 255);

                    output[x + y * width] = (uint)((alpha << 24) | 0xffffff);
                }
            }

            Array.Copy(output, input, output.Length);
        }