<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: andris</title>
    <description>The latest articles on DEV Community by andris (@brianelete).</description>
    <link>https://dev.to/brianelete</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1585%2FSbUTPYjc.jpg</url>
      <title>DEV Community: andris</title>
      <link>https://dev.to/brianelete</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brianelete"/>
    <language>en</language>
    <item>
      <title>Text wrap with SkiaSharp</title>
      <dc:creator>andris</dc:creator>
      <pubDate>Mon, 15 Jun 2020 09:49:40 +0000</pubDate>
      <link>https://dev.to/brianelete/text-wrap-with-skiasharp-5722</link>
      <guid>https://dev.to/brianelete/text-wrap-with-skiasharp-5722</guid>
      <description>&lt;p&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I am working on &lt;a href="https://morepuzzles.com/"&gt;https://morepuzzles.com/&lt;/a&gt; where we are drawing our puzzles with SkiaSharp. More on that in this post: &lt;a href="https://medium.com/@brianelete/a-brief-experience-with-skiasharp-20478ca05ef2"&gt;https://medium.com/@brianelete/a-brief-experience-with-skiasharp-20478ca05ef2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to wrap text lines&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My main issue when displaying text with Skia was that the lines were sometimes long and went out of the canvas or the current column. Skia’s DrawText method works like that it starts at the given position and draws the text in one line with the given paint (what is containing the text size). Lucky for us we can measure text and break lines where we need them to be wrapped. That may be the edge of the page or at the start of a new column (we can add margins too).&lt;/p&gt;

&lt;p&gt;We use bounding boxes inside the document to determine the position and the relative size of things. You can measure the text height and length using a given font size so you can calculate what font size fits the box and where you should wrap each line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void WrapLines(string longLine, float lineLengthLimit, SKCanvas canvas, SKPaint defPaint)
{
    var wrappedLines = new List&amp;lt;string&amp;gt;();
    var lineLength = 0f;
    var line = "";
    foreach (var word in longLine.Split(' '))
    {
        var wordWithSpace = word + " ";
        var wordWithSpaceLength = defPaint.MeasureText(wordWithSpace);
        if (lineLength + wordWithSpaceLength &amp;gt; lineLengthLimit)
        {
            wrappedLines.Add(line);
            line = "" + wordWithSpace;
            lineLength = wordWithSpaceLength;
        }
        else
        {
            line += wordWithSpace;
            lineLength += wordWithSpaceLength;
        }
    }
    var x = 0;
    var y = 0;
    foreach (var wrappedLine in wrappedLines)
    {
        canvas.DrawText(wrappedLine, x, y, paint);
        y += paint.FontSpacing;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a slightly modified example to fit this post’s topic. You can of course separate the line wrapping and the drawing itself if it better fits your needs.&lt;/p&gt;

</description>
      <category>skiasharp</category>
      <category>csharp</category>
      <category>wordwrap</category>
      <category>example</category>
    </item>
  </channel>
</rss>
