All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.svg.SvgText Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.svg;

import com.hfg.css.CSS;
import com.hfg.css.CSSProperty;
import com.hfg.graphics.Graphics2DState;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.graphics.units.Points;
import com.hfg.graphics.ColorUtil;
import com.hfg.graphics.TextUtil;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

//------------------------------------------------------------------------------
/**
 * Object representation of an SVG (Scalable Vector Graphics) text tag.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class SvgText extends AbstractSvgNode implements SvgNode
{
   private Font mFont;

   private static Font sDefaultFont = new Font("Arial", Font.PLAIN, 10);

   //**************************************************************************
   // CONSTRUCTORS
   //**************************************************************************

   //---------------------------------------------------------------------------
   private SvgText()
   {
      super(SVG.text);
//      setFont(sDefaultFont);
   }

   //---------------------------------------------------------------------------
   public SvgText(String inText)
   {
      this();
      setContent(inText);
   }

   //---------------------------------------------------------------------------
   public SvgText(String inText, Font inFont, Point2D inLocation)
   {
      this(inText);
      setFont(inFont);

      double x = inLocation.getX();
      double y = inLocation.getY();

      setAttribute(SvgAttr.x, (Math.floor(x) == x ? (int) x : String.format("%.3f", inLocation.getX())));
      setAttribute(SvgAttr.y, (Math.floor(y) == y ? (int) y : String.format("%.3f", inLocation.getY())));
   }

   //---------------------------------------------------------------------------
   public SvgText(XMLTag inXMLTag)
   {
      this();
      initFromXMLTag(inXMLTag);
   }

   //**************************************************************************
   // PUBLIC METHODS
   //**************************************************************************

   //---------------------------------------------------------------------------
   public SvgText setX(int inValue)
   {
      setAttribute(SvgAttr.x, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Integer getX()
   {
      String xAttr = getAttributeValue(SvgAttr.x);
      return (StringUtil.isSet(xAttr) ? (int) Float.parseFloat(xAttr) : null);
   }


   //---------------------------------------------------------------------------
   public SvgText setY(int inValue)
   {
      setAttribute(SvgAttr.y, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public Integer getY()
   {
      String yAttr = getAttributeValue(SvgAttr.y);
      return (StringUtil.isSet(yAttr) ? (int) Float.parseFloat(yAttr) : null);
   }


   //---------------------------------------------------------------------------
   /**
    * Sets a relative (delta) X coordinate adjustment to the text position.
    * Can be absolute (in, cm, mm, pt, pc) or relative (em, ex, px) lengths.
    * @param inValue a relative X coordinate adjustment to the text position
    * @return this text object to allow for method chaining
    */
   public SvgText setDx(String inValue)
   {
      setAttribute(SvgAttr.dx, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getDx()
   {
      return getAttributeValue(SvgAttr.dx);
   }


   //---------------------------------------------------------------------------
   /**
    * Sets a relative (delta) Y coordinate adjustment to the text position.
    * Can be absolute (in, cm, mm, pt, pc) or relative (em, ex, px) lengths.
    * @param inValue a relative Y coordinate adjustment to the text position
    * @return this text object to allow for method chaining
    */
   public SvgText setDy(String inValue)
   {
      setAttribute(SvgAttr.dy, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getDy()
   {
      return getAttributeValue(SvgAttr.dy);
   }

   
   //---------------------------------------------------------------------------
   public int getWidth()
   {
      return (int) Float.parseFloat(getAttributeValue(SvgAttr.width));
   }

   //---------------------------------------------------------------------------
   public int getHeight()
   {
      return (int) Float.parseFloat(getAttributeValue(SvgAttr.height));
   }

   //---------------------------------------------------------------------------
   public SvgText setFill(Color inColor)
   {
      setAttribute(SvgAttr.fill, "#" + ColorUtil.colorToHex(inColor));
      return this;
   }

   //---------------------------------------------------------------------------
   public SvgText setFont(Font inValue)
   {
      if (inValue != null)
      {
         // TODO: Remove existing style fields relating to the font.

         mFont = inValue;
         addStyle(CSSProperty.font_family.name() + ":" + mFont.getName());
         addStyle(CSSProperty.font_size.name() + ":" + new Points(mFont.getSize2D()).to(GfxUnits.pixels) + "px");
         if (mFont.isBold())
         {
            addStyle(CSS.BOLD);
         }

         if (mFont.isItalic())
         {
            addStyle(CSS.ITALIC);
         }
      }

      return this;
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgText setFilter(String inValue)
   {
      return (SvgText) super.setFilter(inValue);
   }

   //---------------------------------------------------------------------------
   public SvgText setTitle(String inValue)
   {
      setAttribute(SvgAttr.title, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgText setClass(String inValue)
   {
      return (SvgText) super.setClass(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgText setId(String inValue)
   {
      return (SvgText) super.setId(inValue);
   }

   //--------------------------------------------------------------------------
   @Override
   public SvgText addStyle(String inValue)
   {
      return (SvgText) super.addStyle(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgText setStyle(String inValue)
   {
      return (SvgText) super.setStyle(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgText setTransform(String inValue)
   {
      return (SvgText) super.setTransform(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public Rectangle getBoundsBox()
   {
      float x = (getX() != null ? getX() : 0);
      float y = (getAttributeValue(SvgAttr.y) != null ?  Float.parseFloat(getAttributeValue(SvgAttr.y)) : 0);

      Rectangle textBounds = (mFont != null ? TextUtil.getStringRect(getContent(), mFont) : new Rectangle(0, 0, 0, 0));

      Rectangle boundsBox = new Rectangle2D.Float(x, y - textBounds.height, textBounds.width, textBounds.height).getBounds();
      adjustBoundsForTransform(boundsBox);

      return boundsBox;
   }

   //--------------------------------------------------------------------------
   @Override
   public void draw(Graphics2D g2)
   {
      // Save settings
      Graphics2DState origState = new Graphics2DState(g2);

      Paint paint   = getG2Paint();
      if (paint != null) g2.setPaint(paint);

      if (mFont != null)
      {
         g2.setFont(mFont);
      }
      else
      {
         Font locallyAdjustedFont = getAdjustedFont(origState.getFont());
         if (locallyAdjustedFont != null)
         {
            g2.setFont(locallyAdjustedFont);
         }
      }

      applyTransforms(g2);

      int x = (getX() != null ? getX() : 0);
      int y = (getY() != null ? getY() : 0);

      g2.drawString(getContent(), x, y);

      // Restore settings
      origState.applyTo(g2);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy