1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package wrap.glwrap; 26 27 import java.io.IOException; 28 import java.lang.foreign.Arena; 29 import java.lang.foreign.MemorySegment; 30 31 import static opengl.opengl_h.*; 32 33 public class Fonts { 34 static class FontsWindow extends GLWindow { 35 public FontsWindow( Arena arena, int width, int height, String name, int mode, GLTexture... textures) { 36 super( arena, width, height, name, mode, textures); 37 } 38 39 @Override 40 public void reshape(int w, int h) { 41 width = w; 42 height = h; 43 System.out.println("reshaped " + w + ", " + h); 44 double size; 45 double aspect; 46 glViewport(0, 0, w, h); 47 glMatrixMode(GL_PROJECTION()); 48 glLoadIdentity(); 49 size = (double) ((w >= h) ? w : h) / 2.0; 50 if (w <= h) { 51 aspect = (double) h / (double) w; 52 glOrtho(-size, size, -size * aspect, size * aspect, -100000.0, 100000.0); 53 } else { 54 aspect = (double) w / (double) h; 55 glOrtho(-size * aspect, size * aspect, -size, size, -100000.0, 100000.0); 56 } 57 glScaled(aspect, aspect, 1.0); 58 glMatrixMode(GL_MODELVIEW()); 59 } 60 61 @Override 62 public void display() { 63 glClearColor(0f, 0f, 0f, 0f); 64 glPushMatrix1(() -> { 65 float x = -225.0f; 66 float y = 70.0f; 67 float ystep = 10.0f; 68 boolean stroke = true; 69 if (stroke) { 70 float stroke_scale = 0.2f; 71 glTranslatef(x, y, 0.0f); 72 MemorySegment font = glutStrokeRoman$segment(); 73 for (int j = 0; j < 4; j++) { 74 glPushMatrix1(() -> { 75 glScalef(stroke_scale, stroke_scale, stroke_scale); 76 for (int c : "This text stroked".getBytes()) { 77 glutStrokeCharacter(font, c); 78 } 79 }); 80 glTranslatef(0f, -ystep * 10 + j, 0.0f); 81 } 82 } else { 83 glColor3f(0.0f, 1.0f, 0.0f); 84 var font = glutBitmapTimesRoman24$segment(); 85 for (int j = 0; j < 4; j++) { 86 glRasterPos2f(10f, 10f + ystep * j * 10); 87 for (int c : "This text ".getBytes()) { 88 glutBitmapCharacter(font, c); 89 } 90 } 91 } 92 }); 93 glutSwapBuffers(); 94 } 95 96 97 @Override 98 public void onIdle() { 99 super.onIdle(); 100 } 101 } 102 public static void main(String[] args) throws IOException { 103 104 try (var arena = Arena.ofConfined()) { 105 // var particleTexture = new GLTexture(arena, Fonts.class.getResourceAsStream("/particle.png")); 106 107 new FontsWindow( arena, 1000, 1000, "Fonts", GLUT_RGB() | GLUT_DOUBLE() /*particleTexture particleTexture */).bindEvents().mainLoop(); 108 } 109 } 110 } 111