Package net.sf.latexdraw.generators.svg

Source Code of net.sf.latexdraw.generators.svg.LDotSVGGenerator

package net.sf.latexdraw.generators.svg;

import java.awt.geom.Point2D;
import java.util.List;

import net.sf.latexdraw.badaboom.BadaboomCollector;
import net.sf.latexdraw.glib.models.ShapeFactory;
import net.sf.latexdraw.glib.models.interfaces.prop.IDotProp.DotStyle;
import net.sf.latexdraw.glib.models.interfaces.shape.IDot;
import net.sf.latexdraw.glib.views.Java2D.interfaces.IViewDot;
import net.sf.latexdraw.glib.views.Java2D.interfaces.IViewShape;
import net.sf.latexdraw.glib.views.Java2D.interfaces.View2DTK;
import net.sf.latexdraw.parsers.svg.CSSColors;
import net.sf.latexdraw.parsers.svg.SVGAttributes;
import net.sf.latexdraw.parsers.svg.SVGDocument;
import net.sf.latexdraw.parsers.svg.SVGElement;
import net.sf.latexdraw.parsers.svg.SVGGElement;
import net.sf.latexdraw.parsers.svg.parsers.Graphics2D2SVG;
import net.sf.latexdraw.parsers.svg.parsers.SVGPointsParser;
import net.sf.latexdraw.util.LNamespace;

import org.malai.mapping.MappingRegistry;

/**
* Defines a SVG generator for a dot.<br>
*<br>
* This file is part of LaTeXDraw.<br>
* Copyright (c) 2005-2014 Arnaud BLOUIN<br>
*<br>
*  LaTeXDraw 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.<br>
*<br>
*  LaTeXDraw is distributed 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.<br>
*<br>
* 11/11/07<br>
* @author Arnaud BLOUIN
* @version 3.0
*/
class LDotSVGGenerator extends LShapeSVGGenerator<IDot> {
  /**
   * Creates a generator of SVG dot.
   * @param dot The dot used for the generation.
   * @throws IllegalArgumentException If dot is null.
   */
  protected LDotSVGGenerator(final IDot dot) {
    super(dot);
  }


  /**
   * Creates a dot from a latexdraw-SVG element.
   * @param elt The source element.
   * @since 2.0.0
   */
  protected LDotSVGGenerator(final SVGGElement elt, final boolean withTransformation) {
    this(ShapeFactory.createDot(ShapeFactory.createPoint()));

    if(elt==null)
      throw new IllegalArgumentException();

    String v = elt.getAttribute(elt.getUsablePrefix(LNamespace.LATEXDRAW_NAMESPACE_URI)+LNamespace.XML_SIZE);
    final SVGElement main = getLaTeXDrawElement(elt, null);

    try { shape.setDotStyle(DotStyle.getStyle(elt.getAttribute(elt.getUsablePrefix(LNamespace.LATEXDRAW_NAMESPACE_URI)+LNamespace.XML_DOT_SHAPE))); }
    catch(final IllegalArgumentException e) { BadaboomCollector.INSTANCE.add(e); }

    if(v!=null)
      try { shape.setDiametre(Double.parseDouble(v)); }
      catch(final NumberFormatException e) { BadaboomCollector.INSTANCE.add(e); }

    v = elt.getAttribute(elt.getUsablePrefix(LNamespace.LATEXDRAW_NAMESPACE_URI)+LNamespace.XML_POSITION);

    final List<Point2D> pos = SVGPointsParser.getPoints(v);

    if(pos!=null && !pos.isEmpty())
      shape.setPosition(pos.get(0).getX(), pos.get(0).getY());

    setSVGLatexdrawParameters(elt);
    setSVGParameters(main);

    if(withTransformation)
      applyTransformations(elt);

    if(!shape.isFillable() && shape.isFilled())
      shape.setLineColour(CSSColors.INSTANCE.getRGBColour(main.getFill()));
  }


  @Override
  public SVGElement toSVG(final SVGDocument doc) {
    if(doc==null)
      return null;

    final Graphics2D2SVG graphics = new Graphics2D2SVG(doc);
        final SVGElement root;
        final boolean viewCreated;
        // Instead of creating a view, its is gathered from the Java view of the application.
    IViewShape view = MappingRegistry.REGISTRY.getTargetFromSource(shape, IViewDot.class);

    if(view==null) {
      view =  View2DTK.getFactory().createView(shape);
      viewCreated = true;
    }
    else viewCreated = false;

        view.paint(graphics, null);
        root = graphics.getElement();

        root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE+':'+LNamespace.XML_TYPE, LNamespace.XML_TYPE_DOT);
        root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE+':'+LNamespace.XML_SIZE, String.valueOf(shape.getDiametre()));
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE+':'+LNamespace.XML_DOT_SHAPE, shape.getDotStyle().getPSTToken());
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE+':'+LNamespace.XML_POSITION, shape.getPosition().getX() + " " + shape.getPosition().getY()); //$NON-NLS-1$

    graphics.dispose();

    if(viewCreated)
      view.flush();

    return root;
  }
}
TOP

Related Classes of net.sf.latexdraw.generators.svg.LDotSVGGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.