private void cleanup() { if (textChunks == null) return; for (StyledText chunk : textChunks) chunk.teardownStyles(); textChunks = null; }
@Test public void unrecognizedInterlacedStyle() { createStyles(); parent.setSize(200, 100); panel.setText( "This is <my_other_style>some </my_other_style><bogus_style>fantastic</bogus_style><my_style>text</my_style>", parent); panel.buildLines(); List<StyledText> chunks = panel.getTextChunks(); assertEquals(4, chunks.size()); StyledText interlacedLayout = chunks.get(2); assertNoSubString("name=Cuneiform", interlacedLayout.toString()); assertNoSubString("size=19", interlacedLayout.toString()); }
private AttributedString prepareAttributedString() { List<StyledText> textChunks = getTextChunks(); StringBuilder buf = new StringBuilder(); for (StyledText textChunk : textChunks) { buf.append(textChunk.getText()); } AttributedString attributedString = new AttributedString(buf.toString()); int startIndex = 0; int endIndex = 0; for (StyledText textChunk : textChunks) { if (textChunk == textChunks.get(textChunks.size() - 1)) endIndex = buf.length(); else endIndex = startIndex + textChunk.getText().length(); attributedString.addAttribute(TextAttribute.FONT, textChunk.getFont(), startIndex, endIndex); attributedString.addAttribute( TextAttribute.FOREGROUND, textChunk.getColor(), startIndex, endIndex); startIndex += textChunk.getText().length(); } return attributedString; }
@Test public void styledInheritsFromDefault() { createStyles(); parent.setSize(200, 100); panel.setText("<size_only_style>This some text</size_only_style>", parent); panel.buildLines(); List<TextLayout> lines = panel.getLines(); assertEquals(1, lines.size()); String onlyLine = lines.get(0).toString(); assertSubString("name=" + defaultFontFace, onlyLine); assertSubString("size=" + "25", onlyLine); assertSubString("style=" + defaultFontStyle, onlyLine); StyledText first = panel.getTextChunks().get(0); assertEquals(defaultTextColor, first.getColor()); }
public synchronized void buildLines() { consumableArea = panel.getChildConsumableBounds(); lines = new LinkedList<TextLayout>(); if (text != null && text.length() > 0) { StyledTextParser parser = new StyledTextParser(); textChunks = parser.parse(text); final Scene root = getRoot(); if (root != null) // TODO MDM - It happens.... but how? Ah! Need to acquire tree lock when // removing panels. { Map<String, RichStyle> styleMap = root.getStyles(); for (StyledText styledText : textChunks) styledText.setupStyles(styleMap, getStyle(), this); // TODO MDM StyleObservers may cause a memory leak. Styles keep track of panels that are no // longer used? addLines(); } } }
@Test public void textColor() throws Exception { createStyles(); panel.setText("text <my_other_style>here</my_other_style> man", parent); panel.buildLines(); StyledText first = panel.getTextChunks().get(0); assertEquals(defaultTextColor, first.getColor()); StyledText second = panel.getTextChunks().get(1); assertEquals(new Color(0x0000FF), second.getColor()); StyledText third = panel.getTextChunks().get(2); assertEquals(defaultTextColor, third.getColor()); }
@Test public void multipleStylesAppliedToLine() throws Exception { createStyles(); parent.setSize(200, 100); panel.setText("<my_style>some </my_style><my_other_style>text</my_other_style>", parent); panel.buildLines(); List<StyledText> chunks = panel.getTextChunks(); StyledText layout = chunks.get(0); assertEquals(5, layout.getText().length()); assertSubString("family=Helvetica", layout.toString()); assertSubString("name=Helvetica", layout.toString()); assertSubString("style=bold", layout.toString()); assertSubString("size=20", layout.toString()); StyledText layout2 = chunks.get(1); assertEquals(5, layout.getText().length()); assertSubString("family=Dialog", layout2.toString()); assertSubString("name=Cuneiform", layout2.toString()); assertSubString("style=italic", layout2.toString()); assertSubString("size=19", layout2.toString()); }