Blame view

build4/epsilon-master/poincare/src/layout/char_layout.cpp 2.25 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include "char_layout.h"
  #include <poincare/expression_layout_cursor.h>
  #include <ion/charset.h>
  #include <assert.h>
  #include <stdlib.h>
  
  namespace Poincare {
  
  CharLayout::CharLayout(char c, KDText::FontSize fontSize) :
    StaticLayoutHierarchy<0>(),
    m_char(c),
    m_fontSize(fontSize)
  {
  }
  
  ExpressionLayout * CharLayout::clone() const {
    CharLayout * layout = new CharLayout(m_char, m_fontSize);
    return layout;
  }
  
  ExpressionLayoutCursor CharLayout::cursorLeftOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) {
    assert(cursor.pointedExpressionLayout() == this);
    // Case: Right. Go Left.
    if (cursor.position() == ExpressionLayoutCursor::Position::Right) {
      return ExpressionLayoutCursor(this, ExpressionLayoutCursor::Position::Left);
    }
    // Case: Left. Ask the parent.
    if (m_parent) {
      return m_parent->cursorLeftOf(cursor, shouldRecomputeLayout);
    }
    return ExpressionLayoutCursor();
  }
  
  ExpressionLayoutCursor CharLayout::cursorRightOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) {
    assert(cursor.pointedExpressionLayout() == this);
    // Case: Left. Go Right.
    if (cursor.position() == ExpressionLayoutCursor::Position::Left) {
      return ExpressionLayoutCursor(this, ExpressionLayoutCursor::Position::Right);
    }
    // Case: Right. Ask the parent.
    if (m_parent) {
      return m_parent->cursorRightOf(cursor, shouldRecomputeLayout);
    }
    return ExpressionLayoutCursor();
  }
  
  bool CharLayout::isCollapsable(int * numberOfOpenParenthesis, bool goingLeft) const {
    if (*numberOfOpenParenthesis <= 0
        && (m_char == '+'
          || m_char == '-'
          || m_char == '*'
          || m_char == Ion::Charset::MultiplicationSign
          || m_char == Ion::Charset::MiddleDot
          || m_char == Ion::Charset::Sto
          || m_char == '='
          || m_char == ','))
    {
      return false;
    }
    return true;
  }
  
  void CharLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) {
    char string[2] = {m_char, 0};
    ctx->drawString(string, p, m_fontSize, expressionColor, backgroundColor);
  }
  
  KDSize CharLayout::computeSize() {
    return KDText::charSize(m_fontSize);
  }
  
  void CharLayout::computeBaseline() {
    // Half height of the font.
    m_baseline = (KDText::charSize(m_fontSize).height()+1)/2;
    m_baselined = true;
  }
  
  }