Exemple #1
0
        private static bool LoadTextTextures(Text text)
        {
            var success = true;

            for (var i = 0; i < text.Content.Length; i++)
            {
                if (SpriteRendererPanel.TextTextures.ContainsKey(text.GetCharKey(i)))
                {
                    continue;
                }

                var glyphSlot = FontGlyphs.GetOrSetGlyphMetrics(text, i);

                var gdipBitmap = default(Bitmap);
                using (var glyph = glyphSlot.GetGlyph())
                {
                    var bitmapGlyph = glyph.ToBitmapGlyph();
                    var bitmap      = bitmapGlyph.Bitmap;

                    if (bitmap.Rows != 0)
                    {
                        var metricRect = new Rectangle(
                            (bitmap.Width - glyphSlot.Metrics.Width.Round()) / 2,
                            (bitmap.Rows - glyphSlot.Metrics.Height.Round()) / 2,
                            glyphSlot.Metrics.Width.Round(),
                            glyphSlot.Metrics.Height.Round());
                        gdipBitmap = bitmap.ToGdipBitmap(Color.White).Crop(metricRect);

                        // TODO: Fix real reason for malformed X
                        if (text.Content[i] == 'X' && Math.Abs(text.Font.SizeInPoints - 15) < double.Epsilon * 8)
                        {
                            var fixedXBitmap = new Bitmap(gdipBitmap, metricRect.Size);
                            for (int c = 0; c < metricRect.Width; c++)
                            {
                                fixedXBitmap.SetPixel(c, metricRect.Height - 1, fixedXBitmap.GetPixel(c, metricRect.Height - 2));
                            }
                            gdipBitmap = fixedXBitmap;
                        }
                    }
                    else
                    {
                        gdipBitmap = new Bitmap(1, 1);
                    }

                    var newTexture = new Texture(gdipBitmap, (SpriteRendererPanel.UsedTextureUnits - 1).ToTextureUnit());
                    if (newTexture.Loaded)
                    {
                        SpriteRendererPanel.TextTextures[text.GetCharKey(i)] = newTexture;
                    }

                    success &= newTexture.Loaded;
                }
            }

            return(success);
        }
Exemple #2
0
        public Size MeasureText(string text, Font font)
        {
            if (font == null)
            {
                throw new ArgumentNullException();
            }

            if (text == string.Empty)
            {
                return(new Size(0, 0));
            }

            var renderedText = new Text {
                Color = Color.Black, Content = text, Font = font, Position = new Point(0, 0)
            };
            var glyphMetrics = Enumerable.Range(0, text.Length).Select(i => FontGlyphs.GetOrSetGlyphMetrics(renderedText, i));

            return(new Size((int)Math.Round(glyphMetrics.Sum(gm => gm.Metrics.HorizontalAdvance.ToDouble())), (int)Math.Round(glyphMetrics.Max(gm => gm.Metrics.Height.ToDouble()))));
        }