SkPathEffect.cpp revision 8a1c16ff38322f0210116fa7293eb8817c7e477e
1/* libs/graphics/sgl/SkPathEffect.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "SkPathEffect.h"
19#include "SkPath.h"
20#include "SkBuffer.h"
21
22//////////////////////////////////////////////////////////////////////////////////
23
24SkPairPathEffect::SkPairPathEffect(SkPathEffect* pe0, SkPathEffect* pe1)
25    : fPE0(pe0), fPE1(pe1)
26{
27    SkASSERT(pe0);
28    SkASSERT(pe1);
29    fPE0->ref();
30    fPE1->ref();
31}
32
33SkPairPathEffect::~SkPairPathEffect()
34{
35    fPE0->unref();
36    fPE1->unref();
37}
38
39/*
40    Format: [oe0-factory][pe1-factory][pe0-size][pe0-data][pe1-data]
41*/
42void SkPairPathEffect::flatten(SkFlattenableWriteBuffer& buffer)
43{
44    buffer.writeFlattenable(fPE0);
45    buffer.writeFlattenable(fPE1);
46}
47
48SkPairPathEffect::SkPairPathEffect(SkFlattenableReadBuffer& buffer)
49{
50    fPE0 = (SkPathEffect*)buffer.readFlattenable();
51    fPE1 = (SkPathEffect*)buffer.readFlattenable();
52}
53
54//////////////////////////////////////////////////////////////////////////////////
55
56bool SkComposePathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
57{
58    SkPath          tmp;
59    const SkPath*   ptr = &src;
60
61    if (fPE1->filterPath(&tmp, src, width))
62        ptr = &tmp;
63    return fPE0->filterPath(dst, *ptr, width);
64}
65
66//////////////////////////////////////////////////////////////////////////////////
67
68bool SkSumPathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
69{
70    // use bit-or so that we always call both, even if the first one succeeds
71    return  fPE0->filterPath(dst, src, width) | fPE1->filterPath(dst, src, width);
72}
73
74/////////////////////////////////////////////////////////////////////////////////
75
76#include "SkStroke.h"
77
78SkStrokePathEffect::SkStrokePathEffect(const SkPaint& paint)
79    : fWidth(paint.getStrokeWidth()), fMiter(paint.getStrokeMiter()),
80      fStyle(SkToU8(paint.getStyle())), fJoin(SkToU8(paint.getStrokeJoin())), fCap(SkToU8(paint.getStrokeCap()))
81{
82}
83
84SkStrokePathEffect::SkStrokePathEffect(SkScalar width, SkPaint::Style style, SkPaint::Join join, SkPaint::Cap cap, SkScalar miter)
85    : fWidth(width), fMiter(miter), fStyle(SkToU8(style)), fJoin(SkToU8(join)), fCap(SkToU8(cap))
86{
87    if (miter < 0)  // signal they want the default
88        fMiter = SK_DefaultMiterLimit;
89}
90
91bool SkStrokePathEffect::filterPath(SkPath* dst, const SkPath& src, SkScalar* width)
92{
93    if (fWidth < 0 || fStyle == SkPaint::kFill_Style)
94        return false;
95
96    if (fStyle == SkPaint::kStroke_Style && fWidth == 0)    // hairline
97    {
98        *width = 0;
99        return true;
100    }
101
102    SkStroke    stroke;
103
104    stroke.setWidth(fWidth);
105    stroke.setMiterLimit(fMiter);
106    stroke.setJoin((SkPaint::Join)fJoin);
107    stroke.setCap((SkPaint::Cap)fCap);
108    stroke.setDoFill(fStyle == SkPaint::kStrokeAndFill_Style);
109
110    stroke.strokePath(src, dst);
111    return true;
112}
113
114SkFlattenable::Factory SkStrokePathEffect::getFactory()
115{
116    return CreateProc;
117}
118
119SkFlattenable* SkStrokePathEffect::CreateProc(SkFlattenableReadBuffer& buffer)
120{
121    return SkNEW_ARGS(SkStrokePathEffect, (buffer));
122}
123
124void SkStrokePathEffect::flatten(SkFlattenableWriteBuffer& buffer)
125{
126    buffer.writeScalar(fWidth);
127    buffer.writeScalar(fMiter);
128    buffer.write8(fStyle);
129    buffer.write8(fJoin);
130    buffer.write8(fCap);
131}
132
133SkStrokePathEffect::SkStrokePathEffect(SkFlattenableReadBuffer& buffer)
134{
135    fWidth = buffer.readScalar();
136    fMiter = buffer.readScalar();
137    fStyle = buffer.readU8();
138    fJoin = buffer.readU8();
139    fCap = buffer.readU8();
140}
141
142
143