Blame view

build3/poincare/src/layout_engine.cpp 5.55 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  #include <poincare/layout_engine.h>
  #include "layout/string_layout.h"
  #include "layout/parenthesis_layout.h"
  #include "layout/horizontal_layout.h"
  extern "C" {
  #include<assert.h>
  }
  
  namespace Poincare {
  
  ExpressionLayout * LayoutEngine::createInfixLayout(const Expression * expression, Expression::FloatDisplayMode floatDisplayMode, Expression::ComplexFormat complexFormat, const char * operatorName) {
    assert(floatDisplayMode != Expression::FloatDisplayMode::Default);
    assert(complexFormat != Expression::ComplexFormat::Default);
    int numberOfOperands = expression->numberOfOperands();
    assert(numberOfOperands > 1);
    ExpressionLayout** children_layouts = new ExpressionLayout * [2*numberOfOperands-1];
    children_layouts[0] = expression->operand(0)->createLayout();
    for (int i=1; i<numberOfOperands; i++) {
      children_layouts[2*i-1] = new StringLayout(operatorName, 1);
      children_layouts[2*i] = expression->operand(i)->type() == Expression::Type::Opposite ? new ParenthesisLayout(expression->operand(i)->createLayout(floatDisplayMode, complexFormat)) : expression->operand(i)->createLayout(floatDisplayMode, complexFormat);
    }
    /* HorizontalLayout holds the children layouts so they do not need to be
     * deleted here. */
    ExpressionLayout * layout = new HorizontalLayout(children_layouts, 2*numberOfOperands-1);
    delete[] children_layouts;
    return layout;
  }
  
  ExpressionLayout * LayoutEngine::createPrefixLayout(const Expression * expression, Expression::FloatDisplayMode floatDisplayMode, Expression::ComplexFormat complexFormat, const char * operatorName) {
    assert(floatDisplayMode != Expression::FloatDisplayMode::Default);
    assert(complexFormat != Expression::ComplexFormat::Default);
    int numberOfOperands = expression->numberOfOperands();
    ExpressionLayout * argumentLayouts = nullptr;
    if (numberOfOperands > 0) {
      ExpressionLayout ** grandChildrenLayouts = new ExpressionLayout *[2*numberOfOperands-1];
      int layoutIndex = 0;
      grandChildrenLayouts[layoutIndex++] = expression->operand(0)->createLayout(floatDisplayMode, complexFormat);
      for (int i = 1; i < numberOfOperands; i++) {
        grandChildrenLayouts[layoutIndex++] = new StringLayout(",", 1);
        grandChildrenLayouts[layoutIndex++] = expression->operand(i)->createLayout(floatDisplayMode, complexFormat);
      }
      /* HorizontalLayout holds the grand children layouts so they do not need to
       * be deleted */
      argumentLayouts = new HorizontalLayout(grandChildrenLayouts, 2*numberOfOperands-1);
      delete [] grandChildrenLayouts;
    }
    ExpressionLayout * childrenLayouts[2];
    childrenLayouts[0] = new StringLayout(operatorName, strlen(operatorName));
    childrenLayouts[1] = new ParenthesisLayout(argumentLayouts);
    /* Same comment as above */
    return new HorizontalLayout(childrenLayouts, 2);
  }
  
  int LayoutEngine::writeInfixExpressionTextInBuffer(const Expression * expression, char * buffer, int bufferSize, int numberOfDigits, const char * operatorName) {
    if (bufferSize == 0) {
      return -1;
    }
    buffer[bufferSize-1] = 0;
    int numberOfChar = 0;
    int numberOfOperands = expression->numberOfOperands();
    assert(numberOfOperands > 1);
    if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
    if (expression->operand(0)->needParenthesisWithParent(expression)) {
      buffer[numberOfChar++] = '(';
      if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
    }
    numberOfChar += expression->operand(0)->writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar, numberOfDigits);
    if (expression->operand(0)->needParenthesisWithParent(expression)) {
      buffer[numberOfChar++] = ')';
      if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
    }
    for (int i=1; i<numberOfOperands; i++) {
      if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
      numberOfChar += strlcpy(buffer+numberOfChar, operatorName, bufferSize-numberOfChar);
      if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
      if (expression->operand(i)->needParenthesisWithParent(expression)) {
        buffer[numberOfChar++] = '(';
        if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
      }
      numberOfChar += expression->operand(i)->writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar, numberOfDigits);
      if (expression->operand(i)->needParenthesisWithParent(expression)) {
        buffer[numberOfChar++] = ')';
        if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
      }
    }
    buffer[numberOfChar] = 0;
    return numberOfChar;
  }
  
  int LayoutEngine::writePrefixExpressionTextInBuffer(const Expression * expression, char * buffer, int bufferSize, int numberOfDigits, const char * operatorName) {
    if (bufferSize == 0) {
      return -1;
    }
    buffer[bufferSize-1] = 0;
    int numberOfChar = 0;
    numberOfChar += strlcpy(buffer, operatorName, bufferSize);
    if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
    buffer[numberOfChar++] = '(';
    if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
    int numberOfOperands = expression->numberOfOperands();
    if (numberOfOperands > 0) {
      numberOfChar += expression->operand(0)->writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar, numberOfDigits);
      for (int i = 1; i < numberOfOperands; i++) {
        if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
        buffer[numberOfChar++] = ',';
        if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
        numberOfChar += expression->operand(i)->writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar, numberOfDigits);
      }
      if (numberOfChar >= bufferSize-1) { return bufferSize-1; }
    }
    buffer[numberOfChar++] = ')';
    buffer[numberOfChar] = 0;
    return numberOfChar;
  }
  
  }