0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FreeTypeSharpを使った文字列描画(複数行を描画する)

Posted at

1.前回の内容

2.目標

改行コードを含んだ文字列の時、複数行の描画を行う。

3.ソースコード

4.変更点

文字列からStringImageDataオブジェクトへ変換するときに、行ごとに配列化したCharImageDataオブジェクトを作成してから渡す。
(行ごとに配列化したCharImageDataオブジェクトはcidListオブジェクトに格納される)

ImageFont.cs

        public StringImageData Render(string text)
        {
            if (string.IsNullOrEmpty(text)) return StringImageData.Empty;

            var charList = text.ReplaceLineEndings("\n").ToCharArray();
            var cidList = new List<List<CharImageData>>();
            cidList.Add(new List<CharImageData>());

            int baseHeight = (int)_FaceFacade.FaceRec->size->metrics.height >> 6;
            int fontHeight = baseHeight;
            int fontWidth = (int)_FaceFacade.FaceRec->size->metrics.max_advance >> 6;
            foreach (var c in charList)
            {
                switch (c)
                {
                    case '\r':
                    case '\n':
                        cidList.Add(new List<CharImageData>());
                        break;

                    case '\t':
                        var tab = new CharImageData(fontWidth * 2, 0, 0, 0, 0);
                        cidList.Last().Add(tab);
                        break;

                    default:
                        var cid = CreateCharImageData(c);
                        var tmpHeight = baseHeight - cid.BearingY + cid.Height;
                        if (fontHeight < tmpHeight) fontHeight = tmpHeight;
                        cidList.Last().Add(cid);
                        break;
                }
            }

            return new StringImageData(text, baseHeight, fontHeight, fontWidth, cidList);
        }


5.まとめ

あまりスマートなやり方ではないが、改行を描画側に任せることができた。
これで、描画範囲に合わせて自動改行する機能の下地ができたので良しとする。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?