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

URI resolveURI(String uri); URI resolveURI(String uri);
} }


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


public URI resolveURI(URI uri) { public URI resolveURI(URI uri) {
return uri; return uri;

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

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

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



package org.apache.fop.fonts; package org.apache.fop.fonts;


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


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


*/ */
public interface FontCacheManager { 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. * Loads the font cache into memory from the given file.
* @param file the serialized font cache
* @return the de-serialized font cache * @return the de-serialized font cache
*/ */
FontCache load(File file);
FontCache load();


/** /**
* Serializes the font cache to file. * 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 * @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. * 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 * @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

package org.apache.fop.fonts; package org.apache.fop.fonts;


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


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




private static final class FontCacheManagerImpl implements FontCacheManager { private static final class FontCacheManagerImpl implements FontCacheManager {


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

private FontCache fontCache; private FontCache fontCache;


public FontCache load(File cacheFile) {
public FontCache load() {
if (fontCache == null) { if (fontCache == null) {
fontCache = FontCache.loadFrom(cacheFile);
fontCache = FontCache.loadFrom(getCacheFile(false));
if (fontCache == null) { if (fontCache == null) {
fontCache = new FontCache(); fontCache = new FontCache();
} }
return fontCache; return fontCache;
} }


public void save(File cacheFile) throws FOPException {
public void save() throws FOPException {
if (fontCache != null && fontCache.hasChanged()) { 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 + "'."); 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 { private static final class DisabledFontCacheManager implements FontCacheManager {


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


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


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

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

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



package org.apache.fop.fonts; package org.apache.fop.fonts;


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


import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOPException;
/** FontTriplet matcher for fonts that shall be referenced rather than embedded. */ /** FontTriplet matcher for fonts that shall be referenced rather than embedded. */
private FontTriplet.Matcher referencedFontsMatcher; private FontTriplet.Matcher referencedFontsMatcher;


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

/** /**
* Main constructor * Main constructor
* *


/** /**
* Sets the font cache file * 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));
} }


/** /**
* @return the font cache * @return the font cache
*/ */
public FontCache getFontCache() { public FontCache getFontCache() {
return fontCacheManager.load(getCacheFile());
return fontCacheManager.load();
} }


/** /**
* @throws FOPException fop exception * @throws FOPException fop exception
*/ */
public void saveCache() throws FOPException { public void saveCache() throws FOPException {
fontCacheManager.save(getCacheFile());
fontCacheManager.save();
} }


/** /**
* @throws FOPException if an error was thrown while deleting the cache * @throws FOPException if an error was thrown while deleting the cache
*/ */
public void deleteCache() throws FOPException { public void deleteCache() throws FOPException {
fontCacheManager.delete(getCacheFile(true));
fontCacheManager.delete();
} }


/** /**

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



package org.apache.fop.fonts; package org.apache.fop.fonts;


import java.io.File;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List; import java.util.List;
* @throws FOPException if an exception occurs while processing the configuration * @throws FOPException if an exception occurs while processing the configuration
*/ */
public void configure(FontManager fontManager, boolean strict) throws FOPException { 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) // caching (fonts)
if (cfg.getChild("use-cache", false) != null) { if (cfg.getChild("use-cache", false) != null) {
try { try {
fontManager.disableFontCache(); fontManager.disableFontCache();
} else { } else {
if (cfg.getChild("cache-file", false) != null) { 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) { } catch (ConfigurationException mfue) {
LogUtil.handleException(log, mfue, true); 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, // [GA] permit configuration control over base14 kerning; without this,
// there is no way for a user to enable base14 kerning other than by // there is no way for a user to enable base14 kerning other than by
// programmatic API; // programmatic API;

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

/*
* 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