From 8466d5b124b593fe919cec73188ce889f15468ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 4 Mar 2019 10:48:07 +0100 Subject: [PATCH] [poincare] Rational::createLyt gives single CodePointLyt if possible This reduces the size of created layouts, thus allowing more layouts to be created, as there is a limit on the number of layout in a lyout field --- poincare/include/poincare/integer.h | 4 ++-- poincare/src/integer.cpp | 9 ++++++++- poincare/src/rational.cpp | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 11aea50db..ce940e225 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -96,8 +96,8 @@ public: // Serialization int serialize(char * buffer, int bufferSize) const; - //Layout - HorizontalLayout createLayout() const; + // Layout + Layout createLayout() const; // Approximation template T approximate() const; diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 2c38ce69c..a28e19e0b 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -1,7 +1,9 @@ #include +#include #include #include #include +#include #include #include #include @@ -202,9 +204,14 @@ int Integer::serialize(char * buffer, int bufferSize) const { // Layout -HorizontalLayout Integer::createLayout() const { +Layout Integer::createLayout() const { char buffer[k_maxNumberOfDigitsBase10]; int numberOfChars = serialize(buffer, k_maxNumberOfDigitsBase10); + assert(numberOfChars >= 1); + if ((int)UTF8Decoder::CharSizeOfCodePoint(buffer[0]) == numberOfChars) { + UTF8Decoder decoder = UTF8Decoder(buffer); + return CodePointLayout::Builder(decoder.nextCodePoint()); + } return LayoutHelper::String(buffer, numberOfChars); } diff --git a/poincare/src/rational.cpp b/poincare/src/rational.cpp index e5e9ee87a..e94eecd6d 100644 --- a/poincare/src/rational.cpp +++ b/poincare/src/rational.cpp @@ -96,11 +96,11 @@ Expression RationalNode::setSign(Sign s, Context * context, Preferences::Complex // Layout Layout RationalNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { - HorizontalLayout numeratorLayout = signedNumerator().createLayout(); + Layout numeratorLayout = signedNumerator().createLayout(); if (denominator().isOne()) { return numeratorLayout; } - HorizontalLayout denominatorLayout = denominator().createLayout(); + Layout denominatorLayout = denominator().createLayout(); return FractionLayout::Builder(numeratorLayout, denominatorLayout); }