Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Heuristics for single words, simplify unit test, copy heuristics into clipboard

AuthorDaveJarvis <email>
Date2023-02-19 22:12:56 GMT-0800
Commit2ce59b35ea297a7a5bd11dcf8c6292f136bd9948
Parentf668f99
Delta80 lines added, 78 lines removed, 2-line increase
src/test/java/com/keenwrite/processors/markdown/ImageLinkExtensionTest.java
package com.keenwrite.processors.markdown;
-import com.keenwrite.AwaitFxExtension;
import com.keenwrite.editors.common.Caret;
import com.keenwrite.processors.Processor;
import com.keenwrite.processors.ProcessorContext;
import com.keenwrite.processors.markdown.extensions.ImageLinkExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
-import javafx.stage.Stage;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.testfx.framework.junit5.ApplicationExtension;
-import org.testfx.framework.junit5.Start;
import java.io.File;
import static com.keenwrite.constants.Constants.DOCUMENT_DEFAULT;
import static java.lang.String.format;
-import static javafx.application.Platform.runLater;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.testfx.util.WaitForAsyncUtils.waitForFxEvents;
/**
* Responsible for testing that linked images render into HTML according to
* the {@link ImageLinkExtension} rules.
*/
-@ExtendWith( {ApplicationExtension.class, AwaitFxExtension.class} )
@SuppressWarnings( "SameParameterValue" )
public class ImageLinkExtensionTest {
addUri( "http://" + URI_WEB );
addUri( "https://" + URI_WEB );
- }
-
- @Start
- @SuppressWarnings( "unused" )
- private void start( final Stage stage ) {
}
final var node = parser.parse( key );
final var expectedHtml = entry.getValue();
- final var actualHtml = new StringBuilder( 128 );
-
- runLater( () -> actualHtml.append( renderer.render( node ) ) );
+ final var actualHtml = renderer.render( node );
- waitForFxEvents();
- assertEquals( expectedHtml, actualHtml.toString() );
+ assertEquals( expectedHtml, actualHtml );
}
}
src/main/java/com/keenwrite/preview/images/ResampleOp.java
public ResampleOp(
- final ResampleFilter filter, final int destWidth, final int destHeight ) {
- this( filter,
- createAbsolutionDimension( destWidth, destHeight ) );
+ final ResampleFilter filter,
+ final int dstWidth,
+ final int dstHeight ) {
+ this( filter, createAbsolutionDimension( dstWidth, dstHeight ) );
}
public ResampleOp(
final ResampleFilter filter, ConstrainedDimension dimensionConstrain ) {
super( dimensionConstrain );
mFilter = filter;
}
public BufferedImage doFilter(
- BufferedImage srcImg, BufferedImage dest, int dstWidth, int dstHeight ) {
+ BufferedImage srcImg, BufferedImage dst, int dstWidth, int dstHeight ) {
this.dstWidth = dstWidth;
this.dstHeight = dstHeight;
final BufferedImage out;
- if( dest != null &&
- dstWidth == dest.getWidth() &&
- dstHeight == dest.getHeight() ) {
- out = dest;
- int nrDestChannels = ImageUtils.nrChannels( dest );
+ if( dst != null &&
+ dstWidth == dst.getWidth() &&
+ dstHeight == dst.getHeight() ) {
+ out = dst;
+ int nrDestChannels = ImageUtils.nrChannels( dst );
if( nrDestChannels != nrChannels ) {
final var errorMgs = format(
for( int k = 0; k < max; k++ ) { tot += arrWeight[ subindex + k ]; }
assert tot != 0 : "should never happen except bug in filter";
- if( tot != 0f ) {
- for( int k = 0; k < max; k++ ) { arrWeight[ subindex + k ] /= tot; }
- }
+ for( int k = 0; k < max; k++ ) { arrWeight[ subindex + k ] /= tot; }
}
}
src/main/java/com/keenwrite/preview/SvgReplacedElementFactory.java
import com.keenwrite.io.MediaType;
import com.keenwrite.ui.adapters.ReplacedElementAdapter;
+import io.sf.carte.echosvg.transcoder.TranscoderException;
+import org.w3c.dom.Element;
import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.swing.ImageReplacedElement;
import java.awt.image.BufferedImage;
import java.io.File;
+import java.io.IOException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.nio.file.Path;
+import java.text.ParseException;
import static com.keenwrite.events.StatusEvent.clue;
private static final ImageReplacedElement BROKEN_IMAGE =
- createImageReplacedElement( BROKEN_IMAGE_PLACEHOLDER );
+ createElement( BROKEN_IMAGE_PLACEHOLDER );
@Override
public ReplacedElement createReplacedElement(
final LayoutContext c,
final BlockBox box,
final UserAgentCallback uac,
final int cssWidth,
final int cssHeight ) {
final var e = box.getElement();
-
- ImageReplacedElement image = null;
try {
- BufferedImage raster = null;
-
- switch( e.getNodeName() ) {
- case HTML_IMAGE -> {
- final var source = e.getAttribute( HTML_IMAGE_SRC );
+ final BufferedImage raster =
+ switch( e.getNodeName() ) {
+ case HTML_IMAGE -> createHtmlImage( box, e );
+ case HTML_TEX -> createTexImage( e );
+ default -> null;
+ };
- URI uri = null;
+ return createElement( raster );
+ } catch( final Exception ex ) {
+ clue( ex );
+ }
- if( getProtocol( source ).isHttp() ) {
- try( final var response = open( source ) ) {
- if( response.isSvg() ) {
- // Rasterize SVG from URL resource.
- raster = rasterize(
- response.getInputStream(),
- box.getContentWidth()
- );
- }
+ return BROKEN_IMAGE;
+ }
- clue( "Main.status.image.request.fetch", source );
- }
- }
- else if( MediaType.fromFilename( source ).isSvg() ) {
- // Attempt to rasterize based on file name.
- final var srcUri = new URI( source ).getPath();
- final var path = Path.of( new File( srcUri ).getCanonicalPath() );
+ /**
+ * Convert an HTML element to a raster graphic.
+ */
+ private static BufferedImage createHtmlImage(
+ final BlockBox box, final Element e )
+ throws TranscoderException, URISyntaxException, IOException {
+ final var source = e.getAttribute( HTML_IMAGE_SRC );
- if( path.isAbsolute() ) {
- uri = path.toUri();
- }
- else {
- final var base = new URI( e.getBaseURI() ).getPath();
- uri = Path.of( base, source ).toUri();
- }
- }
+ URI uri = null;
+ BufferedImage raster = null;
- if( uri != null ) {
- raster = rasterize( uri, box.getContentWidth() );
- }
+ if( getProtocol( source ).isHttp() ) {
+ try( final var response = open( source ) ) {
+ if( response.isSvg() ) {
+ // Rasterize SVG from URL resource.
+ raster = rasterize(
+ response.getInputStream(),
+ box.getContentWidth()
+ );
}
- case HTML_TEX ->
- // Convert the TeX element to a raster graphic.
- raster = rasterize( MathRenderer.toString( e.getTextContent() ) );
+
+ clue( "Main.status.image.request.fetch", source );
}
+ }
+ else if( MediaType.fromFilename( source ).isSvg() ) {
+ // Attempt to rasterize based on file name.
+ final var srcUri = new URI( source ).getPath();
+ final var path = Path.of( new File( srcUri ).getCanonicalPath() );
- if( raster != null ) {
- image = createImageReplacedElement( raster );
+ if( path.isAbsolute() ) {
+ uri = path.toUri();
}
- } catch( final Exception ex ) {
- image = BROKEN_IMAGE;
- clue( ex );
+ else {
+ final var base = new URI( e.getBaseURI() ).getPath();
+ uri = Path.of( base, source ).toUri();
+ }
}
- return image == null ? BROKEN_IMAGE : image;
+ final int w = box.getContentWidth();
+
+ if( uri != null && w > 0 ) {
+ raster = rasterize( uri, w );
+ }
+
+ return raster;
}
- private static ImageReplacedElement createImageReplacedElement(
- final BufferedImage bi ) {
- return new ImageReplacedElement( bi, bi.getWidth(), bi.getHeight() );
+ /**
+ * Convert the TeX element to a raster graphic.
+ */
+ private BufferedImage createTexImage( final Element e )
+ throws TranscoderException, ParseException {
+ return rasterize( MathRenderer.toString( e.getTextContent() ) );
+ }
+
+ private static ImageReplacedElement createElement( final BufferedImage bi ) {
+ return bi == null
+ ? BROKEN_IMAGE
+ : new ImageReplacedElement( bi, bi.getWidth(), bi.getHeight() );
}
}