b824672000-09-03Martin Nilsson // This file is part of Roxen WebServer.
f41b982009-05-07Martin Stjernholm // Copyright © 1996 - 2009, Roxen IS.
b824672000-09-03Martin Nilsson  #include <config.h>
84aee52015-10-23Henrik Grubbström (Grubba) constant cvs_version = "$Id$";
c6fd2e2000-09-03Per Hedbor  constant name = "Compatibility bitmap fonts";
0bd4d92000-09-04Per Hedbor constant doc = "Compatibility (bitmapped) fonts for Roxen 1.3 and earlier."
c6239d2000-09-04Per Hedbor " Compact image file fonts are preferable to this format, since they are " "almost always smaller, and easier to create.";
c6fd2e2000-09-03Per Hedbor  inherit FontHandler;
631e742008-11-13Jonas Wallden Thread.Mutex lock = Thread.Mutex(); protected mapping font_cache; protected void build_font_cache()
c6fd2e2000-09-03Per Hedbor {
631e742008-11-13Jonas Wallden  mapping res = ([ ]);
c6fd2e2000-09-03Per Hedbor  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)) {
631e742008-11-13Jonas Wallden  res["32/" + f + "/" + style] = replace(f, "_", " ");
c6fd2e2000-09-03Per Hedbor  } } } } }
631e742008-11-13Jonas Wallden  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));
c6fd2e2000-09-03Per Hedbor }
b824672000-09-03Martin Nilsson array(mapping) font_information( string fnt )
c6fd2e2000-09-03Per Hedbor {
a6bf1d2000-09-04Per Hedbor  string ofnt = fnt; fnt = replace(lower_case( fnt ), " ", "_");
b824672000-09-03Martin Nilsson  array font_infos=({});
c6fd2e2000-09-03Per Hedbor  foreach(roxen->query("font_dirs"), string dir) { dir+="32/"; if( r_file_stat( dir+fnt ) )
d3a6822000-09-03Martin Nilsson  // the font file exists in this dir..
c6fd2e2000-09-03Per Hedbor  { array d = r_get_dir(dir+fnt); foreach( ({ "nn", "ni", "li", "ln", "Bi", "Bn", "bi", "bn" }), string style) if(has_value(d, style)) {
b824672000-09-03Martin Nilsson  mapping font_info = ([ "name":fnt,
a6bf1d2000-09-04Per Hedbor  "family":ofnt,
b824672000-09-03Martin Nilsson  "path":dir+fnt,
a6bf1d2000-09-04Per Hedbor  "style":"",
b824672000-09-03Martin Nilsson  "format":"bitmap dump" ]); switch(style[0]) {
a6bf1d2000-09-04Per Hedbor  case 'l': font_info->style+="light"; break; case 'b': font_info->style+="bold"; break; case 'B': font_info->style+="black"; break;
b824672000-09-03Martin Nilsson  }
a6bf1d2000-09-04Per Hedbor  if(style[1]=='i') font_info->style+="italic"; if(style[1]=='I') font_info->style+="oblique";
b824672000-09-03Martin Nilsson  font_infos+=({ font_info });
c6fd2e2000-09-03Per Hedbor  } } }
b824672000-09-03Martin Nilsson  return font_infos;
c6fd2e2000-09-03Per Hedbor } string fix_name( string what ) { return replace( lower_case(what), " ", "_" ); } array has_font( string name, int size ) { string base_dir;
6ac69d2009-02-06Jonas Wallden  array available;
6dea542001-03-23Per Hedbor  if( String.width( name ) > 8 ) return 0;
631e742008-11-13Jonas Wallden  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; }
bea6382001-05-04Jonas Wallden  }
631e742008-11-13Jonas Wallden  key = 0; return has_font(name, 32);
c6fd2e2000-09-03Per Hedbor  }
631e742008-11-13Jonas Wallden  return 0;
c6fd2e2000-09-03Per Hedbor }
11b6b72001-02-02Martin Nilsson 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 ); }
7319b62001-08-29Martin Nilsson  string _sprintf() { return sprintf( "OldFont" ); }
11b6b72001-02-02Martin Nilsson }
c6fd2e2000-09-03Per Hedbor Font open( string name, int size, int bold, int italic ) {
6dea542001-03-23Per Hedbor  if( String.width( name ) > 8 ) return 0;
c6fd2e2000-09-03Per Hedbor  string f = make_font_name( name, size, bold, italic );
11b6b72001-02-02Martin Nilsson  Image.Font fn = MyFont();
c6fd2e2000-09-03Per Hedbor  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; } } }