Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2006 The Android Open Source Project
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkGlyph_DEFINED
      9 #define SkGlyph_DEFINED
     10 
     11 #include "SkTypes.h"
     12 #include "SkFixed.h"
     13 #include "SkMask.h"
     14 
     15 class SkPath;
     16 
     17 // needs to be != to any valid SkMask::Format
     18 #define MASK_FORMAT_UNKNOWN         (0xFF)
     19 #define MASK_FORMAT_JUST_ADVANCE    MASK_FORMAT_UNKNOWN
     20 
     21 #define kMaxGlyphWidth (1<<13)
     22 
     23 struct SkGlyph {
     24     void*       fImage;
     25     SkPath*     fPath;
     26     SkFixed     fAdvanceX, fAdvanceY;
     27 
     28     uint32_t    fID;
     29     uint16_t    fWidth, fHeight;
     30     int16_t     fTop, fLeft;
     31 
     32     void*       fDistanceField;
     33     uint8_t     fMaskFormat;
     34     int8_t      fRsbDelta, fLsbDelta;  // used by auto-kerning
     35     int8_t      fForceBW;
     36 
     37     void init(uint32_t id) {
     38         fID             = id;
     39         fImage          = NULL;
     40         fPath           = NULL;
     41         fDistanceField  = NULL;
     42         fMaskFormat     = MASK_FORMAT_UNKNOWN;
     43         fForceBW        = 0;
     44     }
     45 
     46     /**
     47      *  Compute the rowbytes for the specified width and mask-format.
     48      */
     49     static unsigned ComputeRowBytes(unsigned width, SkMask::Format format) {
     50         unsigned rb = width;
     51         if (SkMask::kBW_Format == format) {
     52             rb = (rb + 7) >> 3;
     53         } else if (SkMask::kARGB32_Format == format ||
     54                    SkMask::kLCD32_Format == format)
     55         {
     56             rb <<= 2;
     57         } else if (SkMask::kLCD16_Format == format) {
     58             rb = SkAlign4(rb << 1);
     59         } else {
     60             rb = SkAlign4(rb);
     61         }
     62         return rb;
     63     }
     64 
     65     unsigned rowBytes() const {
     66         return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat);
     67     }
     68 
     69     bool isJustAdvance() const {
     70         return MASK_FORMAT_JUST_ADVANCE == fMaskFormat;
     71     }
     72 
     73     bool isFullMetrics() const {
     74         return MASK_FORMAT_JUST_ADVANCE != fMaskFormat;
     75     }
     76 
     77     uint16_t getGlyphID() const {
     78         return ID2Code(fID);
     79     }
     80 
     81     unsigned getSubX() const {
     82         return ID2SubX(fID);
     83     }
     84 
     85     SkFixed getSubXFixed() const {
     86         return SubToFixed(ID2SubX(fID));
     87     }
     88 
     89     SkFixed getSubYFixed() const {
     90         return SubToFixed(ID2SubY(fID));
     91     }
     92 
     93     size_t computeImageSize() const;
     94 
     95     /** Call this to set all of the metrics fields to 0 (e.g. if the scaler
     96         encounters an error measuring a glyph). Note: this does not alter the
     97         fImage, fPath, fID, fMaskFormat fields.
     98      */
     99     void zeroMetrics();
    100 
    101     enum {
    102         kSubBits = 2,
    103         kSubMask = ((1 << kSubBits) - 1),
    104         kSubShift = 24, // must be large enough for glyphs and unichars
    105         kCodeMask = ((1 << kSubShift) - 1),
    106         // relative offsets for X and Y subpixel bits
    107         kSubShiftX = kSubBits,
    108         kSubShiftY = 0
    109     };
    110 
    111     static unsigned ID2Code(uint32_t id) {
    112         return id & kCodeMask;
    113     }
    114 
    115     static unsigned ID2SubX(uint32_t id) {
    116         return id >> (kSubShift + kSubShiftX);
    117     }
    118 
    119     static unsigned ID2SubY(uint32_t id) {
    120         return (id >> (kSubShift + kSubShiftY)) & kSubMask;
    121     }
    122 
    123     static unsigned FixedToSub(SkFixed n) {
    124         return (n >> (16 - kSubBits)) & kSubMask;
    125     }
    126 
    127     static SkFixed SubToFixed(unsigned sub) {
    128         SkASSERT(sub <= kSubMask);
    129         return sub << (16 - kSubBits);
    130     }
    131 
    132     static uint32_t MakeID(unsigned code) {
    133         return code;
    134     }
    135 
    136     static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) {
    137         SkASSERT(code <= kCodeMask);
    138         x = FixedToSub(x);
    139         y = FixedToSub(y);
    140         return (x << (kSubShift + kSubShiftX)) |
    141                (y << (kSubShift + kSubShiftY)) |
    142                code;
    143     }
    144 
    145     void toMask(SkMask* mask) const;
    146 };
    147 
    148 #endif
    149