1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkStrokeRec.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* 1.5 + * Copyright 2012 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 "SkStrokeRec.h" 1.12 +#include "SkPaintDefaults.h" 1.13 + 1.14 +// must be < 0, since ==0 means hairline, and >0 means normal stroke 1.15 +#define kStrokeRec_FillStyleWidth (-SK_Scalar1) 1.16 + 1.17 +SkStrokeRec::SkStrokeRec(InitStyle s) { 1.18 + fWidth = (kFill_InitStyle == s) ? kStrokeRec_FillStyleWidth : 0; 1.19 + fMiterLimit = SkPaintDefaults_MiterLimit; 1.20 + fCap = SkPaint::kDefault_Cap; 1.21 + fJoin = SkPaint::kDefault_Join; 1.22 + fStrokeAndFill = false; 1.23 +} 1.24 + 1.25 +SkStrokeRec::SkStrokeRec(const SkStrokeRec& src) { 1.26 + memcpy(this, &src, sizeof(src)); 1.27 +} 1.28 + 1.29 +SkStrokeRec::SkStrokeRec(const SkPaint& paint) { 1.30 + switch (paint.getStyle()) { 1.31 + case SkPaint::kFill_Style: 1.32 + fWidth = kStrokeRec_FillStyleWidth; 1.33 + fStrokeAndFill = false; 1.34 + break; 1.35 + case SkPaint::kStroke_Style: 1.36 + fWidth = paint.getStrokeWidth(); 1.37 + fStrokeAndFill = false; 1.38 + break; 1.39 + case SkPaint::kStrokeAndFill_Style: 1.40 + if (0 == paint.getStrokeWidth()) { 1.41 + // hairline+fill == fill 1.42 + fWidth = kStrokeRec_FillStyleWidth; 1.43 + fStrokeAndFill = false; 1.44 + } else { 1.45 + fWidth = paint.getStrokeWidth(); 1.46 + fStrokeAndFill = true; 1.47 + } 1.48 + break; 1.49 + default: 1.50 + SkDEBUGFAIL("unknown paint style"); 1.51 + // fall back on just fill 1.52 + fWidth = kStrokeRec_FillStyleWidth; 1.53 + fStrokeAndFill = false; 1.54 + break; 1.55 + } 1.56 + 1.57 + // copy these from the paint, regardless of our "style" 1.58 + fMiterLimit = paint.getStrokeMiter(); 1.59 + fCap = paint.getStrokeCap(); 1.60 + fJoin = paint.getStrokeJoin(); 1.61 +} 1.62 + 1.63 +SkStrokeRec::Style SkStrokeRec::getStyle() const { 1.64 + if (fWidth < 0) { 1.65 + return kFill_Style; 1.66 + } else if (0 == fWidth) { 1.67 + return kHairline_Style; 1.68 + } else { 1.69 + return fStrokeAndFill ? kStrokeAndFill_Style : kStroke_Style; 1.70 + } 1.71 +} 1.72 + 1.73 +void SkStrokeRec::setFillStyle() { 1.74 + fWidth = kStrokeRec_FillStyleWidth; 1.75 + fStrokeAndFill = false; 1.76 +} 1.77 + 1.78 +void SkStrokeRec::setHairlineStyle() { 1.79 + fWidth = 0; 1.80 + fStrokeAndFill = false; 1.81 +} 1.82 + 1.83 +void SkStrokeRec::setStrokeStyle(SkScalar width, bool strokeAndFill) { 1.84 + if (strokeAndFill && (0 == width)) { 1.85 + // hairline+fill == fill 1.86 + this->setFillStyle(); 1.87 + } else { 1.88 + fWidth = width; 1.89 + fStrokeAndFill = strokeAndFill; 1.90 + } 1.91 +} 1.92 + 1.93 +#include "SkStroke.h" 1.94 + 1.95 +bool SkStrokeRec::applyToPath(SkPath* dst, const SkPath& src) const { 1.96 + if (fWidth <= 0) { // hairline or fill 1.97 + return false; 1.98 + } 1.99 + 1.100 + SkStroke stroker; 1.101 + stroker.setCap(fCap); 1.102 + stroker.setJoin(fJoin); 1.103 + stroker.setMiterLimit(fMiterLimit); 1.104 + stroker.setWidth(fWidth); 1.105 + stroker.setDoFill(fStrokeAndFill); 1.106 + stroker.strokePath(src, dst); 1.107 + return true; 1.108 +}