Skip to content

Commit 87f0a37

Browse files
authored
Add getNullableCharacterWidth-method (#3)
1 parent d2cc64b commit 87f0a37

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<!-- === Project Coordinates === -->
88
<groupId>io.github.chrimle</groupId>
99
<artifactId>fontmeter</artifactId>
10-
<version>0.1.0-alpha.1</version>
10+
<version>0.1.0-alpha.2</version>
1111
<packaging>jar</packaging>
1212

1313
<!-- === Project Metadata === -->

src/main/java/io/github/chrimle/fontmeter/fonts/FontMetrics.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ public static BaselineStep builder() {
3030
return FontMetricsBuilder.newInstance();
3131
}
3232

33+
/**
34+
* {@inheritDoc}
35+
*
36+
* @param character to get the <strong>width</strong> of.
37+
* @param fontSize of the {@code character}. <strong>MUST</strong> be positive.
38+
* @return the width of the {@code character}, in <em>points (pt)</em>. <strong>MAY</strong> be
39+
* {@code null} if the {@code width} cannot be determined.
40+
* @throws IllegalArgumentException if {@code fontSize} is negative, or zero.
41+
* @since 0.1.0
42+
*/
43+
@Override
44+
public Double getNullableCharacterWidth(char character, int fontSize)
45+
throws IllegalArgumentException {
46+
if (fontSize < 1) throw new IllegalArgumentException("`fontSize` MUST be positive");
47+
final var widthMap = fontSizeMap.get(fontSize);
48+
if (widthMap == null) return null;
49+
return widthMap.get(character);
50+
}
51+
3352
/**
3453
* <em>Builder-class</em> for {@link FontMetrics}-instances.
3554
*

src/main/java/io/github/chrimle/fontmeter/fonts/IFontMetrics.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@
1111
*/
1212
public sealed interface IFontMetrics permits FontMetrics {
1313

14+
/**
15+
* Gets the <strong>width</strong> of the given {@code character} and {@code fontSize}. If the
16+
* {@code character} is not supported, {@code null} is returned. If the <strong>width</strong>
17+
* cannot be determined, by not having it pre-calculated, or if <em>this instance</em> does not
18+
* support "on-demand" calculations, then {@code null} is returned.
19+
*
20+
* @param character to get the <strong>width</strong> of. <strong>MUST NOT</strong> be {@code
21+
* null}.
22+
* @param fontSize of the {@code character}. <strong>MUST</strong> be positive.
23+
* @return the width of the {@code character}, in <em>points (pt)</em>. <strong>MAY</strong> be
24+
* {@code null} if the {@code width} cannot be determined.
25+
* @throws NullPointerException if {@code character} is {@code null}.
26+
* @throws IllegalArgumentException if {@code fontSize} is negative, or zero.
27+
* @since 0.1.0
28+
*/
29+
default Double getNullableCharacterWidth(final Character character, final int fontSize)
30+
throws NullPointerException, IllegalArgumentException {
31+
return getNullableCharacterWidth(character.charValue(), fontSize);
32+
}
33+
34+
/**
35+
* Gets the <strong>width</strong> of the given {@code character} and {@code fontSize}. If the
36+
* {@code character} is not supported, {@code null} is returned. If the <strong>width</strong>
37+
* cannot be determined, by not having it pre-calculated, or if <em>this instance</em> does not
38+
* support "on-demand" calculations, then {@code null} is returned.
39+
*
40+
* @param character to get the <strong>width</strong> of.
41+
* @param fontSize of the {@code character}. <strong>MUST</strong> be positive.
42+
* @return the width of the {@code character}, in <em>points (pt)</em>. <strong>MAY</strong> be
43+
* {@code null} if the {@code width} cannot be determined.
44+
* @throws IllegalArgumentException if {@code fontSize} is negative, or zero.
45+
* @since 0.1.0
46+
*/
47+
Double getNullableCharacterWidth(final char character, final int fontSize)
48+
throws IllegalArgumentException;
49+
1450
/**
1551
* The <em>Builder</em>-step for configuring the <em>baseline</em> for {@link IFontMetrics}. The
1652
* <em>baseline</em> is a {@link Map} of supported {@link Character}s and their respective

src/test/java/io/github/chrimle/fontmeter/fonts/FontMetricsTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,67 @@ void testEmptyBaselineMapThrows() {
7272
}
7373
}
7474
}
75+
76+
@Nested
77+
class GetNullableCharacterWidthTests {
78+
79+
private final IFontMetrics fontMetrics =
80+
FontMetrics.builder()
81+
.setBaseline(7, Map.of('x', 42d))
82+
.skipPreCalculation()
83+
.disableOnDemandCalculations()
84+
.build();
85+
86+
@Nested
87+
class CharacterTests {
88+
@Test
89+
void testNullCharacterThrowsNPE() {
90+
assertThrows(
91+
NullPointerException.class, () -> fontMetrics.getNullableCharacterWidth(null, 7));
92+
}
93+
94+
@Test
95+
void testUnsupportedCharacterReturnsNull() {
96+
assertNull(fontMetrics.getNullableCharacterWidth('z', 7));
97+
}
98+
99+
@Test
100+
void testSupportedCharacterReturnsExpectedWidth() {
101+
assertEquals(42d, fontMetrics.getNullableCharacterWidth('x', 7));
102+
}
103+
}
104+
105+
@Nested
106+
class FontSizeTests {
107+
@ParameterizedTest
108+
@ValueSource(ints = {Integer.MIN_VALUE, -42, -7, -1})
109+
void testNegativeFontSizeThrowsIAE(final int fontSize) {
110+
assertThrows(
111+
IllegalArgumentException.class,
112+
() -> fontMetrics.getNullableCharacterWidth('x', fontSize));
113+
}
114+
115+
@Test
116+
void testZeroFontSizeThrowsIAE() {
117+
assertThrows(
118+
IllegalArgumentException.class, () -> fontMetrics.getNullableCharacterWidth('x', 0));
119+
}
120+
121+
@ParameterizedTest
122+
@ValueSource(ints = {1, 3, 7, 11, 42, Integer.MAX_VALUE})
123+
void testPositiveFontSizeDoesNotThrow(final int fontSize) {
124+
assertDoesNotThrow(() -> fontMetrics.getNullableCharacterWidth('x', fontSize));
125+
}
126+
127+
@Test
128+
void testUnsupportedFontSizeReturnsNull() {
129+
assertNull(fontMetrics.getNullableCharacterWidth('x', 11));
130+
}
131+
132+
@Test
133+
void testSupportedFontSizeReturnsExpectedWidth() {
134+
assertEquals(42d, fontMetrics.getNullableCharacterWidth('x', 7));
135+
}
136+
}
137+
}
75138
}

0 commit comments

Comments
 (0)