MuPDFCore 1.8.0
Multiplatform .NET bindings for MuPDF
Loading...
Searching...
No Matches
MuPDFContext.cs
1/*
2 MuPDFCore - A set of multiplatform .NET Core bindings for MuPDF.
3 Copyright (C) 2020 Giorgio Bianchini
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>
16*/
17
18using System;
19
20namespace MuPDFCore
21{
22 /// <summary>
23 /// A wrapper around a MuPDF context object, which contains the exception stack and the resource cache store.
24 /// </summary>
25 public class MuPDFContext : IDisposable
26 {
27 /// <summary>
28 /// A pointer to the native context object.
29 /// </summary>
30 internal readonly IntPtr NativeContext;
31
32 /// <summary>
33 /// The current size in bytes of the resource cache store. Read-only.
34 /// </summary>
35 public long StoreSize
36 {
37 get
38 {
39 return (long)NativeMethods.GetCurrentStoreSize(this.NativeContext);
40 }
41 }
42
43 /// <summary>
44 /// The maximum size in bytes of the resource cache store. Read-only.
45 /// </summary>
46 public long StoreMaxSize
47 {
48 get
49 {
50 return (long)NativeMethods.GetMaxStoreSize(this.NativeContext);
51 }
52 }
53
54 /// <summary>
55 /// Sets the current anti-aliasing level. Changing this value will affect both
56 /// the <see cref="TextAntiAliasing"/> and the <see cref="GraphicsAntiAliasing"/>.
57 /// </summary>
58 public int AntiAliasing
59 {
60 set
61 {
62 if (value < 0 || value > 8)
63 {
64 throw new ArgumentOutOfRangeException(nameof(value), value, "The anti-aliasing level must range between 0 and 8 (inclusive).");
65 }
66
67 NativeMethods.SetAALevel(this.NativeContext, value, -1, -1);
68 }
69 }
70
71 /// <summary>
72 /// Gets or sets the current text anti-aliasing level.
73 /// </summary>
75 {
76 get
77 {
78 NativeMethods.GetAALevel(this.NativeContext, out _, out _, out int tbr);
79 return tbr;
80 }
81
82 set
83 {
84 if (value < 0 || value > 8)
85 {
86 throw new ArgumentOutOfRangeException(nameof(value), value, "The anti-aliasing level must range between 0 and 8 (inclusive).");
87 }
88
89 NativeMethods.SetAALevel(this.NativeContext, -1, -1, value);
90 }
91 }
92
93 /// <summary>
94 /// Gets or sets the current graphics anti-aliasing level.
95 /// </summary>
97 {
98 get
99 {
100 NativeMethods.GetAALevel(this.NativeContext, out _, out int tbr, out _);
101 return tbr;
102 }
103
104 set
105 {
106 if (value < 0 || value > 8)
107 {
108 throw new ArgumentOutOfRangeException(nameof(value), value, "The anti-aliasing level must range between 0 and 8 (inclusive).");
109 }
110
111 int prevTxt = this.TextAntiAliasing;
112 NativeMethods.SetAALevel(this.NativeContext, -1, value, prevTxt);
113 }
114 }
115
116 /// <summary>
117 /// Create a new <see cref="MuPDFContext"/> instance with the specified cache store size.
118 /// </summary>
119 /// <param name="storeSize">The maximum size in bytes of the resource cache store. The default value is 256 MiB.</param>
120 public MuPDFContext(uint storeSize = 256 << 20)
121 {
122 ExitCodes result = (ExitCodes)NativeMethods.CreateContext((ulong)storeSize, ref NativeContext);
123
124 switch (result)
125 {
126 case ExitCodes.EXIT_SUCCESS:
127 break;
128 case ExitCodes.ERR_CANNOT_CREATE_CONTEXT:
129 throw new MuPDFException("Cannot create MuPDF context", result);
130 case ExitCodes.ERR_CANNOT_REGISTER_HANDLERS:
131 throw new MuPDFException("Cannot register document handlers", result);
132 default:
133 throw new MuPDFException("Unknown error", result);
134 }
135 }
136
137 /// <summary>
138 /// Wrap an existing pointer to a native MuPDF context object.
139 /// </summary>
140 /// <param name="nativeContext">The pointer to the native context that should be used.</param>
141 internal MuPDFContext(IntPtr nativeContext)
142 {
143 this.NativeContext = nativeContext;
144 }
145
146 /// <summary>
147 /// Evict all items from the resource cache store (freeing the memory where they were held).
148 /// </summary>
149 public void ClearStore()
150 {
151 NativeMethods.EmptyStore(this.NativeContext);
152 }
153
154 /// <summary>
155 /// Evict items from the resource cache store (freeing the memory where they were held) until the the size of the store drops to the specified fraction of the current size.
156 /// </summary>
157 /// <param name="fraction">The fraction of the current size that constitutes the target size of the store. If this is &lt;= 0, the cache is cleared. If this is &gt;= 1, nothing happens.</param>
158 public void ShrinkStore(double fraction)
159 {
160 if (fraction <= 0)
161 {
162 ClearStore();
163 }
164 else if (Math.Round(fraction * 100) < 100)
165 {
166 NativeMethods.ShrinkStore(this.NativeContext, (uint)Math.Round(fraction * 100));
167 }
168 }
169
170 private bool disposedValue;
171
172 ///<inheritdoc/>
173 protected virtual void Dispose(bool disposing)
174 {
175 if (!disposedValue)
176 {
177 if (disposing)
178 {
179 // TODO: eliminare lo stato gestito (oggetti gestiti)
180 }
181
182 NativeMethods.DisposeContext(NativeContext);
183 disposedValue = true;
184 }
185 }
186
187 ///<inheritdoc/>
188 ~MuPDFContext()
189 {
190 Dispose(disposing: false);
191 }
192
193 ///<inheritdoc/>
194 public void Dispose()
195 {
196 Dispose(disposing: true);
197 GC.SuppressFinalize(this);
198 }
199 }
200}
A wrapper around a MuPDF context object, which contains the exception stack and the resource cache st...
Definition: MuPDFContext.cs:26
int AntiAliasing
Sets the current anti-aliasing level. Changing this value will affect both the TextAntiAliasing and t...
Definition: MuPDFContext.cs:59
int GraphicsAntiAliasing
Gets or sets the current graphics anti-aliasing level.
Definition: MuPDFContext.cs:97
void ClearStore()
Evict all items from the resource cache store (freeing the memory where they were held).
void ShrinkStore(double fraction)
Evict items from the resource cache store (freeing the memory where they were held) until the the siz...
long StoreMaxSize
The maximum size in bytes of the resource cache store. Read-only.
Definition: MuPDFContext.cs:47
int TextAntiAliasing
Gets or sets the current text anti-aliasing level.
Definition: MuPDFContext.cs:75
MuPDFContext(uint storeSize=256<< 20)
Create a new MuPDFContext instance with the specified cache store size.
long StoreSize
The current size in bytes of the resource cache store. Read-only.
Definition: MuPDFContext.cs:36
The exception that is thrown when a MuPDF operation fails.
Definition: MuPDF.cs:494
ExitCodes
Exit codes returned by native methods describing various errors that can occur.
Definition: MuPDF.cs:32