michael@0: michael@0: /* michael@0: * Copyright 2010 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "GrTemplates.h" michael@0: #include "SkGr.h" michael@0: #include "SkDescriptor.h" michael@0: #include "SkGlyphCache.h" michael@0: michael@0: class SkGrDescKey : public GrKey { michael@0: public: michael@0: explicit SkGrDescKey(const SkDescriptor& desc); michael@0: virtual ~SkGrDescKey(); michael@0: michael@0: protected: michael@0: // overrides michael@0: virtual bool lt(const GrKey& rh) const; michael@0: virtual bool eq(const GrKey& rh) const; michael@0: michael@0: private: michael@0: SkDescriptor* fDesc; michael@0: enum { michael@0: kMaxStorageInts = 16 michael@0: }; michael@0: uint32_t fStorage[kMaxStorageInts]; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkGrDescKey::SkGrDescKey(const SkDescriptor& desc) : GrKey(desc.getChecksum()) { michael@0: size_t size = desc.getLength(); michael@0: if (size <= sizeof(fStorage)) { michael@0: fDesc = GrTCast(fStorage); michael@0: } else { michael@0: fDesc = SkDescriptor::Alloc(size); michael@0: } michael@0: memcpy(fDesc, &desc, size); michael@0: } michael@0: michael@0: SkGrDescKey::~SkGrDescKey() { michael@0: if (fDesc != GrTCast(fStorage)) { michael@0: SkDescriptor::Free(fDesc); michael@0: } michael@0: } michael@0: michael@0: bool SkGrDescKey::lt(const GrKey& rh) const { michael@0: const SkDescriptor* srcDesc = ((const SkGrDescKey*)&rh)->fDesc; michael@0: size_t lenLH = fDesc->getLength(); michael@0: size_t lenRH = srcDesc->getLength(); michael@0: int cmp = memcmp(fDesc, srcDesc, SkTMin(lenLH, lenRH)); michael@0: if (0 == cmp) { michael@0: return lenLH < lenRH; michael@0: } else { michael@0: return cmp < 0; michael@0: } michael@0: } michael@0: michael@0: bool SkGrDescKey::eq(const GrKey& rh) const { michael@0: const SkDescriptor* srcDesc = ((const SkGrDescKey*)&rh)->fDesc; michael@0: return fDesc->equals(*srcDesc); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkGrFontScaler::SkGrFontScaler(SkGlyphCache* strike) { michael@0: fStrike = strike; michael@0: fKey = NULL; michael@0: } michael@0: michael@0: SkGrFontScaler::~SkGrFontScaler() { michael@0: SkSafeUnref(fKey); michael@0: } michael@0: michael@0: GrMaskFormat SkGrFontScaler::getMaskFormat() { michael@0: SkMask::Format format = fStrike->getMaskFormat(); michael@0: switch (format) { michael@0: case SkMask::kBW_Format: michael@0: // fall through to kA8 -- we store BW glyphs in our 8-bit cache michael@0: case SkMask::kA8_Format: michael@0: return kA8_GrMaskFormat; michael@0: case SkMask::kLCD16_Format: michael@0: return kA565_GrMaskFormat; michael@0: case SkMask::kLCD32_Format: michael@0: return kA888_GrMaskFormat; michael@0: case SkMask::kARGB32_Format: michael@0: return kARGB_GrMaskFormat; michael@0: default: michael@0: SkDEBUGFAIL("unsupported SkMask::Format"); michael@0: return kA8_GrMaskFormat; michael@0: } michael@0: } michael@0: michael@0: const GrKey* SkGrFontScaler::getKey() { michael@0: if (NULL == fKey) { michael@0: fKey = SkNEW_ARGS(SkGrDescKey, (fStrike->getDescriptor())); michael@0: } michael@0: return fKey; michael@0: } michael@0: michael@0: bool SkGrFontScaler::getPackedGlyphBounds(GrGlyph::PackedID packed, michael@0: SkIRect* bounds) { michael@0: const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), michael@0: GrGlyph::UnpackFixedX(packed), michael@0: GrGlyph::UnpackFixedY(packed)); michael@0: bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight); michael@0: return true; michael@0: michael@0: } michael@0: michael@0: namespace { michael@0: // expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to michael@0: // A8, RGB565, or RGBA8888. michael@0: template michael@0: void expand_bits(INT_TYPE* dst, michael@0: const uint8_t* src, michael@0: int width, michael@0: int height, michael@0: int dstRowBytes, michael@0: int srcRowBytes) { michael@0: for (int i = 0; i < height; ++i) { michael@0: int rowWritesLeft = width; michael@0: const uint8_t* s = src; michael@0: INT_TYPE* d = dst; michael@0: while (rowWritesLeft > 0) { michael@0: unsigned mask = *s++; michael@0: for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { michael@0: *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0; michael@0: } michael@0: } michael@0: dst = reinterpret_cast(reinterpret_cast(dst) + dstRowBytes); michael@0: src += srcRowBytes; michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkGrFontScaler::getPackedGlyphImage(GrGlyph::PackedID packed, michael@0: int width, int height, michael@0: int dstRB, void* dst) { michael@0: const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), michael@0: GrGlyph::UnpackFixedX(packed), michael@0: GrGlyph::UnpackFixedY(packed)); michael@0: SkASSERT(glyph.fWidth == width); michael@0: SkASSERT(glyph.fHeight == height); michael@0: const void* src = fStrike->findImage(glyph); michael@0: if (NULL == src) { michael@0: return false; michael@0: } michael@0: michael@0: int srcRB = glyph.rowBytes(); michael@0: // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to michael@0: // check the glyph's format, not the strike's format, and to be able to convert to any of the michael@0: // GrMaskFormats. michael@0: if (SkMask::kBW_Format == glyph.fMaskFormat) { michael@0: // expand bits to our mask type michael@0: const uint8_t* bits = reinterpret_cast(src); michael@0: switch (this->getMaskFormat()) { michael@0: case kA8_GrMaskFormat:{ michael@0: uint8_t* bytes = reinterpret_cast(dst); michael@0: expand_bits(bytes, bits, width, height, dstRB, srcRB); michael@0: break; michael@0: } michael@0: case kA565_GrMaskFormat: { michael@0: uint16_t* rgb565 = reinterpret_cast(dst); michael@0: expand_bits(rgb565, bits, width, height, dstRB, srcRB); michael@0: break; michael@0: } michael@0: case kA888_GrMaskFormat: { michael@0: uint32_t* rgba8888 = reinterpret_cast(dst); michael@0: expand_bits(rgba8888, bits, width, height, dstRB, srcRB); michael@0: break; michael@0: } michael@0: default: michael@0: GrCrash("Invalid GrMaskFormat"); michael@0: } michael@0: } else if (srcRB == dstRB) { michael@0: memcpy(dst, src, dstRB * height); michael@0: } else { michael@0: const int bbp = GrMaskFormatBytesPerPixel(this->getMaskFormat()); michael@0: for (int y = 0; y < height; y++) { michael@0: memcpy(dst, src, width * bbp); michael@0: src = (const char*)src + srcRB; michael@0: dst = (char*)dst + dstRB; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // we should just return const SkPath* (NULL means false) michael@0: bool SkGrFontScaler::getGlyphPath(uint16_t glyphID, SkPath* path) { michael@0: michael@0: const SkGlyph& glyph = fStrike->getGlyphIDMetrics(glyphID); michael@0: const SkPath* skPath = fStrike->findPath(glyph); michael@0: if (skPath) { michael@0: *path = *skPath; michael@0: return true; michael@0: } michael@0: return false; michael@0: }