From 7f01c67a96493fc5d6275a25414dd0841a2617b9 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Wed, 13 Sep 2023 12:43:32 +0100 Subject: [PATCH] FOP-3148: NoSuchElementException when using font with no family name --- .../apache/fop/svg/font/FOPFontFamilyResolverImpl.java | 7 ++++++- .../fop/svg/font/FOPFontFamilyResolverTestCase.java | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/fop-core/src/main/java/org/apache/fop/svg/font/FOPFontFamilyResolverImpl.java b/fop-core/src/main/java/org/apache/fop/svg/font/FOPFontFamilyResolverImpl.java index 4305838da..a4e743571 100644 --- a/fop-core/src/main/java/org/apache/fop/svg/font/FOPFontFamilyResolverImpl.java +++ b/fop-core/src/main/java/org/apache/fop/svg/font/FOPFontFamilyResolverImpl.java @@ -21,6 +21,7 @@ package org.apache.fop.svg.font; import java.io.InputStream; import java.util.Map; +import java.util.Set; import org.apache.batik.bridge.FontFace; import org.apache.batik.gvt.font.GVTFontFace; @@ -68,7 +69,11 @@ public class FOPFontFamilyResolverImpl implements FOPFontFamilyResolver { Map fonts = fontInfo.getFonts(); for (Typeface font : fonts.values()) { if (font.hasChar(c)) { - String fontFamily = font.getFamilyNames().iterator().next(); + Set familyNames = font.getFamilyNames(); + String fontFamily = font.getFontName(); + if (!familyNames.isEmpty()) { + fontFamily = familyNames.iterator().next(); + } return new FOPGVTFontFamily(fontInfo, fontFamily, new FontTriplet(fontFamily, Font.STYLE_NORMAL, Font.WEIGHT_NORMAL), new GVTFontFace(fontFamily)); diff --git a/fop-core/src/test/java/org/apache/fop/svg/font/FOPFontFamilyResolverTestCase.java b/fop-core/src/test/java/org/apache/fop/svg/font/FOPFontFamilyResolverTestCase.java index d14752b90..4376779f9 100644 --- a/fop-core/src/test/java/org/apache/fop/svg/font/FOPFontFamilyResolverTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/svg/font/FOPFontFamilyResolverTestCase.java @@ -42,6 +42,7 @@ import static org.junit.Assert.assertTrue; import org.apache.batik.gvt.font.GVTFontFamily; import org.apache.batik.gvt.font.GVTLineMetrics; +import org.apache.fop.fonts.CustomFont; import org.apache.fop.fonts.FontInfo; public class FOPFontFamilyResolverTestCase { @@ -79,6 +80,14 @@ public class FOPFontFamilyResolverTestCase { assertNull(family); } + @Test + public void testGetFamilyThatCanDisplayNoFamily() { + CustomFont font = (CustomFont) fontInfo.getFonts().values().iterator().next(); + font.setFamilyNames(Collections.emptySet()); + GVTFontFamily family = resolver.getFamilyThatCanDisplay('\u0180'); + assertEquals(font.getFontName(), family.getFamilyName()); + } + @Test public void testDeriveFont() { FOPGVTFontFamily family = resolver.resolve(FontInfoBuilder.DEJAVU_LGC_SERIF); -- 2.39.5