Example #1
0
        private void _DrawText(Graphics g, Size bmpSize, CFont font, List <CTextElement> elements)
        {
            Font fo = CFonts.GetSystemFont(font);

            float maxHeight = elements.Select(el => el.Height).Max();
            int   lineCount = elements.Last().Line + 1;

            //Have to use size in em not pixels!
            float   emSize        = fo.Size * fo.FontFamily.GetCellAscent(fo.Style) / fo.FontFamily.GetEmHeight(fo.Style);
            float   outlineSize   = CFonts.GetOutlineSize(font) * font.Height;
            SColorF outlineColorF = CFonts.GetOutlineColor(font);

            outlineColorF.A = outlineColorF.A * _TextColor.A;

            using (var path = new GraphicsPath())
                using (var pen = new Pen(outlineColorF.AsColor(), outlineSize / 2))
                {
                    pen.LineJoin  = LineJoin.Round;
                    pen.Alignment = PenAlignment.Outset;
                    float top        = (bmpSize.Height - _MarginBottom - _MarginTop - maxHeight * lineCount) / 2 + _MarginTop;
                    int   nextLineEl = 0;
                    for (int i = 0; i < lineCount; i++)
                    {
                        int firstEl = nextLineEl;
                        for (; nextLineEl < elements.Count; nextLineEl++)
                        {
                            if (elements[nextLineEl].Line > i)
                            {
                                break;
                            }
                        }

                        string line = elements.GetRange(firstEl, nextLineEl - firstEl).Aggregate("", (current, element) => current + element.Text);
                        float  left;
                        if (lineCount == 1 || (i == 1 && lineCount == 3))
                        {
                            //Center Text if this is the only line or the middle line
                            float width = _GetWidth(elements, firstEl, nextLineEl - 1);
                            left = (bmpSize.Width - _MarginLeft - _MarginRight - width) / 2 + _MarginLeft;
                        }
                        else if (i == lineCount - 1)
                        {
                            //Place last line at right
                            float width = _GetWidth(elements, firstEl, nextLineEl - 1);
                            left = bmpSize.Width - width - _MarginRight;
                        }
                        else
                        {
                            left = _MarginLeft;
                        }
                        //g.DrawString(line, fo, new SolidBrush(_TextColor.AsColor()), left, top, StringFormat.GenericTypographic);
                        path.AddString(line, fo.FontFamily, (int)fo.Style, emSize, new PointF(left, top), StringFormat.GenericTypographic);
                        top += maxHeight + _LineSpace;
                    }
                    g.DrawPath(pen, path);
                    g.FillPath(new SolidBrush(_TextColor.AsColor()), path);
                }
        }
Example #2
0
        public Bitmap GetCover(string text, string firstCoverPath)
        {
            if (!_Valid)
            {
                return(null);
            }
            text = CLanguage.Translate(_Theme.Text.Text.Replace("%TEXT%", text));
            using (Bitmap bmpImage = new Bitmap(_Image))
            {
                Bitmap bmp = new Bitmap(bmpImage.Width, bmpImage.Height, PixelFormat.Format32bppArgb);
                try
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.SmoothingMode     = SmoothingMode.AntiAlias;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.TextRenderingHint = TextRenderingHint.AntiAlias;

                        _DrawBackground(g, bmpImage, firstCoverPath);

                        if (text != "")
                        {
                            CFont font = new CFont(_Theme.Text.Font);
                            Font  fo   = CFonts.GetSystemFont(font);
                            IEnumerable <string> textParts = _SplitText(text);
                            List <CTextElement>  elements  = textParts.Select(line => new CTextElement(line, g, fo)).ToList();
                            float factor = _DistributeText(elements, bmp.Width, bmp.Height);
                            foreach (CTextElement element in elements)
                            {
                                element.AdjustSize(factor);
                            }
                            font.Height *= factor / (1f + CFonts.GetOutlineSize(font)); //Adjust for outline size
                            _DrawText(g, bmp.GetSize(), font, elements);
                        }
                    }
                    return(bmp);
                }
                catch (Exception)
                {
                    bmp.Dispose();
                }
            }
            return(null);
        }