public void Apply(TextLayout layout) { if (Enabled) { var factory = FontWrapperFactory.GetFactory(); var sc = this.Color; SharpDX.Color4 c = *(SharpDX.Color4 *) & sc; colorStyle = factory.CreateColor(c); SharpDX.DirectWrite.TextLayout tl = new SharpDX.DirectWrite.TextLayout(layout.ComPointer); tl.SetDrawingEffect(colorStyle.NativePointer, new SharpDX.DirectWrite.TextRange(Range.StartPosition, Range.Length)); Marshal.Release(colorStyle.NativePointer); } }
public void Apply(TextLayout layout) { if (Enabled) { var sc = this.Color; SharpDX.Color4 c = *(SharpDX.Color4 *) & sc; if (colorStyle == null) { var factory = FontWrapperFactory.GetFactory(); colorStyle = factory.CreateColor(c); } else { colorStyle.SetColor(c.Red, c.Green, c.Blue, c.Alpha); } SharpDX.DirectWrite.TextLayout tl = new SharpDX.DirectWrite.TextLayout(layout.ComPointer); tl.SetDrawingEffect(colorStyle.NativePointer, new SharpDX.DirectWrite.TextRange(Range.StartPosition, Range.Length)); } }
private static void Main() { RenderForm renderForm = new RenderForm("SharpFontWrapper - Simple Text"); renderForm.Width = 1024; renderForm.Height = 768; viewPort = new ViewportF(0, 0, renderForm.Width, renderForm.Height, 0.0f, 1.0f); device = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); deviceContext = device.ImmediateContext; using (SharpDX.DXGI.Factory dxgiFactory = new SharpDX.DXGI.Factory1()) { SharpDX.DXGI.SwapChainDescription swapChainDesc = new SharpDX.DXGI.SwapChainDescription() { BufferCount = 2, Flags = SharpDX.DXGI.SwapChainFlags.None, IsWindowed = true, ModeDescription = new SharpDX.DXGI.ModeDescription(0, 0, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm), OutputHandle = renderForm.Handle, SampleDescription = new SharpDX.DXGI.SampleDescription(4, 0), SwapEffect = SharpDX.DXGI.SwapEffect.Discard, Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput }; swapChain = new SwapChain(dxgiFactory, device, swapChainDesc); using (Texture2D backBuffer = swapChain.GetBackBuffer <Texture2D>(0)) { renderView = new RenderTargetView(device, backBuffer); } } fontFactory = new SharpFontWrapper.Factory(); fontWrapper = fontFactory.CreateFontWrapper(device, "Arial"); colorStyle = fontFactory.CreateColor(SharpDX.Color.Green); textFormat = new SharpDX.DirectWrite.TextFormat(fontWrapper.DWriteFactory, "Consolas", 32.0f); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Center; textFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near; textLayout = new SharpDX.DirectWrite.TextLayout(fontWrapper.DWriteFactory, "DirectWrite Text Layout with formatting and color", textFormat, 1000.0f, 1000.0f); textLayout.SetFontStyle(SharpDX.DirectWrite.FontStyle.Italic, new SharpDX.DirectWrite.TextRange(0, 11)); textLayout.SetFontFamilyName("Arial", new SharpDX.DirectWrite.TextRange(24, 4)); textLayout.SetFontSize(72.0f, new SharpDX.DirectWrite.TextRange(29, 10)); //Stylistic typeography SharpDX.DirectWrite.Typography typoGraphy = new SharpDX.DirectWrite.Typography(fontWrapper.DWriteFactory); typoGraphy.AddFontFeature(new SharpDX.DirectWrite.FontFeature(SharpDX.DirectWrite.FontFeatureTag.StylisticSet7, 1)); textLayout.SetFontFamilyName("Gabriola", new SharpDX.DirectWrite.TextRange(12, 4)); textLayout.SetTypography(typoGraphy, new SharpDX.DirectWrite.TextRange(12, 4)); //Note : to pass color, make sure to use native pointer, as in that case we do not need sharpdx/.net to build com wrapper, colorStyle is already one textLayout.SetDrawingEffect(colorStyle.NativePointer, new SharpDX.DirectWrite.TextRange(44, 5)); RenderLoop.Run(renderForm, () => { float t = (float)watch.Elapsed.TotalMilliseconds; float x = ((float)Math.Sin(t * 0.002f) * 100.0f) + (renderForm.Width * 0.5f); deviceContext.OutputMerger.SetRenderTargets(renderView); deviceContext.Rasterizer.SetViewport(viewPort); deviceContext.ClearRenderTargetView(renderView, Color.Black); SharpFontWrapper.TextFlags flags = SharpFontWrapper.TextFlags.NoWordWrapping | SharpFontWrapper.TextFlags.VerticalCenter | SharpFontWrapper.TextFlags.Center; fontWrapper.DrawString(deviceContext, "Hello SharpFontWrapper", 64.0f, new Vector2(x, renderForm.Height * 0.25f), Color.Yellow, flags); fontWrapper.DrawString(deviceContext, "This is another text", 64.0f, new Vector2(renderForm.Width * 0.5f, renderForm.Height * 0.25f + 100.0f), Color.White, flags); fontWrapper.DrawTextLayout(deviceContext, textLayout, new Vector2(0, renderForm.Height * 0.25f + 200.0f), Color.White, flags); swapChain.Present(1, SharpDX.DXGI.PresentFlags.None); }); typoGraphy.Dispose(); colorStyle.Dispose(); textLayout.Dispose(); textFormat.Dispose(); fontWrapper.Dispose(); fontFactory.Dispose(); deviceContext.ClearState(); deviceContext.Flush(); renderView.Dispose(); swapChain.Dispose(); deviceContext.Dispose(); device.Dispose(); }