Dave Jarvis' Repositories

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

Enforce replaced element factory execution order

AuthorDaveJarvis <email>
Date2020-12-29 14:29:40 GMT-0800
Commit343c019f67334c5a7f374f64eb17f9e8a525f44f
Parent2b99920
Delta29 lines added, 21 lines removed, 8-line increase
src/test/java/com/keenwrite/io/MediaTypeTest.java
public void test_FilenameExtensions_Supported_Success() {
final var map = Map.of(
- "png", IMAGE_PNG,
"jpeg", IMAGE_JPEG,
+ "png", IMAGE_PNG,
"svg", IMAGE_SVG_XML,
- "txt", TEXT_PLAIN,
"md", TEXT_MARKDOWN,
"Rmd", TEXT_R_MARKDOWN,
"Rxml", TEXT_R_XML,
+ "txt", TEXT_PLAIN,
"yml", TEXT_YAML
);
map.forEach( ( k, v ) -> assertEquals( v, valueFrom( "f." + k ) ) );
}
/**
- * Test that {@link MediaType#valueFrom(URI)} will pull and identify the
+ * Test that {@link HttpMediaType#valueFrom(URI)} will pull and identify the
* type of resource based on the HTTP Content-Type header.
*/
map.forEach( ( k, v ) -> {
try {
- assertEquals( v, valueFrom( new URI( k ) ) );
+ assertEquals( v, HttpMediaType.valueFrom( new URI( k ) ) );
} catch( Exception e ) {
fail();
src/main/java/com/keenwrite/preview/HtmlPreview.java
public final class HtmlPreview extends SwingNode {
+ private static final ChainedReplacedElementFactory FACTORY;
+
+ static {
+ FACTORY = new ChainedReplacedElementFactory();
+ FACTORY.addFactory( new SvgReplacedElementFactory() );
+ FACTORY.addFactory( new SwingReplacedElementFactory() );
+ }
+
/**
* Render CSS using points (pt) not pixels (px) to reduce the chance of
setCacheHint( SPEED );
setContent( mScrollPane );
-
- final var creFactory = new ChainedReplacedElementFactory();
- creFactory.addFactory( new SvgReplacedElementFactory() );
- creFactory.addFactory( new SwingReplacedElementFactory() );
final var context = mView.getSharedContext();
final var textRenderer = context.getTextRenderer();
- context.setReplacedElementFactory( creFactory );
+ context.setReplacedElementFactory( FACTORY );
textRenderer.setSmoothingThreshold( 0 );
src/main/java/com/keenwrite/preview/SvgReplacedElementFactory.java
package com.keenwrite.preview;
+import com.keenwrite.io.HttpMediaType;
import com.keenwrite.io.MediaType;
import com.keenwrite.util.BoundedCache;
import static com.keenwrite.StatusBarNotifier.clue;
-import static com.keenwrite.io.MediaType.IMAGE_SVG_XML;
-import static com.keenwrite.io.MediaType.TEXT_PLAIN;
+import static com.keenwrite.io.MediaType.*;
import static com.keenwrite.preview.SvgRasterizer.BROKEN_IMAGE_PLACEHOLDER;
import static com.keenwrite.preview.SvgRasterizer.rasterize;
final int cssWidth,
final int cssHeight ) {
- // Seems to resolve a race-condition between rastering and rendering.
- Thread.yield();
-
final var e = box.getElement();
if( getProtocol( source ).isHttp() ) {
- // Attempt to rasterize SVG depending on URL resource content.
- uri = new URI( source );
+ var mediaType = MediaType.valueFrom( source );
- // Attempt rasterization for SVG or plain text formats.
- final var mediaType = MediaType.valueFrom( uri );
- if( !(mediaType == TEXT_PLAIN || mediaType == IMAGE_SVG_XML) ) {
- uri = null;
+ if( isSvg( mediaType ) || mediaType == UNDEFINED ) {
+ // Attempt to rasterize SVG depending on URL resource content.
+ uri = new URI( source );
+
+ // Attempt rasterization for SVG or plain text formats.
+ if( !isSvg( HttpMediaType.valueFrom( uri ) ) ) {
+ uri = null;
+ }
}
}
- else if( MediaType.valueFrom( source ) == IMAGE_SVG_XML ) {
+ else if( isSvg( MediaType.valueFrom( source ) ) ) {
// Attempt to rasterize based on file name.
final var base = new URI( getBaseUri( e ) ).getPath();
final BufferedImage bi ) {
return new ImageReplacedElement( bi, bi.getWidth(), bi.getHeight() );
+ }
+
+ private static boolean isSvg( final MediaType mediaType ) {
+ return mediaType == TEXT_PLAIN || mediaType == IMAGE_SVG_XML;
}
}