📄 textreportelement.java
字号:
/*
* RectangleReportElement.java
*
* iReport -- Visual designer for generating JasperReports Documents
* Copyright (C) 2002 Giulio Toffoli gt@businesslogic.it
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Giulio Toffoli
* Via T.Aspetti, 233
* 35100 Padova ITALY
* gt@businesslogic.it
*
* Created on 28 febbraio 2003, 19.20
*/
package it.businesslogic.ireport;
import it.businesslogic.ireport.gui.*;
import it.businesslogic.ireport.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.util.*;
import java.util.List;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
public abstract class TextReportElement extends ReportElement
{
static public Rotation ROTATION_NONE;
static public Rotation ROTATION_LEFT;
static public Rotation ROTATION_RIGHT;
static
{
ROTATION_NONE = new Rotation("None", 0);
ROTATION_LEFT = new Rotation("Left", 1);
ROTATION_RIGHT = new Rotation("Right", 2);
}
private String text="";
private String reportFont="";
private String fontName="";
private String PDFFontName="";
private int fontSize=10;
private String TTFFont="";
private boolean bold=false;
private boolean underline=false;
private boolean italic=false;
private boolean strikeTrought=false;
private String lineSpacing="";
private String align="";
private String verticalAlign="";
private boolean pdfEmbedded=false;
private String pdfEncoding="";
private java.awt.Font font=null;
private String rotate = "None";
private boolean isStyledText = false;
private Box box = null;
public TextReportElement(int x, int y, int width, int height)
{
super(x,y,width,height);
//see net.sf.jasperreports.engine.base.JRBaseFont
//setGraphicElementPen("Thin");
this.bgcolor = Color.WHITE;
this.fgcolor = Color.BLACK;
this.text = "Static text\nsecond line";
this.verticalAlign = "Top";
this.align = "Left";
this.lineSpacing = "Single";
box = new Box();
//set font
setIReportFont(new IReportFont());
}
public void setIReportFont(IReportFont ireportFont){
if(ireportFont == null){
ireportFont = new IReportFont();
}
this.fontSize = ireportFont.getFontSize();
this.fontName = ireportFont.getFontName();//"SansSerif";
this.PDFFontName = ireportFont.getPDFFontName();//"Helvetica";
this.pdfEncoding = ireportFont.getPdfEncoding();//"CP1252";
this.pdfEmbedded = ireportFont.isPdfEmbedded();//false;
this.font = ireportFont.getJavaAWTFont();
}
/** Return a instance of IReportFont. */
public IReportFont getIReportFont(){
IReportFont ireportFont = new IReportFont();
ireportFont.setFontSize(this.fontSize);
ireportFont.setFontName(this.fontName);
ireportFont.setPDFFontName(this.PDFFontName);
ireportFont.setPdfEncoding(this.pdfEncoding);
ireportFont.setPdfEmbedded(this.pdfEmbedded);
ireportFont.setBold(bold);
ireportFont.setItalic(italic);
if(font != null){
Map fontAttributes = font.getAttributes();
ireportFont.setUnderline( (fontAttributes.containsKey(TextAttribute.UNDERLINE) && fontAttributes.get(TextAttribute.UNDERLINE).equals(TextAttribute.UNDERLINE_ON)) );
ireportFont.setStrikeTrought( (fontAttributes.containsKey(TextAttribute.STRIKETHROUGH) && fontAttributes.get(TextAttribute.STRIKETHROUGH).equals(TextAttribute.STRIKETHROUGH_ON)) );
}
return ireportFont;
}
public void drawObject(Graphics2D g,double zoom_factor, int x_shift_origin, int y_shift_origin)
{
//System.out.println(new java.util.Date() + " Print text " + this.getText() + " " + x_shift_origin +" " +y_shift_origin);
ArrayList textLayouts;
float x, y;
TextReportElementLayout textReportElementLayout;
AffineTransform transform;
position.x -= 10;
position.y -= 10;
x_shift_origin -= 10;
y_shift_origin -= 10;
this.zoom_factor = zoom_factor;
g.setColor( bgcolor );
if (!getTransparent().equalsIgnoreCase("Transparent"))
{
g.fillRect(getZoomedDim(position.x)-x_shift_origin,
getZoomedDim(position.y)-y_shift_origin,
getZoomedDim(width),
getZoomedDim(height));
}
g.setColor( this.fgcolor );
// Set font to default font....
//Font oldFont = g.getFont();
if (font == null)
{
int style = 0;
if (isBold()) style |= Font.BOLD;
if (isItalic()) style |= Font.ITALIC;
if (style == 0) style = Font.PLAIN;
font = new Font( this.getFontName(), style , getZoomedDim( this.getFontSize() ));
}
// Code for rotation by gt (taked by jasperreports...
int gfx_x = getZoomedDim(position.x + getBox().getLeftPadding() )-x_shift_origin;
int gfx_y = getZoomedDim(position.y + getBox().getTopPadding())-y_shift_origin;
int gfx_width = getZoomedDim(width - getBox().getLeftPadding() - getBox().getRightPadding());
int gfx_height = getZoomedDim(height - getBox().getTopPadding() - getBox().getBottomPadding());
Java2DUtil.setClip(g,
// 0,0, 3000,3000);
gfx_x,
gfx_y,
gfx_width,
gfx_height);
double angle = 0;
double angle_restore = 0;
// Apply the transformation "rotation"
// - Do nothing when rotate = "none"
transform = null;
if(rotate.equals(ROTATION_LEFT.getName()))
{
transform = new AffineTransform();
transform.rotate(-Math.PI / 2, gfx_x, gfx_y);
transform.translate(-gfx_height, -gfx_height);
gfx_y = getZoomedDim(position.y)-y_shift_origin + getZoomedDim(height);
gfx_width = getZoomedDim(height);
gfx_height = getZoomedDim(width);
Java2DUtil.setTransform(g, transform);
}
else if(rotate.equals(ROTATION_RIGHT.getName()))
{
transform = new AffineTransform();
transform.rotate(Math.PI / 2, gfx_x, gfx_y);
transform.translate(0, -gfx_width);
gfx_x = getZoomedDim(position.x)-x_shift_origin + getZoomedDim(width);
gfx_width = getZoomedDim(height);
gfx_height = getZoomedDim(width);
Java2DUtil.setTransform(g, transform);
}
// End code for rotation by gt
// We must center the text..
/*
Rectangle orgClipBounds = g.getClipBounds();
g.setClip(getZoomedDim(position.x)-x_shift_origin,
getZoomedDim(position.y)-y_shift_origin,
getZoomedDim(width),
getZoomedDim(height));
*/
if (this.getText() != null && this.getText().length() > 0)
{
int zoomedFieldHeight = gfx_height;
String allText = Misc.treatNewLineChars(this.getText());
float formatWidth = (float)gfx_width;
float verticalOffset = 0f;
FontRenderContext fontRenderContext = g.getFontRenderContext();
java.util.Map fontAttributes = font.getAttributes();
fontAttributes.put(TextAttribute.SIZE, new Float(getZoomedDim( this.getFontSize() )) );
fontAttributes.put(TextAttribute.FAMILY, this.getFontName() );
if (this.isBold())
{
fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
if (this.isItalic())
{
fontAttributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
}
if (this.isUnderline())
{
fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
}
if (this.isStrikeTrought())
{
fontAttributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
}
float lineSpacing = 1f;
if (this.getLineSpacing().equals("Single")) lineSpacing = 1f;
else if (this.getLineSpacing().equals("1_1_2")) lineSpacing = 1.5f;
else if (this.getLineSpacing().equals("Double")) lineSpacing = 2f;
AttributedString atext;
AttributedCharacterIterator paragraph;
int paragraphStart;
int paragraphEnd;
LineBreakMeasurer lineMeasurer;
TextLayout layout = null;
String paragr_text = "";
boolean isMaxHeightReached = false;
StringTokenizer tkzer = new StringTokenizer(allText, "\n");
float drawPosY = 0;
float drawPosX = 0;
paragr_text = "";
isMaxHeightReached = false;
tkzer = new StringTokenizer(allText, "\n");
textLayouts = new ArrayList();
// Calculate the layouts. (But don't draw yet because we don't know yet
// the offset which is needed if we align the text "middle" or "bottom")
while(tkzer.hasMoreTokens() && !isMaxHeightReached)
{
paragr_text = tkzer.nextToken();
atext = new AttributedString(paragr_text, fontAttributes);
paragraph = atext.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
lineMeasurer = new LineBreakMeasurer(paragraph, fontRenderContext);
lineMeasurer.setPosition(paragraphStart);
layout = null;
while (lineMeasurer.getPosition() < paragraphEnd && !isMaxHeightReached)
{
layout = lineMeasurer.nextLayout(formatWidth);
drawPosY += layout.getLeading() + lineSpacing * layout.getAscent();
if (drawPosY + layout.getDescent() <= zoomedFieldHeight+1)
{
if (this.getAlign().equals("Justify"))
{
if (layout.isLeftToRight())
{
drawPosX = 0;
}
else
{
drawPosX = formatWidth - layout.getAdvance();
}
if (lineMeasurer.getPosition() < paragraphEnd)
{
layout = layout.getJustifiedLayout(formatWidth);
}
}
else if (this.getAlign().equals("Right"))
{
if (layout.isLeftToRight())
{
drawPosX = formatWidth - layout.getAdvance();
}
else
{
drawPosX = formatWidth;
}
}
else if (this.getAlign().equals("Center"))
{
drawPosX = (formatWidth - layout.getAdvance()) / 2;
}
else //if (this.getAlign().equals("Left"))
{
if (layout.isLeftToRight())
{
drawPosX = 0;
}
else
{
drawPosX = formatWidth - layout.getAdvance();
}
}
x = drawPosX + gfx_x; //getZoomedDim(position.x)-x_shift_origin;
y = drawPosY + gfx_y; //getZoomedDim(position.y)-y_shift_origin;
textReportElementLayout = new TextReportElementLayout(layout, x, y);
textLayouts.add(textReportElementLayout);
drawPosY += layout.getDescent();
}
else
{
drawPosY -= layout.getLeading() + lineSpacing * layout.getAscent();
isMaxHeightReached = true;
}
}
}
// Calculate the offset when aligning the text.
float textHeight = drawPosY;
if (this.getVerticalAlign().equals("Top"))
{
verticalOffset = 0f;
}
else if (this.getVerticalAlign().equals("Middle"))
{
verticalOffset = ((float)zoomedFieldHeight- textHeight) / 2f;
}
else if (this.getVerticalAlign().equals("Bottom"))
{
verticalOffset = (float)zoomedFieldHeight - (float)textHeight;
}
/*
*/
// Now draw the text according to the calculated layouts.
for(Iterator i=textLayouts.iterator(); i.hasNext();)
{
textReportElementLayout = (TextReportElementLayout) i.next();
textReportElementLayout.drawWithOffset(g, verticalOffset);
}
if(transform != null)
{
// Undo the transformation "rotation"
Java2DUtil.resetTransform(g);
}
/*
int txt_width = g.getFontMetrics().stringWidth( this.getText() )/2;
int txt_height = g.getFontMetrics().getHeight();
StringTokenizer st = new StringTokenizer(getText(),"\n");
while( st.hasMoreElements())
{
String line = st.nextToken();
float formatWidth = (float) this.getWidth();
g.drawString(line,
(float)(getZoomedDim(position.x)-x_shift_origin),
getZoomedDim(position.y)-y_shift_origin + verticalOffset); //, zoomed_width, getZoomedDim(band.getHeight()
verticalOffset += g.getFontMetrics().getHeight();
}
*/
//g.drawLine(0, getZoomedDim(y)+10-vertical_scroll - txt_height - getZoomedDim(band.getHeight()/2), 700,getZoomedDim(y)+10-vertical_scroll - txt_height - getZoomedDim(band.getHeight()/2));
//g.drawLine(0, getZoomedDim(y)+10-vertical_scroll + txt_height - getZoomedDim(band.getHeight()/2), 700,getZoomedDim(y)+10-vertical_scroll + txt_height - getZoomedDim(band.getHeight()/2));
}
//g.setClip(null);
//g.setClip(orgClipBounds);
Java2DUtil.resetClip(g);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -