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

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Fonts.TrueType/TrueTypeFontTable.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.Runtime.InteropServices;
 
33
using PdfSharp.Drawing;
 
34
using PdfSharp.Internal;
 
35
 
 
36
using Fixed = System.Int32;
 
37
using FWord = System.Int16;
 
38
using UFWord = System.UInt16;
 
39
 
 
40
namespace PdfSharp.Fonts.TrueType
 
41
{
 
42
  /// <summary>
 
43
  /// Base class for all TrueType fonts.
 
44
  /// </summary>
 
45
  internal class TrueTypeFontTable : ICloneable
 
46
  {
 
47
    ///// <summary>
 
48
    ///// Initializes a new instance of the <see cref="TrueTypeFontTable"/> class.
 
49
    ///// </summary>
 
50
    //public TrueTypeFontTable(FontData fontData)
 
51
    //{
 
52
    //  this.fontData = fontData;
 
53
    //}
 
54
 
 
55
    public TrueTypeFontTable(FontData fontData, string tag)
 
56
    {
 
57
      this.fontData = fontData;
 
58
      if (fontData != null && fontData.tableDictionary.ContainsKey(tag))
 
59
        this.DirectoryEntry = fontData.tableDictionary[tag];
 
60
      else
 
61
        this.DirectoryEntry = new TableDirectoryEntry(tag);
 
62
      this.DirectoryEntry.FontTable = this;
 
63
    }
 
64
 
 
65
    /// <summary>
 
66
    /// Creates a deep copy of the current instance.
 
67
    /// </summary>
 
68
    public object Clone()
 
69
    {
 
70
      return DeepCopy();
 
71
    }
 
72
 
 
73
    protected virtual TrueTypeFontTable DeepCopy()
 
74
    {
 
75
      TrueTypeFontTable fontTable = (TrueTypeFontTable)MemberwiseClone();
 
76
      fontTable.DirectoryEntry.Offset = 0;
 
77
      fontTable.DirectoryEntry.FontTable = fontTable;
 
78
      return fontTable;
 
79
    }
 
80
 
 
81
    /// <summary>
 
82
    /// Gets the font image the table belongs to.
 
83
    /// </summary>
 
84
    public FontData FontData
 
85
    {
 
86
      get { return this.fontData; }
 
87
    }
 
88
    internal FontData fontData;
 
89
 
 
90
    public TableDirectoryEntry DirectoryEntry;
 
91
 
 
92
    /// <summary>
 
93
    /// When overridden in a derived class, prepares the font table to be compiled into its binary representation.
 
94
    /// </summary>
 
95
    public virtual void PrepareForCompilation()
 
96
    {
 
97
    }
 
98
 
 
99
    /// <summary>
 
100
    /// When overridden in a derived class, converts the font into its binary representation.
 
101
    /// </summary>
 
102
    public virtual void Write(TrueTypeFontWriter writer)
 
103
    {
 
104
    }
 
105
 
 
106
    /// <summary>
 
107
    /// Calculates the checksum of a table represented by its bytes.
 
108
    /// </summary>
 
109
    public static uint CalcChecksum(byte[] bytes)
 
110
    {
 
111
      Debug.Assert((bytes.Length & 3) == 0);
 
112
      // Cannot use Buffer.BlockCopy because 32-bit values are Big-endian
 
113
      uint byte3, byte2, byte1, byte0;
 
114
      byte3 = byte2 = byte1 = byte0 = 0;
 
115
      int length = bytes.Length;
 
116
      for (int idx = 0; idx < length;)
 
117
      {
 
118
        byte3 += bytes[idx++];
 
119
        byte2 += bytes[idx++];
 
120
        byte1 += bytes[idx++];
 
121
        byte0 += bytes[idx++];
 
122
      }
 
123
      return (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0;
 
124
    }
 
125
  }
 
126
}