private void buildBlock(List<Building> buildingList) { Block block = new Block(); block.add(buildingList.get(0)); if (buildingList.size() == 1) { blockList.add(block); return; } int maxRight = buildingList.get(0).getRight(); for (int i = 1; i < buildingList.size(); i++) { if (buildingList.get(i).getLeft() <= maxRight) { if (buildingList.get(i).getRight() > maxRight) { maxRight = buildingList.get(i).getRight(); } block.add(buildingList.get(i)); // the last building if (i == buildingList.size() - 1) { blockList.add(block); } } if (buildingList.get(i).getLeft() > maxRight) { blockList.add(block); buildBlock(buildingList.subList(i, buildingList.size())); break; } } }
/** * Processes MathML data. * * @param element MathML data element * @param block parent block * @throws DocumentException if some error occurs */ private void processMath(Element element, Block<Atom> block) throws DocumentException { try { element.setAttribute("xmlns:m", URI_M); element.setAttribute("xmlns:w", URI_W); ByteArrayOutputStream omml = new ByteArrayOutputStream(); DomUtil.save(element, omml); Source xsl = new StreamSource(getClass().getResourceAsStream(XSL_OMML2MML)); Source xml = new StreamSource(new ByteArrayInputStream(omml.toByteArray())); DOMResult mml = new DOMResult(); XslUtil.transform(xml, xsl, mml); LayoutContextImpl context = new LayoutContextImpl(LayoutContextImpl.getDefaultLayoutContext()); Float fontSize = (Float) context.getParameter(Parameter.MATHSIZE); final float factor = 1.5F; context.setParameter(Parameter.MATHSIZE, factor * fontSize); context.setParameter(Parameter.MFRAC_KEEP_SCRIPTLEVEL, Boolean.FALSE); ByteArrayOutputStream out = new ByteArrayOutputStream(); ConverterRegistry.getInstance() .getConverter("image/png") .convert(mml.getNode(), context, out); Image image = new Image(); mathImageIndex++; final String imageName = "mml-" + mathImageIndex + ".png"; image.setUrl(imageName); image.setAlt(imageName); image.setData(out.toByteArray()); block.add(image); } catch (XmlException e) { throw new DocumentException(e); } catch (IOException e) { throw new DocumentException(e); } }
/** * Processes image data. * * @param element image element * @param block parent block * @throws DocumentException if some error occurs */ private void processImage(Element element, Block<Atom> block) throws DocumentException { try { String path = "drawing/inline/graphic/graphicData/pic/blipFill/" + "blip/@embed"; String id = getText(element, path); String resource = "word/" + resources.get(id); Image image = new Image(); String text = getText(element, "drawing/inline/docPr/@descr"); image.setAlt(text); image.setUrl(resource); DocxReader.this.processors.put(resource, new ImageProcessor(image)); block.add(image); } catch (XmlException e) { throw new DocumentException(e); } }
/** * Processes text node. * * @param element text node parent * @param block parent block * @throws DocumentException if some error occurs */ private void processText(Element element, Block<Atom> block) throws DocumentException { try { TextNode node = new TextNode(); node.setText(getText(element, TAG_T)); node.setBold(exists(element, "rPr/b")); node.setItalic(exists(element, "rPr/i")); node.setUnderlined(exists(element, "rPr/u")); String vertAlign = getText(element, "rPr/vertAlign/@val"); node.setSuperscript("superscript".equals(vertAlign)); node.setSubscript("subscript".equals(vertAlign)); // TODO add font support block.add(node); } catch (XmlException e) { throw new DocumentException(e); } }