@Test public void testFontRangeWithUnrenderableCompositeCharAtStart() { Font font = FontCache.getDefaultInstance().getFont("Bitstream Vera Sans"); Font fallbackFont = FontCache.getDefaultInstance().getFont("Droid Sans Fallback"); String compositeChar = "\ud859\ude26"; String remainingChars = "\u5b57\u5b57\u5b57\u5b57\u5b57\u5b57\u5b57\u5b57"; LabelSplitter splitter = new LabelSplitter(); String text = compositeChar + remainingChars; Font[] fonts = {font, font, font, font, font, fallbackFont}; List<FontRange> ranges = splitter.buildFontRanges(text, fonts); assertEquals(2, ranges.size()); FontRange startRange = ranges.get(0); assertEquals(0, startRange.startChar); assertEquals(2, startRange.endChar); assertEquals(font, startRange.font); assertEquals(compositeChar, startRange.text); FontRange endRange = ranges.get(1); assertEquals(2, endRange.startChar); assertEquals(10, endRange.endChar); assertEquals(fallbackFont, endRange.font); assertEquals(remainingChars, endRange.text); }
List<FontRange> buildFontRanges(String text, Font[] fonts) { if (fonts.length == 1) { return Arrays.asList(new FontRange(text, 0, text.length(), fonts[0])); } List<FontRange> result = new ArrayList<>(); int start = 0; int lastSupportedChar = 0; char[] chars = text.toCharArray(); while (start < chars.length) { for (int i = 0; i < fonts.length; ) { Font font = fonts[i]; int newPosition = font.canDisplayUpTo(chars, start, chars.length); if (newPosition == -1) { result.add(new FontRange(text, start, chars.length, font)); start = chars.length; lastSupportedChar = start; break; } else if (newPosition > start) { result.add(new FontRange(text, start, newPosition, font)); start = newPosition; lastSupportedChar = start; if (i > 0) { i = 0; } } else { i++; } } if (start < chars.length) { int base = start; start++; boolean foundFont = false; while (start < chars.length && !foundFont) { char curr = chars[start]; for (int i = 0; i < fonts.length; i++) { Font font = fonts[i]; if (font.canDisplay(curr)) { foundFont = true; result.add(new FontRange(text, base, start, fonts[0])); break; } } if (!foundFont) { start++; } } } } if (lastSupportedChar < chars.length) { result.add(new FontRange(text, lastSupportedChar, chars.length, fonts[0])); } return result; }
LabelSplitter { List<FontRange> buildFontRanges(String text, Font[] fonts) { if (fonts.length == 1) { return Arrays.asList(new FontRange(text, 0, text.length(), fonts[0])); } List<FontRange> result = new ArrayList<>(); int start = 0; int lastSupportedChar = 0; char[] chars = text.toCharArray(); while (start < chars.length) { for (int i = 0; i < fonts.length; ) { Font font = fonts[i]; int newPosition = font.canDisplayUpTo(chars, start, chars.length); if (newPosition == -1) { result.add(new FontRange(text, start, chars.length, font)); start = chars.length; lastSupportedChar = start; break; } else if (newPosition > start) { result.add(new FontRange(text, start, newPosition, font)); start = newPosition; lastSupportedChar = start; if (i > 0) { i = 0; } } else { i++; } } if (start < chars.length) { int base = start; start++; boolean foundFont = false; while (start < chars.length && !foundFont) { char curr = chars[start]; for (int i = 0; i < fonts.length; i++) { Font font = fonts[i]; if (font.canDisplay(curr)) { foundFont = true; result.add(new FontRange(text, base, start, fonts[0])); break; } } if (!foundFont) { start++; } } } } if (lastSupportedChar < chars.length) { result.add(new FontRange(text, lastSupportedChar, chars.length, fonts[0])); } return result; } }
LabelSplitter { List<FontRange> buildFontRanges(String text, Font[] fonts) { if (fonts.length == 1) { return Arrays.asList(new FontRange(text, 0, text.length(), fonts[0])); } List<FontRange> result = new ArrayList<>(); int start = 0; int lastSupportedChar = 0; char[] chars = text.toCharArray(); while (start < chars.length) { for (int i = 0; i < fonts.length; ) { Font font = fonts[i]; int newPosition = font.canDisplayUpTo(chars, start, chars.length); if (newPosition == -1) { result.add(new FontRange(text, start, chars.length, font)); start = chars.length; lastSupportedChar = start; break; } else if (newPosition > start) { result.add(new FontRange(text, start, newPosition, font)); start = newPosition; lastSupportedChar = start; if (i > 0) { i = 0; } } else { i++; } } if (start < chars.length) { int base = start; start++; boolean foundFont = false; while (start < chars.length && !foundFont) { char curr = chars[start]; for (int i = 0; i < fonts.length; i++) { Font font = fonts[i]; if (font.canDisplay(curr)) { foundFont = true; result.add(new FontRange(text, base, start, fonts[0])); break; } } if (!foundFont) { start++; } } } } if (lastSupportedChar < chars.length) { result.add(new FontRange(text, lastSupportedChar, chars.length, fonts[0])); } return result; }  }
LabelSplitter { List<FontRange> buildFontRanges(String text, Font[] fonts) { if (fonts.length == 1) { return Arrays.asList(new FontRange(text, 0, text.length(), fonts[0])); } List<FontRange> result = new ArrayList<>(); int start = 0; int lastSupportedChar = 0; char[] chars = text.toCharArray(); while (start < chars.length) { for (int i = 0; i < fonts.length; ) { Font font = fonts[i]; int newPosition = font.canDisplayUpTo(chars, start, chars.length); if (newPosition == -1) { result.add(new FontRange(text, start, chars.length, font)); start = chars.length; lastSupportedChar = start; break; } else if (newPosition > start) { result.add(new FontRange(text, start, newPosition, font)); start = newPosition; lastSupportedChar = start; if (i > 0) { i = 0; } } else { i++; } } if (start < chars.length) { int base = start; start++; boolean foundFont = false; while (start < chars.length && !foundFont) { char curr = chars[start]; for (int i = 0; i < fonts.length; i++) { Font font = fonts[i]; if (font.canDisplay(curr)) { foundFont = true; result.add(new FontRange(text, base, start, fonts[0])); break; } } if (!foundFont) { start++; } } } } if (lastSupportedChar < chars.length) { result.add(new FontRange(text, lastSupportedChar, chars.length, fonts[0])); } return result; }  List<LineInfo> layout(LabelCacheItem labelItem, Graphics2D graphics); }
LabelSplitter { List<FontRange> buildFontRanges(String text, Font[] fonts) { if (fonts.length == 1) { return Arrays.asList(new FontRange(text, 0, text.length(), fonts[0])); } List<FontRange> result = new ArrayList<>(); int start = 0; int lastSupportedChar = 0; char[] chars = text.toCharArray(); while (start < chars.length) { for (int i = 0; i < fonts.length; ) { Font font = fonts[i]; int newPosition = font.canDisplayUpTo(chars, start, chars.length); if (newPosition == -1) { result.add(new FontRange(text, start, chars.length, font)); start = chars.length; lastSupportedChar = start; break; } else if (newPosition > start) { result.add(new FontRange(text, start, newPosition, font)); start = newPosition; lastSupportedChar = start; if (i > 0) { i = 0; } } else { i++; } } if (start < chars.length) { int base = start; start++; boolean foundFont = false; while (start < chars.length && !foundFont) { char curr = chars[start]; for (int i = 0; i < fonts.length; i++) { Font font = fonts[i]; if (font.canDisplay(curr)) { foundFont = true; result.add(new FontRange(text, base, start, fonts[0])); break; } } if (!foundFont) { start++; } } } } if (lastSupportedChar < chars.length) { result.add(new FontRange(text, lastSupportedChar, chars.length, fonts[0])); } return result; }  List<LineInfo> layout(LabelCacheItem labelItem, Graphics2D graphics);  }