Skip to content
Merged
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
14 changes: 10 additions & 4 deletions code/src/main/java/com/codeforces/commons/text/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2023,16 +2023,22 @@ 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) {
SimplePair<Rectangle2D, Rectangle2D> rectangles = getRenderingRectangles(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;
}
}
38 changes: 2 additions & 36 deletions code/src/test/java/com/codeforces/commons/text/StringUtilTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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(""));
Expand All @@ -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<String> 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++) {
Expand Down