123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- 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 变量和属性
- /// <summary>
- /// 数据源
- /// </summary>
- internal PatternDataItem PatternDataItem { get; set; }
- /// <summary>
- /// 文本设置
- /// </summary>
- [XtraSerializableProperty(XtraSerializationVisibility.Content)]
- public CodeOptions CodeOptions { get; } = new CodeOptions();
- /// <summary>
- /// 字体
- /// </summary>
- private string FontFamily => CodeOptions.FontFamily.ToString();
- /// <summary>
- /// 字号
- /// </summary>
- private int FontSize => CodeOptions.FontSize;
- /// <summary>
- /// 字体类型
- /// </summary>
- private FontStyle FontStyle => CodeOptions.FontStyle;
- /// <summary>
- /// 左边
- /// </summary>
- internal List<PointF> LeftCodePointFs = new List<PointF>();
- /// <summary>
- /// 布身
- /// </summary>
- internal List<PointF> MiddleCodePointFs = new List<PointF>();
- /// <summary>
- /// 余花
- /// </summary>
- internal List<PointF> SurplusCodePointFs = new List<PointF>();
- /// <summary>
- /// 右边
- /// </summary>
- internal List<PointF> RightCodePointFs = new List<PointF>();
- /// <summary>
- /// 循环位置
- /// </summary>
- internal PointF CountFs;
- /// <summary>
- /// 文本框尺寸
- /// </summary>
- 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 对外方法
- /// <summary>
- /// 生成内容
- /// </summary>
- public void GenerateContent(PatternDataItem patternDataItem)
- {
- /* 记录花型数据 */
- PatternDataItem = patternDataItem;
- /* 根据数据创建花型 */
- CreatePatternPart(patternDataItem.PatternData);
- }
- #endregion
- #region 内部方法
- /// <summary>
- /// 根据数据创建花型
- /// </summary>
- 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<PointF> pointFs = new List<PointF>();
- 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);
- }
- /// <summary>
- /// 添加花型控件
- /// </summary>
- private void AddThreadBrick(List<PointF> 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++;
- }
- }
- /// <summary>
- /// 获取文本大小和位置
- /// </summary>
- 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);
- }
- /// <summary>
- /// 创建文本
- /// </summary>
- 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,
- };
- }
- /// <summary>
- /// 设置样式
- /// </summary>
- 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)
- };
- }
- /// <summary>
- /// 获取字体高度
- /// </summary>
- private static float GetFontHeight(Font font)
- {
- var metrics = new FontMetrics(font, GraphicsUnit.Document);
- var height = metrics.CalculateHeight(1);
- return height;
- }
- /// <summary>
- /// 设置字体
- /// </summary>
- private static Font CreateFont(string fontFamily, int fontSize, FontStyle fontStyle) =>
- new Font(fontFamily, fontSize, fontStyle);
- #endregion
- }
- }
|