80 using System.Drawing.Imaging;
81 using System.Runtime.InteropServices;
82 using SiliconStudio.Core.Mathematics;
83 using SiliconStudio.Core.Serialization;
84 using SiliconStudio.Paradox.Graphics;
90 internal static class SpriteFontWriter
92 public static StaticSpriteFontData CreateSpriteFontData(SpriteFontAsset options,
Glyph[] glyphs,
float lineSpacing,
float baseLine, Bitmap bitmap)
96 Size = FontHelper.PointsToPixels(options.Size),
97 BaseOffset = baseLine,
98 FontDefaultLineSpacing = lineSpacing,
99 ExtraLineSpacing = options.LineSpacing,
100 ExtraSpacing = options.Spacing,
104 WriteGlyphs(spriteFontData, glyphs);
107 var image = GetImage(options, bitmap);
110 return spriteFontData;
115 spriteFont.Glyphs =
new Graphics.Font.Glyph[glyphs.Length];
116 for (
int i = 0; i < glyphs.Length; i++)
118 var glyph = glyphs[i];
119 spriteFont.Glyphs[i] =
new Graphics.Font.Glyph
121 Character = glyph.Character,
122 Subrect =
new Core.Mathematics.Rectangle(glyph.Subrect.X, glyph.Subrect.Y, glyph.Subrect.Width, glyph.Subrect.Height),
123 Offset =
new Vector2(glyph.XOffset, glyph.YOffset),
124 XAdvance = glyph.XAdvance,
129 static Graphics.Image GetImage(SpriteFontAsset options, Bitmap bitmap)
131 switch (options.Format)
134 case FontTextureFormat.Rgba32:
135 return GetImageRgba32(bitmap);
141 throw new NotSupportedException();
147 static Graphics.Image GetImageRgba32(Bitmap bitmap)
149 var image = Graphics.Image.New2D(bitmap.Width, bitmap.Height, 1, Graphics.PixelFormat.R8G8B8A8_UNorm);
150 var pixelBuffer = image.PixelBuffer[0];
151 using (var bitmapData =
new BitmapUtils.PixelAccessor(bitmap, ImageLockMode.ReadOnly))
153 for (
int y = 0;
y < bitmap.Height;
y++)
155 for (
int x = 0; x < bitmap.Width; x++)
157 var color = bitmapData[x,
y];
158 pixelBuffer.SetPixel(x,
y,
new Core.Mathematics.Color(color.R, color.G, color.B, color.A));
166 static unsafe Graphics.Image GetCompressedMono(Bitmap bitmap, SpriteFontAsset options)
168 if ((bitmap.Width & 3) != 0 ||
169 (bitmap.Height & 3) != 0)
171 throw new ArgumentException(
"Block compression requires texture size to be a multiple of 4.");
174 var image = Graphics.Image.New2D(bitmap.Width, bitmap.Height, 1, Graphics.PixelFormat.BC2_UNorm);
175 var pixelBuffer = (BC2Pixel*)image.PixelBuffer[0].DataPointer;
176 using (var bitmapData =
new BitmapUtils.PixelAccessor(bitmap, ImageLockMode.ReadOnly))
178 for (
int y = 0;
y < bitmap.Height;
y += 4)
180 for (
int x = 0; x < bitmap.Width; x += 4)
183 CompressBlock( bitmapData, x,
y, options, out bc2Pixel);
184 *pixelBuffer = bc2Pixel;
192 [StructLayout(LayoutKind.Sequential, Pack = 4)]
195 public long AlphaBits;
196 public uint EndPoint;
224 static void CompressBlock(BitmapUtils.PixelAccessor bitmapData,
int blockX,
int blockY, SpriteFontAsset options, out BC2Pixel bc2Pixel)
231 for (
int y = 0;
y < 4;
y++)
233 for (
int x = 0; x < 4; x++)
238 int value = bitmapData[blockX + x, blockY +
y].A;
240 if (options.NoPremultiply)
254 else if (value < 256 / 2)
259 else if (value < 256 * 5 / 6)
272 alphaBits |= alpha << (pixelCount * 4);
273 rgbBits |= rgb << (pixelCount * 2);
280 bc2Pixel.AlphaBits = alphaBits;
283 bc2Pixel.EndPoint = 0x0000FFFF;
286 bc2Pixel.RgbBits = rgbBits;
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
char DefaultCharacter
The default character fall-back.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
SharpDX.DirectWrite.Font Font
SpriteFont to use with SpriteBatch. See SpriteFont to learn how to use it.
Description of a glyph (a single character)
Data for a static SpriteFont object that supports kerning.