Buggy code:
public class Snippet205 { public static void main ( String [ ] args ) { Display display = new Display ( ) ; final Shell shell = new Shell ( display , ( ( SWT . SHELL_TRIM ) | ( SWT . DOUBLE_BUFFERED ) ) ) ; <START_BUG> shell . setText ( "Embedding<seq2seq4repair_space>Objects<seq2seq4repair_space>in<seq2seq4repair_space>StyledText" ) ; <END_BUG> final Image [ ] images = new Image [ ] { new Image ( display , 32 , 32 ) , new Image ( display , 20 , 40 ) , new Image ( display , 40 , 20 ) } ; int [ ] colors = new int [ ] { SWT . COLOR_BLUE , SWT . COLOR_MAGENTA , SWT . COLOR_GREEN } ; for ( int i = 0 ; i < ( images . length ) ; i ++ ) { GC gc = new GC ( images [ i ] ) ; gc . setBackground ( display . getSystemColor ( colors [ i ] ) ) ; gc . fillRectangle ( images [ i ] . getBounds ( ) ) ; gc . dispose ( ) ; } final Button button = new Button ( shell , SWT . PUSH ) ; button . setText ( "Button" ) ; button . pack ( ) ; String text = "Here<seq2seq4repair_space>is<seq2seq4repair_space>some<seq2seq4repair_space>text<seq2seq4repair_space>with<seq2seq4repair_space>a<seq2seq4repair_space>blue<seq2seq4repair_space>image<seq2seq4repair_space>￼,<seq2seq4repair_space>a<seq2seq4repair_space>magenta<seq2seq4repair_space>image<seq2seq4repair_space>￼,<seq2seq4repair_space>a<seq2seq4repair_space>green<seq2seq4repair_space>image<seq2seq4repair_space>￼,<seq2seq4repair_space>and<seq2seq4repair_space>a<seq2seq4repair_space>button:<seq2seq4repair_space>￼." ; final int [ ] imageOffsets = new int [ ] { 36 , 55 , 72 } ; final TextLayout layout = new TextLayout ( display ) ; layout . setText ( text ) ; for ( int i = 0 ; i < ( images . length ) ; i ++ ) { Rectangle bounds = images [ i ] . getBounds ( ) ; TextStyle imageStyle = new TextStyle ( null , null , null ) ; imageStyle . metrics = new GlyphMetrics ( bounds . height , 0 , bounds . width ) ; layout . setStyle ( imageStyle , imageOffsets [ i ] , imageOffsets [ i ] ) ; } Rectangle bounds = button . getBounds ( ) ; TextStyle buttonStyle = new TextStyle ( null , null , null ) ; buttonStyle . metrics = new GlyphMetrics ( bounds . height , 0 , bounds . width ) ; final int buttonOffset = ( text . length ( ) ) - 2 ; layout . setStyle ( buttonStyle , buttonOffset , buttonOffset ) ; shell . addListener ( Paint , new Listener ( ) { public void handleEvent ( Event event ) { GC gc = event . gc ; Point margin = new Point ( 10 , 10 ) ; layout . setWidth ( ( ( shell . getClientArea ( ) . width ) - ( 2 * ( margin . x ) ) ) ) ; layout . draw ( event . gc , margin . x , margin . y ) ; for ( int i = 0 ; i < ( images . length ) ; i ++ ) { int offset = imageOffsets [ i ] ; int lineIndex = layout . getLineIndex ( offset ) ; FontMetrics lineMetrics = layout . getLineMetrics ( lineIndex ) ; Point point = layout . getLocation ( offset , false ) ; GlyphMetrics glyphMetrics = layout . getStyle ( offset ) . metrics ; gc . drawImage ( images [ i ] , ( ( point . x ) + ( margin . x ) ) , ( ( ( ( point . y ) + ( margin . y ) ) + ( lineMetrics . getAscent ( ) ) ) - ( glyphMetrics . ascent ) ) ) ; } int lineIndex = layout . getLineIndex ( buttonOffset ) ; FontMetrics lineMetrics = layout . getLineMetrics ( lineIndex ) ; Point point = layout . getLocation ( buttonOffset , false ) ; GlyphMetrics glyphMetrics = layout . getStyle ( buttonOffset ) . metrics ; button . setLocation ( ( ( point . x ) + ( margin . x ) ) , ( ( ( ( point . y ) + ( margin . y ) ) + ( lineMetrics . getAscent ( ) ) ) - ( glyphMetrics . ascent ) ) ) ; } } ) ; shell . open ( ) ; while ( ! ( shell . isDisposed ( ) ) ) { if ( ! ( display . readAndDispatch ( ) ) ) display . sleep ( ) ; } layout . dispose ( ) ; for ( int i = 0 ; i < ( images . length ) ; i ++ ) { images [ i ] . dispose ( ) ; } display . dispose ( ) ; } }

Fix:
shell . setText ( "Embedding<seq2seq4repair_space>objects<seq2seq4repair_space>in<seq2seq4repair_space>text" ) ;