michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project 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 "SkScalerContext.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkDescriptor.h" michael@0: #include "SkDraw.h" michael@0: #include "SkFontHost.h" michael@0: #include "SkGlyph.h" michael@0: #include "SkMaskFilter.h" michael@0: #include "SkMaskGamma.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkPathEffect.h" michael@0: #include "SkRasterizer.h" michael@0: #include "SkRasterClip.h" michael@0: #include "SkStroke.h" michael@0: #include "SkThread.h" michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: #include "SkTypeface_android.h" michael@0: #endif michael@0: michael@0: #define ComputeBWRowBytes(width) (((unsigned)(width) + 7) >> 3) michael@0: michael@0: void SkGlyph::toMask(SkMask* mask) const { michael@0: SkASSERT(mask); michael@0: michael@0: mask->fImage = (uint8_t*)fImage; michael@0: mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight); michael@0: mask->fRowBytes = this->rowBytes(); michael@0: mask->fFormat = static_cast(fMaskFormat); michael@0: } michael@0: michael@0: size_t SkGlyph::computeImageSize() const { michael@0: const size_t size = this->rowBytes() * fHeight; michael@0: michael@0: switch (fMaskFormat) { michael@0: case SkMask::k3D_Format: michael@0: return 3 * size; michael@0: default: michael@0: return size; michael@0: } michael@0: } michael@0: michael@0: void SkGlyph::zeroMetrics() { michael@0: fAdvanceX = 0; michael@0: fAdvanceY = 0; michael@0: fWidth = 0; michael@0: fHeight = 0; michael@0: fTop = 0; michael@0: fLeft = 0; michael@0: fRsbDelta = 0; michael@0: fLsbDelta = 0; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: #define DUMP_RECx michael@0: #endif michael@0: michael@0: static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag, michael@0: SkFlattenable::Type ft) { michael@0: SkFlattenable* obj = NULL; michael@0: uint32_t len; michael@0: const void* data = desc->findEntry(tag, &len); michael@0: michael@0: if (data) { michael@0: SkReadBuffer buffer(data, len); michael@0: obj = buffer.readFlattenable(ft); michael@0: SkASSERT(buffer.offset() == buffer.size()); michael@0: } michael@0: return obj; michael@0: } michael@0: michael@0: SkScalerContext::SkScalerContext(SkTypeface* typeface, const SkDescriptor* desc) michael@0: : fRec(*static_cast(desc->findEntry(kRec_SkDescriptorTag, NULL))) michael@0: michael@0: , fBaseGlyphCount(0) michael@0: , fTypeface(SkRef(typeface)) michael@0: , fPathEffect(static_cast(load_flattenable(desc, kPathEffect_SkDescriptorTag, michael@0: SkFlattenable::kSkPathEffect_Type))) michael@0: , fMaskFilter(static_cast(load_flattenable(desc, kMaskFilter_SkDescriptorTag, michael@0: SkFlattenable::kSkMaskFilter_Type))) michael@0: , fRasterizer(static_cast(load_flattenable(desc, kRasterizer_SkDescriptorTag, michael@0: SkFlattenable::kSkRasterizer_Type))) michael@0: // Initialize based on our settings. Subclasses can also force this. michael@0: , fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != NULL || fRasterizer != NULL) michael@0: michael@0: , fNextContext(NULL) michael@0: michael@0: , fPreBlend(fMaskFilter ? SkMaskGamma::PreBlend() : SkScalerContext::GetMaskPreBlend(fRec)) michael@0: , fPreBlendForFilter(fMaskFilter ? SkScalerContext::GetMaskPreBlend(fRec) michael@0: : SkMaskGamma::PreBlend()) michael@0: { michael@0: #ifdef DUMP_REC michael@0: desc->assertChecksum(); michael@0: SkDebugf("SkScalerContext checksum %x count %d length %d\n", michael@0: desc->getChecksum(), desc->getCount(), desc->getLength()); michael@0: SkDebugf(" textsize %g prescale %g preskew %g post [%g %g %g %g]\n", michael@0: rec->fTextSize, rec->fPreScaleX, rec->fPreSkewX, rec->fPost2x2[0][0], michael@0: rec->fPost2x2[0][1], rec->fPost2x2[1][0], rec->fPost2x2[1][1]); michael@0: SkDebugf(" frame %g miter %g hints %d framefill %d format %d join %d\n", michael@0: rec->fFrameWidth, rec->fMiterLimit, rec->fHints, rec->fFrameAndFill, michael@0: rec->fMaskFormat, rec->fStrokeJoin); michael@0: SkDebugf(" pathEffect %x maskFilter %x\n", michael@0: desc->findEntry(kPathEffect_SkDescriptorTag, NULL), michael@0: desc->findEntry(kMaskFilter_SkDescriptorTag, NULL)); michael@0: #endif michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: uint32_t len; michael@0: const void* data = desc->findEntry(kAndroidOpts_SkDescriptorTag, &len); michael@0: if (data) { michael@0: SkReadBuffer buffer(data, len); michael@0: fPaintOptionsAndroid.unflatten(buffer); michael@0: SkASSERT(buffer.offset() == buffer.size()); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: SkScalerContext::~SkScalerContext() { michael@0: SkDELETE(fNextContext); michael@0: michael@0: SkSafeUnref(fPathEffect); michael@0: SkSafeUnref(fMaskFilter); michael@0: SkSafeUnref(fRasterizer); michael@0: } michael@0: michael@0: // Return the context associated with the next logical typeface, or NULL if michael@0: // there are no more entries in the fallback chain. michael@0: SkScalerContext* SkScalerContext::allocNextContext() const { michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: SkTypeface* newFace = SkAndroidNextLogicalTypeface(fRec.fFontID, michael@0: fRec.fOrigFontID, michael@0: fPaintOptionsAndroid); michael@0: if (0 == newFace) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkAutoTUnref aur(newFace); michael@0: uint32_t newFontID = newFace->uniqueID(); michael@0: michael@0: SkWriteBuffer androidBuffer; michael@0: fPaintOptionsAndroid.flatten(androidBuffer); michael@0: michael@0: SkAutoDescriptor ad(sizeof(fRec) + androidBuffer.bytesWritten() michael@0: + SkDescriptor::ComputeOverhead(2)); michael@0: SkDescriptor* desc = ad.getDesc(); michael@0: michael@0: desc->init(); michael@0: SkScalerContext::Rec* newRec = michael@0: (SkScalerContext::Rec*)desc->addEntry(kRec_SkDescriptorTag, michael@0: sizeof(fRec), &fRec); michael@0: androidBuffer.writeToMemory(desc->addEntry(kAndroidOpts_SkDescriptorTag, michael@0: androidBuffer.bytesWritten(), NULL)); michael@0: michael@0: newRec->fFontID = newFontID; michael@0: desc->computeChecksum(); michael@0: michael@0: return newFace->createScalerContext(desc); michael@0: #else michael@0: return NULL; michael@0: #endif michael@0: } michael@0: michael@0: /* Return the next context, creating it if its not already created, but return michael@0: NULL if the fonthost says there are no more fonts to fallback to. michael@0: */ michael@0: SkScalerContext* SkScalerContext::getNextContext() { michael@0: SkScalerContext* next = fNextContext; michael@0: // if next is null, then either it isn't cached yet, or we're at the michael@0: // end of our possible chain michael@0: if (NULL == next) { michael@0: next = this->allocNextContext(); michael@0: if (NULL == next) { michael@0: return NULL; michael@0: } michael@0: // next's base is our base + our local count michael@0: next->setBaseGlyphCount(fBaseGlyphCount + this->getGlyphCount()); michael@0: // cache the answer michael@0: fNextContext = next; michael@0: } michael@0: return next; michael@0: } michael@0: michael@0: SkScalerContext* SkScalerContext::getGlyphContext(const SkGlyph& glyph) { michael@0: unsigned glyphID = glyph.getGlyphID(); michael@0: SkScalerContext* ctx = this; michael@0: for (;;) { michael@0: unsigned count = ctx->getGlyphCount(); michael@0: if (glyphID < count) { michael@0: break; michael@0: } michael@0: glyphID -= count; michael@0: ctx = ctx->getNextContext(); michael@0: if (NULL == ctx) { michael@0: // SkDebugf("--- no context for glyph %x\n", glyph.getGlyphID()); michael@0: // just return the original context (this) michael@0: return this; michael@0: } michael@0: } michael@0: return ctx; michael@0: } michael@0: michael@0: SkScalerContext* SkScalerContext::getContextFromChar(SkUnichar uni, michael@0: uint16_t* glyphID) { michael@0: SkScalerContext* ctx = this; michael@0: for (;;) { michael@0: const uint16_t glyph = ctx->generateCharToGlyph(uni); michael@0: if (glyph) { michael@0: if (NULL != glyphID) { michael@0: *glyphID = glyph; michael@0: } michael@0: break; // found it michael@0: } michael@0: ctx = ctx->getNextContext(); michael@0: if (NULL == ctx) { michael@0: return NULL; michael@0: } michael@0: } michael@0: return ctx; michael@0: } michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: SkFontID SkScalerContext::findTypefaceIdForChar(SkUnichar uni) { michael@0: SkScalerContext* ctx = this->getContextFromChar(uni, NULL); michael@0: if (NULL != ctx) { michael@0: return ctx->fRec.fFontID; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* This loops through all available fallback contexts (if needed) until it michael@0: finds some context that can handle the unichar and return it. michael@0: michael@0: As this is somewhat expensive operation, it should only be done on the first michael@0: char of a run. michael@0: */ michael@0: unsigned SkScalerContext::getBaseGlyphCount(SkUnichar uni) { michael@0: SkScalerContext* ctx = this->getContextFromChar(uni, NULL); michael@0: if (NULL != ctx) { michael@0: return ctx->fBaseGlyphCount; michael@0: } else { michael@0: SkDEBUGF(("--- no context for char %x\n", uni)); michael@0: return this->fBaseGlyphCount; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* This loops through all available fallback contexts (if needed) until it michael@0: finds some context that can handle the unichar. If all fail, returns 0 michael@0: */ michael@0: uint16_t SkScalerContext::charToGlyphID(SkUnichar uni) { michael@0: michael@0: uint16_t tempID; michael@0: SkScalerContext* ctx = this->getContextFromChar(uni, &tempID); michael@0: if (NULL == ctx) { michael@0: return 0; // no more contexts, return missing glyph michael@0: } michael@0: // add the ctx's base, making glyphID unique for chain of contexts michael@0: unsigned glyphID = tempID + ctx->fBaseGlyphCount; michael@0: // check for overflow of 16bits, since our glyphID cannot exceed that michael@0: if (glyphID > 0xFFFF) { michael@0: glyphID = 0; michael@0: } michael@0: return SkToU16(glyphID); michael@0: } michael@0: michael@0: SkUnichar SkScalerContext::glyphIDToChar(uint16_t glyphID) { michael@0: SkScalerContext* ctx = this; michael@0: unsigned rangeEnd = 0; michael@0: do { michael@0: unsigned rangeStart = rangeEnd; michael@0: michael@0: rangeEnd += ctx->getGlyphCount(); michael@0: if (rangeStart <= glyphID && glyphID < rangeEnd) { michael@0: return ctx->generateGlyphToChar(glyphID - rangeStart); michael@0: } michael@0: ctx = ctx->getNextContext(); michael@0: } while (NULL != ctx); michael@0: return 0; michael@0: } michael@0: michael@0: void SkScalerContext::getAdvance(SkGlyph* glyph) { michael@0: // mark us as just having a valid advance michael@0: glyph->fMaskFormat = MASK_FORMAT_JUST_ADVANCE; michael@0: // we mark the format before making the call, in case the impl michael@0: // internally ends up calling its generateMetrics, which is OK michael@0: // albeit slower than strictly necessary michael@0: this->getGlyphContext(*glyph)->generateAdvance(glyph); michael@0: } michael@0: michael@0: void SkScalerContext::getMetrics(SkGlyph* glyph) { michael@0: this->getGlyphContext(*glyph)->generateMetrics(glyph); michael@0: michael@0: // for now we have separate cache entries for devkerning on and off michael@0: // in the future we might share caches, but make our measure/draw michael@0: // code make the distinction. Thus we zap the values if the caller michael@0: // has not asked for them. michael@0: if ((fRec.fFlags & SkScalerContext::kDevKernText_Flag) == 0) { michael@0: // no devkern, so zap the fields michael@0: glyph->fLsbDelta = glyph->fRsbDelta = 0; michael@0: } michael@0: michael@0: // if either dimension is empty, zap the image bounds of the glyph michael@0: if (0 == glyph->fWidth || 0 == glyph->fHeight) { michael@0: glyph->fWidth = 0; michael@0: glyph->fHeight = 0; michael@0: glyph->fTop = 0; michael@0: glyph->fLeft = 0; michael@0: glyph->fMaskFormat = 0; michael@0: return; michael@0: } michael@0: michael@0: if (fGenerateImageFromPath) { michael@0: SkPath devPath, fillPath; michael@0: SkMatrix fillToDevMatrix; michael@0: michael@0: this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); michael@0: michael@0: if (fRasterizer) { michael@0: SkMask mask; michael@0: michael@0: if (fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, michael@0: fMaskFilter, &mask, michael@0: SkMask::kJustComputeBounds_CreateMode)) { michael@0: glyph->fLeft = mask.fBounds.fLeft; michael@0: glyph->fTop = mask.fBounds.fTop; michael@0: glyph->fWidth = SkToU16(mask.fBounds.width()); michael@0: glyph->fHeight = SkToU16(mask.fBounds.height()); michael@0: } else { michael@0: goto SK_ERROR; michael@0: } michael@0: } else { michael@0: // just use devPath michael@0: SkIRect ir; michael@0: devPath.getBounds().roundOut(&ir); michael@0: michael@0: if (ir.isEmpty() || !ir.is16Bit()) { michael@0: goto SK_ERROR; michael@0: } michael@0: glyph->fLeft = ir.fLeft; michael@0: glyph->fTop = ir.fTop; michael@0: glyph->fWidth = SkToU16(ir.width()); michael@0: glyph->fHeight = SkToU16(ir.height()); michael@0: michael@0: if (glyph->fWidth > 0) { michael@0: switch (fRec.fMaskFormat) { michael@0: case SkMask::kLCD16_Format: michael@0: case SkMask::kLCD32_Format: michael@0: glyph->fWidth += 2; michael@0: glyph->fLeft -= 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (SkMask::kARGB32_Format != glyph->fMaskFormat) { michael@0: glyph->fMaskFormat = fRec.fMaskFormat; michael@0: } michael@0: michael@0: // If we are going to create the mask, then we cannot keep the color michael@0: if ((fGenerateImageFromPath || fMaskFilter) && michael@0: SkMask::kARGB32_Format == glyph->fMaskFormat) { michael@0: glyph->fMaskFormat = SkMask::kA8_Format; michael@0: } michael@0: michael@0: if (fMaskFilter) { michael@0: SkMask src, dst; michael@0: SkMatrix matrix; michael@0: michael@0: glyph->toMask(&src); michael@0: fRec.getMatrixFrom2x2(&matrix); michael@0: michael@0: src.fImage = NULL; // only want the bounds from the filter michael@0: if (fMaskFilter->filterMask(&dst, src, matrix, NULL)) { michael@0: if (dst.fBounds.isEmpty() || !dst.fBounds.is16Bit()) { michael@0: goto SK_ERROR; michael@0: } michael@0: SkASSERT(dst.fImage == NULL); michael@0: glyph->fLeft = dst.fBounds.fLeft; michael@0: glyph->fTop = dst.fBounds.fTop; michael@0: glyph->fWidth = SkToU16(dst.fBounds.width()); michael@0: glyph->fHeight = SkToU16(dst.fBounds.height()); michael@0: glyph->fMaskFormat = dst.fFormat; michael@0: } michael@0: } michael@0: return; michael@0: michael@0: SK_ERROR: michael@0: // draw nothing 'cause we failed michael@0: glyph->fLeft = 0; michael@0: glyph->fTop = 0; michael@0: glyph->fWidth = 0; michael@0: glyph->fHeight = 0; michael@0: // put a valid value here, in case it was earlier set to michael@0: // MASK_FORMAT_JUST_ADVANCE michael@0: glyph->fMaskFormat = fRec.fMaskFormat; michael@0: } michael@0: michael@0: #define SK_SHOW_TEXT_BLIT_COVERAGE 0 michael@0: michael@0: static void applyLUTToA8Mask(const SkMask& mask, const uint8_t* lut) { michael@0: uint8_t* SK_RESTRICT dst = (uint8_t*)mask.fImage; michael@0: unsigned rowBytes = mask.fRowBytes; michael@0: michael@0: for (int y = mask.fBounds.height() - 1; y >= 0; --y) { michael@0: for (int x = mask.fBounds.width() - 1; x >= 0; --x) { michael@0: dst[x] = lut[dst[x]]; michael@0: } michael@0: dst += rowBytes; michael@0: } michael@0: } michael@0: michael@0: template michael@0: static void pack4xHToLCD16(const SkBitmap& src, const SkMask& dst, michael@0: const SkMaskGamma::PreBlend& maskPreBlend) { michael@0: #define SAMPLES_PER_PIXEL 4 michael@0: #define LCD_PER_PIXEL 3 michael@0: SkASSERT(kAlpha_8_SkColorType == src.colorType()); michael@0: SkASSERT(SkMask::kLCD16_Format == dst.fFormat); michael@0: michael@0: const int sample_width = src.width(); michael@0: const int height = src.height(); michael@0: michael@0: uint16_t* dstP = (uint16_t*)dst.fImage; michael@0: size_t dstRB = dst.fRowBytes; michael@0: // An N tap FIR is defined by michael@0: // out[n] = coeff[0]*x[n] + coeff[1]*x[n-1] + ... + coeff[N]*x[n-N] michael@0: // or michael@0: // out[n] = sum(i, 0, N, coeff[i]*x[n-i]) michael@0: michael@0: // The strategy is to use one FIR (different coefficients) for each of r, g, and b. michael@0: // This means using every 4th FIR output value of each FIR and discarding the rest. michael@0: // The FIRs are aligned, and the coefficients reach 5 samples to each side of their 'center'. michael@0: // (For r and b this is technically incorrect, but the coeffs outside round to zero anyway.) michael@0: michael@0: // These are in some fixed point repesentation. michael@0: // Adding up to more than one simulates ink spread. michael@0: // For implementation reasons, these should never add up to more than two. michael@0: michael@0: // Coefficients determined by a gausian where 5 samples = 3 std deviations (0x110 'contrast'). michael@0: // Calculated using tools/generate_fir_coeff.py michael@0: // With this one almost no fringing is ever seen, but it is imperceptibly blurry. michael@0: // The lcd smoothed text is almost imperceptibly different from gray, michael@0: // but is still sharper on small stems and small rounded corners than gray. michael@0: // This also seems to be about as wide as one can get and only have a three pixel kernel. michael@0: // TODO: caculate these at runtime so parameters can be adjusted (esp contrast). michael@0: static const unsigned int coefficients[LCD_PER_PIXEL][SAMPLES_PER_PIXEL*3] = { michael@0: //The red subpixel is centered inside the first sample (at 1/6 pixel), and is shifted. michael@0: { 0x03, 0x0b, 0x1c, 0x33, 0x40, 0x39, 0x24, 0x10, 0x05, 0x01, 0x00, 0x00, }, michael@0: //The green subpixel is centered between two samples (at 1/2 pixel), so is symetric michael@0: { 0x00, 0x02, 0x08, 0x16, 0x2b, 0x3d, 0x3d, 0x2b, 0x16, 0x08, 0x02, 0x00, }, michael@0: //The blue subpixel is centered inside the last sample (at 5/6 pixel), and is shifted. michael@0: { 0x00, 0x00, 0x01, 0x05, 0x10, 0x24, 0x39, 0x40, 0x33, 0x1c, 0x0b, 0x03, }, michael@0: }; michael@0: michael@0: for (int y = 0; y < height; ++y) { michael@0: const uint8_t* srcP = src.getAddr8(0, y); michael@0: michael@0: // TODO: this fir filter implementation is straight forward, but slow. michael@0: // It should be possible to make it much faster. michael@0: for (int sample_x = -4, pixel_x = 0; sample_x < sample_width + 4; sample_x += 4, ++pixel_x) { michael@0: int fir[LCD_PER_PIXEL] = { 0 }; michael@0: for (int sample_index = SkMax32(0, sample_x - 4), coeff_index = sample_index - (sample_x - 4) michael@0: ; sample_index < SkMin32(sample_x + 8, sample_width) michael@0: ; ++sample_index, ++coeff_index) michael@0: { michael@0: int sample_value = srcP[sample_index]; michael@0: for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { michael@0: fir[subpxl_index] += coefficients[subpxl_index][coeff_index] * sample_value; michael@0: } michael@0: } michael@0: for (int subpxl_index = 0; subpxl_index < LCD_PER_PIXEL; ++subpxl_index) { michael@0: fir[subpxl_index] /= 0x100; michael@0: fir[subpxl_index] = SkMin32(fir[subpxl_index], 255); michael@0: } michael@0: michael@0: U8CPU r = sk_apply_lut_if(fir[0], maskPreBlend.fR); michael@0: U8CPU g = sk_apply_lut_if(fir[1], maskPreBlend.fG); michael@0: U8CPU b = sk_apply_lut_if(fir[2], maskPreBlend.fB); michael@0: #if SK_SHOW_TEXT_BLIT_COVERAGE michael@0: r = SkMax32(r, 10); g = SkMax32(g, 10); b = SkMax32(b, 10); michael@0: #endif michael@0: dstP[pixel_x] = SkPack888ToRGB16(r, g, b); michael@0: } michael@0: dstP = (uint16_t*)((char*)dstP + dstRB); michael@0: } michael@0: } michael@0: michael@0: template michael@0: static void pack4xHToLCD32(const SkBitmap& src, const SkMask& dst, michael@0: const SkMaskGamma::PreBlend& maskPreBlend) { michael@0: SkASSERT(kAlpha_8_SkColorType == src.colorType()); michael@0: SkASSERT(SkMask::kLCD32_Format == dst.fFormat); michael@0: michael@0: const int width = dst.fBounds.width(); michael@0: const int height = dst.fBounds.height(); michael@0: SkPMColor* dstP = (SkPMColor*)dst.fImage; michael@0: size_t dstRB = dst.fRowBytes; michael@0: michael@0: for (int y = 0; y < height; ++y) { michael@0: const uint8_t* srcP = src.getAddr8(0, y); michael@0: michael@0: // TODO: need to use fir filter here as well. michael@0: for (int x = 0; x < width; ++x) { michael@0: U8CPU r = sk_apply_lut_if(*srcP++, maskPreBlend.fR); michael@0: U8CPU g = sk_apply_lut_if(*srcP++, maskPreBlend.fG); michael@0: U8CPU b = sk_apply_lut_if(*srcP++, maskPreBlend.fB); michael@0: dstP[x] = SkPackARGB32(0xFF, r, g, b); michael@0: } michael@0: dstP = (SkPMColor*)((char*)dstP + dstRB); michael@0: } michael@0: } michael@0: michael@0: static inline int convert_8_to_1(unsigned byte) { michael@0: SkASSERT(byte <= 0xFF); michael@0: return byte >> 7; michael@0: } michael@0: michael@0: static uint8_t pack_8_to_1(const uint8_t alpha[8]) { michael@0: unsigned bits = 0; michael@0: for (int i = 0; i < 8; ++i) { michael@0: bits <<= 1; michael@0: bits |= convert_8_to_1(alpha[i]); michael@0: } michael@0: return SkToU8(bits); michael@0: } michael@0: michael@0: static void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) { michael@0: const int height = mask.fBounds.height(); michael@0: const int width = mask.fBounds.width(); michael@0: const int octs = width >> 3; michael@0: const int leftOverBits = width & 7; michael@0: michael@0: uint8_t* dst = mask.fImage; michael@0: const int dstPad = mask.fRowBytes - SkAlign8(width)/8; michael@0: SkASSERT(dstPad >= 0); michael@0: michael@0: const int srcPad = srcRB - width; michael@0: SkASSERT(srcPad >= 0); michael@0: michael@0: for (int y = 0; y < height; ++y) { michael@0: for (int i = 0; i < octs; ++i) { michael@0: *dst++ = pack_8_to_1(src); michael@0: src += 8; michael@0: } michael@0: if (leftOverBits > 0) { michael@0: unsigned bits = 0; michael@0: int shift = 7; michael@0: for (int i = 0; i < leftOverBits; ++i, --shift) { michael@0: bits |= convert_8_to_1(*src++) << shift; michael@0: } michael@0: *dst++ = bits; michael@0: } michael@0: src += srcPad; michael@0: dst += dstPad; michael@0: } michael@0: } michael@0: michael@0: static void generateMask(const SkMask& mask, const SkPath& path, michael@0: const SkMaskGamma::PreBlend& maskPreBlend) { michael@0: SkPaint paint; michael@0: michael@0: int srcW = mask.fBounds.width(); michael@0: int srcH = mask.fBounds.height(); michael@0: int dstW = srcW; michael@0: int dstH = srcH; michael@0: int dstRB = mask.fRowBytes; michael@0: michael@0: SkMatrix matrix; michael@0: matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), michael@0: -SkIntToScalar(mask.fBounds.fTop)); michael@0: michael@0: SkBitmap::Config config = SkBitmap::kA8_Config; michael@0: paint.setAntiAlias(SkMask::kBW_Format != mask.fFormat); michael@0: switch (mask.fFormat) { michael@0: case SkMask::kBW_Format: michael@0: dstRB = 0; // signals we need a copy michael@0: break; michael@0: case SkMask::kA8_Format: michael@0: break; michael@0: case SkMask::kLCD16_Format: michael@0: case SkMask::kLCD32_Format: michael@0: // TODO: trigger off LCD orientation michael@0: dstW = 4*dstW - 8; michael@0: matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft + 1), michael@0: -SkIntToScalar(mask.fBounds.fTop)); michael@0: matrix.postScale(SkIntToScalar(4), SK_Scalar1); michael@0: dstRB = 0; // signals we need a copy michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unexpected mask format"); michael@0: } michael@0: michael@0: SkRasterClip clip; michael@0: clip.setRect(SkIRect::MakeWH(dstW, dstH)); michael@0: michael@0: SkBitmap bm; michael@0: bm.setConfig(config, dstW, dstH, dstRB); michael@0: michael@0: if (0 == dstRB) { michael@0: if (!bm.allocPixels()) { michael@0: // can't allocate offscreen, so empty the mask and return michael@0: sk_bzero(mask.fImage, mask.computeImageSize()); michael@0: return; michael@0: } michael@0: bm.lockPixels(); michael@0: } else { michael@0: bm.setPixels(mask.fImage); michael@0: } michael@0: sk_bzero(bm.getPixels(), bm.getSafeSize()); michael@0: michael@0: SkDraw draw; michael@0: draw.fRC = &clip; michael@0: draw.fClip = &clip.bwRgn(); michael@0: draw.fMatrix = &matrix; michael@0: draw.fBitmap = &bm; michael@0: draw.drawPath(path, paint); michael@0: michael@0: switch (mask.fFormat) { michael@0: case SkMask::kBW_Format: michael@0: packA8ToA1(mask, bm.getAddr8(0, 0), bm.rowBytes()); michael@0: break; michael@0: case SkMask::kA8_Format: michael@0: if (maskPreBlend.isApplicable()) { michael@0: applyLUTToA8Mask(mask, maskPreBlend.fG); michael@0: } michael@0: break; michael@0: case SkMask::kLCD16_Format: michael@0: if (maskPreBlend.isApplicable()) { michael@0: pack4xHToLCD16(bm, mask, maskPreBlend); michael@0: } else { michael@0: pack4xHToLCD16(bm, mask, maskPreBlend); michael@0: } michael@0: break; michael@0: case SkMask::kLCD32_Format: michael@0: if (maskPreBlend.isApplicable()) { michael@0: pack4xHToLCD32(bm, mask, maskPreBlend); michael@0: } else { michael@0: pack4xHToLCD32(bm, mask, maskPreBlend); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: static void extract_alpha(const SkMask& dst, michael@0: const SkPMColor* srcRow, size_t srcRB) { michael@0: int width = dst.fBounds.width(); michael@0: int height = dst.fBounds.height(); michael@0: int dstRB = dst.fRowBytes; michael@0: uint8_t* dstRow = dst.fImage; michael@0: michael@0: for (int y = 0; y < height; ++y) { michael@0: for (int x = 0; x < width; ++x) { michael@0: dstRow[x] = SkGetPackedA32(srcRow[x]); michael@0: } michael@0: // zero any padding on each row michael@0: for (int x = width; x < dstRB; ++x) { michael@0: dstRow[x] = 0; michael@0: } michael@0: dstRow += dstRB; michael@0: srcRow = (const SkPMColor*)((const char*)srcRow + srcRB); michael@0: } michael@0: } michael@0: michael@0: void SkScalerContext::getImage(const SkGlyph& origGlyph) { michael@0: const SkGlyph* glyph = &origGlyph; michael@0: SkGlyph tmpGlyph; michael@0: michael@0: // in case we need to call generateImage on a mask-format that is different michael@0: // (i.e. larger) than what our caller allocated by looking at origGlyph. michael@0: SkAutoMalloc tmpGlyphImageStorage; michael@0: michael@0: // If we are going to draw-from-path, then we cannot generate color, since michael@0: // the path only makes a mask. This case should have been caught up in michael@0: // generateMetrics(). michael@0: SkASSERT(!fGenerateImageFromPath || michael@0: SkMask::kARGB32_Format != origGlyph.fMaskFormat); michael@0: michael@0: if (fMaskFilter) { // restore the prefilter bounds michael@0: tmpGlyph.init(origGlyph.fID); michael@0: michael@0: // need the original bounds, sans our maskfilter michael@0: SkMaskFilter* mf = fMaskFilter; michael@0: fMaskFilter = NULL; // temp disable michael@0: this->getMetrics(&tmpGlyph); michael@0: fMaskFilter = mf; // restore michael@0: michael@0: // we need the prefilter bounds to be <= filter bounds michael@0: SkASSERT(tmpGlyph.fWidth <= origGlyph.fWidth); michael@0: SkASSERT(tmpGlyph.fHeight <= origGlyph.fHeight); michael@0: michael@0: if (tmpGlyph.fMaskFormat == origGlyph.fMaskFormat) { michael@0: tmpGlyph.fImage = origGlyph.fImage; michael@0: } else { michael@0: tmpGlyphImageStorage.reset(tmpGlyph.computeImageSize()); michael@0: tmpGlyph.fImage = tmpGlyphImageStorage.get(); michael@0: } michael@0: glyph = &tmpGlyph; michael@0: } michael@0: michael@0: if (fGenerateImageFromPath) { michael@0: SkPath devPath, fillPath; michael@0: SkMatrix fillToDevMatrix; michael@0: SkMask mask; michael@0: michael@0: this->internalGetPath(*glyph, &fillPath, &devPath, &fillToDevMatrix); michael@0: glyph->toMask(&mask); michael@0: michael@0: if (fRasterizer) { michael@0: mask.fFormat = SkMask::kA8_Format; michael@0: sk_bzero(glyph->fImage, mask.computeImageSize()); michael@0: michael@0: if (!fRasterizer->rasterize(fillPath, fillToDevMatrix, NULL, michael@0: fMaskFilter, &mask, michael@0: SkMask::kJustRenderImage_CreateMode)) { michael@0: return; michael@0: } michael@0: if (fPreBlend.isApplicable()) { michael@0: applyLUTToA8Mask(mask, fPreBlend.fG); michael@0: } michael@0: } else { michael@0: SkASSERT(SkMask::kARGB32_Format != mask.fFormat); michael@0: generateMask(mask, devPath, fPreBlend); michael@0: } michael@0: } else { michael@0: this->getGlyphContext(*glyph)->generateImage(*glyph); michael@0: } michael@0: michael@0: if (fMaskFilter) { michael@0: SkMask srcM, dstM; michael@0: SkMatrix matrix; michael@0: michael@0: // the src glyph image shouldn't be 3D michael@0: SkASSERT(SkMask::k3D_Format != glyph->fMaskFormat); michael@0: michael@0: SkAutoSMalloc<32*32> a8storage; michael@0: glyph->toMask(&srcM); michael@0: if (SkMask::kARGB32_Format == srcM.fFormat) { michael@0: // now we need to extract the alpha-channel from the glyph's image michael@0: // and copy it into a temp buffer, and then point srcM at that temp. michael@0: srcM.fFormat = SkMask::kA8_Format; michael@0: srcM.fRowBytes = SkAlign4(srcM.fBounds.width()); michael@0: size_t size = srcM.computeImageSize(); michael@0: a8storage.reset(size); michael@0: srcM.fImage = (uint8_t*)a8storage.get(); michael@0: extract_alpha(srcM, michael@0: (const SkPMColor*)glyph->fImage, glyph->rowBytes()); michael@0: } michael@0: michael@0: fRec.getMatrixFrom2x2(&matrix); michael@0: michael@0: if (fMaskFilter->filterMask(&dstM, srcM, matrix, NULL)) { michael@0: int width = SkFastMin32(origGlyph.fWidth, dstM.fBounds.width()); michael@0: int height = SkFastMin32(origGlyph.fHeight, dstM.fBounds.height()); michael@0: int dstRB = origGlyph.rowBytes(); michael@0: int srcRB = dstM.fRowBytes; michael@0: michael@0: const uint8_t* src = (const uint8_t*)dstM.fImage; michael@0: uint8_t* dst = (uint8_t*)origGlyph.fImage; michael@0: michael@0: if (SkMask::k3D_Format == dstM.fFormat) { michael@0: // we have to copy 3 times as much michael@0: height *= 3; michael@0: } michael@0: michael@0: // clean out our glyph, since it may be larger than dstM michael@0: //sk_bzero(dst, height * dstRB); michael@0: michael@0: while (--height >= 0) { michael@0: memcpy(dst, src, width); michael@0: src += srcRB; michael@0: dst += dstRB; michael@0: } michael@0: SkMask::FreeImage(dstM.fImage); michael@0: michael@0: if (fPreBlendForFilter.isApplicable()) { michael@0: applyLUTToA8Mask(srcM, fPreBlendForFilter.fG); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkScalerContext::getPath(const SkGlyph& glyph, SkPath* path) { michael@0: this->internalGetPath(glyph, NULL, path, NULL); michael@0: } michael@0: michael@0: void SkScalerContext::getFontMetrics(SkPaint::FontMetrics* fm) { michael@0: // All of this complexity should go away when we change generateFontMetrics michael@0: // to just take one parameter (since it knows if it is vertical or not) michael@0: SkPaint::FontMetrics* mx = NULL; michael@0: SkPaint::FontMetrics* my = NULL; michael@0: if (fRec.fFlags & kVertical_Flag) { michael@0: mx = fm; michael@0: } else { michael@0: my = fm; michael@0: } michael@0: this->generateFontMetrics(mx, my); michael@0: } michael@0: michael@0: SkUnichar SkScalerContext::generateGlyphToChar(uint16_t glyph) { michael@0: return 0; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkScalerContext::internalGetPath(const SkGlyph& glyph, SkPath* fillPath, michael@0: SkPath* devPath, SkMatrix* fillToDevMatrix) { michael@0: SkPath path; michael@0: michael@0: this->getGlyphContext(glyph)->generatePath(glyph, &path); michael@0: michael@0: if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { michael@0: SkFixed dx = glyph.getSubXFixed(); michael@0: SkFixed dy = glyph.getSubYFixed(); michael@0: if (dx | dy) { michael@0: path.offset(SkFixedToScalar(dx), SkFixedToScalar(dy)); michael@0: } michael@0: } michael@0: michael@0: if (fRec.fFrameWidth > 0 || fPathEffect != NULL) { michael@0: // need the path in user-space, with only the point-size applied michael@0: // so that our stroking and effects will operate the same way they michael@0: // would if the user had extracted the path themself, and then michael@0: // called drawPath michael@0: SkPath localPath; michael@0: SkMatrix matrix, inverse; michael@0: michael@0: fRec.getMatrixFrom2x2(&matrix); michael@0: if (!matrix.invert(&inverse)) { michael@0: // assume fillPath and devPath are already empty. michael@0: return; michael@0: } michael@0: path.transform(inverse, &localPath); michael@0: // now localPath is only affected by the paint settings, and not the canvas matrix michael@0: michael@0: SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); michael@0: michael@0: if (fRec.fFrameWidth > 0) { michael@0: rec.setStrokeStyle(fRec.fFrameWidth, michael@0: SkToBool(fRec.fFlags & kFrameAndFill_Flag)); michael@0: // glyphs are always closed contours, so cap type is ignored, michael@0: // so we just pass something. michael@0: rec.setStrokeParams(SkPaint::kButt_Cap, michael@0: (SkPaint::Join)fRec.fStrokeJoin, michael@0: fRec.fMiterLimit); michael@0: } michael@0: michael@0: if (fPathEffect) { michael@0: SkPath effectPath; michael@0: if (fPathEffect->filterPath(&effectPath, localPath, &rec, NULL)) { michael@0: localPath.swap(effectPath); michael@0: } michael@0: } michael@0: michael@0: if (rec.needToApply()) { michael@0: SkPath strokePath; michael@0: if (rec.applyToPath(&strokePath, localPath)) { michael@0: localPath.swap(strokePath); michael@0: } michael@0: } michael@0: michael@0: // now return stuff to the caller michael@0: if (fillToDevMatrix) { michael@0: *fillToDevMatrix = matrix; michael@0: } michael@0: if (devPath) { michael@0: localPath.transform(matrix, devPath); michael@0: } michael@0: if (fillPath) { michael@0: fillPath->swap(localPath); michael@0: } michael@0: } else { // nothing tricky to do michael@0: if (fillToDevMatrix) { michael@0: fillToDevMatrix->reset(); michael@0: } michael@0: if (devPath) { michael@0: if (fillPath == NULL) { michael@0: devPath->swap(path); michael@0: } else { michael@0: *devPath = path; michael@0: } michael@0: } michael@0: michael@0: if (fillPath) { michael@0: fillPath->swap(path); michael@0: } michael@0: } michael@0: michael@0: if (devPath) { michael@0: devPath->updateBoundsCache(); michael@0: } michael@0: if (fillPath) { michael@0: fillPath->updateBoundsCache(); michael@0: } michael@0: } michael@0: michael@0: michael@0: void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const { michael@0: dst->setAll(fPost2x2[0][0], fPost2x2[0][1], 0, michael@0: fPost2x2[1][0], fPost2x2[1][1], 0, michael@0: 0, 0, SkScalarToPersp(SK_Scalar1)); michael@0: } michael@0: michael@0: void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const { michael@0: SkPaint::SetTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX); michael@0: } michael@0: michael@0: void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const { michael@0: this->getLocalMatrix(m); michael@0: michael@0: // now concat the device matrix michael@0: SkMatrix deviceMatrix; michael@0: this->getMatrixFrom2x2(&deviceMatrix); michael@0: m->postConcat(deviceMatrix); michael@0: } michael@0: michael@0: SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix) { michael@0: SkASSERT(!matrix.hasPerspective()); michael@0: michael@0: if (0 == matrix[SkMatrix::kMSkewY]) { michael@0: return kX_SkAxisAlignment; michael@0: } michael@0: if (0 == matrix[SkMatrix::kMScaleX]) { michael@0: return kY_SkAxisAlignment; michael@0: } michael@0: return kNone_SkAxisAlignment; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkFontHost.h" michael@0: michael@0: class SkScalerContext_Empty : public SkScalerContext { michael@0: public: michael@0: SkScalerContext_Empty(SkTypeface* face, const SkDescriptor* desc) michael@0: : SkScalerContext(face, desc) {} michael@0: michael@0: protected: michael@0: virtual unsigned generateGlyphCount() SK_OVERRIDE { michael@0: return 0; michael@0: } michael@0: virtual uint16_t generateCharToGlyph(SkUnichar uni) SK_OVERRIDE { michael@0: return 0; michael@0: } michael@0: virtual void generateAdvance(SkGlyph* glyph) SK_OVERRIDE { michael@0: glyph->zeroMetrics(); michael@0: } michael@0: virtual void generateMetrics(SkGlyph* glyph) SK_OVERRIDE { michael@0: glyph->zeroMetrics(); michael@0: } michael@0: virtual void generateImage(const SkGlyph& glyph) SK_OVERRIDE {} michael@0: virtual void generatePath(const SkGlyph& glyph, SkPath* path) SK_OVERRIDE {} michael@0: virtual void generateFontMetrics(SkPaint::FontMetrics* mx, michael@0: SkPaint::FontMetrics* my) SK_OVERRIDE { michael@0: if (mx) { michael@0: sk_bzero(mx, sizeof(*mx)); michael@0: } michael@0: if (my) { michael@0: sk_bzero(my, sizeof(*my)); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: extern SkScalerContext* SkCreateColorScalerContext(const SkDescriptor* desc); michael@0: michael@0: SkScalerContext* SkTypeface::createScalerContext(const SkDescriptor* desc, michael@0: bool allowFailure) const { michael@0: SkScalerContext* c = this->onCreateScalerContext(desc); michael@0: michael@0: if (!c && !allowFailure) { michael@0: c = SkNEW_ARGS(SkScalerContext_Empty, michael@0: (const_cast(this), desc)); michael@0: } michael@0: return c; michael@0: }