Skip to content
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
24 changes: 23 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/FontFactoryImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -515,7 +516,7 @@ public void registerFamily(String familyName, String fullName, String path) {
tmp = new ArrayList<>();
tmp.add(fullName);
fontFamilies.put(familyName, tmp);
} else {
} else if (!tmp.contains(fullName)) {
int fullNameLength = fullName.length();
boolean inserted = false;
for (int j = 0; j < tmp.size(); ++j) {
Expand Down Expand Up @@ -727,6 +728,27 @@ public Set<String> getRegisteredFamilies() {
return Utilities.getKeySet(fontFamilies);
}

/**
* Gets the font names registered for a given font family. Package-private: intended for
* testing the deduplication done by {@link #registerFamily}, not part of the public API.
*
* @param family the font family (case-insensitive)
* @return an unmodifiable copy of the registered font names for the family, or an empty list if
* the family is not registered
*/
List<String> getRegisteredFamily(String family) {
lock.readLock().lock();
try {
List<String> members = fontFamilies.get(family.toLowerCase(Locale.ROOT));
if (members == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(new ArrayList<>(members));
} finally {
lock.readLock().unlock();
}
}

/**
* Checks if a certain font is registered.
*
Expand Down
29 changes: 29 additions & 0 deletions openpdf/src/test/java/com/lowagie/text/FontFactoryImpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lowagie.text;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class FontFactoryImpTest {

/**
* Registering the same family/name combination several times must not add duplicate
* entries to the font family list, otherwise the list grows unbounded and slows down
* the lookup loop in {@link FontFactoryImp#getFont}.
*/
@Test
void registerFamilyShouldNotStoreDuplicateFullNames() {
FontFactoryImp fontFactory = new FontFactoryImp();

fontFactory.registerFamily("my-family", "My-Font-Regular", null);
fontFactory.registerFamily("my-family", "My-Font-Regular", null);
fontFactory.registerFamily("my-family", "My-Font-Regular", null);

// A distinct member of the same family must still be added.
fontFactory.registerFamily("my-family", "My-Font-Bold", null);

assertThat(fontFactory.getRegisteredFamilies()).contains("my-family");
assertThat(fontFactory.getRegisteredFamily("my-family"))
.containsExactlyInAnyOrder("My-Font-Regular", "My-Font-Bold");
}
}