Example #1
0
        private void prepare(string text, Dictionary <char, CharTextureInfo> characters, Func <ICharacterDrawer> drawerMaker)
        {
            this.Initialize();

            var missinCharacters = new HashSet <char>();

            foreach (char c in text)
            {
                if (!characters.ContainsKey(c) && !char.IsWhiteSpace(c))
                {
                    missinCharacters.Add(c);
                }
            }

            if (missinCharacters.Count == 0)
            {
                return;
            }

            using (var drawer = drawerMaker())
                foreach (char c in missinCharacters)
                {
                    characters[c] = new CharTextureInfo(drawer.Draw(c), Width, Height);
                }

            TextureUtils.UpdateTexture(this.TextureId, this.textureBitmap);
        }
Example #2
0
        private void prepare(string text)
        {
            this.Initialize();

            var missinCharacters = new HashSet <char>();

            foreach (char c in text)
            {
                if (!this.characterInfos.ContainsKey(c) && !char.IsWhiteSpace(c))
                {
                    missinCharacters.Add(c);
                }
            }

            if (missinCharacters.Count == 0)
            {
                return;
            }

            foreach (char c in missinCharacters)
            {
                var path = new GraphicsPath();
                path.AddString(c.ToString(), font.FontFamily, (int)font.Style, font.Size, new Point(0, 0), StringFormat.GenericTypographic);
                path.Flatten();

                var contures = getContures(path).ToList();
                var minX     = (int)Math.Floor(path.PathPoints.Min(p => p.X));
                var maxX     = (int)Math.Ceiling(path.PathPoints.Max(p => p.X));
                var minY     = (int)Math.Floor(path.PathPoints.Min(p => p.Y));
                var maxY     = (int)Math.Ceiling(path.PathPoints.Max(p => p.Y));
                path.Dispose();

                var width  = maxX - minX;
                var height = maxY - minY;
                var rect   = this.textureBuilder.Add(new Size(width + SdfPadding * 2, height + SdfPadding * 2));

                var distField = new double[rect.Size.Height, rect.Size.Width];

                for (int y = 0; y < rect.Size.Height; y++)
                {
                    for (int x = 0; x < rect.Size.Width; x++)
                    {
                        var fromP   = new Vector2(x + minX - SdfPadding, y + minY - SdfPadding);
                        var minDist = Math.Min(contures.Min(shape => shape.Distance(fromP)), SdfPadding);
                        if (contures.Sum(shape => shape.RayHits(fromP)) % 2 != 0)
                        {
                            minDist *= -1;
                        }

                        distField[y, x] = -minDist / SdfPadding / 2 + 0.5;
                    }
                }

                for (int y = 0; y < rect.Size.Height; y++)
                {
                    for (int x = 0; x < rect.Size.Width; x++)
                    {
                        this.textureData[rect.X + x, rect.Y + y] = Color.FromArgb((int)(distField[y, x] * 255), 255, 255, 255);
                    }
                }

                this.characterInfos[c] = new CharTextureInfo(
                    rect,
                    this.textureData.Width, this.textureData.Height,
                    rect.Size.Width / font.Size,
                    rect.Size.Height / font.Size,
                    (minX + maxX) / font.Size / 2,
                    -(minY + maxY) / font.Size / 2,
                    maxX / font.Size,
                    maxY / font.Size
                    );
            }

            TextureUtils.UpdateTexture(this.TextureId, this.textureData);
        }