Skip to content

Commit

Permalink
Merge pull request #200 from flyingsaucerproject/refactoring/use-gene…
Browse files Browse the repository at this point in the history
…rics

[refactoring] use generics
  • Loading branch information
asolntsev authored Sep 22, 2023
2 parents 1f5882c + 74a21c4 commit a91ab99
Show file tree
Hide file tree
Showing 211 changed files with 6,032 additions and 8,167 deletions.
12 changes: 3 additions & 9 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,48 @@
import org.xhtmlrenderer.render.FSFont;
import org.xhtmlrenderer.swing.AWTFSFont;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static java.util.Arrays.asList;

/**
* Description of the Class
*
* @author Joshua Marinacci
*/
public class AWTFontResolver implements FontResolver {
/**
* Description of the Field
*/
HashMap instance_hash;
/**
* Description of the Field
*/
HashMap available_fonts_hash;
private final Map<String, Font> fontsCache = new HashMap<>();

private final Set<String> availableFontNames = new HashSet<>();
private final Map<String, Font> availableFonts = new HashMap<>();

/**
* Constructor for the FontResolverTest object
*/
public AWTFontResolver() {
init();
}

private void init() {
GraphicsEnvironment gfx = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] available_fonts = gfx.getAvailableFontFamilyNames();
instance_hash = new HashMap();
fontsCache.clear();

// preload the font map with the font names as keys
// don't add the actual font objects because that would be a waste of memory
// we will only add them once we need to use them
// put empty strings in instead
available_fonts_hash = new HashMap();
for (int i = 0; i < available_fonts.length; i++) {
available_fonts_hash.put(available_fonts[i], "");
}
GraphicsEnvironment gfx = GraphicsEnvironment.getLocalGraphicsEnvironment();
availableFontNames.clear();
availableFontNames.addAll(asList(gfx.getAvailableFontFamilyNames()));

// preload sans, serif, and monospace into the available font hash
available_fonts_hash.put("Serif", new Font("Serif", Font.PLAIN, 1));
available_fonts_hash.put("SansSerif", new Font("SansSerif", Font.PLAIN, 1));
available_fonts_hash.put("Monospaced", new Font("Monospaced", Font.PLAIN, 1));
// preload sans, serif, and monospace
availableFonts.clear();
availableFonts.put("Serif", new Font("Serif", Font.PLAIN, 1));
availableFonts.put("SansSerif", new Font("SansSerif", Font.PLAIN, 1));
availableFonts.put("Monospaced", new Font("Monospaced", Font.PLAIN, 1));
}

@Override
public void flushCache() {
init();
}

/**
* Description of the Method
*
* @param ctx
* @param families PARAM
* @param size PARAM
* @param weight PARAM
* @param style PARAM
* @param variant PARAM
* @return Returns
*/
public FSFont resolveFont(SharedContext ctx, String[] families, float size, IdentValue weight, IdentValue style, IdentValue variant) {
// for each font family
if (families != null) {
for (int i = 0; i < families.length; i++) {
Font font = resolveFont(ctx, families[i], size, weight, style, variant);
for (String family : families) {
Font font = resolveFont(ctx, family, size, weight, style, variant);
if (font != null) {
return new AWTFSFont(font);
}
Expand All @@ -105,8 +80,8 @@ public FSFont resolveFont(SharedContext ctx, String[] families, float size, Iden
family = "Serif";
}

Font fnt = createFont(ctx, (Font) available_fonts_hash.get(family), size, weight, style, variant);
instance_hash.put(getFontInstanceHashName(ctx, family, size, weight, style, variant), fnt);
Font fnt = createFont(ctx, availableFonts.get(family), size, weight, style, variant);
fontsCache.put(getFontInstanceHashName(ctx, family, size, weight, style, variant), fnt);
return new AWTFSFont(fnt);
}

Expand All @@ -117,20 +92,9 @@ public FSFont resolveFont(SharedContext ctx, String[] families, float size, Iden
* @param font The new fontMapping value
*/
public void setFontMapping(String name, Font font) {
available_fonts_hash.put(name, font.deriveFont(1f));
availableFonts.put(name, font.deriveFont(1.0f));
}

/**
* Description of the Method
*
* @param ctx
* @param root_font PARAM
* @param size PARAM
* @param weight PARAM
* @param style PARAM
* @param variant PARAM
* @return Returns
*/
protected static Font createFont(SharedContext ctx, Font root_font, float size, IdentValue weight, IdentValue style, IdentValue variant) {
int font_const = Font.PLAIN;
if (weight != null &&
Expand Down Expand Up @@ -158,17 +122,6 @@ protected static Font createFont(SharedContext ctx, Font root_font, float size,
return fnt;
}

/**
* Description of the Method
*
* @param ctx
* @param font PARAM
* @param size PARAM
* @param weight PARAM
* @param style PARAM
* @param variant PARAM
* @return Returns
*/
protected Font resolveFont(SharedContext ctx, String font, float size, IdentValue weight, IdentValue style, IdentValue variant) {
// strip off the "s if they are there
if (font.startsWith("\"")) {
Expand All @@ -195,29 +148,23 @@ protected Font resolveFont(SharedContext ctx, String font, float size, IdentValu
// assemble a font instance hash name
String font_instance_name = getFontInstanceHashName(ctx, font, size, weight, style, variant);
// check if the font instance exists in the hash table
if (instance_hash.containsKey(font_instance_name)) {
if (fontsCache.containsKey(font_instance_name)) {
// if so then return it
return (Font) instance_hash.get(font_instance_name);
return fontsCache.get(font_instance_name);
}

// if not then
// does the font exist
if (available_fonts_hash.containsKey(font)) {
Object value = available_fonts_hash.get(font);
// have we actually allocated the root font object yet?
Font root_font;
if (value instanceof Font) {
root_font = (Font) value;
} else {
root_font = new Font(font, Font.PLAIN, 1);
available_fonts_hash.put(font, root_font);
}
Font root_font = availableFonts.get(font);
if (root_font == null && availableFontNames.contains(font)) {
root_font = new Font(font, Font.PLAIN, 1);
availableFonts.put(font, root_font);
}

if (root_font != null) {
// now that we have a root font, we need to create the correct version of it
Font fnt = createFont(ctx, root_font, size, weight, style, variant);

// add the font to the hash, so we don't have to do this again
instance_hash.put(font_instance_name, fnt);
fontsCache.put(font_instance_name, fnt);
return fnt;
}

Expand All @@ -227,18 +174,12 @@ protected Font resolveFont(SharedContext ctx, String font, float size, IdentValu

/**
* Gets the fontInstanceHashName attribute of the FontResolverTest object
*
* @param ctx
*@param name PARAM
* @param size PARAM
* @param weight PARAM
* @param style PARAM
* @param variant PARAM @return The fontInstanceHashName value
*/
protected static String getFontInstanceHashName(SharedContext ctx, String name, float size, IdentValue weight, IdentValue style, IdentValue variant) {
return name + "-" + (size * ctx.getTextRenderer().getFontScale()) + "-" + weight + "-" + style + "-" + variant;
}

@Override
public FSFont resolveFont(SharedContext renderingContext, FontSpecification spec) {
return resolveFont(renderingContext, spec.families, spec.size, spec.fontWeight, spec.fontStyle, spec.variant);
}
Expand Down
Loading

0 comments on commit a91ab99

Please sign in to comment.