Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/SwissAS/jcgm-core
Browse files Browse the repository at this point in the history
  • Loading branch information
philippecade committed Dec 17, 2021
2 parents 2b98e03 + 8b54a7f commit d50f702
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 119 deletions.
15 changes: 11 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<project name="jcgm" default="jar">

<!-- The core package -->
<property name="version-core" value="0.4.0"/>
<property name="package-name-core" value="jcgm-core-${version-core}"/>
<property name="jar-name-core" value="${package-name-core}.jar"/>
<property name="package-name-core-bin" value="${package-name-core}-bin"/>
<property name="version-core" value="0.4.0"/>
<property name="package-name-core" value="jcgm-core-${version-core}"/>
<property name="jar-name-core" value="${package-name-core}.jar"/>
<property name="jar-name-core-sources" value="${package-name-core}-sources.jar"/>
<property name="package-name-core-bin" value="${package-name-core}-bin"/>

<!-- Source code only distributed in one package -->
<property name="package-name-src" value="jcgm-src"/>
Expand All @@ -28,6 +29,12 @@
includes="net/sf/jcgm/core/**"
/>
</target>

<target name="jar-sources" depends="compile">
<jar destfile="build/${jar-name-core-sources}">
<fileset dir="src" includes="**/*.java"/>
</jar>
</target>

<target name="compile" description="Compiles all java source files">
<mkdir dir="build/classes"/>
Expand Down
60 changes: 60 additions & 0 deletions src/net/sf/jcgm/core/AppendText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.sf.jcgm.core;

import java.io.DataInput;
import java.io.IOException;

/**
* Class=4, Element=6
* @author diaa (daviddiana11)
* @version $Id$
* @since Dec 16, 2021
*/
public class AppendText extends Command {

private final String string;
private final boolean finalNotFinal;

/**
* {@code true} if the cmd has already been executed i.e. the text appended to the previous {@link TextCommand};<br>
* {@code false} otherwise
*/
private boolean executed = false;

public AppendText(int ec, int eid, int l, DataInput in) throws IOException {
super(ec, eid, l, in);

this.finalNotFinal = makeEnum() >= 1;
this.string = makeString();
}

@Override
public void paint(CGMDisplay d) {
if (this.executed) {
// just ignore this cmd if it has already been executed - useful if commands are cached
return;
}

this.executed = true;

String decodedString = d.useSymbolEncoding() ? SymbolDecoder.decode(this.string) : this.string;

d.appendText(decodedString);
if (!this.finalNotFinal) {
// if there is still text to append
return;
}

TextCommand textCommand = d.getTextCommand();
if (textCommand == null) {
throw new IllegalArgumentException("cannot append and paint a string if there is no previous TextCommand");
}

textCommand.setStringComplete(true);
textCommand.paint(d);
}

@Override
public String toString() {
return "AppendText" + " String [" + this.string + "]";
}
}
27 changes: 27 additions & 0 deletions src/net/sf/jcgm/core/CGMDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public class CGMDisplay {
*/
private boolean isViewCleared= false;

/** Cached {@link TextCommand} that if set means that some text is still being appended to the string. */
private TextCommand textCommand;

public CGMDisplay(CGM cgm) {
reset();
this.lineDashes = new HashMap<Integer, float[]>();
Expand Down Expand Up @@ -1003,6 +1006,30 @@ public void setViewCleared(boolean b) {
this.isViewCleared = b;
}

/**
* Appends the given text string to the string builder.
*
* @param text the text to append
*/
public void appendText(String text) {
TextCommand textCommand = getTextCommand();
if (textCommand == null) {
throw new IllegalArgumentException("cannot append text if there is no previous TextCommand");
}
textCommand.appendText(text);
}

public void setTextCommand(TextCommand textCommand) {
this.textCommand = textCommand;
}

public TextCommand getTextCommand() {
return this.textCommand;
}

public void resetTextCommand() {
this.textCommand = null;
}
}

/*
Expand Down
3 changes: 1 addition & 2 deletions src/net/sf/jcgm/core/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,7 @@ private static Command readGraphicalPrimitiveElements(DataInput in, int ec, int
return new RestrictedText(ec, eid, l, in);

case APPEND_TEXT: // 6
unsupported(ec, eid);
return new Command(ec, eid, l, in);
return new AppendText(ec, eid, l, in);

case POLYGON: // 7
return new PolygonElement(ec, eid, l, in);
Expand Down
94 changes: 12 additions & 82 deletions src/net/sf/jcgm/core/RestrictedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.DataInput;
import java.io.IOException;

Expand All @@ -53,14 +48,18 @@ public RestrictedText(int ec, int eid, int l, DataInput in)
throws IOException {
super(ec, eid, l, in);

// (2VDC)
this.deltaWidth = makeVdc();
this.deltaHeight = makeVdc();

// point (P)
this.position = makePoint();

/* int finalNotFinal = */ makeEnum();

// flag (E)
this.finalFlag = makeEnum() >= 1;
setStringComplete(this.finalFlag);
// string (S)
this.string = makeString();

this.type = RestrictedTextType.getType();

// make sure all the arguments were read
Expand Down Expand Up @@ -287,85 +286,16 @@ else if (this.type.equals(RestrictedTextType.Type.JUSTIFIED)) {
}

@Override
public void paint(CGMDisplay d) {
if (this.string.length() == 0) {
// ignore empty strings
return;
}

Graphics2D g2d = d.getGraphics2D();

// save the transformation since we are going to apply another one that
// is specific to this string
AffineTransform savedTransform = g2d.getTransform();

AffineTransform coordinateSystemTransformation = d.getCoordinateSystemTransformation(
this.position,
d.getCharacterOrientationBaselineVector(), d.getCharacterOrientationUpVector());

AffineTransform textTransform = d.getTextTransform();
coordinateSystemTransformation.concatenate(textTransform);

g2d.transform(coordinateSystemTransformation);

Point2D.Double textOrigin = getTextOffset(d);
g2d.translate(textOrigin.x, textOrigin.y);

// DEBUG: draw the outline
// g2d.setColor(Color.MAGENTA);
// g2d.draw(new Rectangle2D.Double(0, -this.deltaHeight,
// this.deltaWidth, this.deltaHeight));

g2d.setColor(d.getTextColor());

String decodedString = d.useSymbolEncoding() ? SymbolDecoder.decode(this.string) : this.string;

// the text path left is easy: just flip the string
if (TextPath.Type.LEFT.equals(d.getTextPath())) {
decodedString = flipString(decodedString);
}

Font font = g2d.getFont();
Font adjustedFont = font;

protected Font getAdjustedFont(Font font, CGMDisplay d) {
// FIXME: remove those magic values
// adjust the size of the font depending on the extent. If the extent is
// very big, having small font sizes may create problems
Point2D.Double[] extent = d.getExtent();
if (Math.abs(extent[0].y - extent[1].y) > 1000) {
adjustedFont = font.deriveFont((float) (Math.abs(extent[0].y - extent[1].y) / 100));
g2d.setFont(adjustedFont);
}

FontRenderContext fontRenderContext = g2d.getFontRenderContext();
GlyphVector glyphVector = adjustedFont.createGlyphVector(fontRenderContext, decodedString);
Rectangle2D logicalBounds = glyphVector.getLogicalBounds();

FontMetrics fontMetrics = g2d.getFontMetrics(adjustedFont);
// XXX: unfortunately, getAscent() does not return correct values,
// see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6623223
// so we are always going to be a bit off
int screenResolution;
if (GraphicsEnvironment.isHeadless()) {
// if we're in a headless environment, assume 96 dots per inch
// (default setting for Windows XP)
screenResolution = 96;
return font.deriveFont((float) (Math.abs(extent[0].y - extent[1].y) / 100));
}
else {
screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
}
double height = fontMetrics.getAscent() * 72 / screenResolution;

scaleText(d, fontMetrics, glyphVector, logicalBounds.getWidth(), height);

if (TextPath.Type.UP.equals(d.getTextPath()) || TextPath.Type.DOWN.equals(d.getTextPath())) {
applyTextPath(d, glyphVector);
}

g2d.drawGlyphVector(glyphVector, 0, 0);

// restore the transformation that existed before painting the string
g2d.setTransform(savedTransform);

return font;
}

}
Expand Down
11 changes: 8 additions & 3 deletions src/net/sf/jcgm/core/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@
* @version $Id$
*/
public class Text extends TextCommand {


public Text(int ec, int eid, int l, DataInput in) throws IOException {
super(ec, eid, l, in);

// point (P)
this.position = makePoint();

int finalNotFinal = makeEnum();

// flag (E)
this.finalFlag = makeEnum() >= 1;
setStringComplete(this.finalFlag);
// string (S)
this.string = makeString();

// make sure all the arguments were read
Expand Down
Loading

0 comments on commit d50f702

Please sign in to comment.