/* Copyright (C) 2002 The gtkmm Development Team
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

#include <pangomm/font.h>
#include <pangomm/attributes.h>
#include <pangomm/glyphstring.h>

namespace Pango
{

Analysis::Analysis()
{}

Analysis::Analysis(const PangoAnalysis* src)
:
  gobject_ (*src)
{}

SListHandle_Attribute Analysis::get_extra_attrs() const
{
  return SListHandle_Attribute(gobj()->extra_attrs, Glib::OWNERSHIP_NONE);
}


Item::Item(const Item& src)
:
  gobject_ ((src.gobject_) ? pango_item_copy(src.gobject_) : 0)
{}

Item::Item(PangoItem* castitem, bool make_a_copy)
{
  // For BoxedType wrappers, make_a_copy is true by default.  The static
  // BoxedType wrappers must always take a copy, thus make_a_copy = true
  // ensures identical behaviour if the default argument is used.

  if(make_a_copy)
  {
    if(castitem)
      gobject_ = pango_item_copy(castitem);
    else
      gobject_ = 0;
  }
  else
  {
    // It was given to us by a function which has already made a copy for us to keep.
    gobject_ = castitem;
  }
}

Item& Item::operator=(const Item& src)
{
  PangoItem *const new_gobject = (src.gobject_) ? pango_item_copy(src.gobject_) : 0;

  if(gobject_)
    pango_item_free(gobject_);
  gobject_ = new_gobject;

  return *this;
}

Item::~Item()
{
  if(gobject_)
    pango_item_free(gobject_);
}

PangoItem* Item::gobj_copy() const
{
  return pango_item_copy(gobject_);
}

Analysis Item::get_analysis() const
{
  return Analysis(&gobj()->analysis);
}

Glib::ustring Item::get_segment(const Glib::ustring& text) const
{
  const char *const start = text.data() + gobj()->offset;
  return Glib::ustring(start, start + gobj()->length);
}

Pango::GlyphString Item::shape(const Glib::ustring& text) const
{
  return GlyphString(text, get_analysis());
}

Pango::GlyphString Item::shape(const Glib::ustring& item_text,
  const Glib::ustring& paragraph_text, ShapeFlags flags) const
{
  return GlyphString(item_text, paragraph_text, get_analysis(), flags);
}

} /* namespace Pango */


namespace Glib
{

Pango::Analysis& wrap(PangoAnalysis* object)
{
  return *reinterpret_cast<Pango::Analysis*>(object);
}

const Pango::Analysis& wrap(const PangoAnalysis* object)
{
  return *reinterpret_cast<const Pango::Analysis*>(object);
}

Pango::Item wrap(PangoItem* object, bool take_copy)
{
  return Pango::Item(object, take_copy);
}

} /* namespace Glib */
