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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
//
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// 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.
#endregion
using System;
using System.Collections;
using System.Text;
using System.IO;
using PdfSharp.Internal;
namespace PdfSharp.Pdf.Advanced
{
/// <summary>
/// Represents a PDF resource object.
/// </summary>
public sealed class PdfResources : PdfDictionary
{
// Resource management works roughly like this:
// When the user creates an XFont and uses it in the XGraphics of a PdfPage, then at the first time
// a PdfFont is created and cached in the document global font table. If the user creates a new
// XFont object for an exisisting PdfFont, the PdfFont object is reused. When the PdfFont is added
// to the resources of a PdfPage for the first time, it is added to the page local PdfResourceMap for
// fonts and automatically associated with a local resource name.
/// <summary>
/// Initializes a new instance of the <see cref="PdfResources"/> class.
/// </summary>
/// <param name="document">The document.</param>
public PdfResources(PdfDocument document)
: base(document)
{
Elements[Keys.ProcSet] = new PdfLiteral("[/PDF/Text/ImageB/ImageC/ImageI]");
}
PdfResources(PdfDictionary dict)
: base(dict)
{
}
/// <summary>
/// Adds the specified font to this resource dictionary and returns its local resource name.
/// </summary>
public string AddFont(PdfFont font)
{
string name = (string)this.resources[font];
if (name == null)
{
name = NextFontName;
this.resources[font] = name;
if (font.Reference == null)
this.Owner.irefTable.Add(font);
Fonts.Elements[name] = font.Reference;
}
return name;
}
/// <summary>
/// Adds the specified image to this resource dictionary
/// and returns its local resource name.
/// </summary>
public string AddImage(PdfImage image)
{
string name = (string)this.resources[image];
if (name == null)
{
name = NextImageName;
this.resources[image] = name;
if (image.Reference == null)
this.Owner.irefTable.Add(image);
XObjects.Elements[name] = image.Reference;
}
return name;
}
/// <summary>
/// Adds the specified form object to this resource dictionary
/// and returns its local resource name.
/// </summary>
public string AddForm(PdfFormXObject form)
{
string name = (string)this.resources[form];
if (name == null)
{
name = NextFormName;
this.resources[form] = name;
if (form.Reference == null)
this.Owner.irefTable.Add(form);
XObjects.Elements[name] = form.Reference;
}
return name;
}
/// <summary>
/// Adds the specified graphics state to this resource dictionary
/// and returns its local resource name.
/// </summary>
public string AddExtGState(PdfExtGState extGState)
{
string name = (string)this.resources[extGState];
if (name == null)
{
name = NextExtGStateName;
this.resources[extGState] = name;
if (extGState.Reference == null)
this.Owner.irefTable.Add(extGState);
ExtGStates.Elements[name] = extGState.Reference;
}
return name;
}
/// <summary>
/// Adds the specified pattern to this resource dictionary
/// and returns its local resource name.
/// </summary>
public string AddPattern(PdfShadingPattern pattern)
{
string name = (string)this.resources[pattern];
if (name == null)
{
name = NextPatternName;
this.resources[pattern] = name;
if (pattern.Reference == null)
this.Owner.irefTable.Add(pattern);
Patterns.Elements[name] = pattern.Reference;
}
return name;
}
/// <summary>
/// Adds the specified pattern to this resource dictionary
/// and returns its local resource name.
/// </summary>
public string AddPattern(PdfTilingPattern pattern)
{
string name = (string)this.resources[pattern];
if (name == null)
{
name = NextPatternName;
this.resources[pattern] = name;
if (pattern.Reference == null)
this.Owner.irefTable.Add(pattern);
Patterns.Elements[name] = pattern.Reference;
}
return name;
}
/// <summary>
/// Adds the specified shading to this resource dictionary
/// and returns its local resource name.
/// </summary>
public string AddShading(PdfShading shading)
{
string name = (string)this.resources[shading];
if (name == null)
{
name = NextShadingName;
this.resources[shading] = name;
if (shading.Reference == null)
this.Owner.irefTable.Add(shading);
Shadings.Elements[name] = shading.Reference;
}
return name;
}
/// <summary>
/// Gets the fonts map.
/// </summary>
internal PdfResourceMap Fonts
{
get
{
if (this.fonts == null)
this.fonts = (PdfResourceMap)Elements.GetValue(Keys.Font, VCF.Create);
return this.fonts;
}
}
PdfResourceMap fonts;
/// <summary>
/// Gets the external objects map.
/// </summary>
internal PdfResourceMap XObjects
{
get
{
if (this.xObjects == null)
this.xObjects = (PdfResourceMap)Elements.GetValue(Keys.XObject, VCF.Create);
return this.xObjects;
}
}
PdfResourceMap xObjects;
// TODO: make own class
internal PdfResourceMap ExtGStates
{
get
{
if (this.extGStates == null)
this.extGStates = (PdfResourceMap)Elements.GetValue(Keys.ExtGState, VCF.Create);
return this.extGStates;
}
}
PdfResourceMap extGStates;
// TODO: make own class
internal PdfResourceMap ColorSpaces
{
get
{
if (this.colorSpaces == null)
this.colorSpaces = (PdfResourceMap)Elements.GetValue(Keys.ColorSpace, VCF.Create);
return this.colorSpaces;
}
}
PdfResourceMap colorSpaces;
// TODO: make own class
internal PdfResourceMap Patterns
{
get
{
if (this.patterns == null)
this.patterns = (PdfResourceMap)Elements.GetValue(Keys.Pattern, VCF.Create);
return this.patterns;
}
}
PdfResourceMap patterns;
// TODO: make own class
internal PdfResourceMap Shadings
{
get
{
if (this.shadings == null)
this.shadings = (PdfResourceMap)Elements.GetValue(Keys.Shading, VCF.Create);
return this.shadings;
}
}
PdfResourceMap shadings;
// TODO: make own class
internal PdfResourceMap Properties
{
get
{
if (this.properties == null)
this.properties = (PdfResourceMap)Elements.GetValue(Keys.Properties, VCF.Create);
return this.properties;
}
}
PdfResourceMap properties;
/// <summary>
/// Gets a new local name for this resource.
/// </summary>
string NextFontName
{
get
{
string name;
while (ExistsResourceNames(name = String.Format("/F{0}", this.fontNumber++))) { }
return name;
}
}
int fontNumber;
/// <summary>
/// Gets a new local name for this resource.
/// </summary>
string NextImageName
{
get
{
string name;
while (ExistsResourceNames(name = String.Format("/I{0}", this.imageNumber++))) { }
return name;
}
}
int imageNumber;
/// <summary>
/// Gets a new local name for this resource.
/// </summary>
string NextFormName
{
get
{
string name;
while (ExistsResourceNames(name = String.Format("/Fm{0}", this.formNumber++))) { }
return name;
}
}
int formNumber;
/// <summary>
/// Gets a new local name for this resource.
/// </summary>
string NextExtGStateName
{
get
{
string name;
while (ExistsResourceNames(name = String.Format("/GS{0}", this.ExtGStateNumber++))) { }
return name;
}
}
int ExtGStateNumber;
/// <summary>
/// Gets a new local name for this resource.
/// </summary>
string NextPatternName
{
get
{
string name;
while (ExistsResourceNames(name = String.Format("/Pa{0}", this.PatternNumber++))) ;
return name;
}
}
int PatternNumber;
/// <summary>
/// Gets a new local name for this resource.
/// </summary>
string NextShadingName
{
get
{
string name;
while (ExistsResourceNames(name = String.Format("/Sh{0}", this.ShadingNumber++))) ;
return name;
}
}
int ShadingNumber;
/// <summary>
/// Check whether a resource name is already used in the context of this resource dictionary.
/// PDF4NET uses GUIDs as resource names, but I think this weapon is to heavy.
/// </summary>
internal bool ExistsResourceNames(string name)
{
// TODO: more precise: is this page imported and is PageOptions != Replace
// BUG:
//if (!this.Owner.IsImported)
// return false;
// Collect all resouce names of all imported resources.
if (this.importedResourceNames == null)
{
this.importedResourceNames = new Hashtable();
if (Elements[Keys.Font] != null)
Fonts.CollectResourceNames(this.importedResourceNames);
if (Elements[Keys.XObject] != null)
XObjects.CollectResourceNames(this.importedResourceNames);
if (Elements[Keys.ExtGState] != null)
ExtGStates.CollectResourceNames(this.importedResourceNames);
if (Elements[Keys.ColorSpace] != null)
ColorSpaces.CollectResourceNames(this.importedResourceNames);
if (Elements[Keys.Pattern] != null)
Patterns.CollectResourceNames(this.importedResourceNames);
if (Elements[Keys.Shading] != null)
Shadings.CollectResourceNames(this.importedResourceNames);
if (Elements[Keys.Properties] != null)
Properties.CollectResourceNames(this.importedResourceNames);
}
return this.importedResourceNames.Contains(name);
// This is superfluous because PDFsharp resource names cannot be double.
// this.importedResourceNames.Add(name, null);
}
/// <summary>
/// All the names of imported resources.
/// </summary>
Hashtable importedResourceNames;
/// <summary>
/// Maps all PDFsharp resources to their local resource names.
/// </summary>
readonly Hashtable resources = new Hashtable();
/// <summary>
/// Predefined keys of this dictionary.
/// </summary>
public sealed class Keys : KeysBase
{
/// <summary>
/// (Optional) A dictionary that maps resource names to graphics state
/// parameter dictionaries.
/// </summary>
[KeyInfo(KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string ExtGState = "/ExtGState";
/// <summary>
/// (Optional) A dictionary that maps each resource name to either the name of a
/// device-dependent color space or an array describing a color space.
/// </summary>
[KeyInfo(KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string ColorSpace = "/ColorSpace";
/// <summary>
/// (Optional) A dictionary that maps each resource name to either the name of a
/// device-dependent color space or an array describing a color space.
/// </summary>
[KeyInfo(KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string Pattern = "/Pattern";
/// <summary>
/// (Optional; PDF 1.3) A dictionary that maps resource names to shading dictionaries.
/// </summary>
[KeyInfo("1.3", KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string Shading = "/Shading";
/// <summary>
/// (Optional) A dictionary that maps resource names to external objects.
/// </summary>
[KeyInfo(KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string XObject = "/XObject";
/// <summary>
/// (Optional) A dictionary that maps resource names to font dictionaries.
/// </summary>
[KeyInfo(KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string Font = "/Font";
/// <summary>
/// (Optional) An array of predefined procedure set names.
/// </summary>
[KeyInfo(KeyType.Array | KeyType.Optional)]
public const string ProcSet = "/ProcSet";
/// <summary>
/// (Optional; PDF 1.2) A dictionary that maps resource names to property list
/// dictionaries for marked content.
/// </summary>
[KeyInfo(KeyType.Dictionary | KeyType.Optional, typeof(PdfResourceMap))]
public const string Properties = "/Properties";
/// <summary>
/// Gets the KeysMeta for these keys.
/// </summary>
internal static DictionaryMeta Meta
{
get
{
if (Keys.meta == null)
Keys.meta = CreateMeta(typeof(Keys));
return Keys.meta;
}
}
static DictionaryMeta meta;
}
/// <summary>
/// Gets the KeysMeta of this dictionary type.
/// </summary>
internal override DictionaryMeta Meta
{
get { return Keys.Meta; }
}
}
}
|