[poincare] Fix more specialized layouts

This commit is contained in:
Léa Saviot
2018-08-10 13:17:50 +02:00
parent c4b5d07ed4
commit 17daf19e05
12 changed files with 53 additions and 17 deletions

View File

@@ -20,8 +20,15 @@ objs += $(addprefix poincare/src/,\
left_parenthesis_layout_node.o\
left_square_bracket_layout_node.o\
matrix_layout_node.o\
nth_root_layout_node.o\
parenthesis_layout_node.o\
product_layout_node.o\
right_parenthesis_layout_node.o\
right_square_bracket_layout_node.o\
sequence_layout_node.o\
square_bracket_layout_node.o\
sum_layout_node.o\
vertical_offset_layout_node.o\
)
objs += $(addprefix poincare/src/,\

View File

@@ -12,6 +12,7 @@ class LayoutReference : public TreeByReference {
friend class GridLayoutNode;
friend class HorizontalLayoutNode;
friend class LayoutCursor;
friend class VerticalOffsetLayoutNode;
public:
using TreeByReference::operator==;
using TreeByReference::operator!=;

View File

@@ -17,7 +17,8 @@ public:
// TreeNode
static LeftSquareBracketLayoutNode * FailedAllocationStaticNode();
LeftSquareBracketLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); } size_t size() const override { return sizeof(LeftSquareBracketLayoutNode); }
LeftSquareBracketLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
size_t size() const override { return sizeof(LeftSquareBracketLayoutNode); }
#if TREE_LOG
const char * description() const override { return "LeftSquareBracketLayout"; }
#endif

View File

@@ -74,7 +74,7 @@ public:
addChildAtIndexInPlace(l4, 3, 3);
setDimensions(2, 2);
}
bool hasGreySquares() const { node()->hasGreySquares(); }
bool hasGreySquares() const { return node()->hasGreySquares(); }
void addGreySquares() { node()->addGreySquares(); }
void removeGreySquares() { node()->removeGreySquares(); }
void setDimensions(int rows, int columns);

View File

@@ -31,6 +31,8 @@ public:
bool hasUpperLeftIndex() const override { return m_hasIndex; }
// TreeNode
static NthRootLayoutNode * FailedAllocationStaticNode();
NthRootLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
size_t size() const override { return sizeof(NthRootLayoutNode); }
int numberOfChildren() const override { return m_hasIndex ? 2 : 1; }
#if TREE_LOG
@@ -72,10 +74,8 @@ public:
NthRootLayoutRef(LayoutRef radicand, LayoutRef index) : NthRootLayoutRef() {
replaceChildAtIndexInPlace(0, radicand);
addChildTreeAtIndex(index, 1, 1);
if (!node()->isAllocationFailure()) {
static_cast<NthRootLayoutNode *>(node())->setNumberOfChildren(2);
}
addChildAtIndexInPlace(index, 1, 1);
static_cast<NthRootLayoutNode *>(node())->setNumberOfChildren(2);
}
private:

View File

@@ -10,6 +10,8 @@ class ProductLayoutNode : public SequenceLayoutNode {
public:
using SequenceLayoutNode::SequenceLayoutNode;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
static ProductLayoutNode * FailedAllocationStaticNode();
ProductLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
size_t size() const override { return sizeof(ProductLayoutNode); }
#if TREE_LOG
const char * description() const override { return "ProductLayout"; }

View File

@@ -10,6 +10,8 @@ class SumLayoutNode : public SequenceLayoutNode {
public:
using SequenceLayoutNode::SequenceLayoutNode;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
static SumLayoutNode * FailedAllocationStaticNode();
SumLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
private:
void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override;
};

View File

@@ -1,5 +1,7 @@
#include <poincare/nth_root_layout_node.h>
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/layout_helper.h>
#include <poincare/serialization_helper.h>
#include <ion/charset.h>
#include <assert.h>
@@ -18,6 +20,12 @@ const uint8_t radixPixel[NthRootLayoutNode::k_leftRadixHeight][NthRootLayoutNode
{0xFF, 0xFF, 0xFF, 0xFF, 0x00},
};
NthRootLayoutNode * NthRootLayoutNode::FailedAllocationStaticNode() {
static AllocationFailureLayoutNode<NthRootLayoutNode> failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}
void NthRootLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) {
if (radicandLayout()
&& cursor->layoutNode() == radicandLayout()
@@ -162,11 +170,11 @@ int NthRootLayoutNode::serialize(char * buffer, int bufferSize, Preferences::Pri
&& (const_cast<NthRootLayoutNode *>(this))->indexLayout()
&& !(const_cast<NthRootLayoutNode *>(this))->indexLayout()->isEmpty())
{
return SerializationHelper::Prefix(SerializableRef(this), buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "root");
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "root");
}
// Case: squareRoot(x)
if (!m_hasIndex) {
return SerializationHelper::Prefix(SerializableRef(this), buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "\x91");
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "\x91");
}
// Case: root(x,empty)
// Write "'SquareRootSymbol'('radicandLayout')".

View File

@@ -1,4 +1,5 @@
#include <poincare/product_layout_node.h>
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/char_layout_node.h>
#include <poincare/horizontal_layout_node.h>
@@ -6,6 +7,12 @@ namespace Poincare {
static inline KDCoordinate max(KDCoordinate x, KDCoordinate y) { return x > y ? x : y; }
ProductLayoutNode * ProductLayoutNode::FailedAllocationStaticNode() {
static AllocationFailureLayoutNode<ProductLayoutNode> failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}
int ProductLayoutNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SequenceLayoutNode::writeDerivedClassInBuffer("product", buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits);
}

View File

@@ -13,8 +13,8 @@ constexpr char SequenceLayoutNode::k_nEquals[];
void SequenceLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) {
if (cursor->position() == LayoutCursor::Position::Left
&& ((lowerBoundLayout() && cursor->layoutReference() == lowerBoundLayout())
|| (upperBoundLayout() && cursor->layoutReference() == upperBoundLayout())))
&& ((lowerBoundLayout() && cursor->layoutNode() == lowerBoundLayout())
|| (upperBoundLayout() && cursor->layoutNode() == upperBoundLayout())))
{
// Case: Left of the bounds. Go Left of the sequence.
cursor->setLayoutNode(this);
@@ -22,7 +22,7 @@ void SequenceLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldReco
}
if (cursor->position() == LayoutCursor::Position::Left
&& argumentLayout()
&& cursor->layoutReference() == argumentLayout())
&& cursor->layoutNode() == argumentLayout())
{
// Case: Left of the argument. Go Right of the lower bound.
assert(lowerBoundLayout() != nullptr);
@@ -30,7 +30,7 @@ void SequenceLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldReco
cursor->setPosition(LayoutCursor::Position::Right);
return;
}
assert(cursor->layoutReference() == this);
assert(cursor->layoutNode() == this);
if (cursor->position() == LayoutCursor::Position::Right) {
// Case: Right. Go to the argument and move Left.
assert(argumentLayout() != nullptr);
@@ -48,8 +48,8 @@ void SequenceLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldReco
void SequenceLayoutNode::moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) {
if (cursor->position() == LayoutCursor::Position::Right
&& ((lowerBoundLayout() && cursor->layoutReference() == lowerBoundLayout())
|| (upperBoundLayout() && cursor->layoutReference() == upperBoundLayout())))
&& ((lowerBoundLayout() && cursor->layoutNode() == lowerBoundLayout())
|| (upperBoundLayout() && cursor->layoutNode() == upperBoundLayout())))
{
// Case: Right of the bounds. Go Left of the argument.
assert(argumentLayout() != nullptr);
@@ -59,13 +59,13 @@ void SequenceLayoutNode::moveCursorRight(LayoutCursor * cursor, bool * shouldRec
}
if (cursor->position() == LayoutCursor::Position::Right
&& argumentLayout()
&& cursor->layoutReference() == argumentLayout())
&& cursor->layoutNode() == argumentLayout())
{
// Case: Right of the argument. Go Right.
cursor->setLayoutNode(this);
return;
}
assert(cursor->layoutReference() == this);
assert(cursor->layoutNode() == this);
if (cursor->position() == LayoutCursor::Position::Left) {
// Case: Left. Go to the upper bound
assert(upperBoundLayout() != nullptr);

View File

@@ -1,4 +1,5 @@
#include <poincare/sum_layout_node.h>
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/char_layout_node.h>
#include <poincare/horizontal_layout_node.h>
@@ -25,6 +26,12 @@ const uint8_t symbolPixel[SumLayoutNode::k_symbolHeight][SumLayoutNode::k_symbol
};
SumLayoutNode * SumLayoutNode::FailedAllocationStaticNode() {
static AllocationFailureLayoutNode<SumLayoutNode> failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}
int SumLayoutNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SequenceLayoutNode::writeDerivedClassInBuffer("sum", buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits);
}

View File

@@ -1,4 +1,5 @@
#include <poincare/vertical_offset_layout_node.h>
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/empty_layout_node.h>
#include <poincare/layout_helper.h>
#include <poincare/left_parenthesis_layout_node.h>
@@ -190,7 +191,7 @@ int VerticalOffsetLayoutNode::serialize(char * buffer, int bufferSize, Preferenc
}
assert(m_type == Type::Superscript);
// If the layout is a superscript, write "^(indice)"
int numberOfChar = SerializationHelper::Prefix(SerializableRef(this), buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "^");
int numberOfChar = SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "^");
if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
// Add a multiplication if omitted.