|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2019 iText Group NV |
| 4 | + Authors: iText Software. |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Affero General Public License version 3 |
| 8 | + as published by the Free Software Foundation with the addition of the |
| 9 | + following permission added to Section 15 as permitted in Section 7(a): |
| 10 | + FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | + ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | + OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, but |
| 15 | + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | + or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | + See the GNU Affero General Public License for more details. |
| 18 | + You should have received a copy of the GNU Affero General Public License |
| 19 | + along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | + Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | + http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | + The interactive user interfaces in modified source and object code versions |
| 25 | + of this program must display Appropriate Legal Notices, as required under |
| 26 | + Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | + In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | + a covered work must retain the producer line in every PDF that is created |
| 30 | + or manipulated using iText. |
| 31 | +
|
| 32 | + You can be released from the requirements of the license by purchasing |
| 33 | + a commercial license. Buying such a license is mandatory as soon as you |
| 34 | + develop commercial activities involving the iText software without |
| 35 | + disclosing the source code of your own applications. |
| 36 | + These activities include: offering paid services to customers as an ASP, |
| 37 | + serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | + source product. |
| 39 | +
|
| 40 | + For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | + */ |
1 | 43 | package com.itextpdf.kernel.pdf.canvas;
|
2 | 44 |
|
3 | 45 | import com.itextpdf.io.source.ByteArrayOutputStream;
|
|
6 | 48 | import com.itextpdf.kernel.colors.Color;
|
7 | 49 | import com.itextpdf.kernel.colors.ColorConstants;
|
8 | 50 | import com.itextpdf.kernel.colors.DeviceCmyk;
|
| 51 | +import com.itextpdf.kernel.colors.DeviceGray; |
9 | 52 | import com.itextpdf.kernel.colors.DeviceN;
|
| 53 | +import com.itextpdf.kernel.colors.DeviceRgb; |
10 | 54 | import com.itextpdf.kernel.colors.IccBased;
|
11 | 55 | import com.itextpdf.kernel.colors.Indexed;
|
12 | 56 | import com.itextpdf.kernel.colors.Lab;
|
| 57 | +import com.itextpdf.kernel.colors.PatternColor; |
13 | 58 | import com.itextpdf.kernel.colors.Separation;
|
14 | 59 | import com.itextpdf.kernel.font.PdfFontFactory;
|
15 | 60 | import com.itextpdf.kernel.pdf.CompressionConstants;
|
|
36 | 81 | import java.util.ArrayList;
|
37 | 82 | import org.junit.Assert;
|
38 | 83 | import org.junit.BeforeClass;
|
| 84 | +import org.junit.Rule; |
39 | 85 | import org.junit.Test;
|
40 | 86 | import org.junit.experimental.categories.Category;
|
| 87 | +import org.junit.rules.ExpectedException; |
41 | 88 |
|
42 | 89 | @Category(IntegrationTest.class)
|
43 | 90 | public class PdfCanvasColorTest extends ExtendedITextTest {
|
44 | 91 | public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/canvas/PdfCanvasColorTest/";
|
45 | 92 | public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/canvas/PdfCanvasColorTest/";
|
46 | 93 |
|
| 94 | + @Rule |
| 95 | + public ExpectedException junitExpectedException = ExpectedException.none(); |
| 96 | + |
47 | 97 | @BeforeClass
|
48 | 98 | public static void beforeClass() {
|
49 | 99 | createOrClearDestinationFolder(destinationFolder);
|
@@ -325,4 +375,244 @@ private void setColorSameColorSpacesTest(String pdfName, boolean pattern) throws
|
325 | 375 |
|
326 | 376 | Assert.assertNull(new CompareTool().compareByContent(destFile, cmpFile, destinationFolder, "diff_"));
|
327 | 377 | }
|
| 378 | + |
| 379 | + @Test |
| 380 | + public void patternColorColoredAxialPatternTest() throws Exception { |
| 381 | + String name = "patternColorColoredAxialPatternTest.pdf"; |
| 382 | + PdfWriter writer = new PdfWriter(destinationFolder + name); |
| 383 | + writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION); |
| 384 | + PdfDocument document = new PdfDocument(writer); |
| 385 | + PdfPage page = document.addNewPage(); |
| 386 | + |
| 387 | + PdfCanvas canvas = new PdfCanvas(page); |
| 388 | + |
| 389 | + PdfShading axial = new PdfShading.Axial( |
| 390 | + new PdfDeviceCs.Rgb(), |
| 391 | + 36, 716, new float[]{1, .784f, 0}, |
| 392 | + 396, 788, new float[]{0, 0, 1}, |
| 393 | + new boolean[] {true, true} |
| 394 | + ); |
| 395 | + |
| 396 | + canvas.setFillColor(new PatternColor(new PdfPattern.Shading(axial))); |
| 397 | + canvas.rectangle(30, 300, 400, 400).fill(); |
| 398 | + |
| 399 | + canvas.release(); |
| 400 | + document.close(); |
| 401 | + |
| 402 | + Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); |
| 403 | + } |
| 404 | + |
| 405 | + @Test |
| 406 | + public void patternColorColoredRadialPatternTest() throws Exception { |
| 407 | + String name = "patternColorColoredRadialPatternTest.pdf"; |
| 408 | + PdfWriter writer = new PdfWriter(destinationFolder + name); |
| 409 | + writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION); |
| 410 | + PdfDocument document = new PdfDocument(writer); |
| 411 | + PdfPage page = document.addNewPage(); |
| 412 | + |
| 413 | + PdfCanvas canvas = new PdfCanvas(page); |
| 414 | + |
| 415 | + PdfShading radial = new PdfShading.Radial( |
| 416 | + new PdfDeviceCs.Rgb(), |
| 417 | + 200, 700, 50, new float[] {1, 0.968f, 0.58f}, |
| 418 | + 300, 700, 100, new float[] {0.968f, 0.541f, 0.42f} |
| 419 | + ); |
| 420 | + |
| 421 | + canvas.setFillColor(new PatternColor(new PdfPattern.Shading(radial))); |
| 422 | + canvas.rectangle(30, 300, 400, 400).fill(); |
| 423 | + |
| 424 | + canvas.release(); |
| 425 | + document.close(); |
| 426 | + |
| 427 | + Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); |
| 428 | + } |
| 429 | + |
| 430 | + @Test |
| 431 | + public void patternColorUncoloredCircleRgbTest() throws Exception { |
| 432 | + String name = "patternColorUncoloredCircleRgbTest.pdf"; |
| 433 | + PdfWriter writer = new PdfWriter(destinationFolder + name); |
| 434 | + writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION); |
| 435 | + PdfDocument document = new PdfDocument(writer); |
| 436 | + PdfPage page = document.addNewPage(); |
| 437 | + |
| 438 | + PdfCanvas canvas = new PdfCanvas(page); |
| 439 | + |
| 440 | + PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false); |
| 441 | + new PdfPatternCanvas(circle, document).circle(7.5f, 7.5f, 2.5f).fill().release(); |
| 442 | + |
| 443 | + PdfSpecialCs.UncoloredTilingPattern uncoloredRgbCs |
| 444 | + = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Rgb()); |
| 445 | + |
| 446 | + float[] green = {0, 1, 0}; |
| 447 | + |
| 448 | + canvas.setFillColor(new PatternColor(circle, uncoloredRgbCs, green)); |
| 449 | + canvas.rectangle(30, 300, 400, 400).fill(); |
| 450 | + |
| 451 | + canvas.release(); |
| 452 | + document.close(); |
| 453 | + |
| 454 | + Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); |
| 455 | + } |
| 456 | + |
| 457 | + @Test |
| 458 | + public void patternColorUncoloredLineGrayTest() throws Exception { |
| 459 | + String name = "patternColorUncoloredLineGrayTest.pdf"; |
| 460 | + PdfWriter writer = new PdfWriter(destinationFolder + name); |
| 461 | + writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION); |
| 462 | + PdfDocument document = new PdfDocument(writer); |
| 463 | + PdfPage page = document.addNewPage(); |
| 464 | + |
| 465 | + PdfCanvas canvas = new PdfCanvas(page); |
| 466 | + |
| 467 | + PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false); |
| 468 | + new PdfPatternCanvas(line, document).setLineWidth(1).moveTo(3, -1).lineTo(3, 11).stroke().release(); |
| 469 | + |
| 470 | + canvas.setFillColor(new PatternColor(line, new DeviceGray())); |
| 471 | + canvas.rectangle(30, 300, 400, 400).fill(); |
| 472 | + |
| 473 | + canvas.release(); |
| 474 | + document.close(); |
| 475 | + |
| 476 | + Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); |
| 477 | + } |
| 478 | + |
| 479 | + @Test |
| 480 | + public void patternColorColoredSetTwiceTest() throws Exception { |
| 481 | + String name = "patternColorColoredSetTwiceTest.pdf"; |
| 482 | + PdfWriter writer = new PdfWriter(destinationFolder + name); |
| 483 | + writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION); |
| 484 | + PdfDocument document = new PdfDocument(writer); |
| 485 | + PdfPage page = document.addNewPage(); |
| 486 | + |
| 487 | + PdfCanvas canvas = new PdfCanvas(page); |
| 488 | + |
| 489 | + |
| 490 | + PdfPattern.Tiling square = new PdfPattern.Tiling(15, 15); |
| 491 | + new PdfPatternCanvas(square, document).setFillColor(new DeviceRgb(0xFF, 0xFF, 0x00)) |
| 492 | + .setStrokeColor(new DeviceRgb(0xFF, 0x00, 0x00)) |
| 493 | + .rectangle(5, 5, 5, 5) |
| 494 | + .fillStroke() |
| 495 | + .release(); |
| 496 | + |
| 497 | + PdfPattern.Tiling ellipse = new PdfPattern.Tiling(15, 10, 20, 25); |
| 498 | + new PdfPatternCanvas(ellipse, document) |
| 499 | + .setFillColor(new DeviceRgb(0xFF, 0xFF, 0x00)) |
| 500 | + .setStrokeColor(new DeviceRgb(0xFF, 0x00, 0x00)) |
| 501 | + .ellipse(2, 2, 13, 8) |
| 502 | + .fillStroke() |
| 503 | + .release(); |
| 504 | + |
| 505 | + |
| 506 | + canvas.setFillColor(new PatternColor(square)); |
| 507 | + canvas.rectangle(36, 696, 126, 126).fill(); |
| 508 | + canvas.setFillColor(new PatternColor(square)); |
| 509 | + canvas.rectangle(180, 696, 126, 126).fill(); |
| 510 | + canvas.setFillColor(new PatternColor(ellipse)); |
| 511 | + canvas.rectangle(360, 696, 126, 126).fill(); |
| 512 | + |
| 513 | + byte[] pageContentStreamBytes = canvas.getContentStream().getBytes(); |
| 514 | + |
| 515 | + canvas.release(); |
| 516 | + document.close(); |
| 517 | + |
| 518 | + String contentStreamString = new String(pageContentStreamBytes, StandardCharsets.US_ASCII); |
| 519 | + int p1Count = countSubstringOccurrences(contentStreamString, "/P1 scn"); |
| 520 | + int p2Count = countSubstringOccurrences(contentStreamString, "/P2 scn"); |
| 521 | + Assert.assertEquals(1, p1Count); |
| 522 | + Assert.assertEquals(1, p2Count); |
| 523 | + Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); |
| 524 | + } |
| 525 | + |
| 526 | + @Test |
| 527 | + public void patternColorUncoloredSetTwiceTest() throws Exception { |
| 528 | + String name = "patternColorUncoloredSetTwiceTest.pdf"; |
| 529 | + PdfWriter writer = new PdfWriter(destinationFolder + name); |
| 530 | + writer.setCompressionLevel(CompressionConstants.NO_COMPRESSION); |
| 531 | + PdfDocument document = new PdfDocument(writer); |
| 532 | + PdfPage page = document.addNewPage(); |
| 533 | + |
| 534 | + PdfCanvas canvas = new PdfCanvas(page); |
| 535 | + |
| 536 | + PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false); |
| 537 | + new PdfPatternCanvas(circle, document).circle(7.5f, 7.5f, 2.5f).fill().release(); |
| 538 | + |
| 539 | + PdfPattern.Tiling line = new PdfPattern.Tiling(5, 10, false); |
| 540 | + new PdfPatternCanvas(line, document).setLineWidth(1).moveTo(3, -1).lineTo(3, 11).stroke().release(); |
| 541 | + |
| 542 | + PatternColor patternColorCircle = new PatternColor(circle, ColorConstants.RED); |
| 543 | + |
| 544 | + float[] cyan = {1, 0, 0, 0}; |
| 545 | + float[] magenta = {0, 1, 0, 0}; |
| 546 | + PdfSpecialCs.UncoloredTilingPattern uncoloredTilingCmykCs = new PdfSpecialCs.UncoloredTilingPattern(new PdfDeviceCs.Cmyk()); |
| 547 | + PatternColor patternColorLine = new PatternColor(line, uncoloredTilingCmykCs, magenta); |
| 548 | + |
| 549 | + canvas.setFillColor(patternColorCircle); |
| 550 | + canvas.rectangle(36, 696, 126, 126).fill(); |
| 551 | + canvas.setFillColor(patternColorCircle); |
| 552 | + canvas.rectangle(180, 696, 126, 126).fill(); |
| 553 | + |
| 554 | + canvas.setFillColor(patternColorLine); |
| 555 | + canvas.rectangle(36, 576, 126, 126).fill(); |
| 556 | + |
| 557 | + patternColorLine.setColorValue(cyan); |
| 558 | + canvas.setFillColor(patternColorLine); |
| 559 | + canvas.rectangle(180, 576, 126, 126).fill(); |
| 560 | + |
| 561 | + // this case will be removed when deprecated method is removed |
| 562 | + patternColorLine.setPattern(circle); |
| 563 | + canvas.setFillColor(patternColorLine); |
| 564 | + canvas.rectangle(360, 696, 126, 126).fill(); |
| 565 | + |
| 566 | + byte[] pageContentStreamBytes = canvas.getContentStream().getBytes(); |
| 567 | + |
| 568 | + canvas.release(); |
| 569 | + document.close(); |
| 570 | + |
| 571 | + String contentStreamString = new String(pageContentStreamBytes, StandardCharsets.US_ASCII); |
| 572 | + int p1Count = countSubstringOccurrences(contentStreamString, "/P1 scn"); |
| 573 | + int p2Count = countSubstringOccurrences(contentStreamString, "/P2 scn"); |
| 574 | + Assert.assertEquals(3, p1Count); |
| 575 | + Assert.assertEquals(2, p2Count); |
| 576 | + Assert.assertNull(new CompareTool().compareByContent(destinationFolder + name, sourceFolder + "cmp_" + name, destinationFolder)); |
| 577 | + } |
| 578 | + |
| 579 | + @Test |
| 580 | + public void patternColorUncoloredPatternCsUnitTest() { |
| 581 | + junitExpectedException.expect(IllegalArgumentException.class); |
| 582 | + |
| 583 | + PdfDocument doc = new PdfDocument(new PdfWriter(new java.io.ByteArrayOutputStream())); |
| 584 | + |
| 585 | + PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false); |
| 586 | + new PdfPatternCanvas(circle, doc).circle(7.5f, 7.5f, 2.5f).fill().release(); |
| 587 | + |
| 588 | + new PatternColor(circle, new PdfSpecialCs.Pattern(), new float[0]); |
| 589 | + } |
| 590 | + |
| 591 | + @Test |
| 592 | + public void patternColorUncoloredPatternColorUnitTest() { |
| 593 | + junitExpectedException.expect(IllegalArgumentException.class); |
| 594 | + |
| 595 | + PdfDocument doc = new PdfDocument(new PdfWriter(new java.io.ByteArrayOutputStream())); |
| 596 | + |
| 597 | + PdfPattern.Tiling circle = new PdfPattern.Tiling(15, 15, 10, 20, false); |
| 598 | + new PdfPatternCanvas(circle, doc).circle(7.5f, 7.5f, 2.5f).fill().release(); |
| 599 | + |
| 600 | + PatternColor redCirclePattern = new PatternColor(circle, ColorConstants.RED); |
| 601 | + new PatternColor(circle, redCirclePattern); |
| 602 | + } |
| 603 | + |
| 604 | + private static int countSubstringOccurrences(String str, String findStr) { |
| 605 | + int lastIndex = 0; |
| 606 | + int count = 0; |
| 607 | + |
| 608 | + while(lastIndex != -1){ |
| 609 | + lastIndex = str.indexOf(findStr, lastIndex); |
| 610 | + |
| 611 | + if(lastIndex != -1){ |
| 612 | + ++count; |
| 613 | + lastIndex += findStr.length(); |
| 614 | + } |
| 615 | + } |
| 616 | + return count; |
| 617 | + } |
328 | 618 | }
|
0 commit comments