/* LIBPIKA - The PIKA Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * pikaunit.c
 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
 *
 * 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 3 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 "config.h"

#include <math.h>
#include <string.h>

#include <glib-object.h>

#include "pikabasetypes.h"

#include "pikabase-private.h"
#include "pikaunit.h"


/**
 * SECTION: pikaunit
 * @title: pikaunit
 * @short_description: Provides a collection of predefined units and
 *                     functions for creating user-defined units.
 * @see_also: #PikaUnitMenu, #PikaSizeEntry.
 *
 * Provides a collection of predefined units and functions for
 * creating user-defined units.
 **/


static void   unit_to_string (const GValue *src_value,
                              GValue       *dest_value);
static void   string_to_unit (const GValue *src_value,
                              GValue       *dest_value);

GType
pika_unit_get_type (void)
{
  static GType unit_type = 0;

  if (! unit_type)
    {
      const GTypeInfo type_info = { 0, };

      unit_type = g_type_register_static (G_TYPE_INT, "PikaUnit",
                                          &type_info, 0);

      g_value_register_transform_func (unit_type, G_TYPE_STRING,
                                       unit_to_string);
      g_value_register_transform_func (G_TYPE_STRING, unit_type,
                                       string_to_unit);
    }

  return unit_type;
}

static void
unit_to_string (const GValue *src_value,
                GValue       *dest_value)
{
  PikaUnit unit = (PikaUnit) g_value_get_int (src_value);

  g_value_set_string (dest_value, pika_unit_get_identifier (unit));
}

static void
string_to_unit (const GValue *src_value,
                GValue       *dest_value)
{
  const gchar *str;
  gint         num_units;
  gint         i;

  str = g_value_get_string (src_value);

  if (!str || !*str)
    goto error;

  num_units = pika_unit_get_number_of_units ();

  for (i = PIKA_UNIT_PIXEL; i < num_units; i++)
    if (strcmp (str, pika_unit_get_identifier (i)) == 0)
      break;

  if (i == num_units)
    {
      if (strcmp (str, pika_unit_get_identifier (PIKA_UNIT_PERCENT)) == 0)
        i = PIKA_UNIT_PERCENT;
      else
        goto error;
    }

  g_value_set_int (dest_value, i);
  return;

 error:
  g_warning ("Can't convert string '%s' to PikaUnit.", str);
}


/**
 * pika_unit_get_number_of_units:
 *
 * Returns the number of units which are known to the #PikaUnit system.
 *
 * Returns: The number of defined units.
 **/
gint
pika_unit_get_number_of_units (void)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_number_of_units != NULL,
                        PIKA_UNIT_END);

  return _pika_unit_vtable.unit_get_number_of_units ();
}

/**
 * pika_unit_get_number_of_built_in_units:
 *
 * Returns the number of #PikaUnit's which are hardcoded in the unit system
 * (UNIT_INCH, UNIT_MM, UNIT_POINT, UNIT_PICA and the two "pseudo unit"
 *  UNIT_PIXEL).
 *
 * Returns: The number of built-in units.
 **/
gint
pika_unit_get_number_of_built_in_units (void)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_number_of_built_in_units
                        != NULL, PIKA_UNIT_END);

  return _pika_unit_vtable.unit_get_number_of_built_in_units ();
}

/**
 * pika_unit_new:
 * @identifier: The unit's identifier string.
 * @factor: The unit's factor (how many units are in one inch).
 * @digits: The unit's suggested number of digits (see pika_unit_get_digits()).
 * @symbol: The symbol of the unit (e.g. "''" for inch).
 * @abbreviation: The abbreviation of the unit.
 * @singular: The singular form of the unit.
 * @plural: The plural form of the unit.
 *
 * Returns the integer ID of the new #PikaUnit.
 *
 * Note that a new unit is always created with its deletion flag
 * set to %TRUE. You will have to set it to %FALSE with
 * pika_unit_set_deletion_flag() to make the unit definition persistent.
 *
 * Returns: The ID of the new unit.
 **/
PikaUnit
pika_unit_new (gchar   *identifier,
               gdouble  factor,
               gint     digits,
               gchar   *symbol,
               gchar   *abbreviation,
               gchar   *singular,
               gchar   *plural)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_new != NULL, PIKA_UNIT_INCH);

  return _pika_unit_vtable.unit_new (identifier, factor, digits,
                                     symbol, abbreviation, singular, plural);
}

/**
 * pika_unit_get_deletion_flag:
 * @unit: The unit you want to know the @deletion_flag of.
 *
 * Returns: The unit's @deletion_flag.
 **/
gboolean
pika_unit_get_deletion_flag (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_deletion_flag != NULL, FALSE);

  return _pika_unit_vtable.unit_get_deletion_flag (unit);
}

/**
 * pika_unit_set_deletion_flag:
 * @unit: The unit you want to set the @deletion_flag for.
 * @deletion_flag: The new deletion_flag.
 *
 * Sets a #PikaUnit's @deletion_flag. If the @deletion_flag of a unit is
 * %TRUE when PIKA exits, this unit will not be saved in the users's
 * "unitrc" file.
 *
 * Trying to change the @deletion_flag of a built-in unit will be silently
 * ignored.
 **/
void
pika_unit_set_deletion_flag (PikaUnit unit,
                             gboolean deletion_flag)
{
  g_return_if_fail (_pika_unit_vtable.unit_set_deletion_flag != NULL);

  _pika_unit_vtable.unit_set_deletion_flag (unit, deletion_flag);
}

/**
 * pika_unit_get_factor:
 * @unit: The unit you want to know the factor of.
 *
 * A #PikaUnit's @factor is defined to be:
 *
 * distance_in_units == (@factor * distance_in_inches)
 *
 * Returns 0 for @unit == PIKA_UNIT_PIXEL.
 *
 * Returns: The unit's factor.
 **/
gdouble
pika_unit_get_factor (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_factor != NULL, 1.0);

  if (unit == PIKA_UNIT_PIXEL)
    return 0.0;

  return _pika_unit_vtable.unit_get_factor (unit);
}

/**
 * pika_unit_get_digits:
 * @unit: The unit you want to know the digits.
 *
 * Returns the number of digits set for @unit.
 * Built-in units' accuracy is approximately the same as an inch with
 * two digits. User-defined units can suggest a different accuracy.
 *
 * Note: the value is as-set by defaults or by the user and does not
 * necessary provide enough precision on high-resolution images.
 * When the information is needed for a specific image, the use of
 * pika_unit_get_scaled_digits() may be more appropriate.
 *
 * Returns 0 for @unit == PIKA_UNIT_PIXEL.
 *
 * Returns: The suggested number of digits.
 **/
gint
pika_unit_get_digits (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_digits != NULL, 2);

  return _pika_unit_vtable.unit_get_digits (unit);
}

/**
 * pika_unit_get_scaled_digits:
 * @unit: The unit you want to know the digits.
 * @resolution: the resolution in PPI.
 *
 * Returns the number of digits a @unit field should provide to get
 * enough accuracy so that every pixel position shows a different
 * value from neighboring pixels.
 *
 * Note: when needing digit accuracy to display a diagonal distance,
 * the @resolution may not correspond to the image's horizontal or
 * vertical resolution, but instead to the result of:
 * `distance_in_pixel / distance_in_inch`.
 *
 * Returns: The suggested number of digits.
 **/
gint
pika_unit_get_scaled_digits (PikaUnit unit,
                             gdouble  resolution)
{
  gint digits;

  g_return_val_if_fail (_pika_unit_vtable.unit_get_digits != NULL, 2);

  digits = ceil (log10 (1.0 /
                        pika_pixels_to_units (1.0, unit, resolution)));

  return MAX (digits, pika_unit_get_digits (unit));
}

/**
 * pika_unit_get_identifier:
 * @unit: The unit you want to know the identifier of.
 *
 * This is an untranslated string and must not be changed or freed.
 *
 * Returns: The unit's identifier.
 **/
const gchar *
pika_unit_get_identifier (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_identifier != NULL, NULL);

  return _pika_unit_vtable.unit_get_identifier (unit);
}

/**
 * pika_unit_get_symbol:
 * @unit: The unit you want to know the symbol of.
 *
 * This is e.g. "''" for UNIT_INCH.
 *
 * NOTE: This string must not be changed or freed.
 *
 * Returns: The unit's symbol.
 **/
const gchar *
pika_unit_get_symbol (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_symbol != NULL, NULL);

  return _pika_unit_vtable.unit_get_symbol (unit);
}

/**
 * pika_unit_get_abbreviation:
 * @unit: The unit you want to know the abbreviation of.
 *
 * For built-in units, this function returns the translated abbreviation
 * of the unit.
 *
 * NOTE: This string must not be changed or freed.
 *
 * Returns: The unit's abbreviation.
 **/
const gchar *
pika_unit_get_abbreviation (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_abbreviation != NULL, NULL);

  return _pika_unit_vtable.unit_get_abbreviation (unit);
}

/**
 * pika_unit_get_singular:
 * @unit: The unit you want to know the singular form of.
 *
 * For built-in units, this function returns the translated singular form
 * of the unit's name.
 *
 * NOTE: This string must not be changed or freed.
 *
 * Returns: The unit's singular form.
 **/
const gchar *
pika_unit_get_singular (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_singular != NULL, NULL);

  return _pika_unit_vtable.unit_get_singular (unit);
}

/**
 * pika_unit_get_plural:
 * @unit: The unit you want to know the plural form of.
 *
 * For built-in units, this function returns the translated plural form
 * of the unit's name.
 *
 * NOTE: This string must not be changed or freed.
 *
 * Returns: The unit's plural form.
 **/
const gchar *
pika_unit_get_plural (PikaUnit unit)
{
  g_return_val_if_fail (_pika_unit_vtable.unit_get_plural != NULL, NULL);

  return _pika_unit_vtable.unit_get_plural (unit);
}

static gint print (gchar       *buf,
                   gint         len,
                   gint         start,
                   const gchar *fmt,
                   ...) G_GNUC_PRINTF (4, 5);

static gint
print (gchar       *buf,
       gint         len,
       gint         start,
       const gchar *fmt,
       ...)
{
  va_list args;
  gint printed;

  va_start (args, fmt);

  printed = g_vsnprintf (buf + start, len - start, fmt, args);
  if (printed < 0)
    printed = len - start;

  va_end (args);

  return printed;
}

/**
 * pika_unit_format_string:
 * @format: A printf-like format string which is used to create the unit
 *          string.
 * @unit:   A unit.
 *
 * The @format string supports the following percent expansions:
 *
 * <informaltable pgwide="1" frame="none" role="enum">
 *   <tgroup cols="2"><colspec colwidth="1*"/><colspec colwidth="8*"/>
 *     <tbody>
 *       <row>
 *         <entry>% f</entry>
 *         <entry>Factor (how many units make up an inch)</entry>
 *        </row>
 *       <row>
 *         <entry>% y</entry>
 *         <entry>Symbol (e.g. "''" for PIKA_UNIT_INCH)</entry>
 *       </row>
 *       <row>
 *         <entry>% a</entry>
 *         <entry>Abbreviation</entry>
 *       </row>
 *       <row>
 *         <entry>% s</entry>
 *         <entry>Singular</entry>
 *       </row>
 *       <row>
 *         <entry>% p</entry>
 *         <entry>Plural</entry>
 *       </row>
 *       <row>
 *         <entry>%%</entry>
 *         <entry>Literal percent</entry>
 *       </row>
 *     </tbody>
 *   </tgroup>
 * </informaltable>
 *
 * Returns: A newly allocated string with above percent expressions
 *          replaced with the resp. strings for @unit.
 *
 * Since: 2.8
 **/
gchar *
pika_unit_format_string (const gchar *format,
                         PikaUnit     unit)
{
  gchar buffer[1024];
  gint  i = 0;

  g_return_val_if_fail (format != NULL, NULL);
  g_return_val_if_fail (unit == PIKA_UNIT_PERCENT ||
                        (unit >= PIKA_UNIT_PIXEL &&
                         unit < pika_unit_get_number_of_units ()), NULL);

  while (i < (sizeof (buffer) - 1) && *format)
    {
      switch (*format)
        {
        case '%':
          format++;
          switch (*format)
            {
            case 0:
              g_warning ("%s: unit-menu-format string ended within %%-sequence",
                         G_STRFUNC);
              break;

            case '%':
              buffer[i++] = '%';
              break;

            case 'f': /* factor (how many units make up an inch) */
              i += print (buffer, sizeof (buffer), i, "%f",
                          pika_unit_get_factor (unit));
              break;

            case 'y': /* symbol ("''" for inch) */
              i += print (buffer, sizeof (buffer), i, "%s",
                          pika_unit_get_symbol (unit));
              break;

            case 'a': /* abbreviation */
              i += print (buffer, sizeof (buffer), i, "%s",
                          pika_unit_get_abbreviation (unit));
              break;

            case 's': /* singular */
              i += print (buffer, sizeof (buffer), i, "%s",
                          pika_unit_get_singular (unit));
              break;

            case 'p': /* plural */
              i += print (buffer, sizeof (buffer), i, "%s",
                          pika_unit_get_plural (unit));
              break;

            default:
              g_warning ("%s: unit-menu-format contains unknown format "
                         "sequence '%%%c'", G_STRFUNC, *format);
              break;
            }
          break;

        default:
          buffer[i++] = *format;
          break;
        }

      format++;
    }

  buffer[MIN (i, sizeof (buffer) - 1)] = 0;

  return g_strdup (buffer);
}

/*
 * PIKA_TYPE_PARAM_UNIT
 */

static void      pika_param_unit_class_init     (GParamSpecClass *class);
static gboolean  pika_param_unit_value_validate (GParamSpec      *pspec,
                                                 GValue          *value);

/**
 * pika_param_unit_get_type:
 *
 * Reveals the object type
 *
 * Returns: the #GType for a unit param object
 *
 * Since: 2.4
 **/
GType
pika_param_unit_get_type (void)
{
  static GType spec_type = 0;

  if (! spec_type)
    {
      const GTypeInfo type_info =
      {
        sizeof (GParamSpecClass),
        NULL, NULL,
        (GClassInitFunc) pika_param_unit_class_init,
        NULL, NULL,
        sizeof (PikaParamSpecUnit),
        0, NULL, NULL
      };

      spec_type = g_type_register_static (G_TYPE_PARAM_INT,
                                          "PikaParamUnit",
                                          &type_info, 0);
    }

  return spec_type;
}

static void
pika_param_unit_class_init (GParamSpecClass *class)
{
  class->value_type     = PIKA_TYPE_UNIT;
  class->value_validate = pika_param_unit_value_validate;
}

static gboolean
pika_param_unit_value_validate (GParamSpec *pspec,
                                GValue     *value)
{
  GParamSpecInt     *ispec = G_PARAM_SPEC_INT (pspec);
  PikaParamSpecUnit *uspec = PIKA_PARAM_SPEC_UNIT (pspec);
  gint               oval  = value->data[0].v_int;

  if (!(uspec->allow_percent && value->data[0].v_int == PIKA_UNIT_PERCENT))
    {
      value->data[0].v_int = CLAMP (value->data[0].v_int,
                                    ispec->minimum,
                                    pika_unit_get_number_of_units () - 1);
    }

  return value->data[0].v_int != oval;
}

/**
 * pika_param_spec_unit:
 * @name:          Canonical name of the param
 * @nick:          Nickname of the param
 * @blurb:         Brief description of param.
 * @allow_pixels:  Whether "pixels" is an allowed unit.
 * @allow_percent: Whether "percent" is an allowed unit.
 * @default_value: Unit to use if none is assigned.
 * @flags:         a combination of #GParamFlags
 *
 * Creates a param spec to hold a units param.
 * See g_param_spec_internal() for more information.
 *
 * Returns: (transfer full): a newly allocated #GParamSpec instance
 *
 * Since: 2.4
 **/
GParamSpec *
pika_param_spec_unit (const gchar *name,
                      const gchar *nick,
                      const gchar *blurb,
                      gboolean     allow_pixels,
                      gboolean     allow_percent,
                      PikaUnit     default_value,
                      GParamFlags  flags)
{
  PikaParamSpecUnit *pspec;
  GParamSpecInt     *ispec;

  pspec = g_param_spec_internal (PIKA_TYPE_PARAM_UNIT,
                                 name, nick, blurb, flags);

  ispec = G_PARAM_SPEC_INT (pspec);

  ispec->default_value = default_value;
  ispec->minimum       = allow_pixels ? PIKA_UNIT_PIXEL : PIKA_UNIT_INCH;
  ispec->maximum       = PIKA_UNIT_PERCENT - 1;

  pspec->allow_percent = allow_percent;

  return G_PARAM_SPEC (pspec);
}

/**
 * pika_pixels_to_units:
 * @pixels:     value in pixels
 * @unit:       unit to convert to
 * @resolution: resolution in DPI
 *
 * Converts a @value specified in pixels to @unit.
 *
 * Returns: @pixels converted to units.
 *
 * Since: 2.8
 **/
gdouble
pika_pixels_to_units (gdouble  pixels,
                      PikaUnit unit,
                      gdouble  resolution)
{
  if (unit == PIKA_UNIT_PIXEL)
    return pixels;

  return pixels * pika_unit_get_factor (unit) / resolution;
}

/**
 * pika_units_to_pixels:
 * @value:      value in units
 * @unit:       unit of @value
 * @resolution: resloution in DPI
 *
 * Converts a @value specified in @unit to pixels.
 *
 * Returns: @value converted to pixels.
 *
 * Since: 2.8
 **/
gdouble
pika_units_to_pixels (gdouble  value,
                      PikaUnit unit,
                      gdouble  resolution)
{
  if (unit == PIKA_UNIT_PIXEL)
    return value;

  return value * resolution / pika_unit_get_factor (unit);
}

/**
 * pika_units_to_points:
 * @value:      value in units
 * @unit:       unit of @value
 * @resolution: resloution in DPI
 *
 * Converts a @value specified in @unit to points.
 *
 * Returns: @value converted to points.
 *
 * Since: 2.8
 **/
gdouble
pika_units_to_points (gdouble  value,
                      PikaUnit unit,
                      gdouble  resolution)
{
  if (unit == PIKA_UNIT_POINT)
    return value;

  if (unit == PIKA_UNIT_PIXEL)
    return (value * pika_unit_get_factor (PIKA_UNIT_POINT) / resolution);

  return (value *
          pika_unit_get_factor (PIKA_UNIT_POINT) / pika_unit_get_factor (unit));
}

/**
 * pika_unit_is_metric:
 * @unit: The unit
 *
 * Checks if the given @unit is metric. A simplistic test is used
 * that looks at the unit's factor and checks if it is 2.54 multiplied
 * by some common powers of 10. Currently it checks for mm, cm, dm, m.
 *
 * See also: pika_unit_get_factor()
 *
 * Returns: %TRUE if the @unit is metric.
 *
 * Since: 2.10
 **/
gboolean
pika_unit_is_metric (PikaUnit unit)
{
  gdouble factor;

  if (unit == PIKA_UNIT_MM)
    return TRUE;

  factor = pika_unit_get_factor (unit);

  if (factor == 0.0)
    return FALSE;

  return ((ABS (factor -  0.0254) < 1e-7) || /* m  */
          (ABS (factor -  0.254)  < 1e-6) || /* dm */
          (ABS (factor -  2.54)   < 1e-5) || /* cm */
          (ABS (factor - 25.4)    < 1e-4));  /* mm */
}
