|
| 1 | +using OfficeOpenXml; |
| 2 | +using OfficeOpenXml.Drawing; |
| 3 | +using OfficeOpenXml.Style; |
| 4 | +using System.Globalization; |
| 5 | +using EPPlusImageRenderer; |
| 6 | +using EPPlus.Export.ImageRenderer.RenderItems.SvgItem; |
| 7 | +using EPPlus.Graphics; |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace EPPlus.Export.ImageRenderer.Tests |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + public sealed class TextRenderTests : TestBase |
| 14 | + { |
| 15 | + private void SetFillColor(ExcelDrawingFillBasic fill, string color) |
| 16 | + { |
| 17 | + if (color.StartsWith("#")) |
| 18 | + { |
| 19 | + fill.Style = eFillStyle.SolidFill; |
| 20 | + var r = int.Parse(color.Substring(1, 2), NumberStyles.HexNumber); |
| 21 | + var g = int.Parse(color.Substring(3, 2), NumberStyles.HexNumber); |
| 22 | + var b = int.Parse(color.Substring(5, 2), NumberStyles.HexNumber); |
| 23 | + var c = System.Drawing.Color.FromArgb(0xFF, (byte)r, (byte)g, (byte)b); |
| 24 | + fill.SolidFill.Color.SetRgbColor(c); |
| 25 | + } |
| 26 | + else if (string.IsNullOrWhiteSpace(color) == false) |
| 27 | + { |
| 28 | + fill.Style = eFillStyle.SolidFill; |
| 29 | + try |
| 30 | + { |
| 31 | + var c = System.Drawing.Color.FromName(color); |
| 32 | + if (c.IsEmpty) |
| 33 | + { |
| 34 | + var sc = Enum.Parse<eSchemeColor>(color); |
| 35 | + fill.SolidFill.Color.SetSchemeColor(sc); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + fill.SolidFill.Color.SetPresetColor(c); |
| 40 | + } |
| 41 | + } |
| 42 | + catch |
| 43 | + { |
| 44 | + var sc = Enum.Parse<eSchemeColor>(color); |
| 45 | + fill.SolidFill.Color.SetSchemeColor(sc); |
| 46 | + } |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + fill.Style = eFillStyle.NoFill; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void VerifyTextRunBounds() |
| 57 | + { |
| 58 | + ExcelPackage.License.SetNonCommercialOrganization("EPPlus Project"); |
| 59 | + |
| 60 | + using (var p = new ExcelPackage()) |
| 61 | + { |
| 62 | + var ws = p.Workbook.Worksheets.Add("Sheet1"); |
| 63 | + var cube = ws.Drawings.AddShape("Cube1", OfficeOpenXml.Drawing.eShapeStyle.Cube); |
| 64 | + |
| 65 | + cube.Font.Color = System.Drawing.Color.Goldenrod; |
| 66 | + |
| 67 | + cube.TextBody.TopInsert = 0; |
| 68 | + cube.TextBody.BottomInsert = 0; |
| 69 | + cube.TextBody.RightInsert = 0; |
| 70 | + cube.TextBody.LeftInsert = 0; |
| 71 | + |
| 72 | + var para1 = cube.TextBody.Paragraphs.Add("TextBox\r\na"); |
| 73 | + |
| 74 | + para1.LeftMargin = 5; |
| 75 | + cube.TextBody.TopInsert = 10; |
| 76 | + |
| 77 | + var para2 = cube.TextBody.Paragraphs.Add("TextBox2"); |
| 78 | + para2.TextRuns[0].FontItalic = true; |
| 79 | + para2.TextRuns[0].FontBold = true; |
| 80 | + para2.TextRuns.Add("ra underline").FontUnderLine = eUnderLineType.Dash; |
| 81 | + para2.TextRuns.Add("La Strike").FontStrike = eStrikeType.Single; |
| 82 | + var tRun1 = para2.TextRuns.Add("Goudy size 16"); |
| 83 | + tRun1.SetFromFont("Goudy Stout", 16); |
| 84 | + tRun1.Fill.Color = System.Drawing.Color.IndianRed; |
| 85 | + var tRun2 = para2.TextRuns.Add("SvgSize 24"); |
| 86 | + tRun2.FontSize = 24; |
| 87 | + |
| 88 | + cube.TextAlignment = eTextAlignment.Left; |
| 89 | + cube.TextAnchoring = eTextAnchoringType.Top; |
| 90 | + |
| 91 | + cube.TextBody.HorizontalTextOverflow = eTextHorizontalOverflow.Clip; |
| 92 | + cube.TextBody.VerticalTextOverflow = eTextVerticalOverflow.Clip; |
| 93 | + |
| 94 | + SetFillColor(cube.Fill, "#156082"); |
| 95 | + SetFillColor(cube.Border.Fill, "#042433"); |
| 96 | + |
| 97 | + var aFont = cube.Font; |
| 98 | + var paragraph0 = cube.TextBody.Paragraphs[0]; |
| 99 | + |
| 100 | + var autofit = cube.TextBody.TextAutofit; |
| 101 | + |
| 102 | + cube.GetSizeInPixels(out int testWidth, out int testHeight); |
| 103 | + |
| 104 | + var parentBB = new BoundingBox(); |
| 105 | + parentBB.Width = testWidth; |
| 106 | + parentBB.Height = testHeight; |
| 107 | + |
| 108 | + SvgTextBodyItem tbItem = new SvgTextBodyItem(parentBB); |
| 109 | + |
| 110 | + tbItem.ImportTextBody(cube.TextBody); |
| 111 | + |
| 112 | + var txtRun1Bounds = tbItem.Paragraphs[0]._textRunItems[0].Bounds; |
| 113 | + |
| 114 | + Assert.AreEqual(43.835286458333336d, txtRun1Bounds.Width); |
| 115 | + |
| 116 | + var widthLine2 = tbItem.Paragraphs[0]._textRunItems[0].PerLineWidth[1]; |
| 117 | + var topYLine2 = tbItem.Paragraphs[0]._textRunItems[0].YIncreasePerLine[1]; |
| 118 | + Assert.AreEqual(7.147135416666667d, widthLine2); |
| 119 | + Assert.AreEqual(17.903645833333336d, topYLine2); |
| 120 | + |
| 121 | + var txtRuns2 = tbItem.Paragraphs[1]._textRunItems; |
| 122 | + |
| 123 | + Assert.AreEqual(53.20963541666667d, txtRuns2[0].Bounds.Width); |
| 124 | + var currentLineWidth = txtRuns2[0].Bounds.Width; |
| 125 | + |
| 126 | + Assert.AreEqual(currentLineWidth, txtRuns2[1].Bounds.Left); |
| 127 | + Assert.AreEqual(69.55924479166667d, txtRuns2[1].Bounds.Width); |
| 128 | + currentLineWidth += txtRuns2[1].Bounds.Width; |
| 129 | + |
| 130 | + Assert.AreEqual(currentLineWidth, txtRuns2[2].Bounds.Left); |
| 131 | + Assert.AreEqual(49.89388020833334, txtRuns2[2].Bounds.Width); |
| 132 | + currentLineWidth += txtRuns2[2].Bounds.Width; |
| 133 | + |
| 134 | + Assert.AreEqual(21.333333333333332, txtRuns2[3].Bounds.Height); |
| 135 | + Assert.AreEqual(currentLineWidth, txtRuns2[3].Bounds.Left); |
| 136 | + Assert.AreEqual(283.21875d, txtRuns2[3].Bounds.Width); |
| 137 | + currentLineWidth += txtRuns2[3].Bounds.Width; |
| 138 | + |
| 139 | + Assert.AreEqual(32, txtRuns2[4].Bounds.Height); |
| 140 | + Assert.AreEqual(currentLineWidth, txtRuns2[4].Bounds.Left); |
| 141 | + Assert.AreEqual(134.20312500000006d, txtRuns2[4].Bounds.Width); |
| 142 | + currentLineWidth += txtRuns2[4].Bounds.Width; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void ConceptTextRun() |
| 148 | + { |
| 149 | + string rt1 = "My richtext1\r\n of len"; |
| 150 | + string rt2 = "gth beyond and then I am richtext2"; |
| 151 | + |
| 152 | + string combined = rt1 + rt2; |
| 153 | + |
| 154 | + int charMax = 10; |
| 155 | + |
| 156 | + int lineCharCount = 0; |
| 157 | + |
| 158 | + List<string> lines = new List<string>(); |
| 159 | + |
| 160 | + for (int i = 0; i < combined.Length; i++) |
| 161 | + { |
| 162 | + if(lineCharCount > charMax) |
| 163 | + { |
| 164 | + var currLine = combined.Substring(i - lineCharCount, lineCharCount); |
| 165 | + lines.Add(currLine); |
| 166 | + lineCharCount = 0; |
| 167 | + } |
| 168 | + |
| 169 | + if (combined[i] == '\r') |
| 170 | + { |
| 171 | + var currLine = combined.Substring(i - lineCharCount, lineCharCount); |
| 172 | + lines.Add(currLine); |
| 173 | + lineCharCount = 0; |
| 174 | + i++; |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + lineCharCount++; |
| 179 | + } |
| 180 | + |
| 181 | + var finalLine = combined.Substring(combined.Length - lineCharCount, lineCharCount); |
| 182 | + lines.Add(finalLine); |
| 183 | + |
| 184 | + foreach (string line in lines) |
| 185 | + { |
| 186 | + Debug.WriteLine(line); |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments