public ConfigDialog(IPluginHost pluginHost) { InitializeComponent(); m_PluginHost = pluginHost; if (File.Exists("StrengthReportLayouts.xml")) { m_LayoutBox = LayoutBox.LoadFromFile("StrengthReportLayouts.xml"); } else { m_LayoutBox = LayoutBox.LoadFromString(Resources.LayoutsDefault); } foreach (Layout l in m_LayoutBox) { layoutBoxBindingSource.Add(l); } if (File.Exists("StrengthReportTemplates.xml")) { m_TemplateBox = TemplateBox.LoadFromFile("StrengthReportTemplates.xml"); } else { m_TemplateBox = TemplateBox.LoadFromString(Resources.TemplatesDefault); } foreach (Template l in m_TemplateBox) { templatesBinginSource.Add(l); } tabFormat.SelectedIndex = 1; }
public ConfigDialog(IPluginHost pluginHost) { InitializeComponent(); m_PluginHost = pluginHost; if (File.Exists("StrengthReportLayouts.xml")) { m_LayoutBox = LayoutBox.LoadFromFile("StrengthReportLayouts.xml"); } else { m_LayoutBox = LayoutBox.LoadFromString(Resources.LayoutsDefault); } foreach (Layout l in m_LayoutBox) layoutBoxBindingSource.Add(l); if (File.Exists("StrengthReportTemplates.xml")) { m_TemplateBox = TemplateBox.LoadFromFile("StrengthReportTemplates.xml"); } else { m_TemplateBox = TemplateBox.LoadFromString(Resources.TemplatesDefault); } foreach (Template l in m_TemplateBox) templatesBinginSource.Add(l); tabFormat.SelectedIndex = 1; }
private static double CalculateTop(TemplateBox box, double fy, List <MeasuredLine> lines) { var textHeight = lines.Sum(l => l.Height); var top = box.Top * fy; var bottom = top + (box.Height * fy); var middle = top + (box.Height * fy * 0.5); switch (box.Alignment) { case TextAlignment.TopLeft: case TextAlignment.TopCentre: case TextAlignment.TopRight: return(top); case TextAlignment.MidlineLeft: case TextAlignment.MidlineCentre: case TextAlignment.MidlineRight: return(middle - (textHeight * 0.5)); case TextAlignment.BottomLeft: case TextAlignment.BottomCentre: case TextAlignment.BottomRight: return(bottom - textHeight); default: return(top); } }
private static XStringFormat MapAlignments(TemplateBox box) { switch (box.Alignment) { case TextAlignment.TopLeft: return(XStringFormats.TopLeft); case TextAlignment.TopCentre: return(XStringFormats.TopCenter); case TextAlignment.TopRight: return(XStringFormats.TopRight); case TextAlignment.MidlineLeft: return(XStringFormats.CenterLeft); case TextAlignment.MidlineCentre: return(XStringFormats.Center); case TextAlignment.MidlineRight: return(XStringFormats.CenterRight); case TextAlignment.BottomLeft: return(XStringFormats.BottomLeft); case TextAlignment.BottomCentre: return(XStringFormats.BottomCenter); case TextAlignment.BottomRight: return(XStringFormats.BottomRight); default: return(XStringFormats.Default); } }
private void RenderTextInBox(XFont font, XGraphics gfx, TemplateBox box, double fx, double fy, string str, XRect space, XStringFormat align) { var boxLeft = box.Left * fx; var boxWidth = box.Width * fx; var boxHeight = box.Height * fy; var size = gfx.MeasureString(str, font); if (box.ShrinkToFit && box.WrapText && (size.Width > boxWidth || size.Height > boxHeight)) // try to change the string to match box aspect { var altFont = ShrinkFontAndWrapStringToFit(str, boxWidth, boxHeight, gfx, font, out var lines); RenderWrappedLines(altFont, gfx, box, fy, align, lines, boxLeft, boxWidth); } else if (box.WrapText && size.Width > boxWidth) { WrapStringToFit(SplitByWhitespace(str), boxWidth, gfx, font, out var lines); RenderWrappedLines(font, gfx, box, fy, align, lines, boxLeft, boxWidth); } else if (box.ShrinkToFit && (size.Width > boxWidth || size.Height > boxHeight)) { var altFont = ShrinkFontToFit(str, boxWidth, boxHeight, gfx, font, size); gfx.DrawString(str, altFont, XBrushes.Black, space, align); } else { gfx.DrawString(str, font, XBrushes.Black, space, align); } }
/// <summary> /// Returns true if this box requires special handling -- such as the page count to be known, or image embedding /// </summary> public static bool IsSpecialValue(TemplateBox box, out DocumentBoxType type) { type = DocumentBoxType.Normal; if (box.MappingPath == null || box.MappingPath.Length != 2) { return(false); } if (box.DisplayFormat?.Type == DisplayFormatType.RenderImage) { type = DocumentBoxType.EmbedJpegImage; return(true); } var filter = box.MappingPath[0]; if (filter != "P") { return(false); } if (!Enum.TryParse(box.MappingPath[1], out type)) { return(false); } return(true); }
public void Serialization() { try { XmlDiff diff = new XmlDiff(); TemplateBox templateBox = new TemplateBox(TemplatingResources.TemplatesDefault); String result = templateBox.Serialize(); //Utils.WriteToFile(@"C:\temp\templates.xml", result); diff.IgnoreChildOrder = true; diff.IgnoreComments = true; diff.IgnoreDtd = true; diff.IgnoreNamespaces = true; diff.IgnorePI = true; diff.IgnorePrefixes = true; diff.IgnoreWhitespace = true; diff.IgnoreXmlDecl = true; StringWriter diffgramString = new StringWriter(); XmlTextWriter diffgramXml = new XmlTextWriter(diffgramString); bool diffBool = diff.Compare(new XmlTextReader(new StringReader(result)), new XmlTextReader(new StringReader(TemplatingResources.TemplatesDefault)), diffgramXml); //MessageBox.Show(diffgramString.ToString()); Assert.True(diffBool, diffgramXml.ToString()); } catch (Exception e) { Assert.Fail("Exception: " + e.GetType().Name + "\n" + e.Message + "\n" + e.StackTrace); } }
public void page_info_types_should_trigger_a_special_type(string name) { var box = new TemplateBox { MappingPath = new [] { "P", name } }; var result = DataMapper.IsSpecialValue(box, out var type); Assert.That(result, Is.True, "did not trigger"); Assert.That(type.ToString(), Is.EqualTo(name), "wrong special type"); }
public void normal_paths_should_not_be_treated_as_special_types() { var box = new TemplateBox { MappingPath = new [] { "", "Anything" } }; var result = DataMapper.IsSpecialValue(box, out var type); Assert.That(result, Is.False, "was marked as special, but should not have been"); Assert.That(type, Is.EqualTo(DocumentBoxType.Normal), "wrong type"); }
private void AlignTextToRect(Graphics g, string name, Rectangle rect, TemplateBox box, out float top, out float left) { var size = g.MeasureString(name, Font); left = rect.Left; top = rect.Top; var halfWidth = rect.Width / 2.0f; var halfHeight = rect.Height / 2.0f; var textWidth = size.Width; var textHeight = size.Height; switch (box.Alignment) { case TextAlignment.TopLeft: break; case TextAlignment.TopCentre: left += halfWidth - textWidth / 2.0f; break; case TextAlignment.TopRight: left += rect.Width - textWidth; break; case TextAlignment.MidlineLeft: top += halfHeight - textHeight / 2.0f; break; case TextAlignment.MidlineCentre: left += halfWidth - textWidth / 2.0f; top += halfHeight - textHeight / 2.0f; break; case TextAlignment.MidlineRight: top += halfHeight - textHeight / 2.0f; left += rect.Width - textWidth; break; case TextAlignment.BottomLeft: top += rect.Height - textHeight; break; case TextAlignment.BottomCentre: top += rect.Height - textHeight; left += halfWidth - textWidth / 2.0f; break; case TextAlignment.BottomRight: top += rect.Height - textHeight; left += rect.Width - textWidth; break; } }
public void render_image_should_trigger_a_special_type() { var box = new TemplateBox { MappingPath = new [] { "", "Item" }, DisplayFormat = new DisplayFormatFilter { Type = DisplayFormatType.RenderImage } }; var result = DataMapper.IsSpecialValue(box, out var type); Assert.That(result, Is.True, "did not trigger"); Assert.That(type, Is.EqualTo(DocumentBoxType.EmbedJpegImage), "wrong special type"); }
/// <summary> /// Create a box that is a copy of another /// </summary> /// <param name="parent"></param> public TemplateBox(TemplateBox parent) { WrapText = parent.WrapText; ShrinkToFit = parent.ShrinkToFit; BoxFontSize = parent.BoxFontSize; Alignment = parent.Alignment; DependsOn = parent.DependsOn; DisplayFormat = parent.DisplayFormat; MappingPath = parent.MappingPath; BoxOrder = parent.BoxOrder; Width = parent.Width; Height = parent.Height; Top = parent.Top + (Height / 2); Left = parent.Left + (Width / 2); }
private static void RenderWrappedLines(XFont font, XGraphics gfx, TemplateBox box, double fy, XStringFormat align, List <MeasuredLine> lines, double boxLeft, double boxWidth) { if (lines.Count < 1) { return; } var top = CalculateTop(box, fy, lines); var start = lines[0] !.Text.Length < 1 ? 1 : 0; // Never draw a blank first line for (var index = start; index < lines.Count; index++) { var line = lines[index] !; var lineRect = new XRect(boxLeft, top, boxWidth, line.Height); gfx.DrawString(line.Text, font, XBrushes.Black, lineRect, align); top += line.Height; } }
/// <summary> /// Gather the data available for a single template box. /// </summary> private static Result <DocumentBox?> PrepareBox(DataMapper mapper, TemplateBox box, Dictionary <string, decimal> runningTotals, int pageIndex) { try { if (box.MappingPath is null) { return(Result.Failure <DocumentBox?>("Box has no mapping path")); } var str = mapper.TryFindBoxData(box, pageIndex, runningTotals); if (DataMapper.IsSpecialValue(box, out var type)) { return(Result.Success <DocumentBox?>(new DocumentBox(box) { BoxType = type, RenderContent = str })); } if (str == null) { if (box.IsRequired) { return(Result.Failure <DocumentBox?>($"Required data was not found at [{string.Join(".",box.MappingPath)}]")); } return(Result.Success <DocumentBox?>(null)); // empty data is considered OK } if (box.DisplayFormat != null) { str = DisplayFormatter.ApplyFormat(box.DisplayFormat, str); if (str == null) { return(Result.Failure <DocumentBox?>($"Formatter failed: {box.DisplayFormat.Type} applied on {string.Join(".", box.MappingPath!)}")); } } return(Result.Success <DocumentBox?>(new DocumentBox(box, str))); } catch (Exception ex) { return(Result.Failure <DocumentBox>(ex) !); } }
/// <summary> /// Get the data to display in a box for a specific page. /// </summary> /// <param name="box">Box to be rendered</param> /// <param name="pageIndex">Page definition index</param> /// <param name="runningTotals"></param> /// <returns>String to render, or null if not applicable</returns> public string?TryFindBoxData(TemplateBox box, int pageIndex, Dictionary <string, decimal> runningTotals) { try { if (box.MappingPath == null) { return(null); } var filters = JoinProjectAndPageFilters(pageIndex); var obj = MappingActions.ApplyFilter(MappingType.None, _emptyParams, box.MappingPath, _originalPath, filters, _data, _repeatData, runningTotals); return(obj?.ToString()); } catch { return(null); } }
public DocumentBox(TemplateBox src, string?dataString) { Definition = src; RenderContent = dataString; }
public DocumentBox(TemplateBox src) { Definition = src; RenderContent = null; }