summaryrefslogtreecommitdiffstats
path: root/src/demos/j2d/TextFlow.java
blob: 39283b36066a3f4f0fc215ab17780d21719f124b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 *
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package demos.j2d;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.geom.Rectangle2D;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.awt.TextRenderer;

import demos.common.Demo;
import demos.util.SystemTime;
import demos.util.Time;


/** Illustrates both the TextRenderer's capability for handling
    relatively large amounts of text (more than drawn on the screen --
    showing the least recently used capabilities of its internal
    cache) as well as using the Java 2D text layout mechanisms in
    conjunction with the TextRenderer to flow text across the
    screen. */

public class TextFlow extends Demo {
  public static void main(String[] args) {
    Frame frame = new Frame("Text Flow");
    frame.setLayout(new BorderLayout());

    GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
    GLCanvas canvas = new GLCanvas(caps);
    final TextFlow demo = new TextFlow();

    canvas.addGLEventListener(demo);
    frame.add(canvas, BorderLayout.CENTER);

    frame.setSize(512, 512);
    final Animator animator = new Animator(canvas);
    frame.addWindowListener(new WindowAdapter() {
        @Override
		public void windowClosing(WindowEvent e) {
          // Run this on another thread than the AWT event queue to
          // make sure the call to Animator.stop() completes before
          // exiting
          new Thread(new Runnable() {
              @Override
			public void run() {
                animator.stop();
                System.exit(0);
              }
            }).start();
        }
      });
    frame.setVisible(true);
    animator.start();
  }

  private final List<String> lines = new ArrayList<String>();
  private Time time;
  private TextRenderer renderer;
  private int curParagraph;
  private final float x = 30;
  private float y;
  private final float velocity = 100;  // pixels/sec
  private int lineSpacing;
  private final int EXTRA_LINE_SPACING = 5;

  private void reflow(float width) {
    lines.clear();
    lineSpacing = 0;
    int numLines = 0;
    FontRenderContext frc = renderer.getFontRenderContext();
    for (int i = 0; i < text.length; i++) {
      String paragraph = text[i];
      Map<TextAttribute, Font> attrs = new HashMap<TextAttribute, Font>();
      attrs.put(TextAttribute.FONT, renderer.getFont());
      AttributedString str = new AttributedString(paragraph, attrs);
      LineBreakMeasurer measurer = new LineBreakMeasurer(str.getIterator(), frc);
      int curPos = 0;
      while (measurer.getPosition() < paragraph.length()) {
        int nextPos = measurer.nextOffset(width);
        String line = paragraph.substring(curPos, nextPos);
        Rectangle2D bounds = renderer.getBounds(line);
        lines.add(line);
        lineSpacing += (int) bounds.getHeight();
        ++numLines;
        curPos = nextPos;
        measurer.setPosition(curPos);
      }
      // Indicate end of paragraph with a null LineInfo
      lines.add(null);
    }
    lineSpacing = (int) ((float) lineSpacing / (float) numLines) + EXTRA_LINE_SPACING;
  }

  @Override
public void init(GLAutoDrawable drawable) {
    renderer = new TextRenderer(new Font("SansSerif", Font.PLAIN, 36), true, false);
    time = new SystemTime();
    ((SystemTime) time).rebase();
  }

  @Override
public void dispose(GLAutoDrawable drawable) {
    renderer = null;
    time = null;
  }

  @Override
public void display(GLAutoDrawable drawable) {
    time.update();

    GL gl = drawable.getGL();
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    float deltaT = (float) time.deltaT();
    y += velocity * deltaT;

    // Draw text starting at the specified paragraph
    int paragraph = 0;
    float curY = y;
    renderer.beginRendering(drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
    boolean renderedOne = false;
    for (int i = 0; i < lines.size(); i++) {
      String line = lines.get(i);
      if (line == null) {
        ++paragraph;
        if (paragraph >= curParagraph) {
          // If this paragraph has scrolled off the top of the screen,
          // don't draw it the next frame
          if (paragraph > curParagraph && curY > drawable.getSurfaceHeight()) {
            ++curParagraph;
            y = curY;
          }
          curY -= 2 * lineSpacing;
        }
      } else {
        if (paragraph >= curParagraph) {
          curY -= lineSpacing;
          if (curY < drawable.getSurfaceHeight() + lineSpacing) {
            renderer.draw(line, (int) x, (int) curY);
            renderedOne = true;
          }
          if (curY < 0) {
            // Done rendering all visible lines
            break;
          }
        }
      }
    }
    renderer.endRendering();
    if (!renderedOne) {
      // Start over
      curParagraph = 0;
      y = 0;
    }
  }

  @Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    reflow(Math.max(100, width - 60));
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

  // Use some nonsense Lorem Ipsum text generated from www.lipsum.com
  private static final String[] text = {
    "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla in mi ut augue laoreet gravida. Quisque sodales vehicula ligula. Donec posuere. Morbi aliquet, odio vitae tempus mattis, odio dolor vestibulum leo, congue laoreet risus felis vitae dolor. Nulla arcu. Morbi non quam. Vestibulum pretium dolor fermentum erat. Proin dictum volutpat nibh. Morbi egestas mauris a diam. Vestibulum mauris eros, porttitor at, fermentum a, varius eu, mauris. Cras rutrum felis ut diam. Aenean porttitor risus a nunc. Aliquam et ante eu dolor pretium adipiscing. Sed fermentum, eros in dapibus lacinia, augue nunc fermentum tellus, eu egestas justo elit at mauris. Sed leo nisl, fermentum in, pretium vitae, tincidunt at, lacus. Curabitur non diam.",
    "Etiam varius sagittis lorem. Vivamus iaculis condimentum tortor. Nunc sollicitudin scelerisque dolor. Nunc condimentum fringilla nisl. Fusce purus mauris, blandit eu, lacinia eget, vestibulum nec, massa. Nulla vitae libero. Suspendisse potenti. Aliquam iaculis, lorem eu adipiscing tempor, ipsum dui aliquam sem, eu vehicula leo leo eu ipsum. Pellentesque faucibus. Nullam porttitor ligula eget nibh. Cras elementum mi ac libero. Praesent pellentesque pede vitae quam. Sed nec arcu id ante cursus mollis. Suspendisse quis ipsum. Maecenas feugiat interdum neque. Nullam dui diam, convallis at, condimentum vitae, mattis vitae, metus. Integer sollicitudin, diam id lacinia posuere, quam velit fringilla dolor, eu semper sapien felis ac elit.",
    "Ut a magna vitae lectus euismod hendrerit. Quisque varius consectetuer sapien. Suspendisse ligula. Nullam feugiat venenatis mauris. In consequat lorem at neque. Pellentesque libero. In eget lectus in velit auctor facilisis. Donec nec metus. Aliquam facilisis eros vel dui. Integer a diam. Donec interdum, eros faucibus blandit venenatis, ante ante ornare enim, a gravida ante lectus id metus. Ut sem.",
    "Duis consectetuer leo quis elit. Suspendisse pretium nunc ac dolor. Quisque eleifend fringilla nisl. Suspendisse potenti. Duis vel ipsum at enim tincidunt consectetuer. Aliquam tempor justo nec metus. Nunc ac velit id nibh consequat vulputate. Cras vel dolor eu massa lacinia volutpat. Curabitur nibh nisi, auctor et, tincidunt eget, molestie vel, neque. Sed semper viverra neque. Nullam rhoncus hendrerit libero. Nulla adipiscing. Fusce pede nibh, lacinia a, malesuada a, dictum nec, pede. Etiam ut lorem. Donec quis massa vitae est pharetra mattis.",
    "Nullam dui. Morbi nulla quam, imperdiet iaculis, consectetuer a, porttitor eu, sem. Donec id ipsum vitae nisi viverra porta. In hac habitasse platea dictumst. In ligula libero, dapibus eleifend, eleifend vel, accumsan sit amet, felis. Morbi tortor. Donec mattis ultricies arcu. Ut eget leo. Sed vel quam at ipsum sodales semper. Curabitur tincidunt quam id odio. Quisque porta, magna vel nonummy pulvinar, ligula tellus fringilla tellus, ut pharetra turpis velit ac eros. Cras eu enim vel mi suscipit malesuada. Phasellus ut orci. Aenean vitae turpis vitae lectus malesuada aliquet."
  };
}