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: #ifndef SkScalerContext_DEFINED michael@0: #define SkScalerContext_DEFINED michael@0: michael@0: #include "SkMask.h" michael@0: #include "SkMaskGamma.h" michael@0: #include "SkMatrix.h" michael@0: #include "SkPaint.h" michael@0: #include "SkTypeface.h" michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: #include "SkPaintOptionsAndroid.h" michael@0: #endif michael@0: michael@0: struct SkGlyph; michael@0: class SkDescriptor; michael@0: class SkMaskFilter; michael@0: class SkPathEffect; michael@0: class SkRasterizer; michael@0: michael@0: /* michael@0: * To allow this to be forward-declared, it must be its own typename, rather michael@0: * than a nested struct inside SkScalerContext (where it started). michael@0: */ michael@0: struct SkScalerContextRec { michael@0: uint32_t fOrigFontID; michael@0: uint32_t fFontID; michael@0: SkScalar fTextSize, fPreScaleX, fPreSkewX; michael@0: SkScalar fPost2x2[2][2]; michael@0: SkScalar fFrameWidth, fMiterLimit; michael@0: michael@0: //These describe the parameters to create (uniquely identify) the pre-blend. michael@0: uint32_t fLumBits; michael@0: uint8_t fDeviceGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB michael@0: uint8_t fPaintGamma; //2.6, (0.0, 4.0) gamma, 0.0 for sRGB michael@0: uint8_t fContrast; //0.8+1, [0.0, 1.0] artificial contrast michael@0: uint8_t fReservedAlign; michael@0: michael@0: SkScalar getDeviceGamma() const { michael@0: return SkIntToScalar(fDeviceGamma) / (1 << 6); michael@0: } michael@0: void setDeviceGamma(SkScalar dg) { michael@0: SkASSERT(0 <= dg && dg < SkIntToScalar(4)); michael@0: fDeviceGamma = SkScalarFloorToInt(dg * (1 << 6)); michael@0: } michael@0: michael@0: SkScalar getPaintGamma() const { michael@0: return SkIntToScalar(fPaintGamma) / (1 << 6); michael@0: } michael@0: void setPaintGamma(SkScalar pg) { michael@0: SkASSERT(0 <= pg && pg < SkIntToScalar(4)); michael@0: fPaintGamma = SkScalarFloorToInt(pg * (1 << 6)); michael@0: } michael@0: michael@0: SkScalar getContrast() const { michael@0: return SkIntToScalar(fContrast) / ((1 << 8) - 1); michael@0: } michael@0: void setContrast(SkScalar c) { michael@0: SkASSERT(0 <= c && c <= SK_Scalar1); michael@0: fContrast = SkScalarRoundToInt(c * ((1 << 8) - 1)); michael@0: } michael@0: michael@0: /** michael@0: * Causes the luminance color and contrast to be ignored, and the michael@0: * paint and device gamma to be effectively 1.0. michael@0: */ michael@0: void ignorePreBlend() { michael@0: setLuminanceColor(SK_ColorTRANSPARENT); michael@0: setPaintGamma(SK_Scalar1); michael@0: setDeviceGamma(SK_Scalar1); michael@0: setContrast(0); michael@0: } michael@0: michael@0: uint8_t fMaskFormat; michael@0: uint8_t fStrokeJoin; michael@0: uint16_t fFlags; michael@0: // Warning: when adding members note that the size of this structure michael@0: // must be a multiple of 4. SkDescriptor requires that its arguments be michael@0: // multiples of four and this structure is put in an SkDescriptor in michael@0: // SkPaint::MakeRec. michael@0: michael@0: void getMatrixFrom2x2(SkMatrix*) const; michael@0: void getLocalMatrix(SkMatrix*) const; michael@0: void getSingleMatrix(SkMatrix*) const; michael@0: michael@0: inline SkPaint::Hinting getHinting() const; michael@0: inline void setHinting(SkPaint::Hinting); michael@0: michael@0: SkMask::Format getFormat() const { michael@0: return static_cast(fMaskFormat); michael@0: } michael@0: michael@0: SkColor getLuminanceColor() const { michael@0: return fLumBits; michael@0: } michael@0: michael@0: void setLuminanceColor(SkColor c) { michael@0: fLumBits = c; michael@0: } michael@0: }; michael@0: michael@0: //The following typedef hides from the rest of the implementation the number of michael@0: //most significant bits to consider when creating mask gamma tables. Two bits michael@0: //per channel was chosen as a balance between fidelity (more bits) and cache michael@0: //sizes (fewer bits). Three bits per channel was chosen when #303942; (used by michael@0: //the Chrome UI) turned out too green. michael@0: typedef SkTMaskGamma<3, 3, 3> SkMaskGamma; michael@0: michael@0: class SkScalerContext { michael@0: public: michael@0: typedef SkScalerContextRec Rec; michael@0: michael@0: enum Flags { michael@0: kFrameAndFill_Flag = 0x0001, michael@0: kDevKernText_Flag = 0x0002, michael@0: kEmbeddedBitmapText_Flag = 0x0004, michael@0: kEmbolden_Flag = 0x0008, michael@0: kSubpixelPositioning_Flag = 0x0010, michael@0: kForceAutohinting_Flag = 0x0020, // Use auto instead of bytcode hinting if hinting. michael@0: kVertical_Flag = 0x0040, michael@0: michael@0: // together, these two flags resulting in a two bit value which matches michael@0: // up with the SkPaint::Hinting enum. michael@0: kHinting_Shift = 7, // to shift into the other flags above michael@0: kHintingBit1_Flag = 0x0080, michael@0: kHintingBit2_Flag = 0x0100, michael@0: michael@0: // Pixel geometry information. michael@0: // only meaningful if fMaskFormat is LCD16 or LCD32 michael@0: kLCD_Vertical_Flag = 0x0200, // else Horizontal michael@0: kLCD_BGROrder_Flag = 0x0400, // else RGB order michael@0: michael@0: // Generate A8 from LCD source (for GDI and CoreGraphics). michael@0: // only meaningful if fMaskFormat is kA8 michael@0: kGenA8FromLCD_Flag = 0x0800, // could be 0x200 (bit meaning dependent on fMaskFormat) michael@0: }; michael@0: michael@0: // computed values michael@0: enum { michael@0: kHinting_Mask = kHintingBit1_Flag | kHintingBit2_Flag, michael@0: }; michael@0: michael@0: michael@0: SkScalerContext(SkTypeface*, const SkDescriptor*); michael@0: virtual ~SkScalerContext(); michael@0: michael@0: SkTypeface* getTypeface() const { return fTypeface.get(); } michael@0: michael@0: SkMask::Format getMaskFormat() const { michael@0: return (SkMask::Format)fRec.fMaskFormat; michael@0: } michael@0: michael@0: bool isSubpixel() const { michael@0: return SkToBool(fRec.fFlags & kSubpixelPositioning_Flag); michael@0: } michael@0: michael@0: // remember our glyph offset/base michael@0: void setBaseGlyphCount(unsigned baseGlyphCount) { michael@0: fBaseGlyphCount = baseGlyphCount; michael@0: } michael@0: michael@0: /** Return the corresponding glyph for the specified unichar. Since contexts michael@0: may be chained (under the hood), the glyphID that is returned may in michael@0: fact correspond to a different font/context. In that case, we use the michael@0: base-glyph-count to know how to translate back into local glyph space. michael@0: */ michael@0: uint16_t charToGlyphID(SkUnichar uni); michael@0: michael@0: /** Map the glyphID to its glyph index, and then to its char code. Unmapped michael@0: glyphs return zero. michael@0: */ michael@0: SkUnichar glyphIDToChar(uint16_t glyphID); michael@0: michael@0: unsigned getGlyphCount() { return this->generateGlyphCount(); } michael@0: void getAdvance(SkGlyph*); michael@0: void getMetrics(SkGlyph*); michael@0: void getImage(const SkGlyph&); michael@0: void getPath(const SkGlyph&, SkPath*); michael@0: void getFontMetrics(SkPaint::FontMetrics*); michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: unsigned getBaseGlyphCount(SkUnichar charCode); michael@0: michael@0: // This function must be public for SkTypeface_android.h, but should not be michael@0: // called by other callers michael@0: SkFontID findTypefaceIdForChar(SkUnichar uni); michael@0: #endif michael@0: michael@0: static inline void MakeRec(const SkPaint&, const SkDeviceProperties* deviceProperties, michael@0: const SkMatrix*, Rec* rec); michael@0: static inline void PostMakeRec(const SkPaint&, Rec*); michael@0: michael@0: static SkMaskGamma::PreBlend GetMaskPreBlend(const Rec& rec); michael@0: michael@0: protected: michael@0: Rec fRec; michael@0: unsigned fBaseGlyphCount; michael@0: michael@0: /** Generates the contents of glyph.fAdvanceX and glyph.fAdvanceY. michael@0: * May call getMetrics if that would be just as fast. michael@0: */ michael@0: virtual void generateAdvance(SkGlyph* glyph) = 0; michael@0: michael@0: /** Generates the contents of glyph.fWidth, fHeight, fTop, fLeft, michael@0: * as well as fAdvanceX and fAdvanceY if not already set. michael@0: * michael@0: * TODO: fMaskFormat is set by getMetrics later; cannot be set here. michael@0: */ michael@0: virtual void generateMetrics(SkGlyph* glyph) = 0; michael@0: michael@0: /** Generates the contents of glyph.fImage. michael@0: * When called, glyph.fImage will be pointing to a pre-allocated, michael@0: * uninitialized region of memory of size glyph.computeImageSize(). michael@0: * This method may change glyph.fMaskFormat if the new image size is michael@0: * less than or equal to the old image size. michael@0: * michael@0: * Because glyph.computeImageSize() will determine the size of fImage, michael@0: * generateMetrics will be called before generateImage. michael@0: */ michael@0: virtual void generateImage(const SkGlyph& glyph) = 0; michael@0: michael@0: /** Sets the passed path to the glyph outline. michael@0: * If this cannot be done the path is set to empty; michael@0: * this is indistinguishable from a glyph with an empty path. michael@0: * This does not set glyph.fPath. michael@0: * michael@0: * TODO: path is always glyph.fPath, no reason to pass separately. michael@0: */ michael@0: virtual void generatePath(const SkGlyph& glyph, SkPath* path) = 0; michael@0: michael@0: /** Retrieves font metrics. michael@0: * TODO: there is now a vertical bit, no need for two parameters. michael@0: */ michael@0: virtual void generateFontMetrics(SkPaint::FontMetrics* mX, michael@0: SkPaint::FontMetrics* mY) = 0; michael@0: michael@0: /** Returns the number of glyphs in the font. */ michael@0: virtual unsigned generateGlyphCount() = 0; michael@0: michael@0: /** Returns the glyph id for the given unichar. michael@0: * If there is no 1:1 mapping from the unichar to a glyph id, returns 0. michael@0: */ michael@0: virtual uint16_t generateCharToGlyph(SkUnichar unichar) = 0; michael@0: michael@0: /** Returns the unichar for the given glyph id. michael@0: * If there is no 1:1 mapping from the glyph id to a unichar, returns 0. michael@0: * The default implementation always returns 0, indicating failure. michael@0: */ michael@0: virtual SkUnichar generateGlyphToChar(uint16_t glyphId); michael@0: michael@0: void forceGenerateImageFromPath() { fGenerateImageFromPath = true; } michael@0: michael@0: private: michael@0: // never null michael@0: SkAutoTUnref fTypeface; michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: SkPaintOptionsAndroid fPaintOptionsAndroid; michael@0: #endif michael@0: michael@0: // optional object, which may be null michael@0: SkPathEffect* fPathEffect; michael@0: SkMaskFilter* fMaskFilter; michael@0: SkRasterizer* fRasterizer; michael@0: michael@0: // if this is set, we draw the image from a path, rather than michael@0: // calling generateImage. michael@0: bool fGenerateImageFromPath; michael@0: michael@0: void internalGetPath(const SkGlyph& glyph, SkPath* fillPath, michael@0: SkPath* devPath, SkMatrix* fillToDevMatrix); 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* allocNextContext() const; michael@0: michael@0: // return the next context, treating fNextContext as a cache of the answer michael@0: SkScalerContext* getNextContext(); michael@0: michael@0: // returns the right context from our link-list for this glyph. If no match michael@0: // is found, just returns the original context (this) michael@0: SkScalerContext* getGlyphContext(const SkGlyph& glyph); michael@0: michael@0: // returns the right context from our link-list for this char. If no match michael@0: // is found it returns NULL. If a match is found then the glyphID param is michael@0: // set to the glyphID that maps to the provided char. michael@0: SkScalerContext* getContextFromChar(SkUnichar uni, uint16_t* glyphID); michael@0: michael@0: // link-list of context, to handle missing chars. null-terminated. michael@0: SkScalerContext* fNextContext; michael@0: michael@0: // SkMaskGamma::PreBlend converts linear masks to gamma correcting masks. michael@0: protected: michael@0: // Visible to subclasses so that generateImage can apply the pre-blend directly. michael@0: const SkMaskGamma::PreBlend fPreBlend; michael@0: private: michael@0: // When there is a filter, previous steps must create a linear mask michael@0: // and the pre-blend applied as a final step. michael@0: const SkMaskGamma::PreBlend fPreBlendForFilter; michael@0: }; michael@0: michael@0: #define kRec_SkDescriptorTag SkSetFourByteTag('s', 'r', 'e', 'c') michael@0: #define kPathEffect_SkDescriptorTag SkSetFourByteTag('p', 't', 'h', 'e') michael@0: #define kMaskFilter_SkDescriptorTag SkSetFourByteTag('m', 's', 'k', 'f') michael@0: #define kRasterizer_SkDescriptorTag SkSetFourByteTag('r', 'a', 's', 't') michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: #define kAndroidOpts_SkDescriptorTag SkSetFourByteTag('a', 'n', 'd', 'r') michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: enum SkAxisAlignment { michael@0: kNone_SkAxisAlignment, michael@0: kX_SkAxisAlignment, michael@0: kY_SkAxisAlignment michael@0: }; michael@0: michael@0: /** michael@0: * Return the axis (if any) that the baseline for horizontal text will land on michael@0: * after running through the specified matrix. michael@0: * michael@0: * As an example, the identity matrix will return kX_SkAxisAlignment michael@0: */ michael@0: SkAxisAlignment SkComputeAxisAlignmentForHText(const SkMatrix& matrix); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkPaint::Hinting SkScalerContextRec::getHinting() const { michael@0: unsigned hint = (fFlags & SkScalerContext::kHinting_Mask) >> michael@0: SkScalerContext::kHinting_Shift; michael@0: return static_cast(hint); michael@0: } michael@0: michael@0: void SkScalerContextRec::setHinting(SkPaint::Hinting hinting) { michael@0: fFlags = (fFlags & ~SkScalerContext::kHinting_Mask) | michael@0: (hinting << SkScalerContext::kHinting_Shift); michael@0: } michael@0: michael@0: michael@0: #endif