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
  
// This file is part of Roxen WebServer. 
// Copyright © 1996 - 2009, Roxen IS. 
 
#include <config.h> 
constant cvs_version = "$Id: old.pike,v 1.15 2009/05/07 14:15:53 mast Exp $"; 
 
constant name = "Compatibility bitmap fonts"; 
constant doc =  
"Compatibility (bitmapped) fonts for Roxen 1.3 and earlier." 
" Compact image file fonts are preferable to this format, since they are " 
"almost always smaller, and easier to create."; 
 
inherit FontHandler; 
 
Thread.Mutex lock = Thread.Mutex(); 
 
protected mapping font_cache; 
 
protected void build_font_cache() 
{ 
  mapping res = ([ ]); 
  foreach(roxen->query("font_dirs"), string dir) 
  { 
    dir+="32/"; 
    if(array d = r_get_dir(dir)) 
    { 
      foreach(d,string f) 
      { 
        if(f=="CVS") continue; 
        Stat a; 
        if((a=r_file_stat(dir+f)) && (a[1]==-2))  
        { 
          array d=r_get_dir(dir+f); 
          foreach( ({ "nn", "ni", "li", "ln", "Bi", "Bn", "bi", "bn" }), 
                   string style) 
            if(has_value(d, style))  
            { 
              res["32/" + f + "/" + style] = replace(f, "_", " "); 
            } 
        } 
      } 
    } 
  } 
  font_cache = res; 
} 
 
array available_fonts(int(0..1)|void force_reload) 
{ 
  Thread.MutexKey key = lock->lock(); 
  if (!font_cache || force_reload) 
    build_font_cache(); 
  return Array.uniq(values(font_cache)); 
} 
 
array(mapping) font_information( string fnt ) 
{ 
  string ofnt = fnt; 
  fnt = replace(lower_case( fnt ), " ", "_"); 
  array font_infos=({}); 
  foreach(roxen->query("font_dirs"), string dir) 
  { 
    dir+="32/"; 
    if( r_file_stat( dir+fnt ) ) 
    // the font file exists in this dir.. 
    { 
      array d = r_get_dir(dir+fnt); 
      foreach( ({ "nn", "ni", "li", "ln", "Bi", "Bn", "bi", "bn" }), 
               string style) 
        if(has_value(d, style))  
        { 
          mapping font_info = ([ "name":fnt, 
                                 "family":ofnt, 
                                 "path":dir+fnt, 
                                 "style":"", 
                                 "format":"bitmap dump" ]); 
          switch(style[0]) { 
          case 'l': font_info->style+="light"; break; 
          case 'b': font_info->style+="bold"; break; 
          case 'B': font_info->style+="black"; break; 
          } 
          if(style[1]=='i') font_info->style+="italic"; 
          if(style[1]=='I') font_info->style+="oblique"; 
          font_infos+=({ font_info }); 
        } 
    } 
  } 
  return font_infos; 
} 
 
string fix_name( string what ) 
{ 
  return replace( lower_case(what), " ", "_" ); 
} 
 
array has_font( string name, int size ) 
{ 
  string base_dir; 
  array available; 
  if( String.width( name ) > 8 ) 
    return 0; 
 
  Thread.MutexKey key = lock->lock(); 
  if (!font_cache) 
    build_font_cache(); 
 
  string match_prefix = size + "/" + fix_name(name) + "/"; 
  array matches = filter(indices(font_cache), has_prefix, match_prefix); 
  if (sizeof(matches)) 
    return map(matches, `[], sizeof(match_prefix), 99999); 
   
  if (size != 32) { 
    foreach(roxen->query("font_dirs"), string dir) { 
      string key = size + "/" + fix_name(name); 
      base_dir = dir + key; 
      if (available = r_get_dir(base_dir)) { 
        foreach(available - ({ "CVS" }), string style) 
          font_cache[key + "/" + style] = name; 
        return available; 
      } 
    } 
    key = 0; 
    return has_font(name, 32); 
  } 
  return 0; 
} 
 
class MyFont { 
  inherit Image.Font; 
 
  void set_x_spacing(int|float delta) { 
    if(intp(delta)) 
      ::set_x_spacing( (100.0+delta)/100.0 ); 
    else 
      ::set_x_spacing( delta ); 
  } 
 
  void set_y_spacing(int|float delta) { 
    if(intp(delta)) 
      ::set_y_spacing( (100.0+delta)/100.0 ); 
    else 
      ::set_y_spacing( (float)delta ); 
  } 
 
  string _sprintf() { 
    return sprintf( "OldFont" ); 
  } 
 
} 
 
Font open( string name, int size, int bold, int italic ) 
{ 
  if( String.width( name ) > 8 ) 
    return 0; 
  string f = make_font_name( name, size, bold, italic ); 
  Image.Font fn = MyFont(); 
  foreach( roxen->query( "font_dirs"), string dir ) 
    foreach( ({ size, 32 }), int sz ) 
    { 
      if( r_file_stat( dir+"/"+sz+"/"+f ) ) 
      { 
        if( fn->load( roxen_path( dir+"/"+sz+"/"+f ) ) ) 
          return fn; 
      } 
    } 
}