~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp/PSSR.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Resources;
 
33
using System.Reflection;
 
34
using System.Text.RegularExpressions;
 
35
using PdfSharp.Drawing;
 
36
using PdfSharp.Pdf;
 
37
 
 
38
#pragma warning disable 1591
 
39
 
 
40
namespace PdfSharp
 
41
{
 
42
  /// <summary>
 
43
  /// The Pdf-Sharp-String-Resources.
 
44
  /// </summary>
 
45
  public static class PSSR
 
46
  {
 
47
    // How to use:
 
48
    // Create a function or property for each message text, depending on how much parameters are
 
49
    // part of the message. For the time beginning, type plain english text in the function or property. 
 
50
    // The use of functions is save when a parameter must be changed. The compiler tells you all
 
51
    // places in your code that must be modified.
 
52
    // For localization, create a enum value for each function or property with the same name. Then
 
53
    // create localized message files with the enum values as messages identifiers. In the properties
 
54
    // and functions all text is replaced by Format or GetString functions with the according enum value
 
55
    // as first parameter. The use of enums ensures that typing errors in message resource names are 
 
56
    // simply impossible. Use the TestResourceMessages function to ensure that each enum value has an 
 
57
    // appropriate message text.
 
58
 
 
59
    #region Helper functions
 
60
    /// <summary>
 
61
    /// Loads the message from the resource associated with the enum type and formats it
 
62
    /// using 'String.Format'. Because this function is intended to be used during error
 
63
    /// handling it never raises an exception.
 
64
    /// </summary>
 
65
    /// <param name="id">The type of the parameter identifies the resource
 
66
    /// and the name of the enum identifies the message in the resource.</param>
 
67
    /// <param name="args">Parameters passed through 'String.Format'.</param>
 
68
    /// <returns>The formatted message.</returns>
 
69
    public static string Format(PSMsgID id, params object[] args)
 
70
    {
 
71
      string message;
 
72
      try
 
73
      {
 
74
        message = PSSR.GetString(id);
 
75
        if (message != null)
 
76
          message = Format(message, args);
 
77
        else
 
78
          message = "INTERNAL ERROR: Message not found in resources.";
 
79
        return message;
 
80
      }
 
81
      catch (Exception ex)
 
82
      {
 
83
        message = String.Format("UNEXPECTED ERROR while formatting message with ID {0}: {1}", id.ToString(), ex.ToString());
 
84
      }
 
85
      return message;
 
86
    }
 
87
 
 
88
    public static string Format(string format, params object[] args)
 
89
    {
 
90
      if (format == null)
 
91
        throw new ArgumentNullException("format");
 
92
 
 
93
      string message;
 
94
      try
 
95
      {
 
96
        message = String.Format(format, args);
 
97
      }
 
98
      catch (Exception ex)
 
99
      {
 
100
        message = String.Format("UNEXPECTED ERROR while formatting message '{0}': {1}", format, ex.ToString());
 
101
      }
 
102
      return message;
 
103
    }
 
104
 
 
105
    /// <summary>
 
106
    /// Gets the localized message identified by the specified DomMsgID.
 
107
    /// </summary>
 
108
    public static string GetString(PSMsgID id)
 
109
    {
 
110
      return PSSR.ResMngr.GetString(id.ToString());
 
111
    }
 
112
 
 
113
    #endregion
 
114
 
 
115
    #region General messages
 
116
 
 
117
    public static string IndexOutOfRange
 
118
    {
 
119
      get { return "The index is out of range."; }
 
120
    }
 
121
 
 
122
    public static string ListEnumCurrentOutOfRange
 
123
    {
 
124
      get { return "Enumeration out of range."; }
 
125
    }
 
126
 
 
127
    public static string PageIndexOutOfRange
 
128
    {
 
129
      get { return "The index of a page is out of range."; }
 
130
    }
 
131
 
 
132
    public static string OutlineIndexOutOfRange
 
133
    {
 
134
      get { return "The index of an outline is out of range."; }
 
135
    }
 
136
 
 
137
    public static string InvalidValue(int val, string name, int min, int max)
 
138
    {
 
139
      return Format("{0} is not a valid value for {1}. {1} should be greater than or equal to {2} and less than or equal to {3}.",
 
140
        val, name, min, max);
 
141
    }
 
142
 
 
143
    public static string ObsoleteFunktionCalled
 
144
    {
 
145
      get { return "The function is obsolete and must not be called."; }
 
146
    }
 
147
 
 
148
    public static string OwningDocumentRequired
 
149
    {
 
150
      get { return "The PDF object must belong to a PdfDocument, but property Document is null."; }
 
151
    }
 
152
 
 
153
    public static string FileNotFound(string path)
 
154
    {
 
155
      return Format("The file '{0}' does not exist.", path);
 
156
    }
 
157
 
 
158
    public static string FontDataReadOnly
 
159
    {
 
160
      get { return "Font data is read-only"; }
 
161
    }
 
162
 
 
163
    #endregion
 
164
 
 
165
    #region XGraphics specific messages
 
166
 
 
167
    // ----- XGraphics ----------------------------------------------------------------------------
 
168
 
 
169
    public static string PointArrayEmpty
 
170
    {
 
171
      get { return "The PointF array must not be empty."; }
 
172
    }
 
173
 
 
174
    public static string PointArrayAtLeast(int count)
 
175
    {
 
176
      return Format("The point array must contain {0} or more points.", count);
 
177
    }
 
178
 
 
179
    public static string NeedPenOrBrush
 
180
    {
 
181
      get { return "XPen or XBrush or both must not be null."; }
 
182
    }
 
183
 
 
184
    public static string CannotChangeImmutableObject(string typename)
 
185
    {
 
186
      return String.Format("You cannot change this immutable {0} object.", typename);
 
187
    }
 
188
 
 
189
    #endregion
 
190
 
 
191
    #region PDF specific messages
 
192
 
 
193
    // ----- PDF ----------------------------------------------------------------------------------
 
194
 
 
195
    public static string InvalidPdf
 
196
    {
 
197
      get { return "The file is not a valid PDF document."; }
 
198
    }
 
199
 
 
200
    public static string InvalidVersionNumber
 
201
    {
 
202
      get { return "Invalid version number. Valid values are 12, 13, and 14."; }
 
203
    }
 
204
 
 
205
    public static string CannotHandleXRefStreams
 
206
    {
 
207
      get { return "Cannot handle iref streams. The current implementation of PDFsharp cannot handle this PDF feature introduced with Acrobat 6."; }
 
208
    }
 
209
 
 
210
    public static string PasswordRequired
 
211
    {
 
212
      get { return "A password is required to open the PDF document."; }
 
213
    }
 
214
 
 
215
    public static string InvalidPassword
 
216
    {
 
217
      get { return "The specified password is invalid."; }
 
218
    }
 
219
 
 
220
    public static string OwnerPasswordRequired
 
221
    {
 
222
      get { return "To modify the document the owner password is required"; }
 
223
    }
 
224
 
 
225
    public static string UserOrOwnerPasswordRequired
 
226
    {
 
227
      get { return GetString(PSMsgID.UserOrOwnerPasswordRequired); }
 
228
      //get { return "At least a user or an owner password is required to encrypt the document."; }
 
229
    }
 
230
 
 
231
    public static string CannotModify
 
232
    {
 
233
      get { return "The document cannot be modified."; }
 
234
    }
 
235
 
 
236
    public static string NameMustStartWithSlash
 
237
    {
 
238
      //get { return GetString(PSMsgID.NameMustStartWithSlash); }
 
239
      get { return "A PDF name must start with a slash (/)."; }
 
240
    }
 
241
 
 
242
    public static string ImportPageNumberOutOfRange(int pageNumber, int maxPage, string path)
 
243
    {
 
244
      return String.Format("The page cannot be imported from document '{2}', because the page number is out of range. " +
 
245
        "The specified page number is {0}, but it must be in the range from 1 to {1}.", pageNumber, maxPage, path);
 
246
    }
 
247
 
 
248
    public static string MultiplePageInsert
 
249
    {
 
250
      get { return "The page cannot be added to this document because the document already owned this page."; }
 
251
    }
 
252
 
 
253
    public static string UnexpectedTokenInPdfFile
 
254
    {
 
255
      get { return "Unexpected token in PDF file. The PDF file may be currupt. If it is not, please send us the file for serivce."; }
 
256
    }
 
257
 
 
258
    public static string InappropriateColorSpace(PdfColorMode colorMode, XColorSpace colorSpace)
 
259
    {
 
260
      string mode;
 
261
      switch (colorMode)
 
262
      {
 
263
        case PdfColorMode.Rgb:
 
264
          mode = "RGB";
 
265
          break;
 
266
 
 
267
        case PdfColorMode.Cmyk:
 
268
          mode = "CMYK";
 
269
          break;
 
270
 
 
271
        default:
 
272
          mode = "(undefined)";
 
273
          break;
 
274
      }
 
275
 
 
276
      string space;
 
277
      switch (colorSpace)
 
278
      {
 
279
        case XColorSpace.Rgb:
 
280
          space = "RGB";
 
281
          break;
 
282
 
 
283
        case XColorSpace.Cmyk:
 
284
          space = "CMYK";
 
285
          break;
 
286
 
 
287
        case XColorSpace.GrayScale:
 
288
          space = "grayscale";
 
289
          break;
 
290
 
 
291
        default:
 
292
          space = "(undefined)";
 
293
          break;
 
294
      }
 
295
      return String.Format("The document requires color mode {0}, but a color is defined using {1}. " +
 
296
        "Use only colors that match the color mode of the PDF document", mode, space);
 
297
    }
 
298
 
 
299
    public static string CannotGetGlyphTypeface(string fontName)
 
300
    {
 
301
      return Format("Cannot get a matching glyph typeface for font '{0}'.", fontName);
 
302
    }
 
303
 
 
304
 
 
305
    // ----- PdfParser ----------------------------------------------------------------------------
 
306
 
 
307
    public static string UnexpectedToken(string token)
 
308
    {
 
309
      return Format(PSMsgID.UnexpectedToken, token);
 
310
      //return Format("Token '{0}' was not expected.", token);
 
311
    }
 
312
 
 
313
    public static string UnknownEncryption
 
314
    {
 
315
      get { return GetString(PSMsgID.UnknownEncryption); }
 
316
      //get { return "The PDF document is protected with an encryption not supported by PDFsharp."; }
 
317
    }
 
318
 
 
319
    #endregion
 
320
 
 
321
    #region Resource manager
 
322
 
 
323
    /// <summary>
 
324
    /// Gets the resource manager for this module.
 
325
    /// </summary>
 
326
    public static ResourceManager ResMngr
 
327
    {
 
328
      get
 
329
      {
 
330
        if (PSSR.resmngr == null)
 
331
        {
 
332
#if true_
 
333
          // Force the english language, even on a German PC.
 
334
          System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
 
335
#endif
 
336
          PSSR.resmngr = new ResourceManager("PdfSharp.Resources.Messages", Assembly.GetExecutingAssembly());
 
337
        }
 
338
        return PSSR.resmngr;
 
339
      }
 
340
    }
 
341
    static ResourceManager resmngr;
 
342
 
 
343
    /// <summary>
 
344
    /// Writes all messages defined by PSMsgID.
 
345
    /// </summary>
 
346
    [Conditional("DEBUG")]
 
347
    public static void TestResourceMessages()
 
348
    {
 
349
      string[] names = Enum.GetNames(typeof(PSMsgID));
 
350
      foreach (string name in names)
 
351
      {
 
352
        string message = String.Format("{0}: '{1}'", name, ResMngr.GetString(name));
 
353
        Debug.Assert(message != null);
 
354
        Debug.WriteLine(message);
 
355
      }
 
356
    }
 
357
 
 
358
    static PSSR()
 
359
    {
 
360
      //TestResourceMessages();
 
361
    }
 
362
 
 
363
    #endregion
 
364
  }
 
365
}