Browse Source

Bugzilla#53751: Slightly restructured the way the FontCache creates a cache-file


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1374238 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-2_0
Mehdi Houshmand 11 years ago
parent
commit
c52db65ef8

+ 1
- 1
src/java/org/apache/fop/afp/util/AFPResourceAccessor.java View File

@@ -106,7 +106,7 @@ public final class AFPResourceAccessor {
URI resolveURI(String uri);
}

private final class NullBaseURIResolver implements URIResolver {
private static final class NullBaseURIResolver implements URIResolver {

public URI resolveURI(URI uri) {
return uri;

+ 1
- 1
src/java/org/apache/fop/cli/CommandLineOptions.java View File

@@ -421,7 +421,7 @@ public class CommandLineOptions {
throw new FOPException("if you use '-cache', you must specify "
+ "the name of the font cache file");
} else {
factory.getFontManager().setCacheFile(new File(args[i + 1]));
factory.getFontManager().setCacheFile(URI.create(args[i + 1]));
return 1;
}
}

+ 10
- 8
src/java/org/apache/fop/fonts/FontCacheManager.java View File

@@ -19,7 +19,7 @@

package org.apache.fop.fonts;

import java.io.File;
import java.net.URI;

import org.apache.fop.apps.FOPException;

@@ -29,25 +29,27 @@ import org.apache.fop.apps.FOPException;
*/
public interface FontCacheManager {

/**
* Sets the font cache file given the URI pointing to the file.
* @param fontCacheURI the font cache URI
*/
void setCacheFile(URI fontCacheURI);

/**
* Loads the font cache into memory from the given file.
* @param file the serialized font cache
* @return the de-serialized font cache
*/
FontCache load(File file);
FontCache load();

/**
* Serializes the font cache to file.
* @param file the file to serialize the font cache to
* @throws FOPException if an error occurs serializing the font cache
*/
void save(File file) throws FOPException;
void save() throws FOPException;

/**
* Deletes the font cache from the file-system.
* @param file delete the serialized font cache
* @throws FOPException if an error occurs deleting the font cache
*/
void delete(File file) throws FOPException;

void delete() throws FOPException;
}

+ 28
- 9
src/java/org/apache/fop/fonts/FontCacheManagerFactory.java View File

@@ -20,6 +20,7 @@
package org.apache.fop.fonts;

import java.io.File;
import java.net.URI;

import org.apache.fop.apps.FOPException;

@@ -50,11 +51,14 @@ public final class FontCacheManagerFactory {

private static final class FontCacheManagerImpl implements FontCacheManager {

/** Provides a font cache file path **/
private File cacheFile;

private FontCache fontCache;

public FontCache load(File cacheFile) {
public FontCache load() {
if (fontCache == null) {
fontCache = FontCache.loadFrom(cacheFile);
fontCache = FontCache.loadFrom(getCacheFile(false));
if (fontCache == null) {
fontCache = new FontCache();
}
@@ -62,31 +66,46 @@ public final class FontCacheManagerFactory {
return fontCache;
}

public void save(File cacheFile) throws FOPException {
public void save() throws FOPException {
if (fontCache != null && fontCache.hasChanged()) {
fontCache.saveTo(cacheFile);
fontCache.saveTo(getCacheFile(true));
}
}

public void delete(File cacheFile) throws FOPException {
if (!cacheFile.delete()) {
public void delete() throws FOPException {
if (!getCacheFile(true).delete()) {
throw new FOPException("Failed to flush the font cache file '" + cacheFile + "'.");
}
}

private File getCacheFile(boolean forWriting) {
if (cacheFile != null) {
return cacheFile;
}
return FontCache.getDefaultCacheFile(forWriting);
}

public void setCacheFile(URI fontCacheURI) {
cacheFile = new File(fontCacheURI);
}
}

private static final class DisabledFontCacheManager implements FontCacheManager {

public FontCache load(File cacheFile) {
public FontCache load() {
return null;
}

public void save(File cacheFile) throws FOPException {
public void save() throws FOPException {
// nop
}

public void delete(File cacheFile) throws FOPException {
public void delete() throws FOPException {
throw new FOPException("Font Cache disabled");
}

public void setCacheFile(URI fontCacheURI) {
// nop
}
}
}

+ 7
- 25
src/java/org/apache/fop/fonts/FontManager.java View File

@@ -19,7 +19,7 @@

package org.apache.fop.fonts;

import java.io.File;
import java.net.URI;
import java.util.List;

import org.apache.fop.apps.FOPException;
@@ -52,9 +52,6 @@ public class FontManager {
/** FontTriplet matcher for fonts that shall be referenced rather than embedded. */
private FontTriplet.Matcher referencedFontsMatcher;

/** Provides a font cache file path **/
private File cacheFile;

/**
* Main constructor
*
@@ -115,25 +112,10 @@ public class FontManager {

/**
* Sets the font cache file
* @param cacheFile the font cache file
*/
public void setCacheFile(File cacheFile) {
this.cacheFile = cacheFile;
}

/**
* Returns the font cache file
* @return the font cache file
* @param cacheFileURI the URI of the font cache file
*/
public File getCacheFile() {
return getCacheFile(false);
}

private File getCacheFile(boolean writable) {
if (cacheFile != null) {
return cacheFile;
}
return FontCache.getDefaultCacheFile(writable);
public void setCacheFile(URI cacheFileURI) {
fontCacheManager.setCacheFile(resourceResolver.resolveFromBase(cacheFileURI));
}

/**
@@ -148,7 +130,7 @@ public class FontManager {
* @return the font cache
*/
public FontCache getFontCache() {
return fontCacheManager.load(getCacheFile());
return fontCacheManager.load();
}

/**
@@ -157,7 +139,7 @@ public class FontManager {
* @throws FOPException fop exception
*/
public void saveCache() throws FOPException {
fontCacheManager.save(getCacheFile());
fontCacheManager.save();
}

/**
@@ -165,7 +147,7 @@ public class FontManager {
* @throws FOPException if an error was thrown while deleting the cache
*/
public void deleteCache() throws FOPException {
fontCacheManager.delete(getCacheFile(true));
fontCacheManager.delete();
}

/**

+ 15
- 16
src/java/org/apache/fop/fonts/FontManagerConfigurator.java View File

@@ -19,7 +19,6 @@

package org.apache.fop.fonts;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
@@ -72,6 +71,19 @@ public class FontManagerConfigurator {
* @throws FOPException if an exception occurs while processing the configuration
*/
public void configure(FontManager fontManager, boolean strict) throws FOPException {
if (cfg.getChild("font-base", false) != null) {
try {
URI fontBase = InternalResourceResolver.getBaseURI(cfg.getChild("font-base")
.getValue(null));
fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
defaultBaseUri.resolve(fontBase), resourceResolver));
} catch (URISyntaxException use) {
LogUtil.handleException(log, use, true);
}
} else {
fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
defaultBaseUri, resourceResolver));
}
// caching (fonts)
if (cfg.getChild("use-cache", false) != null) {
try {
@@ -79,27 +91,14 @@ public class FontManagerConfigurator {
fontManager.disableFontCache();
} else {
if (cfg.getChild("cache-file", false) != null) {
fontManager.setCacheFile(new File(cfg.getChild("cache-file").getValue()));

fontManager.setCacheFile(URI.create(cfg.getChild("cache-file").getValue()));
}
}
} catch (ConfigurationException mfue) {
LogUtil.handleException(log, mfue, true);
}
}
if (cfg.getChild("font-base", false) != null) {
try {
URI fontBase = InternalResourceResolver.getBaseURI(cfg.getChild("font-base").getValue(
null));
fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
defaultBaseUri.resolve(fontBase), resourceResolver));
} catch (URISyntaxException use) {
LogUtil.handleException(log, use, true);
}
} else {
fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
defaultBaseUri, resourceResolver));
}

// [GA] permit configuration control over base14 kerning; without this,
// there is no way for a user to enable base14 kerning other than by
// programmatic API;

+ 77
- 0
test/java/org/apache/fop/fonts/FontManagerTestCase.java View File

@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.fop.fonts;

import java.net.URI;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.io.InternalResourceResolver;

public class FontManagerTestCase {

private FontManager sut;
private FontCacheManager fontCacheManager;
private FontDetector fontDetector;
private InternalResourceResolver resolver;

@Before
public void setUp() {
resolver = mock(InternalResourceResolver.class);
fontCacheManager = mock(FontCacheManager.class);
fontDetector = mock(FontDetector.class);

sut = new FontManager(resolver, fontDetector, fontCacheManager);
}

@Test
public void testSetCacheFile() {
URI testURI = URI.create("test/uri");
sut.setCacheFile(testURI);

InOrder inOrder = inOrder(resolver, fontCacheManager);
inOrder.verify(resolver).resolveFromBase(testURI);
inOrder.verify(fontCacheManager).setCacheFile(any(URI.class));
}

@Test
public void testGetFontCache() {
sut.getFontCache();
verify(fontCacheManager).load();
}

@Test
public void testSaveCache() throws FOPException {
sut.saveCache();
verify(fontCacheManager).save();
}

@Test
public void testDeleteCache() throws FOPException {
sut.deleteCache();
verify(fontCacheManager).delete();
}
}

Loading…
Cancel
Save