1<?php
2// $Header: /cvsroot/html2ps/font.resolver.class.php,v 1.15 2006/11/11 13:43:52 Konstantin Exp $
3
4require_once(HTML2PS_DIR.'font.constants.inc.php');
5
6class FontResolver {
7  var $families = array();
8  var $aliases = array();
9
10  var $overrides = array();
11  var $overrides_mask = array();
12
13  var $ttf_mappings = array();
14  var $afm_mappings = array();
15
16  var $ps_fonts = array();
17  var $ps_fonts_counter = 1;
18
19  function FontResolver() {
20  }
21
22  function setup_ttf_mappings($pdf) {
23    foreach ($this->ttf_mappings as $typeface => $file) {
24      pdf_set_parameter($pdf, "FontOutline", $typeface."=".TTF_FONTS_REPOSITORY.$file);
25    };
26  }
27
28  function add_ttf_mapping($typeface, $file, $embed) {
29    $this->ttf_mappings[$typeface] = $file;
30    $this->embed[$typeface]        = $embed;
31  }
32
33  function add_afm_mapping($typeface, $file) {
34    $this->afm_mappings[$typeface] = $file;
35  }
36
37  function font_resolved($family, $weight, $style, $encoding) {
38    return
39      isset($this->ps_fonts[$family]) and
40      isset($this->ps_fonts[$family][$weight]) and
41      isset($this->ps_fonts[$family][$weight][$style]) and
42      isset($this->ps_fonts[$family][$weight][$style][$encoding]);
43  }
44
45  function get_afm_mapping($typeface) {
46    return (isset($this->afm_mappings[$typeface]) ?
47            $this->afm_mappings[$typeface] :
48            "");
49  }
50
51  function resolve_font($family, $weight, $style, $encoding) {
52    if (!$this->font_resolved($family, $weight, $style, $encoding)) {
53      $this->ps_fonts[$family][$weight][$style][$encoding] = 'font'.$this->ps_fonts_counter;
54      $this->ps_fonts_counter++;
55    };
56    return $this->ps_fonts[$family][$weight][$style][$encoding];
57  }
58
59  function add_family_normal_encoding_override($family, $encoding, $normal, $italic, $oblique) {
60    $this->overrides[$encoding][$family][WEIGHT_NORMAL][FS_NORMAL]  = $normal;
61    $this->overrides[$encoding][$family][WEIGHT_NORMAL][FS_ITALIC]  = $italic;
62    $this->overrides[$encoding][$family][WEIGHT_NORMAL][FS_OBLIQUE] = $oblique;
63  }
64
65  function add_family_normal_encoding_override_mask($family, $encoding, $normal, $italic, $oblique) {
66    $this->overrides_mask[$family][WEIGHT_NORMAL][FS_NORMAL][]  = array('mask' => $encoding,
67                                                                        'override' => $normal);
68    $this->overrides_mask[$family][WEIGHT_NORMAL][FS_ITALIC][]  = array('mask' => $encoding,
69                                                                        'override' => $italic);
70    $this->overrides_mask[$family][WEIGHT_NORMAL][FS_OBLIQUE][] = array('mask' => $encoding,
71                                                                        'override' => $oblique);
72  }
73
74  function add_family_bold_encoding_override($family, $encoding, $normal, $italic, $oblique) {
75    $this->overrides[$encoding][$family][WEIGHT_BOLD][FS_NORMAL]  = $normal;
76    $this->overrides[$encoding][$family][WEIGHT_BOLD][FS_ITALIC]  = $italic;
77    $this->overrides[$encoding][$family][WEIGHT_BOLD][FS_OBLIQUE] = $oblique;
78  }
79
80  function add_family_bold_encoding_override_mask($family, $encoding, $normal, $italic, $oblique) {
81    $this->overrides_mask[$family][WEIGHT_BOLD][FS_NORMAL][]  = array('mask' => $encoding,
82                                                                      'override' => $normal);
83    $this->overrides_mask[$family][WEIGHT_BOLD][FS_ITALIC][]  = array('mask' => $encoding,
84                                                                      'override' => $italic);
85    $this->overrides_mask[$family][WEIGHT_BOLD][FS_OBLIQUE][] = array('mask' => $encoding,
86                                                                      'override' => $oblique);
87  }
88
89  function add_normal_encoding_override($encoding, $normal, $italic, $oblique) {
90    $this->add_family_normal_encoding_override(" ", $encoding, $normal, $italic, $oblique);
91  }
92
93  function add_normal_encoding_override_mask($encoding, $normal, $italic, $oblique) {
94    $this->add_family_normal_encoding_override_mask(" ", $encoding, $normal, $italic, $oblique);
95  }
96
97  function add_bold_encoding_override($encoding, $normal, $italic, $oblique) {
98    $this->add_family_bold_encoding_override(" ", $encoding, $normal, $italic, $oblique);
99  }
100
101  function add_bold_encoding_override_mask($encoding, $normal, $italic, $oblique) {
102    $this->add_family_bold_encoding_override_mask(" ", $encoding, $normal, $italic, $oblique);
103  }
104
105  function get_global_encoding_override($weight, $style, $encoding) {
106    return $this->get_family_encoding_override(" ", $weight, $style, $encoding);
107  }
108
109  function get_family_encoding_override($family, $weight, $style, $encoding) {
110    if (isset($this->overrides[$encoding]) &&
111        isset($this->overrides[$encoding][$family]) &&
112        isset($this->overrides[$encoding][$family][$weight]) &&
113        isset($this->overrides[$encoding][$family][$weight][$style])) {
114      return $this->overrides[$encoding][$family][$weight][$style];
115    };
116
117    if (isset($this->overrides_mask[$family]) &&
118        isset($this->overrides_mask[$family][$weight]) &&
119        isset($this->overrides_mask[$family][$weight][$style])) {
120      foreach ($this->overrides_mask[$family][$weight][$style] as $override) {
121        if (preg_match($override['mask'], $encoding)) {
122          return $override['override'];
123        };
124      };
125    };
126
127    return '';
128  }
129
130  function have_global_encoding_override($weight, $style, $encoding) {
131    return $this->get_global_encoding_override($weight, $style, $encoding) !== "";
132  }
133
134  function have_family_encoding_override($family, $weight, $style, $encoding) {
135    return $this->get_family_encoding_override($family, $weight, $style, $encoding) !== "";
136  }
137
138  function add_alias($alias, $family) { $this->aliases[$alias] = $family; }
139
140  function add_normal_family($family, $normal, $italic, $oblique) {
141    $this->families[$family][WEIGHT_NORMAL][FS_NORMAL]  = $normal;
142    $this->families[$family][WEIGHT_NORMAL][FS_ITALIC]  = $italic;
143    $this->families[$family][WEIGHT_NORMAL][FS_OBLIQUE] = $oblique;
144  }
145
146  function add_bold_family($family, $normal, $italic, $oblique) {
147    $this->families[$family][WEIGHT_BOLD][FS_NORMAL]  = $normal;
148    $this->families[$family][WEIGHT_BOLD][FS_ITALIC]  = $italic;
149    $this->families[$family][WEIGHT_BOLD][FS_OBLIQUE] = $oblique;
150  }
151
152  function get_typeface_name($family, $weight, $style, $encoding) {
153    if ($this->have_alias($family)) {
154      return $this->get_typeface_name($this->aliases[$family], $weight, $style, $encoding);
155    }
156
157    // Check for family-specific encoding override
158    if ($this->have_family_encoding_override($family, $weight, $style, $encoding)) {
159      return $this->get_family_encoding_override($family, $weight, $style, $encoding);
160    }
161
162    // Check for global encoding override
163    if ($this->have_global_encoding_override($weight, $style, $encoding)) {
164      return $this->get_global_encoding_override($weight, $style, $encoding);
165    }
166
167    if (!isset($this->families[$family])) { return "Times-Roman"; };
168    if (!isset($this->families[$family][$weight])) { return "Times-Roman"; };
169    if (!isset($this->families[$family][$weight][$style])) { return "Times-Roman"; };
170
171    return $this->families[$family][$weight][$style];
172  }
173
174  function have_alias($family) {
175    return isset($this->aliases[$family]);
176  }
177
178  function have_font_family($family) {
179    return isset($this->families[$family]) or $this->have_alias($family);
180  }
181}
182
183global $g_font_resolver, $g_font_resolver_pdf;
184$g_font_resolver = new FontResolver();
185$g_font_resolver_pdf = new FontResolver();
186
187?>