001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.painter.text;
031:
032: import java.awt.*;
033: import java.awt.geom.AffineTransform;
034:
035: import javax.swing.JComponent;
036: import javax.swing.plaf.basic.BasicGraphicsUtils;
037:
038: import org.jvnet.lafwidget.utils.RenderingUtils;
039: import org.jvnet.substance.utils.SubstanceCoreUtilities;
040:
041: /**
042: * The default core implementation of {@link SubstanceTextPainter}.
043: *
044: * @author Kirill Grouchnikov
045: */
046: public class DefaultTextPainter extends AbstractTextPainter {
047: /*
048: * (non-Javadoc)
049: *
050: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#needsBackgroundImage()
051: */
052: public boolean needsBackgroundImage() {
053: return false;
054: }
055:
056: public void setBackgroundFill(JComponent comp,
057: Color backgroundFillColor, boolean toOverlayWatermark,
058: int watermarkOffsetX, int watermarkOffsetY) {
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#renderSurface(java.awt.Graphics)
065: */
066: public void renderSurface(Graphics g) {
067: Graphics2D g2d = (Graphics2D) g.create();
068: SubstanceCoreUtilities.workaroundBug6576507(g2d);
069: RenderingUtils.installDesktopHints(g2d);
070:
071: for (TextLineInfo lineInfo : textLines) {
072: g2d.setFont(lineInfo.font);
073: g2d.setColor(lineInfo.color);
074: if (lineInfo.clip != null)
075: g2d.setClip(lineInfo.clip);
076: AffineTransform currTransform = g2d.getTransform();
077: if (lineInfo.transform != null)
078: g2d.transform(lineInfo.transform);
079: String text = lineInfo.text;
080: if ((text != null) && (text.length() > 0)) {
081: BasicGraphicsUtils.drawStringUnderlineCharAt(g2d,
082: lineInfo.text, lineInfo.mnemonicIndex,
083: lineInfo.textRect.x, lineInfo.textRect.y
084: + g2d.getFontMetrics().getAscent());
085: }
086: g2d.setTransform(currTransform);
087: }
088: g2d.dispose();
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#attachVerticalText(javax.swing.JComponent,
095: * java.awt.Rectangle, java.lang.String, int, java.awt.Font,
096: * java.awt.Color, java.awt.Rectangle, boolean)
097: */
098: public void attachVerticalText(JComponent comp, Rectangle textRect,
099: String text, int mnemonicIndex, Font font, Color color,
100: Rectangle clip, boolean isFromBottomToTop) {
101: AffineTransform at = null;
102:
103: if (!isFromBottomToTop) {
104: at = AffineTransform.getTranslateInstance(textRect.x
105: + textRect.width, textRect.y);
106: at.rotate(Math.PI / 2);
107: } else {
108: at = AffineTransform.getTranslateInstance(textRect.x,
109: textRect.y + textRect.height);
110: at.rotate(-Math.PI / 2);
111: }
112: Rectangle newRect = new Rectangle(0, 0, textRect.width,
113: textRect.height);
114: this .textLines.add(new TextLineInfo(newRect, text,
115: mnemonicIndex, font, color, clip, at));
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#isNative()
122: */
123: public boolean isNative() {
124: return false;
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#dispose()
131: */
132: public void dispose() {
133: }
134:
135: public java.awt.Dimension getTextBounds(Component comp,
136: java.awt.Font font, String text) {
137: FontMetrics fm = comp.getFontMetrics(font);
138: return new Dimension(fm.stringWidth(text), fm.getHeight());
139: }
140: }
|