mirror of
https://gitee.com/openharmony/arkui_ace_engine
synced 2024-11-23 23:21:05 +00:00
Svg内容裁剪
Signed-off-by: aryawang <wangcaihui@huawei.com> Change-Id: Iaaeda982e881f34db7fd47b770616b60e16690d1
This commit is contained in:
parent
058035cdae
commit
dd16d35c91
@ -47,5 +47,7 @@ build_component_ng("svg_ng") {
|
||||
"parse/svg_use.cpp",
|
||||
"svg_context.cpp",
|
||||
"svg_dom.cpp",
|
||||
"svg_fit_convertor.cpp",
|
||||
"svg_utils.cpp",
|
||||
]
|
||||
}
|
||||
|
@ -387,6 +387,8 @@ void SvgNode::Draw(RSCanvas& canvas, const Size& viewPort, const std::optional<C
|
||||
#endif
|
||||
if (!hrefClipPath_.empty()) {
|
||||
OnClipPath(canvas, viewPort);
|
||||
} else if (isRootNode_) {
|
||||
AdjustContentAreaByViewBox(canvas, viewPort);
|
||||
}
|
||||
if (!transform_.empty() || !animateTransform_.empty()) {
|
||||
OnTransform(canvas, viewPort);
|
||||
|
@ -165,6 +165,10 @@ public:
|
||||
{
|
||||
return imagePath_;
|
||||
}
|
||||
void SetIsRootNode(bool isRoot)
|
||||
{
|
||||
isRootNode_ = isRoot;
|
||||
}
|
||||
protected:
|
||||
// override as need by derived class
|
||||
// called by function AppendChild
|
||||
@ -175,6 +179,7 @@ protected:
|
||||
virtual void OnDraw(RSCanvas& canvas, const Size& viewPort, const std::optional<Color>& color) {}
|
||||
|
||||
virtual void OnDrawTraversed(RSCanvas& canvas, const Size& viewPort, const std::optional<Color>& color);
|
||||
virtual void AdjustContentAreaByViewBox(RSCanvas& canvas, const Size& viewPort) {}
|
||||
bool OnCanvas(RSCanvas& canvas);
|
||||
void OnClipPath(RSCanvas& canvas, const Size& viewPort);
|
||||
void OnFilter(RSCanvas& canvas, const Size& viewPort);
|
||||
@ -186,7 +191,6 @@ protected:
|
||||
|
||||
std::optional<Gradient> GetGradient(const std::string& href);
|
||||
const Rect& GetRootViewBox() const;
|
||||
|
||||
virtual void PrepareAnimation(const RefPtr<SvgAnimation>& animate);
|
||||
// create animation that changes an attribute
|
||||
template<typename T>
|
||||
@ -235,7 +239,7 @@ protected:
|
||||
bool passStyle_ = true; // pass style attributes to child node, TAGS circle/path/line/... = false
|
||||
bool inheritStyle_ = true; // inherit style attributes from parent node, TAGS mask/defs/pattern/filter = false
|
||||
bool drawTraversed_ = true; // enable OnDraw, TAGS mask/defs/pattern/filter = false
|
||||
|
||||
bool isRootNode_ = false;
|
||||
#ifndef USE_ROSEN_DRAWING
|
||||
SkCanvas* skCanvas_ = nullptr;
|
||||
#else
|
||||
|
@ -24,6 +24,7 @@
|
||||
namespace OHOS::Ace::NG {
|
||||
namespace {
|
||||
const char DOM_SVG_SRC_VIEW_BOX[] = "viewBox";
|
||||
constexpr float HALF = 0.5f;
|
||||
}
|
||||
|
||||
SvgSvg::SvgSvg() : SvgGroup() {}
|
||||
@ -55,6 +56,53 @@ RSRecordingPath SvgSvg::AsPath(const Size& viewPort) const
|
||||
}
|
||||
#endif
|
||||
|
||||
void SvgSvg::AdjustContentAreaByViewBox(RSCanvas& canvas, const Size& viewPort)
|
||||
{
|
||||
auto svgSize = GetSize();
|
||||
auto viewBox = GetViewBox();
|
||||
if (LessOrEqual(viewBox.Width(), 0.0) || LessOrEqual(viewBox.Height(), 0.0)) {
|
||||
RSRect clipRect(0.0f, 0.0f, LessNotEqual(svgSize.Width(), 0.0) ? viewPort.Width() : svgSize.Width(),
|
||||
LessNotEqual(svgSize.Height(), 0.0) ? viewPort.Height() : svgSize.Height());
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
return;
|
||||
}
|
||||
if (LessNotEqual(svgSize.Width(), 0.0) && LessNotEqual(svgSize.Height(), 0.0)) {
|
||||
RSRect clipRect(0.0f, 0.0f, viewPort.Width(), viewPort.Height());
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
auto scale = std::min(viewPort.Width() / viewBox.Width(), viewPort.Height() / viewBox.Height());
|
||||
auto translateX = (viewPort.Width() - viewBox.Width() * scale) * HALF;
|
||||
auto translateY = (viewPort.Height() - viewBox.Height() * scale) * HALF;
|
||||
canvas.Translate(translateX, translateY);
|
||||
canvas.Scale(scale, scale);
|
||||
canvas.Translate(-1 * viewBox.Left(), -1 * viewBox.Top());
|
||||
return;
|
||||
}
|
||||
if (LessNotEqual(svgSize.Width(), 0.0)) {
|
||||
RSRect clipRect(0.0f, 0.0f, viewBox.Width() / viewBox.Height() * svgSize.Height(), svgSize.Height());
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
auto scaleY = svgSize.Height() / viewBox.Height();
|
||||
canvas.Scale(scaleY, scaleY);
|
||||
canvas.Translate(-1 * viewBox.Left(), -1 * viewBox.Top());
|
||||
return;
|
||||
}
|
||||
if (LessNotEqual(svgSize.Height(), 0.0)) {
|
||||
RSRect clipRect(0.0f, 0.0f, svgSize.Width(), viewBox.Height() / viewBox.Width() * svgSize.Width());
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
auto scaleX = svgSize.Width() / viewBox.Width();
|
||||
canvas.Scale(scaleX, scaleX);
|
||||
canvas.Translate(-1 * viewBox.Left(), -1 * viewBox.Top());
|
||||
return;
|
||||
}
|
||||
RSRect clipRect(0.0f, 0.0f, svgSize.Width(), svgSize.Height());
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
auto scale = std::min(svgSize.Width() / viewBox.Width(), svgSize.Height() / viewBox.Height());
|
||||
auto translateX = (svgSize.Width() - viewBox.Width() * scale) * HALF;
|
||||
auto translateY = (svgSize.Height() - viewBox.Height() * scale) * HALF;
|
||||
canvas.Translate(translateX, translateY);
|
||||
canvas.Scale(scale, scale);
|
||||
canvas.Translate(-1 * viewBox.Left(), -1 * viewBox.Top());
|
||||
}
|
||||
|
||||
Size SvgSvg::GetSize() const
|
||||
{
|
||||
return Size(svgAttr_.width.Value(), svgAttr_.height.Value());
|
||||
|
@ -39,6 +39,7 @@ public:
|
||||
|
||||
Rect GetViewBox() const;
|
||||
bool ParseAndSetSpecializedAttr(const std::string& name, const std::string& value) override;
|
||||
void AdjustContentAreaByViewBox(RSCanvas& canvas, const Size& viewPort) override;
|
||||
|
||||
private:
|
||||
SvgAttributes svgAttr_;
|
||||
|
@ -46,6 +46,8 @@
|
||||
#include "frameworks/core/components_ng/svg/parse/svg_stop.h"
|
||||
#include "frameworks/core/components_ng/svg/parse/svg_svg.h"
|
||||
#include "frameworks/core/components_ng/svg/parse/svg_use.h"
|
||||
#include "frameworks/core/components_ng/svg/svg_fit_convertor.h"
|
||||
#include "frameworks/core/components_ng/svg/svg_ulils.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
namespace {
|
||||
@ -295,6 +297,7 @@ void SvgDom::DrawImage(
|
||||
RSCanvas& canvas, const ImageFit& imageFit, const Size& layout)
|
||||
{
|
||||
CHECK_NULL_VOID(root_);
|
||||
root_->SetIsRootNode(true);
|
||||
canvas.Save();
|
||||
// viewBox scale and imageFit scale
|
||||
FitImage(canvas, imageFit, layout);
|
||||
@ -311,71 +314,20 @@ void SvgDom::DrawImage(
|
||||
void SvgDom::FitImage(RSCanvas& canvas, const ImageFit& imageFit, const Size& layout)
|
||||
{
|
||||
// scale svg to layout_ with ImageFit applied
|
||||
double scaleX = 1.0;
|
||||
double scaleY = 1.0;
|
||||
|
||||
float scaleViewBox = 1.0;
|
||||
float tx = 0.0;
|
||||
float ty = 0.0;
|
||||
constexpr float half = 0.5f;
|
||||
|
||||
float addTx = 0.0f;
|
||||
float addTy = 0.0f;
|
||||
|
||||
if (!layout.IsEmpty()) {
|
||||
layout_ = layout;
|
||||
}
|
||||
if (!layout_.IsEmpty() && (svgSize_.IsValid() && !svgSize_.IsInfinite())) {
|
||||
if (NearEqual(svgSize_.Width(), viewBox_.Width()) && NearEqual(svgSize_.Height(), viewBox_.Height()) &&
|
||||
NearZero(viewBox_.Left()) && NearZero(viewBox_.Top())) {
|
||||
needAdjustAlignment_ = true;
|
||||
}
|
||||
ApplyImageFit(imageFit, scaleX, scaleY, addTx, addTy);
|
||||
}
|
||||
/*
|
||||
* 1. viewBox_, svgSize_, and layout_ are on 3 different scales.
|
||||
* 2. Elements are painted in viewBox_ scale but displayed in layout_ scale.
|
||||
* 3. To center align svg content, we first align viewBox_ to svgSize_, then we align svgSize_ to layout_.
|
||||
* 4. Canvas is initially in layout_ scale, so transformation (tx, ty) needs to be in that scale, too.
|
||||
*/
|
||||
if (viewBox_.IsValid()) {
|
||||
if (svgSize_.IsValid() && !svgSize_.IsInfinite()) {
|
||||
// center align viewBox_ to svg, need to map viewBox_ to the scale of svgSize_
|
||||
scaleViewBox = std::min(svgSize_.Width() / viewBox_.Width(), svgSize_.Height() / viewBox_.Height());
|
||||
tx = svgSize_.Width() * half - (viewBox_.Width() * half + viewBox_.Left()) * scaleViewBox;
|
||||
ty = svgSize_.Height() * half - (viewBox_.Height() * half + viewBox_.Top()) * scaleViewBox;
|
||||
// map transformation to layout_ scale
|
||||
tx *= scaleX;
|
||||
ty *= scaleY;
|
||||
|
||||
// center align svg to layout container
|
||||
tx += (layout_.Width() - svgSize_.Width() * scaleX) * half;
|
||||
ty += (layout_.Height() - svgSize_.Height() * scaleY) * half;
|
||||
|
||||
tx += addTx;
|
||||
ty += addTy;
|
||||
} else if (!layout_.IsEmpty()) {
|
||||
// no svgSize_, center align viewBox to layout container
|
||||
scaleViewBox = std::min(layout_.Width() / viewBox_.Width(), layout_.Height() / viewBox_.Height());
|
||||
tx = layout_.Width() * half - (viewBox_.Width() * half + viewBox_.Left()) * scaleViewBox;
|
||||
ty = layout_.Height() * half - (viewBox_.Height() * half + viewBox_.Top()) * scaleViewBox;
|
||||
} else {
|
||||
LOGW("FitImage containerSize and svgSize is null");
|
||||
}
|
||||
}
|
||||
RSRect clipRect(0.0f, 0.0f, layout_.Width(), layout_.Height());
|
||||
if (radius_) {
|
||||
ImagePainterUtils::ClipRRect(canvas, clipRect, *radius_);
|
||||
} else {
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
}
|
||||
|
||||
canvas.Translate(tx, ty);
|
||||
|
||||
if (NearZero(scaleX) || NearZero(scaleViewBox) || NearZero(scaleY)) {
|
||||
return;
|
||||
if (!layout_.IsEmpty()) {
|
||||
Size svgContentSize;
|
||||
SvgUtils::CalculateSvgConentSize(svgContentSize, layout_, svgSize_, viewBox_);
|
||||
SvgFitConvertor::ApplyFit(imageFit, canvas, layout_, svgContentSize);
|
||||
}
|
||||
canvas.Scale(scaleX * scaleViewBox, scaleY * scaleViewBox);
|
||||
}
|
||||
|
||||
void SvgDom::FitViewPort(const Size& layout)
|
||||
|
157
frameworks/core/components_ng/svg/svg_fit_convertor.cpp
Normal file
157
frameworks/core/components_ng/svg/svg_fit_convertor.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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 "core/components_ng/svg/svg_fit_convertor.h"
|
||||
#include "core/common/ace_application_info.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
namespace {
|
||||
constexpr float HALF = 0.5f;
|
||||
constexpr double SCALE_BASE = 1.0;
|
||||
constexpr int32_t DEFAULT_FIT_INDEX = static_cast<int32_t>(ImageFit::COVER);
|
||||
}
|
||||
const std::vector<void (*)(RSCanvas& canvas,
|
||||
const Size& layoutSize, const Size& svgSize)> SvgFitConvertor::FIT_OPERATIONS = {
|
||||
ApplyFill,
|
||||
ApplyContain,
|
||||
ApplyCover,
|
||||
ApplyWidth,
|
||||
ApplyHeight,
|
||||
ApplyNone,
|
||||
ApplyScaleDown,
|
||||
ApplyAlignmentTopLeft,
|
||||
ApplyAlignmentTop,
|
||||
ApplyAlignmentTopRight,
|
||||
ApplyAlignmentCenterLeft,
|
||||
ApplyAlignmentCenter,
|
||||
ApplyAlignmentCenterRight,
|
||||
ApplyAlignmentBottomLeft,
|
||||
ApplyAlignmentBottom,
|
||||
ApplyAlignmentBottomRight
|
||||
};
|
||||
void SvgFitConvertor::ApplyFit(ImageFit imageFit, RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
if (NearZero(svgSize.Width()) || NearZero(svgSize.Height())) {
|
||||
DrawNothing(canvas);
|
||||
return;
|
||||
}
|
||||
auto fitIndex = static_cast<int32_t>(imageFit);
|
||||
fitIndex = fitIndex < static_cast<int32_t>(FIT_OPERATIONS.size()) ? fitIndex : DEFAULT_FIT_INDEX;
|
||||
FIT_OPERATIONS[fitIndex](canvas, layoutSize, svgSize);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyFill(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
canvas.Scale(layoutSize.Width() / svgSize.Width(), layoutSize.Height() / svgSize.Height());
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyContain(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize,
|
||||
std::min(layoutSize.Width() / svgSize.Width(), layoutSize.Height() / svgSize.Height()), Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyCover(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize,
|
||||
std::max(layoutSize.Width() / svgSize.Width(), layoutSize.Height() / svgSize.Height()), Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyWidth(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, layoutSize.Width() / svgSize.Width(), Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyHeight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, layoutSize.Height() / svgSize.Height(), Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyNone(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE, Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyScaleDown(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
auto scale = std::min(layoutSize.Width() / svgSize.Width(), layoutSize.Height() / svgSize.Height());
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, std::min(scale, SCALE_BASE), Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentTopLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE,
|
||||
AceApplicationInfo::GetInstance().IsRightToLeft() ? Alignment::TOP_RIGHT : Alignment::TOP_LEFT);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentTop(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE, Alignment::TOP_CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentTopRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE,
|
||||
AceApplicationInfo::GetInstance().IsRightToLeft() ? Alignment::TOP_LEFT : Alignment::TOP_RIGHT);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentCenterLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE,
|
||||
AceApplicationInfo::GetInstance().IsRightToLeft() ? Alignment::CENTER_RIGHT : Alignment::CENTER_LEFT);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentCenter(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE, Alignment::CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentCenterRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE,
|
||||
AceApplicationInfo::GetInstance().IsRightToLeft() ? Alignment::CENTER_LEFT : Alignment::CENTER_RIGHT);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentBottomLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE,
|
||||
AceApplicationInfo::GetInstance().IsRightToLeft() ? Alignment::BOTTOM_RIGHT : Alignment::BOTTOM_LEFT);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentBottom(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE, Alignment::BOTTOM_CENTER);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentBottomRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
AdjustContentFit(canvas, layoutSize, svgSize, SCALE_BASE,
|
||||
AceApplicationInfo::GetInstance().IsRightToLeft() ? Alignment::BOTTOM_LEFT : Alignment::BOTTOM_RIGHT);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::AdjustContentFit(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize,
|
||||
float svgScale, Alignment alignMent)
|
||||
{
|
||||
auto translateX = (1.0 + alignMent.GetHorizontal()) * (layoutSize.Width() - svgSize.Width() * svgScale) * HALF;
|
||||
auto translateY = (1.0 + alignMent.GetVertical()) * (layoutSize.Height() - svgSize.Height() * svgScale) * HALF;
|
||||
canvas.Translate(translateX, translateY);
|
||||
canvas.Scale(svgScale, svgScale);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::DrawNothing(RSCanvas& canvas)
|
||||
{
|
||||
RSRect clipRect(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
canvas.ClipRect(clipRect, RSClipOp::INTERSECT);
|
||||
}
|
||||
}
|
52
frameworks/core/components_ng/svg/svg_fit_convertor.h
Normal file
52
frameworks/core/components_ng/svg/svg_fit_convertor.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_IMAGE_FIT_CONVERTOR_H
|
||||
#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_IMAGE_FIT_CONVERTOR_H
|
||||
|
||||
#include "core/components_ng/render/drawing.h"
|
||||
#include "core/components/common/properties/alignment.h"
|
||||
#include "core/components/common/layout/constants.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
class SvgFitConvertor {
|
||||
public:
|
||||
SvgFitConvertor();
|
||||
~SvgFitConvertor();
|
||||
|
||||
static void ApplyFit(ImageFit imageFit, RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyFill(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyContain(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyCover(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyWidth(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyHeight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyNone(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyScaleDown(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentTopLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentTop(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentTopRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentCenterLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentCenter(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentCenterRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentBottomLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentBottom(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void ApplyAlignmentBottomRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize);
|
||||
static void AdjustContentFit(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize,
|
||||
float svgScale, Alignment alignMent);
|
||||
static void DrawNothing(RSCanvas& canvas);
|
||||
static const std::vector<void (*)(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)> FIT_OPERATIONS;
|
||||
};
|
||||
}
|
||||
#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_IMAGE_FIT_CONVERTOR_H
|
30
frameworks/core/components_ng/svg/svg_ulils.h
Normal file
30
frameworks/core/components_ng/svg/svg_ulils.h
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_UTILS_H
|
||||
#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_UTILS_H
|
||||
|
||||
#include "base/geometry/rect.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
class SvgUtils {
|
||||
public:
|
||||
SvgUtils();
|
||||
~SvgUtils();
|
||||
static void CalculateSvgConentSize(Size& svgContentSize, const Size& svgContainerSize,
|
||||
const Size& svgSize, const Rect& viewBox);
|
||||
};
|
||||
}
|
||||
#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SVG_SVG_UTILS_H
|
46
frameworks/core/components_ng/svg/svg_utils.cpp
Normal file
46
frameworks/core/components_ng/svg/svg_utils.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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 "core/components_ng/svg/svg_ulils.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
|
||||
void SvgUtils::CalculateSvgConentSize(Size& svgContentSize, const Size& svgContainerSize,
|
||||
const Size& svgSize, const Rect& viewBox)
|
||||
{
|
||||
if (LessOrEqual(viewBox.Width(), 0.0) || LessOrEqual(viewBox.Height(), 0.0)) {
|
||||
svgContentSize.SetWidth(LessNotEqual(svgSize.Width(), 0.0) ? svgContainerSize.Width() : svgSize.Width());
|
||||
svgContentSize.SetHeight(LessNotEqual(svgSize.Height(), 0.0) ? svgContainerSize.Height() : svgSize.Height());
|
||||
return;
|
||||
}
|
||||
if (LessNotEqual(svgSize.Width(), 0.0) && LessNotEqual(svgSize.Height(), 0.0)) {
|
||||
svgContentSize.SetWidth(svgContainerSize.Width());
|
||||
svgContentSize.SetHeight(svgContainerSize.Height());
|
||||
return;
|
||||
}
|
||||
if (LessNotEqual(svgSize.Width(), 0.0)) {
|
||||
svgContentSize.SetWidth(viewBox.Width() / viewBox.Height() * svgSize.Height());
|
||||
svgContentSize.SetHeight(svgSize.Height());
|
||||
return;
|
||||
}
|
||||
if (LessNotEqual(svgSize.Height(), 0.0)) {
|
||||
svgContentSize.SetWidth(svgSize.Width());
|
||||
svgContentSize.SetHeight(viewBox.Height() / viewBox.Width() * svgSize.Width());
|
||||
return;
|
||||
}
|
||||
svgContentSize.SetWidth(svgSize.Width());
|
||||
svgContentSize.SetHeight(svgSize.Height());
|
||||
}
|
||||
}
|
81
test/mock/core/svg/mock_svg_fit_convertor.cpp
Normal file
81
test/mock/core/svg/mock_svg_fit_convertor.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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 "core/components_ng/svg/svg_fit_convertor.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
const std::vector<void (*)(RSCanvas& canvas,
|
||||
const Size& layoutSize, const Size& svgSize)> SvgFitConvertor::FIT_OPERATIONS = {
|
||||
ApplyFill,
|
||||
};
|
||||
void SvgFitConvertor::ApplyFit(ImageFit imageFit, RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{
|
||||
FIT_OPERATIONS[0](canvas, layoutSize, svgSize);
|
||||
}
|
||||
|
||||
void SvgFitConvertor::ApplyFill(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyContain(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyCover(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyWidth(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyHeight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyNone(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyScaleDown(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentTopLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentTop(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentTopRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentCenterLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentCenter(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentCenterRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentBottomLeft(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentBottom(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::ApplyAlignmentBottomRight(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::AdjustContentFit(RSCanvas& canvas, const Size& layoutSize, const Size& svgSize,
|
||||
float svgScale, Alignment alignMent)
|
||||
{}
|
||||
|
||||
void SvgFitConvertor::DrawNothing(RSCanvas& canvas)
|
||||
{}
|
||||
}
|
23
test/mock/core/svg/mock_svg_utils.cpp
Normal file
23
test/mock/core/svg/mock_svg_utils.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
||||
* 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 "core/components_ng/svg/svg_ulils.h"
|
||||
|
||||
namespace OHOS::Ace::NG {
|
||||
|
||||
void SvgUtils::CalculateSvgConentSize(Size& svgContentSize, const Size& svgContainerSize,
|
||||
const Size& svgSize, const Rect& viewBox)
|
||||
{}
|
||||
}
|
@ -86,6 +86,8 @@ ohos_unittest("parse_test_ng") {
|
||||
"$ace_root/test/mock/core/svg/mock_image_painter_utils.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_rosen_svg_painter.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_shared_transition_effect.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_svg_fit_convertor.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_svg_utils.cpp",
|
||||
"parse_test_ng.cpp",
|
||||
"parse_testtwo_ng.cpp",
|
||||
]
|
||||
@ -175,6 +177,8 @@ ohos_unittest("svg_dom_test_ng") {
|
||||
"$ace_root/test/mock/core/svg/mock_image_painter_utils.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_rosen_svg_painter.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_shared_transition_effect.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_svg_fit_convertor.cpp",
|
||||
"$ace_root/test/mock/core/svg/mock_svg_utils.cpp",
|
||||
"svg_dom_test_ng.cpp",
|
||||
]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user