michael@0: /* michael@0: * Copyright 2012 Google Inc. 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: #include "SkStrokeRec.h" michael@0: #include "SkPaintDefaults.h" michael@0: michael@0: // must be < 0, since ==0 means hairline, and >0 means normal stroke michael@0: #define kStrokeRec_FillStyleWidth (-SK_Scalar1) michael@0: michael@0: SkStrokeRec::SkStrokeRec(InitStyle s) { michael@0: fWidth = (kFill_InitStyle == s) ? kStrokeRec_FillStyleWidth : 0; michael@0: fMiterLimit = SkPaintDefaults_MiterLimit; michael@0: fCap = SkPaint::kDefault_Cap; michael@0: fJoin = SkPaint::kDefault_Join; michael@0: fStrokeAndFill = false; michael@0: } michael@0: michael@0: SkStrokeRec::SkStrokeRec(const SkStrokeRec& src) { michael@0: memcpy(this, &src, sizeof(src)); michael@0: } michael@0: michael@0: SkStrokeRec::SkStrokeRec(const SkPaint& paint) { michael@0: switch (paint.getStyle()) { michael@0: case SkPaint::kFill_Style: michael@0: fWidth = kStrokeRec_FillStyleWidth; michael@0: fStrokeAndFill = false; michael@0: break; michael@0: case SkPaint::kStroke_Style: michael@0: fWidth = paint.getStrokeWidth(); michael@0: fStrokeAndFill = false; michael@0: break; michael@0: case SkPaint::kStrokeAndFill_Style: michael@0: if (0 == paint.getStrokeWidth()) { michael@0: // hairline+fill == fill michael@0: fWidth = kStrokeRec_FillStyleWidth; michael@0: fStrokeAndFill = false; michael@0: } else { michael@0: fWidth = paint.getStrokeWidth(); michael@0: fStrokeAndFill = true; michael@0: } michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unknown paint style"); michael@0: // fall back on just fill michael@0: fWidth = kStrokeRec_FillStyleWidth; michael@0: fStrokeAndFill = false; michael@0: break; michael@0: } michael@0: michael@0: // copy these from the paint, regardless of our "style" michael@0: fMiterLimit = paint.getStrokeMiter(); michael@0: fCap = paint.getStrokeCap(); michael@0: fJoin = paint.getStrokeJoin(); michael@0: } michael@0: michael@0: SkStrokeRec::Style SkStrokeRec::getStyle() const { michael@0: if (fWidth < 0) { michael@0: return kFill_Style; michael@0: } else if (0 == fWidth) { michael@0: return kHairline_Style; michael@0: } else { michael@0: return fStrokeAndFill ? kStrokeAndFill_Style : kStroke_Style; michael@0: } michael@0: } michael@0: michael@0: void SkStrokeRec::setFillStyle() { michael@0: fWidth = kStrokeRec_FillStyleWidth; michael@0: fStrokeAndFill = false; michael@0: } michael@0: michael@0: void SkStrokeRec::setHairlineStyle() { michael@0: fWidth = 0; michael@0: fStrokeAndFill = false; michael@0: } michael@0: michael@0: void SkStrokeRec::setStrokeStyle(SkScalar width, bool strokeAndFill) { michael@0: if (strokeAndFill && (0 == width)) { michael@0: // hairline+fill == fill michael@0: this->setFillStyle(); michael@0: } else { michael@0: fWidth = width; michael@0: fStrokeAndFill = strokeAndFill; michael@0: } michael@0: } michael@0: michael@0: #include "SkStroke.h" michael@0: michael@0: bool SkStrokeRec::applyToPath(SkPath* dst, const SkPath& src) const { michael@0: if (fWidth <= 0) { // hairline or fill michael@0: return false; michael@0: } michael@0: michael@0: SkStroke stroker; michael@0: stroker.setCap(fCap); michael@0: stroker.setJoin(fJoin); michael@0: stroker.setMiterLimit(fMiterLimit); michael@0: stroker.setWidth(fWidth); michael@0: stroker.setDoFill(fStrokeAndFill); michael@0: stroker.strokePath(src, dst); michael@0: return true; michael@0: }