Let EmulGL compute the font width to compute text align

This commit is contained in:
Martin Pernollet 2021-04-05 22:50:20 +02:00
parent 9329122271
commit 0db566b8e6
11 changed files with 81 additions and 25 deletions

View File

@ -2,6 +2,7 @@ package org.jzy3d.painters;
import org.jzy3d.colors.Color;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.rendering.canvas.ICanvas;
import org.jzy3d.plot3d.rendering.view.Camera;
import org.jzy3d.plot3d.rendering.view.View;
import org.jzy3d.plot3d.transform.Transform;
@ -11,6 +12,7 @@ public abstract class AbstractPainter implements IPainter {
protected Camera camera;
protected View view;
protected ICanvas canvas;
public AbstractPainter() {
super();
@ -26,6 +28,16 @@ public abstract class AbstractPainter implements IPainter {
this.view = view;
}
@Override
public ICanvas getCanvas() {
return canvas;
}
@Override
public void setCanvas(ICanvas canvas) {
this.canvas = canvas;
}
@Override
public Camera getCamera() {
return camera;

View File

@ -3,7 +3,6 @@ package org.jzy3d.painters;
import java.nio.Buffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.jzy3d.colors.Color;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.primitives.Drawable;
@ -176,7 +175,17 @@ public interface IPainter {
protected String name;
protected int code;
protected int height;
public static Font getById(int id) {
switch(id) {
case BITMAP_HELVETICA_10: return Helvetica_10;
case BITMAP_HELVETICA_12: return Helvetica_12;
case BITMAP_HELVETICA_18: return Helvetica_18;
case BITMAP_TIMES_ROMAN_10: return TimesRoman_10;
case BITMAP_TIMES_ROMAN_24: return TimesRoman_24;
default: return null;
}
}
}
// Font constants below are picked from GLU object in JOGL
@ -204,6 +213,10 @@ public interface IPainter {
public void setView(View view);
public ICanvas getCanvas();
public void setCanvas(ICanvas canvas);
/** Apply quality settings as OpenGL commands */
public void configureGL(Quality quality);

View File

@ -673,8 +673,6 @@ public class AxisBox implements IAxis {
/**
* Computes the visibility of each cube face.
*
* @param painter TODO
*/
protected boolean[] getHiddenQuads(IPainter painter) {
Coord3d scaledEye = painter.getCamera().getEye().div(scale);

View File

@ -56,9 +56,6 @@ public class TextBitmapRenderer extends AbstractTextRenderer implements ITextRen
// compute a corrected 3D position according to the 2D layout on screen
float textHeight = font.getHeight();
java.awt.Font f = new java.awt.Font(font.getName(), java.awt.Font.PLAIN, font.getHeight());
//float textWidth = (float)f.getStringBounds(text, null).getWidth();
float textWidth = painter.glutBitmapLength(font.getCode(), text);
Coord3d screen = painter.getCamera().modelToScreen(painter, position);

View File

@ -1,5 +1,7 @@
package org.jzy3d.painters;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
@ -7,20 +9,18 @@ import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import org.jzy3d.colors.Color;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.pipelines.NotImplementedException;
import org.jzy3d.plot3d.primitives.PolygonFill;
import org.jzy3d.plot3d.primitives.PolygonMode;
import org.jzy3d.plot3d.rendering.canvas.EmulGLCanvas;
import org.jzy3d.plot3d.rendering.canvas.ICanvas;
import org.jzy3d.plot3d.rendering.canvas.Quality;
import org.jzy3d.plot3d.rendering.image.GLImage;
import org.jzy3d.plot3d.rendering.lights.LightModel;
import org.jzy3d.plot3d.rendering.lights.MaterialProperty;
import jgl.GL;
import jgl.GLU;
import jgl.GLUT;
@ -600,15 +600,34 @@ public class EmulGLPainter extends AbstractPainter implements IPainter {
}
/**
* Not implemented yet.
* Process the given font length to further process alignement.
*
* @see {@link #glutBitmapString(Font, String, Coord3d, Color)} as a convenient replacement.
* As this method is not implemented in jGL, we rely on AWT to process string width
* as soon as the canvas graphics is non null.
*
* In case the canvas is not able to return a {@link Graphics2D} instance we fallback
* on a simple formulae that is valid for the default font.
*
* <code>6 * string.length()</code>
*/
@Override
public int glutBitmapLength(int font, String string) {
return 6 * string.length();
// throw new NotImplementedException();
// return glut.glutBitmapLength(font, string);
EmulGLCanvas c = (EmulGLCanvas)getCanvas();
Graphics2D g = (Graphics2D)c.getGraphics();
if(g!=null) {
Font fnt = Font.getById(font);
if(fnt!=null) {
java.awt.Font f = new java.awt.Font(fnt.getName(), java.awt.Font.PLAIN, fnt.getHeight());
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
if(fm!=null) {
return fm.stringWidth(string);
}
}
}
return 6 * string.length();
}
/**

View File

@ -50,6 +50,7 @@ public class EmulGLCanvas extends GLCanvas implements IScreenCanvas, IMonitorabl
super();
view = scene.newView(this, quality);
painter = (EmulGLPainter) view.getPainter();
painter.setCanvas(this);
init();
animator = factory.getPainterFactory().newAnimator(this);
@ -173,10 +174,8 @@ public class EmulGLCanvas extends GLCanvas implements IScreenCanvas, IMonitorabl
postRenderString("Frame Size : " + getWidth() + "x" + getHeight(), x, y * 4, Color.BLACK);
}
Font profileFont = new Font("Arial", Font.PLAIN, 12);
int kDisplay = 0;
protected Font profileFont = new Font("Arial", Font.PLAIN, 12);
protected int kDisplay = 0;
/** Draw a 2d text at the given position */
@ -200,10 +199,8 @@ public class EmulGLCanvas extends GLCanvas implements IScreenCanvas, IMonitorabl
public synchronized void doMotion(int x, int y) {
doDisplay();
// repaint();
// paint
System.out.println("EmulGLCanvas.doMotion!" + kDisplay);
}

View File

@ -1,11 +1,15 @@
package org.jzy3d.chart;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.jzy3d.chart.controllers.mouse.camera.ICameraMouseController;
import org.jzy3d.chart.factories.ChartFactory;
import org.jzy3d.chart.factories.EmulGLChartFactory;
import org.jzy3d.painters.IPainter.Font;
import org.jzy3d.plot3d.primitives.axis.AxisBox;
import org.jzy3d.plot3d.rendering.canvas.Quality;
import org.jzy3d.plot3d.text.renderers.TextBitmapRenderer;
public class TestChart {
/**
@ -48,5 +52,16 @@ public class TestChart {
Assert.assertTrue(mouse.isUpdateViewDefault());
}
@Ignore
@Test
public void whenChartAxisLayoutHasCustomFont_ThenAxisBoxHasThisFont() {
Chart chart = new EmulGLChartFactory().newChart();
chart.getAxisLayout().setFont(Font.TimesRoman_24);
AxisBox axis = (AxisBox)chart.getView().getAxis();
TextBitmapRenderer tbr = (TextBitmapRenderer)axis.getTextRenderer();
Assert.assertEquals(Font.TimesRoman_24, tbr.getFont());
}
}

View File

@ -50,6 +50,8 @@ public class CanvasAWT extends GLCanvas implements IScreenCanvas, INativeCanvas
super(glci);
view = scene.newView(this, quality);
view.getPainter().setCanvas(this);
renderer =
((NativePainterFactory) factory.getPainterFactory()).newRenderer3D(view, traceGL, debugGL);
addGLEventListener(renderer);

View File

@ -47,6 +47,8 @@ public class CanvasNewtAwt extends Panel implements IScreenCanvas, INativeCanvas
window = GLWindow.create(glci);
canvas = new NewtCanvasAWT(window);
view = scene.newView(this, quality);
view.getPainter().setCanvas(this);
renderer =
((NativePainterFactory) factory.getPainterFactory()).newRenderer3D(view, traceGL, debugGL);
window.addGLEventListener(renderer);

View File

@ -53,6 +53,8 @@ public class CanvasSwing extends GLJPanel implements IScreenCanvas, INativeCanva
super(glci);
view = scene.newView(this, quality);
view.getPainter().setCanvas(this);
renderer =
((NativePainterFactory) factory.getPainterFactory()).newRenderer3D(view, traceGL, debugGL);
addGLEventListener(renderer);

View File

@ -45,6 +45,8 @@ public class CanvasNewtSWT extends Composite implements IScreenCanvas, INativeCa
window = GLWindow.create(glci);
canvas = new NewtCanvasSWT(this, SWT.NONE, window);
view = scene.newView(this, quality);
view.getPainter().setCanvas(this);
renderer =
((NativePainterFactory) factory.getPainterFactory()).newRenderer3D(view, traceGL, debugGL);
window.addGLEventListener(renderer);
@ -56,11 +58,8 @@ public class CanvasNewtSWT extends Composite implements IScreenCanvas, INativeCa
window.setAutoSwapBufferMode(quality.isAutoSwapBuffer());
animator = ((SWTChartFactory) factory).newAnimator(window);
if (quality.isAnimated()) {
animator.start();
}