Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@
*
* @see GC
* @since 3.133
*
* @noreference This class is provisional API and subject to change. It is being made available to gather early feedback. The API or behavior may change in future releases as the implementation evolves based on user feedback.
*/
public class PDFDocument implements Drawable {
Device device;
public final class PDFDocument extends Device {
long pdfContext;
NSGraphicsContext graphicsContext;
boolean isGCCreated = false;
boolean disposed = false;
boolean pageStarted = false;
String filename;

/**
* Width of the page in points (1/72 inch)
Expand All @@ -62,6 +63,16 @@ public class PDFDocument implements Drawable {
*/
double heightInPoints;

/**
* Internal data class to pass PDF document parameters through
* the Device constructor.
*/
static class PDFDocumentData extends DeviceData {
String filename;
double widthInPoints;
double heightInPoints;
}

/**
* Constructs a new PDFDocument with the specified filename and page dimensions.
* <p>
Expand All @@ -83,52 +94,38 @@ public class PDFDocument implements Drawable {
* @see #dispose()
*/
public PDFDocument(String filename, double widthInPoints, double heightInPoints) {
this(null, filename, widthInPoints, heightInPoints);
super(checkData(filename, widthInPoints, heightInPoints));
}

/**
* Constructs a new PDFDocument with the specified filename and page dimensions,
* associated with the given device.
* <p>
* You must dispose the PDFDocument when it is no longer required.
* </p>
*
* @param device the device to associate with this PDFDocument
* @param filename the path to the PDF file to create
* @param widthInPoints the width of each page in points (1/72 inch)
* @param heightInPoints the height of each page in points (1/72 inch)
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if filename is null</li>
* <li>ERROR_INVALID_ARGUMENT - if width or height is not positive</li>
* </ul>
* @exception SWTError <ul>
* <li>ERROR_NO_HANDLES - if the PDF context could not be created</li>
* </ul>
*
* @see #dispose()
* Validates and prepares the data for construction.
*/
public PDFDocument(Device device, String filename, double widthInPoints, double heightInPoints) {
static PDFDocumentData checkData(String filename, double widthInPoints, double heightInPoints) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (widthInPoints <= 0 || heightInPoints <= 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
PDFDocumentData data = new PDFDocumentData();
data.filename = filename;
data.widthInPoints = widthInPoints;
data.heightInPoints = heightInPoints;
return data;
}

/**
* Creates the PDF device in the operating system.
* This method is called before <code>init</code>.
*
* @param data the DeviceData which describes the receiver
*/
@Override
protected void create(DeviceData data) {
PDFDocumentData pdfData = (PDFDocumentData) data;
this.filename = pdfData.filename;
this.widthInPoints = pdfData.widthInPoints;
this.heightInPoints = pdfData.heightInPoints;

NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
this.widthInPoints = widthInPoints;
this.heightInPoints = heightInPoints;

// Get device from the current display if not provided
if (device == null) {
try {
this.device = org.eclipse.swt.widgets.Display.getDefault();
} catch (Exception e) {
this.device = null;
}
} else {
this.device = device;
}

// Create CFURL from the filename
NSString path = NSString.stringWith(filename);
NSURL fileURL = NSURL.fileURLWithPath(path);
Expand Down Expand Up @@ -184,11 +181,11 @@ private void ensurePageStarted() {
* </p>
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
public void newPage() {
if (disposed) SWT.error(SWT.ERROR_WIDGET_DISPOSED);
checkDevice();
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
Expand Down Expand Up @@ -216,11 +213,11 @@ public void newPage() {
* <li>ERROR_INVALID_ARGUMENT - if width or height is not positive</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
public void newPage(double widthInPoints, double heightInPoints) {
if (disposed) SWT.error(SWT.ERROR_WIDGET_DISPOSED);
checkDevice();
if (widthInPoints <= 0 || heightInPoints <= 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);

this.widthInPoints = widthInPoints;
Expand All @@ -234,11 +231,11 @@ public void newPage(double widthInPoints, double heightInPoints) {
* @return the width in points (1/72 inch)
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
public double getWidth() {
if (disposed) SWT.error(SWT.ERROR_WIDGET_DISPOSED);
checkDevice();
return widthInPoints;
}

Expand All @@ -248,14 +245,47 @@ public double getWidth() {
* @return the height in points (1/72 inch)
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
public double getHeight() {
if (disposed) SWT.error(SWT.ERROR_WIDGET_DISPOSED);
checkDevice();
return heightInPoints;
}

/**
* Returns the DPI (dots per inch) of the PDF document.
* PDF documents work in points where 1 point = 1/72 inch,
* so the DPI is always 72.
*
* @return a point whose x coordinate is the horizontal DPI and whose y coordinate is the vertical DPI
*
* @exception SWTException <ul>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
@Override
public Point getDPI() {
checkDevice();
return new Point(72, 72);
}

/**
* Returns a rectangle describing the receiver's size and location.
* The rectangle dimensions are in points (1/72 inch).
*
* @return the bounding rectangle
*
* @exception SWTException <ul>
* <li>ERROR_DEVICE_DISPOSED - if the receiver has been disposed</li>
* </ul>
*/
@Override
public Rectangle getBounds() {
checkDevice();
return new Rectangle(0, 0, (int) widthInPoints, (int) heightInPoints);
}

/**
* Invokes platform specific functionality to allocate a new GC handle.
* <p>
Expand All @@ -273,7 +303,7 @@ public double getHeight() {
*/
@Override
public long internal_new_GC(GCData data) {
if (disposed) SWT.error(SWT.ERROR_WIDGET_DISPOSED);
checkDevice();
if (isGCCreated) SWT.error(SWT.ERROR_INVALID_ARGUMENT);

NSAutoreleasePool pool = null;
Expand All @@ -290,18 +320,16 @@ public long internal_new_GC(GCData data) {
if ((data.style & mask) == 0) {
data.style |= SWT.LEFT_TO_RIGHT;
}
data.device = device;
data.device = this;
data.flippedContext = graphicsContext;
data.restoreContext = true;
NSSize size = new NSSize();
size.width = widthInPoints;
size.height = heightInPoints;
data.size = size;
if (device != null) {
data.background = device.getSystemColor(SWT.COLOR_WHITE).handle;
data.foreground = device.getSystemColor(SWT.COLOR_BLACK).handle;
data.font = device.getSystemFont();
}
data.background = getSystemColor(SWT.COLOR_WHITE).handle;
data.foreground = getSystemColor(SWT.COLOR_BLACK).handle;
data.font = getSystemFont();
}
isGCCreated = true;
return graphicsContext.id;
Expand Down Expand Up @@ -350,27 +378,12 @@ public boolean isAutoScalable() {
}

/**
* Returns <code>true</code> if the PDFDocument has been disposed,
* and <code>false</code> otherwise.
*
* @return <code>true</code> when the PDFDocument is disposed and <code>false</code> otherwise
* Destroys the PDF document handle.
* This method is called internally by the dispose
* mechanism of the <code>Device</code> class.
*/
public boolean isDisposed() {
return disposed;
}

/**
* Disposes of the operating system resources associated with
* the PDFDocument. Applications must dispose of all PDFDocuments
* that they allocate.
* <p>
* This method finalizes the PDF file and writes it to disk.
* </p>
*/
public void dispose() {
if (disposed) return;
disposed = true;

@Override
protected void destroy() {
NSAutoreleasePool pool = null;
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
try {
Expand Down
Loading
Loading