コード例 #1
0
        public static XStringFormat GetFormat(StringFormatEx stringFormat)
        {
            var format = new XStringFormat();

            format.Alignment     = (XStringAlignment)Enum.Parse(typeof(XStringAlignment), stringFormat.Alignment.ToString());
            format.LineAlignment = (XLineAlignment)Enum.Parse(typeof(XLineAlignment), stringFormat.LineAlignment.ToString());
            return(format);
        }
コード例 #2
0
        private static string DrawText(string txt, RectangleF bounds, StringFormatEx format = null)
        {
            var canvas = new TxtDrawingCanvas(new Size(10, 10), "\n");

            canvas.DrawString(txt, null, null, bounds, format ?? StringFormatEx.GetGenericDefault);
            using var writer = new StringWriter();
            canvas.Write(writer);
            return(writer.ToString());
        }
コード例 #3
0
        public static IEnumerable <Line> SplitLines(string txt, StringFormatEx format, int width)
        {
            switch (format.WrapMode)
            {
            case WrapMode.NoWrap:
                return(CharWrap(txt, int.MaxValue));

            case WrapMode.CharWrap:
                return(CharWrap(txt, width));

            case WrapMode.WordWrap:
                return(WordWrap(txt, width));

            default:
                throw new ArgumentOutOfRangeException("format.WrapMode");
            }
        }
コード例 #4
0
        private static XParagraphAlignment GetAlignment(StringFormatEx format)
        {
            if (format.IsJustified)
            {
                return(XParagraphAlignment.Justify);
            }
            switch (format.LineAlignment)
            {
            case StringAlignmentEx.Near:
                return(XParagraphAlignment.Left);

            case StringAlignmentEx.Center:
                return(XParagraphAlignment.Center);

            case StringAlignmentEx.Far:
                return(XParagraphAlignment.Right);
            }
            return(XParagraphAlignment.Default);
        }
コード例 #5
0
        public void DrawString(string value, FontInfo font, BrushEx brush, RectangleF layoutRectangle,
                               StringFormatEx format)
        {
            var pts = new[] { layoutRectangle.Location, new PointF(layoutRectangle.Right, layoutRectangle.Bottom) };

            Transform.TransformPoints(pts);

            var w = pts[1].X - pts[0].X;
            var h = pts[1].Y - pts[0].Y;

            var textItem = new TextItem()
            {
                Value  = value,
                Bounds = new Rectangle(
                    (int)(pts[0].X / _settingsFontSizeTwips.Width),
                    (int)(pts[0].Y / _settingsFontSizeTwips.Height),
                    (int)(w / _settingsFontSizeTwips.Width),
                    (int)(h / _settingsFontSizeTwips.Height)),
                Format = format
            };

            _items.Add(textItem);
        }
コード例 #6
0
        void IDrawingCanvas.DrawString(string value, FontInfo font, BrushEx brush, RectangleF rect, StringFormatEx format)
        {
            XGraphicsContainer clipState = _graphics.BeginContainer();
            var xRect = PdfConverter.Convert(rect);
            var xFont = _fonts.GetPdfFont(font);

            _graphics.IntersectClip(xRect);
            var resultRect = _graphics.MeasureString(value, xFont, GetFormat(format));

            if ((format.WrapMode == WrapMode.NoWrap && !value.Contains('\n')) ||
                (resultRect.Width * PdfConverter.TwipsPerPoint <= rect.Width))
            {
                _graphics.DrawString(value, xFont, (BrushBase)brush, xRect, GetFormat(format));
                return;
            }

            if (format.WrapMode == WrapMode.NoWrap && value.Contains('\n'))
            {
                var lines = value.Split('\n');
                var y     = xRect.Y;
                for (var i = 0; i < lines.Length; i++)
                {
                    var line = lines[i].Trim('\r');
                    _graphics.DrawString(line, xFont, (BrushBase)brush, new XRect(xRect.X, y, xRect.Width, resultRect.Height), GetFormat(format));
                    y += resultRect.Height;
                }
                return;
            }

            // http://developer.th-soft.com/developer/2015/07/17/pdfsharp-improving-the-xtextformatter-class-measuring-the-height-of-the-text/
            // http://developer.th-soft.com/developer/2015/09/21/xtextformatter-revisited-xtextformatterex2-for-pdfsharp-1-50-beta-2/
            var tf = new XTextFormatter(_graphics)
            {
                Alignment = GetAlignment(format)
            };

            tf.DrawString(value, xFont, (BrushBase)brush, xRect);

            _graphics.EndContainer(clipState);
        }
コード例 #7
0
 public string Format(string template, object obj)
 {
     return(StringFormatEx.Format(obj, template));
 }