<commit_before>/* --------------------------------------------------------------------------
 *    Name: match.c
 * Purpose: Font matching
 *  Author: <NAME>
 * Version: $Id: match.c,v 1.2 2010-01-13 18:08:40 dpt Exp $
 * ----------------------------------------------------------------------- */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "oslib/font.h"

#include "appengine/types.h"
#include "appengine/vdu/font.h"
#include "appengine/base/strings.h"

#define DBUG(args)

typedef struct nameattr
{
  char        name[11];
  int         mask;
  font__attrs value;
}
nameattr;

/* Sorted by name for binary searching. */
/* Weights are taken from TTF/OTF OS/2 table usWeightClass. */
static const nameattr map[] =
{
  { "Black",      font__WEIGHT_MASK,  font__WEIGHT_BLACK      },
  { "Bold",       font__WEIGHT_MASK,  font__WEIGHT_BOLD       },
  { "Book",       font__WEIGHT_MASK,  font__WEIGHT_400        },
  { "Comp",       font__STRETCH_MASK, font__STRETCH_COND      },
  { "Cond",       font__STRETCH_MASK, font__STRETCH_COND      },
  { "Demi",       font__WEIGHT_MASK,  font__WEIGHT_600        },
  { "DemiBold",   font__WEIGHT_MASK,  font__WEIGHT_600        },
  { "Expert",     font__VARIANT_MASK, font__VARIANT_EXPERT    },
  { "Extended",   font__STRETCH_MASK, font__STRETCH_EXT       },
  { "Extra",      font__WEIGHT_MASK,  font__WEIGHT_950        },
  { "ExtraBlack", font__WEIGHT_MASK,  font__WEIGHT_950        },
  { "ExtraBold",  font__WEIGHT_MASK,  font__WEIGHT_800        },
  { "ExtraComp",  font__STRETCH_MASK, font__STRETCH_EXTRACOND },
  { "ExtraLight", font__WEIGHT_MASK,  font__WEIGHT_200        },
  { "Heavy",      font__WEIGHT_MASK,  font__WEIGHT_900        },
  { "Italic",     font__STYLE_MASK,   font__STYLE_ITALIC      },
  { "Light",      font__WEIGHT_MASK,  font__WEIGHT_LIGHT      },
  { "Medium",     font__WEIGHT_MASK,  font__WEIGHT_500        },
  { "Normal",     font__WEIGHT_MASK,  font__WEIGHT_NORMAL     },
  { "OSF",        font__VARIANT_MASK, font__VARIANT_OSF       },
  { "Oblique",    font__STYLE_MASK,   font__STYLE_OBLIQUE     },
  { "Open",       font__VARIANT_MASK, font__VARIANT_OPEN      },
  { "Poster",     font__VARIANT_MASK, font__VARIANT_POSTER    },
  { "Regular",    font__WEIGHT_MASK,  font__WEIGHT_400        },
  { "Roman",      font__WEIGHT_MASK,  font__WEIGHT_400        },
  { "SemiBold",   font__WEIGHT_MASK,  font__WEIGHT_600        },
  { "Slanted",    font__STYLE_MASK,   font__STYLE_SLANTED     },
  { "SmallCaps",  font__VARIANT_MASK, font__VARIANT_SMALLCAPS },
  { "Thin",       font__WEIGHT_MASK,  font__WEIGHT_100        },
  { "Title",      font__VARIANT_MASK, font__VARIANT_TITLE     },
  { "Ultra",      font__WEIGHT_MASK,  font__WEIGHT_800        },
  { "UltraBlack", font__WEIGHT_MASK,  font__WEIGHT_950        },
  { "UltraBold",  font__WEIGHT_MASK,  font__WEIGHT_800        },
  { "UltraComp",  font__STRETCH_MASK, font__STRETCH_ULTRACOND },
  { "UltraLight", font__WEIGHT_MASK,  font__WEIGHT_200        },
};

/* Copy 'in' to 'out' zero-terminating all full stops and placing pointers
 * to each field in tokens[]. */
static int tokenise(const char *in, char *out, char *tokens[])
{
  int         i;
  const char *p;
  char       *q;

  /* e.g. Baskerville.SmallCaps.DemiBold.Italic */

  i = 0;
  for (p = in, q = out; *p != '\0'; p++)
  {
    int c = *p;

    if (c == '.')
    {
      *q++ = '\0'; /* terminate each token */
      tokens[i++] = q;
    }
    else
    {
      *q++ = c;
    }
  }

  *q++ = '\0';

  return i; /* return number of found tokens */
}

static int compattrs(const void *a, const void *b)
{
  const nameattr *ma = a;
  const nameattr *mb = b;

  return strcasecmp(ma->name, mb->name);
}

/* Compute the distance between two attrs. A smaller value is better (ie.
 * closer). */
static int score(font__attrs a, font__attrs b)
{
  int score;

  score = abs((a & font__WEIGHT_MASK)  - (b & font__WEIGHT_MASK))  +
          abs((a & font__STYLE_MASK)   - (b & font__STYLE_MASK))   +
          abs((a & font__STRETCH_MASK) - (b & font__STRETCH_MASK)) +
          abs((a & font__VARIANT_MASK) - (b & font__VARIANT_MASK));

  return score;
}

/* Split a font name into family name and attrs.
 *
 * We consider the font name to be the leading part of the string up to the
 * first recognised token. ie. the "Goudy.OldStyle.Bold" family is
 * "Goudy.OldStyle".
 *
 * Foo.Bold.Bar        -> Foo<commit_msg>Reducing shadowed declaration warnings: drop local score var.<commit_after>/* --------------------------------------------------------------------------
 *    Name: match.c
 * Purpose: Font matching
 *  Author: <NAME>
 * Version: $Id: match.c,v 1.2 2010-01-13 18:08:40 dpt Exp $
 * ----------------------------------------------------------------------- */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "oslib/font.h"

#include "appengine/types.h"
#include "appengine/vdu/font.h"
#include "appengine/base/strings.h"

#define DBUG(args)

typedef struct nameattr
{
  char        name[11];
  int         mask;
  font__attrs value;
}
nameattr;

/* Sorted by name for binary searching. */
/* Weights are taken from TTF/OTF OS/2 table usWeightClass. */
static const nameattr map[] =
{
  { "Black",      font__WEIGHT_MASK,  font__WEIGHT_BLACK      },
  { "Bold",       font__WEIGHT_MASK,  font__WEIGHT_BOLD       },
  { "Book",       font__WEIGHT_MASK,  font__WEIGHT_400        },
  { "Comp",       font__STRETCH_MASK, font__STRETCH_COND      },
  { "Cond",       font__STRETCH_MASK, font__STRETCH_COND      },
  { "Demi",       font__WEIGHT_MASK,  font__WEIGHT_600        },
  { "DemiBold",   font__WEIGHT_MASK,  font__WEIGHT_600        },
  { "Expert",     font__VARIANT_MASK, font__VARIANT_EXPERT    },
  { "Extended",   font__STRETCH_MASK, font__STRETCH_EXT       },
  { "Extra",      font__WEIGHT_MASK,  font__WEIGHT_950        },
  { "ExtraBlack", font__WEIGHT_MASK,  font__WEIGHT_950        },
  { "ExtraBold",  font__WEIGHT_MASK,  font__WEIGHT_800        },
  { "ExtraComp",  font__STRETCH_MASK, font__STRETCH_EXTRACOND },
  { "ExtraLight", font__WEIGHT_MASK,  font__WEIGHT_200        },
  { "Heavy",      font__WEIGHT_MASK,  font__WEIGHT_900        },
  { "Italic",     font__STYLE_MASK,   font__STYLE_ITALIC      },
  { "Light",      font__WEIGHT_MASK,  font__WEIGHT_LIGHT      },
  { "Medium",     font__WEIGHT_MASK,  font__WEIGHT_500        },
  { "Normal",     font__WEIGHT_MASK,  font__WEIGHT_NORMAL     },
  { "OSF",        font__VARIANT_MASK, font__VARIANT_OSF       },
  { "Oblique",    font__STYLE_MASK,   font__STYLE_OBLIQUE     },
  { "Open",       font__VARIANT_MASK, font__VARIANT_OPEN      },
  { "Poster",     font__VARIANT_MASK, font__VARIANT_POSTER    },
  { "Regular",    font__WEIGHT_MASK,  font__WEIGHT_400        },
  { "Roman",      font__WEIGHT_MASK,  font__WEIGHT_400        },
  { "SemiBold",   font__WEIGHT_MASK,  font__WEIGHT_600        },
  { "Slanted",    font__STYLE_MASK,   font__STYLE_SLANTED     },
  { "SmallCaps",  font__VARIANT_MASK, font__VARIANT_SMALLCAPS },
  { "Thin",       font__WEIGHT_MASK,  font__WEIGHT_100        },
  { "Title",      font__VARIANT_MASK, font__VARIANT_TITLE     },
  { "Ultra",      font__WEIGHT_MASK,  font__WEIGHT_800        },
  { "UltraBlack", font__WEIGHT_MASK,  font__WEIGHT_950        },
  { "UltraBold",  font__WEIGHT_MASK,  font__WEIGHT_800        },
  { "UltraComp",  font__STRETCH_MASK, font__STRETCH_ULTRACOND },
  { "UltraLight", font__WEIGHT_MASK,  font__WEIGHT_200        },
};

/* Copy 'in' to 'out' zero-terminating all full stops and placing pointers
 * to each field in tokens[]. */
static int tokenise(const char *in, char *out, char *tokens[])
{
  int         i;
  const char *p;
  char       *q;

  /* e.g. Baskerville.SmallCaps.DemiBold.Italic */

  i = 0;
  for (p = in, q = out; *p != '\0'; p++)
  {
    int c = *p;

    if (c == '.')
    {
      *q++ = '\0'; /* terminate each token */
      tokens[i++] = q;
    }
    else
    {
      *q++ = c;
    }
  }

  *q++ = '\0';

  return i; /* return number of found tokens */
}

static int compattrs(const void *a, const void *b)
{
  const nameattr *ma = a;
  const nameattr *mb = b;

  return strcasecmp(ma->name, mb->name);
}

/* Compute the distance between two attrs. A smaller value is better (ie.
 * closer). */
static int score(font__attrs a, font__attrs b)
{
  return abs((a & font__WEIGHT_MASK)  - (b & font__WEIGHT_MASK))  +
         abs((a & font__STYLE_MASK)   - (b & font__STYLE_MASK))   +
         abs((a & font__STRETCH_MASK) - (b & font__STRETCH_MASK)) +
         abs((a & font__VARIANT_MASK) - (b & font__VARIANT_MASK));
}

/* Split a font name into family name and attrs.
 *
 * We consider the font name to be the leading part of the string up to the
 * first recognised token. ie. the "Goudy.OldStyle.Bold" family is
 * "Goudy.OldStyle".
 *
 * Foo.Bold.Bar        -> Foo
{'fraction_non_alphanumeric': 0.15566513504426763, 'fraction_numerical': 0.016586349882326573, 'mean_word_length': 2.454897406116918, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7225478', 'n_tokens_mistral': 3395, 'n_tokens_neox': 3233, 'n_words': 662}