Example #1
0
        private static void CopyGlyphToTexture(FontRenderer.Glyph glyph, DynamicTexture texture, IntVector2 position)
        {
            var srcPixels = glyph.Pixels;

            if (srcPixels == null)
            {
                return;                 // Invisible glyph
            }
            int    si;
            Color4 color         = Color4.Black;
            var    dstPixels     = texture.GetPixels();
            int    bytesPerPixel = glyph.RgbIntensity ? 3 : 1;

            for (int i = 0; i < glyph.Height; i++)
            {
                // Sometimes glyph.VerticalOffset could be negative for symbols with diacritics and this results the symbols
                // get overlapped or run out of the texture bounds. We shift the glyph down and increase the current line
                // height to fix the issue. Also we store the shift amount in FontChar.VerticalOffset property.
                int verticalOffsetCorrection = -Math.Min(0, glyph.VerticalOffset);

                var di = (position.Y + glyph.VerticalOffset + i + verticalOffsetCorrection) * texture.ImageSize.Width + position.X;
                for (int j = 0; j < glyph.Width; j++)
                {
                    si = i * glyph.Pitch + j * bytesPerPixel;
                    if (glyph.RgbIntensity)
                    {
                        color.A = 255;
                        color.R = srcPixels[si];
                        color.G = srcPixels[si + 1];
                        color.B = srcPixels[si + 2];
                    }
                    else
                    {
                        color   = Color4.White;
                        color.A = srcPixels[si];
                    }

                    dstPixels[di++] = color;
                }
            }
            texture.Invalidated = true;
        }