/** * Copies the given AttributeSet to a new set, converting * any CSS attributes found to arguments of an HTML style * attribute. */ private static void convertToHTML40(AttributeSet from, MutableAttributeSet to) { Enumeration keys = from.getAttributeNames(); String value = ""; while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (key instanceof CSS.Attribute) { value = value + " " + key + "=" + from.getAttribute(key) + ";"; } else { to.addAttribute(key, from.getAttribute(key)); } } if (value.length() > 0) { to.addAttribute(HTML.Attribute.STYLE, value); } }
/** * Create/update an HTML <font> tag attribute. The * value of the attribute should be a MutableAttributeSet so * that the attributes can be updated as they are discovered. */ private static void createFontAttribute(CSS.Attribute a, AttributeSet from, MutableAttributeSet to) { MutableAttributeSet fontAttr = (MutableAttributeSet) to.getAttribute(HTML.Tag.FONT); if (fontAttr == null) { fontAttr = new SimpleAttributeSet(); to.addAttribute(HTML.Tag.FONT, fontAttr); } // edit the parameters to the font tag String htmlValue = from.getAttribute(a).toString(); if (a == CSS.Attribute.FONT_FAMILY) { fontAttr.addAttribute(HTML.Attribute.FACE, htmlValue); } else if (a == CSS.Attribute.FONT_SIZE) { fontAttr.addAttribute(HTML.Attribute.SIZE, htmlValue); } else if (a == CSS.Attribute.COLOR) { fontAttr.addAttribute(HTML.Attribute.COLOR, htmlValue); } }
/** * Create/update an HTML <font> tag attribute. The value of the * attribute should be a MutableAttributeSet so that the attributes can be * updated as they are discovered. */ private static void createFontAttribute(CSS.Attribute a, AttributeSet from, MutableAttributeSet to) { MutableAttributeSet fontAttr = (MutableAttributeSet) to.getAttribute(HTML.Tag.FONT); if (fontAttr == null) { fontAttr = new SimpleAttributeSet(); to.addAttribute(HTML.Tag.FONT, fontAttr); } // edit the parameters to the font tag String htmlValue = from.getAttribute(a).toString(); if (a == CSS.Attribute.FONT_FAMILY) { fontAttr.addAttribute(HTML.Attribute.FACE, htmlValue); } else if (a == CSS.Attribute.FONT_SIZE) { fontAttr.addAttribute(HTML.Attribute.SIZE, htmlValue); } else if (a == CSS.Attribute.COLOR) { fontAttr.addAttribute(HTML.Attribute.COLOR, htmlValue); } }
/** * Copies the given AttributeSet to a new set, converting any CSS attributes * found to arguments of an HTML style attribute. */ private static void convertToHTML40(AttributeSet from, MutableAttributeSet to) { Enumeration keys = from.getAttributeNames(); String value = ""; while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (key instanceof CSS.Attribute) { value = value + " " + key + "=" + from.getAttribute(key) + ";"; } else { to.addAttribute(key, from.getAttribute(key)); } } if (value.length() > 0) { to.addAttribute(HTML.Attribute.STYLE, value); } }
private float getFontScaleFactor(AttributeSet a) { final Object attribute = a.getAttribute(CSS.Attribute.FONT_SIZE); if(attribute == null) return ScaledEditorKit.getFontScaleFactor(); final String fontSize = attribute.toString(); final int fsLength = fontSize.length(); if(fsLength <= 1 || Character.isDigit(fontSize.charAt(fsLength-1)) || fontSize.endsWith("pt")) return ScaledEditorKit.getFontScaleFactor(); if(fontSize.endsWith("px")) return 1/1.3f; if(fontSize.endsWith("%") || fontSize.endsWith("em") || fontSize.endsWith("ex") || fontSize.endsWith("er")) return getFontScaleFactor(a); return ScaledEditorKit.getFontScaleFactor(); }
/** * Create an older style of HTML attributes. This will convert character * level attributes that have a StyleConstants mapping over to an HTML * tag/attribute. Other CSS attributes will be placed in an HTML style * attribute. */ private static void convertToHTML(AttributeSet from, MutableAttributeSet to) { if (from == null) { return; } Enumeration keys = from.getAttributeNames(); String value = ""; while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (key instanceof CSS.Attribute) { // default is to store in a HTML style attribute if (value.length() > 0) { value = value + "; "; } value = value + key + ": " + from.getAttribute(key); } else { to.addAttribute(key, from.getAttribute(key)); } } if (value.length() > 0) { to.addAttribute(HTML.Attribute.STYLE, value); } }
/** * Gets the font family name at the {@link JEditorPane}'s current caret position * * @param editor * @return The font family name, or null if no font is set */ public static String getFontFamily(JEditorPane editor) { AttributeSet attr = getCharacterAttributes(editor); if(attr != null) { Object val = attr.getAttribute(StyleConstants.FontFamily); if(val != null) return val.toString(); val = attr.getAttribute(CSS.Attribute.FONT_FAMILY); if(val != null) return val.toString(); val = attr.getAttribute(HTML.Tag.FONT); if(val != null && val instanceof AttributeSet) { AttributeSet set = (AttributeSet)val; val = set.getAttribute(HTML.Attribute.FACE); if(val != null) return val.toString(); } } return null; //no font family was defined }
public static void corrigePImplied(ExtendedHTMLDocument doc) { List<Element> tds = findElementByTag(doc, Tag.TD); for(Element td: tds) { if(td.getElementCount() > 0) { Element p = td.getElement(0); AttributeSet attrs = p.getAttributes(); if(attrs.containsAttribute(StyleConstants.NameAttribute, Tag.IMPLIED) && attrs.isDefined(CSS.Attribute.TEXT_ALIGN)) { SimpleAttributeSet s = new SimpleAttributeSet(); s.addAttribute(StyleConstants.NameAttribute, Tag.P); doc.setParagraphAttributes(p.getStartOffset(), p.getEndOffset(), s, false); } } } }
/** * Outputs the named style. <code>outputStyle</code> indicates * whether or not a style has been output yet. This will return * true if a style is written. */ boolean writeStyle(String name, Style style, boolean outputStyle) throws IOException { boolean didOutputStyle = false; Enumeration attributes = style.getAttributeNames(); if (attributes != null) { while (attributes.hasMoreElements()) { Object attribute = attributes.nextElement(); if (attribute instanceof CSS.Attribute) { String value = style.getAttribute(attribute).toString(); if (value != null) { if (!outputStyle) { writeStyleStartTag(); outputStyle = true; } if (!didOutputStyle) { didOutputStyle = true; indent(); write(name); write(" {"); } else { write(";"); } write(' '); write(attribute.toString()); write(": "); write(value); } } } } if (didOutputStyle) { write(" }"); writeLineSeparator(); } return didOutputStyle; }
/** * Outputs the named style. <code>outputStyle</code> indicates whether or * not a style has been output yet. This will return true if a style is * written. */ boolean writeStyle(String name, Style style, boolean outputStyle) throws IOException { boolean didOutputStyle = false; Enumeration attributes = style.getAttributeNames(); if (attributes != null) { while (attributes.hasMoreElements()) { Object attribute = attributes.nextElement(); if (attribute instanceof CSS.Attribute) { String value = style.getAttribute(attribute).toString(); if (value != null) { if (!outputStyle) { writeStyleStartTag(); outputStyle = true; } if (!didOutputStyle) { didOutputStyle = true; indent(); write(name); write(" {"); } else { write(";"); } write(' '); write(attribute.toString()); write(": "); write(value); } } } } if (didOutputStyle) { write(" }"); writeLineSeparator(); } return didOutputStyle; }
private void decouperTexteEnLignes_String(Element element) { if (!element.isLeaf()) { Object display = trouverProprieteCSSHeritage(CSS.Attribute.DISPLAY, element); if (display != null) display = display.toString().toLowerCase(); else { HTML.Tag tag = HTML.getTag(element.getName()); if (tag == null) display = ""; else if (tag.isBlock()) display = "block"; else display = "inline"; } if (display.equals("block")) { decoupe += "<<" + element.getName() + ">>"; } int elementCount = element.getElementCount(); for (int i = 0; i < elementCount; i++) { Element elem = element.getElement(i); decouperTexteEnLignes_String(elem); } if (display.equals("block")) { decoupe += "<<" + element.getName() + ">>"; } } else { int beg = element.getStartOffset(); int end = element.getEndOffset(); String text = null; try { text = getText(beg, end - beg); text = text.replaceAll("\\s+", " "); decoupe += text; } catch (BadLocationException e) { } } }
public String[] trouverFontFamily(Element element) { AttributeSet as; String family = null; as = element.getAttributes(); // Vérifier si l'élément de texte contient directement un // attribut "font-family". NOTE: Normalement, il ne devrait pas // le contenir car notre lecteur HTML a été modifié par rapport // au HTMLDocument.HTMLReader original qui, lui, convertit certains // éléments HTML affectant le texte en attributs qu'il ajoute à // l'élément de texte (par exemple, <FONT face=verdana> devient // un attribut font-family=verdana ajouté au texte qui suit.) // if (as.isDefined(CSS.Attribute.FONT_FAMILY)) // Essayer l'attribut css font-family Object familyObject = as.getAttribute(CSS.Attribute.FONT_FAMILY); if (familyObject == null) family = null; else family = familyObject.toString(); // Sinon, essay l'attribut css font // if (family == null) { // String font = (String)as.getAttribute(CSS.Attribute.FONT); // family = parseFontForFamily(font); // } if (family == null) { Element parent = element.getParentElement(); // fontFamily = // transInuk.trouverFontFamilyHerit(parent,doc); if (parent != null) { String[] families = trouverFontFamilyHerit(parent); return families; } else return null; } else { return (String[]) new String[] { family }; } }
@Override public void caretUpdate(CaretEvent e) { int referentCharacter = e.getDot(); try { if(referentCharacter>0 && !editeur.getText(referentCharacter-1, 1).equals("\n")) {referentCharacter--;} } catch (BadLocationException ex) { Logger.getLogger(JMathTextPane.class.getName()).log(Level.SEVERE, null, ex); } AttributeSet ast = editeur.getHTMLdoc().getCharacterElement(referentCharacter).getAttributes(); Object textDecorationValue = ast.getAttribute(CSS.Attribute.TEXT_DECORATION); editeur.getEditeurKit().getBoutonStrike().setSelected(textDecorationValue!=null && textDecorationValue.toString().equals("line-through")); }