gen-paper-names.c 5.41 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10
 * Copyright (C) 2006 Matthias Clasen
 *
 * 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 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 13 14
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 */

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

#include <glib.h>

#include "paper_names.c"

static const gint n_infos = G_N_ELEMENTS (standard_names);
static const gint n_extra = G_N_ELEMENTS (extra_ppd_names);

typedef struct {
  const gchar *s;
  gint         len;
  gint         suffix;
  gint         offset;
} NameInfo;

static NameInfo *names = NULL;
static gint n_names = 0;

static void
add_name (const gchar *name)
{
  names[n_names].s = name;
  names[n_names].len = strlen (name);
  names[n_names].suffix = -1;
  names[n_names].offset = 0;

  n_names++;
}

static gint
find_name (const gchar *name)
{
  gint i;

  if (!name)
    return -1;

  for (i = 0; i < n_names; i++)
    {
      if (strcmp (names[i].s, name) == 0)
61
        return names[i].offset;
62 63 64 65 66 67 68 69 70 71 72 73
    }

  fprintf (stderr, "BOO! %s not found\n", name);

  return -2;
}

#define MM_PER_INCH 25.4
#define POINTS_PER_INCH 72

static gboolean
parse_media_size (const gchar *size,
74 75
                  gdouble     *width_mm, 
                  gdouble     *height_mm)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
  const gchar *p;
  gchar *e;
  gdouble short_dim, long_dim;

  p = size;
  
  short_dim = g_ascii_strtod (p, &e);

  if (p == e || *e != 'x')
    return FALSE;

  p = e + 1; /* Skip x */

  long_dim = g_ascii_strtod (p, &e);

  if (p == e)
    return TRUE;

  p = e;

  if (strcmp (p, "in") == 0)
    {
      short_dim = short_dim * MM_PER_INCH;
      long_dim = long_dim * MM_PER_INCH;
    }
  else if (strcmp (p, "mm") != 0)
    return FALSE;

  if (width_mm)
    *width_mm = short_dim;
  if (height_mm)
    *height_mm = long_dim;
  
  return TRUE;  
}

int 
main (int argc, char *argv[])
{
  gint i, j, offset;
  gdouble width, height;

  names = (NameInfo *) malloc (sizeof (NameInfo) * (4 + n_infos + 2 * n_extra));
  n_names = 0;

  /* collect names */
  for (i = 0; i < n_infos; i++)
    {
      add_name (standard_names[i].name);
      add_name (standard_names[i].display_name);
      if (standard_names[i].ppd_name)
128
        add_name (standard_names[i].ppd_name);
129 130 131 132 133 134 135 136 137 138 139 140
    }

  for (i = 0; i < n_extra; i++)
    {
      add_name (extra_ppd_names[i].ppd_name);
      add_name (extra_ppd_names[i].standard_name);
    }

  /* find suffixes and dupes */
  for (i = 0; i < n_names; i++)
    for (j = 0; j < n_names; j++)
      {
141 142 143 144 145 146 147 148 149 150 151
        if (i == j) continue;

        if (names[i].len < names[j].len ||
            (names[i].len == names[j].len && j < i))
          {
            if (strcmp (names[i].s, names[j].s + names[j].len - names[i].len) == 0)
              {
                names[i].suffix = j;
                break;
              }
          }
152
      }
153

154 155 156 157 158
  /* calculate offsets for regular entries */
  offset = 0;
  for (i = 0; i < n_names; i++)
    {
      if (names[i].suffix == -1)
159 160 161 162
        {
          names[i].offset = offset;
          offset += names[i].len + 1;
        }
163
    }
164

165 166 167 168
  /* calculate offsets for suffixes */
  for (i = 0; i < n_names; i++)
    {
      if (names[i].suffix != -1)
169 170 171 172 173 174 175
        {
          j = i;
          do {
            j = names[j].suffix;
          } while (names[j].suffix != -1);
          names[i].offset = names[j].offset + names[j].len - names[i].len;
        }
176
    }
177

178 179 180 181 182 183
  printf ("/* Generated by gen-paper-names */\n\n");

  /* write N_ marked names */

  printf ("#if 0\n");
  for (i = 0; i < n_infos; i++)
184
    printf ("NC_(\"paper size\", \"%s\")\n", standard_names[i].display_name);
185 186 187
  printf ("#endif\n\n");

  /* write strings */
188
  printf ("static const char paper_names[] =");
189 190 191
  for (i = 0; i < n_names; i++)
    {
      if (names[i].suffix == -1)
192
        printf ("\n  \"%s\\0\"", names[i].s);
193 194 195 196 197
    }
  printf (";\n\n");

  /* dump PaperInfo array */
  printf ("typedef struct {\n"
198 199
          "  int name;\n"
          "  float width;\n"
200
          "  float height;\n"
201 202 203
          "  int display_name;\n"
          "  int ppd_name;\n"
          "} PaperInfo;\n\n"
204
          "static const PaperInfo standard_names_offsets[] = {\n");
205 206 207 208 209

  for (i = 0; i < n_infos; i++)
    {
      width = height = 0.0;
      if (!parse_media_size (standard_names[i].size, &width, &height))
210
        printf ("failed to parse size %s\n", standard_names[i].size);
211

212 213 214 215 216
      printf ("  { %4d, %g, %g, %4d, %4d },\n",
              find_name (standard_names[i].name),
              width, height,
              find_name (standard_names[i].display_name),
              find_name (standard_names[i].ppd_name));
217
    }
218

219 220 221 222 223
  printf ("};\n\n");


  /* dump extras */

224
  printf ("static const struct {\n"
225 226 227
          "  int ppd_name;\n"
          "  int standard_name;\n"
          "} extra_ppd_names_offsets[] = {\n");
228 229 230

  for (i = 0; i < n_extra; i++)
    {
231 232 233
      printf ("  { %4d, %4d },\n",
              find_name (extra_ppd_names[i].ppd_name),
              find_name (extra_ppd_names[i].standard_name));
234
    }
235

236 237 238 239
  printf ("};\n\n");

  return 0;
}