mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[Poincare] Update for C++ Kandinsky
Change-Id: Ib0d44841245a290c0395c43b2cde4c7ea69c17ca
This commit is contained in:
2
Makefile
2
Makefile
@@ -58,7 +58,7 @@ include libaxx/Makefile
|
||||
endif
|
||||
include ion/Makefile
|
||||
include kandinsky/Makefile
|
||||
#include poincare/Makefile
|
||||
include poincare/Makefile
|
||||
include escher/Makefile
|
||||
include apps/Makefile
|
||||
include quiz/Makefile # Quiz should be included at the end
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
#ifndef POINCARE_EXPRESSION_LAYOUT_H
|
||||
#define POINCARE_EXPRESSION_LAYOUT_H
|
||||
|
||||
extern "C" {
|
||||
#include <kandinsky.h>
|
||||
}
|
||||
|
||||
class ExpressionLayout {
|
||||
public:
|
||||
ExpressionLayout();
|
||||
virtual ~ExpressionLayout();
|
||||
|
||||
void draw(KDPoint point);
|
||||
void draw(KDContext * ctx, KDPoint p);
|
||||
KDPoint origin();
|
||||
KDSize size();
|
||||
KDCoordinate baseline();
|
||||
void setParent(ExpressionLayout* parent);
|
||||
protected:
|
||||
virtual void render(KDPoint point) = 0;
|
||||
virtual void render(KDContext * ctx, KDPoint p) = 0;
|
||||
virtual KDSize computeSize() = 0;
|
||||
virtual ExpressionLayout * child(uint16_t index) = 0;
|
||||
virtual KDPoint positionOfChild(ExpressionLayout * child) = 0;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <poincare/integer.h>
|
||||
#include <kandinsky/text.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
@@ -17,17 +17,17 @@ ExponentLayout::~ExponentLayout() {
|
||||
delete m_base_layout;
|
||||
}
|
||||
|
||||
// There is nothing to draw for a power, only the position of the children matters
|
||||
void ExponentLayout::render(KDPoint point) { }
|
||||
void ExponentLayout::render(KDContext * ctx, KDPoint p) {
|
||||
// There is nothing to draw for a power, only the position of the children matters
|
||||
}
|
||||
|
||||
KDSize ExponentLayout::computeSize() {
|
||||
KDSize s;
|
||||
KDSize exponent_size, base_size;
|
||||
exponent_size = m_exponent_layout->size();
|
||||
base_size = m_base_layout->size();
|
||||
s.height = base_size.height + exponent_size.height - EXPONENT_HEIGHT;
|
||||
s.width = base_size.width + exponent_size.width;
|
||||
return s;
|
||||
KDSize exponent_size = m_exponent_layout->size();
|
||||
KDSize base_size = m_base_layout->size();
|
||||
return KDSize(
|
||||
base_size.height() + exponent_size.height() - EXPONENT_HEIGHT,
|
||||
base_size.width() + exponent_size.width()
|
||||
);
|
||||
}
|
||||
|
||||
ExpressionLayout * ExponentLayout::child(uint16_t index) {
|
||||
@@ -42,15 +42,16 @@ ExpressionLayout * ExponentLayout::child(uint16_t index) {
|
||||
}
|
||||
|
||||
KDPoint ExponentLayout::positionOfChild(ExpressionLayout * child) {
|
||||
KDPoint p;
|
||||
KDCoordinate x = 0;
|
||||
KDCoordinate y = 0;
|
||||
if (child == m_base_layout) {
|
||||
p.x = 0;
|
||||
p.y = m_exponent_layout->baseline() - EXPONENT_HEIGHT;
|
||||
x = 0;
|
||||
y = m_exponent_layout->baseline() - EXPONENT_HEIGHT;
|
||||
} else if (child == m_exponent_layout) {
|
||||
p.x = m_base_layout->size().width;
|
||||
p.y = 0;
|
||||
x = m_base_layout->size().width();
|
||||
y = 0;
|
||||
} else {
|
||||
assert(false); // Should not happen
|
||||
}
|
||||
return p;
|
||||
return KDPoint(x,y);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class ExponentLayout : public ExpressionLayout {
|
||||
ExponentLayout(ExpressionLayout * base_layout, ExpressionLayout * exponent_layout);
|
||||
~ExponentLayout();
|
||||
protected:
|
||||
void render(KDPoint point) override;
|
||||
void render(KDContext * ctx, KDPoint p) override;
|
||||
KDSize computeSize() override;
|
||||
ExpressionLayout * child(uint16_t index) override;
|
||||
KDPoint positionOfChild(ExpressionLayout * child) override;
|
||||
|
||||
@@ -21,36 +21,37 @@ KDPoint ExpressionLayout::origin() {
|
||||
if (m_parent == nullptr) {
|
||||
return absoluteOrigin();
|
||||
} else {
|
||||
return KDPointMake(absoluteOrigin().x-m_parent->absoluteOrigin().x, absoluteOrigin().y-m_parent->absoluteOrigin().y);
|
||||
return KDPoint(absoluteOrigin().x() - m_parent->absoluteOrigin().x(),
|
||||
absoluteOrigin().y() - m_parent->absoluteOrigin().y());
|
||||
}
|
||||
}
|
||||
|
||||
void ExpressionLayout::draw(KDPoint point) {
|
||||
void ExpressionLayout::draw(KDContext * ctx, KDPoint p) {
|
||||
int i = 0;
|
||||
while (ExpressionLayout * c = child(i++)) {
|
||||
c->draw(point);
|
||||
c->draw(ctx, p);
|
||||
}
|
||||
render(KDPointTranslate(absoluteOrigin(), point));
|
||||
render(ctx, absoluteOrigin().translatedBy(p));
|
||||
}
|
||||
|
||||
KDPoint ExpressionLayout::absoluteOrigin() {
|
||||
if (!m_positioned) {
|
||||
if (m_parent != nullptr) {
|
||||
m_frame.origin = KDPointTranslate(m_parent->absoluteOrigin(), m_parent->positionOfChild(this));
|
||||
m_frame.setOrigin(m_parent->absoluteOrigin().translatedBy(m_parent->positionOfChild(this)));
|
||||
} else {
|
||||
m_frame.origin = KDPointZero;
|
||||
m_frame.setOrigin(KDPointZero);
|
||||
}
|
||||
m_positioned = true;
|
||||
}
|
||||
return m_frame.origin;
|
||||
return m_frame.origin();
|
||||
}
|
||||
|
||||
KDSize ExpressionLayout::size() {
|
||||
if (!m_sized) {
|
||||
m_frame.size = computeSize();
|
||||
m_frame.setSize(computeSize());
|
||||
m_sized = true;
|
||||
}
|
||||
return m_frame.size;
|
||||
return m_frame.size();
|
||||
}
|
||||
|
||||
void ExpressionLayout::setParent(ExpressionLayout* parent) {
|
||||
|
||||
@@ -14,7 +14,9 @@ FractionLayout::FractionLayout(ExpressionLayout * numerator_layout, ExpressionLa
|
||||
ExpressionLayout(), m_numerator_layout(numerator_layout), m_denominator_layout(denominator_layout) {
|
||||
m_numerator_layout->setParent(this);
|
||||
m_denominator_layout->setParent(this);
|
||||
m_baseline = m_numerator_layout->size().height + FRACTION_LINE_MARGIN + KDStringSize(" ").height/2;
|
||||
m_baseline = m_numerator_layout->size().height()
|
||||
+ FRACTION_LINE_MARGIN
|
||||
+ KDText::stringSize(" ").height()/2;
|
||||
}
|
||||
|
||||
FractionLayout::~FractionLayout() {
|
||||
@@ -22,21 +24,23 @@ FractionLayout::~FractionLayout() {
|
||||
delete m_numerator_layout;
|
||||
}
|
||||
|
||||
void FractionLayout::render(KDPoint point) {
|
||||
KDCoordinate fractionLineY = point.y + m_numerator_layout->size().height + FRACTION_LINE_MARGIN;
|
||||
void FractionLayout::render(KDContext * ctx, KDPoint p) {
|
||||
KDCoordinate fractionLineY = p.y() + m_numerator_layout->size().height() + FRACTION_LINE_MARGIN;
|
||||
|
||||
KDDrawLine(
|
||||
KDPointMake(point.x, fractionLineY),
|
||||
KDPointMake(point.x + size().width, fractionLineY),
|
||||
0xFF);
|
||||
ctx->drawLine(
|
||||
KDPoint(p.x(), fractionLineY),
|
||||
KDPoint(p.x() + size().width(), fractionLineY),
|
||||
KDColorRed
|
||||
);
|
||||
}
|
||||
|
||||
KDSize FractionLayout::computeSize() {
|
||||
KDSize s;
|
||||
s.width = max(m_numerator_layout->size().width, m_denominator_layout->size().width) + 2*FRACTION_BORDER_LENGTH;
|
||||
s.height = m_numerator_layout->size().height + FRACTION_LINE_MARGIN
|
||||
+ FRACTION_LINE_HEIGHT + FRACTION_LINE_MARGIN + m_denominator_layout->size().height;
|
||||
return s;
|
||||
KDCoordinate width = max(m_numerator_layout->size().width(), m_denominator_layout->size().width())
|
||||
+ 2*FRACTION_BORDER_LENGTH;
|
||||
KDCoordinate height = m_numerator_layout->size().height()
|
||||
+ FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT + FRACTION_LINE_MARGIN
|
||||
+ m_denominator_layout->size().height();
|
||||
return KDSize(width, height);
|
||||
}
|
||||
|
||||
ExpressionLayout * FractionLayout::child(uint16_t index) {
|
||||
@@ -51,15 +55,15 @@ ExpressionLayout * FractionLayout::child(uint16_t index) {
|
||||
}
|
||||
|
||||
KDPoint FractionLayout::positionOfChild(ExpressionLayout * child) {
|
||||
KDPoint p;
|
||||
KDCoordinate x = 0;
|
||||
KDCoordinate y = 0;
|
||||
if (child == m_numerator_layout) {
|
||||
p.x = (KDCoordinate)((size().width - m_numerator_layout->size().width)/2);
|
||||
p.y = 0;
|
||||
x = (KDCoordinate)((size().width() - m_numerator_layout->size().width())/2);
|
||||
} else if (child == m_denominator_layout) {
|
||||
p.x = (KDCoordinate)((size().width - m_denominator_layout->size().width)/2);
|
||||
p.y = (KDCoordinate)(m_numerator_layout->size().height + 2*FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT);
|
||||
x = (KDCoordinate)((size().width() - m_denominator_layout->size().width())/2);
|
||||
y = (KDCoordinate)(m_numerator_layout->size().height() + 2*FRACTION_LINE_MARGIN + FRACTION_LINE_HEIGHT);
|
||||
} else {
|
||||
assert(false); // Should not happen
|
||||
}
|
||||
return p;
|
||||
return KDPoint(x, y);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class FractionLayout : public ExpressionLayout {
|
||||
FractionLayout(ExpressionLayout * numerator, ExpressionLayout * denominator);
|
||||
~FractionLayout();
|
||||
protected:
|
||||
void render(KDPoint point) override;
|
||||
void render(KDContext * ctx, KDPoint p) override;
|
||||
KDSize computeSize() override;
|
||||
ExpressionLayout * child(uint16_t index) override;
|
||||
KDPoint positionOfChild(ExpressionLayout * child) override;
|
||||
|
||||
@@ -24,25 +24,25 @@ HorizontalLayout::~HorizontalLayout() {
|
||||
free(m_children_layouts);
|
||||
}
|
||||
|
||||
void HorizontalLayout::render(KDPoint point) { }
|
||||
void HorizontalLayout::render(KDContext * ctx, KDPoint p) {
|
||||
}
|
||||
|
||||
KDSize HorizontalLayout::computeSize() {
|
||||
KDSize size = (KDSize){.width = 0, .height = 0};
|
||||
KDCoordinate totalWidth = 0;
|
||||
int i = 0;
|
||||
KDCoordinate max_under_baseline = 0;
|
||||
KDCoordinate max_above_baseline = 0;
|
||||
while (ExpressionLayout * c = child(i++)) {
|
||||
KDSize childSize = c->size();
|
||||
size.width += childSize.width;
|
||||
if (childSize.height - c->baseline() > max_under_baseline) {
|
||||
max_under_baseline = childSize.height - c->baseline() ;
|
||||
totalWidth += childSize.width();
|
||||
if (childSize.height() - c->baseline() > max_under_baseline) {
|
||||
max_under_baseline = childSize.height() - c->baseline() ;
|
||||
}
|
||||
if (c->baseline() > max_above_baseline) {
|
||||
max_above_baseline = c->baseline();
|
||||
}
|
||||
}
|
||||
size.height = max_under_baseline + max_above_baseline;
|
||||
return size;
|
||||
return KDSize(totalWidth, max_under_baseline + max_above_baseline);
|
||||
}
|
||||
|
||||
ExpressionLayout * HorizontalLayout::child(uint16_t index) {
|
||||
@@ -55,7 +55,8 @@ ExpressionLayout * HorizontalLayout::child(uint16_t index) {
|
||||
}
|
||||
|
||||
KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
|
||||
KDPoint position = (KDPoint){.x = 0, .y = 0};
|
||||
KDCoordinate x = 0;
|
||||
KDCoordinate y = 0;
|
||||
uint16_t index = 0;
|
||||
for (int i=0;i<m_number_of_children;i++) {
|
||||
if (m_children_layouts[i] == child) {
|
||||
@@ -66,8 +67,8 @@ KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
|
||||
if (index > 0) {
|
||||
ExpressionLayout * previousChild = m_children_layouts[index-1];
|
||||
assert(previousChild != nullptr);
|
||||
position.x = previousChild->origin().x + previousChild->size().width;
|
||||
x = previousChild->origin().x() + previousChild->size().width();
|
||||
}
|
||||
position.y = m_baseline - child->baseline();
|
||||
return position;
|
||||
y = m_baseline - child->baseline();
|
||||
return KDPoint(x, y);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class HorizontalLayout : public ExpressionLayout {
|
||||
HorizontalLayout(ExpressionLayout ** layouts, int number_of_children);
|
||||
~HorizontalLayout();
|
||||
protected:
|
||||
void render(KDPoint point) override;
|
||||
void render(KDContext * ctx, KDPoint p) override;
|
||||
KDSize computeSize() override;
|
||||
ExpressionLayout * child(uint16_t index) override;
|
||||
KDPoint positionOfChild(ExpressionLayout * child) override;
|
||||
|
||||
@@ -9,7 +9,7 @@ ExpressionLayout() {
|
||||
memcpy(m_string, string, (length+1));
|
||||
m_inverse = inverse;
|
||||
// Height of the font.
|
||||
m_baseline = KDStringSize(" ").height;
|
||||
m_baseline = KDText::stringSize(" ").height();
|
||||
}
|
||||
|
||||
StringLayout::~StringLayout() {
|
||||
@@ -20,8 +20,8 @@ ExpressionLayout * StringLayout::child(uint16_t index) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void StringLayout::render(KDPoint point) {
|
||||
KDDrawString(m_string, point, m_inverse);
|
||||
void StringLayout::render(KDContext * ctx, KDPoint p) {
|
||||
ctx->drawString(m_string, p, m_inverse);
|
||||
}
|
||||
|
||||
KDPoint StringLayout::positionOfChild(ExpressionLayout * child) {
|
||||
@@ -30,5 +30,5 @@ KDPoint StringLayout::positionOfChild(ExpressionLayout * child) {
|
||||
}
|
||||
|
||||
KDSize StringLayout::computeSize() {
|
||||
return KDStringSize(m_string);
|
||||
return KDText::stringSize(m_string);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class StringLayout : public ExpressionLayout {
|
||||
StringLayout(const char * string, size_t length, uint8_t inverse=0);
|
||||
~StringLayout();
|
||||
protected:
|
||||
void render(KDPoint point) override;
|
||||
void render(KDContext * ctx, KDPoint p) override;
|
||||
KDSize computeSize() override;
|
||||
ExpressionLayout * child(uint16_t index) override;
|
||||
KDPoint positionOfChild(ExpressionLayout * child) override;
|
||||
|
||||
Reference in New Issue
Block a user