This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
/
CGContextPDF.cs
274 lines (247 loc) · 9.88 KB
/
CGContextPDF.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
// CGContextPDF.cs: Implements the managed CGContextPDF
//
// Authors: Mono Team
//
// Copyright 2009-2010 Novell, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using MonoMac.ObjCRuntime;
using MonoMac.Foundation;
using MonoMac.CoreFoundation;
namespace MonoMac.CoreGraphics {
public class CGPDFPageInfo {
static IntPtr kCGPDFContextMediaBox;
static IntPtr kCGPDFContextCropBox;
static IntPtr kCGPDFContextBleedBox;
static IntPtr kCGPDFContextTrimBox;
static IntPtr kCGPDFContextArtBox;
static CGPDFPageInfo ()
{
IntPtr h = Dlfcn.dlopen (Constants.CoreGraphicsLibrary, 0);
try {
kCGPDFContextMediaBox = Dlfcn.GetIndirect (h, "kCGPDFContextMediaBox");
kCGPDFContextCropBox = Dlfcn.GetIndirect (h, "kCGPDFContextCropBox");
kCGPDFContextBleedBox = Dlfcn.GetIndirect (h, "kCGPDFContextBleedBox");
kCGPDFContextTrimBox = Dlfcn.GetIndirect (h, "kCGPDFContextTrimBox");
kCGPDFContextArtBox = Dlfcn.GetIndirect (h, "kCGPDFContextArtBox");
} finally {
Dlfcn.dlclose (h);
}
}
public RectangleF? MediaBox { get; set; }
public RectangleF? CropBox { get; set; }
public RectangleF? BleedBox { get; set; }
public RectangleF? TrimBox { get; set; }
public RectangleF? ArtBox { get; set; }
static void Add (NSMutableDictionary dict, IntPtr key, RectangleF? val)
{
if (!val.HasValue)
return;
NSData data;
unsafe {
RectangleF f = val.Value;
RectangleF *pf = &f;
data = NSData.FromBytes ((IntPtr) pf, 16);
}
dict.LowlevelSetObject (data, key);
}
internal virtual NSMutableDictionary ToDictionary ()
{
var ret = new NSMutableDictionary ();
Add (ret, kCGPDFContextMediaBox, MediaBox);
Add (ret, kCGPDFContextCropBox, CropBox);
Add (ret, kCGPDFContextBleedBox, BleedBox);
Add (ret, kCGPDFContextTrimBox, TrimBox);
Add (ret, kCGPDFContextArtBox, ArtBox);
return ret;
}
}
public class CGPDFInfo : CGPDFPageInfo {
static IntPtr kCGPDFContextTitle;
static IntPtr kCGPDFContextAuthor;
static IntPtr kCGPDFContextSubject;
static IntPtr kCGPDFContextKeywords;
static IntPtr kCGPDFContextCreator;
static IntPtr kCGPDFContextOwnerPassword;
static IntPtr kCGPDFContextUserPassword;
static IntPtr kCGPDFContextEncryptionKeyLength;
static IntPtr kCGPDFContextAllowsPrinting;
static IntPtr kCGPDFContextAllowsCopying;
#if false
static IntPtr kCGPDFContextOutputIntent;
static IntPtr kCGPDFXOutputIntentSubtype;
static IntPtr kCGPDFXOutputConditionIdentifier;
static IntPtr kCGPDFXOutputCondition;
static IntPtr kCGPDFXRegistryName;
static IntPtr kCGPDFXInfo;
static IntPtr kCGPDFXDestinationOutputProfile;
static IntPtr kCGPDFContextOutputIntents;
#endif
static CGPDFInfo ()
{
IntPtr h = Dlfcn.dlopen (Constants.CoreGraphicsLibrary, 0);
try {
kCGPDFContextTitle = Dlfcn.GetIndirect (h, "kCGPDFContextTitle");
kCGPDFContextAuthor = Dlfcn.GetIndirect (h, "kCGPDFContextAuthor");
kCGPDFContextSubject = Dlfcn.GetIndirect (h, "kCGPDFContextSubject");
kCGPDFContextKeywords = Dlfcn.GetIndirect (h, "kCGPDFContextKeywords");
kCGPDFContextCreator = Dlfcn.GetIndirect (h, "kCGPDFContextCreator");
kCGPDFContextOwnerPassword = Dlfcn.GetIndirect (h, "kCGPDFContextOwnerPassword");
kCGPDFContextUserPassword = Dlfcn.GetIndirect (h, "kCGPDFContextUserPassword");
kCGPDFContextEncryptionKeyLength = Dlfcn.GetIndirect (h, "kCGPDFContextEncryptionKeyLength");
kCGPDFContextAllowsPrinting = Dlfcn.GetIndirect (h, "kCGPDFContextAllowsPrinting");
kCGPDFContextAllowsCopying = Dlfcn.GetIndirect (h, "kCGPDFContextAllowsCopying");
#if false
kCGPDFContextOutputIntent = Dlfcn.GetIndirect (h, "kCGPDFContextOutputIntent");
kCGPDFXOutputIntentSubtype = Dlfcn.GetIndirect (h, "kCGPDFXOutputIntentSubtype");
kCGPDFXOutputConditionIdentifier = Dlfcn.GetIndirect (h, "kCGPDFXOutputConditionIdentifier");
kCGPDFXOutputCondition = Dlfcn.GetIndirect (h, "kCGPDFXOutputCondition");
kCGPDFXRegistryName = Dlfcn.GetIndirect (h, "kCGPDFXRegistryName");
kCGPDFXInfo = Dlfcn.GetIndirect (h, "kCGPDFXInfo");
kCGPDFXDestinationOutputProfile = Dlfcn.GetIndirect (h, "kCGPDFXDestinationOutputProfile");
kCGPDFContextOutputIntents = Dlfcn.GetIndirect (h, "kCGPDFContextOutputIntents");
#endif
} finally {
Dlfcn.dlclose (h);
}
}
public string Title { get; set; }
public string Author { get; set; }
public string Subject { get; set; }
public string [] Keywords { get; set; }
public string Creator { get; set; }
public string OwnerPassword { get; set; }
public string UserPassword { get; set; }
public int? EncryptionKeyLength { get; set; }
public bool? AllowsPrinting { get; set; }
public bool? AllowsCopying { get; set; }
//public NSDictionary OutputIntent { get; set; }
internal override NSMutableDictionary ToDictionary ()
{
var ret = base.ToDictionary ();
if (Title != null)
ret.LowlevelSetObject ((NSString) Title, kCGPDFContextTitle);
if (Author != null)
ret.LowlevelSetObject ((NSString) Author, kCGPDFContextAuthor);
if (Subject != null)
ret.LowlevelSetObject ((NSString) Subject, kCGPDFContextSubject);
if (Keywords != null && Keywords.Length > 0){
if (Keywords.Length == 1)
ret.LowlevelSetObject ((NSString) Keywords [0], kCGPDFContextKeywords);
else
ret.LowlevelSetObject (NSArray.FromStrings (Keywords), kCGPDFContextKeywords);
}
if (Creator != null)
ret.LowlevelSetObject ((NSString) Creator, kCGPDFContextCreator);
if (OwnerPassword != null)
ret.LowlevelSetObject ((NSString) OwnerPassword, kCGPDFContextOwnerPassword);
if (UserPassword != null)
ret.LowlevelSetObject ((NSString) UserPassword, kCGPDFContextUserPassword);
if (EncryptionKeyLength.HasValue)
ret.LowlevelSetObject (NSNumber.FromInt32 (EncryptionKeyLength.Value), kCGPDFContextEncryptionKeyLength);
if (AllowsPrinting.HasValue && AllowsPrinting.Value == false)
ret.LowlevelSetObject (CFBoolean.False.Handle, kCGPDFContextAllowsPrinting);
if (AllowsCopying.HasValue && AllowsCopying.Value == false)
ret.LowlevelSetObject (CFBoolean.False.Handle, kCGPDFContextAllowsCopying);
return ret;
}
}
public class CGContextPDF : CGContext {
bool closed;
[DllImport (Constants.CoreGraphicsLibrary)]
extern static IntPtr CGPDFContextCreateWithURL (IntPtr url, ref RectangleF rect, IntPtr dictionary);
public CGContextPDF (NSUrl url, RectangleF mediaBox, CGPDFInfo info)
{
if (url == null)
throw new ArgumentNullException ("url");
handle = CGPDFContextCreateWithURL (url.Handle, ref mediaBox, info == null ? IntPtr.Zero : info.ToDictionary ().Handle);
}
public CGContextPDF (NSUrl url, RectangleF mediaBox)
{
if (url == null)
throw new ArgumentNullException ("url");
handle = CGPDFContextCreateWithURL (url.Handle, ref mediaBox, IntPtr.Zero);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextClose(IntPtr handle);
public void Close ()
{
if (closed)
return;
CGPDFContextClose (handle);
closed = true;
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextBeginPage (IntPtr handle, IntPtr dict);
public void BeginPage (CGPDFPageInfo info)
{
CGPDFContextBeginPage (handle, info == null ? IntPtr.Zero : info.ToDictionary ().Handle);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextEndPage (IntPtr handle);
public void EndPage ()
{
CGPDFContextEndPage (handle);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextAddDocumentMetadata (IntPtr handle, IntPtr nsDataHandle);
public void AddDocumentMetadata (NSData data)
{
if (data == null)
return;
CGPDFContextAddDocumentMetadata (handle, data.Handle);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextSetURLForRect (IntPtr handle, IntPtr urlh, RectangleF rect);
public void SetUrl (NSUrl url, RectangleF region)
{
if (url == null)
throw new ArgumentNullException ("url");
CGPDFContextSetURLForRect (handle, url.Handle, region);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextAddDestinationAtPoint (IntPtr handle, IntPtr cfstring, PointF point);
public void AddDestination (string name, PointF point)
{
if (name == null)
throw new ArgumentNullException ("name");
CGPDFContextAddDestinationAtPoint (handle, new NSString (name).Handle, point);
}
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFContextSetDestinationForRect (IntPtr handle, IntPtr cfstr, RectangleF rect);
public void SetDestination (string name, RectangleF rect)
{
if (name == null)
throw new ArgumentNullException ("name");
CGPDFContextSetDestinationForRect (handle, new NSString (name).Handle, rect);
}
protected override void Dispose (bool disposing)
{
if (disposing)
Close ();
base.Dispose (disposing);
}
}
}