Ejemplo n.º 1
0
        private void AddGlyphToTexture(char glyph, int index)
        {
            int width, height, xOffset, yOffset;
            var glyphIndex = font.FindGlyphIndex(glyph);

            if (glyphIndex == 0)
            {
                Debug.Print($"Glyph not found in font {glyph.ToString()}");
                return;
            }
            byte[] rawData = font.GetCodepointBitmap(
                glyph, fontScale, fontScale,
                out width, out height, out xOffset, out yOffset
                );
            if (width == 0 | height == 0)
            {
                return;
            }
            int boxX0, boxY0, boxX1, boxY1;

            font.GetCodepointBitmapBox(
                glyph, fontScale, fontScale, out boxX0, out boxY0, out boxX1, out boxY1
                );
            var textureData = new Color[width * height];

            foreach (int i in Enumerable.Range(0, width * height))
            {
                textureData[i] = new Color(rawData[i], rawData[i], rawData[i], rawData[i]);
            }
            var quarterTileSize = appConfig.canvasTileSize / 4;
            var baseline        = ((index / appConfig.glyphPaletteSize) * appConfig.canvasTileSize) + appConfig.canvasTileSize - quarterTileSize;
            var tilePosX        = Math.Max(0, ((index % appConfig.glyphPaletteSize) * appConfig.canvasTileSize) + boxX0);
            var tilePosY        = Math.Max(0, baseline + boxY0);

            if (width + tilePosX > textureSize)
            {
                width = textureSize - tilePosX;
            }
            if (height + tilePosY > textureSize)
            {
                height = textureSize - tilePosY;
            }
            texture.SetData(
                0, new Rectangle(tilePosX, tilePosY, width, height), textureData, 0, width * height
                );
        }