コード例 #1
0
ファイル: TextEngine.cs プロジェクト: sermetk/Orikivo.Classic
        public static Bitmap RenderString(string str, PixelRenderingOptions options, ulong?fontId = null, int padding = 0)
        {
            char[]   map     = str.ToCharArray();
            FontFace oldFont = options.Font;

            options.Font = fontId.HasValue ? FontManager.FontMap.GetFont(fontId.Value) : options.Font;
            FontFace font = options.Font;

            Point pointer = new Point(padding, padding); // This is where the top left letter bitmap will start from.
            Size  s       = str.GetRenderBox(font);      // Correct render box built.

            s.Width  += padding * 2;
            s.Height += padding * 2;
            s.Debug();
            Rectangle area = new Rectangle(pointer, s);

            Bitmap render = new Bitmap(s.Width, s.Height);

            using (Graphics g = Graphics.FromImage(render))
            {
                Bitmap[] renders = RenderChars(map, options);

                bool overhung = false;

                int x = 0;
                for (int i = 0; i < map.Length; i++)
                {
                    if (map[i] == '\n')
                    {
                        if (overhung)
                        {
                            pointer.Y += font.Overhang;
                            overhung   = false;
                        }
                        pointer.Y += font.Ppu.Height + font.Padding;
                        pointer.X  = padding;
                        continue;
                    }
                    if (map[i] == ' ')
                    {
                        pointer.X += font.Spacing;
                        continue;
                    }
                    if (map[i] == '⠀')  // Invisible Braille Block
                    {
                        pointer.X += 4; // The width of all braille.
                        continue;
                    }
                    if (font.HasFallback.HasValue)
                    {
                        if (!font.HasFallback.Value)
                        {
                            if (!font.Search(map[i], options.FontType, out (int i, int x, int y)index).Exists())
                            {
                                continue;
                            }
                        }
                    }
                    if (map[i].Overhangs())
                    {
                        overhung   = true;
                        pointer.Y += font.Overhang;
                    }
                    // if there is no fallback, if the letter doesnt exist, skip it.

                    using (Bitmap b = renders[x])
                    {
                        Rectangle rct = new Rectangle(pointer, b.Size);

                        g.SetClip(rct);
                        g.DrawImage(b, rct);
                        g.ResetClip();

                        pointer.X += rct.Width + font.Padding;
                    }

                    if (map[i].Overhangs())
                    {
                        pointer.Y -= font.Overhang;
                    }

                    x++;
                }
            }

            options.Font = oldFont;
            return(render.Color(options.Palette));
        }