Skip to content

Commit

Permalink
Merge branch 'main' into feature/yg_svg_xml_part
Browse files Browse the repository at this point in the history
  • Loading branch information
YGaurora authored Nov 1, 2024
2 parents 2e45e3c + 7c651fa commit 3881855
Show file tree
Hide file tree
Showing 75 changed files with 1,942 additions and 249 deletions.
30 changes: 21 additions & 9 deletions include/tgfx/core/PathEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ namespace tgfx {
*/
class PathEffect {
public:
/**
* Creates a stroke path effect with the specified stroke options.
*/
static std::unique_ptr<PathEffect> MakeStroke(const Stroke* stroke);

/**
* Creates a dash path effect.
* @param intervals array containing an even number of entries (>=2), with the even indices
* specifying the length of "on" intervals, and the odd indices specifying the length of "off"
* intervals.
* @param count number of elements in the intervals array
* @param phase offset into the intervals array (mod the sum of all of the intervals).
* @param count number of elements in the interval array
* @param phase offset into the interval array (mod the sum of all of the intervals).
*/
static std::unique_ptr<PathEffect> MakeDash(const float intervals[], int count, float phase);

Expand All @@ -49,12 +44,29 @@ class PathEffect {
*/
static std::unique_ptr<PathEffect> MakeCorner(float radius);

/**
* Creates a path effect that returns a segment of the input path based on the given start and
* stop "t" values. The startT and stopT values must be between 0 and 1, inclusive. If they are
* outside this range, they will be clamped to the nearest valid value. If either value is NaN,
* nullptr will be returned.
* @param startT The starting point of the path segment to be returned.
* @param stopT The ending point of the path segment to be returned.
*/
static std::unique_ptr<PathEffect> MakeTrim(float startT, float stopT);

virtual ~PathEffect() = default;

/**
* Applies this effect to specified path. Returns false if this effect cannot be applied, and
* Applies this effect to the given path. Returns false if this effect cannot be applied, and
* leaves this path unchanged.
*/
virtual bool applyTo(Path* path) const = 0;
virtual bool filterPath(Path* path) const = 0;

/**
* Returns the bounds of the path after applying this effect.
*/
virtual Rect filterBounds(const Rect& rect) const {
return rect;
}
};
} // namespace tgfx
6 changes: 4 additions & 2 deletions include/tgfx/core/Picture.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ class Picture {
~Picture();

/**
* Returns the bounding box of the Picture when drawn with the given Matrix.
* Returns the bounding box of the Picture when drawn with the given Matrix. Since the Picture
* may contain text drawing commands, and text outlines can change with different scale factors,
* it's best to use the final drawing matrix for calculating the bounds to ensure accuracy.
*/
Rect getBounds(const Matrix& matrix = Matrix::I()) const;
Rect getBounds(const Matrix* matrix = nullptr) const;

/**
* Replays the drawing commands on the specified canvas. In the case that the commands are
Expand Down
8 changes: 8 additions & 0 deletions include/tgfx/core/Stroke.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#pragma once

#include "tgfx/core/Path.h"

namespace tgfx {
/**
* LineCap draws at the beginning and end of an open path contour.
Expand Down Expand Up @@ -73,6 +75,12 @@ class Stroke {
: width(width), cap(cap), join(join), miterLimit(miterLimit) {
}

/**
* Applies this stroke to the given path. Returns false if this stroke cannot be applied, and
* leaves the path unchanged.
*/
bool applyToPath(Path* path) const;

/**
* The thickness of the pen used to outline the paths or glyphs.
*/
Expand Down
15 changes: 15 additions & 0 deletions include/tgfx/core/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,21 @@ class Surface {
*/
bool readPixels(const ImageInfo& dstInfo, void* dstPixels, int srcX = 0, int srcY = 0);

/**
* Returns the unique ID of the Surface. The ID is unique among all Surfaces.
*/
uint32_t uniqueID() const {
return _uniqueID;
}

private:
/**
* Returns the version of the content. The version is changed when the content is changed.
* The initial version is 1;
*/
uint32_t contentVersion() const;

uint32_t _uniqueID = 0;
std::shared_ptr<RenderTargetProxy> renderTargetProxy = nullptr;
uint32_t _renderFlags = 0;
RenderContext* renderContext = nullptr;
Expand All @@ -184,5 +198,6 @@ class Surface {

friend class RenderContext;
friend class PictureImage;
friend class DisplayList;
};
} // namespace tgfx
9 changes: 6 additions & 3 deletions include/tgfx/core/TextBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TextBlob {
public:
/**
* Creates a new TextBlob from the given text. The text must be in utf-8 encoding. This function
* uses the default character-to-glyph mapping from the Typeface in font. It does not perform
* uses the default character-to-glyph mapping from the Typeface in font. It doesn’t perform
* typeface fallback for characters not found in the Typeface. Glyphs are positioned based on
* their default advances. Returns nullptr if the text is empty or fails to map any characters to
* glyphs.
Expand Down Expand Up @@ -60,9 +60,11 @@ class TextBlob {
virtual ~TextBlob() = default;

/**
* Returns the bounding box of the TextBlob when drawn with the given Matrix.
* Returns the bounding box of the TextBlob when drawn with the given Matrix. Since text outlines
* may change with different scale factors, it's best to use the final drawing matrix for
* calculating the bounds to ensure accuracy.
*/
Rect getBounds(const Matrix& matrix = Matrix::I()) const;
Rect getBounds(const Matrix* matrix = nullptr) const;

private:
std::vector<std::shared_ptr<GlyphRunList>> glyphRunLists = {};
Expand All @@ -72,6 +74,7 @@ class TextBlob {
}

friend class Canvas;
friend class GlyphRunList;
friend class Mask;
};
} // namespace tgfx
13 changes: 8 additions & 5 deletions include/tgfx/layers/DisplayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ class DisplayList {
Layer* root() const;

/**
* Draws the display list to the given surface.
* @param surface The surface to draw the display list to.
* @param replaceAll If true, the surface will be cleared before drawing the display list.
* Otherwise, the display list will be drawn on top of the existing content.
* Renders the display list onto the given surface.
* @param surface The surface to render the display list on.
* @param replaceAll If true, the surface will be cleared before rendering the display list.
* Otherwise, the display list will be rendered over the existing content.
* @return True if the surface content was updated, otherwise false.
*/
void render(Surface* surface, bool replaceAll = true);
bool render(Surface* surface, bool replaceAll = true);

private:
std::shared_ptr<Layer> _root = nullptr;
uint32_t surfaceContentVersion = 0u;
uint32_t surfaceID = 0u;
};
} // namespace tgfx
19 changes: 16 additions & 3 deletions include/tgfx/layers/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Layer {
*/
static std::shared_ptr<Layer> Make();

virtual ~Layer() = default;
virtual ~Layer();

/**
* Returns the type of the layer.
Expand Down Expand Up @@ -428,12 +428,12 @@ class Layer {
* methods to convert points between these coordinate spaces.
* @param x The x coordinate to test it against the calling layer.
* @param y The y coordinate to test it against the calling layer.
* @param shapeFlag Whether to check the actual pixels of the layer (true) or just the bounding
* @param pixelHitTest Whether to check the actual pixels of the layer (true) or just the bounding
* box (false). Note that Image layers are always checked against their bounding box. You can draw
* image layers to a Surface and use the Surface::getColor() method to check the actual pixels.
* @return true if the layer overlaps or intersects with the specified point, false otherwise.
*/
bool hitTestPoint(float x, float y, bool shapeFlag = false);
bool hitTestPoint(float x, float y, bool pixelHitTest = false);

/**
* Draws the layer and all its children onto the given canvas. You can specify the alpha and blend
Expand Down Expand Up @@ -470,6 +470,16 @@ class Layer {
*/
virtual std::unique_ptr<LayerContent> onUpdateContent();

/**
* Attachs a property to this layer.
*/
void attachProperty(LayerProperty* property) const;

/**
* Detaches a property from this layer.
*/
void detachProperty(LayerProperty* property) const;

private:
/**
* Marks the layer's children as changed and needing to be redrawn.
Expand Down Expand Up @@ -504,6 +514,8 @@ class Layer {
std::shared_ptr<ImageFilter> getComposeFilter(
const std::vector<std::shared_ptr<LayerFilter>>& filters, float scale);

bool getLayersUnderPointInternal(float x, float y, std::vector<std::shared_ptr<Layer>>* results);

std::string _name;
float _alpha = 1.0f;
BlendMode _blendMode = BlendMode::SrcOver;
Expand All @@ -528,5 +540,6 @@ class Layer {
} bitFields = {};

friend class DisplayList;
friend class LayerProperty;
};
} // namespace tgfx
8 changes: 8 additions & 0 deletions include/tgfx/layers/LayerContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,13 @@ class LayerContent {
* Draws the content to the given canvas with the given paint.
*/
virtual void draw(Canvas* canvas, const Paint& paint) const = 0;

/**
* Checks if the layer content overlaps or intersects with the specified point (localX, localY).
* The localX and localY coordinates are in the layer's local coordinate space. If the
* pixelHitTest flag is true, it checks the actual pixels of the layer content; otherwise, it
* checks the bounding box.
*/
virtual bool hitTestPoint(float localX, float localY, bool pixelHitTest) = 0;
};
} // namespace tgfx
50 changes: 50 additions & 0 deletions include/tgfx/layers/LayerProperty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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 <memory>
#include <vector>

namespace tgfx {
class Layer;

/**
* A property of a layer that may change the content of the layer.
*/
class LayerProperty {
public:
virtual ~LayerProperty() = default;

protected:
/**
* Called when the property is invalidated. This method will notify the layer that the content
* of the layer should be invalidated.
*/
void invalidate();

private:
void attachToLayer(const Layer* layer);

void detachFromLayer(const Layer* layer);

std::vector<std::weak_ptr<Layer>> owners;

friend class Layer;
};

} // namespace tgfx
4 changes: 4 additions & 0 deletions include/tgfx/layers/LayerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ enum class LayerType {
* A layer displaying a simple text.
*/
Text,
/**
* A layer that fills its bounds with a solid color.
*/
Solid
};
} // namespace tgfx
16 changes: 8 additions & 8 deletions include/tgfx/layers/PathProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@
#pragma once

#include "tgfx/core/Path.h"
#include "tgfx/layers/LayerProperty.h"

namespace tgfx {
/**
* PathProvider is an interface for classes that generates a Path. It defers the acquisition of the
* Path until it is actually required, allowing the Path to be invalidated and regenerate if
* necessary. Note: PathProvider is not thread-safe and should be accessed from a single thread.
*/
class PathProvider {
class PathProvider : public LayerProperty {
public:
/**
* Creates a new PathProvider that wraps the given Path.
*/
static std::shared_ptr<PathProvider> Wrap(const Path& path);

virtual ~PathProvider() = default;

/**
* Returns the Path provided by this object.
*/
Expand All @@ -48,15 +47,16 @@ class PathProvider {
*/
explicit PathProvider(Path path);

virtual Path onGeneratePath();

/**
* Invalidates the path, causing it to be re-computed the next time it is requested.
* Marks the path as dirty and invalidates the cached path.
*/
void invalidate();

virtual Path onGeneratePath();
void invalidatePath();

private:
bool dirty = true;
Path path = {};

bool dirty = true;
};
} // namespace tgfx
2 changes: 2 additions & 0 deletions include/tgfx/layers/ShapeLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class ShapeLayer : public Layer {
*/
void setStrokeEnd(float end);

~ShapeLayer() override;

protected:
ShapeLayer() = default;

Expand Down
6 changes: 2 additions & 4 deletions include/tgfx/layers/ShapeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
#pragma once

#include "tgfx/core/Shader.h"
#include "tgfx/layers/LayerProperty.h"

namespace tgfx {
/**
* ShapeStyle specifies the source color(s) for what is being drawn in a shape layer. There are
* three types of ShapeStyle: SolidColor, Gradient, and ImagePattern. Note: All ShapeStyle objects
* are not thread-safe and should only be accessed from a single thread.
*/
class ShapeStyle {
public:
virtual ~ShapeStyle() = default;

class ShapeStyle : public LayerProperty {
protected:
/**
* Returns the current shader that will be used to draw the shape.
Expand Down
2 changes: 2 additions & 0 deletions include/tgfx/layers/SolidColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ class SolidColor : public ShapeStyle {

private:
Color _color;

friend class SolidLayer;
};
} // namespace tgfx
Loading

0 comments on commit 3881855

Please sign in to comment.