Skip to content

Commit

Permalink
First step towards implementing #71. Size does not yet take into acco…
Browse files Browse the repository at this point in the history
…unt the zoom level, but parsing ^ and _ commands is now active.
  • Loading branch information
DarwinNE committed May 3, 2020
1 parent 070f713 commit f7680dc
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/net/sourceforge/fidocadj/circuit/CircuitPanel.java
Expand Up @@ -438,7 +438,7 @@ public void changeZoom(double tz)
double z=Math.round(tz*100.0)/100.0;
cs.setMagnitudes(z,z);
eea.successiveMove=false;

requestFocusInWindow(); // #
repaint();
}

Expand Down
153 changes: 153 additions & 0 deletions src/net/sourceforge/fidocadj/graphic/DecoratedText.java
@@ -0,0 +1,153 @@
package net.sourceforge.fidocadj.graphic;

/** Decorated text is a class that provides advanced text functions.
It is possible to do things as follows:
I_{dsat}
R^2
V^{2e}
to indicate indices or exponents. The command _ indicates that the next
character will be an index. The command ^ indicates that the next character
is an exponent. If more of one character must be put, put them in braces.
Use \_ to enter a bar and \^ to enter a caret.
<pre>
This file is part of FidoCadJ.
FidoCadJ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FidoCadJ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FidoCadJ. If not,
@see <a href=http://www.gnu.org/licenses/>http://www.gnu.org/licenses/</a>.
Copyright 2020 by Davide Bucci
</pre>
*/
public class DecoratedText
{
GraphicsInterface g;

public DecoratedText(GraphicsInterface g)
{
this.g=g;
}

/** Get the width of the given string with the current font.
@param s the string to be used.
@return the width of the string, in pixels.
*/
public int getStringWidth(String s)
{
return g.getStringWidth(s);
}

StringBuffer btoken;
String bstr;
int currentIndex;
int lastIndex;
int exponentLevel;

final static int CHUNK = 0;
final static int INDEX = 1;
final static int EXPONENT = 2;
final static int END = 3;

private int getToken()
{
if(currentIndex >= lastIndex)
return END;
char c=bstr.charAt(currentIndex);
if(c=='_') {
++currentIndex;
return INDEX;
} else if (c=='^') {
++currentIndex;
return EXPONENT;
}
btoken=new StringBuffer();
while(true) {
btoken.append(c);
++currentIndex;

if(currentIndex>=lastIndex)
return CHUNK;
c=bstr.charAt(currentIndex);
if(c=='_' || c=='^')
return CHUNK;
}
}
private void resetTokenization(String s)
{
bstr=s;
currentIndex=0;
exponentLevel=0;
lastIndex=s.length();
}

private float getSizeMultLevel()
{
switch((int)Math.abs(exponentLevel)){
case 0:
return 1f;
case 1:
return 0.8f;
case 2:
return 0.7f;
case 3:
return 0.6f;
default:
return 0.5f;
}
}
/** Draw a string on the current graphic context.
@param str the string to be drawn.
@param x the x coordinate of the starting point.
@param y the y coordinate of the starting point.
*/
public void drawString(String str,
int x,
int y)
{
/*
[FIDOCAD]
FJC A 0.35
TY 1 2 4 3 0 0 0 Helvetica 1^2^3^4^5^6^7^8^9
*/
resetTokenization(str);
int xc=x;
double fontSize;
fontSize=g.getFontSize();
int t;
while((t=getToken())!=END) {
switch(t) {
case CHUNK:

g.setFontSize(fontSize*getSizeMultLevel());
g.drawString(btoken.toString(),xc,
y-(int)Math.round(exponentLevel*fontSize*0.1));
xc+=g.getStringWidth(btoken.toString());
break;
case EXPONENT:
++exponentLevel;
break;
case INDEX:
--exponentLevel;
break;
case END:
break;
default:
}
}
}
}
6 changes: 6 additions & 0 deletions src/net/sourceforge/fidocadj/graphic/GraphicsInterface.java
Expand Up @@ -124,6 +124,12 @@ public void drawLine(int x1,
public void setFont(String name, double size, boolean isItalic,
boolean isBold);

/** Get the font size */
public double getFontSize();

/** Set the font size */
public void setFontSize(double size);

/** Get the ascent metric of the current font.
@return the value of the ascent, in pixels.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/net/sourceforge/fidocadj/graphic/nil/GraphicsNull.java
Expand Up @@ -35,7 +35,7 @@
along with FidoCadJ. If not,
@see <a href=http://www.gnu.org/licenses/>http://www.gnu.org/licenses/</a>.
Copyright 2014 by Davide Bucci
Copyright 2014-2020 by Davide Bucci
</pre>
*/

Expand Down Expand Up @@ -192,7 +192,17 @@ public void setFont(String name, double size, boolean isItalic,

fm=g.getFontMetrics(f);
}
/** Get the font size */
public double getFontSize()
{
return g.getFont().getSize();
}

/** Set the font size */
public void setFontSize(double size)
{
g.setFont(g.getFont().deriveFont((int)Math.round(size)));
}
/** Simple version. It sets the current font.
@param name the name of the typeface.
@param size the vertical size in pixels.
Expand Down
21 changes: 17 additions & 4 deletions src/net/sourceforge/fidocadj/graphic/swing/Graphics2DSwing.java
Expand Up @@ -301,6 +301,18 @@ public void setFont(String name, double size)
{
setFont(name, size, false, false);
}
/** Get the font size */
public double getFontSize()
{
return f.getSize();
}

/** Set the font size */
public void setFontSize(double size)
{
f=f.deriveFont((float)size);
g.setFont(f);
}

/** Get the ascent metric of the current font.
@return the value of the ascent, in pixels.
Expand Down Expand Up @@ -503,6 +515,7 @@ I therefore choose (from v. 0.20.2) to use only graphic context
AffineTransform ats=(AffineTransform)at.clone();
AffineTransform stretching= new AffineTransform();
AffineTransform mm= new AffineTransform();
DecoratedText dt=new DecoratedText(this);

stretching.scale(1,xyfactor);

Expand All @@ -520,7 +533,7 @@ I therefore choose (from v. 0.20.2) to use only graphic context
if(!g.getFont().equals(f))
g.setFont(f);

g.drawString(txt,-xa,qq+h);
dt.drawString(txt,-xa,qq+h);
}
} else {
// Here the text is normal
Expand All @@ -538,7 +551,7 @@ I therefore choose (from v. 0.20.2) to use only graphic context
} else {
if(!g.getFont().equals(f))
g.setFont(f);
g.drawString(txt,xa,qq+h);
dt.drawString(txt,xa,qq+h);
if(needsStretching)
g.setTransform(ats);
return;
Expand All @@ -555,7 +568,7 @@ I therefore choose (from v. 0.20.2) to use only graphic context
if(!g.getFont().equals(f))
g.setFont(f);

g.drawString(txt,-xa,qq+h);
dt.drawString(txt,-xa,qq+h);

} else {
// Here the text is just rotated
Expand All @@ -564,7 +577,7 @@ I therefore choose (from v. 0.20.2) to use only graphic context
g.setTransform(at);
if(!g.getFont().equals(f))
g.setFont(f);
g.drawString(txt,xa,qq+h);
dt.drawString(txt,xa,qq+h);
}
}
g.setTransform(ats);
Expand Down
6 changes: 4 additions & 2 deletions src/net/sourceforge/fidocadj/toolbars/ToolbarZoom.java
Expand Up @@ -212,21 +212,23 @@ public void actionPerformed(ActionEvent evt)

// Buttons
if(s.equals(Globals.messages.getString("ShowGrid"))) {
//showGrid.setSelected(!showGrid.isSelected());
// Toggle grid visibility
if(changeListener!=null)
changeListener.setGridVisibility(showGrid.isSelected());
} else if(s.equals(Globals.messages.getString("SnapToGrid"))) {
//snapGrid.setSelected(!snapGrid.isSelected());
// Toggle snap to grid
if(changeListener!=null)
changeListener.setSnapState(snapGrid.isSelected());
} else if(s.equals(Globals.messages.getString("Zoom_fit"))) {
// Zoom to fit
if(actualZoomToFitListener!=null) {
actualZoomToFitListener.zoomToFit();
}
} else if(evt.getSource() instanceof JComboBox) {
// ComboBox: the only one is about the zoom settings.
handleZoomChangeEvents(evt);
} else if(s.equals(Globals.messages.getString("Libs"))) {
// Toggle library visibility
actualZoomToFitListener.showLibs(showLibs.isSelected());
}
}
Expand Down

0 comments on commit f7680dc

Please sign in to comment.