[poincare] create a sum layout

Change-Id: I73821aec47aa3693dddbea3d1e730236f24bbcb3
This commit is contained in:
Émilie Feral
2017-01-14 08:36:36 +01:00
parent 3283a063cb
commit 1336d7fb7c
3 changed files with 119 additions and 0 deletions

View File

@@ -47,6 +47,7 @@ objs += $(addprefix poincare/src/layout/,\
nth_root_layout.o\
parenthesis_layout.o\
string_layout.o\
sum_layout.o\
subscript_layout.o\
)

View File

@@ -0,0 +1,92 @@
#include "sum_layout.h"
#include <string.h>
#include <assert.h>
const uint8_t symbolPixel[SumLayout::k_symbolHeight][SumLayout::k_symbolWidth] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
};
SumLayout::SumLayout(ExpressionLayout * lowerBoundLayout, ExpressionLayout * upperBoundLayout, ExpressionLayout * argumentLayout) :
ExpressionLayout(),
m_lowerBoundLayout(lowerBoundLayout),
m_upperBoundLayout(upperBoundLayout),
m_argumentLayout(argumentLayout)
{
m_lowerBoundLayout->setParent(this);
m_upperBoundLayout->setParent(this);
m_argumentLayout->setParent(this);
m_baseline = max(m_upperBoundLayout->size().height()+k_boundHeightMargin+k_symbolHeight, m_argumentLayout->baseline());
}
SumLayout::~SumLayout() {
delete m_lowerBoundLayout;
delete m_upperBoundLayout;
delete m_argumentLayout;
}
void SumLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
KDSize upperBoundSize = m_upperBoundLayout->size();
KDSize lowerBoundSize = m_lowerBoundLayout->size();
KDColor workingBuffer[k_symbolWidth*k_symbolHeight];
KDRect symbolFrame(p.x() + max(max(0, (upperBoundSize.width()-k_symbolWidth)/2), (lowerBoundSize.width()-k_symbolWidth)/2),
p.y() + max(upperBoundSize.height()+k_boundHeightMargin, m_argumentLayout->baseline()-k_symbolHeight),
k_symbolWidth, k_symbolHeight);
ctx->blendRectWithMask(symbolFrame, expressionColor, (const uint8_t *)symbolPixel, (KDColor *)workingBuffer);
}
KDSize SumLayout::computeSize() {
KDSize argumentSize = m_argumentLayout->size();
KDSize lowerBoundSize = m_lowerBoundLayout->size();
KDSize upperBoundSize = m_upperBoundLayout->size();
return KDSize(
max(max(k_symbolWidth, lowerBoundSize.width()), upperBoundSize.width())+k_argumentWidthMargin+argumentSize.width(),
m_baseline + max(k_boundHeightMargin+lowerBoundSize.height(), argumentSize.height() - m_argumentLayout->baseline())
);
}
ExpressionLayout * SumLayout::child(uint16_t index) {
switch (index) {
case 0:
return m_upperBoundLayout;
case 1:
return m_lowerBoundLayout;
case 2:
return m_argumentLayout;
default:
return nullptr;
}
}
KDPoint SumLayout::positionOfChild(ExpressionLayout * child) {
KDSize lowerBoundSize = m_lowerBoundLayout->size();
KDSize upperBoundSize = m_upperBoundLayout->size();
KDCoordinate x = 0;
KDCoordinate y = 0;
if (child == m_lowerBoundLayout) {
x = max(max(0, (k_symbolWidth-lowerBoundSize.width())/2), (upperBoundSize.width()-lowerBoundSize.width())/2);
y = m_baseline + k_boundHeightMargin;
} else if (child == m_upperBoundLayout) {
x = max(max(0, (k_symbolWidth-upperBoundSize.width())/2), (lowerBoundSize.width()-upperBoundSize.width())/2);
y = m_baseline - k_symbolHeight- k_boundHeightMargin-upperBoundSize.height();
} else if (child == m_argumentLayout) {
x = max(max(k_symbolWidth, lowerBoundSize.width()), upperBoundSize.width())+k_argumentWidthMargin;
y = m_baseline - m_argumentLayout->baseline();
} else {
assert(false);
}
return KDPoint(x,y);
}

View File

@@ -0,0 +1,26 @@
#ifndef POINCARE_SUM_LAYOUT_H
#define POINCARE_SUM_LAYOUT_H
#include <poincare/expression.h>
#include <poincare/expression_layout.h>
class SumLayout : public ExpressionLayout {
public:
SumLayout(ExpressionLayout * lowerBoundLayout, ExpressionLayout * upperBoundLayout, ExpressionLayout * argumentLayout);
~SumLayout();
constexpr static KDCoordinate k_symbolHeight = 15;
constexpr static KDCoordinate k_symbolWidth = 9;
protected:
void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override;
KDSize computeSize() override;
ExpressionLayout * child(uint16_t index) override;
KDPoint positionOfChild(ExpressionLayout * child) override;
private:
constexpr static KDCoordinate k_boundHeightMargin = 2;
constexpr static KDCoordinate k_argumentWidthMargin = 2;
ExpressionLayout * m_lowerBoundLayout;
ExpressionLayout * m_upperBoundLayout;
ExpressionLayout * m_argumentLayout;
};
#endif