gen-paper-names.c 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/* GTK - The GIMP Toolkit
 * 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
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#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)
	return names[i].offset;
    }

  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,
		  gdouble     *width_mm, 
		  gdouble     *height_mm)
{
  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)
	add_name (standard_names[i].ppd_name);
    }

  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++)
      {
	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;
	      }
	  }
      }
  
  /* calculate offsets for regular entries */
  offset = 0;
  for (i = 0; i < n_names; i++)
    {
      if (names[i].suffix == -1)
	{
	  names[i].offset = offset;
	  offset += names[i].len + 1;
	}
    }
  
  /* calculate offsets for suffixes */
  for (i = 0; i < n_names; i++)
    {
      if (names[i].suffix != -1)
	{
172 173 174 175
	  j = i;
	  do {
	    j = names[j].suffix;
	  } while (names[j].suffix != -1);
176 177 178 179 180 181 182 183 184 185
	  names[i].offset = names[j].offset + names[j].len - names[i].len;
	}
    }
    
  printf ("/* Generated by gen-paper-names */\n\n");

  /* write N_ marked names */

  printf ("#if 0\n");
  for (i = 0; i < n_infos; i++)
186 187
    printf ("/* translators, strip everything up to the first | */\n"
	    "N_(\"paper size|%s\")\n", standard_names[i].display_name);
188 189 190
  printf ("#endif\n\n");

  /* write strings */
191
  printf ("static const char paper_names[] =");
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  for (i = 0; i < n_names; i++)
    {
      if (names[i].suffix == -1)
	printf ("\n  \"%s\\0\"", names[i].s);
    }
  printf (";\n\n");

  /* dump PaperInfo array */
  printf ("typedef struct {\n"
	  "  int name;\n"
	  "  float width;\n"
          "  float height;\n"
	  "  int display_name;\n"
	  "  int ppd_name;\n"
	  "} PaperInfo;\n\n"
207
          "static const PaperInfo standard_names_offsets[] = {\n");
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

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

      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));
    }
  
  printf ("};\n\n");


  /* dump extras */

227
  printf ("static const struct {\n"
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	  "  int ppd_name;\n"
	  "  int standard_name;\n"
	  "} extra_ppd_names_offsets[] = {\n");

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

  return 0;
}