Home | History | Annotate | Download | only in data
      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 <vector>
     23 
     24 #include "sfntly/port/type.h"
     25 #include "sfntly/data/byte_array.h"
     26 #include "sfntly/port/refcount.h"
     27 
     28 namespace sfntly {
     29 
     30 struct DataSize {
     31   enum {
     32     kBYTE = 1,
     33     kCHAR = 1,
     34     kUSHORT = 2,
     35     kSHORT = 2,
     36     kUINT24 = 3,
     37     kULONG = 4,
     38     kLONG = 4,
     39     kFixed = 4,
     40     kFUNIT = 4,
     41     kFWORD = 2,
     42     kUFWORD = 2,
     43     kF2DOT14 = 2,
     44     kLONGDATETIME = 8,
     45     kTag = 4,
     46     kGlyphID = 2,
     47     kOffset = 2
     48   };
     49 };
     50 
     51 class FontData : virtual public RefCount {
     52  public:
     53   // Gets the maximum size of the FontData. This is the maximum number of bytes
     54   // that the font data can hold and all of it may not be filled with data or
     55   // even fully allocated yet.
     56   // @return the maximum size of this font data
     57   virtual int32_t Size() const;
     58 
     59   // Sets limits on the size of the FontData. The FontData is then only
     60   // visible within the bounds set.
     61   // @param offset the start of the new bounds
     62   // @param length the number of bytes in the bounded array
     63   // @return true if the bounding range was successful; false otherwise
     64   virtual bool Bound(int32_t offset, int32_t length);
     65 
     66   // Sets limits on the size of the FontData. This is a offset bound only so if
     67   // the FontData is writable and growable then there is no limit to that growth
     68   // from the bounding operation.
     69   // @param offset the start of the new bounds which must be within the current
     70   //        size of the FontData
     71   // @return true if the bounding range was successful; false otherwise
     72   virtual bool Bound(int32_t offset);
     73 
     74   // Makes a slice of this FontData. The returned slice will share the data with
     75   // the original <code>FontData</code>.
     76   // @param offset the start of the slice
     77   // @param length the number of bytes in the slice
     78   // @return a slice of the original FontData
     79   virtual CALLER_ATTACH FontData* Slice(int32_t offset, int32_t length) = 0;
     80 
     81   // Makes a bottom bound only slice of this array. The returned slice will
     82   // share the data with the original <code>FontData</code>.
     83   // @param offset the start of the slice
     84   // @return a slice of the original FontData
     85   virtual CALLER_ATTACH FontData* Slice(int32_t offset) = 0;
     86 
     87   // Gets the length of the data.
     88   virtual int32_t Length() const;
     89 
     90  protected:
     91   // Constructor.
     92   // @param ba the byte array to use for the backing data
     93   explicit FontData(ByteArray* ba);
     94 
     95   // Constructor.
     96   // @param data the data to wrap
     97   // @param offset the offset to start the wrap from
     98   // @param length the length of the data wrapped
     99   FontData(FontData* data, int32_t offset, int32_t length);
    100 
    101   // Constructor.
    102   // @param data the data to wrap
    103   // @param offset the offset to start the wrap from
    104   FontData(FontData* data, int32_t offset);
    105   virtual ~FontData();
    106 
    107   void Init(ByteArray* ba);
    108 
    109   // Gets the offset in the underlying data taking into account any bounds on
    110   // the data.
    111   // @param offset the offset to get the bound compensated offset for
    112   // @return the bound compensated offset
    113   int32_t BoundOffset(int32_t offset);
    114 
    115   // Gets the length in the underlying data taking into account any bounds on
    116   // the data.
    117   // @param offset the offset that the length is being used at
    118   // @param length the length to get the bound compensated length for
    119   // @return the bound compensated length
    120   int32_t BoundLength(int32_t offset, int32_t length);
    121 
    122   static const int32_t GROWABLE_SIZE = INT_MAX;
    123 
    124   // TODO(arthurhsu): style guide violation: refactor this protected member
    125   ByteArrayPtr array_;
    126 
    127  private:
    128   int32_t bound_offset_;
    129   int32_t bound_length_;
    130 };
    131 typedef Ptr<FontData> FontDataPtr;
    132 
    133 }  // namespace sfntly
    134 
    135 #endif  // SFNTLY_CPP_SRC_SFNTLY_DATA_FONT_DATA_H_
    136