Copy link to clipboard
Copied
Hi all,
Here I had made a script to write equations with superscript.
The input maybe
a^{2}+b^{2}=c^{2}
The Required output is
a2+b2=c2
The script I made is.
if(documents.length >0){ //if document is open
if(app.activeDocument.selection[0]){ //if a line is selected
var supBPosition=[];
var subBPosition=[];
var doc = app.activeDocument; //getting the active document
var line = doc.pathItems[0]; //getting path items from the active document
var firstPoint = line.pathPoints[0].anchor; // [x, y]
var lastPoint = line.pathPoints[line.pathPoints.length - 1].anchor; // [x, y]
var eqValues = (prompt("Enter something ", "", "")); //getting the values to write
var eqValuesCopy=eqValues;
var startCharacterSize=0;
var ys=[];
var ns=[];
var nsDummy=[];
var yFlag=0;
var nFlag=0;
var plotGraph=1;
getPositions();
var finalContent=eqValues;
if(eqValues.indexOf("^")>0){
plotGraph= doc.textFrames.add(); //adding text
plotGraph.textRange.characterAttributes.size = 8; //character size for text
plotGraph.textRange.characterAttributes.textFont = app.textFonts.getByName("Times-Roman"); //for changing the font to times-roman
plotGraph.textRange.justification = Justification.CENTER; //for aligning the content to center
plotGraph.top = firstPoint[1]; //x coordinate for the new value
plotGraph.left = firstPoint[0]; //y coordinate for the new value
plotGraph.contents=(finalContent);
var t = doc.textFrames[0];
for(var m=0;m<=ns.length;m++){
for(var plotGraphSup=ys
;plotGraphSup<=ns ;plotGraphSup++){ thisChar = t.characters[plotGraphSup]; //writing base value
thisChar.characterAttributes.baselineShift = +3; //making superscript
thisChar.characterAttributes.size = 6; //adjusting size of superscript
}
}
}
}
function getPositions(){
for(var i=0;i<eqValuesCopy.length;i++){
if(eqValuesCopy=="{"){
if(i!=0){
validBCheck=eqValuesCopy[i-1]+eqValuesCopy;
if(validBCheck!="/{"){
ys.push(i);
}
}
}
ysLength=ys.length;
}
for(var i=0;i<eqValuesCopy.length;i++){
nsLength=ns.length;
if(nsLength<ysLength){
if(eqValuesCopy=="{"){
var j=i;
while(j<eqValuesCopy.length){
j++;
if(eqValuesCopy
=="}"){ ns[nsLength]=j;
break;
}else if(eqValuesCopy
=="{"){ nsLength++;
}else{
}
}
i=j;
}
}
}
for(var k=0;k<eqValuesCopy.length;k++){
if(eqValuesCopy
=="}"){ var h=k;
while(h<eqValuesCopy.length){
h++;
if(eqValuesCopy
=="{"){ break;
}else if(eqValuesCopy
=="}"){ nsDummy.push(h);
break;
}else{
}
}
}
}
nsDummy.reverse();
for(var x=0;x<nsLength;x++){
if(ns
===undefined){ ns
=nsDummy ; }
}
}
}
Also I am getting an error in line number "32".
Moved from Illustrator to Illustrator Scripting by moderator
.
Bonjour,
Je ne vois pas l'utilité des accolades { }
...// JavaScript document for Illustrator
// elleere Wed, 25 April 2018 17:39:04 GMT
// INIT---------
var c1 = "^";
var c2 = "_";
var text = "2^2.5 + b^2 = 5 C_12^2 sin(20^3)"; // cas 1
//var text = "2^2.5 + b^2 = 5 C^2_12 sin(20^3)"; // cas 2 sauf si toujours 1 chiffre
var corps = 16;
//-------------
var docRef = activeDocument;
var pointText = docRef.textFrames.add();
pointText.contents = text;
pointText.position = [20,-20];
pointText.textRange.
Copy link to clipboard
Copied
for(var plotGraphSup=ys
should be:
for(var plotGraphSup=ys
Copy link to clipboard
Copied
Bonjour,
Je ne vois pas l'utilité des accolades { }
// JavaScript document for Illustrator
// elleere Wed, 25 April 2018 17:39:04 GMT
// INIT---------
var c1 = "^";
var c2 = "_";
var text = "2^2.5 + b^2 = 5 C_12^2 sin(20^3)"; // cas 1
//var text = "2^2.5 + b^2 = 5 C^2_12 sin(20^3)"; // cas 2 sauf si toujours 1 chiffre
var corps = 16;
//-------------
var docRef = activeDocument;
var pointText = docRef.textFrames.add();
pointText.contents = text;
pointText.position = [20,-20];
pointText.textRange.characterAttributes.size = corps;
//expo_indice(pointText,c1,1); // si cas 2
expo_indice(pointText,c2,0);
expo_indice(pointText,c1,1); // si cas 1
//--------------
function expo_indice(textRf,rx,drap) {
var index, pos, i, r, curentChar, suit, baseline;
baseline = drap ? FontBaselineOption.SUPERSCRIPT : FontBaselineOption.SUBSCRIPT;
pos = index = textRf.contents.indexOf(rx);
suit = textRf.contents;
while (pos != -1) {
suit = suit.substring(pos+1);
r = parseFloat(suit)+"";
//r = drap ? parseFloat(suit)+"" : "1"; // si toujours 1 chiffre
for (i = 1; i <= r.length; i++) {
curentChar = textRf.textRanges[index+i];
curentChar.baselinePosition = baseline;
}
textRf.textRanges[index].remove();
pos = suit.indexOf(rx);
index += pos;
}
}
//--------------
de LR
Copy link to clipboard
Copied
renél80416020 schrieb
Bonjour,
Je ne vois pas l'utilité des accolades { } …
Me too,
but anyway - try this (this is the same code as in your other thread)
var aSample = "a^{2}+b^{2}=c^{2}_{2}";
var aDoc = app.activeDocument;
var aTF = aDoc.textFrames.add();
aTF.position = [10, -2];
aTF.contents = aSample;
aTF.textRange.size = 8;
aTF.textRange.characterAttributes.textFont = app.textFonts.getByName("TimesNewRomanPSMT"); //depends on your Illu version and installed font
var res, idx;
var reg = /\^\{/g;
while (res = reg.exec(aTF.contents)) {
idx = res.index;
aTF.textRange.characters[idx].remove();
aTF.textRange.characters[idx].remove();
aTF.textRange.characters[idx].baselinePosition = FontBaselineOption.SUPERSCRIPT;
aTF.textRange.characters[idx+1].remove();
}
var reg = /_\{/g;
while (res = reg.exec(aTF.contents)) {
idx = res.index;
aTF.textRange.characters[idx].remove();
aTF.textRange.characters[idx].remove();
aTF.textRange.characters[idx].baselinePosition = FontBaselineOption.SUBSCRIPT;
aTF.textRange.characters[idx-1].tracking = -480; // TimesNewRomanPSMT
aTF.textRange.characters[idx+1].remove();
}
Have fun
Copy link to clipboard
Copied
Hi pixxxel,
This works. But the problem comes when, the input is
a^{21}+b^{2}=c^{2}_{2}
Something like this. This is why I had used the loop to get the starting and ending position on superscript and subscript values.
Copy link to clipboard
Copied
There is one rule for scripting and Grep (Regex):
The better/detailed and more the examples - the better the script/grep
How about the solution by renél80416020 in post #3 ? Should work with more than one sperscript/subscript digits.
Regards
Copy link to clipboard
Copied
Maybe I have a final solution for you - but you do not give feedback. In none of your four threads.
Writing equations in illustrator using javascript.
Need to remove some characters in the text in active document (this thread)
match pattern and get the position using javascript in illustrator.
an algorithm to store '{' and '}' in two arrays in illustrator using javascript
Copy link to clipboard
Copied
Hi,
had done just some changes.
// JavaScript document for Illustrator
// elleere Wed, 25 April 2018 17:39:04 GMT
// INIT---------
var c1 = "^";
var c2 = "_";
var text = (prompt("Enter something like 2^2.35 + b^2 + c^2= 5 C_12^2 sin(20^3)", "", "")); //getting the values to write
//var text = "2^2.35 + b^2 + c^2= 5 C_12^2 sin(20^3)"; // cas 1
//var text = "2^2.5 + b^2 = 5 C^2_12 sin(20^3)"; // cas 2 sauf si toujours 1 chiffre
var corps = 8;
//-------------
var docRef = app.activeDocument;
var line = docRef.pathItems[0];
var firstPoint = line.pathPoints[0].anchor; // [x, y]
var pointText = docRef.textFrames.add();
pointText.contents = text;
pointText.position = [firstPoint[0],firstPoint[1]];
pointText.textRange.characterAttributes.size = corps;
pointText.textRange.characterAttributes.textFont = app.textFonts.getByName("RVTimes-Italic");
expo_indice(pointText,c2,0);
expo_indice(pointText,c1,1); // si cas 1
app.cut();
//--------------
function expo_indice(textRf,rx,drap) {
var index, pos, i, r, curentChar, suit, baseline;
baseline = drap ? 1 : 2;
// baseline = drap ? FontBaselineOption.SUPERSCRIPT : FontBaselineOption.SUBSCRIPT;
pos = index = textRf.contents.indexOf(rx);
suit = textRf.contents;
while (pos != -1) {
suit = suit.substring(pos+1);
r = parseFloat(suit)+"";
for (i = 1; i <= r.length; i++) {
curentChar = textRf.textRanges[index+i];
if(baseline==1){
curentChar.characterAttributes.baselineShift = +3;
curentChar.characterAttributes.size = 5.33;
}else{
curentChar.characterAttributes.baselineShift = -1.2;
curentChar.characterAttributes.size = 5.33;
}
//curentChar.baselinePosition = baseline;
}
textRf.textRanges[index].remove();
pos = suit.indexOf(rx);
index += pos;
}
}
Thank-you
Find more inspiration, events, and resources on the new Adobe Community
Explore Now