Java에서 PostScript (PS) 문서의 클립 및 클리핑 경로 작업

Aspose.Page for Java를 사용하여 PostScript (PS/EPS) 문서에서 벡터 클리핑 경로를 정의, 적용 및 관리합니다. 임의의 닫힌 기하학적 경계 내에서 그리기, 텍스트 렌더링 및 이미지 표시 작업을 제한합니다. PostScript 그래픽 상태 스택(writeGraphicsSave/writeGraphicsRestore)을 사용하여 로컬 및 전역 클립 영역을 효율적으로 관리하고 복잡한 마스킹 시각 효과를 생성합니다.

 

Aspose.Page for Java는 Java의 java.awt.Shape 기본 요소를 활용하여 PsDocument 렌더링 상태 내의 현재 클리핑 경로를 수정합니다.

  • 현재 클리핑 경로와 제공된 기하학적 도형의 경계를 교차하는 클립을 적용하려면 PsDocument.writeGraphicsSave() 메서드를 사용합니다.
  • 활성 클립 경계를 포함한 현재 그래픽 상태를 상태 스택에 푸시하려면 PsDocument.writeGraphicsSave() 메서드를 사용합니다.
  • 그래픽 상태를 복원하여 스택의 최상위 그래픽 상태를 제거하고 클리핑 영역을 이전 상태로 되돌리려면 PsDocument.writeGraphicsRestore() 메서드를 사용합니다.
  • 표준 또는 복잡한 닫힌 벡터 경로를 클리핑 영역으로 사용하여 도형을 클리핑하려면 Rectangle2D, Ellipse2D, Path2D를 사용합니다.

간단한 도형 클립(사각형 또는 타원) 적용

텍스트 또는 벡터 그리기 작업이 기본 기하학적 도형 내부에 정확하게 유지되도록 제한합니다.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;

public class BasicClippingPS {
    public static void main(String[] args) throws Exception {
        // Initialize output stream and PS document
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/basic_clipping.ps");
        PsSaveOptions options = new PsSaveOptions();
        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage(null);

        // Define a clipping shape (Circle boundary)
        Ellipse2D clipShape = new Ellipse2D.Float(100, 100, 200, 200);

        // Save state before clipping
        document.writeGraphicsSave();

        // Apply clip shape
        document.clip(clipShape);

        // Draw a large rectangle that extends past the clip boundary
        document.setPaint(Color.BLUE);
        document.fill(new Rectangle2D.Float(50, 50, 300, 300)); // Only the circle area gets rendered

        // Restore state to release clip
        document.writeGraphicsRestore();

        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Basic clip applied successfully to PostScript document.");
    }
}

그래픽 상태를 사용하여 중첩된 클립 영역 관리

클리핑 작업을 writeGraphicsSave()writeGraphicsRestore() 블록으로 감싸 로컬 클립 경계를 격리하고 페이지 요소 간에 클립이 계속 적용되는 것을 방지합니다.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;

public class NestedClippingPS {
    public static void main(String[] args) throws Exception {
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/nested_clipping.ps");
        PsSaveOptions options = new PsSaveOptions();
        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage(null);

        // Outer graphic state
        document.writeGraphicsSave();
        
        // Intersect First Clip (Outer Box)
        document.clip(new Rectangle2D.Float(100, 100, 200, 200));

        // Inner graphic state
        document.writeGraphicsSave();
        
        // Intersect Second Clip (Inner Box - creates combined intersection)
        document.clip(new Rectangle2D.Float(150, 150, 200, 200));

        // Fill combined intersection region
        document.setPaint(Color.RED);
        document.fill(new Rectangle2D.Float(0, 0, 500, 500));

        // Restore back to single outer box clip
        document.writeGraphicsRestore();

        // Restore back to unclipped original page state
        document.writeGraphicsRestore();

        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Nested clip states processed successfully.");
    }
}

복잡한 텍스트 및 벡터 경로 클리핑

Path2D를 사용하여 구성한 사용자 지정 도형을 패턴이 있거나 여러 색상으로 구성된 배경을 위한 동적 마스크로 사용합니다.

import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;

public class ComplexPathClippingPS {
    public static void main(String[] args) throws Exception {
        FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/complex_clip.ps");
        PsSaveOptions options = new PsSaveOptions();
        PsDocument document = new PsDocument(outStream, options, 1);

        document.openPage(null);

        // Construct a custom triangle path for clipping
        Path2D triangleClip = new Path2D.Float();
        triangleClip.moveTo(250, 100);
        triangleClip.lineTo(400, 350);
        triangleClip.lineTo(100, 350);
        triangleClip.closePath();

        document.writeGraphicsSave();

        // Apply custom path clip
        document.clip(triangleClip);

        // Render overlapping colored stripes inside the triangle boundary
        Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };
        for (int i = 0; i < colors.length; i++) {
            document.setPaint(colors[i]);
            document.fill(new Rectangle2D.Float(100, 100 + (i * 60), 300, 50));
        }

        document.writeGraphicsRestore();

        document.closePage();
        document.save();
        outStream.close();

        System.out.println("Complex vector path clip applied successfully.");
    }
}

설치 및 설정

Aspose.Page for Java를 Maven에 추가합니다:

Package Manager Console Command

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-page</artifactId>
    <version>Latest</version>
</dependency>

FAQ

1. PsDocument.clip(Shape)은 현재 클립 영역을 어떻게 변경하나요?

clip(Shape)을 호출하면 지정된 도형과 PostScript 실행 컨텍스트에 있는 기존 클립 경로가 교차됩니다. 먼저 상태를 복원하지 않는 한 클립 경로 전체가 대체되지는 않습니다.

2. PostScript에서 클리핑 경로를 제거하거나 재설정하려면 어떻게 해야 하나요?

PostScript는 교차를 통해 클립을 누적하므로 클립 경로를 직접 ‘제거’할 수 없습니다. 대신 클립을 적용하기 전에 document.writeGraphicsSave()를 사용하여 상태를 저장하고, 완료되면 document.writeGraphicsRestore()를 사용하여 복원합니다.

3. 텍스트 윤곽선이나 곡선 경로와 같은 임의의 도형을 클리핑에 사용할 수 있나요?

예. Path2D, 곡선 Bézier 도형 및 변환된 텍스트 벡터 윤곽선을 포함한 모든 Java java.awt.Shape 구현을 document.clip()에 직접 전달할 수 있습니다.