chenhaozhe
1 天以前 06e105234f820a2e791cac3e6c7012cc8bf4d905
WebAPI/Controllers/WebAPIController.cs
@@ -24129,6 +24129,11 @@
            // 匹配 {{ 字段名 }} 所需正则表达式 支持中文
            var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Compiled);
            // 匹配 矩形框 位置 宽高 所需正则表达式
            var RectRegex = new Regex(@"^\^FO(\d+),(\d+)\^GB(\d+),(\d+)", RegexOptions.Compiled);
            // 匹配 模板字符串 所在行 位置 宽高 所需正则表达式 支持中文
            var fieldRegex = new Regex(@"\^FO(\d+),(\d+)\^A[A-Z]+,(\d+),(\d+).*\{\{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}\}", RegexOptions.Compiled);
            if (ptMode == "ZPL")
            {
                suffix = ".zpl";
@@ -24201,6 +24206,7 @@
                        {
                            string currentLine = lines[i];
                            string preLine = "";
                            string concatStr = "";
                            if(i>0)
                            {
                                // 获取当前行的上一行,用于判断是否是矩形框
@@ -24213,20 +24219,26 @@
                                continue;
                            }
                            // 正则表达式匹配 {{ 字段名 }} 所在行的字段,如果有,则进行替换,没有,则默认为空字符串
                            currentLine = regex.Replace(currentLine, match =>
                            // 判断矩形框,如果前一行是矩形框,则需判断该行字符串长度是否超长
                            if(!getConcatStr(rectRegex: RectRegex, fieldRegex: fieldRegex,
                                PreLine: preLine, CurrLine: currentLine, fieldObject: item, ref concatStr))
                            {
                                string fieldName = match.Groups[1].Value.Trim();
                                if (msg != null && item.ContainsKey(fieldName))
                                // 正则表达式匹配 {{ 字段名 }} 所在行的字段,如果有,则进行替换,没有,则默认为空字符串
                                concatStr = regex.Replace(currentLine, match =>
                                {
                                    return item[fieldName]?.ToString() ?? "";
                                }
                                return "";
                            });
                            // 将所有 \n 的换行符 替换为 \r\n
                                    string fieldName = match.Groups[1].Value.Trim();
                                    if (msg != null && item.ContainsKey(fieldName))
                                    {
                                        return item[fieldName]?.ToString() ?? "";
                                    }
                                    return "";
                                });
                            }
                            sb.Append(currentLine);
                            // 将所有 \n 的换行符 替换为 \r\n
                            sb.Append(concatStr);
                            sb.Append("\r\n"); // 标准换行
                        }
                    }
@@ -24251,6 +24263,69 @@
            }
        }
        private bool getConcatStr(Regex rectRegex, Regex fieldRegex,
            string PreLine, string CurrLine, JObject fieldObject, ref string concatStr)
        {
            var rectRegexResult = rectRegex.Match(PreLine);
            var fieldRegexResult = fieldRegex.Match(CurrLine);
            if (!rectRegexResult.Success)
            {
                return false;
            }
            if(!fieldRegexResult.Success)
            {
                return false;
            }
            // 矩形 位置 宽高
            int rectLocationX = int.Parse(rectRegexResult.Groups[1].Value);
            int rectLocationY = int.Parse(rectRegexResult.Groups[2].Value);
            int rectWidth = int.Parse(rectRegexResult.Groups[3].Value);
            int rectHeight = int.Parse(rectRegexResult.Groups[4].Value);
            // 模板字符串 位置 字符宽高
            int fieldLocationX = int.Parse(fieldRegexResult.Groups[1].Value);
            int fieldLocationY = int.Parse(fieldRegexResult.Groups[2].Value);
            int fieldWidth = int.Parse(fieldRegexResult.Groups[3].Value);
            int fieldHeight = int.Parse(fieldRegexResult.Groups[4].Value);
            string fieldName = fieldRegexResult.Groups[5].Value;
            string fieldValue = fieldObject[fieldName].ToString() ?? "";
            if(string.IsNullOrWhiteSpace(fieldValue))
            {
                return false;
            }
            if(fieldValue.Length * fieldWidth > rectWidth)
            {
                // 字符串长度大于矩形的最大长度 则文本需要从矩形的Y位置开始渲染
                fieldLocationY = rectLocationY;
                // 获取换行所需的行数
                int lineCount = int.Parse(Math.Ceiling((float)fieldValue.Length / fieldWidth).ToString());
                // 计算换行后的最大行数 * 字符高度 是否大于矩形最大高度
                if(lineCount * fieldHeight > rectHeight)
                {
                    fieldWidth = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
                    fieldHeight = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
                }
                for(uint i=0;i<lineCount; i++)
                {
                    concatStr += fieldRegex.Replace(CurrLine, match =>
                    {
                        return "";
                    });
                }
            }
            return true;
        }
        #endregion
        [HttpGet]
        [Route("Web/getSysParameter")]
        public object getSysParameter(string HClientID)
@@ -24296,4 +24371,3 @@
        }
    }
}
#endregion