/// <summary> /// Inits the font family names from DirectWrite /// </summary> private void InitTextFormatLayout() { FontFamilyName = "Gabriola"; FontSize = 72; FontText = "Client Drawing Effect Example!"; // Initialize a TextFormat CurrentTextFormat = new TextFormat(FactoryDWrite, FontFamilyName, FontSize); CurrentTextFormat.TextAlignment = TextAlignment.Center; CurrentTextFormat.ParagraphAlignment = ParagraphAlignment.Center; CurrentTextLayout = new TextLayout(FactoryDWrite, FontText, CurrentTextFormat, ClientRectangle.Width, ClientRectangle.Height); RedDrawingeffect = new ColorDrawingEffect(Color.Red); BlueDrawingEffect = new ColorDrawingEffect(Color.Blue); GreenDrawingEffect = new ColorDrawingEffect(Color.Green); CurrentTextLayout.SetDrawingEffect(RedDrawingeffect, new TextRange(0, 14)); CurrentTextLayout.SetDrawingEffect(BlueDrawingEffect, new TextRange(14, 7)); CurrentTextLayout.SetDrawingEffect(GreenDrawingEffect, new TextRange(21, 8)); CurrentTextLayout.SetUnderline(true, new TextRange(0, 20)); CurrentTextLayout.SetStrikethrough(true, new TextRange(22, 7)); // Set a stylistic typography using (var typo = new Typography(FactoryDWrite)) { typo.AddFontFeature(new FontFeature(FontFeatureTag.StylisticSet7, 1)); CurrentTextLayout.SetTypography(typo, CurrentTextRange); } }
/// <summary> /// Inits the font family names from DirectWrite /// </summary> private void InitTextFormatLayout() { FontFamilyName = "Gabriola"; FontSize = 72; FontText = "Client Drawing Effect Example!"; // Initialize a TextFormat CurrentTextFormat = new TextFormat(FactoryDWrite, FontFamilyName, FontSize) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center }; CurrentTextLayout = new TextLayout(FactoryDWrite, FontText, CurrentTextFormat, ClientRectangle.Width, ClientRectangle.Height); RedDrawingeffect = new ColorDrawingEffect(new Color4(1, 1, 0, 0)); BlueDrawingEffect = new ColorDrawingEffect(new Color4(1, 0, 0, 1)); GreenDrawingEffect = new ColorDrawingEffect(new Color4(1, 0, 1, 0)); CurrentTextLayout.SetDrawingEffect(RedDrawingeffect, new TextRange(0, 14)); CurrentTextLayout.SetDrawingEffect(BlueDrawingEffect, new TextRange(14, 7)); CurrentTextLayout.SetDrawingEffect(GreenDrawingEffect, new TextRange(21, 8)); // Set a stylistic typography var typo = new Typography(FactoryDWrite); typo.AddFontFeature(new FontFeature(FontFeatureTag.StylisticSet7, 1)); CurrentTextLayout.SetTypography(typo, CurrentTextRange); typo.Release(); }
void UpdateCache(ref CachedRenderString cache, string fontName, float size, string text, bool underline, TextAlignment alignment) { if (cache == null) { cache = new DirectWriteCachedText() { FontName = fontName, FontSize = size, Text = text, Underline = underline, Alignment = alignment }; } if (cache is not DirectWriteCachedText pc) { throw new ArgumentException("cache"); } if (pc.quads == null || pc.Update(fontName, text, size, underline, alignment)) { using (var layout = new TextLayout(dwFactory, text, GetFormat(fontName, ConvertSize(size)), float.MaxValue, float.MaxValue)) { layout.TextAlignment = CastAlignment(alignment); layout.SetDrawingEffect(new ColorDrawingEffect(Color4.White, new TextShadow()), new TextRange(0, text.Length)); layout.Draw(Renderer, 0, 0); pc.quads = Renderer.Quads.ToArray(); Renderer.Quads = new List <DrawQuad>(); var metrics = layout.Metrics; pc.size = new Point((int)metrics.WidthIncludingTrailingWhitespace, (int)metrics.Height); } } }
public override void DrawStringBaseline(string fontName, float size, string text, float x, float y, float start_x, Color4 color, bool underline = false, TextShadow shadow = default) { using (var layout = new TextLayout(dwFactory, text, GetFormat(fontName, ConvertSize(size)), float.MaxValue, float.MaxValue)) { var indent = (x - start_x); if (Math.Abs(indent) > 0.001f) { layout.SetInlineObject(new Indent(indent), new TextRange(0, 0)); } layout.SetDrawingEffect(new ColorDrawingEffect(color, shadow), new TextRange(0, text.Length)); layout.Draw(Renderer, 0, 0); foreach (var q in Renderer.Quads) { var d = q.Destination; d.X += (int)start_x; d.Y += (int)y; if (q.Texture == null) { render2d.FillRectangle(d, q.Color); } else { render2d.Draw(q.Texture, q.Source, d, q.Color); } } Renderer.Quads = new List <DrawQuad>(); } }
public D2D_TextElements CreateNewTextElements(UText t) { var textLayout = new TextLayout(dwFact, t.text, new TextFormat( dwFact, t.font.name, t.font.bold ? FontWeight.Bold : FontWeight.Normal, t.font.italic ? FontStyle.Italic : FontStyle.Normal, TranslateFontSize(t.font.size)) { ParagraphAlignment = Translate(t.valign), TextAlignment = Translate(t.halign), WordWrapping = t.wrapped ? WordWrapping.Wrap : WordWrapping.NoWrap }, t.width, t.height); // Set font ranges... textLayout just created, dont worry about any leftover ranges. foreach (var sr in t.SafeGetStyleRanges) { var tr = new TextRange(sr.start, sr.length); if (sr.fontOverride != null) { UFont ft = (UFont)sr.fontOverride; textLayout.SetFontFamilyName(ft.name, tr); textLayout.SetFontSize(TranslateFontSize(ft.size), tr); textLayout.SetFontStyle(ft.italic ? FontStyle.Italic : FontStyle.Normal, tr); textLayout.SetFontWeight(ft.bold ? FontWeight.Bold : FontWeight.Normal, tr); } if (sr.fgOverride != null || sr.bgOverride != null) { ClientTextEffect cte = new ClientTextEffect(); if (sr.fgOverride != null) { cte.fgBrush = CreateBrush(sr.fgOverride); } if (sr.bgOverride != null) { cte.bgBrush = CreateBrush(sr.bgOverride); } textLayout.SetDrawingEffect(cte, tr); } } // Set renderer with a default brush var def = new USolidBrush() { color = new Color(0) }; var textRenderer = new D2D_ClientTextRenderer(realRenderer.renderTarget, new ClientTextEffect() { fgBrush = CreateBrush(def) }); var ret = new D2D_TextElements(textLayout, textRenderer); realRenderer.renderTarget.Disposed += new EventHandler <EventArgs>((o, e) => t.Invalidate()); return(ret); }
protected override void OnCreateDeviceIndependentResources(Direct2DFactory factory) { base.OnCreateDeviceIndependentResources(factory); _redColorDrawingEffect = new ColorDrawingEffect(Color.FromKnown(Colors.Red, 1)); _greenColorDrawingEffect = new ColorDrawingEffect(Color.FromKnown(Colors.Green, 1)); _blueColorDrawingEffect = new ColorDrawingEffect(Color.FromKnown(Colors.Blue, 1)); _textFormat = DirectWriteFactory.CreateTextFormat("Gabriola", 72); _textFormat.TextAlignment = TextAlignment.Center; _textFormat.ParagraphAlignment = ParagraphAlignment.Center; float width = ClientSize.Width / _dpiScaleX; float height = ClientSize.Height / _dpiScaleY; _textLayout = DirectWriteFactory.CreateTextLayout( _text, _textFormat, width, height); _textLayout.SetFontSize(100, new TextRange(20, 6)); _textLayout.SetDrawingEffect(_blueColorDrawingEffect, new TextRange(6, 5)); _textLayout.SetDrawingEffect(_redColorDrawingEffect, new TextRange(20, 6)); _textLayout.SetDrawingEffect(_greenColorDrawingEffect, new TextRange(26, 5)); ClientDrawingEffect effect = _textLayout.GetDrawingEffect(7); _textLayout.SetUnderline(true, new TextRange(20, 11)); _textLayout.SetFontWeight(FontWeight.Bold, new TextRange(20, 11)); using (Typography typography = DirectWriteFactory.CreateTypography()) { typography.AddFontFeature(FontFeatureTag.StylisticSet7, 1); _textLayout.SetTypography(typography, new TextRange(0, _text.Length)); } }
public void SetBrush(Renderer.TextRange range, IBrush brush) { if (Surface.IsDisposing) { return; } if (!(brush is IDxBrush dxBrush)) { throw new ArgumentOutOfRangeException(nameof(brush), Error_FactoryObject); } Convert(range, out SharpDX.DirectWrite.TextRange dxRange); TextLayout.SetDrawingEffect(dxBrush.Brush, dxRange); }
public override BuiltRichText BuildText(IList <RichTextNode> nodes, int width, float sizeMultiplier = 1) { var paragraphs = new List <List <RichTextNode> >(); paragraphs.Add(new List <RichTextNode>()); int first = 0; while (nodes[first] is RichTextParagraphNode && first < nodes.Count) { first++; } DWrite.TextAlignment ta = CastAlignment((nodes[first] as RichTextTextNode).Alignment); paragraphs[paragraphs.Count - 1].Add(nodes[first]); foreach (var node in nodes.Skip(first + 1)) { if (node is RichTextParagraphNode) { paragraphs.Add(new List <RichTextNode>()); } else { var n = (RichTextTextNode)node; var align = CastAlignment(n.Alignment); if (align != ta && paragraphs[paragraphs.Count - 1].Count > 0) { paragraphs.Add(new List <RichTextNode>()); } paragraphs[paragraphs.Count - 1].Add(node); ta = align; } } string lastFont = null; float lastSize = 0; //Format text var layouts = new List <TextLayout>(); for (int j = 0; j < paragraphs.Count; j++) { var p = paragraphs[j]; var builder = new StringBuilder(); foreach (var n in p) { builder.Append(((RichTextTextNode)n).Contents); } var layout = new TextLayout( dwFactory, builder.ToString(), new TextFormat( dwFactory, string.IsNullOrEmpty(lastFont) ? "Arial" : lastFont, lastSize > 0 ? lastSize : 12 ), width, float.MaxValue ); if (p.Count > 0) { layout.TextAlignment = CastAlignment(((RichTextTextNode)p[0]).Alignment); } int startIdx = 0; foreach (var n in p) { var text = (RichTextTextNode)n; var range = new TextRange(startIdx, text.Contents.Length); if (text.Bold) { layout.SetFontWeight(FontWeight.Bold, range); } if (text.Italic) { layout.SetFontStyle(FontStyle.Italic, range); } if (text.Underline) { layout.SetUnderline(true, range); } if (!string.IsNullOrEmpty(text.FontName)) { layout.SetFontFamilyName(text.FontName, range); lastFont = text.FontName; } if (text.FontSize > 0) { layout.SetFontSize(text.FontSize, range); lastSize = text.FontSize; } layout.SetDrawingEffect(new ColorDrawingEffect(text.Color), range); startIdx += text.Contents.Length; } layouts.Add(layout); } //Return var built = new DirectWriteBuiltText(this) { Layout = layouts, Width = width }; built.CacheQuads(); return(built); }
public void DrawMessages() { if (msgs.Count == 0) { return; } lastText = text; text = ""; foreach (OSDMessage msg in msgs) { text += msg.msg + "\r\n"; } text = text.Trim(); if (text == "") { return; } if (lastText == text && !requiresUpdate) { msgs.Clear(); Draw(); return; } Utilities.Dispose(ref layout); if (brushes.Count > 0) { foreach (var brush in brushes) { SolidColorBrush tmp = brush; Utilities.Dispose(ref tmp); } brushes = new List <SolidColorBrush>(); } layout = new TextLayout(renderer.factoryWrite, text, format, hookViewport ? renderer.GetViewport.Width : renderer.HookControl.Width, hookViewport ? renderer.GetViewport.Height : renderer.HookControl.Height); // Fix Styling int curPos = 0; foreach (OSDMessage msg in msgs) { if (msg.styles == null) { curPos += msg.msg.Length + 2; continue; } foreach (SubStyle style in msg.styles) { switch (style.style) { case SubStyles.BOLD: layout.SetFontWeight(FontWeight.Bold, new TextRange(curPos + style.from, style.len)); break; case SubStyles.ITALIC: layout.SetFontStyle(FontStyle.Italic, new TextRange(curPos + style.from, style.len)); break; case SubStyles.UNDERLINE: layout.SetUnderline(true, new TextRange(curPos + style.from, style.len)); break; case SubStyles.STRIKEOUT: layout.SetStrikethrough(true, new TextRange(curPos + style.from, style.len)); break; case SubStyles.COLOR: SolidColorBrush brush2dtmp = new SolidColorBrush(renderer.rtv2d, (Color)style.value); brushes.Add(brush2dtmp); layout.SetDrawingEffect(brush2dtmp.NativePointer, new TextRange(curPos + style.from, style.len)); break; } } curPos += msg.msg.Length + 2; } CreateRectangle(); msgs.Clear(); Draw(); requiresUpdate = false; }
private void InitTextFormatLayout() { if (d2d == null) { return; } TextMetrics metrics; SizeF minSize; CurrentTextFormat = new TextFormat(D2DGraphics.dwriteFactory, foreFont.FamilyName, foreFont.Size); //{ TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center}; CurrentTextFormat.WordWrapping = WordWrapping.NoWrap; CurrentTextLayout = new TextLayout(D2DGraphics.dwriteFactory, txt, CurrentTextFormat, 0, 0); metrics = CurrentTextLayout.Metrics; minSize = new SizeF(metrics.WidthIncludingTrailingWhitespace, metrics.Height); float w = (float)Math.Ceiling(minSize.Width); if (w > boxWidth) { Width = w; } else { Width = boxWidth; } Height = (float)Math.Ceiling(minSize.Height); RawColor4 rawColor = GDIDataD2DUtils.TransToRawColor4(foreColor); //RedDrawingeffect = new ColorDrawingEffect(rawColor); //CurrentTextLayout.SetDrawingEffect(RedDrawingeffect, new TextRange(0, txt.Count() - 1)); SolidColorBrush brush = new SolidColorBrush(d2d.renderTarget, rawColor); CurrentTextLayout.SetDrawingEffect(brush, new TextRange(0, txt.Count())); //BlueDrawingEffect = new ColorDrawingEffect(SharpDX.Color.Blue); //GreenDrawingEffect = new ColorDrawingEffect(SharpDX.Color.Green); //CurrentTextLayout.SetFontSize(10.0f, new TextRange(6, 14)); //CurrentTextLayout.SetDrawingEffect(BlueDrawingEffect, new TextRange(14, 7)); //CurrentTextLayout.SetDrawingEffect(GreenDrawingEffect, new TextRange(21, 8)); //CurrentTextLayout.SetUnderline(true, new TextRange(0, 20)); //CurrentTextLayout.SetStrikethrough(true, new TextRange(22, 7)); //SolidColorBrush greenBrush = new SolidColorBrush(d2d.renderTarget, SharpDX.Color.Black); //CurrentTextLayout.SetDrawingEffect(greenBrush, new TextRange(10, 1)); /* * // Set a stylistic typography * using (var typo = new Typography(d2d.dwriteFactory)) * { * typo.AddFontFeature(new FontFeature(FontFeatureTag.StylisticSet7, 1)); * CurrentTextLayout.SetTypography(typo, CurrentTextRange); * } */ }
public void SetColor(int start, int length, Brush brush) { Contract.Requires <ArgumentNullException>(brush != null, "brush"); TextLayout.SetDrawingEffect(brush, new TextRange(start, length)); }
static void Main(string[] args) { mainForm = new RenderForm("Advanced Text rendering demo"); d2dFactory = new D2DFactory(); dwFactory = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared); textRenderer = new CustomColorRenderer(); CreateResources(); var bgcolor = new Color4(0.1f, 0.1f, 0.1f, 1.0f); //This is the offset where we start our text layout Vector2 offset = new Vector2(202.0f, 250.0f); textFormat = new TextFormat(dwFactory, "Arial", FontWeight.Regular, FontStyle.Normal, 16.0f); textLayout = new TextLayout(dwFactory, introText, textFormat, 300.0f, 200.0f); //Apply various modifications to text textLayout.SetUnderline(true, new TextRange(0, 5)); textLayout.SetDrawingEffect(greenBrush, new TextRange(10, 20)); textLayout.SetFontSize(24.0f, new TextRange(6, 4)); textLayout.SetFontFamilyName("Comic Sans MS", new TextRange(11, 7)); //Measure full layout var textSize = textLayout.Metrics; fullTextBackground = new RectangleF(textSize.Left + offset.X, textSize.Top + offset.Y, textSize.Width, textSize.Height); //Measure text to apply background to var metrics = textLayout.HitTestTextRange(53, 4, 0.0f, 0.0f)[0]; textRegionRect = new RectangleF(metrics.Left + offset.X, metrics.Top + offset.Y, metrics.Width, metrics.Height); //Assign render target and brush to our custom renderer textRenderer.AssignResources(renderTarget, defaultBrush); RenderLoop.Run(mainForm, () => { renderTarget.BeginDraw(); renderTarget.Clear(bgcolor); renderTarget.FillRectangle(fullTextBackground, backgroundBrush); renderTarget.FillRectangle(textRegionRect, redBrush); textLayout.Draw(textRenderer, offset.X, offset.Y); try { renderTarget.EndDraw(); } catch { CreateResources(); } }); d2dFactory.Dispose(); dwFactory.Dispose(); renderTarget.Dispose(); }