-
Notifications
You must be signed in to change notification settings - Fork 0
/
characterStyles-to-CSS.jsx
282 lines (204 loc) · 6.23 KB
/
characterStyles-to-CSS.jsx
1
#target illustrator;var myStyles = app.activeDocument.characterStyles;var myList = [];var wantedAttributes = new Array("baselinePosition","capitalization","fillColor","leading","size","strikeThrough","textFont","tracking");var definedAttributesList = [];var displayErrorMessages = false; // in CSS Display what's skipped as comment (true false to switch) .for (i = 1; i < myStyles.length; i++) { var myStyle = myStyles[i]; var charAttr = myStyle.characterAttributes; //Get style name definedAttributesList.push("." + myStyle.name + "{" +"\r"); //Get font name try {definedAttributesList.push("font-family : \"" + charAttr.textFont.name + "\";" );} catch (_){displayErrorMessage("font-family")} //Get font name. If font != commonSystemFont , font-weight should be set to Normal to prevent @fontface over-bolding problems. //Same issue for fake italics try {definedAttributesList.push("font-weight : " + charAttr.textFont.style + ";" );} catch (_){displayErrorMessage("font-weight")} try {definedAttributesList.push("font-size : " + charAttr.size + "px" + ";");} catch (_){displayErrorMessage("font-size")} try {definedAttributesList.push("line-height : " + charAttr.leading + "px" + ";")} catch (_){displayErrorMessage("line-height")} try {definedAttributesList.push("letter-spacing : " + (charAttr.tracking)/1000 + "em" + ";")} catch (_){displayErrorMessage("letter-spacing")}// Get fillColor. Check First if RVB of colorSport, to extract the RVB values to HexConvert try { redColor = Math.floor(charAttr.fillColor.red); greenColor = Math.floor(charAttr.fillColor.green); blueColor = Math.floor(charAttr.fillColor.blue); redColorHex = getHex(redColor); greenColorHex = getHex(greenColor); blueColorHex = getHex(blueColor); HexColor = redColorHex + greenColorHex + blueColorHex; definedAttributesList.push("color : #" + HexColor + ";")} catch (_){displayErrorMessage("Color")} // Get Capitals options try { var a = "ALLCAPS", b = "ALLSMALLCAPS"; switch(charAttr.capitalization.FontCapsOption) { case a: var CssProperty = "text-transform : uppercase;"; definedAttributesList.push("text-decoration : underline;") break; case b: var CssProperty = "font-variant : small-caps;"; definedAttributesList.push("text-decoration : underline;") break; } } catch (_){displayErrorMessage("Capitalization")} // Get underline or strike options try { var a = false, b = true; switch(charAttr.underline) { case b: definedAttributesList.push("text-decoration : underline;") break; } } catch (_){displayErrorMessage("underline")} try { var a = false, b = true; switch(charAttr.strikeThrough) { case b: definedAttributesList.push("text-decoration : line-through;") break; } } catch (_){displayErrorMessage("line-through")} //return character styles set from Ai character style panel (myStyle) definedAttributesList.push("}\r"); } // Save CSS settings on desktop WriteToFile(definedAttributesList.join("\r")); function displayErrorMessage (what) {var a = false, b = true; switch(displayErrorMessages) { case a: break; case b: definedAttributesList.push("/* " + what + " not defined your Ai StyleSheet */") break; } }function translateProperty(aiProperty) {switch(aiProperty) { case "textFont": aiProperty = "font-family"; break; case "fillColor": aiProperty = "color"; break; case "size": aiProperty = "font-size"; break; case "leading": aiProperty = "line-height"; break; case "tracking": aiProperty = "letter-spacing"; break; case "capitalization": aiProperty = "text-transform"; break; case "capitalization": aiProperty = "text-transform"; break; case "strikeThrough": aiProperty = "text-decoration"; break; case "underline": aiProperty = "text-decoration"; break; }return aiProperty;} function WriteToFile(myText) { myFile = new File("~/Desktop/List of fonts.css"); myFile.open("w"); myFile.write(myText); myFile.close();} // Convert RVB to Hexfunction getHex(colorNumber){ balance = colorNumber / 16; remainder = balance - Math.floor(balance); remainder = remainder * 16; balance = Math.floor(balance); remainder = Math.floor(remainder); newBalance = getBalance(balance); newRemainder = getRemainder(remainder); return newBalance.toString() + newRemainder.toString(); }function getBalance(balance){ if(balance>=10) { switch(balance) { case 10: balance = 'A'; break; case 11: balance = 'B'; break; case 12: balance = 'C'; break; case 13: balance = 'D'; break; case 14: balance = 'E'; break; case 15: balance = 'F'; break; } } else if(balance <10) { balance = balance; } return balance;}function getRemainder(remainder2){ var remainderNow = remainder2; if(remainderNow>=10) { switch(remainderNow) { case 10: remainderNow = 'A'; break; case 11: remainderNow = 'B'; break; case 12: remainderNow = 'C'; break; case 13: remainderNow = 'D'; break; case 14: remainderNow = 'E'; break; case 15: remainderNow = 'F'; break; } } else if(remainderNow <10) { remainderNow = remainderNow; }return remainderNow;}