| Author | DaveJarvis <email> |
|---|---|
| Date | 2020-09-13 16:39:11 GMT-0700 |
| Commit | 4dfb9fad6a6090d747a9fddf308c502e889a7dec |
| Parent | 2b19f30 |
| Delta | 83 lines added, 40 lines removed, 43-line increase |
| import com.whitemagicsoftware.tex.TeXFormula; | ||
| import com.whitemagicsoftware.tex.TeXLayout; | ||
| +import com.whitemagicsoftware.tex.graphics.AbstractGraphics2D; | ||
| +import com.whitemagicsoftware.tex.graphics.SvgDomGraphics2D; | ||
| import com.whitemagicsoftware.tex.graphics.SvgGraphics2D; | ||
| import org.junit.jupiter.api.Test; | ||
| import java.nio.file.Path; | ||
| -import static com.scrivenvar.preview.SvgRasterizer.rasterizeString; | ||
| -import static com.scrivenvar.preview.SvgRasterizer.toSvg; | ||
| +import static com.scrivenvar.preview.SvgRasterizer.*; | ||
| import static java.lang.System.getProperty; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| public void test_Rasterize_SimpleFormula_CorrectImageSize() | ||
| throws IOException { | ||
| - final var svg = createSvg(); | ||
| - final var image = rasterizeString( svg ); | ||
| - final var file = export( image, "image.png" ); | ||
| - assertEquals( FILESIZE, file.length() ); | ||
| + final var g = new SvgGraphics2D(); | ||
| + drawGraphics( g ); | ||
| + verifyImage( rasterizeString( g.toString() ) ); | ||
| } | ||
| /** | ||
| * Test that an SVG document object model can be parsed and rasterized into | ||
| * an image. | ||
| */ | ||
| @Test | ||
| - public void test_Conversion_InputElement_OutputRasterizableSvg() | ||
| + public void getTest_SvgDomGraphics2D_InputElement_OutputRasterizedImage() | ||
| throws ParserConfigurationException, IOException, SAXException { | ||
| - final var expectedSvg = createSvg(); | ||
| + final var g = new SvgGraphics2D(); | ||
| + drawGraphics( g ); | ||
| + | ||
| + final var expectedSvg = g.toString(); | ||
| final var bytes = expectedSvg.getBytes(); | ||
| final var actualSvg = toSvg( doc.getDocumentElement() ); | ||
| - final var image = rasterizeString( actualSvg ); | ||
| + verifyImage( rasterizeString( actualSvg ) ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Test that an SVG image from a DOM element can be rasterized. | ||
| + * | ||
| + * @throws IOException Could not write the image. | ||
| + */ | ||
| + @Test | ||
| + public void test_SvgDomGraphics2D_InputDom_OutputRasterizedImage() | ||
| + throws IOException { | ||
| + final var g = new SvgDomGraphics2D(); | ||
| + drawGraphics( g ); | ||
| + | ||
| + final var dom = g.toDom(); | ||
| + | ||
| + verifyImage( rasterize( dom ) ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Asserts that the given image matches an expected file size. | ||
| + * | ||
| + * @param image The image to check against the file size. | ||
| + * @throws IOException Could not write the image. | ||
| + */ | ||
| + private void verifyImage( final BufferedImage image ) throws IOException { | ||
| final var file = export( image, "dom.png" ); | ||
| assertEquals( FILESIZE, file.length() ); | ||
| } | ||
| /** | ||
| * Creates an SVG string for the default equation and font size. | ||
| - * | ||
| - * @return The equation converted to an SVG string. | ||
| */ | ||
| - private String createSvg() { | ||
| + private void drawGraphics( final AbstractGraphics2D g ) { | ||
| final var size = 100f; | ||
| final var texFont = new DefaultTeXFont( size ); | ||
| final var env = new TeXEnvironment( texFont ); | ||
| - final var g = new SvgGraphics2D(); | ||
| g.scale( size, size ); | ||
| final var formula = new TeXFormula( EQUATION ); | ||
| final var box = formula.createBox( env ); | ||
| final var layout = new TeXLayout( box, size ); | ||
| g.initialize( layout.getWidth(), layout.getHeight() ); | ||
| box.draw( g, layout.getX(), layout.getY() ); | ||
| - return g.toString(); | ||
| } | ||
| + @SuppressWarnings("SameParameterValue") | ||
| private File export( final BufferedImage image, final String filename ) | ||
| throws IOException { | ||
| } | ||
| + /** | ||
| + * Responsible for creating a new {@link ImageRenderer} implementation that | ||
| + * can render a DOM as an SVG image. | ||
| + */ | ||
| private static class BufferedImageTranscoder extends ImageTranscoder { | ||
| private BufferedImage mImage; | ||
| return BROKEN_IMAGE_PLACEHOLDER; | ||
| } | ||
| + } | ||
| + | ||
| + /** | ||
| + * Rasterizes the given document into an image. | ||
| + * | ||
| + * @param svg The SVG {@link Document} to rasterize. | ||
| + * @param width The rasterized image's width (in pixels). | ||
| + * @return The rasterized image. | ||
| + * @throws TranscoderException Signifies an issue with the input document. | ||
| + */ | ||
| + public static BufferedImage rasterize( final Document svg, final int width ) | ||
| + throws TranscoderException { | ||
| + final var transcoder = new BufferedImageTranscoder(); | ||
| + final var input = new TranscoderInput( svg ); | ||
| + | ||
| + transcoder.addTranscodingHint( KEY_WIDTH, (float) width ); | ||
| + transcoder.transcode( input, null ); | ||
| + | ||
| + return transcoder.getImage(); | ||
| } | ||
| throws IOException, TranscoderException { | ||
| return rasterize( FACTORY_DOM.createDocument( url.toString() ), width ); | ||
| + } | ||
| + | ||
| + public static BufferedImage rasterize( final Document document ) { | ||
| + try { | ||
| + final var root = document.getDocumentElement(); | ||
| + final var width = root.getAttribute( "width" ); | ||
| + return rasterize( document, INT_FORMAT.parse( width ).intValue() ); | ||
| + } catch( final Exception e ) { | ||
| + alert( e ); | ||
| + return BROKEN_IMAGE_PLACEHOLDER; | ||
| + } | ||
| } | ||
| public static BufferedImage rasterizeString( final String xml ) { | ||
| try { | ||
| - final var doc = toDocument( xml ); | ||
| - final var root = doc.getDocumentElement(); | ||
| + final var document = toDocument( xml ); | ||
| + final var root = document.getDocumentElement(); | ||
| final var width = root.getAttribute( "width" ); | ||
| return rasterizeString( xml, INT_FORMAT.parse( width ).intValue() ); | ||
| "http://www.w3.org/2000/svg", reader ); | ||
| } | ||
| - } | ||
| - | ||
| - public static BufferedImage rasterize( final Document svg, final int width ) | ||
| - throws TranscoderException { | ||
| - final var transcoder = new BufferedImageTranscoder(); | ||
| - final var input = new TranscoderInput( svg ); | ||
| - | ||
| - transcoder.addTranscodingHint( KEY_WIDTH, (float) width ); | ||
| - transcoder.transcode( input, null ); | ||
| - | ||
| - return transcoder.getImage(); | ||
| } | ||
| import static com.scrivenvar.StatusBarNotifier.alert; | ||
| -import static com.scrivenvar.preview.SvgRasterizer.*; | ||
| +import static com.scrivenvar.preview.SvgRasterizer.rasterize; | ||
| /** | ||
| */ | ||
| private static final String SVG_FILE = "svg"; | ||
| - private static final String HTML_SVG = "svg"; | ||
| /** | ||
| * TeX expression wrapped in a {@code <tex>} element. | ||
| */ | ||
| private static final String HTML_TEX = "tex"; | ||
| - | ||
| - /** | ||
| - * The {@code <svg>} element attribute name containing a value that uniquely | ||
| - * identifies the vector graphic file. This value must always be the same for | ||
| - * the same formula. That is, {@code E=mc^2} must always hash the same way | ||
| - * (e.g., by calling {@link String#hashCode()} on the formula). | ||
| - */ | ||
| - private static final String SVG_IMAGE_SRC = "id"; | ||
| private static final String HTML_IMAGE = "img"; | ||
| private static final String HTML_IMAGE_SRC = "src"; | ||
| + | ||
| + private static final MathRenderer sMathRenderer = new MathRenderer(); | ||
| /** | ||
| } | ||
| } | ||
| - else if( HTML_TEX.equals(nodeName)) { | ||
| + else if( HTML_TEX.equals( nodeName ) ) { | ||
| // Convert the <svg> element to a raster graphic if it isn't cached. | ||
| - final var src = e.getAttribute( SVG_IMAGE_SRC ); | ||
| - image = getCachedImage( src, __ -> rasterizeString( toSvg( e ) ) ); | ||
| + final var src = e.getTextContent(); | ||
| + image = getCachedImage( | ||
| + src, __ -> rasterize( sMathRenderer.render( src ) ) | ||
| + ); | ||
| } | ||
| } catch( final Exception ex ) { | ||