gfx/skia/trunk/src/fonts/SkGScalerContext.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/fonts/SkGScalerContext.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,265 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     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 +#include "SkGScalerContext.h"
    1.12 +#include "SkGlyph.h"
    1.13 +#include "SkPath.h"
    1.14 +#include "SkCanvas.h"
    1.15 +
    1.16 +class SkGScalerContext : public SkScalerContext {
    1.17 +public:
    1.18 +    SkGScalerContext(SkGTypeface*, const SkDescriptor*);
    1.19 +    virtual ~SkGScalerContext();
    1.20 +
    1.21 +protected:
    1.22 +    virtual unsigned generateGlyphCount() SK_OVERRIDE;
    1.23 +    virtual uint16_t generateCharToGlyph(SkUnichar) SK_OVERRIDE;
    1.24 +    virtual void generateAdvance(SkGlyph*) SK_OVERRIDE;
    1.25 +    virtual void generateMetrics(SkGlyph*) SK_OVERRIDE;
    1.26 +    virtual void generateImage(const SkGlyph&) SK_OVERRIDE;
    1.27 +    virtual void generatePath(const SkGlyph&, SkPath*) SK_OVERRIDE;
    1.28 +    virtual void generateFontMetrics(SkPaint::FontMetrics* mX,
    1.29 +                                     SkPaint::FontMetrics* mY) SK_OVERRIDE;
    1.30 +
    1.31 +private:
    1.32 +    SkGTypeface*     fFace;
    1.33 +    SkScalerContext* fProxy;
    1.34 +    SkMatrix         fMatrix;
    1.35 +};
    1.36 +
    1.37 +#define STD_SIZE    1
    1.38 +
    1.39 +#include "SkDescriptor.h"
    1.40 +
    1.41 +SkGScalerContext::SkGScalerContext(SkGTypeface* face, const SkDescriptor* desc)
    1.42 +        : SkScalerContext(face, desc)
    1.43 +        , fFace(face)
    1.44 +{
    1.45 +
    1.46 +    size_t  descSize = SkDescriptor::ComputeOverhead(1) + sizeof(SkScalerContext::Rec);
    1.47 +    SkAutoDescriptor ad(descSize);
    1.48 +    SkDescriptor*    newDesc = ad.getDesc();
    1.49 +
    1.50 +    newDesc->init();
    1.51 +    void* entry = newDesc->addEntry(kRec_SkDescriptorTag,
    1.52 +                                    sizeof(SkScalerContext::Rec), &fRec);
    1.53 +    {
    1.54 +        SkScalerContext::Rec* rec = (SkScalerContext::Rec*)entry;
    1.55 +        rec->fTextSize = STD_SIZE;
    1.56 +        rec->fPreScaleX = SK_Scalar1;
    1.57 +        rec->fPreSkewX = 0;
    1.58 +        rec->fPost2x2[0][0] = rec->fPost2x2[1][1] = SK_Scalar1;
    1.59 +        rec->fPost2x2[1][0] = rec->fPost2x2[0][1] = 0;
    1.60 +    }
    1.61 +    SkASSERT(descSize == newDesc->getLength());
    1.62 +    newDesc->computeChecksum();
    1.63 +
    1.64 +    fProxy = face->proxy()->createScalerContext(newDesc);
    1.65 +
    1.66 +    fRec.getSingleMatrix(&fMatrix);
    1.67 +    fMatrix.preScale(SK_Scalar1 / STD_SIZE, SK_Scalar1 / STD_SIZE);
    1.68 +}
    1.69 +
    1.70 +SkGScalerContext::~SkGScalerContext() {
    1.71 +    SkDELETE(fProxy);
    1.72 +}
    1.73 +
    1.74 +unsigned SkGScalerContext::generateGlyphCount() {
    1.75 +    return fProxy->getGlyphCount();
    1.76 +}
    1.77 +
    1.78 +uint16_t SkGScalerContext::generateCharToGlyph(SkUnichar uni) {
    1.79 +    return fProxy->charToGlyphID(uni);
    1.80 +}
    1.81 +
    1.82 +void SkGScalerContext::generateAdvance(SkGlyph* glyph) {
    1.83 +    fProxy->getAdvance(glyph);
    1.84 +
    1.85 +    SkVector advance;
    1.86 +    fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
    1.87 +                  SkFixedToScalar(glyph->fAdvanceY), &advance);
    1.88 +    glyph->fAdvanceX = SkScalarToFixed(advance.fX);
    1.89 +    glyph->fAdvanceY = SkScalarToFixed(advance.fY);
    1.90 +}
    1.91 +
    1.92 +void SkGScalerContext::generateMetrics(SkGlyph* glyph) {
    1.93 +    fProxy->getMetrics(glyph);
    1.94 +
    1.95 +    SkVector advance;
    1.96 +    fMatrix.mapXY(SkFixedToScalar(glyph->fAdvanceX),
    1.97 +                  SkFixedToScalar(glyph->fAdvanceY), &advance);
    1.98 +    glyph->fAdvanceX = SkScalarToFixed(advance.fX);
    1.99 +    glyph->fAdvanceY = SkScalarToFixed(advance.fY);
   1.100 +
   1.101 +    SkPath path;
   1.102 +    fProxy->getPath(*glyph, &path);
   1.103 +    path.transform(fMatrix);
   1.104 +
   1.105 +    SkRect storage;
   1.106 +    const SkPaint& paint = fFace->paint();
   1.107 +    const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(),
   1.108 +                                                        &storage,
   1.109 +                                                        SkPaint::kFill_Style);
   1.110 +    SkIRect ibounds;
   1.111 +    newBounds.roundOut(&ibounds);
   1.112 +    glyph->fLeft = ibounds.fLeft;
   1.113 +    glyph->fTop = ibounds.fTop;
   1.114 +    glyph->fWidth = ibounds.width();
   1.115 +    glyph->fHeight = ibounds.height();
   1.116 +    glyph->fMaskFormat = SkMask::kARGB32_Format;
   1.117 +}
   1.118 +
   1.119 +void SkGScalerContext::generateImage(const SkGlyph& glyph) {
   1.120 +    if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
   1.121 +        SkPath path;
   1.122 +        fProxy->getPath(glyph, &path);
   1.123 +
   1.124 +        SkBitmap bm;
   1.125 +        bm.setConfig(SkBitmap::kARGB_8888_Config, glyph.fWidth, glyph.fHeight,
   1.126 +                     glyph.rowBytes());
   1.127 +        bm.setPixels(glyph.fImage);
   1.128 +        bm.eraseColor(0);
   1.129 +
   1.130 +        SkCanvas canvas(bm);
   1.131 +        canvas.translate(-SkIntToScalar(glyph.fLeft),
   1.132 +                         -SkIntToScalar(glyph.fTop));
   1.133 +        canvas.concat(fMatrix);
   1.134 +        canvas.drawPath(path, fFace->paint());
   1.135 +    } else {
   1.136 +        fProxy->getImage(glyph);
   1.137 +    }
   1.138 +}
   1.139 +
   1.140 +void SkGScalerContext::generatePath(const SkGlyph& glyph, SkPath* path) {
   1.141 +    fProxy->getPath(glyph, path);
   1.142 +    path->transform(fMatrix);
   1.143 +}
   1.144 +
   1.145 +void SkGScalerContext::generateFontMetrics(SkPaint::FontMetrics*,
   1.146 +                                           SkPaint::FontMetrics* metrics) {
   1.147 +    fProxy->getFontMetrics(metrics);
   1.148 +    if (metrics) {
   1.149 +        SkScalar scale = fMatrix.getScaleY();
   1.150 +        metrics->fTop = SkScalarMul(metrics->fTop, scale);
   1.151 +        metrics->fAscent = SkScalarMul(metrics->fAscent, scale);
   1.152 +        metrics->fDescent = SkScalarMul(metrics->fDescent, scale);
   1.153 +        metrics->fBottom = SkScalarMul(metrics->fBottom, scale);
   1.154 +        metrics->fLeading = SkScalarMul(metrics->fLeading, scale);
   1.155 +        metrics->fAvgCharWidth = SkScalarMul(metrics->fAvgCharWidth, scale);
   1.156 +        metrics->fXMin = SkScalarMul(metrics->fXMin, scale);
   1.157 +        metrics->fXMax = SkScalarMul(metrics->fXMax, scale);
   1.158 +        metrics->fXHeight = SkScalarMul(metrics->fXHeight, scale);
   1.159 +    }
   1.160 +}
   1.161 +
   1.162 +///////////////////////////////////////////////////////////////////////////////
   1.163 +
   1.164 +#include "SkTypefaceCache.h"
   1.165 +
   1.166 +SkGTypeface::SkGTypeface(SkTypeface* proxy, const SkPaint& paint)
   1.167 +    : SkTypeface(proxy->style(), SkTypefaceCache::NewFontID(), false)
   1.168 +    , fProxy(SkRef(proxy))
   1.169 +    , fPaint(paint) {}
   1.170 +
   1.171 +SkGTypeface::~SkGTypeface() {
   1.172 +    fProxy->unref();
   1.173 +}
   1.174 +
   1.175 +SkScalerContext* SkGTypeface::onCreateScalerContext(
   1.176 +                                            const SkDescriptor* desc) const {
   1.177 +    return SkNEW_ARGS(SkGScalerContext, (const_cast<SkGTypeface*>(this), desc));
   1.178 +}
   1.179 +
   1.180 +void SkGTypeface::onFilterRec(SkScalerContextRec* rec) const {
   1.181 +    fProxy->filterRec(rec);
   1.182 +    rec->setHinting(SkPaint::kNo_Hinting);
   1.183 +    rec->fMaskFormat = SkMask::kARGB32_Format;
   1.184 +}
   1.185 +
   1.186 +SkAdvancedTypefaceMetrics* SkGTypeface::onGetAdvancedTypefaceMetrics(
   1.187 +                                SkAdvancedTypefaceMetrics::PerGlyphInfo info,
   1.188 +                                const uint32_t* glyphIDs,
   1.189 +                                uint32_t glyphIDsCount) const {
   1.190 +    return fProxy->getAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
   1.191 +}
   1.192 +
   1.193 +SkStream* SkGTypeface::onOpenStream(int* ttcIndex) const {
   1.194 +    return fProxy->openStream(ttcIndex);
   1.195 +}
   1.196 +
   1.197 +void SkGTypeface::onGetFontDescriptor(SkFontDescriptor* desc,
   1.198 +                                      bool* isLocal) const {
   1.199 +    fProxy->getFontDescriptor(desc, isLocal);
   1.200 +}
   1.201 +
   1.202 +int SkGTypeface::onCharsToGlyphs(const void* chars, Encoding encoding,
   1.203 +                                 uint16_t glyphs[], int glyphCount) const {
   1.204 +    return fProxy->charsToGlyphs(chars, encoding, glyphs, glyphCount);
   1.205 +}
   1.206 +
   1.207 +int SkGTypeface::onCountGlyphs() const {
   1.208 +    return fProxy->countGlyphs();
   1.209 +}
   1.210 +
   1.211 +int SkGTypeface::onGetUPEM() const {
   1.212 +    return fProxy->getUnitsPerEm();
   1.213 +}
   1.214 +
   1.215 +SkTypeface::LocalizedStrings* SkGTypeface::onCreateFamilyNameIterator() const {
   1.216 +    return fProxy->createFamilyNameIterator();
   1.217 +}
   1.218 +
   1.219 +int SkGTypeface::onGetTableTags(SkFontTableTag tags[]) const {
   1.220 +    return fProxy->getTableTags(tags);
   1.221 +}
   1.222 +
   1.223 +size_t SkGTypeface::onGetTableData(SkFontTableTag tag, size_t offset,
   1.224 +                                    size_t length, void* data) const {
   1.225 +    return fProxy->getTableData(tag, offset, length, data);
   1.226 +}
   1.227 +
   1.228 +///////////////////////////////////////////////////////////////////////////////
   1.229 +
   1.230 +#if 0
   1.231 +// under construction -- defining a font purely in terms of skia primitives
   1.232 +// ala an SVG-font.
   1.233 +class SkGFont : public SkRefCnt {
   1.234 +public:
   1.235 +    virtual ~SkGFont();
   1.236 +
   1.237 +    int unicharToGlyph(SkUnichar) const;
   1.238 +
   1.239 +    int countGlyphs() const { return fCount; }
   1.240 +
   1.241 +    float getAdvance(int index) const {
   1.242 +        SkASSERT((unsigned)index < (unsigned)fCount);
   1.243 +        return fGlyphs[index].fAdvance;
   1.244 +    }
   1.245 +
   1.246 +    const SkPath& getPath(int index) const {
   1.247 +        SkASSERT((unsigned)index < (unsigned)fCount);
   1.248 +        return fGlyphs[index].fPath;
   1.249 +    }
   1.250 +
   1.251 +private:
   1.252 +    struct Glyph {
   1.253 +        SkUnichar   fUni;
   1.254 +        float       fAdvance;
   1.255 +        SkPath      fPath;
   1.256 +    };
   1.257 +    int fCount;
   1.258 +    Glyph* fGlyphs;
   1.259 +
   1.260 +    friend class SkGFontBuilder;
   1.261 +    SkGFont(int count, Glyph* array);
   1.262 +};
   1.263 +
   1.264 +class SkGFontBuilder {
   1.265 +public:
   1.266 +
   1.267 +};
   1.268 +#endif

mercurial