Dave Jarvis' Repositories

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

Use single image cache, add DOM-based JMathTeX lib

AuthorDaveJarvis <email>
Date2020-09-13 11:58:54 GMT-0700
Commitbbb7ca1755a170b3d4dab6c34abf1c31e602701d
Parent029445d
Delta12 lines added, 27 lines removed, 15-line decrease
src/main/java/com/scrivenvar/graphics/SvgReplacedElementFactory.java
*/
private final Map<String, BufferedImage> mImageCache =
- new BoundedCache<>( 100 );
-
- /**
- * A bounded cache that removes the oldest document if the maximum number of
- * cached SVG XML documents has been reached. This constrains the number of
- * SVG XML documents loaded into memory.
- */
- private final Map<Element, String> mSvgCache = new BoundedCache<>( 200 );
+ new BoundedCache<>( 150 );
@Override
if( e != null ) {
- final var nodeName = e.getNodeName();
+ try {
+ final var nodeName = e.getNodeName();
- if( HTML_IMAGE.equals( nodeName ) ) {
- final var src = e.getAttribute( HTML_IMAGE_SRC );
- final var ext = FilenameUtils.getExtension( src );
+ if( HTML_IMAGE.equals( nodeName ) ) {
+ final var src = e.getAttribute( HTML_IMAGE_SRC );
+ final var ext = FilenameUtils.getExtension( src );
- if( SVG_FILE.equalsIgnoreCase( ext ) ) {
- try {
+ if( SVG_FILE.equalsIgnoreCase( ext ) ) {
image = getCachedImage(
src, svg -> rasterize( svg, box.getContentWidth() ) );
- } catch( final Exception ex ) {
- alert( ex );
}
}
- }
- else if( HTML_SVG.equalsIgnoreCase( nodeName ) ) {
- // Convert the <svg> element to a raster graphic.
- try {
- final var src = e.getAttribute( "id" );
+ else if( HTML_SVG.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 ) ) );
- } catch( final Exception ex ) {
- alert( ex );
}
+ } catch( final Exception ex ) {
+ alert( ex );
}
}
final String src, final Function<String, BufferedImage> rasterizer ) {
return mImageCache.computeIfAbsent( src, __ -> rasterizer.apply( src ) );
- }
-
- private String getCachedSvg( final Element doc ) {
- return mSvgCache.computeIfAbsent( doc, SvgRasterizer::toSvg );
}
}