diff --git a/code/src/main/java/com/codeforces/commons/text/StringUtil.java b/code/src/main/java/com/codeforces/commons/text/StringUtil.java index b5dbfab..712e5ba 100644 --- a/code/src/main/java/com/codeforces/commons/text/StringUtil.java +++ b/code/src/main/java/com/codeforces/commons/text/StringUtil.java @@ -2023,8 +2023,11 @@ public static int getRenderingWidth(String s) { if (rectangles == null) { return trimToEmpty(s).length(); } - return (int) Math.ceil(Objects.requireNonNull(rectangles.getSecond()).getWidth() - / Objects.requireNonNull(rectangles.getFirst()).getWidth() - 1E-7); + + double width = Objects.requireNonNull(rectangles.getSecond()).getWidth(); + int result = (int) Math.round(width + / Objects.requireNonNull(rectangles.getFirst()).getWidth()); + return result == 0 && width > 0 ? 1 : result; } public static int getRenderingHeight(String s) { @@ -2032,7 +2035,10 @@ public static int getRenderingHeight(String s) { if (rectangles == null) { return getCharCount(trimToEmpty(s), '\n') + 1; } - return (int) Math.ceil(Objects.requireNonNull(rectangles.getSecond()).getHeight() - / Objects.requireNonNull(rectangles.getFirst()).getHeight() - 1E-7); + + double height = Objects.requireNonNull(rectangles.getSecond()).getHeight(); + int result = (int) Math.round(height + / Objects.requireNonNull(rectangles.getFirst()).getHeight()); + return result == 0 && height > 0 ? 1 : result; } } diff --git a/code/src/test/java/com/codeforces/commons/text/StringUtilTest.java b/code/src/test/java/com/codeforces/commons/text/StringUtilTest.java index 031383b..29700cc 100644 --- a/code/src/test/java/com/codeforces/commons/text/StringUtilTest.java +++ b/code/src/test/java/com/codeforces/commons/text/StringUtilTest.java @@ -1,16 +1,9 @@ package com.codeforces.commons.text; -import com.codeforces.commons.io.IoUtil; import com.codeforces.commons.text.similarity.SimilarityChecker; -import com.codeforces.commons.xml.XmlUtilTest; import org.apache.commons.lang3.StringUtils; import org.junit.Test; -import java.awt.*; -import java.awt.font.FontRenderContext; -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.io.InputStream; import java.util.*; import java.util.List; import java.util.function.Predicate; @@ -28,6 +21,7 @@ public class StringUtilTest { " \r\n\t%c%c%c", StringUtil.ZERO_WIDTH_SPACE, StringUtil.THIN_SPACE, StringUtil.NON_BREAKING_SPACE ); + @SuppressWarnings("ConstantValue") @Test public void testIsEmpty() { assertTrue(StringUtil.isEmpty(null)); @@ -299,6 +293,7 @@ public void testLooksLike() { @Test public void testGetRenderingWidth() { + assertEquals(1, StringUtil.getRenderingWidth("a")); assertEquals(1, StringUtil.getRenderingWidth("1")); assertEquals(0, StringUtil.getRenderingWidth(null)); assertEquals(0, StringUtil.getRenderingWidth("")); @@ -318,35 +313,6 @@ public void testGetRenderingHeight() { assertEquals(1, StringUtil.getRenderingHeight("βΈ»")); } -// @Test -// public void testTopHandles() { -// Scanner scanner = new Scanner(getClass().getResourceAsStream("/com/codeforces/commons/text/tophandles.txt")); -// List handles = new ArrayList<>(8000); -// while (scanner.hasNextLine()) { -// String s = scanner.nextLine(); -// if (StringUtil.isNotBlank(s)) { -// s = s.trim(); -// handles.add(s); -// } -// } -// -// int cnt = 0; -// for (String handle1 : handles) { -// for (String handle2 : handles) { -// if (!StringUtil.equals(handle1, handle2)) { -// if (StringUtil.isSimilar(handle1, handle2)) { -// System.out.print(handle1); -// System.out.print(" "); -// System.out.println(handle2); -// cnt++; -// } -// } -// } -// } -// System.err.println(cnt); -// System.err.println((handles.size() - 1) * handles.size() / 2); -// } - private static void internalTestUnicodeTr39(String... strings) { for (int i = 0; i < strings.length; i++) { for (int j = 0; j < strings.length; j++) {