<commit_before>package prefuse;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;

import prefuse.activity.Activity;
import prefuse.activity.SlowInSlowOutPacer;
import prefuse.controls.Control;
import prefuse.data.expression.AndPredicate;
import prefuse.data.expression.BooleanLiteral;
import prefuse.data.expression.Predicate;
import prefuse.data.expression.parser.ExpressionParser;
import prefuse.render.Renderer;
import prefuse.util.ColorLib;
import prefuse.util.StringLib;
import prefuse.util.UpdateListener;
import prefuse.util.collections.CopyOnWriteArrayList;
import prefuse.util.display.BackgroundPainter;
import prefuse.util.display.Clip;
import prefuse.util.display.DebugStatsPainter;
import prefuse.util.display.ExportDisplayAction;
import prefuse.util.display.ItemBoundsListener;
import prefuse.util.display.PaintListener;
import prefuse.util.display.RenderingQueue;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;
import prefuse.visual.sort.ItemSorter;


/**
 * <p>User interface component that provides an interactive view onto
 * a visualization. The Display is responsible for drawing items to the
 * screen and providing callbacks for user interface actions such as
 * mouse and keyboard events. A Display must be associated with an
 * {@link prefuse.Visualization} from which it pulls the items to visualize.
 * </p>
 * 
 * <p>To control which {@link prefuse.visual.VisualItem} instances are
 * drawn, the Display also maintains an optional 
 * {@link prefuse.data.expression.Predicate} for filtering items. The
 * drawing order of items is
 * controlled by an {@link prefuse.visual.sort.ItemSorter} instance,
 * which calculates a score for each item. Items with higher scores
 * are drawn later, and hence on top of lower scoring items.
 * </p>
 * 
 * <p>The {@link prefuse.controls.Control Control}
 * interface provides the user interface callbacks for supporting
 * interaction. The {@link prefuse.controls} package contains a number
 * of pre-built <code>Control</code> implementations for common
 * interactions.</p>
 * 
 * <p>The Display class also supports arbitrary graphics transforms through
 * the <code>java.awt.geom.AffineTransform</code> class. The 
 * {@link #setTransform(java.awt.geom.AffineTransform) setTransform} method
 * allows arbitrary transforms to be applied, while the 
 * {@link #pan(double,double) pan} and 
 * {@link #zoom(java.awt.geom.Point2D,double) zoom}
 * methods provide convenience methods that appropriately update the current
 * transform to achieve panning and zooming of the presentation space.</p>
 * 
 * <p>Additionally, each Display instance also supports use of a text editor
 * to facilitate direct editing of text. See the various
 * {@link #editText(prefuse.visual.VisualItem, String)} methods.</p>
 * 
 * @version 1.0
 * @author <a href="http://jheer.org"><NAME></a>
 * @see Visualization
 * @see prefuse.controls.Control
 * @see prefuse.controls
 */
public class Display extends JComponent {

    private static final Logger s_logger 
        = Logger.getLogger(Display.class.getName());
    
    // visual item source
    protected Visualization m_vis;
    protected AndPredicate  m_predicate = new AndPredicate();
    
    // listeners
    protected CopyOnWriteArrayList m_controls = new CopyOnWriteArrayList();
    protected CopyOnWriteArrayList m_painters;
    protected CopyOnWriteArrayList m_bounders;
    
    // display
    protected BufferedImage m_offscreen;
    protected Clip          m_clip = new Clip();
    protected Clip          m_screen = new Clip();
    protected Clip          m_bounds = new Clip();
    protected Rectangle2D   m_rclip = new Rectangle2D.Double();
    protected boolean       m_damageRedraw = true;
    protected boolean       m_highQuality = false;
    
    // optional background image
    protected BackgroundPainter m_bgpainter = null;
    
    // rendering queue
    protected RenderingQueue m_queue = new RenderingQueue();
    protected int            m_visibleCount = 0;
    
    // transform variables
    protected AffineTransform   m_transform  = new AffineTransform();
    protected AffineTransform   m_itransform = new AffineTransform();
    protected TransformActivity m_transact = new TransformActivity();
    protected Point2D m_tmpPoint = new Point2D.Double();
    
    // frame count and debugging output
    protected double frameRate;
    protected int nframes = 0;
    private int sampleInterval = 10;
    private long mark = -1L;
    
    // text editing variables
    private JTextComponent m_editor;
    private boolean        m_editing;
    private VisualItem     m_editItem;
    private String         m_editAttribute;
    
    /**
     * Creates a new Display instance. You will need to associate this
     * Display with a {@link Visualization} for it to display anything.
     */
    public Display() {
        this(null);
    }
    
    /**
     * Creates a new Display associated with the given Visualization.
     * By default, all {@link prefuse.visual.VisualItem} instances in the
     * {@link Visualization} will be drawn by the Display.
     * @param visualization the {@link Visualization} backing this Display
     */
    public Display(Visualization visualization) {
        this(visualization, (Predicate)null);
    }
    
    /**
     * Creates a new Display associated with the given Visualization that
     * draws all VisualItems in the visualization that pass the given
     * Predicate. The predicate string will be parsed by the
     * {@link prefuse.data.expression.parser.ExpressionParser} to get a
     * {@link prefuse.data.expression.Predicate} instance.
     * @param visualization the {@link Visualization} backing this Display
     * @param predicate a predicate expression in the prefuse expression
     * language. This expression will be parsed; if the parsing fails or does
     * not result in a Predicate instance, an exception will result.
     */
    public Display(Visualization visualization, String predicate) {
        this(visualization,
                (Predicate)ExpressionParser.parse(predicate, true));
    }
    
    /**
     * Creates a new Display associated with the given Visualization that
     * draws all VisualItems in the visualization that pass the given
     * Predicate.
     * @param visualization the {@link Visualization} backing this Display
     * @param predicate the filtering {@link prefuse.data.expression.Predicate}
     */
    public Display(Visualization visualization, Predicate predicate) {
        setDoubleBuffered(false);
        setBackground(Color.WHITE);
        
        // initialize text editor
        m_editing = false;
        m_editor = new JTextField();
        m_editor.setBorder(null);
        m_editor.setVisible(false);
        this.add(m_editor);
        
        // register input event capturer
        InputEventCapturer iec = new InputEventCapturer();
        addMouseListener(iec);
        addMouseMotionListener(iec);
        addMouseWheelListener(iec);
        addKeyListener(iec);
        
        registerDefaultCommands();
        
        // invalidate the display when the filter changes
        m_predicate.addExpressionListener(new UpdateListener() {
            protected void update(Object src) { damageReport(); }
        });
        
        setVisualization(visualization);
        setPredicate(predicate);
        setSize(400,400); // set a default size
    }
    
    /**
     * Registers default keystroke commands on the Display. The default
     * commands are
     * <ul><li><b>ctrl D</b> - Toggle debug info display</li>
     *     <li><b>ctrl H</b> - Toggle high quality rendering</li>
     *     <li><b>ctrl E</b> - Export display view to an image file</li></ul>
     * Subclasses can override this method to prevent these commands from
     * being set. Additional commands can be registered using the
     * <code>registerKeyboardAction</code> method.
     */
    protected void registerDefaultCommands() {
        // add debugging output control
        registerKeyboardAction(new ActionListener() {
            private PaintListener m_debug = null;

            public void actionPerformed(ActionEvent e) {
                if (m_debug == null) {
                    m_debug = new DebugStatsPainter();
                    addPaintListener(m_debug);
                } else {
                    removePaintListener(m_debug);
                    m_debug = null;
                }
                repaint();
            }
        }, "debug info", KeyStroke.getKeyStroke("ctrl D"), WHEN_FOCUSED);
        
        // add quality toggle
        registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setHighQuality(!isHighQuality());
                repaint();
            }
        }, "toggle high-quality drawing", KeyStroke.getKeyStroke("ctrl H"),
                WHEN_FOCUSED);
        
        // add image output control, if this is not an applet
        try {
            registerKeyboardAction(new ExportDisplayAction(this),
             "export display", KeyStroke.getKeyStroke("ctrl E"), WHEN_FOCUSED);
        } catch (SecurityException se) {
        }
    }
    
    /**
     * Set the size of the Display.
     * @param width the width of the Display in pixels
     * @param height the height of the Display in pixels
     * @see java.awt.Component#setSize(int, int)
     */
    public void setSize(int width, int height) {
        m_offscreen = null;
        setPreferredSize(new Dimension(width, height));
        super.setSize(width, height);
    }
    
    /**
     * Set the size of the Display.
     * @param d the dimensions of the Display in pixels
     * @see java.awt.Component#setSize(java.awt.Dimension)
     */
    public void setSize(Dimension d) {
        m_offscreen = null;
        setPreferredSize(d);
        super.setSize(d);
    }

    /**
     * Invalidates this component. Overridden to ensure that an
     * internal damage report is generated.
     * @see java.awt.Component#invalidate()
     */
    public void invalidate() {
        damageReport();
        super.invalidate();
    }
    
    /**
     * @see java.awt.Component#setBounds(int, int, int, int)
     */
    public void setBounds(int x, int y, int w, int h) {
        m_offscreen = null;
        super.setBounds(x,y,w,h);
    }
    
    /**
     * Sets the font used by this Display. This determines the font used
     * by this Display's text editor and in any debugging text.
     * @param f the Font to use
     */
    public void setFont(Font f) {
        super.setFont(f);
        m_editor.setFont(f);
    }
    
    /**
     * Returns the running average frame rate for this Display.
     * @return the frame rate
     */
    public double getFrameRate() {
        return frameRate;
    }
    
    /**
     * Determines if the Display uses a higher quality rendering, using
     * anti-aliasing. This causes drawing to be much slower, however, and
     * so is disabled by default.
     * @param on true to enable anti-aliased rendering, false to disable it
     */
    public void setHighQuality(boolean on) {
        if ( m_highQuality != on )
            damageReport();
        m_highQuality = on;
    }
    
    /**
     * Indicates if the Display is using high quality (return value true) or
     * regular quality (return value false) rendering.
     * @return true if high quality rendering is enabled, false otherwise
     */
    public boolean isHighQuality() {
        return m_highQuality;
    }
    
    /**
     * Returns the Visualization backing this Display.
     * @return this Display's {@link Visualization}
     */
    public Visualization getVisualization() {
        return m_vis;
    }
    
    /**
     * Set the Visualiztion associated with this Display. This Display
     * will render the items contained in the provided visualization. If this
     * Display is already associated with a different Visualization, the
     * Display unregisters itself with the previous one.
     * @param vis the backing {@link Visualization} to use.
     */
    public void setVisualization(Visualization vis) {
        // TODO: synchronization?
        if ( m_vis == vis ) {
            // nothing need be done
            return;
        } else if ( m_vis != null ) {
            // remove this display from it's previous registry
            m_vis.removeDisplay(this);
        }
        m_vis = vis;
        if ( m_vis != null )
            m_vis.addDisplay(this);
    }
    
    /**
     * Returns the filtering Predicate used to control what items are drawn
     * by this display.
     * @return the filtering {@link prefuse.data.expression.Predicate}
     */
    public Predicate getPredicate() {
        if ( m_predicate.size() == 1 ) {
            return BooleanLiteral.TRUE;
        } else {
            return m_predicate.get(0);
        }
    }

    /**
     * Sets the filtering Predicate used to control what items are drawn by
     * this Display.
     * @param expr the filtering predicate to use. The predicate string will be
     * parsed by the {@link prefuse.data.expression.parser.ExpressionParser}.
     * If the parse fails or does not result in a
     * {@link prefuse.data.expression.Predicate} instance, an exception will
     * be thrown.
     */
    public void setPredicate(String expr) {
        Predicate p = (Predicate)ExpressionParser.parse(expr, true);
        setPredicate(p);
    }
    
    /**
     * Sets the filtering Predicate used to control what items are drawn by
     * this Display.
     * @param p the filtering {@link prefuse.data.expression.Predicate} to use
     */
    public synchronized void setPredicate(Predicate p) {
        if ( p == null ) {
            m_predicate.set(VisiblePredicate.TRUE);
        } else {
            m_predicate.set(new Predicate[] {p, VisiblePredicate.TRUE});
        }
    }
    
    /**
     * Returns the number of visible items processed by this Display. This
     * includes items not currently visible on screen due to the current
     * panning or zooming state.
     * @return the count of visible items
     */
    public int getVisibleItemCount() {
        return m_visibleCount;
    }
    
    /**
     * Get the ItemSorter that determines the rendering order of the
     * VisualItems. Items are drawn in ascending order of the scores provided
     * by the ItemSorter.
     * @return this Display's {@link prefuse.visual.sort.ItemSorter}
     */
    public ItemSorter getItemSorter() {
        return m_queue.sort;
    }

    /**
     * Set the ItemSorter that determines the rendering order of the
     * VisualItems. Items are drawn in ascending order of the scores provided
     * by the ItemSorter.
     * @return the {@link prefuse.visual.sort.ItemSorter} to use
     */
    public synchronized void setItemSorter(ItemSorter cmp) {
        damageReport();
        m_queue.sort = cmp;
    }
    

    /**
     * Set a background image for this display.
     * @param image the background Image. If a null value is provided, 
     * than no background image will be shown.
     * @param fixed true if the background image should stay in a fixed
     * position, invariant to panning, zooming, or rotation; false if
     * the image should be subject to view transforms
     * @param tileImage true to tile the image across the visible background,
     * false to only include the image once
     */
    public synchronized void setBackgroundImage(Image image,
                                      boolean fixed, boolean tileImage)
    {
        BackgroundPainter bg = null;
        if ( image != null )
            bg = new BackgroundPainter(image, fixed, tileImage);
        setBackgroundPainter(bg);
    }

    /**
     * Set a background image for this display.
     * @param location a location String of where to retrieve the
     * image file from. Uses
     * {@link prefuse.util.io.IOLib#urlFromString(String)} to resolve
     * the String. If a null value is provided, than no background
     * image will be shown.
     * @param fixed true if the background image should stay in a fixed
     * position, invariant to panning, zooming, or rotation; false if
     * the image should be subject to view transforms
     * @param tileImage true to tile the image across the visible background,
     * false to only include the image once
     */
    public synchronized void setBackgroundImage(String location,
                                      boolean fixed, boolean tileImage)
    {
        BackgroundPainter bg = null;
        if ( location != null )
            bg = new BackgroundPainter(location, fixed, tileImage);
        setBackgroundPainter(bg);        
    }
    
    private void setBackgroundPainter(BackgroundPainter bg) {
        if ( m_bgpainter != null )
            removePaintListener(m_bgpainter);
        m_bgpainter = bg;
        if ( bg != null )
            addPaintListener(bg);
    }
    
    // ------------------------------------------------------------------------
    // Clip / Bounds Management
    
    /**
     * Indicates if damage/redraw rendering is enabled. If enabled, the display
     * will only redraw within the bounding box of all areas that have changed
     * since the last rendering operation. For small changes, such as a single
     * item being dragged, this can result in a significant performance
     * increase. By default, the damage/redraw optimization is enabled. It can
     * be disabled, however, if rendering artifacts are appearing in your
     * visualization. Be careful though, as this may not be the best solution.
     * Rendering artifacts may result because the item bounds returned by
     * {@link prefuse.visual.VisualItem#getBounds()} are not accurate and the
     * item's {@link prefuse.render.Renderer} is drawing outside of the
     * reported bounds. In this case, there is usually a bug in the Renderer.
     * One reported problem arises from Java itself, however, which
     * inaccurately redraws images outside of their reported bounds. If you
     * have a visulization with a number of images and are seeing rendering
     * artifacts, try disabling damage/redraw.
     * @return true if damage/redraw optimizations are enabled, false
     * otherwise (in which case the entire Display is redrawn upon a repaint)
     */
    public synchronized boolean isDamageRedraw() {
        return m_damageRedraw;
    }
    
    /**
     * Sets if damage/redraw rendering is enabled. If enabled, the display
     * will only redraw within the bounding box of all areas that have changed
     * since the last rendering operation. For small changes, such as a single
     * item being dragged, this can result in a significant performance
     * increase. By default, the damage/redraw optimization is enabled. It can
     * be disabled, however, if rendering artifacts are appearing in your
     * visualization. Be careful though, as this may not be the best solution.
     * Rendering artifacts may result because the item bounds returned by
     * {@link prefuse.visual.VisualItem#getBounds()} are not accurate and the
     * item's {@link prefuse.render.Renderer} is drawing outside of the
     * reported bounds. In this case, there is usually a bug in the Renderer.
     * One reported problem arises from Java itself, however, which
     * inaccurately redraws images outside of their reported bounds. If you
     * have a visulization with a number of images and are seeing rendering
     * artifacts, try disabling damage/redraw.
     * @param b true to enable damage/redraw optimizations, false otherwise
     * (in which case the entire Display will be redrawn upon a repaint)
     */
    public synchronized void setDamageRedraw(boolean b) {
        m_damageRedraw = b;
        m_clip.invalidate();
    }
    
    /**
     * Reports damage to the Display within in the specified region.
     * @param region the damaged region, in absolute coordinates
     */
    public synchronized void damageReport(Rectangle2D region) {
        if ( m_damageRedraw )
            m_clip.union(region);
    }
    
    /**
     * Reports damage to the entire Display.
     */
    public synchronized void damageReport() {
        m_clip.invalidate();
    }
   
    /**
     * Clears any reports of damaged regions, causing the Display to believe
     * that the display contents are up-to-date. If used incorrectly this
     * can cause inaccurate rendering. <strong>Call this method only
     * if you know what you are doing.</strong>
     */
    public synchronized void clearDamage() {
        if ( m_damageRedraw )
            m_clip.reset();
    }
    
    /**
     * Returns the bounds, in absolute (item-space) coordinates, of the total
     * bounds occupied by all currently visible VisualItems. This method
     * allocates a new Rectangle2D instance for the result.
     * @return the bounding box of all visibile VisualItems
     * @see #getItemBounds(Rectangle2D)
     */
    public synchronized Rectangle2D getItemBounds() {
        return getItemBounds(new Rectangle2D.Double());
    }

    /**
     * Returns the bounds, in absolute (item-space) coordinates, of the total
     * bounds occupied by all currently visible VisualItems.
     * @param b the Rectangle2D to use to store the return value
     * @return the bounding box of all visibile VisualItems
     */
    public synchronized Rectangle2D getItemBounds(Rectangle2D b) {
        b.setFrameFromDiagonal(m_bounds.getMinX(), m_bounds.getMinY(),
                               m_bounds.getMaxX(), m_bounds.getMaxY());
        return b;
    }
    
    // ------------------------------------------------------------------------
    // Rendering
    
    /**
     * Returns the offscreen buffer used for double buffering.
     * @return the offscreen buffer
     */
    public BufferedImage getOffscreenBuffer() {
        return m_offscreen;
    }
    
    /**
     * Creates a new buffered image to use as an offscreen buffer.
     */
    protected BufferedImage getNewOffscreenBuffer() {
        return (BufferedImage)createImage(getWidth(), getHeight());
    }
    
    /**
     * Saves a copy of this display as an image to the specified output stream.
     * @param output the output stream to write to.
     * @param format the image format (e.g., "JPG", "PNG"). The number and kind
     * of available formats varies by platform. See
     * {@link javax.imageio.ImageIO} and related classes for more.
     * @param scale how much to scale the image by. For example, a value of 2.0
     * will result in an image with twice the pixel width and height of this
     * Display.
     * @return true if image was successfully saved, false if an error occurred.
     */
    public boolean saveImage(OutputStream output, String format, double scale)
    {
        try {
            Dimension d = new Dimension((int)(scale*getWidth()),
                                        (int)(scale*getHeight()));
            BufferedImage img = (BufferedImage)createImage(d.width, d.height);
            Graphics2D g = (Graphics2D)img.getGraphics();
            Point2D p = new Point2D.Double(0,0);
            zoom(p, scale);
            boolean q = isHighQuality();
            setHighQuality(true);
            paintDisplay(g,d);
            setHighQuality(q);
            zoom(p, 1/scale);
            ImageIO.write(img,format,output);
            return true;
        } catch ( Exception e ) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @see java.awt.Component#update(java.awt.Graphics)
     */
    public void update(Graphics g) {
        paint(g);
    }
    
    /**
     * Paints the offscreen buffer to the provided graphics context.
     * @param g the Graphics context to paint to
     */
    protected void paintBufferToScreen(Graphics g) {
        synchronized ( this ) {
            g.drawImage(m_offscreen, 0, 0, null);
        }
    }

    /**
     * Immediately repaints the contents of the offscreen buffer
     * to the screen. This bypasses the usual rendering loop.
     */
    public void repaintImmediate() {
        Graphics g = this.getGraphics();
        if (g != null && m_offscreen != null) {
            paintBufferToScreen(g);
        }
    }

    /**
     * Sets the transform of the provided Graphics context to be the
     * transform of this Display and sets the desired rendering hints.
     * @param g the Graphics context to prepare.
     */
    protected void prepareGraphics(Graphics2D g) {
        if ( m_transform != null )
            g.transform(m_transform);
        setRenderingHints(g);
    }
    
    /**
     * Sets the rendering hints that should be used while drawing
     * the visualization to the screen. Subclasses can override
     * this method to set hints as desired. Such subclasses should
     * consider honoring the high quality flag in one form or another.
     * @param g the Graphics context on which to set the rendering hints
     */
    protected void setRenderingHints(Graphics2D g) {
        if ( m_highQuality ) {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
        } else {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_OFF);
        }
        g.setRenderingHint(
            RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    }


    /**
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics g) {
        if (m_offscreen == null) {
            m_offscreen = getNewOffscreenBuffer();
            damageReport();
        }
        Graphics2D g2D = (Graphics2D)g;
        Graphics2D buf_g2D = (Graphics2D) m_offscreen.getGraphics();
        
        // Why not fire a pre-paint event here?
        // Pre-paint events are fired by the clearRegion method
        
        // paint the visualization
        paintDisplay(buf_g2D, getSize());
        paintBufferToScreen(g2D);       
        
        // fire post-paint events to any painters
        firePostPaint(g2D);
        
        buf_g2D.dispose();
        
        // compute frame rate
        nframes++;
        if ( mark < 0 ) {
            mark = System.currentTimeMillis();
            nframes = 0;
        } else if ( nframes == sampleInterval ){
            long t = System.currentTimeMillis();
            frameRate = (1000.0*nframes)/(t-mark);
            mark = t;
            nframes = 0;<commit_msg>Augmented offscreen buffer generation to work for headless setups<commit_after>package prefuse;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;

import prefuse.activity.Activity;
import prefuse.activity.SlowInSlowOutPacer;
import prefuse.controls.Control;
import prefuse.data.expression.AndPredicate;
import prefuse.data.expression.BooleanLiteral;
import prefuse.data.expression.Predicate;
import prefuse.data.expression.parser.ExpressionParser;
import prefuse.render.Renderer;
import prefuse.util.ColorLib;
import prefuse.util.StringLib;
import prefuse.util.UpdateListener;
import prefuse.util.collections.CopyOnWriteArrayList;
import prefuse.util.display.BackgroundPainter;
import prefuse.util.display.Clip;
import prefuse.util.display.DebugStatsPainter;
import prefuse.util.display.ExportDisplayAction;
import prefuse.util.display.ItemBoundsListener;
import prefuse.util.display.PaintListener;
import prefuse.util.display.RenderingQueue;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;
import prefuse.visual.sort.ItemSorter;


/**
 * <p>User interface component that provides an interactive view onto
 * a visualization. The Display is responsible for drawing items to the
 * screen and providing callbacks for user interface actions such as
 * mouse and keyboard events. A Display must be associated with an
 * {@link prefuse.Visualization} from which it pulls the items to visualize.
 * </p>
 * 
 * <p>To control which {@link prefuse.visual.VisualItem} instances are
 * drawn, the Display also maintains an optional 
 * {@link prefuse.data.expression.Predicate} for filtering items. The
 * drawing order of items is
 * controlled by an {@link prefuse.visual.sort.ItemSorter} instance,
 * which calculates a score for each item. Items with higher scores
 * are drawn later, and hence on top of lower scoring items.
 * </p>
 * 
 * <p>The {@link prefuse.controls.Control Control}
 * interface provides the user interface callbacks for supporting
 * interaction. The {@link prefuse.controls} package contains a number
 * of pre-built <code>Control</code> implementations for common
 * interactions.</p>
 * 
 * <p>The Display class also supports arbitrary graphics transforms through
 * the <code>java.awt.geom.AffineTransform</code> class. The 
 * {@link #setTransform(java.awt.geom.AffineTransform) setTransform} method
 * allows arbitrary transforms to be applied, while the 
 * {@link #pan(double,double) pan} and 
 * {@link #zoom(java.awt.geom.Point2D,double) zoom}
 * methods provide convenience methods that appropriately update the current
 * transform to achieve panning and zooming of the presentation space.</p>
 * 
 * <p>Additionally, each Display instance also supports use of a text editor
 * to facilitate direct editing of text. See the various
 * {@link #editText(prefuse.visual.VisualItem, String)} methods.</p>
 * 
 * @version 1.0
 * @author <a href="http://jheer.org"><NAME></a>
 * @see Visualization
 * @see prefuse.controls.Control
 * @see prefuse.controls
 */
public class Display extends JComponent {

    private static final Logger s_logger 
        = Logger.getLogger(Display.class.getName());
    
    // visual item source
    protected Visualization m_vis;
    protected AndPredicate  m_predicate = new AndPredicate();
    
    // listeners
    protected CopyOnWriteArrayList m_controls = new CopyOnWriteArrayList();
    protected CopyOnWriteArrayList m_painters;
    protected CopyOnWriteArrayList m_bounders;
    
    // display
    protected BufferedImage m_offscreen;
    protected Clip          m_clip = new Clip();
    protected Clip          m_screen = new Clip();
    protected Clip          m_bounds = new Clip();
    protected Rectangle2D   m_rclip = new Rectangle2D.Double();
    protected boolean       m_damageRedraw = true;
    protected boolean       m_highQuality = false;
    
    // optional background image
    protected BackgroundPainter m_bgpainter = null;
    
    // rendering queue
    protected RenderingQueue m_queue = new RenderingQueue();
    protected int            m_visibleCount = 0;
    
    // transform variables
    protected AffineTransform   m_transform  = new AffineTransform();
    protected AffineTransform   m_itransform = new AffineTransform();
    protected TransformActivity m_transact = new TransformActivity();
    protected Point2D m_tmpPoint = new Point2D.Double();
    
    // frame count and debugging output
    protected double frameRate;
    protected int nframes = 0;
    private int sampleInterval = 10;
    private long mark = -1L;
    
    // text editing variables
    private JTextComponent m_editor;
    private boolean        m_editing;
    private VisualItem     m_editItem;
    private String         m_editAttribute;
    
    /**
     * Creates a new Display instance. You will need to associate this
     * Display with a {@link Visualization} for it to display anything.
     */
    public Display() {
        this(null);
    }
    
    /**
     * Creates a new Display associated with the given Visualization.
     * By default, all {@link prefuse.visual.VisualItem} instances in the
     * {@link Visualization} will be drawn by the Display.
     * @param visualization the {@link Visualization} backing this Display
     */
    public Display(Visualization visualization) {
        this(visualization, (Predicate)null);
    }
    
    /**
     * Creates a new Display associated with the given Visualization that
     * draws all VisualItems in the visualization that pass the given
     * Predicate. The predicate string will be parsed by the
     * {@link prefuse.data.expression.parser.ExpressionParser} to get a
     * {@link prefuse.data.expression.Predicate} instance.
     * @param visualization the {@link Visualization} backing this Display
     * @param predicate a predicate expression in the prefuse expression
     * language. This expression will be parsed; if the parsing fails or does
     * not result in a Predicate instance, an exception will result.
     */
    public Display(Visualization visualization, String predicate) {
        this(visualization,
                (Predicate)ExpressionParser.parse(predicate, true));
    }
    
    /**
     * Creates a new Display associated with the given Visualization that
     * draws all VisualItems in the visualization that pass the given
     * Predicate.
     * @param visualization the {@link Visualization} backing this Display
     * @param predicate the filtering {@link prefuse.data.expression.Predicate}
     */
    public Display(Visualization visualization, Predicate predicate) {
        setDoubleBuffered(false);
        setBackground(Color.WHITE);
        
        // initialize text editor
        m_editing = false;
        m_editor = new JTextField();
        m_editor.setBorder(null);
        m_editor.setVisible(false);
        this.add(m_editor);
        
        // register input event capturer
        InputEventCapturer iec = new InputEventCapturer();
        addMouseListener(iec);
        addMouseMotionListener(iec);
        addMouseWheelListener(iec);
        addKeyListener(iec);
        
        registerDefaultCommands();
        
        // invalidate the display when the filter changes
        m_predicate.addExpressionListener(new UpdateListener() {
            protected void update(Object src) { damageReport(); }
        });
        
        setVisualization(visualization);
        setPredicate(predicate);
        setSize(400,400); // set a default size
    }
    
    /**
     * Registers default keystroke commands on the Display. The default
     * commands are
     * <ul><li><b>ctrl D</b> - Toggle debug info display</li>
     *     <li><b>ctrl H</b> - Toggle high quality rendering</li>
     *     <li><b>ctrl E</b> - Export display view to an image file</li></ul>
     * Subclasses can override this method to prevent these commands from
     * being set. Additional commands can be registered using the
     * <code>registerKeyboardAction</code> method.
     */
    protected void registerDefaultCommands() {
        // add debugging output control
        registerKeyboardAction(new ActionListener() {
            private PaintListener m_debug = null;

            public void actionPerformed(ActionEvent e) {
                if (m_debug == null) {
                    m_debug = new DebugStatsPainter();
                    addPaintListener(m_debug);
                } else {
                    removePaintListener(m_debug);
                    m_debug = null;
                }
                repaint();
            }
        }, "debug info", KeyStroke.getKeyStroke("ctrl D"), WHEN_FOCUSED);
        
        // add quality toggle
        registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setHighQuality(!isHighQuality());
                repaint();
            }
        }, "toggle high-quality drawing", KeyStroke.getKeyStroke("ctrl H"),
                WHEN_FOCUSED);
        
        // add image output control, if this is not an applet
        try {
            registerKeyboardAction(new ExportDisplayAction(this),
             "export display", KeyStroke.getKeyStroke("ctrl E"), WHEN_FOCUSED);
        } catch (SecurityException se) {
        }
    }
    
    /**
     * Set the size of the Display.
     * @param width the width of the Display in pixels
     * @param height the height of the Display in pixels
     * @see java.awt.Component#setSize(int, int)
     */
    public void setSize(int width, int height) {
        m_offscreen = null;
        setPreferredSize(new Dimension(width, height));
        super.setSize(width, height);
    }
    
    /**
     * Set the size of the Display.
     * @param d the dimensions of the Display in pixels
     * @see java.awt.Component#setSize(java.awt.Dimension)
     */
    public void setSize(Dimension d) {
        m_offscreen = null;
        setPreferredSize(d);
        super.setSize(d);
    }

    /**
     * Invalidates this component. Overridden to ensure that an
     * internal damage report is generated.
     * @see java.awt.Component#invalidate()
     */
    public void invalidate() {
        damageReport();
        super.invalidate();
    }
    
    /**
     * @see java.awt.Component#setBounds(int, int, int, int)
     */
    public void setBounds(int x, int y, int w, int h) {
        m_offscreen = null;
        super.setBounds(x,y,w,h);
    }
    
    /**
     * Sets the font used by this Display. This determines the font used
     * by this Display's text editor and in any debugging text.
     * @param f the Font to use
     */
    public void setFont(Font f) {
        super.setFont(f);
        m_editor.setFont(f);
    }
    
    /**
     * Returns the running average frame rate for this Display.
     * @return the frame rate
     */
    public double getFrameRate() {
        return frameRate;
    }
    
    /**
     * Determines if the Display uses a higher quality rendering, using
     * anti-aliasing. This causes drawing to be much slower, however, and
     * so is disabled by default.
     * @param on true to enable anti-aliased rendering, false to disable it
     */
    public void setHighQuality(boolean on) {
        if ( m_highQuality != on )
            damageReport();
        m_highQuality = on;
    }
    
    /**
     * Indicates if the Display is using high quality (return value true) or
     * regular quality (return value false) rendering.
     * @return true if high quality rendering is enabled, false otherwise
     */
    public boolean isHighQuality() {
        return m_highQuality;
    }
    
    /**
     * Returns the Visualization backing this Display.
     * @return this Display's {@link Visualization}
     */
    public Visualization getVisualization() {
        return m_vis;
    }
    
    /**
     * Set the Visualiztion associated with this Display. This Display
     * will render the items contained in the provided visualization. If this
     * Display is already associated with a different Visualization, the
     * Display unregisters itself with the previous one.
     * @param vis the backing {@link Visualization} to use.
     */
    public void setVisualization(Visualization vis) {
        // TODO: synchronization?
        if ( m_vis == vis ) {
            // nothing need be done
            return;
        } else if ( m_vis != null ) {
            // remove this display from it's previous registry
            m_vis.removeDisplay(this);
        }
        m_vis = vis;
        if ( m_vis != null )
            m_vis.addDisplay(this);
    }
    
    /**
     * Returns the filtering Predicate used to control what items are drawn
     * by this display.
     * @return the filtering {@link prefuse.data.expression.Predicate}
     */
    public Predicate getPredicate() {
        if ( m_predicate.size() == 1 ) {
            return BooleanLiteral.TRUE;
        } else {
            return m_predicate.get(0);
        }
    }

    /**
     * Sets the filtering Predicate used to control what items are drawn by
     * this Display.
     * @param expr the filtering predicate to use. The predicate string will be
     * parsed by the {@link prefuse.data.expression.parser.ExpressionParser}.
     * If the parse fails or does not result in a
     * {@link prefuse.data.expression.Predicate} instance, an exception will
     * be thrown.
     */
    public void setPredicate(String expr) {
        Predicate p = (Predicate)ExpressionParser.parse(expr, true);
        setPredicate(p);
    }
    
    /**
     * Sets the filtering Predicate used to control what items are drawn by
     * this Display.
     * @param p the filtering {@link prefuse.data.expression.Predicate} to use
     */
    public synchronized void setPredicate(Predicate p) {
        if ( p == null ) {
            m_predicate.set(VisiblePredicate.TRUE);
        } else {
            m_predicate.set(new Predicate[] {p, VisiblePredicate.TRUE});
        }
    }
    
    /**
     * Returns the number of visible items processed by this Display. This
     * includes items not currently visible on screen due to the current
     * panning or zooming state.
     * @return the count of visible items
     */
    public int getVisibleItemCount() {
        return m_visibleCount;
    }
    
    /**
     * Get the ItemSorter that determines the rendering order of the
     * VisualItems. Items are drawn in ascending order of the scores provided
     * by the ItemSorter.
     * @return this Display's {@link prefuse.visual.sort.ItemSorter}
     */
    public ItemSorter getItemSorter() {
        return m_queue.sort;
    }

    /**
     * Set the ItemSorter that determines the rendering order of the
     * VisualItems. Items are drawn in ascending order of the scores provided
     * by the ItemSorter.
     * @return the {@link prefuse.visual.sort.ItemSorter} to use
     */
    public synchronized void setItemSorter(ItemSorter cmp) {
        damageReport();
        m_queue.sort = cmp;
    }
    

    /**
     * Set a background image for this display.
     * @param image the background Image. If a null value is provided, 
     * than no background image will be shown.
     * @param fixed true if the background image should stay in a fixed
     * position, invariant to panning, zooming, or rotation; false if
     * the image should be subject to view transforms
     * @param tileImage true to tile the image across the visible background,
     * false to only include the image once
     */
    public synchronized void setBackgroundImage(Image image,
                                      boolean fixed, boolean tileImage)
    {
        BackgroundPainter bg = null;
        if ( image != null )
            bg = new BackgroundPainter(image, fixed, tileImage);
        setBackgroundPainter(bg);
    }

    /**
     * Set a background image for this display.
     * @param location a location String of where to retrieve the
     * image file from. Uses
     * {@link prefuse.util.io.IOLib#urlFromString(String)} to resolve
     * the String. If a null value is provided, than no background
     * image will be shown.
     * @param fixed true if the background image should stay in a fixed
     * position, invariant to panning, zooming, or rotation; false if
     * the image should be subject to view transforms
     * @param tileImage true to tile the image across the visible background,
     * false to only include the image once
     */
    public synchronized void setBackgroundImage(String location,
                                      boolean fixed, boolean tileImage)
    {
        BackgroundPainter bg = null;
        if ( location != null )
            bg = new BackgroundPainter(location, fixed, tileImage);
        setBackgroundPainter(bg);        
    }
    
    private void setBackgroundPainter(BackgroundPainter bg) {
        if ( m_bgpainter != null )
            removePaintListener(m_bgpainter);
        m_bgpainter = bg;
        if ( bg != null )
            addPaintListener(bg);
    }
    
    // ------------------------------------------------------------------------
    // Clip / Bounds Management
    
    /**
     * Indicates if damage/redraw rendering is enabled. If enabled, the display
     * will only redraw within the bounding box of all areas that have changed
     * since the last rendering operation. For small changes, such as a single
     * item being dragged, this can result in a significant performance
     * increase. By default, the damage/redraw optimization is enabled. It can
     * be disabled, however, if rendering artifacts are appearing in your
     * visualization. Be careful though, as this may not be the best solution.
     * Rendering artifacts may result because the item bounds returned by
     * {@link prefuse.visual.VisualItem#getBounds()} are not accurate and the
     * item's {@link prefuse.render.Renderer} is drawing outside of the
     * reported bounds. In this case, there is usually a bug in the Renderer.
     * One reported problem arises from Java itself, however, which
     * inaccurately redraws images outside of their reported bounds. If you
     * have a visulization with a number of images and are seeing rendering
     * artifacts, try disabling damage/redraw.
     * @return true if damage/redraw optimizations are enabled, false
     * otherwise (in which case the entire Display is redrawn upon a repaint)
     */
    public synchronized boolean isDamageRedraw() {
        return m_damageRedraw;
    }
    
    /**
     * Sets if damage/redraw rendering is enabled. If enabled, the display
     * will only redraw within the bounding box of all areas that have changed
     * since the last rendering operation. For small changes, such as a single
     * item being dragged, this can result in a significant performance
     * increase. By default, the damage/redraw optimization is enabled. It can
     * be disabled, however, if rendering artifacts are appearing in your
     * visualization. Be careful though, as this may not be the best solution.
     * Rendering artifacts may result because the item bounds returned by
     * {@link prefuse.visual.VisualItem#getBounds()} are not accurate and the
     * item's {@link prefuse.render.Renderer} is drawing outside of the
     * reported bounds. In this case, there is usually a bug in the Renderer.
     * One reported problem arises from Java itself, however, which
     * inaccurately redraws images outside of their reported bounds. If you
     * have a visulization with a number of images and are seeing rendering
     * artifacts, try disabling damage/redraw.
     * @param b true to enable damage/redraw optimizations, false otherwise
     * (in which case the entire Display will be redrawn upon a repaint)
     */
    public synchronized void setDamageRedraw(boolean b) {
        m_damageRedraw = b;
        m_clip.invalidate();
    }
    
    /**
     * Reports damage to the Display within in the specified region.
     * @param region the damaged region, in absolute coordinates
     */
    public synchronized void damageReport(Rectangle2D region) {
        if ( m_damageRedraw )
            m_clip.union(region);
    }
    
    /**
     * Reports damage to the entire Display.
     */
    public synchronized void damageReport() {
        m_clip.invalidate();
    }
   
    /**
     * Clears any reports of damaged regions, causing the Display to believe
     * that the display contents are up-to-date. If used incorrectly this
     * can cause inaccurate rendering. <strong>Call this method only
     * if you know what you are doing.</strong>
     */
    public synchronized void clearDamage() {
        if ( m_damageRedraw )
            m_clip.reset();
    }
    
    /**
     * Returns the bounds, in absolute (item-space) coordinates, of the total
     * bounds occupied by all currently visible VisualItems. This method
     * allocates a new Rectangle2D instance for the result.
     * @return the bounding box of all visibile VisualItems
     * @see #getItemBounds(Rectangle2D)
     */
    public synchronized Rectangle2D getItemBounds() {
        return getItemBounds(new Rectangle2D.Double());
    }

    /**
     * Returns the bounds, in absolute (item-space) coordinates, of the total
     * bounds occupied by all currently visible VisualItems.
     * @param b the Rectangle2D to use to store the return value
     * @return the bounding box of all visibile VisualItems
     */
    public synchronized Rectangle2D getItemBounds(Rectangle2D b) {
        b.setFrameFromDiagonal(m_bounds.getMinX(), m_bounds.getMinY(),
                               m_bounds.getMaxX(), m_bounds.getMaxY());
        return b;
    }
    
    // ------------------------------------------------------------------------
    // Rendering
    
    /**
     * Returns the offscreen buffer used for double buffering.
     * @return the offscreen buffer
     */
    public BufferedImage getOffscreenBuffer() {
        return m_offscreen;
    }
    
    /**
     * Creates a new buffered image to use as an offscreen buffer.
     */
    protected BufferedImage getNewOffscreenBuffer(int width, int height) {
        try {
            return (BufferedImage)createImage(width, height);
        } catch ( Exception e ) {
            return new BufferedImage(width, height,
                                     BufferedImage.TYPE_INT_RGB);
        }
    }
    
    /**
     * Saves a copy of this display as an image to the specified output stream.
     * @param output the output stream to write to.
     * @param format the image format (e.g., "JPG", "PNG"). The number and kind
     * of available formats varies by platform. See
     * {@link javax.imageio.ImageIO} and related classes for more.
     * @param scale how much to scale the image by. For example, a value of 2.0
     * will result in an image with twice the pixel width and height of this
     * Display.
     * @return true if image was successfully saved, false if an error occurred.
     */
    public boolean saveImage(OutputStream output, String format, double scale)
    {
        try {
            // get an image to draw into
            Dimension d = new Dimension((int)(scale*getWidth()),
                                        (int)(scale*getHeight()));
            BufferedImage img = getNewOffscreenBuffer(d.width, d.height);
            Graphics2D g = (Graphics2D)img.getGraphics();
            
            // set up the display, render, then revert to normal settings
            Point2D p = new Point2D.Double(0,0);
            zoom(p, scale); // also takes care of damage report
            boolean q = isHighQuality();
            setHighQuality(true);
            paintDisplay(g, d);
            setHighQuality(q);
            zoom(p, 1/scale); // also takes care of damage report
            
            // save the image and return
            ImageIO.write(img, format, output);
            return true;
        } catch ( Exception e ) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @see java.awt.Component#update(java.awt.Graphics)
     */
    public void update(Graphics g) {
        paint(g);
    }
    
    /**
     * Paints the offscreen buffer to the provided graphics context.
     * @param g the Graphics context to paint to
     */
    protected void paintBufferToScreen(Graphics g) {
        synchronized ( this ) {
            g.drawImage(m_offscreen, 0, 0, null);
        }
    }

    /**
     * Immediately repaints the contents of the offscreen buffer
     * to the screen. This bypasses the usual rendering loop.
     */
    public void repaintImmediate() {
        Graphics g = this.getGraphics();
        if (g != null && m_offscreen != null) {
            paintBufferToScreen(g);
        }
    }

    /**
     * Sets the transform of the provided Graphics context to be the
     * transform of this Display and sets the desired rendering hints.
     * @param g the Graphics context to prepare.
     */
    protected void prepareGraphics(Graphics2D g) {
        if ( m_transform != null )
            g.transform(m_transform);
        setRenderingHints(g);
    }
    
    /**
     * Sets the rendering hints that should be used while drawing
     * the visualization to the screen. Subclasses can override
     * this method to set hints as desired. Such subclasses should
     * consider honoring the high quality flag in one form or another.
     * @param g the Graphics context on which to set the rendering hints
     */
    protected void setRenderingHints(Graphics2D g) {
        if ( m_highQuality ) {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
        } else {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_OFF);
        }
        g.setRenderingHint(
            RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    }


    /**
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics g) {
        if (m_offscreen == null) {
            m_offscreen = getNewOffscreenBuffer(getWidth(), getHeight());
            damageReport();
        }
        Graphics2D g2D = (Graphics2D)g;
        Graphics2D buf_g2D = (Graphics2D) m_offscreen.getGraphics();
        
        // Why not fire a pre-paint event here?
        // Pre-paint events are fired by the clearRegion method
        
        // paint the visualization
        paintDisplay(buf_g2D, getSize());
        paintBufferToScreen(g2D);       
        
        // fire post-paint events to any painters
        firePostPaint(g2D);
        
        buf_g2D.dispose();
        
        // compute frame rate
        nframes++;
        if ( mark < 0 ) {
            mark = System.currentTimeMillis();
            nframes = 0;
        } else if ( nframes == sampleInterval ){
            long t = System.currentTimeMillis();
            frameRate = (1000.0*nframes)/(t-mark);
            mark = t;
            nframes = 0;
{'fraction_non_alphanumeric': 0.08394603469198372, 'fraction_numerical': 0.00221286315939753, 'mean_word_length': 2.4229430089792925, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1326448', 'n_tokens_mistral': 14987, 'n_tokens_neox': 14696, 'n_words': 5136}