|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2021 iText Group NV |
| 4 | +Authors: iText Software. |
| 5 | +
|
| 6 | +This program is offered under a commercial and under the AGPL license. |
| 7 | +For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | +AGPL licensing: |
| 10 | +This program is free software: you can redistribute it and/or modify |
| 11 | +it under the terms of the GNU Affero General Public License as published by |
| 12 | +the Free Software Foundation, either version 3 of the License, or |
| 13 | +(at your option) any later version. |
| 14 | +
|
| 15 | +This program is distributed in the hope that it will be useful, |
| 16 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | +You should have received a copy of the GNU Affero General Public License |
| 21 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +*/ |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.IO; |
| 26 | +using System.Linq; |
| 27 | +using iText.IO.Util; |
| 28 | +using iText.Kernel.Colors; |
| 29 | +using iText.Kernel.Pdf; |
| 30 | +using iText.Kernel.Utils; |
| 31 | +using iText.Layout.Element; |
| 32 | +using iText.Layout.Properties; |
| 33 | +using iText.Test; |
| 34 | +using iText.Test.Attributes; |
| 35 | + |
| 36 | +namespace iText.Layout { |
| 37 | + [NUnit.Framework.TestFixtureSource("AlignItemsAndJustifyContentPropertiesTestFixtureData")] |
| 38 | + public class ListAlignmentDirectionTest : ExtendedITextTest { |
| 39 | + public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 40 | + .CurrentContext.TestDirectory) + "/resources/itext/layout/ListAlignmentDirectionTest/"; |
| 41 | + |
| 42 | + public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory |
| 43 | + + "/test/itext/layout/ListAlignmentDirectionTest/"; |
| 44 | + |
| 45 | + private const String PARAMETERS_NAME_PATTERN = "item-text-align: {0}; item-direction: {1}, " + "list-text-align: {2}; list-direction: {3}"; |
| 46 | + |
| 47 | + private const String RESULTANT_FILE_NAME_PATTERN = "item-text-align-{0}_item-dir-{1}_list-text-align-{2}_list-dir-{3}"; |
| 48 | + |
| 49 | + private const String HTML_PATTERN = "<ul style=\"background-color: green; width: 300pt; margin-left: 150pt; text-align: {2}; direction: {3}\">" |
| 50 | + + " <li style=\"background-color: blue;\">Usual line</li>" + " <li style=\"background-color: yellow; text-align: {0}; direction: {1}\">Specific line</li>" |
| 51 | + + "</ul>"; |
| 52 | + |
| 53 | + private TextAlignment? itemTextAlignment; |
| 54 | + |
| 55 | + private BaseDirection? itemBaseDirection; |
| 56 | + |
| 57 | + private TextAlignment? listTextAlignment; |
| 58 | + |
| 59 | + private BaseDirection? listBaseDirection; |
| 60 | + |
| 61 | + public ListAlignmentDirectionTest(Object itemTextAlignment, Object itemBaseDirection, Object listTextAlignment |
| 62 | + , Object listBaseDirection) { |
| 63 | + this.itemTextAlignment = (TextAlignment?)itemTextAlignment; |
| 64 | + this.itemBaseDirection = (BaseDirection?)itemBaseDirection; |
| 65 | + this.listTextAlignment = (TextAlignment?)listTextAlignment; |
| 66 | + this.listBaseDirection = (BaseDirection?)listBaseDirection; |
| 67 | + CreateOrClearDestinationFolder(DESTINATION_FOLDER); |
| 68 | + // Create an HTML for this test |
| 69 | + CreateHtml(); |
| 70 | + } |
| 71 | + |
| 72 | + public ListAlignmentDirectionTest(Object[] array) |
| 73 | + : this(array[0], array[1], array[2], array[3]) { |
| 74 | + } |
| 75 | + |
| 76 | + public static IEnumerable<Object[]> AlignItemsAndJustifyContentProperties() { |
| 77 | + TextAlignment?[] alignmentTestValues = new TextAlignment?[] { TextAlignment.LEFT, TextAlignment.CENTER, TextAlignment |
| 78 | + .RIGHT, TextAlignment.JUSTIFIED, TextAlignment.JUSTIFIED_ALL }; |
| 79 | + BaseDirection?[] directionTestValues = new BaseDirection?[] { BaseDirection.LEFT_TO_RIGHT, BaseDirection.RIGHT_TO_LEFT |
| 80 | + }; |
| 81 | + IList<Object[]> objectList = new List<Object[]>(); |
| 82 | + foreach (TextAlignment? itemTA in alignmentTestValues) { |
| 83 | + foreach (BaseDirection? itemBA in directionTestValues) { |
| 84 | + foreach (TextAlignment? listTA in alignmentTestValues) { |
| 85 | + foreach (BaseDirection? listBA in directionTestValues) { |
| 86 | + objectList.Add(new Object[] { itemTA, itemBA, listTA, listBA }); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return objectList; |
| 92 | + } |
| 93 | + |
| 94 | + public static ICollection<NUnit.Framework.TestFixtureData> AlignItemsAndJustifyContentPropertiesTestFixtureData |
| 95 | + () { |
| 96 | + return AlignItemsAndJustifyContentProperties().Select(array => new NUnit.Framework.TestFixtureData(array)).ToList(); |
| 97 | + } |
| 98 | + |
| 99 | + [NUnit.Framework.Test] |
| 100 | + [LogMessage(iText.IO.LogMessageConstant.TYPOGRAPHY_NOT_FOUND, Count = 8)] |
| 101 | + public virtual void AlignmentDirectionTest() { |
| 102 | + // TODO DEVSIX-5727 direction of the first list-item should define the symbol indent's side. Once the issue |
| 103 | + // is fixed, the corresponding cmps should be updated. |
| 104 | + String fileName = MessageFormatUtil.Format(RESULTANT_FILE_NAME_PATTERN, FormatTextAlignment(itemTextAlignment |
| 105 | + ), FormatBaseDirection(itemBaseDirection), FormatTextAlignment(listTextAlignment), FormatBaseDirection |
| 106 | + (listBaseDirection)); |
| 107 | + String outFileName = DESTINATION_FOLDER + fileName + ".pdf"; |
| 108 | + String cmpFileName = SOURCE_FOLDER + "cmp_" + fileName + ".pdf"; |
| 109 | + PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName)); |
| 110 | + Document document = new Document(pdf); |
| 111 | + Style style = new Style().SetTextAlignment(itemTextAlignment).SetBaseDirection(itemBaseDirection); |
| 112 | + List list = CreateTestList(style); |
| 113 | + list.SetTextAlignment(listTextAlignment); |
| 114 | + list.SetBaseDirection(listBaseDirection); |
| 115 | + document.Add(list); |
| 116 | + document.Close(); |
| 117 | + System.Console.Out.WriteLine("HTML: " + UrlUtil.GetNormalizedFileUriString(DESTINATION_FOLDER + fileName + |
| 118 | + ".html") + "\n"); |
| 119 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER |
| 120 | + , "diff_")); |
| 121 | + } |
| 122 | + |
| 123 | + private static List CreateTestList(Style secondItemStyle) { |
| 124 | + List list = new List(); |
| 125 | + list.SetSymbolIndent(20); |
| 126 | + list.SetListSymbol("\u2022"); |
| 127 | + list.SetBackgroundColor(ColorConstants.GREEN); |
| 128 | + list.SetWidth(300); |
| 129 | + list.SetMarginLeft(150); |
| 130 | + ListItem listItem1 = new ListItem(); |
| 131 | + listItem1.Add(new Paragraph("Usual item")); |
| 132 | + listItem1.SetBackgroundColor(ColorConstants.BLUE); |
| 133 | + list.Add(listItem1); |
| 134 | + ListItem listItem2 = new ListItem(); |
| 135 | + listItem2.AddStyle(secondItemStyle); |
| 136 | + listItem2.Add(new Paragraph("Specific item")); |
| 137 | + listItem2.SetBackgroundColor(ColorConstants.YELLOW); |
| 138 | + list.Add(listItem2); |
| 139 | + return list; |
| 140 | + } |
| 141 | + |
| 142 | + private void CreateHtml() { |
| 143 | + String fileName = MessageFormatUtil.Format(RESULTANT_FILE_NAME_PATTERN, FormatTextAlignment(itemTextAlignment |
| 144 | + ), FormatBaseDirection(itemBaseDirection), FormatTextAlignment(listTextAlignment), FormatBaseDirection |
| 145 | + (listBaseDirection)); |
| 146 | + String htmlString = MessageFormatUtil.Format(HTML_PATTERN, FormatTextAlignment(itemTextAlignment, true), FormatBaseDirection |
| 147 | + (itemBaseDirection), FormatTextAlignment(listTextAlignment, true), FormatBaseDirection(listBaseDirection |
| 148 | + )); |
| 149 | + using (FileStream htmlFile = new FileStream(DESTINATION_FOLDER + fileName + ".html", FileMode.Create)) { |
| 150 | + byte[] htmlBytes = htmlString.GetBytes(System.Text.Encoding.UTF8); |
| 151 | + htmlFile.Write(htmlBytes, 0, htmlBytes.Length); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static String FormatTextAlignment(TextAlignment? alignment) { |
| 156 | + return FormatTextAlignment(alignment, false); |
| 157 | + } |
| 158 | + |
| 159 | + private static String FormatTextAlignment(TextAlignment? alignment, bool isHtml) { |
| 160 | + switch (alignment) { |
| 161 | + case TextAlignment.LEFT: { |
| 162 | + return "left"; |
| 163 | + } |
| 164 | + |
| 165 | + case TextAlignment.RIGHT: { |
| 166 | + return "right"; |
| 167 | + } |
| 168 | + |
| 169 | + case TextAlignment.CENTER: { |
| 170 | + return "center"; |
| 171 | + } |
| 172 | + |
| 173 | + case TextAlignment.JUSTIFIED: { |
| 174 | + return "justify"; |
| 175 | + } |
| 176 | + |
| 177 | + case TextAlignment.JUSTIFIED_ALL: { |
| 178 | + return isHtml ? "justify" : "justify-all"; |
| 179 | + } |
| 180 | + |
| 181 | + default: { |
| 182 | + NUnit.Framework.Assert.Fail("Unexpected text alignment"); |
| 183 | + return null; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private static String FormatBaseDirection(BaseDirection? direction) { |
| 189 | + switch (direction) { |
| 190 | + case BaseDirection.LEFT_TO_RIGHT: { |
| 191 | + return "ltr"; |
| 192 | + } |
| 193 | + |
| 194 | + case BaseDirection.RIGHT_TO_LEFT: { |
| 195 | + return "rtl"; |
| 196 | + } |
| 197 | + |
| 198 | + default: { |
| 199 | + NUnit.Framework.Assert.Fail("Unexpected base direction"); |
| 200 | + return null; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments