Skip to content

Commit

Permalink
Add Cap and Join enums to Stroke class.
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen committed Feb 13, 2022
1 parent d0e6f5e commit fffc089
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/rendering/renderers/ShapeRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "rendering/graphics/Graphic.h"
#include "rendering/graphics/Shape.h"
#include "rendering/utils/PathUtil.h"
#include "rendering/utils/TGFXTypes.h"

namespace pag {

Expand All @@ -47,7 +48,7 @@ enum class PaintType { Fill, Stroke, GradientFill, GradientStroke };
*/
struct StrokePaint {
Stroke getStroke() const {
return Stroke(strokeWidth, lineCap, lineJoin, miterLimit);
return Stroke(strokeWidth, ToTGFXCap(lineCap), ToTGFXJoin(lineJoin), miterLimit);
}

float strokeWidth;
Expand Down
44 changes: 44 additions & 0 deletions src/rendering/utils/TGFXTypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "TGFXTypes.h"

namespace pag {
Stroke::Cap ToTGFXCap(Enum cap) {
switch (cap) {
case LineCap::Round:
return Stroke::Cap::Round;
case LineCap::Square:
return Stroke::Cap::Square;
default:
return Stroke::Cap::Butt;
}
}

Stroke::Join ToTGFXJoin(Enum join) {
switch (join) {
case LineJoin::Round:
return Stroke::Join::Round;
case LineJoin::Bevel:
return Stroke::Join::Bevel;
default:
return Stroke::Join::Miter;
}
}

} // namespace pag
28 changes: 28 additions & 0 deletions src/rendering/utils/TGFXTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "core/Stroke.h"
#include "pag/file.h"

namespace pag {
Stroke::Cap ToTGFXCap(Enum cap);

Stroke::Join ToTGFXJoin(Enum join);
} // namespace pag
47 changes: 42 additions & 5 deletions tgfx/include/core/Stroke.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,57 @@

#pragma once

#include "pag/file.h"

namespace pag {
/**
* Stroke controls options applied when stroking geometries (paths, glyphs).
*/
class Stroke {
public:
/**
* Cap draws at the beginning and end of an open path contour.
*/
enum class Cap {
/**
* No stroke extension.
*/
Butt,
/**
* Adds circle
*/
Round,
/**
* Adds square
*/
Square
};

/**
* Join specifies how corners are drawn when a shape is stroked. Join affects the four corners
* of a stroked rectangle, and the connected segments in a stroked path. Choose miter join to draw
* sharp corners. Choose round join to draw a circle with a radius equal to the stroke width on
* top of the corner. Choose bevel join to minimally connect the thick strokes.
*/
enum class Join {
/**
* Extends to miter limit.
*/
Miter,
/**
* Adds circle.
*/
Round,
/**
* Connects outside edges.
*/
Bevel
};

Stroke() = default;

/**
* Creates a new Stroke width specified options.
*/
explicit Stroke(float width, Enum cap = LineCap::Butt, Enum join = LineJoin::Miter,
explicit Stroke(float width, Cap cap = Cap::Butt, Join join = Join::Miter,
float miterLimit = 4.0f)
: width(width), cap(cap), join(join), miterLimit(miterLimit) {
}
Expand All @@ -44,12 +81,12 @@ class Stroke {
/**
* The geometry drawn at the beginning and end of strokes.
*/
Enum cap = LineCap::Butt;
Cap cap = Cap::Butt;

/**
* The geometry drawn at the corners of strokes.
*/
Enum join = LineJoin::Miter;
Join join = Join::Miter;

/**
* The limit at which a sharp corner is drawn beveled.
Expand Down
8 changes: 4 additions & 4 deletions tgfx/include/gpu/Paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,28 @@ class Paint {
/**
* Returns the geometry drawn at the beginning and end of strokes.
*/
Enum getLineCap() const {
Stroke::Cap getLineCap() const {
return stroke.cap;
}

/**
* Sets the geometry drawn at the beginning and end of strokes.
*/
void setLineCap(Enum cap) {
void setLineCap(Stroke::Cap cap) {
stroke.cap = cap;
}

/**
* Returns the geometry drawn at the corners of strokes.
*/
Enum getLineJoin() const {
Stroke::Join getLineJoin() const {
return stroke.join;
}

/**
* Sets the geometry drawn at the corners of strokes.
*/
void setLineJoin(Enum join) {
void setLineJoin(Stroke::Join join) {
stroke.join = join;
}

Expand Down
3 changes: 3 additions & 0 deletions tgfx/include/gpu/YUVTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

namespace pag {

/**
* YUVTexture wraps separate textures in the GPU backend for Y, U, and V planes.
*/
class YUVTexture : public Texture {
public:
/**
Expand Down
14 changes: 7 additions & 7 deletions tgfx/src/core/PathEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "core/PathEffect.h"
#include "core/PathRef.h"
#include "pag/file.h"
#include "core/Stroke.h"

namespace pag {
using namespace pk;
Expand Down Expand Up @@ -58,22 +58,22 @@ class StrokePathEffect : public PathEffect {
SkPaint paint = {};
};

static SkPaint::Cap ToSkLineCap(Enum cap) {
static SkPaint::Cap ToSkLineCap(Stroke::Cap cap) {
switch (cap) {
case LineCap::Round:
case Stroke::Cap::Round:
return SkPaint::kRound_Cap;
case LineCap::Square:
case Stroke::Cap::Square:
return SkPaint::kSquare_Cap;
default:
return SkPaint::kButt_Cap;
}
}

static SkPaint::Join ToSkLineJoin(Enum join) {
static SkPaint::Join ToSkLineJoin(Stroke::Join join) {
switch (join) {
case LineJoin::Round:
case Stroke::Join::Round:
return SkPaint::kRound_Join;
case LineJoin::Bevel:
case Stroke::Join::Bevel:
return SkPaint::kBevel_Join;
default:
return SkPaint::kMiter_Join;
Expand Down
1 change: 0 additions & 1 deletion tgfx/src/gpu/opengl/GLCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "gpu/ColorShader.h"
#include "gpu/TextureFragmentProcessor.h"
#include "gpu/TextureMaskFragmentProcessor.h"
#include "pag/file.h"

namespace pag {
GLCanvas::GLCanvas(Surface* surface) : Canvas(surface) {
Expand Down

0 comments on commit fffc089

Please sign in to comment.