Home | History | Annotate | Download | only in effects
      1 /*
      2  * Copyright 2006 The Android Open Source Project
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 
      9 #include "Sk1DPathEffect.h"
     10 #include "SkReadBuffer.h"
     11 #include "SkWriteBuffer.h"
     12 #include "SkPathMeasure.h"
     13 #include "SkStrokeRec.h"
     14 
     15 bool Sk1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
     16                                 SkStrokeRec*, const SkRect*) const {
     17     SkPathMeasure   meas(src, false);
     18     do {
     19         SkScalar    length = meas.getLength();
     20         SkScalar    distance = this->begin(length);
     21         while (distance < length) {
     22             SkScalar delta = this->next(dst, distance, meas);
     23             if (delta <= 0) {
     24                 break;
     25             }
     26             distance += delta;
     27         }
     28     } while (meas.nextContour());
     29     return true;
     30 }
     31 
     32 ///////////////////////////////////////////////////////////////////////////////
     33 
     34 SkPath1DPathEffect::SkPath1DPathEffect(const SkPath& path, SkScalar advance,
     35     SkScalar phase, Style style) : fPath(path)
     36 {
     37     SkASSERT(advance > 0 && !path.isEmpty());
     38     // cleanup their phase parameter, inverting it so that it becomes an
     39     // offset along the path (to match the interpretation in PostScript)
     40     if (phase < 0) {
     41         phase = -phase;
     42         if (phase > advance) {
     43             phase = SkScalarMod(phase, advance);
     44         }
     45     } else {
     46         if (phase > advance) {
     47             phase = SkScalarMod(phase, advance);
     48         }
     49         phase = advance - phase;
     50     }
     51     // now catch the edge case where phase == advance (within epsilon)
     52     if (phase >= advance) {
     53         phase = 0;
     54     }
     55     SkASSERT(phase >= 0);
     56 
     57     fAdvance = advance;
     58     fInitialOffset = phase;
     59 
     60     if ((unsigned)style > kMorph_Style) {
     61         SkDEBUGF(("SkPath1DPathEffect style enum out of range %d\n", style));
     62     }
     63     fStyle = style;
     64 }
     65 
     66 bool SkPath1DPathEffect::filterPath(SkPath* dst, const SkPath& src,
     67                             SkStrokeRec* rec, const SkRect* cullRect) const {
     68     if (fAdvance > 0) {
     69         rec->setFillStyle();
     70         return this->INHERITED::filterPath(dst, src, rec, cullRect);
     71     }
     72     return false;
     73 }
     74 
     75 static bool morphpoints(SkPoint dst[], const SkPoint src[], int count,
     76                         SkPathMeasure& meas, SkScalar dist) {
     77     for (int i = 0; i < count; i++) {
     78         SkPoint pos;
     79         SkVector tangent;
     80 
     81         SkScalar sx = src[i].fX;
     82         SkScalar sy = src[i].fY;
     83 
     84         if (!meas.getPosTan(dist + sx, &pos, &tangent)) {
     85             return false;
     86         }
     87 
     88         SkMatrix    matrix;
     89         SkPoint     pt;
     90 
     91         pt.set(sx, sy);
     92         matrix.setSinCos(tangent.fY, tangent.fX, 0, 0);
     93         matrix.preTranslate(-sx, 0);
     94         matrix.postTranslate(pos.fX, pos.fY);
     95         matrix.mapPoints(&dst[i], &pt, 1);
     96     }
     97     return true;
     98 }
     99 
    100 /*  TODO
    101 
    102 Need differentially more subdivisions when the follow-path is curvy. Not sure how to
    103 determine that, but we need it. I guess a cheap answer is let the caller tell us,
    104 but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out.
    105 */
    106 static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas,
    107                       SkScalar dist) {
    108     SkPath::Iter    iter(src, false);
    109     SkPoint         srcP[4], dstP[3];
    110     SkPath::Verb    verb;
    111 
    112     while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) {
    113         switch (verb) {
    114             case SkPath::kMove_Verb:
    115                 if (morphpoints(dstP, srcP, 1, meas, dist)) {
    116                     dst->moveTo(dstP[0]);
    117                 }
    118                 break;
    119             case SkPath::kLine_Verb:
    120                 srcP[2] = srcP[1];
    121                 srcP[1].set(SkScalarAve(srcP[0].fX, srcP[2].fX),
    122                             SkScalarAve(srcP[0].fY, srcP[2].fY));
    123                 // fall through to quad
    124             case SkPath::kQuad_Verb:
    125                 if (morphpoints(dstP, &srcP[1], 2, meas, dist)) {
    126                     dst->quadTo(dstP[0], dstP[1]);
    127                 }
    128                 break;
    129             case SkPath::kCubic_Verb:
    130                 if (morphpoints(dstP, &srcP[1], 3, meas, dist)) {
    131                     dst->cubicTo(dstP[0], dstP[1], dstP[2]);
    132                 }
    133                 break;
    134             case SkPath::kClose_Verb:
    135                 dst->close();
    136                 break;
    137             default:
    138                 SkDEBUGFAIL("unknown verb");
    139                 break;
    140         }
    141     }
    142 }
    143 
    144 SkScalar SkPath1DPathEffect::begin(SkScalar contourLength) const {
    145     return fInitialOffset;
    146 }
    147 
    148 sk_sp<SkFlattenable> SkPath1DPathEffect::CreateProc(SkReadBuffer& buffer) {
    149     SkScalar advance = buffer.readScalar();
    150     if (advance > 0) {
    151         SkPath path;
    152         buffer.readPath(&path);
    153         SkScalar phase = buffer.readScalar();
    154         Style style = (Style)buffer.readUInt();
    155         return SkPath1DPathEffect::Make(path, advance, phase, style);
    156     }
    157     return nullptr;
    158 }
    159 
    160 void SkPath1DPathEffect::flatten(SkWriteBuffer& buffer) const {
    161     buffer.writeScalar(fAdvance);
    162     if (fAdvance > 0) {
    163         buffer.writePath(fPath);
    164         buffer.writeScalar(fInitialOffset);
    165         buffer.writeUInt(fStyle);
    166     }
    167 }
    168 
    169 SkScalar SkPath1DPathEffect::next(SkPath* dst, SkScalar distance,
    170                                   SkPathMeasure& meas) const {
    171     switch (fStyle) {
    172         case kTranslate_Style: {
    173             SkPoint pos;
    174             if (meas.getPosTan(distance, &pos, nullptr)) {
    175                 dst->addPath(fPath, pos.fX, pos.fY);
    176             }
    177         } break;
    178         case kRotate_Style: {
    179             SkMatrix matrix;
    180             if (meas.getMatrix(distance, &matrix)) {
    181                 dst->addPath(fPath, matrix);
    182             }
    183         } break;
    184         case kMorph_Style:
    185             morphpath(dst, fPath, meas, distance);
    186             break;
    187         default:
    188             SkDEBUGFAIL("unknown Style enum");
    189             break;
    190     }
    191     return fAdvance;
    192 }
    193 
    194 
    195 #ifndef SK_IGNORE_TO_STRING
    196 void SkPath1DPathEffect::toString(SkString* str) const {
    197     str->appendf("SkPath1DPathEffect: (");
    198     // TODO: add path and style
    199     str->appendf("advance: %.2f phase %.2f", fAdvance, fInitialOffset);
    200     str->appendf(")");
    201 }
    202 #endif
    203 
    204 ///////////////////////////////////////////////////////////////////////////////////////////////////
    205 
    206 sk_sp<SkPathEffect> SkPath1DPathEffect::Make(const SkPath& path, SkScalar advance, SkScalar phase,
    207                                              Style style) {
    208     if (advance <= 0 || !SkScalarIsFinite(advance) ||
    209         !SkScalarIsFinite(phase) || path.isEmpty()) {
    210         return nullptr;
    211     }
    212     return sk_sp<SkPathEffect>(new SkPath1DPathEffect(path, advance, phase, style));
    213 }
    214