示例#1
0
        public override BuiltRichText BuildText(IList <RichTextNode> nodes, int width, float sizeMultiplier = 1)
        {
            if (!customLoader.Valid)
            {
                CreateCustomCollection();
            }
            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 : (16 * sizeMultiplier)
                        ),
                    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))
                    {
                        if (customCollection.FindFamilyName(text.FontName, out int _))
                        {
                            layout.SetFontCollection(customCollection, range);
                        }
                        layout.SetFontFamilyName(text.FontName, range);
                        lastFont = text.FontName;
                    }
                    if (text.FontSize > 0)
                    {
                        layout.SetFontSize(text.FontSize * sizeMultiplier, range);
                        lastSize = text.FontSize * sizeMultiplier;
                    }
                    layout.SetDrawingEffect(new ColorDrawingEffect(text.Color, text.Shadow), range);
                    startIdx += text.Contents.Length;
                }
                layouts.Add(layout);
            }
            //Return
            var built = new DirectWriteBuiltText(this)
            {
                Layout = layouts, Width = width
            };

            built.CacheQuads();
            return(built);
        }