Skip to content

Commit

Permalink
Fix get space text in LaTeX mode
Browse files Browse the repository at this point in the history
- skip normal spaces in LaTeX brackets
  • Loading branch information
IgolJack authored and KirillovIlya committed Sep 20, 2024
1 parent b605a1b commit b793e5e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
33 changes: 17 additions & 16 deletions word/Editor/Run.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,36 +535,38 @@ ParaRun.prototype.GetText = function(oText)
*/
ParaRun.prototype.GetTextOfElement = function(oMathText, isSelectedText)
{
oMathText = new AscMath.MathTextAndStyles(oMathText);

oMathText = new AscMath.MathTextAndStyles(oMathText);
let isLatex = oMathText.IsLaTeX();

let nStartPos = (isSelectedText == true ? Math.min(this.Selection.StartPos, this.Selection.EndPos) : 0);
let nEndPos = (isSelectedText == true ? Math.max(this.Selection.StartPos, this.Selection.EndPos) : this.Content.length);

let isStrFont = false;
let arrFont = [];

for (let i = nStartPos; i < nEndPos; i++)
{
let oCurrentElement = this.Content[i];
let strCurrentElement = oCurrentElement.GetTextOfElement().GetText();
let oCurrentElement = this.Content[i];
let strCurrentElement = oCurrentElement.GetTextOfElement().GetText();

if (this.Content.length === 1 && oCurrentElement.value === 11034)
return oMathText;

// for LaTeX space processing
let oLast = oMathText.GetLastContent();
let strLast = ""
if (oLast)
strLast = oLast.text[oLast.text.length - 1];

// for LaTeX space processing while convert to professional mode
if (oMathText.IsDefaultText)
{
if (strCurrentElement === " ") //normal space
oMathText.AddText(new AscMath.MathText('\\ ', this))
else
oMathText.AddText(new AscMath.MathText(strCurrentElement, this));
oMathText.AddText(new AscMath.MathText(strCurrentElement, this));
continue;
}

let arrFontContent = oMathText.IsLaTeX() ? AscMath.GetLaTeXFont[strCurrentElement] : undefined;
let strMathFontName = arrFontContent ? AscMath.oStandardFont[arrFontContent[0]] : undefined;
let arrFontContent = oMathText.IsLaTeX()
? AscMath.GetLaTeXFont[strCurrentElement]
: undefined;
let strMathFontName = arrFontContent
? AscMath.oStandardFont[arrFontContent[0]]
: undefined;

if (!strMathFontName && isLatex)
{
Expand All @@ -590,7 +592,7 @@ ParaRun.prototype.GetTextOfElement = function(oMathText, isSelectedText)
{
if (oMathText.IsLaTeX())
{
if (strCurrentElement === " ") //normal space
if (strCurrentElement === " " && strLast !== "\\") //normal space
oMathText.AddText(new AscMath.MathText('\\ ', this))
// else if (strCurrentElement === " ")
// oMathText.AddText(new AscMath.MathText("\\quad", this));
Expand All @@ -607,7 +609,6 @@ ParaRun.prototype.GetTextOfElement = function(oMathText, isSelectedText)
{
oMathText.AddText(new AscMath.MathText(strCurrentElement, this));
}

}
}

Expand Down
32 changes: 19 additions & 13 deletions word/Math/LaTeXParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@
(function (window) {
const Literals = AscMath.MathLiterals;
const Struc = AscMath.MathStructures;

const ConvertTokens = AscMath.ConvertTokens;
const Tokenizer = AscMath.Tokenizer;
const LimitFunctions = AscMath.LimitFunctions;
const GetTypeFont = AscMath.GetTypeFont;
const GetMathFontChar = AscMath.GetMathFontChar;

function CLaTeXParser() {
this.oTokenizer = new Tokenizer(true);
this.intMathFontType = -1;
this.isReceiveOneTokenAtTime = false;
this.isNowMatrix = false;
this.EscapeSymbol = "";
function CLaTeXParser()
{
this.oTokenizer = new Tokenizer(true);
this.intMathFontType = -1;
this.isReceiveOneTokenAtTime = false;
this.isNowMatrix = false;
this.EscapeSymbol = "";
}
CLaTeXParser.prototype.IsNotEscapeSymbol = function ()
{
Expand Down Expand Up @@ -460,31 +459,38 @@
let arrContent = [];
let intCountOfBracketBlock = 1;

while (this.IsElementLiteral() || this.oLookahead.data === "∣" || this.oLookahead.data === "\\mid"|| this.oLookahead.data === "ⓜ")
while (this.IsElementLiteral()
|| this.oLookahead.data === "∣"
|| this.oLookahead.data === "\\mid"
|| this.oLookahead.data === "ⓜ")
{
if (strLeftSymbol && this.oLookahead.data === strLeftSymbol)
break;

if (this.oLookahead.data === "\\right")
break;

if (this.IsElementLiteral())
{
if (arrContent.length === 0)
{
this.SkipFreeSpace();
}
// normal space always skip in LaTeX brackets
this.SkipFreeSpace();

let oToken = [this.GetExpressionLiteral([strLeftSymbol])];
if ((oToken && !Array.isArray(oToken)) || Array.isArray(oToken) && oToken.length > 0)
{
arrContent.push(oToken)
}

// normal space always skip in LaTeX brackets
this.SkipFreeSpace();
}
else
{
arrMiddleStyles.push(this.oLookahead.style)
this.EatToken(this.oLookahead.class);
intCountOfBracketBlock++;
// normal space always skip in LaTeX brackets
this.SkipFreeSpace();
}
}

Expand Down

0 comments on commit b793e5e

Please sign in to comment.