[shared] Fix the buffer size of curve view labels

This commit is contained in:
Émilie Feral
2018-11-29 14:10:54 +01:00
committed by LeaNumworks
parent b0f2131dec
commit e56cfa3f94
8 changed files with 20 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ public:
protected:
char * label(Axis axis, int index) const override;
private:
char m_labels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_labels[k_maxNumberOfXLabels][k_labelBufferSize];
static float EvaluateAtAbscissa(float abscissa, void * model, void * context);
Law * m_law;
Calculation * m_calculation;

View File

@@ -15,8 +15,8 @@ public:
private:
char * label(Axis axis, int index) const override;
Store * m_store;
char m_xLabels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_yLabels[k_maxNumberOfYLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_xLabels[k_maxNumberOfXLabels][k_labelBufferSize];
char m_yLabels[k_maxNumberOfYLabels][k_labelBufferSize];
Responder * m_controller;
};

View File

@@ -131,18 +131,16 @@ float CurveView::floatToPixel(Axis axis, float f) const {
}
void CurveView::computeLabels(Axis axis) {
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
float step = gridUnit(axis);
for (int index = 0; index < numberOfLabels(axis); index++) {
float labelValue = 2.0f*step*(std::ceil(min(axis)/(2.0f*step)))+index*2.0f*step;
if (labelValue < step && labelValue > -step) {
labelValue = 0.0f;
}
PrintFloat::convertFloatToText<float>(labelValue, buffer,
PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits),
/* Label cannot hold more than k_labelBufferSize characters to prevent them
* from overprinting one another.*/
PrintFloat::convertFloatToText<float>(labelValue, label(axis, index), k_labelBufferSize,
Constant::ShortNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal);
//TODO: check for size of label?
strlcpy(label(axis, index), buffer, strlen(buffer)+1);
}
}

View File

@@ -41,6 +41,14 @@ protected:
constexpr static KDCoordinate k_okVerticalMargin = 23;
constexpr static KDCoordinate k_okHorizontalMargin = 10;
constexpr static KDCoordinate k_labelGraduationLength = 6;
/* The labels are bounds by ±1E8 and ±1E-8 which in worse case can be written
* in 6 characters.
* To avoid overlapping labels, k_labelBufferSize should verify:
* k_labelBufferSize = Ion::Display::Width / ((CurveViewRange::k_maxNumberOfXGridUnits/2)*KDFont::SmallFont->glyphWidth)
* = 320/((18/2)*7) ~ 5.
* We take 6 creating small overlap in worse case but preventing from truncating
* labels (ie, "-1E-"). */
constexpr static int k_labelBufferSize = 6;
constexpr static int k_maxNumberOfXLabels = CurveViewRange::k_maxNumberOfXGridUnits;
constexpr static int k_maxNumberOfYLabels = CurveViewRange::k_maxNumberOfYGridUnits;
constexpr static int k_externRectMargin = 2;

View File

@@ -27,8 +27,8 @@ protected:
bool m_shouldColorHighlighted;
private:
char * label(Axis axis, int index) const override;
char m_xLabels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_yLabels[k_maxNumberOfYLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_xLabels[k_maxNumberOfXLabels][k_labelBufferSize];
char m_yLabels[k_maxNumberOfYLabels][k_labelBufferSize];
Poincare::Context * m_context;
};

View File

@@ -27,8 +27,8 @@ protected:
bool m_shouldColorHighlighted;
private:
char * label(Axis axis, int index) const override;
char m_xLabels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_yLabels[k_maxNumberOfYLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_xLabels[k_maxNumberOfXLabels][k_labelBufferSize];
char m_yLabels[k_maxNumberOfYLabels][k_labelBufferSize];
Poincare::Context * m_context;
};

View File

@@ -20,7 +20,7 @@ public:
private:
constexpr static KDCoordinate k_axisMargin = 3;
char * label(Axis axis, int index) const override;
char m_labels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_labels[k_maxNumberOfXLabels][k_labelBufferSize];
BoxRange m_boxRange;
};

View File

@@ -24,7 +24,7 @@ private:
char * label(Axis axis, int index) const override;
HistogramController * m_controller;
Store * m_store;
char m_labels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_labels[k_maxNumberOfXLabels][k_labelBufferSize];
static float EvaluateHistogramAtAbscissa(float abscissa, void * model, void * context);
float m_highlightedBarStart;
float m_highlightedBarEnd;