Wingdings bullets workaround (XPath based post-processing)

Some RTF writers (such as WPTools) use the Wingdings font for bullet signs in unnumbered lists. Wingdings is not a web-safe font, so additional tweaking is required to transform a document generated by ScroogeXHTML to web-safe HTML5.

The library already includes post-processing classes, so we can build a workaround based on existing code and clean up the intermediate DOM.

A basic implementation is shown below. It iterates all nodes which carry a font-family:Wingdings style and does two things:

  • replace the ‘l’ character with a Unicode bullet sign
  • replace the ‘Wingdings’ font name with ‘serif’

Note: older versions of WPTools 7 emitted the font name as “WingDings” (with capital D) and used a “Ÿ” character instead of “l” for the bullet. The code example below has been simplified for newer WPTools versions for better readability.

public void postProcess(PostProcessEventObject e) {
  try {
    XPathFactory xpathFactory = XPathFactory.newInstance();
    // XPath to find Wingdings text nodes.
    XPathExpression xpathExp = xpathFactory.newXPath().compile(
       "//span[contains(@style, 'font-family:Wingdings')]");
    NodeList nodes = (NodeList) xpathExp
      .evaluate(e.getDocument(), XPathConstants.NODESET);

    for (int i = 0; i < nodes.getLength(); i++) {
      Element node = (Element) nodes.item(i);

      // replace the bullet
      String textContent = node.getTextContent();
      if ("l".equals(textContent)) {
        node.setTextContent("\u25CF");
      }

      // replace the font name
      String style = node.getAttribute("style");
      style = style.replace("Wingdings", "serif");
      node.setAttribute("style", style);
    }
  } catch (XPathExpressionException ex) {
      LOGGER.error(ex.getMessage(), ex);
  }
}
Advertisements

One thought on “Wingdings bullets workaround (XPath based post-processing)

  1. Pingback: Wingdings bullets workaround (XPath based post-processing) | Habari! Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s