Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial FontInfo Cache for ignoreCase=false. #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package net.sf.jasperreports.engine.fonts;

import java.io.Serializable;
import java.util.Locale;

/**
* Caching key.
*
* @author Markus Pscheidt
*
*/
public class FontInfoKey implements Serializable {

private static final long serialVersionUID = 1L;

private String name;
private Locale locale;

public FontInfoKey(String name, Locale locale) {
this.name = name;
this.locale = locale;
}

public String getName() {
return name;
}

public Locale getLocale() {
return locale;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((locale == null) ? 0 : locale.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
FontInfoKey other = (FontInfoKey) obj;
if (locale == null) {
if (other.locale != null)
return false;
} else if (!locale.equals(other.locale))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

@Override
public String toString() {
return "FontInfoKey [name=" + name + ", locale=" + locale + "]";
}

}
52 changes: 44 additions & 8 deletions jasperreports/src/net/sf/jasperreports/engine/fonts/FontUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public final class FontUtil

private JasperReportsContext jasperReportsContext;

private Map<FontInfoKey, FontInfo> fontInfoMap = new HashMap<>();

/**
*
Expand Down Expand Up @@ -174,6 +175,25 @@ public Map<Attribute,Object> getAttributesWithoutAwtFont(Map<Attribute,Object> a
return attributes;
}

private FontInfo lookupFontInfoCache(String name, boolean ignoreCase, Locale locale) {
if (ignoreCase) {
// cache not implemented yet for ignoreCase=true
return null;
}

FontInfoKey fontInfoKey = new FontInfoKey(name, locale);
return this.fontInfoMap.get(fontInfoKey);
}

private void putIntoFontInfoCache(String name, boolean ignoreCase, Locale locale, FontInfo fontInfo) {
if (ignoreCase) {
// cache not implemented yet for ignoreCase=true
return;
}

FontInfoKey fontInfoKey = new FontInfoKey(name, locale);
fontInfoMap.put(fontInfoKey, fontInfo);
}

/**
* Returns font information containing the font family, font face and font style.
Expand All @@ -185,9 +205,14 @@ public Map<Attribute,Object> getAttributesWithoutAwtFont(Map<Attribute,Object> a
*/
public FontInfo getFontInfo(String name, boolean ignoreCase, Locale locale)
{
FontInfo awtFamilyMatchFontInfo = null;
// lookup cache entry
FontInfo fontInfo = lookupFontInfoCache(name, ignoreCase, locale);
if (fontInfo != null) {
return fontInfo;
}

//FIXMEFONT do some cache
FontInfo awtFamilyMatchFontInfo = null;

List<FontFamily> families = jasperReportsContext.getExtensions(FontFamily.class);
for (Iterator<FontFamily> itf = families.iterator(); itf.hasNext();)
{
Expand All @@ -196,15 +221,19 @@ public FontInfo getFontInfo(String name, boolean ignoreCase, Locale locale)
{
if (equals(name, family.getName(), ignoreCase))
{
return new FontInfo(family, null, Font.PLAIN);
fontInfo = new FontInfo(family, null, Font.PLAIN);
putIntoFontInfoCache(name, ignoreCase, locale, fontInfo);
return fontInfo;
}

FontFace face = family.getNormalFace();
if (face != null)
{
if (equals(name, face.getName(), ignoreCase))
{
return new FontInfo(family, face, Font.PLAIN);
fontInfo = new FontInfo(family, face, Font.PLAIN);
putIntoFontInfoCache(name, ignoreCase, locale, fontInfo);
return fontInfo;
}
else if (
awtFamilyMatchFontInfo == null
Expand All @@ -221,7 +250,9 @@ && equals(name, face.getFont().getFamily(), ignoreCase)
{
if (equals(name, face.getName(), ignoreCase))
{
return new FontInfo(family, face, Font.BOLD);
fontInfo = new FontInfo(family, face, Font.BOLD);
putIntoFontInfoCache(name, ignoreCase, locale, fontInfo);
return fontInfo;
}
else if (
awtFamilyMatchFontInfo == null
Expand All @@ -238,7 +269,9 @@ && equals(name, face.getFont().getFamily(), ignoreCase)
{
if (equals(name, face.getName(), ignoreCase))
{
return new FontInfo(family, face, Font.ITALIC);
fontInfo = new FontInfo(family, face, Font.ITALIC);
putIntoFontInfoCache(name, ignoreCase, locale, fontInfo);
return fontInfo;
}
else if (
awtFamilyMatchFontInfo == null
Expand All @@ -255,7 +288,9 @@ && equals(name, face.getFont().getFamily(), ignoreCase)
{
if (equals(name, face.getName(), ignoreCase))
{
return new FontInfo(family, face, Font.BOLD | Font.ITALIC);
fontInfo = new FontInfo(family, face, Font.BOLD | Font.ITALIC);
putIntoFontInfoCache(name, ignoreCase, locale, fontInfo);
return fontInfo;
}
else if (
awtFamilyMatchFontInfo == null
Expand Down Expand Up @@ -300,6 +335,7 @@ public FontInfo getFontInfo(String name, Locale locale)
* @return a font info object
* @deprecated Replaced by {@link #getFontInfo(String, boolean, Locale)}.
*/
@Deprecated
public FontInfo getFontInfoIgnoreCase(String name, Locale locale)
{
return getFontInfo(name, true, locale);
Expand Down Expand Up @@ -380,7 +416,6 @@ public FontSetInfo getFontSetInfo(String name, Locale locale, boolean ignoreMiss

public String getExportFontFamily(String name, Locale locale, String exporterKey)
{
//FIXMEFONT do some cache
FontInfo fontInfo = getFontInfo(name, locale);
if (fontInfo != null)
{
Expand Down Expand Up @@ -451,6 +486,7 @@ protected void collectFontSetNames(Collection<String> names)
/**
* @deprecated Replaced by {@link #getAwtFontFromBundles(String, int, float, Locale, boolean)}.
*/
@Deprecated
public Font getAwtFontFromBundles(String name, int style, int size, Locale locale, boolean ignoreMissingFont)
{
return getAwtFontFromBundles(name, style, (float)size, locale, ignoreMissingFont);
Expand Down