~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to src/Resource/Fonts/find_font.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
* MODULE     : find_font.cpp
 
4
* DESCRIPTION: decoding font names
 
5
* COPYRIGHT  : (C) 1999  Joris van der Hoeven
 
6
*******************************************************************************
 
7
* This software falls under the GNU general public license and comes WITHOUT
 
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
 
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
 
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
11
******************************************************************************/
 
12
 
 
13
#include "analyze.hpp"
 
14
#include "tree.hpp"
 
15
#include "font.hpp"
 
16
#include "hashmap.hpp"
 
17
 
 
18
hashmap<string,tree> font_conversion ("rule");
 
19
 
 
20
/******************************************************************************
 
21
* Declare a new rule
 
22
******************************************************************************/
 
23
 
 
24
void
 
25
font_rule (tree which, tree by) {
 
26
  if ((arity (which) * arity (by) == 0) || is_compound (which[0])) return;
 
27
  if (!font_conversion->contains (which[0]->label))
 
28
    font_conversion (which[0]->label)=
 
29
      tree (TUPLE, tree (ASSOCIATE, which, by));
 
30
  else font_conversion (which[0]->label) << tree (ASSOCIATE, which, by);
 
31
}
 
32
 
 
33
/******************************************************************************
 
34
* Find a font
 
35
******************************************************************************/
 
36
 
 
37
static bool
 
38
matches (tree t, tree which, hashmap<string,tree>& H) {
 
39
  int i, n= arity (which);
 
40
  if (arity (t) != n) return false;
 
41
  for (i=0; i<n; i++) {
 
42
    if (which[i]->label[0]=='$') H (which[i]->label)= t[i];
 
43
    else if (t[i]!=which[i]) return false;
 
44
  }
 
45
  return true;
 
46
}
 
47
 
 
48
static tree
 
49
substitute (tree by, hashmap<string,tree>& H) {
 
50
  if (is_atomic (by)) return copy (by);
 
51
  else {
 
52
    int i, n= N(by);
 
53
    tree r (by, n);
 
54
    for (i=0; i<n; i++) {
 
55
      if (is_atomic (by[i]) && starts (by[i]->label, "$"))
 
56
        r[i]= H [by[i]->label];
 
57
      else r[i]= substitute (by[i], H);
 
58
    }
 
59
    return r;
 
60
  }
 
61
}
 
62
 
 
63
font
 
64
find_font (display dis, tree t) {
 
65
  // cout << "Find " << t << "\n";
 
66
 
 
67
  if ((arity (t)==0) || is_compound (t[0])) return font ();
 
68
 
 
69
  if (is_tuple (t, "truetype", 3))
 
70
    return tt_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]));
 
71
 
 
72
  if (is_tuple (t, "ps", 3))
 
73
    return ps_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]));
 
74
 
 
75
  if (is_tuple (t, "tex", 3))
 
76
    return tex_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]));
 
77
 
 
78
  if (is_tuple (t, "tex", 4))
 
79
    return tex_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]),
 
80
                     as_int (t[4]));
 
81
 
 
82
  if (is_tuple (t, "cm", 3))
 
83
    return tex_cm_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]));
 
84
 
 
85
  if (is_tuple (t, "cm", 4))
 
86
    return tex_cm_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]),
 
87
                        as_int (t[4]));
 
88
 
 
89
  if (is_tuple (t, "ec", 3))
 
90
    return tex_ec_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]));
 
91
 
 
92
  if (is_tuple (t, "ec", 4))
 
93
    return tex_ec_font (dis, as_string (t[1]), as_int (t[2]), as_int (t[3]),
 
94
                        as_int (t[4]));
 
95
  
 
96
  if (is_tuple (t, "la", 3))
 
97
    return tex_la_font (dis, as_string (t[1]), as_int (t[2]) * 100,
 
98
                        as_int (t[3]), 1000);
 
99
 
 
100
  if (is_tuple (t, "la", 4))
 
101
    return tex_la_font (dis, as_string (t[1]), as_int (t[2]) * 100,
 
102
                        as_int (t[3]), as_int (t[4]) * 100);
 
103
 
 
104
  if (is_tuple (t, "adobe", 3))
 
105
    return tex_adobe_font (dis, as_string (t[1]), as_int (t[2]),
 
106
                           as_int (t[3]));
 
107
 
 
108
  if (is_tuple (t, "adobe", 4))
 
109
    return tex_adobe_font (dis, as_string (t[1]), as_int (t[2]),
 
110
                           as_int (t[3]), as_int (t[4]));
 
111
 
 
112
  if (is_tuple (t, "tex-rubber", 4))
 
113
    return tex_rubber_font (dis, as_string (t[1]), as_string (t[2]),
 
114
                            as_int (t[3]), as_int (t[4]));
 
115
 
 
116
  if (is_tuple (t, "tex-rubber", 5))
 
117
    return tex_rubber_font (dis, as_string (t[1]), as_string (t[2]),
 
118
                            as_int (t[3]), as_int (t[4]), as_int (t[5]));
 
119
 
 
120
  if (is_tuple (t, "tex-dummy-rubber", 1)) {
 
121
    font fn= find_font (dis, t[1]);
 
122
    if (nil (fn)) return fn;
 
123
    return tex_dummy_rubber_font (fn);
 
124
  }
 
125
  
 
126
  if (is_tuple (t, "error", 1)) {
 
127
    font fn= find_font (dis, t[1]);
 
128
    if (nil (fn)) return fn;
 
129
    return error_font (fn);
 
130
  }
 
131
 
 
132
  if (is_tuple (t, "numeric", 1)) {
 
133
    font fn= find_font (dis, t[1]);
 
134
    if (nil (fn)) return fn;
 
135
    return subfont (fn, num_enc ());
 
136
  }
 
137
 
 
138
  if (is_tuple (t, "capital", 1)) {
 
139
    font fn= find_font (dis, t[1]);
 
140
    if (nil (fn)) return fn;
 
141
    return subfont (fn, capital_enc ());
 
142
  }
 
143
 
 
144
  if (is_tuple (t, "alpha", 1)) {
 
145
    font fn= find_font (dis, t[1]);
 
146
    if (nil (fn)) return fn;
 
147
    return subfont (fn, alpha_enc ());
 
148
  }
 
149
 
 
150
  if (is_tuple (t, "alpha-numeric", 1)) {
 
151
    font fn= find_font (dis, t[1]);
 
152
    if (nil (fn)) return fn;
 
153
    return subfont (fn, alpha_num_enc ());
 
154
  }
 
155
 
 
156
  if (is_tuple (t, "join", 2)) {
 
157
    font fn1= find_font (dis, t[1]);
 
158
    font fn2= find_font (dis, t[2]);
 
159
    if (nil (fn1) || nil (fn2)) return fn1;
 
160
    return join (fn1, fn2);
 
161
  }
 
162
 
 
163
  if (is_tuple (t, "math", 4) && is_tuple (t[1]) && is_tuple (t[2])) {
 
164
    font fn= find_font (dis, t[3]);
 
165
    if (nil (fn)) return fn;
 
166
    font error_fn= error_font (find_font (dis, t[4]));
 
167
    if (nil (error_fn)) error_fn= error_font (fn);
 
168
    return math_font (t, fn, error_fn);
 
169
  }
 
170
 
 
171
  if (!font_conversion->contains (t[0]->label)) return font ();
 
172
 
 
173
  tree rule= font_conversion [t[0]->label];
 
174
  int i, n= N(rule);
 
175
  for (i=0; i<n; i++) {
 
176
    hashmap<string,tree> H ("?");
 
177
    if (matches (t, rule[i][0], H))
 
178
      return find_font (dis, substitute (rule[i][1], H));
 
179
  }
 
180
 
 
181
  return font ();
 
182
}
 
183
 
 
184
/******************************************************************************
 
185
* User interface
 
186
******************************************************************************/
 
187
 
 
188
font
 
189
find_font (display dis, string family, string fn_class,
 
190
           string series, string shape, int sz, int dpi)
 
191
{
 
192
  string s=
 
193
    family * "-" * fn_class * "-" *
 
194
    series * "-" * shape * "-" *
 
195
    as_string (sz) * "-" * as_string (dpi);
 
196
  if (font::instances->contains (s)) return font (s);
 
197
 
 
198
  tree t1 (TUPLE, 6);
 
199
  t1[0]= family;
 
200
  t1[1]= fn_class;
 
201
  t1[2]= series; t1[3]= shape;
 
202
  t1[4]= as_string (sz); t1[5]= as_string (dpi);
 
203
  font fn= find_font (dis, t1);
 
204
  if (!nil (fn)) {
 
205
    font::instances (s)= (pointer) fn.rep;
 
206
    return fn;
 
207
  }
 
208
 
 
209
  tree t2 (TUPLE, 5);
 
210
  t2[0]= family;
 
211
  t2[1]= fn_class; t2[2]= series;
 
212
  t2[3]= as_string (sz); t2[4]= as_string (dpi);
 
213
  fn= find_font (dis, t2);
 
214
  if (!nil (fn)) {
 
215
    font::instances (s)= (pointer) fn.rep;
 
216
    return fn;
 
217
  }
 
218
 
 
219
  tree t3 (TUPLE, 4);
 
220
  t3[0]= family;
 
221
  t3[1]= fn_class; t3[2]= as_string (sz); t3[3]= as_string (dpi);
 
222
  fn= find_font (dis, t3);
 
223
  if (!nil (fn)) {
 
224
    font::instances (s)= (pointer) fn.rep;
 
225
    return fn;
 
226
  }
 
227
 
 
228
  tree panic (TUPLE, "tex", "cmr", as_string (sz), as_string (dpi));
 
229
  fn= find_font (dis, panic);
 
230
  font::instances (s)= (pointer) fn.rep;
 
231
  return fn;
 
232
}