From 89c08acc7f59900fd12f2bdaacbae1827ccb73a1 Mon Sep 17 00:00:00 2001 From: Laury Date: Fri, 22 Oct 2021 22:37:17 +0200 Subject: [PATCH] [reader] Fix bug in Latex handling --- apps/reader/tex_parser.cpp | 90 ++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 52 deletions(-) diff --git a/apps/reader/tex_parser.cpp b/apps/reader/tex_parser.cpp index 026c6493d..5cd229ace 100644 --- a/apps/reader/tex_parser.cpp +++ b/apps/reader/tex_parser.cpp @@ -12,41 +12,7 @@ TexParser::TexParser(const char * text, const char * endOfText) : } Layout TexParser::getLayout() { - HorizontalLayout layout = HorizontalLayout::Builder(); - const char * start = m_text; - - while (m_text < m_endOfText) { - if (*m_text == '\\') { - if (start != m_text) { - layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); - } - m_text ++; - layout.addOrMergeChildAtIndex(popCommand(), layout.numberOfChildren(), false); - start = m_text; - } - else if (*m_text == " ") { - if (start != m_text) { - layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); - } - m_text ++; - start = m_text; - } - else if (*m_text == "^") { - if (start != m_text) { - layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); - } - m_text ++; - layout.addOrMergeChildAtIndex(popCommand(), layout.numberOfChildren(), false); - start = m_text; - } - else { - m_text ++; - } - } - - if (start != m_text) { - layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); - } + Layout layout = popText(0); if (m_hasError) { return CodePointLayout::Builder(CodePoint(0xFFD)); @@ -55,12 +21,12 @@ Layout TexParser::getLayout() { return layout; } -Layout TexParser::popBlock() { +Layout TexParser::popBlock(char block) { while (*m_text == ' ') { m_text ++; } - if (*m_text == '{') { + if (*m_text == block) { m_text ++; return popText('}'); } @@ -80,28 +46,48 @@ Layout TexParser::popBlock() { } Layout TexParser::popText(char stop) { - if (*m_text == '\\') { - m_text ++; - return popCommand(); - } - HorizontalLayout layout = HorizontalLayout::Builder(); const char * start = m_text; - while (m_text < m_endOfText) { - if (*m_text == '\\') { - layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); - m_text ++; - layout.addOrMergeChildAtIndex(popCommand(), layout.numberOfChildren(), false); - start = m_text; + while (m_text < m_endOfText && *m_text != stop) { + switch (*m_text) { + case '\\': + if (start != m_text) { + layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); + } + m_text ++; + layout.addOrMergeChildAtIndex(popCommand(), layout.numberOfChildren(), false); + start = m_text; + break; + case ' ': + if (start != m_text) { + layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); + } + m_text ++; + start = m_text; + break; + case '^': + if (start != m_text) { + layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); + } + m_text ++; + layout.addOrMergeChildAtIndex(popCommand(), layout.numberOfChildren(), false); + start = m_text; + break; + default: + m_text ++; } - - m_text ++; } if (start != m_text) { layout.addOrMergeChildAtIndex(LayoutHelper::String(start, m_text - start), layout.numberOfChildren(), false); } + if (layout.numberOfChildren() == 1) { + layout.squashUnaryHierarchyInPlace(); + } + + m_text ++; + return layout; } @@ -116,8 +102,8 @@ Layout TexParser::popCommand() { } Layout TexParser::popFracCommand() { - Layout firstBlock = popBlock(); - Layout secondBlock = popBlock(); + Layout firstBlock = popBlock('{'); + Layout secondBlock = popBlock('{'); return FractionLayout::Builder(firstBlock, secondBlock); }