Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      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 SkAdvancedTypefaceMetrics_DEFINED
     18 #define SkAdvancedTypefaceMetrics_DEFINED
     19 
     20 #include "SkRect.h"
     21 #include "SkRefCnt.h"
     22 #include "SkString.h"
     23 #include "SkTDArray.h"
     24 #include "SkTemplates.h"
     25 #include "SkTScopedPtr.h"
     26 
     27 /** \class SkAdvancedTypefaceMetrics
     28 
     29     The SkAdvancedTypefaceMetrics class is used by the PDF backend to correctly
     30     embed typefaces.  This class is filled in with information about a given
     31     typeface by the SkFontHost class.
     32 */
     33 
     34 class SkAdvancedTypefaceMetrics : public SkRefCnt {
     35 public:
     36     SkString fFontName;
     37 
     38     enum FontType {
     39         kType1_Font,
     40         kType1CID_Font,
     41         kCFF_Font,
     42         kTrueType_Font,
     43         kOther_Font,
     44         kNotEmbeddable_Font,
     45     };
     46     // The type of the underlying font program.  This field determines which
     47     // of the following fields are valid.  If it is kOther_Font or
     48     // kNotEmbeddable_Font, the per glyph information will never be populated.
     49     FontType fType;
     50 
     51     // fMultiMaster may be true for Type1_Font or CFF_Font.
     52     bool fMultiMaster;
     53     uint16_t fLastGlyphID; // The last valid glyph ID in the font.
     54     uint16_t fEmSize;  // The size of the em box (defines font units).
     55 
     56     // These enum values match the values used in the PDF file format.
     57     enum StyleFlags {
     58         kFixedPitch_Style  = 0x00001,
     59         kSerif_Style       = 0x00002,
     60         kSymbolic_Style    = 0x00004,
     61         kScript_Style      = 0x00008,
     62         kNonsymbolic_Style = 0x00020,
     63         kItalic_Style      = 0x00040,
     64         kAllCaps_Style     = 0x10000,
     65         kSmallCaps_Style   = 0x20000,
     66         kForceBold_Style   = 0x40000,
     67     };
     68     uint16_t fStyle;        // Font style characteristics.
     69     int16_t fItalicAngle;   // Counterclockwise degrees from vertical of the
     70                             // dominant vertical stroke for an Italic face.
     71     // The following fields are all in font units.
     72     int16_t fAscent;       // Max height above baseline, not including accents.
     73     int16_t fDescent;      // Max depth below baseline (negative).
     74     int16_t fStemV;        // Thickness of dominant vertical stem.
     75     int16_t fCapHeight;    // Height (from baseline) of top of flat capitals.
     76 
     77     SkIRect fBBox;  // The bounding box of all glyphs (in font units).
     78 
     79     // The type of advance data wanted.
     80     enum PerGlyphInfo {
     81       kNo_PerGlyphInfo         = 0x0, // Don't populate any per glyph info.
     82       kHAdvance_PerGlyphInfo   = 0x1, // Populate horizontal advance data.
     83       kVAdvance_PerGlyphInfo   = 0x2, // Populate vertical advance data.
     84       kGlyphNames_PerGlyphInfo = 0x4, // Populate glyph names (Type 1 only).
     85       kToUnicode_PerGlyphInfo  = 0x8, // Populate ToUnicode table, ignored
     86                                       // for Type 1 fonts
     87     };
     88 
     89     template <typename Data>
     90     struct AdvanceMetric {
     91         enum MetricType {
     92             kDefault,  // Default advance: fAdvance.count = 1
     93             kRange,    // Advances for a range: fAdvance.count = fEndID-fStartID
     94             kRun,      // fStartID-fEndID have same advance: fAdvance.count = 1
     95         };
     96         MetricType fType;
     97         uint16_t fStartId;
     98         uint16_t fEndId;
     99         SkTDArray<Data> fAdvance;
    100         SkTScopedPtr<AdvanceMetric<Data> > fNext;
    101     };
    102 
    103     struct VerticalMetric {
    104         int16_t fVerticalAdvance;
    105         int16_t fOriginXDisp;  // Horiz. displacement of the secondary origin.
    106         int16_t fOriginYDisp;  // Vert. displacement of the secondary origin.
    107     };
    108     typedef AdvanceMetric<int16_t> WidthRange;
    109     typedef AdvanceMetric<VerticalMetric> VerticalAdvanceRange;
    110 
    111     // This is indexed by glyph id.
    112     SkTScopedPtr<WidthRange> fGlyphWidths;
    113     // Only used for Vertical CID fonts.
    114     SkTScopedPtr<VerticalAdvanceRange> fVerticalMetrics;
    115 
    116     // The names of each glyph, only populated for postscript fonts.
    117     SkTScopedPtr<SkAutoTArray<SkString> > fGlyphNames;
    118 
    119     // The mapping from glyph to Unicode, only populated if
    120     // kToUnicode_PerGlyphInfo is passed to GetAdvancedTypefaceMetrics.
    121     SkTDArray<SkUnichar> fGlyphToUnicode;
    122 };
    123 
    124 namespace skia_advanced_typeface_metrics_utils {
    125 
    126 template <typename Data>
    127 void resetRange(SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
    128                        int startId);
    129 
    130 template <typename Data>
    131 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* appendRange(
    132         SkTScopedPtr<SkAdvancedTypefaceMetrics::AdvanceMetric<Data> >* nextSlot,
    133         int startId);
    134 
    135 template <typename Data>
    136 void finishRange(
    137         SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* range,
    138         int endId,
    139         typename SkAdvancedTypefaceMetrics::AdvanceMetric<Data>::MetricType
    140                 type);
    141 
    142 template <typename Data, typename FontHandle>
    143 SkAdvancedTypefaceMetrics::AdvanceMetric<Data>* getAdvanceData(
    144         FontHandle fontHandle,
    145         int num_glyphs,
    146         bool (*getAdvance)(FontHandle fontHandle, int gId, Data* data));
    147 
    148 } // namespace skia_advanced_typeface_metrics_utils
    149 
    150 #endif
    151