1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com */
7ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
8375ff85e96cf0f8438ea0b11be67e85474e42c29tomhudson@google.com#include "GrTextContext.h"
9e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org#include "GrContext.h"
10e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org
11e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org#include "SkAutoKern.h"
12e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org#include "SkGlyphCache.h"
13e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org#include "SkGr.h"
14ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
15cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.orgGrTextContext::GrTextContext(GrContext* context, const SkDeviceProperties& properties) :
16cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org                            fContext(context), fDeviceProperties(properties), fDrawTarget(NULL) {
17cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org}
18375ff85e96cf0f8438ea0b11be67e85474e42c29tomhudson@google.com
19cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.orgvoid GrTextContext::init(const GrPaint& grPaint, const SkPaint& skPaint) {
20cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org    const GrClipData* clipData = fContext->getClip();
21beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com
22fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    SkRect devConservativeBound;
23641f8b19a6799b6d73ac17b9c2d2f8a5e6f5ad4drobertphillips@google.com    clipData->fClipStack->getConservativeBounds(
24641f8b19a6799b6d73ac17b9c2d2f8a5e6f5ad4drobertphillips@google.com                                     -clipData->fOrigin.fX,
25641f8b19a6799b6d73ac17b9c2d2f8a5e6f5ad4drobertphillips@google.com                                     -clipData->fOrigin.fY,
26cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org                                     fContext->getRenderTarget()->width(),
27cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org                                     fContext->getRenderTarget()->height(),
28641f8b19a6799b6d73ac17b9c2d2f8a5e6f5ad4drobertphillips@google.com                                     &devConservativeBound);
29beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com
30641f8b19a6799b6d73ac17b9c2d2f8a5e6f5ad4drobertphillips@google.com    devConservativeBound.roundOut(&fClipRect);
31beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com
32cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org    fDrawTarget = fContext->getTextTarget();
33cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org
34cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org    fPaint = grPaint;
35cbbc481e918c0d32454455626235f67106f216a1commit-bot@chromium.org    fSkPaint = skPaint;
36ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com}
37e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org
38e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org//*** change to output positions?
39e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.orgvoid GrTextContext::MeasureText(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc,
40e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org                                const char text[], size_t byteLength, SkVector* stopVector) {
41e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    SkFixed     x = 0, y = 0;
42e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    const char* stop = text + byteLength;
43e5d7015cde3b6f2a3929b8e378822e3d7be223d4skia.committer@gmail.com
44e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    SkAutoKern  autokern;
45e5d7015cde3b6f2a3929b8e378822e3d7be223d4skia.committer@gmail.com
46e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    while (text < stop) {
47e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        // don't need x, y here, since all subpixel variants will have the
48e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        // same advance
49e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0);
50e5d7015cde3b6f2a3929b8e378822e3d7be223d4skia.committer@gmail.com
51e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        x += autokern.adjust(glyph) + glyph.fAdvanceX;
52e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        y += glyph.fAdvanceY;
53e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    }
54e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y));
55e5d7015cde3b6f2a3929b8e378822e3d7be223d4skia.committer@gmail.com
56e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    SkASSERT(text == stop);
57e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org}
58e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org
59e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.orgstatic void GlyphCacheAuxProc(void* data) {
60e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    GrFontScaler* scaler = (GrFontScaler*)data;
61e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    SkSafeUnref(scaler);
62e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org}
63e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org
64e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.orgGrFontScaler* GrTextContext::GetGrFontScaler(SkGlyphCache* cache) {
65e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    void* auxData;
66e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    GrFontScaler* scaler = NULL;
67e5d7015cde3b6f2a3929b8e378822e3d7be223d4skia.committer@gmail.com
68e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
69e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        scaler = (GrFontScaler*)auxData;
70e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    }
71e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    if (NULL == scaler) {
72e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
73e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org        cache->setAuxProc(GlyphCacheAuxProc, scaler);
74e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    }
75e5d7015cde3b6f2a3929b8e378822e3d7be223d4skia.committer@gmail.com
76e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org    return scaler;
77e8612d9a8d616c2ed0195421a0675e10b0c2d230commit-bot@chromium.org}
78