Dave Jarvis' Repositories

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

Upgrade libraries, fix image references, fix FTP detection, fix image unit test

AuthorDaveJarvis <email>
Date2023-05-03 16:38:30 GMT-0700
Commita0cc08e2e4a9f2f2368a3efada4ecc70570fe9bb
Parent067146a
Delta21 lines added, 146 lines removed, 125-line decrease
src/test/java/com/keenwrite/r/PluralizeTest.java
entry( "mongoose", "mongooses" ),
entry( "mythos", "mythoi" ),
- entry( "octopus", "octopuses" ),
+ entry( "octopus", "octopodes" ),
entry( "ox", "oxen" ),
entry( "soliloquy", "soliloquies" ),
src/main/java/com/keenwrite/processors/ProcessorContext.java
private Supplier<String> mChapters = () -> "";
- private Supplier<Path> mThemesPath = USER_DIRECTORY::toPath;
+ private Supplier<Path> mThemesDir = USER_DIRECTORY::toPath;
private Supplier<Locale> mLocale = () -> Locale.ENGLISH;
private Supplier<Map<String, String>> mDefinitions = HashMap::new;
private Supplier<Map<String, String>> mMetadata = HashMap::new;
private Supplier<Caret> mCaret = () -> Caret.builder().build();
- private Supplier<Path> mFontsPath = () -> getFontDirectory().toPath();
+ private Supplier<Path> mFontsDir = () -> getFontDirectory().toPath();
- private Supplier<Path> mImagesPath = USER_DIRECTORY::toPath;
+ private Supplier<Path> mImagesDir = USER_DIRECTORY::toPath;
private Supplier<String> mImageServer = () -> DIAGRAM_SERVER_NAME;
private Supplier<String> mImageOrder = () -> PERSIST_IMAGES_DEFAULT;
}
- public void setThemesPath( final Supplier<Path> themesPath ) {
- assert themesPath != null;
- mThemesPath = themesPath;
+ public void setThemesDir( final Supplier<Path> themesDir ) {
+ assert themesDir != null;
+ mThemesDir = themesDir;
}
- public void setCachesPath( final Supplier<File> cachesDir ) {
+ public void setCachesDir( final Supplier<File> cachesDir ) {
assert cachesDir != null;
mCachesPath = () -> {
final var dir = cachesDir.get();
return (dir == null ? USER_DATA_DIR.toFile() : dir).toPath();
};
}
- public void setImagesPath( final Supplier<File> imagesDir ) {
+ public void setImagesDir( final Supplier<File> imagesDir ) {
assert imagesDir != null;
- mImagesPath = () -> {
+ mImagesDir = () -> {
final var dir = imagesDir.get();
}
- public void setFontsPath( final Supplier<File> fontsPath ) {
- assert fontsPath != null;
- mFontsPath = () -> {
- final var dir = fontsPath.get();
+ public void setFontsDir( final Supplier<File> fontsDir ) {
+ assert fontsDir != null;
+ mFontsDir = () -> {
+ final var dir = fontsDir.get();
return (dir == null ? USER_DIRECTORY : dir).toPath();
}
- public Path getThemesPath() {
- return mMutator.mThemesPath.get();
+ public Path getThemesDir() {
+ return mMutator.mThemesDir.get();
}
- public Path getImagesPath() {
- return mMutator.mImagesPath.get();
+ public Path getImagesDir() {
+ return mMutator.mImagesDir.get();
}
}
- public Path getFontsPath() {
- return mMutator.mFontsPath.get();
+ public Path getFontsDir() {
+ return mMutator.mFontsDir.get();
}
src/main/java/com/keenwrite/processors/XhtmlProcessor.java
private Path getImagesPath() {
- return mContext.getImagesPath();
+ return mContext.getImagesDir();
}
src/main/java/com/keenwrite/preview/SvgReplacedElementFactory.java
-/* Copyright 2022 White Magic Software, Ltd. -- All rights reserved. */
-package com.keenwrite.preview;
-
-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;
-import static com.keenwrite.io.downloads.DownloadManager.open;
-import static com.keenwrite.preview.SvgRasterizer.*;
-import static com.keenwrite.processors.markdown.extensions.tex.TexNode.HTML_TEX;
-import static com.keenwrite.util.ProtocolScheme.getProtocol;
-
-/**
- * Responsible for running {@link SvgRasterizer} on SVG images detected within
- * a document to transform them into rasterized versions.
- */
-public final class SvgReplacedElementFactory extends ReplacedElementAdapter {
-
- public static final String HTML_IMAGE = "img";
- public static final String HTML_IMAGE_SRC = "src";
-
- private static final ImageReplacedElement BROKEN_IMAGE =
- 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();
-
- try {
- final BufferedImage raster =
- switch( e.getNodeName() ) {
- case HTML_IMAGE -> createHtmlImage( box, e );
- case HTML_TEX -> createTexImage( e );
- default -> null;
- };
-
- return createElement( raster );
- } catch( final Exception ex ) {
- clue( ex );
- }
-
- return BROKEN_IMAGE;
- }
-
- /**
- * 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 );
-
- URI uri = null;
- BufferedImage raster = null;
-
- if( getProtocol( source ).isHttp() ) {
- try( final var response = open( source ) ) {
- if( response.isSvg() ) {
- // Rasterize SVG from URL resource.
- raster = rasterize(
- response.getInputStream(),
- box.getContentWidth()
- );
- }
-
- 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( path.isAbsolute() ) {
- uri = path.toUri();
- }
- else {
- final var base = new URI( e.getBaseURI() ).getPath();
- uri = Path.of( base, source ).toUri();
- }
- }
-
- final int w = box.getContentWidth();
-
- if( uri != null && w > 0 ) {
- raster = rasterize( uri, w );
- }
-
- return raster;
- }
-
- /**
- * 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() );
- }
-}