Skip to content

Commit

Permalink
fix issues in CSS conversion; add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 19, 2023
1 parent 6dccf73 commit 5971b8a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
19 changes: 12 additions & 7 deletions utility/src/main/java/com/dua3/utility/text/FontDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ public static FontDef parseCssFontDef(String fontdef) {
FontDef fd = new FontDef();

for (String rule : fontdef.split(";")) {
if (rule.isBlank()) {
continue;
}

Pair<String, String> pair = parseCssRule(rule);

String attribute = pair.first().toLowerCase(Locale.ROOT);
Expand Down Expand Up @@ -270,7 +274,7 @@ public String fontspec() {
LangUtil.triStateSelect(bold, "-bold", "-regular", "-*") +
LangUtil.triStateSelect(italic, "-italic", "-normal", "-*") +
LangUtil.triStateSelect(underline, "-underline", "-none", "-*") +
LangUtil.triStateSelect(strikeThrough, "-strikethrough", "-no_line", "*") +
LangUtil.triStateSelect(strikeThrough, "-strikethrough", "-no_line", "-*") +
'-' + (size != null ? size : "*") +
'-' + (color != null ? color.toCss() : "*");
}
Expand Down Expand Up @@ -408,17 +412,18 @@ public String getCssStyle() {
boolean isUnderline = underline != null && underline;
boolean isStrikeThrough = strikeThrough != null && strikeThrough;
//noinspection StringConcatenationMissingWhitespace
return (color == null ? "" : "color: " + color + ";") +
(size == null ? "" : "font-size: " + size + "pt;") +
(family == null ? "" : "font-family: " + family + ";") +
(bold == null ? "" : "font-weight: " + (bold ? "bold" : "normal") + ";") +
(italic == null ? "" : "font-style: " + (italic ? "italic" : "normal") + ";") +
String css = (color == null ? "" : " color: " + color + ";") +
(size == null ? "" : " font-size: " + size + "pt;") +
(family == null ? "" : " font-family: " + family + ";") +
(bold == null ? "" : " font-weight: " + (bold ? "bold" : "normal") + ";") +
(italic == null ? "" : " font-style: " + (italic ? "italic" : "normal") + ";") +
(isStrikeThrough || isUnderline
? "text-decoration:" +
? " text-decoration:" +
(isUnderline ? " underline" : "") +
(isStrikeThrough ? " line-through" : "") +
";"
: "");
return css.stripLeading();
}

/**
Expand Down
52 changes: 50 additions & 2 deletions utility/src/test/java/com/dua3/utility/text/FontDefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class FontDefTest {
@Test
Expand Down Expand Up @@ -53,8 +54,8 @@ void parseFontspec() {
String fontspec = "TimesNewRoman-bold-italic-12-red";
FontDef result = FontDef.parseFontspec(fontspec);
Assertions.assertEquals("TimesNewRoman", result.getFamily());
Assertions.assertTrue(result.getBold());
Assertions.assertTrue(result.getItalic());
assertTrue(result.getBold());
assertTrue(result.getItalic());
Assertions.assertEquals(12, result.getSize());
Assertions.assertEquals(Color.valueOf("red"), result.getColor());
}
Expand All @@ -70,4 +71,51 @@ void parseFontspecThrowsIllegalArgumentExceptionWhenSizeInvalid() {
String fontspec = "TimesNewRoman-bold-italic-undefinedSize-red";
Assertions.assertThrows(IllegalArgumentException.class, () -> FontDef.parseFontspec(fontspec));
}

@Test
public void testParseCssFontDef() {
String fontdef = "{ font-size: 14px; color: #FFFFFF; font-family: Arial; font-weight: bold; font-style: italic; }";

FontDef fd = FontDef.parseCssFontDef(fontdef);

assertEquals(fd.getSize(), 10.5f); // 14px = 10.5pt
assertEquals(fd.getColor(), Color.WHITE);
assertEquals(fd.getFamily(), "Arial");
assertTrue(fd.getBold());
assertTrue(fd.getItalic());
}

@Test
public void testFontspec() {
FontDef fd = new FontDef();

fd.setSize(14.0f);
fd.setColor(Color.WHITE);
fd.setFamily("Arial");
fd.setBold(true);
fd.setItalic(true);

// assuming the fontspec() method returns a font specification in the format 'family-bold/regular/italic-14.0-#FFFFFF'
String expectedFontSpec = "Arial-bold-italic-*-*-14.0-#ffffff";
String actualFontSpec = fd.fontspec();

// put your appropriate assertions here
assertEquals(expectedFontSpec, actualFontSpec);
}

@Test
void getCssStyle() {
FontDef fd = new FontDef();

fd.setSize(14.0f);
fd.setColor(Color.WHITE);
fd.setFamily("Arial");
fd.setBold(true);
fd.setItalic(true);

String expected = "color: #ffffff; font-size: 14.0pt; font-family: Arial; font-weight: bold; font-style: italic;";
String actual = fd.getCssStyle();

assertEquals(expected, actual);
}
}

0 comments on commit 5971b8a

Please sign in to comment.