Add baseline for pretty printing.

Change-Id: I77e76cb4b6191992137dd83f366006115ae65661
This commit is contained in:
Felix Raimundo
2016-03-29 12:36:12 +02:00
parent 1742146812
commit 5e01cfe421
6 changed files with 23 additions and 9 deletions

View File

@@ -13,12 +13,14 @@ class ExpressionLayout {
void draw(KDPoint point);
KDPoint origin();
KDSize size();
KDCoordinate baseline();
void setParent(ExpressionLayout* parent);
protected:
virtual void render(KDPoint point) = 0;
virtual KDSize computeSize() = 0;
virtual ExpressionLayout * child(uint16_t index) = 0;
virtual KDPoint positionOfChild(ExpressionLayout * child) = 0;
KDCoordinate m_baseline;
private:
KDPoint absoluteOrigin();
//void computeLayout();//ExpressionLayout * parent, uint16_t childIndex);

View File

@@ -9,6 +9,7 @@ ExponentLayout::ExponentLayout(ExpressionLayout * base_layout, ExpressionLayout
ExpressionLayout(), m_base_layout(base_layout), m_exponent_layout(exponent_layout) {
m_base_layout->setParent(this);
m_exponent_layout->setParent(this);
m_baseline = m_exponent_layout->baseline() + m_base_layout->baseline() - EXPONENT_HEIGHT;
}
ExponentLayout::~ExponentLayout() {
@@ -44,7 +45,7 @@ KDPoint ExponentLayout::positionOfChild(ExpressionLayout * child) {
KDPoint p;
if (child == m_base_layout) {
p.x = 0;
p.y = m_exponent_layout->size().height - EXPONENT_HEIGHT;
p.y = m_exponent_layout->baseline() - EXPONENT_HEIGHT;
} else if (child == m_exponent_layout) {
p.x = m_base_layout->size().width;
p.y = 0;

View File

@@ -3,6 +3,7 @@
#include "string_layout.h"
ExpressionLayout::ExpressionLayout() :
m_baseline(0),
m_parent(nullptr),
m_sized(false),
m_positioned(false),
@@ -12,6 +13,10 @@ ExpressionLayout::ExpressionLayout() :
ExpressionLayout::~ExpressionLayout() {
}
KDCoordinate ExpressionLayout::baseline() {
return m_baseline;
}
KDPoint ExpressionLayout::origin() {
if (m_parent == nullptr) {
return absoluteOrigin();
@@ -26,11 +31,6 @@ void ExpressionLayout::draw(KDPoint point) {
c->draw(point);
}
render(KDPointTranslate(absoluteOrigin(), point));
#if 0
KDPoint topLeft = KDPointTranslate(absoluteOrigin(), point);
KDPoint bottomRight = KDPointTranslate(KDPointTranslate(absoluteOrigin(), KDPOINT(size().width, 0 /*size().height*/)), point);
KDDrawLine(topLeft, bottomRight);
#endif
}
KDPoint ExpressionLayout::absoluteOrigin() {

View File

@@ -14,6 +14,7 @@ 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;
}
FractionLayout::~FractionLayout() {

View File

@@ -11,6 +11,9 @@ HorizontalLayout::HorizontalLayout(ExpressionLayout ** children_layouts, int num
assert(number_of_children > 0);
for (int i=0; i<m_number_of_children; i++) {
m_children_layouts[i]->setParent(this);
if (m_children_layouts[i]->baseline() > m_baseline) {
m_baseline = m_children_layouts[i]->baseline();
}
}
}
@@ -26,13 +29,18 @@ void HorizontalLayout::render(KDPoint point) { }
KDSize HorizontalLayout::computeSize() {
KDSize size = (KDSize){.width = 0, .height = 0};
int i = 0;
KDCoordinate max_under_baseline, max_above_baseline;
while (ExpressionLayout * c = child(i++)) {
KDSize childSize = c->size();
size.width += childSize.width;
if (childSize.height > size.height) {
size.height = childSize.height;
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;
}
@@ -59,6 +67,6 @@ KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
assert(previousChild != nullptr);
position.x = previousChild->origin().x + previousChild->size().width;
}
position.y = (size().height - child->size().height)/2;
position.y = m_baseline - child->baseline();
return position;
}

View File

@@ -8,6 +8,8 @@ ExpressionLayout() {
m_string = (char *)malloc(sizeof(char)*(length+1));
memcpy(m_string, string, (length+1));
m_inverse = inverse;
// Height of the font.
m_baseline = KDStringSize(" ").height;
}
StringLayout::~StringLayout() {