using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text.RegularExpressions; using CustomControls.Pattern.DataModels; using DevExpress.Utils; using DevExpress.Utils.Serializing; using DevExpress.XtraPrinting; using DevExpress.XtraPrinting.BrickExporters; using DevExpress.XtraPrinting.Native; using DevExpress.XtraReports.UI; namespace CustomControls.Pattern { [BrickExporter(typeof(PatternBrickExporter))] public class PatternBrick : PanelBrick { #region 变量和属性 /// /// 数据源 /// internal PatternDataItem PatternDataItem { get; set; } /// /// 文本设置 /// [XtraSerializableProperty(XtraSerializationVisibility.Content)] public CodeOptions CodeOptions { get; } = new CodeOptions(); /// /// 字体 /// private string FontFamily => CodeOptions.FontFamily.ToString(); /// /// 字号 /// private int FontSize => CodeOptions.FontSize; /// /// 字体类型 /// private FontStyle FontStyle => CodeOptions.FontStyle; /// /// 左边 /// internal List LeftCodePointFs = new List(); /// /// 布身 /// internal List MiddleCodePointFs = new List(); /// /// 余花 /// internal List SurplusCodePointFs = new List(); /// /// 右边 /// internal List RightCodePointFs = new List(); /// /// 循环位置 /// internal PointF CountFs; /// /// 文本框尺寸 /// internal float FontHeight; private int index; #endregion #region 构造函数 public PatternBrick() : this(null) { } public PatternBrick(IBrickOwner brickOwner) : base(brickOwner) { BackColor = Color.Transparent; Sides = BorderSide.None; } #endregion public override string BrickType => nameof(PatternBrick); #region 对外方法 /// /// 生成内容 /// public void GenerateContent(PatternDataItem patternDataItem) { /* 记录花型数据 */ PatternDataItem = patternDataItem; /* 根据数据创建花型 */ CreatePatternPart(patternDataItem.PatternData); } #endregion #region 内部方法 /// /// 根据数据创建花型 /// private void CreatePatternPart(string patternData) { if (string.IsNullOrWhiteSpace(patternData)) { return; } /* 获取字体高度 */ var font = CreateFont(FontFamily, FontSize, FontStyle); FontHeight = GetFontHeight(font); /* 拆分字符串,获取左、中、右、余花排列 */ var threads = patternData.Split('|'); /* 循环添加 */ index = 0; for (int i = 0; i < threads.Length; i++) { List pointFs = new List(); switch (i) { case 0: pointFs = LeftCodePointFs; break; case 1: pointFs = MiddleCodePointFs; break; case 2: pointFs = SurplusCodePointFs; break; case 3: pointFs = RightCodePointFs; break; } AddThreadBrick(pointFs, threads[i], font); } /* 获取循环位置 */ var allPointFs = LeftCodePointFs.Concat(MiddleCodePointFs).Concat(SurplusCodePointFs).Concat(RightCodePointFs).ToList(); var lowestPointF = allPointFs.OrderByDescending(x => x.Y).First(); var countX = MiddleCodePointFs[0].X + (MiddleCodePointFs[MiddleCodePointFs.Count - 1].X - MiddleCodePointFs[0].X) / 2; CountFs = new PointF(countX, lowestPointF.Y + PatternDataItem.RowSpace + FontHeight); /* 添加循环控件 */ var rectangleF = new RectangleF(countX, lowestPointF.Y + PatternDataItem.RowSpace, FontHeight * 2, FontHeight); var countText = CreateTextBirck(threads[threads.Length - 1], font, rectangleF); Bricks.Add(countText); } /// /// 添加花型控件 /// private void AddThreadBrick(List pointFs, string thread, Font font) { /* 获取所有字母 */ var strLatter = Regex.Replace(thread, "[0-9]", "", RegexOptions.IgnoreCase); /* 循环添加 */ for (var i = 0; i < strLatter.Length; i++) { Enum.TryParse(strLatter[i].ToString(), out ColorCode code); /* 确定控件位置 */ var rectangleF = GetRectangleF(index, (int)code, FontHeight); /* 获取文本 */ var index1 = i == 0 ? 0 : thread.IndexOf(strLatter[i - 1]); var index2 = thread.IndexOf(strLatter[i]); var text = thread.Substring(index1 + 1, index2 - index1 - 1); /* 创建控件 */ var textBrick = CreateTextBirck(text, font, rectangleF); /* 记录控件坐标 */ pointFs.Add(rectangleF.Location); /* 添加控件 */ Bricks.Add(textBrick); thread = thread.Remove(0, index2); index++; } } /// /// 获取文本大小和位置 /// private RectangleF GetRectangleF(int index, int codeIndex, float heigh) { var x = PatternDataItem.ColumnSpace * index + 5f; var y = PatternDataItem.RowSpace * codeIndex + 1f; return new RectangleF(x, y, heigh * 2, heigh); } /// /// 创建文本 /// private static TextBrick CreateTextBirck(string text, Font font, RectangleF bounds) { return new TextBrick(CreateBrickStyle(font)) { Rect = bounds, Text = text, HorzAlignment = HorzAlignment.Center, VertAlignment = VertAlignment.Center, }; } /// /// 设置样式 /// private static BrickStyle CreateBrickStyle(Font font) { return new XRControlStyle() { Font = font, Padding = new PaddingInfo(1, 1, 0, 0, 96), StringFormat = BrickStringFormat.Create(TextAlignment.TopLeft, true, StringTrimming.EllipsisCharacter, false) }; } /// /// 获取字体高度 /// private static float GetFontHeight(Font font) { var metrics = new FontMetrics(font, GraphicsUnit.Document); var height = metrics.CalculateHeight(1); return height; } /// /// 设置字体 /// private static Font CreateFont(string fontFamily, int fontSize, FontStyle fontStyle) => new Font(fontFamily, fontSize, fontStyle); #endregion } }