1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkGlyph.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* 1.5 + * Copyright 2006 The Android Open Source Project 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkGlyph_DEFINED 1.12 +#define SkGlyph_DEFINED 1.13 + 1.14 +#include "SkTypes.h" 1.15 +#include "SkFixed.h" 1.16 +#include "SkMask.h" 1.17 + 1.18 +class SkPath; 1.19 + 1.20 +// needs to be != to any valid SkMask::Format 1.21 +#define MASK_FORMAT_UNKNOWN (0xFF) 1.22 +#define MASK_FORMAT_JUST_ADVANCE MASK_FORMAT_UNKNOWN 1.23 + 1.24 +#define kMaxGlyphWidth (1<<13) 1.25 + 1.26 +struct SkGlyph { 1.27 + void* fImage; 1.28 + SkPath* fPath; 1.29 + SkFixed fAdvanceX, fAdvanceY; 1.30 + 1.31 + uint32_t fID; 1.32 + uint16_t fWidth, fHeight; 1.33 + int16_t fTop, fLeft; 1.34 + 1.35 + uint8_t fMaskFormat; 1.36 + int8_t fRsbDelta, fLsbDelta; // used by auto-kerning 1.37 + 1.38 + void init(uint32_t id) { 1.39 + fID = id; 1.40 + fImage = NULL; 1.41 + fPath = NULL; 1.42 + fMaskFormat = MASK_FORMAT_UNKNOWN; 1.43 + } 1.44 + 1.45 + /** 1.46 + * Compute the rowbytes for the specified width and mask-format. 1.47 + */ 1.48 + static unsigned ComputeRowBytes(unsigned width, SkMask::Format format) { 1.49 + unsigned rb = width; 1.50 + if (SkMask::kBW_Format == format) { 1.51 + rb = (rb + 7) >> 3; 1.52 + } else if (SkMask::kARGB32_Format == format || 1.53 + SkMask::kLCD32_Format == format) 1.54 + { 1.55 + rb <<= 2; 1.56 + } else if (SkMask::kLCD16_Format == format) { 1.57 + rb = SkAlign4(rb << 1); 1.58 + } else { 1.59 + rb = SkAlign4(rb); 1.60 + } 1.61 + return rb; 1.62 + } 1.63 + 1.64 + unsigned rowBytes() const { 1.65 + return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat); 1.66 + } 1.67 + 1.68 + bool isJustAdvance() const { 1.69 + return MASK_FORMAT_JUST_ADVANCE == fMaskFormat; 1.70 + } 1.71 + 1.72 + bool isFullMetrics() const { 1.73 + return MASK_FORMAT_JUST_ADVANCE != fMaskFormat; 1.74 + } 1.75 + 1.76 + uint16_t getGlyphID() const { 1.77 + return ID2Code(fID); 1.78 + } 1.79 + 1.80 + unsigned getGlyphID(unsigned baseGlyphCount) const { 1.81 + unsigned code = ID2Code(fID); 1.82 + SkASSERT(code >= baseGlyphCount); 1.83 + return code - baseGlyphCount; 1.84 + } 1.85 + 1.86 + unsigned getSubX() const { 1.87 + return ID2SubX(fID); 1.88 + } 1.89 + 1.90 + SkFixed getSubXFixed() const { 1.91 + return SubToFixed(ID2SubX(fID)); 1.92 + } 1.93 + 1.94 + SkFixed getSubYFixed() const { 1.95 + return SubToFixed(ID2SubY(fID)); 1.96 + } 1.97 + 1.98 + size_t computeImageSize() const; 1.99 + 1.100 + /** Call this to set all of the metrics fields to 0 (e.g. if the scaler 1.101 + encounters an error measuring a glyph). Note: this does not alter the 1.102 + fImage, fPath, fID, fMaskFormat fields. 1.103 + */ 1.104 + void zeroMetrics(); 1.105 + 1.106 + enum { 1.107 + kSubBits = 2, 1.108 + kSubMask = ((1 << kSubBits) - 1), 1.109 + kSubShift = 24, // must be large enough for glyphs and unichars 1.110 + kCodeMask = ((1 << kSubShift) - 1), 1.111 + // relative offsets for X and Y subpixel bits 1.112 + kSubShiftX = kSubBits, 1.113 + kSubShiftY = 0 1.114 + }; 1.115 + 1.116 + static unsigned ID2Code(uint32_t id) { 1.117 + return id & kCodeMask; 1.118 + } 1.119 + 1.120 + static unsigned ID2SubX(uint32_t id) { 1.121 + return id >> (kSubShift + kSubShiftX); 1.122 + } 1.123 + 1.124 + static unsigned ID2SubY(uint32_t id) { 1.125 + return (id >> (kSubShift + kSubShiftY)) & kSubMask; 1.126 + } 1.127 + 1.128 + static unsigned FixedToSub(SkFixed n) { 1.129 + return (n >> (16 - kSubBits)) & kSubMask; 1.130 + } 1.131 + 1.132 + static SkFixed SubToFixed(unsigned sub) { 1.133 + SkASSERT(sub <= kSubMask); 1.134 + return sub << (16 - kSubBits); 1.135 + } 1.136 + 1.137 + static uint32_t MakeID(unsigned code) { 1.138 + return code; 1.139 + } 1.140 + 1.141 + static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) { 1.142 + SkASSERT(code <= kCodeMask); 1.143 + x = FixedToSub(x); 1.144 + y = FixedToSub(y); 1.145 + return (x << (kSubShift + kSubShiftX)) | 1.146 + (y << (kSubShift + kSubShiftY)) | 1.147 + code; 1.148 + } 1.149 + 1.150 + void toMask(SkMask* mask) const; 1.151 +}; 1.152 + 1.153 +#endif