From cf0d22136ad34cc88794241a6789c6cdced1453a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Thu, 26 Jul 2018 17:40:16 +0200 Subject: [PATCH] [poincare] Remove dummy layout use in sum and product layouts --- poincare/include/poincare/sequence_layout_node.h | 6 +++--- poincare/src/product_layout_node.cpp | 10 ++++++---- poincare/src/sequence_layout_node.cpp | 10 ++++++---- poincare/src/sum_layout_node.cpp | 6 ++++-- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/poincare/include/poincare/sequence_layout_node.h b/poincare/include/poincare/sequence_layout_node.h index 179a875ee..36b6b8bbc 100644 --- a/poincare/include/poincare/sequence_layout_node.h +++ b/poincare/include/poincare/sequence_layout_node.h @@ -38,6 +38,9 @@ protected: constexpr static KDCoordinate k_boundHeightMargin = 2; constexpr static KDCoordinate k_argumentWidthMargin = 2; constexpr static KDText::FontSize k_fontSize = KDText::FontSize::Large; + constexpr static char k_nEquals[] = {'n', '=', 0}; + + KDSize lowerBoundSizeWithNEquals(); // LayoutNode void computeSize() override; @@ -57,10 +60,7 @@ protected: assert(numberOfChildren() == 3); return childAtIndex(2); } - void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override; -private: - KDSize lowerBoundSizeWithNEquals(); }; } diff --git a/poincare/src/product_layout_node.cpp b/poincare/src/product_layout_node.cpp index 5aab9bbe7..2821ae6c0 100644 --- a/poincare/src/product_layout_node.cpp +++ b/poincare/src/product_layout_node.cpp @@ -4,6 +4,8 @@ namespace Poincare { +static inline KDCoordinate max(KDCoordinate x, KDCoordinate y) { return x > y ? x : y; } + int ProductLayoutNode::writeTextInBuffer(char * buffer, int bufferSize, PrintFloat::Mode floatDisplayMode, int numberOfSignificantDigits) const { return SequenceLayoutNode::writeDerivedClassInBuffer("product", buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits); } @@ -11,16 +13,16 @@ int ProductLayoutNode::writeTextInBuffer(char * buffer, int bufferSize, PrintFlo void ProductLayoutNode::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) { // Compute sizes. KDSize upperBoundSize = upperBoundLayout()->layoutSize(); - KDSize lowerBoundSizeWithNEquals = HorizontalLayoutRef(CharLayoutRef('n'), CharLayoutRef('='), LayoutRef(lowerBoundLayout()).clone()).layoutSize(); + KDSize lowerBoundNEqualsSize = lowerBoundSizeWithNEquals(); // Render the Product symbol. - ctx->fillRect(KDRect(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundSizeWithNEquals.width()-k_symbolWidth)/2), + ctx->fillRect(KDRect(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundNEqualsSize.width()-k_symbolWidth)/2), p.y() + max(upperBoundSize.height()+k_boundHeightMargin, argumentLayout()->baseline()-(k_symbolHeight+1)/2), k_lineThickness, k_symbolHeight), expressionColor); - ctx->fillRect(KDRect(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundSizeWithNEquals.width()-k_symbolWidth)/2), + ctx->fillRect(KDRect(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundNEqualsSize.width()-k_symbolWidth)/2), p.y() + max(upperBoundSize.height()+k_boundHeightMargin, argumentLayout()->baseline()-(k_symbolHeight+1)/2), k_symbolWidth, k_lineThickness), expressionColor); - ctx->fillRect(KDRect(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundSizeWithNEquals.width()-k_symbolWidth)/2)+k_symbolWidth, + ctx->fillRect(KDRect(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundNEqualsSize.width()-k_symbolWidth)/2)+k_symbolWidth, p.y() + max(upperBoundSize.height()+k_boundHeightMargin, argumentLayout()->baseline()-(k_symbolHeight+1)/2), k_lineThickness, k_symbolHeight), expressionColor); diff --git a/poincare/src/sequence_layout_node.cpp b/poincare/src/sequence_layout_node.cpp index f1ce7241e..2553bfc04 100644 --- a/poincare/src/sequence_layout_node.cpp +++ b/poincare/src/sequence_layout_node.cpp @@ -9,6 +9,8 @@ namespace Poincare { static inline KDCoordinate max(KDCoordinate x, KDCoordinate y) { return x > y ? x : y; } +constexpr char SequenceLayoutNode::k_nEquals[]; + void SequenceLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) { if (cursor->position() == LayoutCursor::Position::Left && ((lowerBoundLayout() && cursor->layoutReference() == lowerBoundLayout()) @@ -148,7 +150,7 @@ void SequenceLayoutNode::computeBaseline() { KDPoint SequenceLayoutNode::positionOfChild(LayoutNode * l) { KDSize nEqualslowerBoundSize = lowerBoundSizeWithNEquals(); - KDSize nEqualsSize = KDText::stringSize("n=", k_fontSize); + KDSize nEqualsSize = KDText::stringSize(k_nEquals, k_fontSize); KDSize upperBoundSize = upperBoundLayout()->layoutSize(); KDCoordinate x = 0; KDCoordinate y = 0; @@ -212,8 +214,8 @@ int SequenceLayoutNode::writeDerivedClassInBuffer(const char * operatorName, cha void SequenceLayoutNode::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) { // Render the "n=" - KDPoint nEqualsPosition = positionOfChild(lowerBoundLayout()).translatedBy(KDPoint(-KDText::stringSize("n=", k_fontSize).width(), 0)); - ctx->drawString("n=", p.translatedBy(nEqualsPosition), k_fontSize, expressionColor, backgroundColor); + KDPoint nEqualsPosition = positionOfChild(lowerBoundLayout()).translatedBy(KDPoint(-KDText::stringSize(k_nEquals, k_fontSize).width(), 0)); + ctx->drawString(k_nEquals, p.translatedBy(nEqualsPosition), k_fontSize, expressionColor, backgroundColor); // Render the parentheses KDCoordinate argumentWithParenthesesHeight = ParenthesisLayoutNode::HeightGivenChildHeight(argumentLayout()->layoutSize().height()); @@ -232,7 +234,7 @@ void SequenceLayoutNode::render(KDContext * ctx, KDPoint p, KDColor expressionCo KDSize SequenceLayoutNode::lowerBoundSizeWithNEquals() { KDSize lowerBoundSize = lowerBoundLayout()->layoutSize(); - KDSize nEqualsSize = KDText::stringSize("n=", k_fontSize); + KDSize nEqualsSize = KDText::stringSize(k_nEquals, k_fontSize); return KDSize( nEqualsSize.width() + lowerBoundSize.width(), max(nEqualsSize.height(), lowerBoundSize.height())); diff --git a/poincare/src/sum_layout_node.cpp b/poincare/src/sum_layout_node.cpp index e578fa1b0..84ea878e4 100644 --- a/poincare/src/sum_layout_node.cpp +++ b/poincare/src/sum_layout_node.cpp @@ -4,6 +4,8 @@ namespace Poincare { +static inline KDCoordinate max(KDCoordinate x, KDCoordinate y) { return x > y ? x : y; } + const uint8_t symbolPixel[SumLayoutNode::k_symbolHeight][SumLayoutNode::k_symbolWidth] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, @@ -30,11 +32,11 @@ int SumLayoutNode::writeTextInBuffer(char * buffer, int bufferSize, PrintFloat:: void SumLayoutNode::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) { // Computes sizes. KDSize upperBoundSize = upperBoundLayout()->layoutSize(); - KDSize lowerBoundSizeWithNEquals = HorizontalLayoutRef(CharLayoutRef('n'), CharLayoutRef('='), LayoutRef(lowerBoundLayout()).clone()).layoutSize(); + KDSize lowerBoundNEqualsSize = lowerBoundSizeWithNEquals(); // Render the Sum symbol. KDColor workingBuffer[k_symbolWidth*k_symbolHeight]; - KDRect symbolFrame(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundSizeWithNEquals.width()-k_symbolWidth)/2), + KDRect symbolFrame(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundNEqualsSize.width()-k_symbolWidth)/2), p.y() + max(upperBoundSize.height()+k_boundHeightMargin, argumentLayout()->baseline()-(k_symbolHeight+1)/2), k_symbolWidth, k_symbolHeight); ctx->blendRectWithMask(symbolFrame, expressionColor, (const uint8_t *)symbolPixel, (KDColor *)workingBuffer);