コード例 #1
0
        protected override void OnCreateDeviceIndependentResources(Direct2DFactory factory)
        {
            base.OnCreateDeviceIndependentResources(factory);

            _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.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));
            }
        }
コード例 #2
0
ファイル: D2DDraw.cs プロジェクト: windygu/noForms
        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);
        }
コード例 #3
0
        public void DrawText(string text, float x, float y, float fontSize, Direct2DFont font, Direct2DBrush brush)
        {
            var layout = new TextLayout(_fontFactory, text, font, float.MaxValue, float.MaxValue);

            layout.SetFontSize(fontSize, new TextRange(0, text.Length));

            _device.DrawTextLayout(new RawVector2(x, y), layout, brush, DrawTextOptions.NoSnap);

            layout.Dispose();
        }
コード例 #4
0
        public void DrawTextWithBackground(string text, float x, float y, float fontSize, Direct2DFont font, Direct2DBrush brush, Direct2DBrush backgroundBrush)
        {
            var layout = new TextLayout(_fontFactory, text, font, float.MaxValue, float.MaxValue);

            layout.SetFontSize(fontSize, new TextRange(0, text.Length));

            float modifier = fontSize / 4.0f;

            _device.FillRectangle(new RawRectangleF(x - modifier, y - modifier, x + layout.Metrics.Width + modifier, y + layout.Metrics.Height + modifier), backgroundBrush);

            _device.DrawTextLayout(new RawVector2(x, y), layout, brush, DrawTextOptions.NoSnap);

            layout.Dispose();
        }
コード例 #5
0
            public void SetFont(Renderer.TextRange range, IFont font)
            {
                if (Surface.IsDisposing)
                {
                    return;
                }

                if (!(font is DxFont dxFont))
                {
                    throw new ArgumentOutOfRangeException(nameof(font), Error_FactoryObject);
                }
                Convert(range, out SharpDX.DirectWrite.TextRange dxRange);
                TextLayout.SetFontFamilyName(dxFont.Family, dxRange);
                TextLayout.SetFontSize(dxFont.Size, dxRange);
            }
コード例 #6
0
        private void InitializeMultiformattedText()
        {
            _mainForm.MultiformattedTextRenderPanel.InitializeDevice();
            _multiformattedTextBrush = new SolidColorBrush(MultiformattedRenderTarget, new Color4(0.0f, 0.0f, 0.0f));
            string text = "Hello World using   DirectWrite!";

            _multiformattedLayout = _writeFactory.CreateTextLayout(text, _textFormat, 640.0f, 480.0f);
            _multiformattedLayout.SetFontSize(100.0f, new TextRange(20, 6));
            _multiformattedLayout.SetUnderline(true, new TextRange(20, 11));
            _multiformattedLayout.SetFontWeight(FontWeight.Bold, new TextRange(20, 11));
            using (Typography typography = _writeFactory.CreateTypography())
            {
                typography.AddFeature(new FontFeature(FontFeatureTag.StylisticSet7, 1));
                _multiformattedLayout.SetTypography(typography, new TextRange(0, text.Length));
            }
        }
コード例 #7
0
        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));
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
 protected override void DoApply(TextLayout layout, TextRange range)
 {
     layout.SetFontSize(this.Size, range);
 }
コード例 #10
0
ファイル: FontSizeNode.cs プロジェクト: dotprodukt/dx11-vvvv
 protected override void DoApply(TextLayout layout, TextRange range)
 {
     layout.SetFontSize(this.Size, range);
 }
コード例 #11
0
ファイル: HelloWorldSample.cs プロジェクト: zhandb/slimdx
		private void InitializeMultiformattedText()
		{
			_mainForm.MultiformattedTextRenderPanel.InitializeDevice();
			_multiformattedTextBrush = new SolidColorBrush(MultiformattedRenderTarget, new Color4(0.0f, 0.0f, 0.0f));
			string text = "Hello World using   DirectWrite!";
			_multiformattedLayout = _writeFactory.CreateTextLayout(text, _textFormat, 640.0f, 480.0f);
			_multiformattedLayout.SetFontSize(100.0f, new TextRange(20, 6));
			_multiformattedLayout.SetUnderline(true, new TextRange(20, 11));
			_multiformattedLayout.SetFontWeight(FontWeight.Bold, new TextRange(20, 11));
			using (Typography typography = _writeFactory.CreateTypography())
			{
				typography.AddFeature(new FontFeature(FontFeatureTag.StylisticSet7, 1));
				_multiformattedLayout.SetTypography(typography, new TextRange(0, text.Length));
			}
		}
コード例 #12
0
ファイル: FontSizeNode.cs プロジェクト: hameleon-ed/dx11-vvvv
 protected override void Apply(TextLayout layout, bool enable, int slice)
 {
     var res = layout.SetFontSize(size[slice], new TextRange(ffrom[slice], fto[slice]));
 }
コード例 #13
0
 protected override void Apply(TextLayout layout, bool enable, int slice)
 {
     var res = layout.SetFontSize(size[slice], new TextRange(ffrom[slice], fto[slice]));
 }
コード例 #14
0
        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();
        }