protected void createTextureForChar(Char letter) { int width, height, xOffset, yOffset; uint index; if (letter == ' ') { index = _font.FindGlyphIndex('-'); } else { index = _font.FindGlyphIndex(letter); } byte[] data = _font.GetGlyphBitmap(index, _scale, _scale, out width, out height, out xOffset, out yOffset); Color[] colorData = new Color[width * height]; Texture2D texture = new Texture2D(_graphicsDeviceManager.GraphicsDevice, width, height); if (letter != ' ') { for (int i = 0; i < colorData.Length; i++) { colorData[i] = new Color(data[i], data[i], data[i], data[i]); } } texture.SetData(colorData); _textureForChar[letter] = texture; _offsetForChar[letter] = new Vector2(xOffset, yOffset); }
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 ); }