[poincare] Add Vector Norm Layout

Change-Id: Iae5bbeabaa0ca627ae6651f187d1a42b970df37d
This commit is contained in:
Hugo Saint-Vignes
2020-07-23 14:29:36 +02:00
committed by Émilie Feral
parent 8d2af7e77f
commit 3aa5e8bcd6
10 changed files with 89 additions and 15 deletions

View File

@@ -29,7 +29,7 @@ public:
private:
KDCoordinate widthMargin() const override { return 2; }
virtual KDCoordinate verticalExternMargin() const override { return 1; }
KDCoordinate verticalExternMargin() const override { return 1; }
bool renderTopBar() const override { return false; }
bool renderBottomBar() const override { return false; }
};

View File

@@ -49,6 +49,7 @@ private:
virtual KDCoordinate verticalExternMargin() const { return k_verticalExternMargin; }
virtual bool renderTopBar() const { return true; }
virtual bool renderBottomBar() const { return true; }
virtual bool renderDoubleBar() const { return false; }
void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor, Layout * selectionStart = nullptr, Layout * selectionEnd = nullptr, KDColor selectionColor = KDColorRed) override;
};

View File

@@ -39,6 +39,7 @@ public:
RightParenthesisLayout,
RightSquareBracketLayout,
SumLayout,
VectorNormLayout,
VerticalOffsetLayout
};

View File

@@ -25,8 +25,7 @@ private:
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
// Simplification
Expression shallowReduce(ReductionContext reductionContext) override;
LayoutShape leftLayoutShape() const override { return LayoutShape::MoreLetters; };
LayoutShape rightLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }
LayoutShape leftLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }
// Evaluation
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<float>(context, complexFormat, angleUnit); }
Evaluation<double> approximate(DoublePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<double>(context, complexFormat, angleUnit); }

View File

@@ -0,0 +1,46 @@
#ifndef POINCARE_VECTOR_NORM_LAYOUT_NODE_H
#define POINCARE_VECTOR_NORM_LAYOUT_NODE_H
#include <poincare/vector_norm.h>
#include <poincare/bracket_pair_layout.h>
#include <poincare/serialization_helper.h>
namespace Poincare {
class VectorNormLayoutNode final : public BracketPairLayoutNode {
public:
using BracketPairLayoutNode::BracketPairLayoutNode;
// Layout
Type type() const override { return Type::VectorNormLayout; }
// SerializationHelperInterface
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, VectorNorm::s_functionHelper.name(), true);
}
// TreeNode
size_t size() const override { return sizeof(VectorNormLayoutNode); }
#if POINCARE_TREE_LOG
void logNodeName(std::ostream & stream) const override {
stream << "VectorNormLayout";
}
#endif
private:
KDCoordinate widthMargin() const override { return 2; }
KDCoordinate verticalExternMargin() const override { return 1; }
bool renderTopBar() const override { return false; }
bool renderBottomBar() const override { return false; }
bool renderDoubleBar() const override { return true; }
};
class VectorNormLayout final : public Layout {
public:
static VectorNormLayout Builder(Layout child) { return TreeHandle::FixedArityBuilder<VectorNormLayout, VectorNormLayoutNode>({child}); }
VectorNormLayout() = delete;
};
}
#endif

View File

@@ -24,6 +24,7 @@
#include <poincare/right_square_bracket_layout.h>
#include <poincare/square_bracket_layout.h>
#include <poincare/sum_layout.h>
#include <poincare/vector_norm_layout.h>
#include <poincare/vertical_offset_layout.h>
#endif

View File

@@ -114,22 +114,41 @@ int BracketPairLayoutNode::serialize(char * buffer, int bufferSize, Preferences:
void BracketPairLayoutNode::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor, Layout * selectionStart, Layout * selectionEnd, KDColor selectionColor) {
KDSize childSize = childLayout()->layoutSize();
KDCoordinate verticalBarHeight = childSize.height() + 2*k_verticalMargin;
ctx->fillRect(KDRect(p.x()+externWidthMargin(), p.y()+verticalExternMargin(), k_lineThickness, verticalBarHeight), expressionColor);
ctx->fillRect(KDRect(p.x()+externWidthMargin()+childSize.width()+2*widthMargin()+k_lineThickness, p.y()+verticalExternMargin(), k_lineThickness, verticalBarHeight), expressionColor);
KDCoordinate horizontalPosition = p.x() + externWidthMargin();
KDCoordinate verticalPosition = p.y() + verticalExternMargin();
if (renderDoubleBar()) {
ctx->fillRect(KDRect(horizontalPosition, verticalPosition, k_lineThickness, verticalBarHeight), expressionColor);
horizontalPosition += k_lineThickness + widthMargin();
}
ctx->fillRect(KDRect(horizontalPosition, verticalPosition, k_lineThickness, verticalBarHeight), expressionColor);
if (renderTopBar()) {
ctx->fillRect(KDRect(p.x()+externWidthMargin(), p.y()+verticalExternMargin(), k_bracketWidth, k_lineThickness), expressionColor);
ctx->fillRect(KDRect(p.x()+externWidthMargin()+2*k_lineThickness+childSize.width()+2*widthMargin()-k_bracketWidth, p.y() + verticalExternMargin(), k_bracketWidth, k_lineThickness), expressionColor);
ctx->fillRect(KDRect(horizontalPosition, verticalPosition, k_bracketWidth, k_lineThickness), expressionColor);
}
if (renderBottomBar()) {
ctx->fillRect(KDRect(p.x()+externWidthMargin(), p.y()+verticalExternMargin()+verticalBarHeight-k_lineThickness, k_bracketWidth, k_lineThickness), expressionColor);
ctx->fillRect(KDRect(p.x()+externWidthMargin()+2*k_lineThickness+childSize.width()+2*widthMargin()-k_bracketWidth, p.y()+verticalExternMargin()+verticalBarHeight-k_lineThickness, k_bracketWidth, k_lineThickness), expressionColor);
ctx->fillRect(KDRect(horizontalPosition, verticalPosition + verticalBarHeight - k_lineThickness, k_bracketWidth, k_lineThickness), expressionColor);
}
horizontalPosition += k_lineThickness + widthMargin() + childSize.width() + widthMargin();
ctx->fillRect(KDRect(horizontalPosition, verticalPosition, k_lineThickness, verticalBarHeight), expressionColor);
horizontalPosition += k_lineThickness;
if (renderTopBar()) {
ctx->fillRect(KDRect(horizontalPosition - k_bracketWidth, verticalPosition, k_bracketWidth, k_lineThickness), expressionColor);
}
if (renderBottomBar()) {
ctx->fillRect(KDRect(horizontalPosition - k_bracketWidth, verticalPosition + verticalBarHeight - k_lineThickness, k_bracketWidth, k_lineThickness), expressionColor);
}
if (renderDoubleBar()) {
horizontalPosition += widthMargin();
ctx->fillRect(KDRect(horizontalPosition, verticalPosition, k_lineThickness, verticalBarHeight), expressionColor);
}
}
KDSize BracketPairLayoutNode::computeSize() {
KDSize childSize = childLayout()->layoutSize();
KDSize result = KDSize(childSize.width() + 2*externWidthMargin() + 2*widthMargin() + 2*k_lineThickness, childSize.height() + 2 * k_verticalMargin + 2*verticalExternMargin());
return result;
KDCoordinate horizontalCoordinates = childSize.width() + 2*externWidthMargin() + 2*widthMargin() + 2*k_lineThickness;
if (renderDoubleBar()) {
horizontalCoordinates += 2*k_lineThickness + 2*widthMargin();
}
return KDSize(horizontalCoordinates, childSize.height() + 2 * k_verticalMargin + 2*verticalExternMargin());
}
KDCoordinate BracketPairLayoutNode::computeBaseline() {
@@ -138,7 +157,11 @@ KDCoordinate BracketPairLayoutNode::computeBaseline() {
KDPoint BracketPairLayoutNode::positionOfChild(LayoutNode * child) {
assert(child == childLayout());
return KDPoint(widthMargin() + externWidthMargin() + k_lineThickness, k_verticalMargin + verticalExternMargin());
KDCoordinate horizontalCoordinates = widthMargin() + externWidthMargin() + k_lineThickness;
if (renderDoubleBar()) {
horizontalCoordinates += k_lineThickness + widthMargin();
}
return KDPoint(horizontalCoordinates, k_verticalMargin + verticalExternMargin());
}
}

View File

@@ -217,7 +217,8 @@ int HorizontalLayoutNode::serializeChildrenBetweenIndexes(char * buffer, int buf
|| (nextChildType == LayoutNode::Type::NthRootLayout
&& !static_cast<NthRootLayoutNode *>(nextChild)->isSquareRoot())
|| nextChildType == LayoutNode::Type::ProductLayout
|| nextChildType == LayoutNode::Type::SumLayout)
|| nextChildType == LayoutNode::Type::SumLayout
|| nextChildType == LayoutNode::Type::VectorNormLayout)
&& currentChild->canBeOmittedMultiplicationLeftFactor())
{
assert(nextChildType != LayoutNode::Type::HorizontalLayout);

View File

@@ -372,6 +372,7 @@ template Unreal TreeHandle::FixedArityBuilder<Unreal, UnrealNode>(const Tuple &)
template VectorCross TreeHandle::FixedArityBuilder<VectorCross, VectorCrossNode>(const Tuple &);
template VectorDot TreeHandle::FixedArityBuilder<VectorDot, VectorDotNode>(const Tuple &);
template VectorNorm TreeHandle::FixedArityBuilder<VectorNorm, VectorNormNode>(const Tuple &);
template VectorNormLayout TreeHandle::FixedArityBuilder<VectorNormLayout, VectorNormLayoutNode>(const Tuple &);
template MatrixLayout TreeHandle::NAryBuilder<MatrixLayout, MatrixLayoutNode>(const Tuple &);
}

View File

@@ -1,9 +1,10 @@
#include <poincare/vector_norm.h>
#include <poincare/addition.h>
#include <poincare/layout_helper.h>
#include <poincare/matrix.h>
#include <poincare/serialization_helper.h>
#include <poincare/undefined.h>
#include <poincare/vector_norm.h>
#include <poincare/vector_norm_layout.h>
namespace Poincare {
@@ -16,7 +17,7 @@ Expression VectorNormNode::shallowReduce(ReductionContext reductionContext) {
}
Layout VectorNormNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return LayoutHelper::Prefix(VectorNorm(this), floatDisplayMode, numberOfSignificantDigits, VectorNorm::s_functionHelper.name());
return VectorNormLayout::Builder(childAtIndex(0)->createLayout(floatDisplayMode, numberOfSignificantDigits));
}
int VectorNormNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {