• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SFNTLY_CPP_SRC_SFNTLY_DATA_FONT_DATA_H_
18 #define SFNTLY_CPP_SRC_SFNTLY_DATA_FONT_DATA_H_
19 
20 #include <limits.h>
21 
22 #include "sfntly/data/byte_array.h"
23 #include "sfntly/port/refcount.h"
24 #include "sfntly/port/type.h"
25 
26 namespace sfntly {
27 
28 struct DataSize {
29   enum {
30     kBYTE = 1,
31     kCHAR = 1,
32     kUSHORT = 2,
33     kSHORT = 2,
34     kUINT24 = 3,
35     kULONG = 4,
36     kLONG = 4,
37     kFixed = 4,
38     kFUNIT = 4,
39     kFWORD = 2,
40     kUFWORD = 2,
41     kF2DOT14 = 2,
42     kLONGDATETIME = 8,
43     kTag = 4,
44     kGlyphID = 2,
45     kOffset = 2
46   };
47 };
48 
49 class FontData : virtual public RefCount {
50  public:
51   // Gets the maximum size of the FontData. This is the maximum number of bytes
52   // that the font data can hold and all of it may not be filled with data or
53   // even fully allocated yet.
54   // @return the maximum size of this font data
55   virtual int32_t Size() const;
56 
57   // Sets limits on the size of the FontData. The FontData is then only
58   // visible within the bounds set.
59   // @param offset the start of the new bounds
60   // @param length the number of bytes in the bounded array
61   virtual void Bound(int32_t offset, int32_t length);
62 
63   // Makes a slice of this FontData. The returned slice will share the data with
64   // the original <code>FontData</code>.
65   // @param offset the start of the slice
66   // @param length the number of bytes in the slice
67   // @return a slice of the original FontData
68   virtual CALLER_ATTACH FontData* Slice(int32_t offset, int32_t length) = 0;
69 
70   // Makes a bottom bound only slice of this array. The returned slice will
71   // share the data with the original <code>FontData</code>.
72   // @param offset the start of the slice
73   // @return a slice of the original FontData
74   virtual CALLER_ATTACH FontData* Slice(int32_t offset) = 0;
75 
76   // Gets the length of the data.
77   virtual int32_t Length() const;
78 
79  protected:
80   // Constructor.
81   // @param ba the byte array to use for the backing data
82   explicit FontData(ByteArray* ba);
83 
84   // Constructor.
85   // @param data the data to wrap
86   // @param offset the offset to start the wrap from
87   // @param length the length of the data wrapped
88   FontData(FontData* data, int32_t offset, int32_t length);
89 
90   // Constructor.
91   // @param data the data to wrap
92   // @param offset the offset to start the wrap from
93   FontData(FontData* data, int32_t offset);
94   virtual ~FontData();
95 
96   void Init(ByteArray* ba);
97 
98   // Gets the offset in the underlying data taking into account any bounds on
99   // the data.
100   // @param offset the offset to get the bound compensated offset for
101   // @return the bound compensated offset
102   int32_t BoundOffset(int32_t offset);
103 
104   // Gets the length in the underlying data taking into account any bounds on
105   // the data.
106   // @param offset the offset that the length is being used at
107   // @param length the length to get the bound compensated length for
108   // @return the bound compensated length
109   int32_t BoundLength(int32_t offset, int32_t length);
110 
111   static const int32_t GROWABLE_SIZE = INT_MAX;
112 
113   // TODO(arthurhsu): style guide violation: refactor this protected member
114   ByteArrayPtr array_;
115 
116  private:
117   int32_t bound_offset_;
118   int32_t bound_length_;
119 };
120 typedef Ptr<FontData> FontDataPtr;
121 
122 }  // namespace sfntly
123 
124 #endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_FONT_DATA_H_
125