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
|
#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.Diagnostics;
using System.Collections;
using System.Globalization;
using System.Text;
using System.IO;
using PdfSharp.Drawing;
using PdfSharp.Internal;
using PdfSharp.Fonts.TrueType;
namespace PdfSharp.Pdf.Advanced
{
internal enum FontType
{
/// <summary>
/// TrueType with WinAnsi encoding.
/// </summary>
TrueType = 1,
/// <summary>
/// TrueType with Identity-H encoding (unicode).
/// </summary>
Type0 = 2,
}
/// <summary>
/// Contains all used fonts of a document.
/// </summary>
internal sealed class PdfFontTable : PdfResourceTable
{
/// <summary>
/// Initializes a new instance of this class, which is a singleton for each document.
/// </summary>
public PdfFontTable(PdfDocument document)
: base(document)
{
}
/// <summary>
/// Gets a PdfFont from an XFont. If no PdfFont already exists, a new one is created.
/// </summary>
public PdfFont GetFont(XFont font)
{
string fontName = font.Name;
PdfFontTable.FontSelector selector = font.selector;
if (selector == null)
{
selector = new FontSelector(font);
font.selector = selector;
}
PdfFont pdfFont = this.fonts[selector] as PdfFont;
if (pdfFont == null)
{
if (font.Unicode)
pdfFont = new PdfType0Font(this.owner, font);
else
pdfFont = new PdfTrueTypeFont(this.owner, font);
//pdfFont.Document = this.document;
Debug.Assert(pdfFont.Owner == this.owner);
this.fonts[selector] = pdfFont;
//if (this.owner.EarlyWrite)
//{
// //pdfFont.Close(); delete
// //pdfFont.AssignObjID(ref this.document.ObjectID); // BUG: just test code!!!!
// //pdfFont.WriteObject(null);
//}
}
return pdfFont;
#if false
goto TrueTypeFont;
switch (font.Name)
{
case "Times":
case "Times New Roman":
std = 0;
break;
case "Helvetica":
std = 1;
break;
case "Courier":
std = 2;
break;
case "Symbol":
std = 3;
break;
case "ZapfDingbats":
std = 4;
break;
}
if (std != -1)
{
int idx = (int)font.Style & 0x3;
string name = pdfStandardFonts[std][idx];
PdfFont pdfFont = GetFont(name);
if (pdfFont == null)
{
pdfFont = new PdfFont();
pdfFont.SubType = "/Type1";
pdfFont.BaseFont = name;
pdfFont.DefaultName = string.Format("F{0}", PdfFontTable.fontNumber++);
}
return pdfFont;
}
else
{
TrueTypeFont:
// TrueType font
PdfFont pdfFont = new PdfFont();
pdfFont.SubType = "/TrueType";
pdfFont.FirstChar = 0;
pdfFont.LastChar = 255;
pdfFont.BaseFont = font.Name;
pdfFont.DefaultName = string.Format("F{0}", PdfFontTable.fontNumber++);
}
#endif
// TrueType font
// PdfFont pdfFont = new PdfFont();
// pdfFont.descriptor = new PdfFontDescriptor((TrueTypeDescriptor)FontDescriptorStock.Global.CreateDescriptor(font));
// pdfFont.SubType = "/TrueType";
// pdfFont.FirstChar = 0;
// pdfFont.LastChar = 255;
// pdfFont.BaseFont = font.Name;
// pdfFont.BaseFont = pdfFont.BaseFont.Replace(" ", "");
// switch (font.Style & (XFontStyle.Bold | XFontStyle.Italic))
// {
// case XFontStyle.Bold:
// pdfFont.BaseFont += ",Bold";
// break;
//
// case XFontStyle.Italic:
// pdfFont.BaseFont += ",Italic";
// break;
//
// case XFontStyle.Bold | XFontStyle.Italic:
// pdfFont.BaseFont += ",BoldItalic";
// break;
// }
// pdfFont.descriptor.FontName = pdfFont.BaseFont;
// pdfFont.DefaultName = string.Format("F{0}", PdfFontTable.fontNumber++);
}
//string[][] pdfStandardFonts =
//{
// new string[]{"Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic"},
// new string[]{"Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique"},
// new string[]{"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique"},
// new string[]{"Symbol", "Symbol", "Symbol", "Symbol"},
// new string[]{"ZapfDingbats", "ZapfDingbats", "ZapfDingbats", "ZapfDingbats"},
//};
#if true
/// <summary>
/// Gets a PdfFont from a font program. If no PdfFont already exists, a new one is created.
/// </summary>
public PdfFont GetFont(string idName, byte[] fontData)
{
PdfFontTable.FontSelector selector = new FontSelector(idName);
PdfFont pdfFont = this.fonts[selector] as PdfFont;
if (pdfFont == null)
{
//if (font.Unicode)
pdfFont = new PdfType0Font(this.owner, idName, fontData);
//else
// pdfFont = new PdfTrueTypeFont(this.owner, font);
//pdfFont.Document = this.document;
Debug.Assert(pdfFont.Owner == this.owner);
this.fonts[selector] = pdfFont;
}
return pdfFont;
}
#endif
/// <summary>
/// Tries to gets a PdfFont from the font dictionary.
/// Returns null if no such PdfFont exists.
/// </summary>
public PdfFont TryGetFont(string idName)
{
PdfFontTable.FontSelector selector = new FontSelector(idName);
PdfFont pdfFont = this.fonts[selector] as PdfFont;
return pdfFont;
}
/// <summary>
/// Map from PdfFontSelector to PdfFont.
/// </summary>
Hashtable fonts = new Hashtable();
public void PrepareForSave()
{
foreach (PdfFont font in this.fonts.Values)
font.PrepareForSave();
}
/// <summary>
/// A collection of information that uniquely idendifies a particular PDF font.
/// ... more docu...
/// Two PDF fonts are equal if and only if their font selector objects are equal.
/// </summary>
public class FontSelector
{
/// <summary>
/// Initializes a new instance of PdfFontSelector from an XFont.
/// </summary>
public FontSelector(XFont font)
{
this.name = font.Name;
// Ignore Strikeout and Underline
this.style = font.Style & (XFontStyle.Bold | XFontStyle.Italic);
// Clear styles that are not available as a separate type face to prevent embedding of identical font files
#if GDI && !WPF
if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && !font.FontFamily.IsStyleAvailable(XFontStyle.Bold))
this.style &= ~XFontStyle.Bold;
if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && !font.FontFamily.IsStyleAvailable(XFontStyle.Italic))
this.style &= ~XFontStyle.Italic;
#endif
#if WPF && !GDI
Debug.Assert(font.typeface != null);
if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && font.typeface.IsBoldSimulated)
this.style &= ~XFontStyle.Bold;
if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && font.typeface.IsObliqueSimulated)
this.style &= ~XFontStyle.Italic;
#endif
#if WPF && GDI
Debug.Assert(font.typeface != null);
if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && font.typeface.IsBoldSimulated)
this.style &= ~XFontStyle.Bold;
if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && font.typeface.IsObliqueSimulated)
this.style &= ~XFontStyle.Italic;
#endif
this.fontType = font.Unicode ? FontType.Type0 : FontType.TrueType;
}
/// <summary>
/// Initializes a new instance of PdfFontSelector from a unique name.
/// </summary>
public FontSelector(string name)
{
this.name = name;
//// Ignore Strikeout and Underline
//this.style = font.Style & (XFontStyle.Bold | XFontStyle.Italic);
//// Clear styles that are not available to prevent embedding of identical font files
//if ((this.style & XFontStyle.Bold) == XFontStyle.Bold && !font.FontFamily.IsStyleAvailable(XFontStyle.Bold))
// this.style &= ~XFontStyle.Bold;
//if ((this.style & XFontStyle.Italic) == XFontStyle.Italic && !font.FontFamily.IsStyleAvailable(XFontStyle.Italic))
// this.style &= ~XFontStyle.Italic;
this.fontType = FontType.Type0;
}
public FontSelector(XFontFamily family, XFontStyle style)
{
throw new NotImplementedException("PdfFontSelector(XFontFamily family, XFontStyle style)");
}
/// <summary>
/// Gets the (generated) resource name of the font. In our own PDF files equal fonts share the
/// same resource name in all contents streams.
/// </summary>
public string Name
{
get { return this.name; }
}
string name;
/// <summary>
/// Gets the style. Contains only flags that effects the font face and are available for the specified font.
/// </summary>
/// <value>The style.</value>
public XFontStyle Style
{
get { return this.style; }
}
XFontStyle style;
/// <summary>
/// Gets the type of the font (TrueType with Ansi Encoding or CID font).
/// </summary>
public FontType FontType
{
get { return this.fontType; }
}
FontType fontType;
public static bool operator ==(FontSelector selector1, FontSelector selector2)
{
if (!Object.ReferenceEquals(selector1, null))
selector1.Equals(selector2);
return Object.ReferenceEquals(selector2, null);
}
public static bool operator !=(FontSelector selector1, FontSelector selector2)
{
return !(selector1 == selector2);
}
public override bool Equals(object obj)
{
FontSelector selector = obj as FontSelector;
if (obj != null && this.name == selector.name && this.style == selector.style)
return this.fontType == selector.fontType;
return false;
}
public override int GetHashCode()
{
return this.name.GetHashCode() ^ this.style.GetHashCode() ^ this.fontType.GetHashCode();
}
/// <summary>
/// Returns a string for diagnostic purposes only.
/// </summary>
public override string ToString()
{
string variation = "";
switch (this.style)
{
case XFontStyle.Regular:
variation = "(Regular)";
break;
case XFontStyle.Bold:
variation = "(Bold)";
break;
case XFontStyle.Italic:
variation = "(Italic)";
break;
case XFontStyle.Bold | XFontStyle.Italic:
variation = "(BoldItalic)";
break;
}
return this.name + variation;
}
}
}
}
|