Skip to content

Commit 26784bd

Browse files
Copilotlaeubi
authored andcommitted
Add native PDF output support
Currently if one wants to create a PDF file it requires external libraries and as SWT does not allows an abstraction like Grahics2D in AWT one can not export real content of SWT components (e.g. Canvas) except exporting as an raster image or using some hacks. This now introduce a new PDFDocument to enable direct PDF generation from SWT widgets via Control.print(GC). This allows applications to export widget content to PDF files using the standard GC drawing API as well as even creating completely customized documents.
1 parent f005865 commit 26784bd

File tree

11 files changed

+1463
-10
lines changed

11 files changed

+1463
-10
lines changed

bundles/org.eclipse.swt/Eclipse SWT PI/cairo/library/cairo.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* IBM
1717
* - Binding to permit interfacing between Cairo and SWT
18-
* - Copyright (C) 2005, 2022 IBM Corp. All Rights Reserved.
18+
* - Copyright (C) 2005, 2025 IBM Corp. All Rights Reserved.
1919
*
2020
* ***** END LICENSE BLOCK ***** */
2121

@@ -704,6 +704,30 @@ JNIEXPORT void JNICALL Cairo_NATIVE(cairo_1pattern_1set_1matrix)
704704
}
705705
#endif
706706

707+
#ifndef NO_cairo_1pdf_1surface_1create
708+
JNIEXPORT jlong JNICALL Cairo_NATIVE(cairo_1pdf_1surface_1create)
709+
(JNIEnv *env, jclass that, jbyteArray arg0, jdouble arg1, jdouble arg2)
710+
{
711+
jbyte *lparg0=NULL;
712+
jlong rc = 0;
713+
Cairo_NATIVE_ENTER(env, that, cairo_1pdf_1surface_1create_FUNC);
714+
if (arg0) if ((lparg0 = (*env)->GetByteArrayElements(env, arg0, NULL)) == NULL) goto fail;
715+
/*
716+
rc = (jlong)cairo_pdf_surface_create((const char *)lparg0, arg1, arg2);
717+
*/
718+
{
719+
Cairo_LOAD_FUNCTION(fp, cairo_pdf_surface_create)
720+
if (fp) {
721+
rc = (jlong)((jlong (CALLING_CONVENTION*)(const char *, jdouble, jdouble))fp)((const char *)lparg0, arg1, arg2);
722+
}
723+
}
724+
fail:
725+
if (arg0 && lparg0) (*env)->ReleaseByteArrayElements(env, arg0, lparg0, 0);
726+
Cairo_NATIVE_EXIT(env, that, cairo_1pdf_1surface_1create_FUNC);
727+
return rc;
728+
}
729+
#endif
730+
707731
#ifndef NO_cairo_1pdf_1surface_1set_1size
708732
JNIEXPORT void JNICALL Cairo_NATIVE(cairo_1pdf_1surface_1set_1size)
709733
(JNIEnv *env, jclass that, jlong arg0, jdouble arg1, jdouble arg2)

bundles/org.eclipse.swt/Eclipse SWT PI/cairo/library/cairo_custom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define cairo_ps_surface_set_size_LIB LIB_CAIRO
3131
#define cairo_surface_set_device_scale_LIB LIB_CAIRO
3232
#define cairo_surface_get_device_scale_LIB LIB_CAIRO
33+
#define cairo_pdf_surface_create_LIB LIB_CAIRO
3334

3435
#ifdef CAIRO_HAS_XLIB_SURFACE
3536
#define cairo_xlib_surface_get_height_LIB LIB_CAIRO

bundles/org.eclipse.swt/Eclipse SWT PI/cairo/library/cairo_stats.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* IBM
1717
* - Binding to permit interfacing between Cairo and SWT
18-
* - Copyright (C) 2005, 2023 IBM Corp. All Rights Reserved.
18+
* - Copyright (C) 2005, 2025 IBM Corp. All Rights Reserved.
1919
*
2020
* ***** END LICENSE BLOCK ***** */
2121

@@ -86,6 +86,7 @@ typedef enum {
8686
cairo_1pattern_1set_1extend_FUNC,
8787
cairo_1pattern_1set_1filter_FUNC,
8888
cairo_1pattern_1set_1matrix_FUNC,
89+
cairo_1pdf_1surface_1create_FUNC,
8990
cairo_1pdf_1surface_1set_1size_FUNC,
9091
cairo_1pop_1group_1to_1source_FUNC,
9192
cairo_1ps_1surface_1set_1size_FUNC,

bundles/org.eclipse.swt/Eclipse SWT PI/cairo/org/eclipse/swt/internal/cairo/Cairo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,13 @@ public class Cairo extends Platform {
418418
*/
419419
public static final native void memmove(double[] dest, long src, long size);
420420

421+
/** Surface type constant for SVG */
422+
public static final int CAIRO_SURFACE_TYPE_SVG = 4;
423+
424+
/**
425+
* @method flags=dynamic
426+
* @param filename cast=(const char *)
427+
*/
428+
public static final native long cairo_pdf_surface_create(byte[] filename, double width_in_points, double height_in_points);
429+
421430
}

bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/library/os.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
66
* which accompanies this distribution, and is available at
77
* https://www.eclipse.org/legal/epl-2.0/
88
*
99
* SPDX-License-Identifier: EPL-2.0
10-
*
11-
* Contributors:
12-
* IBM Corporation - initial API and implementation
1310
*******************************************************************************/
1411

1512
/* Note: This file was auto-generated by org.eclipse.swt.tools.internal.JNIGenerator */
@@ -1596,6 +1593,52 @@ JNIEXPORT void JNICALL OS_NATIVE(CGImageRelease)
15961593
}
15971594
#endif
15981595

1596+
#ifndef NO_CGPDFContextBeginPage
1597+
JNIEXPORT void JNICALL OS_NATIVE(CGPDFContextBeginPage)
1598+
(JNIEnv *env, jclass that, jlong arg0, jlong arg1)
1599+
{
1600+
OS_NATIVE_ENTER(env, that, CGPDFContextBeginPage_FUNC);
1601+
CGPDFContextBeginPage((CGContextRef)arg0, (CFDictionaryRef)arg1);
1602+
OS_NATIVE_EXIT(env, that, CGPDFContextBeginPage_FUNC);
1603+
}
1604+
#endif
1605+
1606+
#ifndef NO_CGPDFContextClose
1607+
JNIEXPORT void JNICALL OS_NATIVE(CGPDFContextClose)
1608+
(JNIEnv *env, jclass that, jlong arg0)
1609+
{
1610+
OS_NATIVE_ENTER(env, that, CGPDFContextClose_FUNC);
1611+
CGPDFContextClose((CGContextRef)arg0);
1612+
OS_NATIVE_EXIT(env, that, CGPDFContextClose_FUNC);
1613+
}
1614+
#endif
1615+
1616+
#ifndef NO_CGPDFContextCreateWithURL
1617+
JNIEXPORT jlong JNICALL OS_NATIVE(CGPDFContextCreateWithURL)
1618+
(JNIEnv *env, jclass that, jlong arg0, jobject arg1, jlong arg2)
1619+
{
1620+
CGRect _arg1, *lparg1=NULL;
1621+
jlong rc = 0;
1622+
OS_NATIVE_ENTER(env, that, CGPDFContextCreateWithURL_FUNC);
1623+
if (arg1) if ((lparg1 = getCGRectFields(env, arg1, &_arg1)) == NULL) goto fail;
1624+
rc = (jlong)CGPDFContextCreateWithURL((CFURLRef)arg0, (const CGRect *)lparg1, (CFDictionaryRef)arg2);
1625+
fail:
1626+
if (arg1 && lparg1) setCGRectFields(env, arg1, lparg1);
1627+
OS_NATIVE_EXIT(env, that, CGPDFContextCreateWithURL_FUNC);
1628+
return rc;
1629+
}
1630+
#endif
1631+
1632+
#ifndef NO_CGPDFContextEndPage
1633+
JNIEXPORT void JNICALL OS_NATIVE(CGPDFContextEndPage)
1634+
(JNIEnv *env, jclass that, jlong arg0)
1635+
{
1636+
OS_NATIVE_ENTER(env, that, CGPDFContextEndPage_FUNC);
1637+
CGPDFContextEndPage((CGContextRef)arg0);
1638+
OS_NATIVE_EXIT(env, that, CGPDFContextEndPage_FUNC);
1639+
}
1640+
#endif
1641+
15991642
#ifndef NO_CGPathAddCurveToPoint
16001643
JNIEXPORT void JNICALL OS_NATIVE(CGPathAddCurveToPoint)
16011644
(JNIEnv *env, jclass that, jlong arg0, jlong arg1, jdouble arg2, jdouble arg3, jdouble arg4, jdouble arg5, jdouble arg6, jdouble arg7)

bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/library/os_stats.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2023 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
66
* which accompanies this distribution, and is available at
77
* https://www.eclipse.org/legal/epl-2.0/
88
*
99
* SPDX-License-Identifier: EPL-2.0
10-
*
11-
* Contributors:
12-
* IBM Corporation - initial API and implementation
1310
*******************************************************************************/
1411

1512
/* Note: This file was auto-generated by org.eclipse.swt.tools.internal.JNIGenerator */
@@ -117,6 +114,10 @@ typedef enum {
117114
CGImageGetHeight_FUNC,
118115
CGImageGetWidth_FUNC,
119116
CGImageRelease_FUNC,
117+
CGPDFContextBeginPage_FUNC,
118+
CGPDFContextClose_FUNC,
119+
CGPDFContextCreateWithURL_FUNC,
120+
CGPDFContextEndPage_FUNC,
120121
CGPathAddCurveToPoint_FUNC,
121122
CGPathAddLineToPoint_FUNC,
122123
CGPathApply_FUNC,

bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/org/eclipse/swt/internal/cocoa/OS.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3198,6 +3198,25 @@ public static Selector getSelector (long value) {
31983198
* @param image cast=(CGImageRef)
31993199
*/
32003200
public static final native void CGImageRelease(long image);
3201+
/**
3202+
* @param url cast=(CFURLRef)
3203+
* @param mediaBox cast=(const CGRect *)
3204+
* @param auxiliaryInfo cast=(CFDictionaryRef)
3205+
*/
3206+
public static final native long CGPDFContextCreateWithURL(long url, CGRect mediaBox, long auxiliaryInfo);
3207+
/**
3208+
* @param context cast=(CGContextRef)
3209+
* @param pageInfo cast=(CFDictionaryRef)
3210+
*/
3211+
public static final native void CGPDFContextBeginPage(long context, long pageInfo);
3212+
/**
3213+
* @param context cast=(CGContextRef)
3214+
*/
3215+
public static final native void CGPDFContextEndPage(long context);
3216+
/**
3217+
* @param context cast=(CGContextRef)
3218+
*/
3219+
public static final native void CGPDFContextClose(long context);
32013220
/**
32023221
* @param path cast=(CGMutablePathRef)
32033222
* @param m cast=(CGAffineTransform*)

0 commit comments

Comments
 (0)