1<?php
2// $Header: /cvsroot/html2ps/config.parse.php,v 1.7 2007/05/07 13:28:39 Konstantin Exp $
3
4require_once(HTML2PS_DIR.'font.resolver.class.php');
5require_once(HTML2PS_DIR.'treebuilder.class.php');
6require_once(HTML2PS_DIR.'media.layout.inc.php');
7
8// Get list of media types being used by script;
9// It should be a list of two types:
10// 1. Current CSS media type chose by user (defaults to 'screen')
11// 2. 'all' media type
12//
13function config_get_allowed_media() {
14  return array($GLOBALS['g_config']['cssmedia'], 'all');
15}
16
17function parse_encoding_override_node_config_file($root, &$resolver) {
18  $child = $root->first_child();
19  do {
20    if ($child->node_type() == XML_ELEMENT_NODE) {
21      switch ($child->tagname()) {
22      case "normal":
23        if ($root->has_attribute('name')) {
24          $names = explode(',', $root->get_attribute('name'));
25          foreach ($names as $name) {
26            $resolver->add_normal_encoding_override($name,
27                                                    $child->get_attribute('normal'),
28                                                    $child->get_attribute('italic'),
29                                                    $child->get_attribute('oblique'));
30          };
31        };
32
33        if ($root->has_attribute('mask')) {
34          foreach ($names as $name) {
35            $resolver->add_normal_encoding_override_mask($root->get_attribute('mask'),
36                                                         $child->get_attribute('normal'),
37                                                         $child->get_attribute('italic'),
38                                                         $child->get_attribute('oblique'));
39          };
40        };
41
42        break;
43      case "bold":
44        if ($root->has_attribute('name')) {
45          $names = explode(',', $root->get_attribute('name'));
46          foreach ($names as $name) {
47            $resolver->add_bold_encoding_override($name,
48                                                    $child->get_attribute('normal'),
49                                                    $child->get_attribute('italic'),
50                                                    $child->get_attribute('oblique'));
51          };
52        };
53
54        if ($root->has_attribute('mask')) {
55          foreach ($names as $name) {
56            $resolver->add_bold_encoding_override_mask($root->get_attribute('mask'),
57                                                       $child->get_attribute('normal'),
58                                                       $child->get_attribute('italic'),
59                                                       $child->get_attribute('oblique'));
60          };
61        };
62
63        break;
64      };
65    };
66  } while ($child = $child->next_sibling());
67}
68
69function parse_metrics_node_config_file($root, &$resolver) {
70  $resolver->add_afm_mapping($root->get_attribute('typeface'),
71                             $root->get_attribute('file'));
72}
73
74function parse_ttf_node_config_file($root, &$resolver) {
75  switch (FONT_EMBEDDING_MODE) {
76  case 'all':
77    $embed_flag = true;
78    break;
79  case 'none':
80    $embed_flag = false;
81    break;
82  case 'config':
83    $embed_flag = (bool)$root->get_attribute('embed');
84    break;
85  }
86
87  $resolver->add_ttf_mapping($root->get_attribute('typeface'),
88                             $root->get_attribute('file'),
89                             $embed_flag);
90}
91
92function parse_family_encoding_override_node_config_file($family, $root, &$resolver) {
93  $child = $root->first_child();
94  do {
95    if ($child->node_type() == XML_ELEMENT_NODE) {
96      switch ($child->tagname()) {
97      case "normal":
98        $names = explode(",",$root->get_attribute('name'));
99        foreach ($names as $name) {
100          $resolver->add_family_normal_encoding_override($family,
101                                                         $name,
102                                                         $child->get_attribute('normal'),
103                                                         $child->get_attribute('italic'),
104                                                         $child->get_attribute('oblique'));
105        };
106        break;
107      case "bold":
108        $names = explode(",",$root->get_attribute('name'));
109        foreach ($names as $name) {
110          $resolver->add_family_bold_encoding_override($family,
111                                                       $name,
112                                                       $child->get_attribute('normal'),
113                                                       $child->get_attribute('italic'),
114                                                       $child->get_attribute('oblique'));
115        };
116        break;
117      };
118    };
119  } while ($child = $child->next_sibling());
120}
121
122function parse_fonts_family_node_config_file($root, &$resolver) {
123  // Note: font family names are always converted to lower case to be non-case-sensitive
124  $child = $root->first_child();
125  do {
126    if ($child->node_type() == XML_ELEMENT_NODE) {
127      $font_family_name = strtolower($root->get_attribute('name'));
128      switch ($child->tagname()) {
129      case "normal":
130        $resolver->add_normal_family($font_family_name,
131                                     $child->get_attribute('normal'),
132                                     $child->get_attribute('italic'),
133                                     $child->get_attribute('oblique'));
134        break;
135      case "bold":
136        $resolver->add_bold_family($font_family_name,
137                                   $child->get_attribute('normal'),
138                                   $child->get_attribute('italic'),
139                                   $child->get_attribute('oblique'));
140        break;
141      case "encoding-override":
142        parse_family_encoding_override_node_config_file($font_family_name, $child, $resolver);
143        break;
144      };
145    };
146  } while ($child = $child->next_sibling());
147}
148
149function parse_fonts_node_config_file($root, &$resolver) {
150  $child = $root->first_child();
151  do {
152    if ($child->node_type() == XML_ELEMENT_NODE) {
153      switch ($child->tagname()) {
154      case "alias":
155        $resolver->add_alias(strtolower($child->get_attribute('alias')), $child->get_attribute('family'));
156        break;
157      case "family":
158        parse_fonts_family_node_config_file($child, $resolver);
159        break;
160      case "encoding-override":
161        parse_encoding_override_node_config_file($child, $resolver);
162        break;
163      case "ttf":
164        parse_ttf_node_config_file($child, $resolver);
165        break;
166      case "metrics":
167        parse_metrics_node_config_file($child, $resolver);
168        break;
169      };
170    };
171  } while ($child = $child->next_sibling());
172}
173
174function parse_config_file($filename) {
175  // Save old magic_quotes_runtime value and disable it
176  $mq_runtime = get_magic_quotes_runtime();
177  set_magic_quotes_runtime(0);
178
179  $doc = TreeBuilder::build(file_get_contents($filename));
180  $root=$doc->document_element();
181
182  $child = $root->first_child();
183  do {
184    if ($child->node_type() == XML_ELEMENT_NODE) {
185      switch ($child->tagname()) {
186      case "fonts":
187        global $g_font_resolver;
188        parse_fonts_node_config_file($child, $g_font_resolver);
189        break;
190      case "fonts-pdf":
191        global $g_font_resolver_pdf;
192        parse_fonts_node_config_file($child, $g_font_resolver_pdf);
193        break;
194      case "media":
195        add_predefined_media($child->get_attribute('name'),
196                             (float)$child->get_attribute('height'),
197                             (float)$child->get_attribute('width'));
198        break;
199      };
200    };
201  } while ($child = $child->next_sibling());
202
203  // Restore old magic_quotes_runtime values
204  set_magic_quotes_runtime($mq_runtime);
205}
206?>