From 2ecc566c2cf27eedbfde23c867d2759127678969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 26 Jul 2019 14:37:17 +0200 Subject: [PATCH] [escher] TextField: when handling event with text, replace system parenthesis by user parenthesis to switch from layout structure to linear text. When copying a layout and pasting it to a textfield, we add required parentheses: 2+1 ----- serializes to ---> [2+1]/3 and is turned to (2+1)/3 before being 3 handled by a textfield. --- escher/src/text_field.cpp | 4 ++++ .../include/poincare/serialization_helper.h | 3 +++ poincare/src/serialization_helper.cpp | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 13232f5a4..0fb8c03d0 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include static inline int minInt(int x, int y) { return x < y ? x : y; } @@ -462,6 +463,9 @@ bool TextField::handleEventWithText(const char * eventText, bool indentation, bo char buffer[bufferSize]; UTF8Helper::CopyAndRemoveCodePoint(buffer, bufferSize, eventText, UCodePointEmpty); + // Replace System parentheses (used to keep layout tree structure) by normal parentheses + Poincare::SerializationHelper::ReplaceSystemParenthesesByUserParentheses(buffer); + const char * nextCursorLocation = m_contentView.draftTextBuffer() + draftTextLength(); if (insertTextAtLocation(buffer, cursorLocation())) { /* The cursor position depends on the text as we sometimes want to position diff --git a/poincare/include/poincare/serialization_helper.h b/poincare/include/poincare/serialization_helper.h index 4ee86219b..3496687c0 100644 --- a/poincare/include/poincare/serialization_helper.h +++ b/poincare/include/poincare/serialization_helper.h @@ -13,6 +13,9 @@ namespace Poincare { */ namespace SerializationHelper { + + void ReplaceSystemParenthesesByUserParentheses(char * buffer); + // SerializableReference to text int Infix( const TreeNode * node, diff --git a/poincare/src/serialization_helper.cpp b/poincare/src/serialization_helper.cpp index 4991226e4..c2d1566d1 100644 --- a/poincare/src/serialization_helper.cpp +++ b/poincare/src/serialization_helper.cpp @@ -1,10 +1,30 @@ #include #include +#include #include #include namespace Poincare { +void replaceOneCharSizedCodePointWith(char * buffer, CodePoint searchedCodePoint, CodePoint newCodePoint) { + assert(UTF8Decoder::CharSizeOfCodePoint(searchedCodePoint == 1)); + assert(UTF8Decoder::CharSizeOfCodePoint(newCodePoint == 1)); + UTF8Helper::PerformAtCodePoints( + buffer, + searchedCodePoint, + [](int codePointOffset, void * text, int newCodePoint, int bufferLength) { + *((char *)text+codePointOffset) = (char)newCodePoint; + }, + [](int c1, void * c2, int c3, int c4) {}, + (void *)buffer, + newCodePoint); +} + +void SerializationHelper::ReplaceSystemParenthesesByUserParentheses(char * buffer) { + replaceOneCharSizedCodePointWith(buffer, UCodePointLeftSystemParenthesis, '('); + replaceOneCharSizedCodePointWith(buffer, UCodePointRightSystemParenthesis, ')'); +} + static bool checkBufferSize(char * buffer, int bufferSize, int * result) { // If buffer has size 0 or 1, put a zero if it fits and return if (bufferSize == 0) {