// Tools :: Typesetting

/* stylelint-disable */

// Generates a rem font-size (with pixel fallback) and a baseline-compatible
// unitless line-height from a pixel font-size value. Basic usage is simply:
//
//   @include fabric-font-size(18px);
//
// You can force a specific line-height by passing it as the second argument:
//
//   @include fabric-font-size(16px, 1);
//
// You can also modify the line-height by increments, while staying in the
// baseline grid, by setting the `$modifier` parameter. It takes a positive
// or negative integer, and it will add or remove "lines" to the  generated
// line-height. This is the recomended way to do it, unless you really need
// an absolute value. i.e.:
//
//   // add 2 lines:
//   @include fabric-font-size(24px, $modifier: +2);
//
//   // subtract 1 line:
//   @include fabric-font-size(24px, $modifier: -1);

@function rem($target, $context: $fabric-rhythm-unit) {
  @if $target == 0 {
    @return 0;
  }
  @return $target / $context + 0rem;
}

@mixin fabric-font-size(
  $font-size,
  $line-height: auto,
  $modifier: 0,
  $important: false
) {
  @if (type-of($font-size) == number) {
    @if (unit($font-size) != 'px') {
      @error '`#{$font-size}` needs to be a pixel value.';
    }
  } @else {
    @error '`#{$font-size}` needs to be a number.';
  }

  @if ($important == true) {
    $important: !important;
  } @else if ($important == false) {
    $important: null;
  } @else {
    @error '`#{$important}` needs to be `true` or `false`.';
  }

  // We provide a `px` fallback for old IEs not supporting `rem` values.
  font-size: $font-size $important;
  font-size: ($font-size / $fabric-global-font-size) * 1rem $important;

  @if ($line-height == 'auto') {
    // Define how many grid lines each text line should span.
    // By default, we set it to the minimum number of lines necessary
    // in order to contain the defined font-size, +1 for some breathing room.
    // This can be modified with the `$modifier` parameter.
    $lines: ceil($font-size / $fabric-global-baseline) + $modifier + 1;
    // $line-height: 24px;
    // $rhythm-unit: $line-height;

    // line-height: ($line-height / $font-size) $important;

    line-height: ceil($font-size / $fabric-rhythm-unit) *
      ($fabric-rhythm-unit / $font-size);
  } @else {
    @if (
      type-of($line-height) ==
        number or
        $line-height ==
        'inherit' or
        $line-height ==
        'normal'
    ) {
      line-height: $line-height $important;
    } @else if ($line-height != 'none' and $line-height != false) {
      @error 'D’oh! `#{$line-height}` is not a valid value for `$line-height`.';
    }
  }
}

//convert kerning to letter spacing
@function fabric-letter-spacing($value) {
  @return $value / 1000 + 0em;
}
