private static void GenerateDrawCalls(int sizeX, int sizeY, STRLayout[] tl, out CharTableDescription TableDesc, out CharRenderCall[] drawCalls) { drawCalls = new CharRenderCall[256]; TableDesc = new CharTableDescription(); int line = 0, xPos = 0, yPos = 0; for (int i = 0; i < 256; ++i) { //1 additional pixel on each side int charWidth = 2 + (int)Math.Ceiling(tl[i].LayoutSize.X + tl[i].OverhangLeft + tl[i].OverhangRight); int charHeight = 2 + (int)Math.Ceiling(tl[i].LayoutSize.Y + tl[i].OverhangTop + tl[i].OverhangBottom); line = Math.Max(line, charHeight); if (xPos + charWidth >= sizeX) { xPos = 0; yPos += line; line = 0; } var charDesc = new CharDescription(); charDesc.CharSize = new STRVector(tl[i].WidthIncludingTrailingWhitespaces, tl[i].Size.Y); charDesc.OverhangLeft = tl[i].OverhangLeft + 1; charDesc.OverhangTop = tl[i].OverhangTop + 1; //Make XPos + CD.Overhang.Left an integer number in order to draw at integer positions charDesc.OverhangLeft += (float)Math.Ceiling(xPos + charDesc.OverhangLeft) - (xPos + charDesc.OverhangLeft); //Make YPos + CD.Overhang.Top an integer number in order to draw at integer positions charDesc.OverhangTop += (float)Math.Ceiling(yPos + charDesc.OverhangTop) - (yPos + charDesc.OverhangTop); charDesc.OverhangRight = charWidth - charDesc.CharSize.X - charDesc.OverhangLeft; charDesc.OverhangBottom = charHeight - charDesc.CharSize.Y - charDesc.OverhangTop; charDesc.TexCoordsStart = new STRVector(((float)xPos / sizeX), ((float)yPos / sizeY)); charDesc.TexCoordsSize = new STRVector((float)charWidth / sizeX, (float)charHeight / sizeY); charDesc.TableDescription = TableDesc; TableDesc.Chars[i] = charDesc; drawCalls[i] = new CharRenderCall() { Position = new Vector2(xPos + charDesc.OverhangLeft, yPos + charDesc.OverhangTop), TextLayout = tl[i].TextLayout }; xPos += charWidth; } }
private void CreateCharTable(byte bytePrefix) { var TableDesc = new CharTableDescription(); //Get appropriate texture size int sizeX = (int)(Font.FontSize * 12); sizeX = (int)Math.Pow(2, Math.Ceiling(Math.Log(sizeX, 2))); //Try how many lines are needed: var tl = new TextLayout[256]; int line = 0, xPos = 0, yPos = 0; for (int i = 0; i < 256; ++i) { tl[i] = new TextLayout(ModelEx.FontManager.Instance.WriteFactory, Convert.ToChar(i + (bytePrefix << 8)).ToString(), Font); int charWidth = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutWidth + tl[i].OverhangMetrics.Left + tl[i].OverhangMetrics.Right); int charHeight = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutHeight + tl[i].OverhangMetrics.Top + tl[i].OverhangMetrics.Bottom); line = Math.Max(line, charHeight); if (xPos + charWidth >= sizeX) { xPos = 0; yPos += line; line = 0; } xPos += charWidth; } int sizeY = (int)(line + yPos); sizeY = (int)Math.Pow(2, Math.Ceiling(Math.Log(sizeY, 2))); //Create Texture var TexDesc = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = sizeY, Width = sizeX, MipLevels = 1, OptionFlags = ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var texture = new Texture2D(ModelEx.FontManager.Instance.D3DDevice10, TexDesc); var rtv = new RenderTargetView(ModelEx.FontManager.Instance.D3DDevice10, texture); ModelEx.FontManager.Instance.D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(0, 1, 1, 1)); //D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(1, 0, 0, 0)); Surface surface = texture.AsSurface(); var target = RenderTarget.FromDXGI(ModelEx.FontManager.Instance.D2DFactory, surface, rtp); var color = new SolidColorBrush(target, new SlimDX.Color4(1, 1, 1, 1)); target.BeginDraw(); line = 0; xPos = 0; yPos = 0; //for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i) { //1 additional pixel on each side int charWidth = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutWidth + tl[i].OverhangMetrics.Left + tl[i].OverhangMetrics.Right); int charHeight = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutHeight + tl[i].OverhangMetrics.Top + tl[i].OverhangMetrics.Bottom); line = Math.Max(line, charHeight); if (xPos + charWidth >= sizeX) { xPos = 0; yPos += line; line = 0; } var charDesc = new CharDescription(); charDesc.CharSize = new Vector2(tl[i].Metrics.WidthIncludingTrailingWhitespace, tl[i].Metrics.Height); charDesc.OverhangLeft = tl[i].OverhangMetrics.Left + 1; charDesc.OverhangTop = tl[i].OverhangMetrics.Top + 1; //Make XPos + CD.Overhang.Left an integer number in order to draw at integer positions charDesc.OverhangLeft += (float)Math.Ceiling(xPos + charDesc.OverhangLeft) - (xPos + charDesc.OverhangLeft); //Make YPos + CD.Overhang.Top an integer number in order to draw at integer positions charDesc.OverhangTop += (float)Math.Ceiling(yPos + charDesc.OverhangTop) - (yPos + charDesc.OverhangTop); charDesc.OverhangRight = charWidth - charDesc.CharSize.X - charDesc.OverhangLeft; charDesc.OverhangBottom = charHeight - charDesc.CharSize.Y - charDesc.OverhangTop; charDesc.TexCoordsStart = new Vector2(((float)xPos / sizeX), ((float)yPos / sizeY)); charDesc.TexCoordsSize = new Vector2((float)charWidth / sizeX, (float)charHeight / sizeY); charDesc.TableDescription = TableDesc; TableDesc.Chars[i] = charDesc; target.DrawTextLayout(new PointF(xPos + charDesc.OverhangLeft, yPos + charDesc.OverhangTop), tl[i], color); xPos += charWidth; tl[i].Dispose(); } target.EndDraw(); color.Dispose(); //This is a workaround for Windows 8.1 machines. //If these lines would not be present, the shared resource would be empty. //TODO: find a nicer solution using (var ms = new MemoryStream()) Texture2D.ToStream(texture, ImageFileFormat.Bmp, ms); System.Threading.Monitor.Enter(D3DDevice11); var dxgiResource = new SlimDX.DXGI.Resource(texture); SlimDX.Direct3D11.Texture2D Texture11; if (PixCompatible) { Texture11 = new SlimDX.Direct3D11.Texture2D(D3DDevice11, new SlimDX.Direct3D11.Texture2DDescription() { ArraySize = 1, BindFlags = SlimDX.Direct3D11.BindFlags.ShaderResource | SlimDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = sizeY, Width = sizeX, MipLevels = 1, OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = SlimDX.Direct3D11.ResourceUsage.Default }); } else { Texture11 = D3DDevice11.OpenSharedResource <SlimDX.Direct3D11.Texture2D>(dxgiResource.SharedHandle); } var srv = new SlimDX.Direct3D11.ShaderResourceView(D3DDevice11, Texture11); TableDesc.Texture = Texture11; TableDesc.SRV = srv; rtv.Dispose(); System.Threading.Monitor.Exit(D3DDevice11); System.Diagnostics.Debug.WriteLine("Created Char Table " + bytePrefix + " in " + sizeX + " x " + sizeY); //System.Threading.Monitor.Enter(D3DDevice11); //SlimDX.Direct3D11.Texture2D.SaveTextureToFile(Sprite.Device.ImmediateContext, Texture11, SlimDX.Direct3D11.ImageFileFormat.Png, Font.FontFamilyName + "Table" + BytePrefix + ".png"); //System.Threading.Monitor.Exit(D3DDevice11); CharTables.Add(bytePrefix, TableDesc); dxgiResource.Dispose(); target.Dispose(); surface.Dispose(); texture.Dispose(); }