Example #1
0
        public void DrawText(TextRun run, Color color)
        {
            double yOffset = FontStyler.ScaleOffset(run.Typeface.Style, run.Typeface.Size);
            double size    = FontStyler.ScaleSize(run.Typeface.Style, run.Typeface.Size);

            G.DrawString(run.Text, ToXFont(run.Typeface.Font, size, run.Typeface.Weight), ToXBrush(color), new XPoint(Offset.X, Offset.Y + yOffset));
        }
Example #2
0
        public static List <LineNode> Create(Leaf leaf, string?text)
        {
            if (text == null)
            {
                return(new List <LineNode>());
            }

            var font        = leaf.Font ?? throw new ArgumentException("Specified element missing font");
            var size        = FontStyler.ScaleSize(leaf.FontStyle, leaf.FontSize);
            var spaceWidth  = GetWidth(" ");
            var hyphenWidth = GetWidth("-");
            var stretch     = Math.Max(0, spaceWidth / 2);
            var shrink      = Math.Max(0, spaceWidth / 3);

            var chunks = Chunkenize(text).ToArray();
            var nodes  = new List <LineNode>();

            Glue(text, out bool start, out bool end);

            Hyphenator?hyph = leaf.Document?.Hyphenator;

            if (start)
            {
                nodes.Add(LineNode.Glue(spaceWidth, stretch, shrink));
            }

            for (int i = 0; i < chunks.Length; i++)
            {
                if (i > 0)
                {
                    var glue = LineNode.Glue(spaceWidth, stretch, shrink);
                    nodes.Add(glue);
                }

                if (hyph != null)
                {
                    var hyphenated = hyph.Hyphenate(chunks[i]);

                    for (int j = 0; j < hyphenated.Length; j++)
                    {
                        var box = LineNode.Box(GetWidth(hyphenated[j]), hyphenated[j]);
                        box.Element = leaf;
                        nodes.Add(box);

                        if (j < hyphenated.Length - 1)
                        {
                            var penalty = LineNode.Penalty(hyphenWidth, 10, true);
                            penalty.Element = leaf;
                            nodes.Add(penalty);
                        }
                    }
                }
                else
                {
                    var box = LineNode.Box(GetWidth(chunks[i]), chunks[i]);
                    box.Element = leaf;
                    nodes.Add(box);
                }
            }

            if (end)
            {
                nodes.Add(LineNode.Glue(spaceWidth, stretch, shrink));
            }

            return(nodes);

            double GetWidth(string value)
            {
                return(font.GetWidth(value, size, leaf.FontWeight));
            }
        }