summaryrefslogtreecommitdiff
path: root/tfm/fontdimen.c
blob: 779af16c24d1d4ad104c0560f8f6859aa5e37dfe (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
# fontdimen.c: handle TFM fontdimens a.k.a. font parameters.
#
# Copyright (C) 1992, 2011 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
*/


#include "config.h"

#include "tfm.h"


/* This structure maps font parameter names to numbers.  */
typedef struct
{
  string name;
  unsigned number;
} font_param_type;

/* The list of fontdimen names and their corresponding numbers.  I wish
   I could figure out a good way to give this list only once (in tfm.h),
   instead of three times.  */

static font_param_type font_param[] = {
  { "slant",			TFM_SLANT_PARAMETER },
  { "space",			TFM_SPACE_PARAMETER },
  { "stretch",			TFM_STRETCH_PARAMETER },
  { "shrink",			TFM_SHRINK_PARAMETER },
  { "xheight",			TFM_XHEIGHT_PARAMETER },
  { "quad",			TFM_QUAD_PARAMETER },
  { "extraspace",		TFM_EXTRASPACE_PARAMETER },
  { "num1",			TFM_NUM1_PARAMETER },
  { "num2",			TFM_NUM2_PARAMETER },
  { "num3",			TFM_NUM3_PARAMETER },
  { "denom1",			TFM_DENOM1_PARAMETER },
  { "denom2",			TFM_DENOM2_PARAMETER },
  { "sup1",			TFM_SUP1_PARAMETER },
  { "sup2",			TFM_SUP2_PARAMETER },
  { "sup3",			TFM_SUP3_PARAMETER },
  { "sub1",			TFM_SUB1_PARAMETER },
  { "sub2",			TFM_SUB2_PARAMETER },
  { "supdrop",			TFM_SUPDROP_PARAMETER },
  { "subdrop",			TFM_SUBDROP_PARAMETER },
  { "delim1",			TFM_DELIM1_PARAMETER },
  { "delim2",			TFM_DELIM2_PARAMETER },
  { "axisheight",		TFM_AXISHEIGHT_PARAMETER },
  { "defaultrulethickness",	TFM_DEFAULTRULETHICKNESS_PARAMETER },
  { "bigopspacing1",		TFM_BIGOPSPACING1_PARAMETER },
  { "bigopspacing2",		TFM_BIGOPSPACING2_PARAMETER },
  { "bigopspacing3",		TFM_BIGOPSPACING3_PARAMETER },
  { "bigopspacing4",		TFM_BIGOPSPACING4_PARAMETER },
  { "bigopspacing5",		TFM_BIGOPSPACING5_PARAMETER },
  { "leadingheight",		TFM_LEADINGHEIGHT_PARAMETER },
  { "leadingdepth",		TFM_LEADINGDEPTH_PARAMETER },
  { "fontsize",			TFM_FONTSIZE_PARAMETER },
  { "version",			TFM_VERSION_PARAMETER },
};

/* Set the font parameter entries in TFM_INFO according to the string S.
   If S is non-null and non-empty, it should look like
   <fontdimen>:<real>,<fontdimen>:<real>,..., where each <fontdimen> is
   either a standard name or a number between 1 and 30 (the 30 comes
   from the default value in PLtoTF, it's not critical).  

   Also, if the `design_size' member of TFM_INFO is nonzero, we set the
   `FONTSIZE' parameter to 1.0 (meaning it's equal to the design size),
   unless the `FONTSIZE' parameter is already set.  */
   
void
tfm_set_fontdimens (string s, tfm_global_info_type *tfm_info)
{
  string spec;
  
  /* If S is empty, we have nothing more to do.  */
  if (s == NULL || *s == 0)
    return;
  
  /* Parse the specification string.  */
  for (spec = strtok (s, ","); spec != NULL; spec = strtok (NULL, ","))
    {
      string fontdimen;
      real value;
      unsigned param_number = 0;
      string value_string = strchr (spec, ':');
      
      if (value_string == NULL || !float_ok (value_string))
        FATAL1 ("Fontdimens look like `<fontdimen>:<real>', not `%s'",
                spec);

      value = atof (value_string + 1);
      fontdimen = substring (spec, 0, value_string - spec - 1);
      
      /* If `fontdimen' is all numerals, we'll take it to be an integer.  */
      if (strspn (fontdimen, "0123456789") == strlen (fontdimen))
        {
          param_number = atou (fontdimen);

          if (param_number == 0 || param_number > TFM_MAX_FONT_PARAMETERS)
            FATAL2 ("Font parameter %u is not between 1 and the maximum (%u)",
                    param_number, TFM_MAX_FONT_PARAMETERS);
        }
      else
        { /* It wasn't a number; see if it's a valid name.  */
	  param_number = tfm_fontdimen_number (fontdimen);

	  if (param_number == 0)
            FATAL1 ("I don't know the font parameter name `%s'", fontdimen);
        }
      
      /* If we got here, `param_number' is the number of the font
         parameter we are supposed to assign to.  */
      tfm_set_font_parameter (param_number, value, tfm_info);
      
      /* If `param_number' is past the last fontdimen previously
         assigned, we have to assign zero to all the intervening ones.
         But this can never happen, because we always assign the
         `fontsize' parameter, which happens to be the last one.  */
      assert (param_number <= TFM_FONT_PARAMETER_COUNT (*tfm_info));
    }
}

/* Return zero if we do not recognize S as the name of a fontdimen, else
   its corresponding number.  We just do a linear search through the
   structure, since it's so small.  */

unsigned
tfm_fontdimen_number (string s)
{
  unsigned this_param;
  unsigned param_number = 0;

  for (this_param = 0;
       this_param < TFM_MAX_FONT_PARAMETERS && param_number == 0;
       this_param++)
    if (STREQ (s, font_param[this_param].name))
      param_number = font_param[this_param].number;

  return param_number;
}

/* Set the PARAMETER-th font parameter of TFM_INFO to VALUE.  If
   PARAMETER is beyond the current last parameter of TFM_INFO, set
   all the intervening parameters to zero.  */

void
tfm_set_fontdimen (tfm_global_info_type *tfm_info,
                   unsigned parameter, real value, 
{
  if (TFM_FONT_PARAMETER_COUNT (*tfm_info) < parameter)
    {
      unsigned p;
      
      for (p = TFM_FONT_PARAMETER_COUNT (*tfm_info) + 1; p < parameter; p++)
        TFM_FONT_PARAMETER (*tfm_info, p) = 0.0;
      
      /* Now we have more parameters.  */
      TFM_FONT_PARAMETER_COUNT (*tfm_info) = parameter;
    }

  TFM_FONT_PARAMETER (*tfm_info, parameter) = value;
}


/* Set the `FONTSIZE' parameter of TFM_INFO, if the design size is set.  */

void
tfm_set_fontsize (tfm_global_info_type *tfm_info)
{
  if (TFM_DESIGN_SIZE (*tfm_info) != 0.0)
    tfm_set_font_parameter (TFM_FONTSIZE_PARAMETER, 
			    TFM_DESIGN_SIZE (*tfm_info), tfm_info);
}