|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using SharpDX.Direct3D11; |
| 6 | +using SharpDX; |
| 7 | +using System.Diagnostics; |
| 8 | + |
| 9 | +namespace DirectX.Direct3D11.Overlay |
| 10 | +{ |
| 11 | + public class DXFont : IDisposable |
| 12 | + { |
| 13 | + Device _device; |
| 14 | + DeviceContext _deviceContext; |
| 15 | + |
| 16 | + public DXFont(Device device, DeviceContext deviceContext) |
| 17 | + { |
| 18 | + _device = device; |
| 19 | + _deviceContext = deviceContext; |
| 20 | + _initialized = false; |
| 21 | + _fontSheetTex = null; |
| 22 | + _fontSheetSRV = null; |
| 23 | + _texWidth = 1024; |
| 24 | + _texHeight = 0; |
| 25 | + _spaceWidth = 0; |
| 26 | + _charHeight = 0; |
| 27 | + } |
| 28 | + |
| 29 | + public void Dispose() |
| 30 | + { |
| 31 | + if (_fontSheetTex != null) |
| 32 | + _fontSheetTex.Dispose(); |
| 33 | + if (_fontSheetSRV != null) |
| 34 | + _fontSheetSRV.Dispose(); |
| 35 | + |
| 36 | + _fontSheetTex = null; |
| 37 | + _fontSheetSRV = null; |
| 38 | + _device = null; |
| 39 | + _deviceContext = null; |
| 40 | + } |
| 41 | + |
| 42 | + enum STYLE |
| 43 | + { |
| 44 | + STYLE_NORMAL = 0, |
| 45 | + STYLE_BOLD = 1, |
| 46 | + STYLE_ITALIC = 2, |
| 47 | + STYLE_BOLD_ITALIC = 3, |
| 48 | + STYLE_UNDERLINE = 4, |
| 49 | + STYLE_STRIKEOUT = 8 |
| 50 | + }; |
| 51 | + |
| 52 | + bool _initialized; |
| 53 | + const char StartChar = (char)33; |
| 54 | + const char EndChar = (char)127; |
| 55 | + const uint NumChars = EndChar - StartChar; |
| 56 | + ShaderResourceView _fontSheetSRV; |
| 57 | + Texture2D _fontSheetTex; |
| 58 | + int _texWidth, _texHeight; |
| 59 | + Rectangle[] _charRects = new Rectangle[NumChars]; |
| 60 | + int _spaceWidth, _charHeight; |
| 61 | + |
| 62 | + public bool Initialize(string FontName, float FontSize, System.Drawing.FontStyle FontStyle, bool AntiAliased) |
| 63 | + { |
| 64 | + Debug.Assert(!_initialized); |
| 65 | + System.Drawing.Font font = new System.Drawing.Font(FontName, FontSize, FontStyle, System.Drawing.GraphicsUnit.Pixel); |
| 66 | + |
| 67 | + System.Drawing.Text.TextRenderingHint hint = AntiAliased ? System.Drawing.Text.TextRenderingHint.AntiAlias : System.Drawing.Text.TextRenderingHint.SystemDefault; |
| 68 | + |
| 69 | + int tempSize = (int)(FontSize * 2); |
| 70 | + using (System.Drawing.Bitmap charBitmap = new System.Drawing.Bitmap(tempSize, tempSize, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) |
| 71 | + { |
| 72 | + using (System.Drawing.Graphics charGraphics = System.Drawing.Graphics.FromImage(charBitmap)) |
| 73 | + { |
| 74 | + charGraphics.PageUnit = System.Drawing.GraphicsUnit.Pixel; |
| 75 | + charGraphics.TextRenderingHint = hint; |
| 76 | + |
| 77 | + MeasureChars(font, charGraphics); |
| 78 | + |
| 79 | + using (var fontSheetBitmap = new System.Drawing.Bitmap(_texWidth, _texHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) |
| 80 | + { |
| 81 | + using (var fontSheetGraphics = System.Drawing.Graphics.FromImage(fontSheetBitmap)) |
| 82 | + { |
| 83 | + fontSheetGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; |
| 84 | + fontSheetGraphics.Clear(System.Drawing.Color.FromArgb(0, System.Drawing.Color.Black)); |
| 85 | + |
| 86 | + BuildFontSheetBitmap(font, charGraphics, charBitmap, fontSheetGraphics); |
| 87 | + |
| 88 | + if (!BuildFontSheetTexture(fontSheetBitmap)) |
| 89 | + { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + _initialized = true; |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + private bool BuildFontSheetTexture(System.Drawing.Bitmap fontSheetBitmap) |
| 103 | + { |
| 104 | + System.Drawing.Imaging.BitmapData bmData; |
| 105 | + |
| 106 | + bmData = fontSheetBitmap.LockBits(new System.Drawing.Rectangle(0, 0, _texWidth, _texHeight), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); |
| 107 | + Texture2DDescription texDesc = new Texture2DDescription(); |
| 108 | + texDesc.Width = _texWidth; |
| 109 | + texDesc.Height = _texHeight; |
| 110 | + texDesc.MipLevels = 1; |
| 111 | + texDesc.ArraySize = 1; |
| 112 | + texDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm; |
| 113 | + texDesc.SampleDescription.Count = 1; |
| 114 | + texDesc.SampleDescription.Quality = 0; |
| 115 | + texDesc.Usage = ResourceUsage.Immutable; |
| 116 | + texDesc.BindFlags = BindFlags.ShaderResource; |
| 117 | + texDesc.CpuAccessFlags = CpuAccessFlags.None; |
| 118 | + texDesc.OptionFlags = ResourceOptionFlags.None; |
| 119 | + |
| 120 | + |
| 121 | + SharpDX.DataBox data; |
| 122 | + data.DataPointer = bmData.Scan0; |
| 123 | + data.RowPitch = _texWidth * 4; |
| 124 | + data.SlicePitch = 0; |
| 125 | + |
| 126 | + _fontSheetTex = new Texture2D(_device, texDesc, new[] { data }); |
| 127 | + if (_fontSheetTex == null) |
| 128 | + return false; |
| 129 | + |
| 130 | + ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription(); |
| 131 | + srvDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm; |
| 132 | + srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D; |
| 133 | + srvDesc.Texture2D.MipLevels = 1; |
| 134 | + srvDesc.Texture2D.MostDetailedMip = 0; |
| 135 | + |
| 136 | + _fontSheetSRV = new ShaderResourceView(_device, _fontSheetTex, srvDesc); |
| 137 | + if (_fontSheetSRV == null) |
| 138 | + return false; |
| 139 | + |
| 140 | + fontSheetBitmap.UnlockBits(bmData); |
| 141 | + |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + void MeasureChars(System.Drawing.Font font, System.Drawing.Graphics charGraphics) |
| 146 | + { |
| 147 | + char[] allChars = new char[NumChars]; |
| 148 | + |
| 149 | + for (char i = (char)0; i < NumChars; ++i) |
| 150 | + allChars[i] = (char)(StartChar + i); |
| 151 | + |
| 152 | + System.Drawing.SizeF size; |
| 153 | + size = charGraphics.MeasureString(new String(allChars), font, new System.Drawing.PointF(0, 0), System.Drawing.StringFormat.GenericDefault); |
| 154 | + |
| 155 | + _charHeight = (int)(size.Height + 0.5f); |
| 156 | + |
| 157 | + int numRows = (int)(size.Width / _texWidth) + 1; |
| 158 | + _texHeight = (numRows * _charHeight) + 1; |
| 159 | + |
| 160 | + System.Drawing.StringFormat sf = System.Drawing.StringFormat.GenericDefault; |
| 161 | + sf.FormatFlags |= System.Drawing.StringFormatFlags.MeasureTrailingSpaces; |
| 162 | + size = charGraphics.MeasureString(" ", font, 0, sf); |
| 163 | + _spaceWidth = (int)(size.Width + 0.5f); |
| 164 | + } |
| 165 | + |
| 166 | + void BuildFontSheetBitmap(System.Drawing.Font font, System.Drawing.Graphics charGraphics, System.Drawing.Bitmap charBitmap, System.Drawing.Graphics fontSheetGraphics) |
| 167 | + { |
| 168 | + System.Drawing.Brush whiteBrush = System.Drawing.Brushes.White; |
| 169 | + int fontSheetX = 0; |
| 170 | + int fontSheetY = 0; |
| 171 | + |
| 172 | + |
| 173 | + for (int i = 0; i < NumChars; ++i) |
| 174 | + { |
| 175 | + charGraphics.Clear(System.Drawing.Color.FromArgb(0, System.Drawing.Color.Black)); |
| 176 | + charGraphics.DrawString(((char)(StartChar + i)).ToString(), font, whiteBrush, new System.Drawing.PointF(0.0f, 0.0f)); |
| 177 | + |
| 178 | + int minX = GetCharMinX(charBitmap); |
| 179 | + int maxX = GetCharMaxX(charBitmap); |
| 180 | + int charWidth = maxX - minX + 1; |
| 181 | + |
| 182 | + if (fontSheetX + charWidth >= _texWidth) |
| 183 | + { |
| 184 | + fontSheetX = 0; |
| 185 | + fontSheetY += (int)(_charHeight) + 1; |
| 186 | + } |
| 187 | + |
| 188 | + _charRects[i] = new Rectangle(fontSheetX, fontSheetY, charWidth, _charHeight); |
| 189 | + |
| 190 | + fontSheetGraphics.DrawImage(charBitmap, fontSheetX, fontSheetY, new System.Drawing.Rectangle(minX, 0, charWidth, _charHeight), System.Drawing.GraphicsUnit.Pixel); |
| 191 | + |
| 192 | + fontSheetX += charWidth + 1; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private int GetCharMaxX(System.Drawing.Bitmap charBitmap) |
| 197 | + { |
| 198 | + int width = charBitmap.Width; |
| 199 | + int height = charBitmap.Height; |
| 200 | + |
| 201 | + for (int x = width - 1; x >= 0; --x) |
| 202 | + { |
| 203 | + for (int y = 0; y < height; ++y) |
| 204 | + { |
| 205 | + System.Drawing.Color color; |
| 206 | + |
| 207 | + color = charBitmap.GetPixel(x, y); |
| 208 | + if (color.A > 0) |
| 209 | + return x; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return width - 1; |
| 214 | + } |
| 215 | + |
| 216 | + private int GetCharMinX(System.Drawing.Bitmap charBitmap) |
| 217 | + { |
| 218 | + int width = charBitmap.Width; |
| 219 | + int height = charBitmap.Height; |
| 220 | + |
| 221 | + for (int x = 0; x < width; ++x) |
| 222 | + { |
| 223 | + for (int y = 0; y < height; ++y) |
| 224 | + { |
| 225 | + System.Drawing.Color color; |
| 226 | + |
| 227 | + color = charBitmap.GetPixel(x, y); |
| 228 | + if (color.A > 0) |
| 229 | + return x; |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return 0; |
| 234 | + } |
| 235 | + |
| 236 | + public ShaderResourceView GetFontSheetSRV() |
| 237 | + { |
| 238 | + Debug.Assert(_initialized); |
| 239 | + |
| 240 | + return _fontSheetSRV; |
| 241 | + } |
| 242 | + |
| 243 | + public Rectangle GetCharRect(char c) |
| 244 | + { |
| 245 | + Debug.Assert(_initialized); |
| 246 | + |
| 247 | + return _charRects[c - StartChar]; |
| 248 | + } |
| 249 | + |
| 250 | + public int GetSpaceWidth() |
| 251 | + { |
| 252 | + Debug.Assert(_initialized); |
| 253 | + |
| 254 | + return _spaceWidth; |
| 255 | + } |
| 256 | + |
| 257 | + public int GetCharHeight() |
| 258 | + { |
| 259 | + Debug.Assert(_initialized); |
| 260 | + |
| 261 | + return _charHeight; |
| 262 | + } |
| 263 | + |
| 264 | + } |
| 265 | +} |
0 commit comments