00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 @require_once('config.inc.php');
00013 require_once('common.inc.php');
00014 require_once('html.inc.php');
00015 require_once('html_parse.inc.php');
00016 require_once('modules.inc.php');
00017
00018 require_once('util.inc.php');
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 function _include_woff_font($font_family, $style_to_include = '')
00033 {
00034 static $already_included = array();
00035
00036
00037
00038 $font_family = str_replace('"', '', $font_family);
00039 $font_family = str_replace('\'', '', $font_family);
00040 $woff_fonts = _woff_fonts();
00041 if (!array_key_exists($font_family, $woff_fonts)) {
00042 return false;
00043 }
00044
00045 foreach ($woff_fonts[$font_family] as $style=>$woff) {
00046 if (!empty($style_to_include) && $style_to_include != $style) {
00047 continue;
00048 }
00049
00050 if (isset($already_included[$font_family])) {
00051 if (isset($already_included[$font_family][$style])) {
00052 continue;
00053 }
00054 }
00055
00056 $rule = '@font-face {'.nl();
00057 $rule .= tab().'font-family: \''.$font_family.'\';'.nl();
00058 if ($style == 'italic' || $style == 'bolditalic') {
00059 $rule .= tab().'font-style: italic;'.nl();
00060 } else {
00061 $rule .= tab().'font-style: normal;'.nl();
00062 }
00063 if ($style == 'bold' || $style == 'bolditalic') {
00064 $rule .= tab().'font-weight: bold;'.nl();
00065 } else {
00066 $rule .= tab().'font-weight: normal;'.nl();
00067 }
00068 $rule .= tab().'src: url('.base_url().'img/'.$woff.') format("woff");'.nl();
00069 $rule .= '}';
00070 html_add_css_inline($rule, 5);
00071 // add to list of already included font styles
00072 if (!isset($already_included[$font_family])) {
00073 $already_included[$font_family] = array();
00074 }
00075 $already_included[$font_family][$style] = true;
00076 }
00077 return true;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 function _is_woff_font($font_family)
00088 {
00089 $woff_fonts = _woff_fonts();
00090 // strip quotation marks for the comparison
00091 // TODO (later): do proper parsing of font-family string
00092 $font_family = str_replace('"', '', $font_family);
00093 $font_family = str_replace('\'', '', $font_family);
00094 return array_key_exists($font_family, $woff_fonts);
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 function _text_render_content($s, $name)
00107 {
00108 // resolve any aliases
00109 $s = resolve_aliases($s, $name);
00110 $s = html_encode_str_smart($s);
00111 // automatically add <br> elements for newlines
00112 if (TEXT_AUTO_BR) {
00113 $s = str_replace("\r\n", "\n", $s);
00114 $s = str_replace("\n", "<br>\n", $s);
00115 }
00116 // encode non-breakable spaces (160, 0xc2 0xa0 in utf-8)
00117 $s = str_replace("\xc2\xa0", ' ', $s);
00118 // resolve any relative urls
00119 $s = resolve_relative_urls($s);
00120 return $s;
00121 }
00122
00123
00124 // TODO (later): why do global variables not work here?
00125
00126
00127
00128
00129
00130 function _woff_fonts()
00131 {
00132 // use a hardcoded array of woff fonts for now
00133 return array(
00134 'DejaVuSans' => array(
00135 'normal' => 'dejavusans-webfont.woff',
00136 'italic' => 'dejavusans-oblique-webfont.woff',
00137 'bold' => 'dejavusans-bold-webfont.woff',
00138 'bolditalic' => 'dejavusans-boldoblique-webfont.woff'
00139 ),
00140 'DejaVuSerif' => array(
00141 'normal' => 'dejavuserif-webfont.woff',
00142 'italic' => 'dejavuserif-italic-webfont.woff',
00143 'bold' => 'dejavuserif-bold-webfont.woff',
00144 'bolditalic' => 'dejavuserif-bolditalic-webfont.woff'
00145 ),
00146 'DejaVuSansMono' => array(
00147 'normal' => 'dejavusansmono-webfont.woff',
00148 'italic' => 'dejavusansmono-oblique-webfont.woff',
00149 'bold' => 'dejavusansmono-bold-webfont.woff',
00150 'bolditalic' => 'dejavusansmono-boldoblique-webfont.woff'
00151 )
00152 );
00153 }
00154
00155
00156 function text_alter_save($args)
00157 {
00158 $elem = $args['elem'];
00159 $obj = &$args['obj'];
00160 if (!elem_has_class($elem, 'text')) {
00161 return false;
00162 }
00163
00164 // background-color
00165 if (elem_css($elem, 'background-color') !== NULL) {
00166 $obj['text-background-color'] = elem_css($elem, 'background-color');
00167 } else {
00168 unset($obj['text-background-color']);
00169 }
00170 // we don't handle content here
00171 // see the comments in $.glue.object.register_alter_pre_save (at text-edit.js)
00172 // font-color
00173 if (elem_css($elem, 'color') !== NULL) {
00174 $obj['text-font-color'] = elem_css($elem, 'color');
00175 } else {
00176 unset($obj['text-font-color']);
00177 }
00178 // font-family
00179 if (elem_css($elem, 'font-family') !== NULL) {
00180 $obj['text-font-family'] = elem_css($elem, 'font-family');
00181 } else {
00182 unset($obj['text-font-family']);
00183 }
00184 // font-size
00185 if (elem_css($elem, 'font-size') !== NULL) {
00186 $obj['text-font-size'] = elem_css($elem, 'font-size');
00187 } else {
00188 unset($obj['text-font-size']);
00189 }
00190 // font-style
00191 if (elem_css($elem, 'font-style') !== NULL) {
00192 $obj['text-font-style'] = elem_css($elem, 'font-style');
00193 } else {
00194 unset($obj['text-font-style']);
00195 }
00196 // font-weight
00197 if (elem_css($elem, 'font-weight') !== NULL) {
00198 $obj['text-font-weight'] = elem_css($elem, 'font-weight');
00199 } else {
00200 unset($obj['text-font-weight']);
00201 }
00202 // letter-spacing
00203 if (elem_css($elem, 'letter-spacing') !== NULL) {
00204 $obj['text-letter-spacing'] = elem_css($elem, 'letter-spacing');
00205 } else {
00206 unset($obj['text-letter-spacing']);
00207 }
00208 // line-height
00209 if (elem_css($elem, 'line-height') !== NULL) {
00210 $obj['text-line-height'] = elem_css($elem, 'line-height');
00211 } else {
00212 unset($obj['text-line-height']);
00213 }
00214 if (elem_css($elem, 'padding') !== NULL) {
00215 // parse padding
00216 // this is needed for Firefox
00217 $s = expl(' ', elem_css($elem, 'padding'));
00218 if (count($s) == 1) {
00219 // padding-x = padding-y
00220 $obj['text-padding-x'] = $s[0];
00221 $obj['text-padding-y'] = $s[0];
00222 } elseif (1 < count($s)) {
00223 // padding-x
00224 $obj['text-padding-x'] = $s[1];
00225 // padding-y
00226 $obj['text-padding-y'] = $s[0];
00227 }
00228 } else {
00229 // padding-x
00230 if (elem_css($elem, 'padding-left') !== NULL) {
00231 $obj['text-padding-x'] = elem_css($elem, 'padding-left');
00232 } else {
00233 unset($obj['text-padding-x']);
00234 }
00235 // padding-y
00236 if (elem_css($elem, 'padding-top') !== NULL) {
00237 $obj['text-padding-y'] = elem_css($elem, 'padding-top');
00238 } else {
00239 unset($obj['text-padding-y']);
00240 }
00241 }
00242 // text-align
00243 if (elem_css($elem, 'text-align') !== NULL) {
00244 $obj['text-align'] = elem_css($elem, 'text-align');
00245 } else {
00246 unset($obj['text-align']);
00247 }
00248 // word-spacing
00249 if (elem_css($elem, 'word-spacing') !== NULL) {
00250 $obj['text-word-spacing'] = elem_css($elem, 'word-spacing');
00251 } else {
00252 unset($obj['text-word-spacing']);
00253 }
00254
00255 return true;
00256 }
00257
00258
00259 function text_alter_render_early($args)
00260 {
00261 $elem = &$args['elem'];
00262 $obj = $args['obj'];
00263 if (!elem_has_class($elem, 'text')) {
00264 return false;
00265 }
00266
00267 // background-color
00268 if (!empty($obj['text-background-color'])) {
00269 elem_css($elem, 'background-color', $obj['text-background-color']);
00270 }
00271 // content
00272 if (!isset($obj['content'])) {
00273 $obj['content'] = '';
00274 }
00275 if ($args['edit']) {
00276 // add a textarea
00277 $i = elem('textarea');
00278 elem_add_class($i, 'glue-text-input');
00279 elem_css($i, 'width', '100%');
00280 elem_css($i, 'height', '100%');
00281 // hide the text area by default
00282 elem_css($i, 'display', 'none');
00283 // set the context to the textarea to the (unrendered) object content
00284 $content = htmlspecialchars($obj['content'], ENT_NOQUOTES, 'UTF-8');
00285 // replace newline characters by an entity to prevent render_object()
00286 // from adding some indentation
00287 $content = str_replace("\r\n", ' ', $content);
00288 $content = str_replace("\n", ' ', $content);
00289 // why not replace tabs as well why we are at it
00290 $content = str_replace("\t", '	', $content);
00291 elem_val($i, $content);
00292 elem_append($elem, $i);
00293 // and a nested div
00294 $r = elem('div');
00295 elem_add_class($r, 'glue-text-render');
00296 elem_css($r, 'width', '100%');
00297 elem_css($r, 'height', '100%');
00298 // set the content of the div to the rendered object content
00299 elem_val($r, _text_render_content($obj['content'], $obj['name']));
00300 elem_append($elem, $r);
00301 } else {
00302 elem_append($elem, _text_render_content($obj['content'], $obj['name']));
00303 }
00304 // font-color
00305 if (!empty($obj['text-font-color'])) {
00306 elem_css($elem, 'color', $obj['text-font-color']);
00307 }
00308 // font-family
00309 if (!empty($obj['text-font-family'])) {
00310 elem_css($elem, 'font-family', $obj['text-font-family']);
00311 if (TEXT_USE_WOFF_FONTS) {
00312 if (_is_woff_font($obj['text-font-family'])) {
00313 // include all styles of the font because of inline html
00314 // (<strong>, etc)
00315 _include_woff_font($obj['text-font-family']);
00316 }
00317 }
00318 }
00319 // font-size
00320 if (!empty($obj['text-font-size'])) {
00321 elem_css($elem, 'font-size', $obj['text-font-size']);
00322 }
00323 // font-style
00324 if (!empty($obj['text-font-style'])) {
00325 elem_css($elem, 'font-style', $obj['text-font-style']);
00326 }
00327 // font-weight
00328 if (!empty($obj['text-font-weight'])) {
00329 elem_css($elem, 'font-weight', $obj['text-font-weight']);
00330 }
00331 // letter-spacing
00332 if (!empty($obj['text-letter-spacing'])) {
00333 elem_css($elem, 'letter-spacing', $obj['text-letter-spacing']);
00334 }
00335 // line-height
00336 if (!empty($obj['text-line-height'])) {
00337 elem_css($elem, 'line-height', $obj['text-line-height']);
00338 }
00339 // padding-x
00340 if (!empty($obj['text-padding-x'])) {
00341 elem_css($elem, 'padding-left', $obj['text-padding-x']);
00342 elem_css($elem, 'padding-right', $obj['text-padding-x']);
00343 }
00344 // padding-y
00345 if (!empty($obj['text-padding-y'])) {
00346 elem_css($elem, 'padding-top', $obj['text-padding-y']);
00347 elem_css($elem, 'padding-bottom', $obj['text-padding-y']);
00348 }
00349 // text-align
00350 if (!empty($obj['text-align'])) {
00351 elem_css($elem, 'text-align', $obj['text-align']);
00352 }
00353 // word-spacing
00354 if (!empty($obj['text-word-spacing'])) {
00355 elem_css($elem, 'word-spacing', $obj['text-word-spacing']);
00356 }
00357
00358 return true;
00359 }
00360
00361
00362 function text_render_object($args)
00363 {
00364 $obj = $args['obj'];
00365 if (!isset($obj['type']) || $obj['type'] != 'text') {
00366 return false;
00367 }
00368
00369 $e = elem('div');
00370 elem_attr($e, 'id', $obj['name']);
00371 elem_add_class($e, 'text');
00372 elem_add_class($e, 'resizable');
00373 elem_add_class($e, 'object');
00374
00375 // hooks
00376 invoke_hook_first('alter_render_early', 'text', array('obj'=>$obj, 'elem'=>&$e, 'edit'=>$args['edit']));
00377 $html = elem_finalize($e);
00378 invoke_hook_last('alter_render_late', 'text', array('obj'=>$obj, 'html'=>&$html, 'elem'=>$e, 'edit'=>$args['edit']));
00379
00380 return $html;
00381 }
00382
00383
00384 function text_render_page_early($args)
00385 {
00386 if ($args['edit']) {
00387 if (USE_MIN_FILES) {
00388 html_add_js(base_url().'modules/text/text-edit.min.js');
00389 } else {
00390 html_add_js(base_url().'modules/text/text-edit.js');
00391 }
00392 html_add_css(base_url().'modules/text/text-edit.css');
00393 html_add_js_var('$.glue.conf.text.auto_br', TEXT_AUTO_BR);
00394
00395 if (TEXT_USE_WOFF_FONTS) {
00396 $woff_fonts = _woff_fonts();
00397 foreach ($woff_fonts as $font=>$styles) {
00398 _include_woff_font($font);
00399 // TODO (later): check css encoding
00400 $rule = '.glue-font-woff-'.$font.' {'.nl();
00401 // we use single quotes as they don't clash with inline styles
00402 $rule .= tab().'font-family: \''.$font.'\';'.nl();
00403 $rule .= '}';
00404 html_add_css_inline($rule, 6);
00405 }
00406 }
00407 }
00408 }
00409
00410
00411 function text_save_state($args)
00412 {
00413 $elem = $args['elem'];
00414 $obj = $args['obj'];
00415 if (array_shift(elem_classes($elem)) != 'text') {
00416 return false;
00417 }
00418
00419 // make sure the type is set
00420 $obj['type'] = 'text';
00421 $obj['module'] = 'text';
00422
00423 // hook
00424 invoke_hook('alter_save', array('obj'=>&$obj, 'elem'=>$elem));
00425
00426 load_modules('glue');
00427 $ret = save_object($obj);
00428 if ($ret['#error']) {
00429 load_msg('error', 'text_save_state: save_object returned '.quot($ret['#data']));
00430 return false;
00431 } else {
00432 return true;
00433 }
00434 }