[poincare] Factorize Left/RightBracket layouts code.

Change-Id: I8708b9e0cfe0154232184f01afd532d4879b4cc0
This commit is contained in:
Léa Saviot
2018-04-24 10:16:22 +02:00
parent 3dc55230f7
commit 51e986606c
6 changed files with 60 additions and 104 deletions

View File

@@ -1,4 +1,5 @@
#include "bracket_layout.h"
#include <escher/metric.h>
#include <poincare/expression_layout_cursor.h>
extern "C" {
#include <assert.h>
@@ -50,14 +51,68 @@ KDSize BracketLayout::computeSize() {
return KDSize(k_externWidthMargin + k_lineThickness + k_widthMargin, operandHeight() + k_lineThickness);
}
void BracketLayout::computeBaseline() {
assert(m_parent != nullptr);
m_baseline = operandHeight()/2;
int currentNumberOfOpenBrackets = 1;
int increment = isLeftBracket() ? 1 : -1;
int numberOfSiblings = m_parent->numberOfChildren();
for (int i = m_parent->indexOfChild(this) + increment; i >= 0 && i < numberOfSiblings; i+=increment) {
ExpressionLayout * sibling = m_parent->editableChild(i);
if ((isRightBracket() && sibling->isLeftBracket())
|| (isLeftBracket() && sibling->isRightBracket()))
{
currentNumberOfOpenBrackets--;
if (currentNumberOfOpenBrackets == 0) {
break;
}
} else if ((isRightBracket() && sibling->isRightBracket())
|| (isLeftBracket() && sibling->isLeftBracket()))
{
currentNumberOfOpenBrackets++;
}
if (sibling->baseline() > m_baseline) {
m_baseline = sibling->baseline();
}
}
m_baselined = true;
}
KDCoordinate BracketLayout::operandHeight() {
if (!m_operandHeightComputed) {
computeOperandHeight();
m_operandHeightComputed = true;
}
return m_operandHeight;
}
void BracketLayout::computeOperandHeight() {
assert(m_parent != nullptr);
m_operandHeight = Metric::MinimalBracketAndParenthesisHeight;
int currentNumberOfOpenBrackets = 1;
int increment = isLeftBracket() ? 1 : -1;
int numberOfSiblings = m_parent->numberOfChildren();
for (int i = m_parent->indexOfChild(this) + increment; i >= 0 && i < numberOfSiblings; i+=increment) {
ExpressionLayout * sibling = m_parent->editableChild(i);
if ((isRightBracket() && sibling->isLeftBracket())
|| (isLeftBracket() && sibling->isRightBracket()))
{
currentNumberOfOpenBrackets--;
if (currentNumberOfOpenBrackets == 0) {
break;
}
} else if ((isRightBracket() && sibling->isRightBracket())
|| (isLeftBracket() && sibling->isLeftBracket()))
{
currentNumberOfOpenBrackets++;
}
KDCoordinate siblingHeight = sibling->size().height();
if (siblingHeight > m_operandHeight) {
m_operandHeight = siblingHeight;
}
}
m_operandHeightComputed = true;
}
KDPoint BracketLayout::positionOfChild(ExpressionLayout * child) {
assert(false);
return KDPointZero;

View File

@@ -17,8 +17,9 @@ protected:
constexpr static KDCoordinate k_widthMargin = 5;
constexpr static KDCoordinate k_externWidthMargin = 2;
KDSize computeSize() override;
void computeBaseline() override;
KDCoordinate operandHeight();
virtual void computeOperandHeight() = 0;
void computeOperandHeight();
KDPoint positionOfChild(ExpressionLayout * child) override;
bool m_operandHeightComputed;
uint16_t m_operandHeight;

View File

@@ -1,14 +1,9 @@
#include "left_bracket_layout.h"
#include <escher/metric.h>
extern "C" {
#include <assert.h>
}
namespace Poincare {
ExpressionLayout * LeftBracketLayout::clone() const {
LeftBracketLayout * layout = new LeftBracketLayout();
return layout;
return new LeftBracketLayout();
}
void LeftBracketLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
@@ -17,48 +12,4 @@ void LeftBracketLayout::render(KDContext * ctx, KDPoint p, KDColor expressionCol
ctx->fillRect(KDRect(p.x()+k_externWidthMargin, p.y() + operandHeight(), k_bracketWidth, k_lineThickness), expressionColor);
}
void LeftBracketLayout::computeOperandHeight() {
assert(m_parent != nullptr);
m_operandHeight = Metric::MinimalBracketAndParenthesisHeight;
int currentNumberOfOpenBrackets = 1;
int numberOfSiblings = m_parent->numberOfChildren();
for (int i = m_parent->indexOfChild(this) + 1; i < numberOfSiblings; i++) {
ExpressionLayout * sibling = m_parent->editableChild(i);
if (sibling->isRightBracket()) {
currentNumberOfOpenBrackets--;
if (currentNumberOfOpenBrackets == 0) {
return;
}
} else if (sibling->isLeftBracket()) {
currentNumberOfOpenBrackets++;
}
KDCoordinate siblingHeight = sibling->size().height();
if (siblingHeight > m_operandHeight) {
m_operandHeight = siblingHeight;
}
}
}
void LeftBracketLayout::computeBaseline() {
assert(m_parent != nullptr);
m_baseline = operandHeight()/2;
int currentNumberOfOpenBrackets = 1;
int numberOfSiblings = m_parent->numberOfChildren();
for (int i = m_parent->indexOfChild(this) + 1; i < numberOfSiblings; i++) {
ExpressionLayout * sibling = m_parent->editableChild(i);
if (sibling->isRightBracket()) {
currentNumberOfOpenBrackets--;
if (currentNumberOfOpenBrackets == 0) {
break;
}
} else if (sibling->isLeftBracket()) {
currentNumberOfOpenBrackets++;
}
if (sibling->baseline() > m_baseline) {
m_baseline = sibling->baseline();
}
}
m_baselined = true;
}
}

View File

@@ -16,8 +16,6 @@ public:
bool isLeftBracket() const override { return true; }
protected:
void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override;
void computeOperandHeight() override;
void computeBaseline() override;
};
}

View File

@@ -1,14 +1,9 @@
#include "right_bracket_layout.h"
#include <escher/metric.h>
extern "C" {
#include <assert.h>
}
namespace Poincare {
ExpressionLayout * RightBracketLayout::clone() const {
RightBracketLayout * layout = new RightBracketLayout();
return layout;
return new RightBracketLayout();
}
void RightBracketLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
@@ -17,46 +12,4 @@ void RightBracketLayout::render(KDContext * ctx, KDPoint p, KDColor expressionCo
ctx->fillRect(KDRect(p.x()+k_widthMargin-k_bracketWidth+1, p.y() + operandHeight(), k_bracketWidth, k_lineThickness), expressionColor);
}
void RightBracketLayout::computeOperandHeight() {
assert(m_parent != nullptr);
m_operandHeight = Metric::MinimalBracketAndParenthesisHeight;
int currentNumberOfOpenBrackets = 1;
for (int i = m_parent->indexOfChild(this) - 1; i >= 0; i--) {
ExpressionLayout * sibling = m_parent->editableChild(i);
if (sibling->isLeftBracket()) {
currentNumberOfOpenBrackets--;
if (currentNumberOfOpenBrackets == 0) {
return;
}
} else if (sibling->isRightBracket()) {
currentNumberOfOpenBrackets++;
}
KDCoordinate siblingHeight = sibling->size().height();
if (siblingHeight > m_operandHeight) {
m_operandHeight = siblingHeight;
}
}
}
void RightBracketLayout::computeBaseline() {
assert(m_parent != nullptr);
m_baseline = operandHeight()/2;
int currentNumberOfOpenBrackets = 1;
for (int i = m_parent->indexOfChild(this) - 1; i >= 0; i--) {
ExpressionLayout * sibling = m_parent->editableChild(i);
if (sibling->isLeftBracket()) {
currentNumberOfOpenBrackets--;
if (currentNumberOfOpenBrackets == 0) {
break;
}
} else if (sibling->isRightBracket()) {
currentNumberOfOpenBrackets++;
}
if (sibling->baseline() > m_baseline) {
m_baseline = sibling->baseline();
}
}
m_baselined = true;
}
}

View File

@@ -16,8 +16,6 @@ public:
bool isRightBracket() const override { return true; }
protected:
void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override;
void computeOperandHeight() override;
void computeBaseline() override;
};
}