<commit_before>/*$Id$
 *
 * This source file is a part of the Fresco Project.
 * Copyright (C) 1999 <NAME> <<EMAIL>> 
 * http://www.fresco.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 * MA 02139, USA.
 */

#include <Prague/Sys/Tracer.hh>
#include <Fresco/config.hh>
#include <Fresco/PickTraversal.hh>
#include <Fresco/DrawTraversal.hh>
#include <Fresco/DrawingKit.hh>
#include <Fresco/IO.hh>
#include <Berlin/Geometry.hh>
#include <Berlin/TransformImpl.hh>
#include <Berlin/RegionImpl.hh>
#include <Berlin/Color.hh>
#include <Berlin/Vertex.hh>
#include <Berlin/Provider.hh>
#include "FigureImpl.hh"

using namespace Geometry;
using namespace Prague;
using namespace Fresco;

using namespace Berlin::FigureKit;

TransformFigure::TransformFigure()
  : _mode(Figure::outline),
    _tx(new TransformImpl),
    _ext(new RegionImpl)
{
  _fg.red = _fg.green = _fg.blue = 0., _fg.alpha = 1.;
  _bg.red = _bg.green = _bg.blue = 0., _bg.alpha = 1.;
}

TransformFigure::~TransformFigure() {}
Transform_ptr TransformFigure::transformation() { return _tx->_this();}
void TransformFigure::request(Fresco::Graphic::Requisition &r)
{
  Trace trace("TransformFigure::request");
  Allocation::Info info;
  Lease_var<RegionImpl> region(Provider<RegionImpl>::provide());
  extension(info, Region_var(region->_this()));
  if (region->valid)
    {
      Coord x_lead = -region->lower.x, x_trail = region->upper.x;
      Coord y_lead = -region->lower.y, y_trail = region->upper.y;
      GraphicImpl::require_lead_trail(r.x, x_lead, x_lead, x_lead, x_trail, x_trail, x_trail);
      GraphicImpl::require_lead_trail(r.y, y_lead, y_lead, y_lead, y_trail, y_trail, y_trail);
      r.z.defined = false;
    }
  else
    {
      r.x.defined = false;
      r.y.defined = false;
      r.z.defined = false;
    }
}

void TransformFigure::extension(const Allocation::Info &info, Region_ptr region)
{
  Trace trace("TransformFigure::extension");
  if (_ext->valid)
    {
      Lease_var<RegionImpl> tmp(Provider<RegionImpl>::provide());
      tmp->copy(Region_var(_ext->_this()));
      tmp->xalign = tmp->yalign = tmp->zalign = 0.;
      Lease_var<TransformImpl> transformation(Provider<TransformImpl>::provide());
      if (!CORBA::is_nil(info.transformation)) transformation->copy(info.transformation);
      transformation->premultiply(Transform_var(_tx->_this()));
      tmp->apply_transform(Transform_var(transformation->_this()));
      region->merge_union(Region_var(tmp->_this()));
    }
}

void TransformFigure::pick(PickTraversal_ptr traversal)
{
  if (_ext->valid && traversal->intersects_region(Region_var(_ext->_this())))
    traversal->hit();
}

void TransformFigure::need_redraw()
{
  Trace trace("TransformFigure::need_redraw");
  Allocation::Info info;
  Lease_var<RegionImpl> region(Provider<RegionImpl>::provide());
  extension(info, Region_var(region->_this()));
  need_redraw_region(Region_var(region->_this()));
}

void TransformFigure::resize() {}
void TransformFigure::copy(const TransformFigure &tf)
{
  _mode = tf._mode;
  _fg = tf._fg;
  _bg = tf._bg;
  _tx->copy(Transform_var(tf._tx->_this()));
  if (tf._ext->valid) _ext->copy(Region_var(tf._ext->_this()));
}

FigureImpl::FigureImpl() : _path(new Fresco::Path()) { _path->shape = convex;}
FigureImpl::~FigureImpl() {}

void FigureImpl::add_point(Coord x, Coord y)
{
  if (_path->nodes.length() == 0)
    {
      _ext->lower.x = x;
      _ext->upper.x = x;
      _ext->lower.y = y;
      _ext->upper.y = y;
      _ext->lower.z = Coord(0);
      _ext->upper.z = Coord(0);
      _ext->valid = true;
    }
  else
    {
      _ext->lower.x = Math::min(_ext->lower.x, x);
      _ext->upper.x = Math::max(_ext->upper.x, x);
      _ext->lower.y = Math::min(_ext->lower.y, y);
      _ext->upper.y = Math::max(_ext->upper.y, y);
    }
  Vertex v;
  v.x = x;
  v.y = y;
  v.z = 0.;
  CORBA::ULong n = _path->nodes.length();
  _path->nodes.length(n + 1);
  _path->nodes[n] = v;
}

void FigureImpl::extension(const Allocation::Info &info, Region_ptr region)
{
  Trace trace("FigureImpl::extension");
  if (_path->nodes.length() > 0)
    {
      Lease_var<RegionImpl> tmp(Provider<RegionImpl>::provide());
      tmp->copy(Region_var(_ext->_this()));
      tmp->xalign = tmp->yalign = tmp->zalign = 0.;
      Lease_var<TransformImpl> transformation(Provider<TransformImpl>::provide());
      transformation->copy(info.transformation);
      transformation->premultiply(Transform_var(_tx->_this()));
      tmp->apply_transform(Transform_var(transformation->_this()));
      if (_mode & Figure::outline)
	{
// 	  Coord w = 1.;
// 	  if (is_not_nil(style_))
// 	    {
// 	      Brush_var b = style_->brush_attr();
// 	      if (is_not_nil(b))
// 		{
// 		  XfBrush::Info* i;
// 		  b->brush_info(i);
// 		  if (!Math::equal(i->width, float(0), float(1e-2))) w = i->width;
//                 }
//             }
// 	  tmp->lower.x -= w; tmp->upper.x += w;
// 	  tmp->lower.y -= w; tmp->upper.y += w;
// 	  tmp->lower.z -= w; tmp->upper.z += w;
	}
      region->merge_union(Region_var(tmp->_this()));
    }
}

void FigureImpl::draw(DrawTraversal_ptr traversal)
{
  Trace trace("FigureImpl::draw");
  if (_path->nodes.length() > 0)
    {
      // bounding box culling, use extension(...) to add brush effect into extension.
      Allocation::Info info;
      Lease_var<RegionImpl> region(Provider<RegionImpl>::provide());
      extension(info, Region_var(region->_this()));
      if (traversal->intersects_region(Region_var(region->_this())))
	{
      std::cout << "FigureImpl::draw with " << _path->nodes.length() << " vertices" << std::endl;
      for (size_t i = 0; i != _path->nodes.length(); ++i)
         std::cout << _path->nodes[i].x << ' ' << _path->nodes[i].y << std::endl;
      DrawingKit_var drawing = traversal->drawing();
	  Color color = drawing->foreground();
	  drawing->save();
	  Transform_var tmp = drawing->transformation();
	  Lease_var<TransformImpl> cumulative(Provider<TransformImpl>::provide());
	  cumulative->copy(Transform_var(drawing->transformation()));
	  cumulative->premultiply(Transform_var(_tx->_this()));
	  drawing->transformation(Transform_var(cumulative->_this()));
//  	  if (_mode & Figure::fill)
//  	    {
// 	      drawing->foreground(_bg);
// 	      drawing->surface_fillstyle(DrawingKit::solid);
// 	      drawing->draw_path(_path);
// 	    }
//  	  if (_mode & Figure::outline)
// 	    {
// 	      drawing->foreground(_fg);
// 	      drawing->surface_fillstyle(DrawingKit::outlined);
	  drawing->draw_path(_path);
// 	}
	  drawing->restore();
	}
    }
}

/*<commit_msg>Remove silly debugging information.<commit_after>/*$Id$
 *
 * This source file is a part of the Fresco Project.
 * Copyright (C) 1999 <NAME> <<EMAIL>> 
 * http://www.fresco.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 * MA 02139, USA.
 */

#include <Prague/Sys/Tracer.hh>
#include <Fresco/config.hh>
#include <Fresco/PickTraversal.hh>
#include <Fresco/DrawTraversal.hh>
#include <Fresco/DrawingKit.hh>
#include <Fresco/IO.hh>
#include <Berlin/Geometry.hh>
#include <Berlin/TransformImpl.hh>
#include <Berlin/RegionImpl.hh>
#include <Berlin/Color.hh>
#include <Berlin/Vertex.hh>
#include <Berlin/Provider.hh>
#include "FigureImpl.hh"

using namespace Geometry;
using namespace Prague;
using namespace Fresco;

using namespace Berlin::FigureKit;

TransformFigure::TransformFigure()
  : _mode(Figure::outline),
    _tx(new TransformImpl),
    _ext(new RegionImpl)
{
  _fg.red = _fg.green = _fg.blue = 0., _fg.alpha = 1.;
  _bg.red = _bg.green = _bg.blue = 0., _bg.alpha = 1.;
}

TransformFigure::~TransformFigure() {}
Transform_ptr TransformFigure::transformation() { return _tx->_this();}
void TransformFigure::request(Fresco::Graphic::Requisition &r)
{
  Trace trace("TransformFigure::request");
  Allocation::Info info;
  Lease_var<RegionImpl> region(Provider<RegionImpl>::provide());
  extension(info, Region_var(region->_this()));
  if (region->valid)
    {
      Coord x_lead = -region->lower.x, x_trail = region->upper.x;
      Coord y_lead = -region->lower.y, y_trail = region->upper.y;
      GraphicImpl::require_lead_trail(r.x, x_lead, x_lead, x_lead, x_trail, x_trail, x_trail);
      GraphicImpl::require_lead_trail(r.y, y_lead, y_lead, y_lead, y_trail, y_trail, y_trail);
      r.z.defined = false;
    }
  else
    {
      r.x.defined = false;
      r.y.defined = false;
      r.z.defined = false;
    }
}

void TransformFigure::extension(const Allocation::Info &info, Region_ptr region)
{
  Trace trace("TransformFigure::extension");
  if (_ext->valid)
    {
      Lease_var<RegionImpl> tmp(Provider<RegionImpl>::provide());
      tmp->copy(Region_var(_ext->_this()));
      tmp->xalign = tmp->yalign = tmp->zalign = 0.;
      Lease_var<TransformImpl> transformation(Provider<TransformImpl>::provide());
      if (!CORBA::is_nil(info.transformation)) transformation->copy(info.transformation);
      transformation->premultiply(Transform_var(_tx->_this()));
      tmp->apply_transform(Transform_var(transformation->_this()));
      region->merge_union(Region_var(tmp->_this()));
    }
}

void TransformFigure::pick(PickTraversal_ptr traversal)
{
  if (_ext->valid && traversal->intersects_region(Region_var(_ext->_this())))
    traversal->hit();
}

void TransformFigure::need_redraw()
{
  Trace trace("TransformFigure::need_redraw");
  Allocation::Info info;
  Lease_var<RegionImpl> region(Provider<RegionImpl>::provide());
  extension(info, Region_var(region->_this()));
  need_redraw_region(Region_var(region->_this()));
}

void TransformFigure::resize() {}
void TransformFigure::copy(const TransformFigure &tf)
{
  _mode = tf._mode;
  _fg = tf._fg;
  _bg = tf._bg;
  _tx->copy(Transform_var(tf._tx->_this()));
  if (tf._ext->valid) _ext->copy(Region_var(tf._ext->_this()));
}

FigureImpl::FigureImpl() : _path(new Fresco::Path()) { _path->shape = convex;}
FigureImpl::~FigureImpl() {}

void FigureImpl::add_point(Coord x, Coord y)
{
  if (_path->nodes.length() == 0)
    {
      _ext->lower.x = x;
      _ext->upper.x = x;
      _ext->lower.y = y;
      _ext->upper.y = y;
      _ext->lower.z = Coord(0);
      _ext->upper.z = Coord(0);
      _ext->valid = true;
    }
  else
    {
      _ext->lower.x = Math::min(_ext->lower.x, x);
      _ext->upper.x = Math::max(_ext->upper.x, x);
      _ext->lower.y = Math::min(_ext->lower.y, y);
      _ext->upper.y = Math::max(_ext->upper.y, y);
    }
  Vertex v;
  v.x = x;
  v.y = y;
  v.z = 0.;
  CORBA::ULong n = _path->nodes.length();
  _path->nodes.length(n + 1);
  _path->nodes[n] = v;
}

void FigureImpl::extension(const Allocation::Info &info, Region_ptr region)
{
  Trace trace("FigureImpl::extension");
  if (_path->nodes.length() > 0)
    {
      Lease_var<RegionImpl> tmp(Provider<RegionImpl>::provide());
      tmp->copy(Region_var(_ext->_this()));
      tmp->xalign = tmp->yalign = tmp->zalign = 0.;
      Lease_var<TransformImpl> transformation(Provider<TransformImpl>::provide());
      transformation->copy(info.transformation);
      transformation->premultiply(Transform_var(_tx->_this()));
      tmp->apply_transform(Transform_var(transformation->_this()));
      if (_mode & Figure::outline)
	{
// 	  Coord w = 1.;
// 	  if (is_not_nil(style_))
// 	    {
// 	      Brush_var b = style_->brush_attr();
// 	      if (is_not_nil(b))
// 		{
// 		  XfBrush::Info* i;
// 		  b->brush_info(i);
// 		  if (!Math::equal(i->width, float(0), float(1e-2))) w = i->width;
//                 }
//             }
// 	  tmp->lower.x -= w; tmp->upper.x += w;
// 	  tmp->lower.y -= w; tmp->upper.y += w;
// 	  tmp->lower.z -= w; tmp->upper.z += w;
	}
      region->merge_union(Region_var(tmp->_this()));
    }
}

void FigureImpl::draw(DrawTraversal_ptr traversal)
{
  Trace trace("FigureImpl::draw");
  if (_path->nodes.length() > 0)
    {
      // bounding box culling, use extension(...) to add brush effect into extension.
      Allocation::Info info;
      Lease_var<RegionImpl> region(Provider<RegionImpl>::provide());
      extension(info, Region_var(region->_this()));
      if (traversal->intersects_region(Region_var(region->_this())))
	{
      DrawingKit_var drawing = traversal->drawing();
	  Color color = drawing->foreground();
	  drawing->save();
	  Transform_var tmp = drawing->transformation();
	  Lease_var<TransformImpl> cumulative(Provider<TransformImpl>::provide());
	  cumulative->copy(Transform_var(drawing->transformation()));
	  cumulative->premultiply(Transform_var(_tx->_this()));
	  drawing->transformation(Transform_var(cumulative->_this()));
//  	  if (_mode & Figure::fill)
//  	    {
// 	      drawing->foreground(_bg);
// 	      drawing->surface_fillstyle(DrawingKit::solid);
// 	      drawing->draw_path(_path);
// 	    }
//  	  if (_mode & Figure::outline)
// 	    {
// 	      drawing->foreground(_fg);
// 	      drawing->surface_fillstyle(DrawingKit::outlined);
	  drawing->draw_path(_path);
// 	}
	  drawing->restore();
	}
    }
}

/*
{'fraction_non_alphanumeric': 0.1669247009148487, 'fraction_numerical': 0.004292751583391977, 'mean_word_length': 3.691647408385606, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 263, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4536461', 'n_tokens_mistral': 4944, 'n_tokens_neox': 4743, 'n_words': 952}