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() メソッドを使用します。
  • 標準または複雑な閉じたベクターパスをクリッピング領域として使用して図形をクリップするには、Rectangle2DEllipse2DPath2D を使用します。

単純な図形クリップ(長方形または楕円)を適用する

テキストまたはベクター描画操作を基本的な幾何学図形の内側に厳密に収まるように制限します。

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() に直接渡すことができます。