Repository navigation
Expand file tree
/
Copy pathmodule_text.inc.php
More file actions
760 lines (716 loc) · 25.6 KB
/
Copy pathmodule_text.inc.php
File metadata and controls
760 lines (716 loc) · 25.6 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<?php
/*
* module_text.inc.php
* Module for placing text elements on a page
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('common.inc.php');
require_once('html.inc.php');
require_once('html_parse.inc.php');
require_once('modules.inc.php');
// module glue gets loaded on demand
require_once('util.inc.php');
// module_image.inc.php has more information on what's going on inside modules
// (they can be easier than that one though)
/**
* include the font-face css for a woff-font
*
* @param string $font_family font family to include
* @param string $style_to_include style to include (normal, italic, bold, bolditalic) (default: include all styles)
* @return true if successful, false if not
*/
function _include_woff_font($font_family, $style_to_include = '')
{
static $already_included = [];
// strip quotation marks
// TODO (later): do proper parsing of font-family string
$font_family = str_replace('"', '', $font_family);
$font_family = str_replace('\'', '', $font_family);
$woff_fonts = _woff_fonts();
if (!array_key_exists($font_family, $woff_fonts)) {
return false;
}
foreach ($woff_fonts[$font_family] as $style=>$woff) {
if (!empty($style_to_include) && $style_to_include != $style) {
continue;
}
// check if already included
if (isset($already_included[$font_family])) {
if (isset($already_included[$font_family][$style])) {
continue;
}
}
// TODO (later): check css encoding
$rule = '@font-face {'.nl();
$rule .= tab().'font-family: \''.$font_family.'\';'.nl();
if ($style == 'italic' || $style == 'bolditalic') {
$rule .= tab().'font-style: italic;'.nl();
} else {
$rule .= tab().'font-style: normal;'.nl();
}
if ($style == 'bold' || $style == 'bolditalic') {
$rule .= tab().'font-weight: bold;'.nl();
} else {
$rule .= tab().'font-weight: normal;'.nl();
}
// the paths are site-root-relative (fonts/...), and the format
// follows the extension: woff2 for the bundle, woff for the legacy
// faces it absorbed
$ext = filext($woff);
$rule .= tab().'src: url('.$woff.') format("'.($ext == 'woff2' ? 'woff2' : 'woff').'");'.nl();
$rule .= '}';
html_add_css_inline($rule, 5);
// add to list of already included font styles
if (!isset($already_included[$font_family])) {
$already_included[$font_family] = [];
}
$already_included[$font_family][$style] = true;
}
return true;
}
/**
* return if the font-family includes a woff-font
*
* @param string $font_family font family
* @return true if there is a woff-font, false if not
*/
function _is_woff_font($font_family)
{
$woff_fonts = _woff_fonts();
// strip quotation marks for the comparison
// TODO (later): do proper parsing of font-family string
$font_family = str_replace('"', '', $font_family);
$font_family = str_replace('\'', '', $font_family);
return array_key_exists($font_family, $woff_fonts);
}
/**
* include the font-face css for a user-uploaded custom font (site
* settings, see /pages) - counterpart to _include_woff_font() for
* site_custom_fonts() instead of the hardcoded _woff_fonts()
*
* @param string $font_family font family (as returned by site_custom_fonts())
* @return true if successful, false if not
*/
function _include_custom_font($font_family)
{
static $already_included = [];
// normalize before checking/setting the cache - checking the raw
// (possibly quoted) value but caching under the normalized one meant
// the two could never match, so the cache never actually hit for any
// quoted font-family value (e.g. a computed style of '"Quicksand-Bold"'
// with literal quotes, which is what gets stored/reused for this
// object attribute)
$font_family = str_replace('"', '', $font_family);
$font_family = str_replace('\'', '', $font_family);
if (isset($already_included[$font_family])) {
return true;
}
foreach (site_custom_fonts() as $font) {
if (empty($font['name']) || empty($font['file']) || $font['name'] != $font_family) {
continue;
}
switch (strtolower(filext($font['file']))) {
case 'woff2':
$format = 'woff2';
break;
case 'ttf':
$format = 'truetype';
break;
case 'otf':
$format = 'opentype';
break;
default:
$format = 'woff';
}
$rule = '@font-face {'.nl();
$rule .= tab().'font-family: \''.$font_family.'\';'.nl();
// kept relative (not prefixed with base_url()) so it still resolves
// correctly when viewed through a different domain than the one
// configured/detected as the base url - see
// module_object.inc.php's object_alter_render_late() for the full rationale
$rule .= tab().'src: url('.CONTENT_DIR.'/'.get_first_item(expl('.', site_options_page())).'/shared/'.rawurlencode($font['file']).') format("'.$format.'");'.nl();
$rule .= '}';
html_add_css_inline($rule, 5);
$already_included[$font_family] = true;
return true;
}
return false;
}
/**
* return if the font-family is a user-uploaded custom font
*
* @param string $font_family font family
* @return true if it is a custom font, false if not
*/
function _is_custom_font($font_family)
{
$font_family = str_replace('"', '', $font_family);
$font_family = str_replace('\'', '', $font_family);
foreach (site_custom_fonts() as $font) {
if (!empty($font['name']) && $font['name'] == $font_family) {
return true;
}
}
return false;
}
/**
* helper function for rendering the content of a text object for use in
* editing (outside the textarea) and viewing
*
* @param string $s content
* @param string $name object name
* @return html-encoded content
*/
function _text_render_content($s, $name)
{
// resolve any aliases
$s = resolve_aliases($s, $name);
$s = html_encode_str_smart($s);
// automatically add <br> elements for newlines
if (TEXT_AUTO_BR) {
$s = str_replace("\r\n", "\n", $s);
$s = str_replace("\n", "<br>\n", $s);
}
// encode non-breakable spaces (160, 0xc2 0xa0 in utf-8)
$s = str_replace("\xc2\xa0", ' ', $s);
// resolve any relative urls
$s = resolve_relative_urls($s);
return $s;
}
// TODO (later): why do global variables not work here?
/**
* return an array of all available woff-fonts
*
* the bundled font set (SOW-font-bundle.md plus the legacy defaults it
* absorbed): every family self-hosted under fonts/, keyed by family name
* (the value text-font-family stores) with the styles it ships, each
* style's file relative to the site root. The array order is the picker
* order. Licenses travel with the files (fonts/MANIFEST.md records
* font -> license -> source).
*
* @return array
*/
function _woff_fonts()
{
return [
'Inter' => [
'normal' => 'fonts/inter/inter-400.woff2',
'italic' => 'fonts/inter/inter-400i.woff2',
'bold' => 'fonts/inter/inter-700.woff2',
'bolditalic' => 'fonts/inter/inter-700i.woff2'
],
'Lora' => [
'normal' => 'fonts/lora/lora-400.woff2',
'italic' => 'fonts/lora/lora-400i.woff2',
'bold' => 'fonts/lora/lora-700.woff2',
'bolditalic' => 'fonts/lora/lora-700i.woff2'
],
'Space Mono' => [
'normal' => 'fonts/spacemono/spacemono-400.woff2',
'italic' => 'fonts/spacemono/spacemono-400i.woff2',
'bold' => 'fonts/spacemono/spacemono-700.woff2',
'bolditalic' => 'fonts/spacemono/spacemono-700i.woff2'
],
'Press Start 2P' => [
'normal' => 'fonts/pressstart2p/pressstart2p-400.woff2'
],
'VT323' => [
'normal' => 'fonts/vt323/vt323-400.woff2'
],
'Silkscreen' => [
'normal' => 'fonts/silkscreen/silkscreen-400.woff2',
'bold' => 'fonts/silkscreen/silkscreen-700.woff2'
],
'UnifrakturMaguntia' => [
'normal' => 'fonts/unifrakturmaguntia/unifrakturmaguntia-400.woff2'
],
'Anton' => [
'normal' => 'fonts/anton/anton-400.woff2'
],
'Archivo Black' => [
'normal' => 'fonts/archivoblack/archivoblack-400.woff2'
],
'Caveat' => [
'normal' => 'fonts/caveat/caveat-400.woff2',
'bold' => 'fonts/caveat/caveat-700.woff2'
],
'Shadows Into Light' => [
'normal' => 'fonts/shadowsintolight/shadowsintolight-400.woff2'
],
'Rubik Glitch' => [
'normal' => 'fonts/rubikglitch/rubikglitch-400.woff2'
],
'Rubik Wet Paint' => [
'normal' => 'fonts/rubikwetpaint/rubikwetpaint-400.woff2'
],
'Rubik Puddles' => [
'normal' => 'fonts/rubikpuddles/rubikpuddles-400.woff2'
],
'LatinModern' => [
'normal' => 'fonts/latinmodern/lmsans10-regular-webfont.woff',
'italic' => 'fonts/latinmodern/lmsans10-oblique-webfont.woff',
'bold' => 'fonts/latinmodern/lmsans10-bold-webfont.woff',
'bolditalic' => 'fonts/latinmodern/lmsans10-boldoblique-webfont.woff'
],
'DejaVuSans' => [
'normal' => 'fonts/dejavusans/dejavusans-webfont.woff',
'italic' => 'fonts/dejavusans/dejavusans-oblique-webfont.woff',
'bold' => 'fonts/dejavusans/dejavusans-bold-webfont.woff',
'bolditalic' => 'fonts/dejavusans/dejavusans-boldoblique-webfont.woff'
],
'DejaVuSerif' => [
'normal' => 'fonts/dejavuserif/dejavuserif-webfont.woff',
'italic' => 'fonts/dejavuserif/dejavuserif-italic-webfont.woff',
'bold' => 'fonts/dejavuserif/dejavuserif-bold-webfont.woff',
'bolditalic' => 'fonts/dejavuserif/dejavuserif-bolditalic-webfont.woff'
],
'DejaVuSansMono' => [
'normal' => 'fonts/dejavusansmono/dejavusansmono-webfont.woff',
'italic' => 'fonts/dejavusansmono/dejavusansmono-oblique-webfont.woff',
'bold' => 'fonts/dejavusansmono/dejavusansmono-bold-webfont.woff',
'bolditalic' => 'fonts/dejavusansmono/dejavusansmono-boldoblique-webfont.woff'
]
];
}
function text_alter_save($args)
{
$elem = $args['elem'];
$obj = &$args['obj'];
if (!elem_has_class($elem, 'text')) {
return false;
}
// background-color
if (elem_css($elem, 'background-color') !== NULL) {
$obj['text-background-color'] = elem_css($elem, 'background-color');
} else {
unset($obj['text-background-color']);
}
// we don't handle content here
// see the comments in $.glue.object.register_alter_pre_save (at text-edit.js)
// font-color
if (elem_css($elem, 'color') !== NULL) {
$obj['text-font-color'] = elem_css($elem, 'color');
} else {
unset($obj['text-font-color']);
}
// font-family
if (elem_css($elem, 'font-family') !== NULL) {
$obj['text-font-family'] = elem_css($elem, 'font-family');
} else {
unset($obj['text-font-family']);
}
// font-size
if (elem_css($elem, 'font-size') !== NULL) {
$obj['text-font-size'] = elem_css($elem, 'font-size');
} else {
unset($obj['text-font-size']);
}
// font-style
if (elem_css($elem, 'font-style') !== NULL) {
$obj['text-font-style'] = elem_css($elem, 'font-style');
} else {
unset($obj['text-font-style']);
}
// font-weight
if (elem_css($elem, 'font-weight') !== NULL) {
$obj['text-font-weight'] = elem_css($elem, 'font-weight');
} else {
unset($obj['text-font-weight']);
}
// the text shadow's ingredients (see text_render_object) - the glow's
// three, and since 2026-09-28 the drop shadow's distance, angle and spread
foreach (['radius', 'alpha', 'color', 'distance', 'angle', 'spread'] as $part) {
if (elem_css($elem, '--glue-shadow-'.$part) !== NULL) {
$obj['text-shadow-'.$part] = elem_css($elem, '--glue-shadow-'.$part);
} else {
unset($obj['text-shadow-'.$part]);
}
}
// text-decoration - underline and strikethrough, which share one CSS
// property and are therefore stored as one value ('underline',
// 'line-through', or both). Nothing stored this before the font popover
// existed, and a property that is not on this list is dropped on save, so
// without it the two toggles would look right until the page reloaded.
if (elem_css($elem, 'text-decoration') !== NULL) {
$obj['text-text-decoration'] = elem_css($elem, 'text-decoration');
} else {
unset($obj['text-text-decoration']);
}
// letter-spacing
if (elem_css($elem, 'letter-spacing') !== NULL) {
$obj['text-letter-spacing'] = elem_css($elem, 'letter-spacing');
} else {
unset($obj['text-letter-spacing']);
}
// line-height
if (elem_css($elem, 'line-height') !== NULL) {
$obj['text-line-height'] = elem_css($elem, 'line-height');
} else {
unset($obj['text-line-height']);
}
// padding: the x/y pair while the sides are symmetric, the four sides
// individually when they are not (the padding panel's "more knobs"). The
// renderer below applies the per-side attributes first and falls back to
// the pair, so objects saved as x/y keep rendering exactly as before.
$pad = array('top' => NULL, 'right' => NULL, 'bottom' => NULL, 'left' => NULL);
if (elem_css($elem, 'padding') !== NULL) {
// parse the shorthand - this is needed for Firefox, which serialises
// inline padding as shorthand rather than longhands
$s = expl(' ', elem_css($elem, 'padding'));
if (count($s) == 1) {
$pad['top'] = $pad['right'] = $pad['bottom'] = $pad['left'] = $s[0];
} elseif (count($s) == 2) {
$pad['top'] = $pad['bottom'] = $s[0];
$pad['left'] = $pad['right'] = $s[1];
} elseif (count($s) == 3) {
$pad['top'] = $s[0];
$pad['left'] = $pad['right'] = $s[1];
$pad['bottom'] = $s[2];
} else {
$pad['top'] = $s[0];
$pad['right'] = $s[1];
$pad['bottom'] = $s[2];
$pad['left'] = $s[3];
}
} else {
foreach ($pad as $side => $v) {
$pad[$side] = elem_css($elem, 'padding-'.$side);
}
}
// left/right -> text-padding-x, or the two longhands when they differ -
// and whichever form is written, the other one goes: the render reads
// the longhands first, so a stale text-padding-left outranked the pair,
// and a padding dragged back to 0 came back on reload (2026-09-27)
if ($pad['left'] !== NULL || $pad['right'] !== NULL) {
if ($pad['left'] !== NULL && $pad['right'] !== NULL && $pad['left'] == $pad['right']) {
$obj['text-padding-x'] = $pad['left'];
unset($obj['text-padding-left']);
unset($obj['text-padding-right']);
} else {
unset($obj['text-padding-x']);
if ($pad['left'] !== NULL) {
$obj['text-padding-left'] = $pad['left'];
} else {
unset($obj['text-padding-left']);
}
if ($pad['right'] !== NULL) {
$obj['text-padding-right'] = $pad['right'];
} else {
unset($obj['text-padding-right']);
}
}
} else {
unset($obj['text-padding-x']);
unset($obj['text-padding-left']);
unset($obj['text-padding-right']);
}
// top/bottom likewise
if ($pad['top'] !== NULL || $pad['bottom'] !== NULL) {
if ($pad['top'] !== NULL && $pad['bottom'] !== NULL && $pad['top'] == $pad['bottom']) {
$obj['text-padding-y'] = $pad['top'];
unset($obj['text-padding-top']);
unset($obj['text-padding-bottom']);
} else {
unset($obj['text-padding-y']);
if ($pad['top'] !== NULL) {
$obj['text-padding-top'] = $pad['top'];
} else {
unset($obj['text-padding-top']);
}
if ($pad['bottom'] !== NULL) {
$obj['text-padding-bottom'] = $pad['bottom'];
} else {
unset($obj['text-padding-bottom']);
}
}
} else {
unset($obj['text-padding-y']);
unset($obj['text-padding-top']);
unset($obj['text-padding-bottom']);
}
// text-align
if (elem_css($elem, 'text-align') !== NULL) {
$obj['text-align'] = elem_css($elem, 'text-align');
} else {
unset($obj['text-align']);
}
// word-spacing
if (elem_css($elem, 'word-spacing') !== NULL) {
$obj['text-word-spacing'] = elem_css($elem, 'word-spacing');
} else {
unset($obj['text-word-spacing']);
}
// accessibility round-trip (SOW-accessibility.md): the serialized tag
// itself carries the heading level (html_parse_elem lowercases it)
$tag = elem_tag($elem);
if (in_array($tag, array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
$obj['text-heading-level'] = $tag;
} else {
unset($obj['text-heading-level']);
}
return true;
}
function text_alter_render_early($args)
{
$elem = &$args['elem'];
$obj = $args['obj'];
if (!elem_has_class($elem, 'text')) {
return false;
}
// accessibility (SOW-accessibility.md): a semantic heading level turns
// the wrapper into h1..h6. reset.css already neutralises the browser's
// heading defaults (margin, size, weight), and every visual property
// still comes from the author's own text-* keys below, so the change is
// structure-only - appearance is untouched and the author stays in
// control of how a heading looks.
if (!empty($obj['text-heading-level']) &&
in_array($obj['text-heading-level'], array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
$elem['tag'] = $obj['text-heading-level'];
}
// The text module deliberately ships no stylesheet, like the historical
// engine: a text object without stored text-font-size/text-padding-*
// keys renders with the page's inherited font-size and no padding, and
// links inside text inherit the main.css a-reset (no underline, no blue)
// exactly as they did in 2010 (parity A/B, F2/F3). The padding rows and
// the "change font size" control reset by clearing their own inline
// override, which then immediately falls back to that inherent default
// (no reload needed, and nothing to keep in sync here). The padding rows
// are the object properties panel's section, not a text control of their
// own, since 2026-09-16 - the storage is unchanged, which is why this file
// still owns the keys
// background-color
if (!empty($obj['text-background-color'])) {
elem_css($elem, 'background-color', $obj['text-background-color']);
}
// content
if (!isset($obj['content'])) {
$obj['content'] = '';
}
if ($args['edit']) {
// add a textarea
$i = elem('textarea');
elem_add_class($i, 'glue-text-input');
elem_css($i, 'width', '100%');
elem_css($i, 'height', '100%');
// hide the text area by default
elem_css($i, 'display', 'none');
// set the context to the textarea to the (unrendered) object content
$content = htmlspecialchars($obj['content'], ENT_NOQUOTES, 'UTF-8');
// replace newline characters by an entity to prevent render_object()
// from adding some indentation
$content = str_replace("\r\n", ' ', $content);
$content = str_replace("\n", ' ', $content);
// why not replace tabs as well why we are at it
$content = str_replace("\t", '	', $content);
elem_val($i, $content);
elem_append($elem, $i);
// and a nested div
$r = elem('div');
elem_add_class($r, 'glue-text-render');
elem_css($r, 'width', '100%');
elem_css($r, 'height', '100%');
// set the content of the div to the rendered object content
elem_val($r, _text_render_content($obj['content'], $obj['name']));
elem_append($elem, $r);
} else {
elem_append($elem, _text_render_content($obj['content'], $obj['name']));
}
// font-color
if (!empty($obj['text-font-color'])) {
elem_css($elem, 'color', $obj['text-font-color']);
}
// font-family
if (!empty($obj['text-font-family'])) {
elem_css($elem, 'font-family', $obj['text-font-family']);
if (TEXT_USE_WOFF_FONTS) {
if (_is_woff_font($obj['text-font-family'])) {
// include all styles of the font because of inline html
// (<strong>, etc)
_include_woff_font($obj['text-font-family']);
} elseif (_is_custom_font($obj['text-font-family'])) {
_include_custom_font($obj['text-font-family']);
}
}
}
// font-size
if (!empty($obj['text-font-size'])) {
elem_css($elem, 'font-size', $obj['text-font-size']);
}
// font-style
if (!empty($obj['text-font-style'])) {
elem_css($elem, 'font-style', $obj['text-font-style']);
}
// font-weight
if (!empty($obj['text-font-weight'])) {
elem_css($elem, 'font-weight', $obj['text-font-weight']);
}
// text-decoration (see text_alter_save)
if (!empty($obj['text-text-decoration'])) {
elem_css($elem, 'text-decoration', $obj['text-text-decoration']);
}
// a shadow behind the text - see .glue-text-shadow in css/main.css. The
// ingredients are stored and the shadow is composed there, so the value
// exists in one place rather than in the editor and the renderer both.
// It is on while it has a blur, a distance or a spread: a hard drop
// shadow is a distance with no blur (2026-09-28). A part that is absent
// is the rule's own default, and the spread draws its ring of copies
// only under its own class.
if (!empty($obj['text-shadow-radius']) || !empty($obj['text-shadow-distance'])
|| !empty($obj['text-shadow-spread'])) {
foreach (['radius', 'alpha', 'color', 'distance', 'angle', 'spread'] as $part) {
if (!empty($obj['text-shadow-'.$part])) {
elem_css($elem, '--glue-shadow-'.$part, $obj['text-shadow-'.$part]);
}
}
elem_add_class($elem, 'glue-text-shadow');
if (!empty($obj['text-shadow-spread'])) {
elem_add_class($elem, 'glue-text-shadow-spread');
}
}
// letter-spacing
if (!empty($obj['text-letter-spacing'])) {
elem_css($elem, 'letter-spacing', $obj['text-letter-spacing']);
}
// line-height
if (!empty($obj['text-line-height'])) {
elem_css($elem, 'line-height', $obj['text-line-height']);
}
// padding: the four longhands when the object was saved with per-side
// values ("more knobs" in the padding panel), the x/y pair as the
// fallback for objects saved the older way. A side that is in neither is
// left alone and falls back to the CSS class default.
if (!empty($obj['text-padding-left'])) {
elem_css($elem, 'padding-left', $obj['text-padding-left']);
}
if (!empty($obj['text-padding-right'])) {
elem_css($elem, 'padding-right', $obj['text-padding-right']);
}
if (empty($obj['text-padding-left']) && empty($obj['text-padding-right']) && !empty($obj['text-padding-x'])) {
elem_css($elem, 'padding-left', $obj['text-padding-x']);
elem_css($elem, 'padding-right', $obj['text-padding-x']);
}
if (!empty($obj['text-padding-top'])) {
elem_css($elem, 'padding-top', $obj['text-padding-top']);
}
if (!empty($obj['text-padding-bottom'])) {
elem_css($elem, 'padding-bottom', $obj['text-padding-bottom']);
}
if (empty($obj['text-padding-top']) && empty($obj['text-padding-bottom']) && !empty($obj['text-padding-y'])) {
elem_css($elem, 'padding-top', $obj['text-padding-y']);
elem_css($elem, 'padding-bottom', $obj['text-padding-y']);
}
// text-align
if (!empty($obj['text-align'])) {
elem_css($elem, 'text-align', $obj['text-align']);
}
// word-spacing
if (!empty($obj['text-word-spacing'])) {
elem_css($elem, 'word-spacing', $obj['text-word-spacing']);
}
return true;
}
function text_render_object($args)
{
$obj = $args['obj'];
if (!isset($obj['type']) || $obj['type'] != 'text') {
return false;
}
$e = elem('div');
elem_attr($e, 'id', $obj['name']);
elem_add_class($e, 'text');
elem_add_class($e, 'resizable');
elem_add_class($e, 'object');
// hooks
invoke_hook_first('alter_render_early', 'text', ['obj'=>$obj, 'elem'=>&$e, 'edit'=>$args['edit']]);
$html = elem_finalize($e);
invoke_hook_last('alter_render_late', 'text', ['obj'=>$obj, 'html'=>&$html, 'elem'=>$e, 'edit'=>$args['edit']]);
return $html;
}
function text_render_page_early($args)
{
if ($args['edit']) {
if (USE_MIN_FILES) {
html_add_js(base_url().'modules/text/text-edit.min.js');
} else {
html_add_js(base_url().'modules/text/text-edit.js');
}
html_add_css(base_url().'modules/text/text-edit.css');
html_add_js_var('$.glue.conf.text.auto_br', TEXT_AUTO_BR);
// last typeface/font size/line height picked via "change typeface"/
// "change font size"/"change line height" (site-wide, see
// page_set_last_font()/page_set_last_font_size()/
// page_set_last_line_height()) - applied as the default for newly
// created text objects, see text-edit.js's "new text" handler
$site_obj = site_options_load();
if (!$site_obj['#error'] && !empty($site_obj['#data']['page-last-text-font'])) {
html_add_js_var('$.glue.conf.text.last_font', $site_obj['#data']['page-last-text-font']);
}
if (!$site_obj['#error'] && !empty($site_obj['#data']['page-last-text-font-size'])) {
html_add_js_var('$.glue.conf.text.last_font_size', $site_obj['#data']['page-last-text-font-size']);
}
if (!$site_obj['#error'] && !empty($site_obj['#data']['page-last-text-line-height'])) {
html_add_js_var('$.glue.conf.text.last_line_height', $site_obj['#data']['page-last-text-line-height']);
}
if (TEXT_USE_WOFF_FONTS) {
// user-uploaded custom fonts (site settings, see /pages) go
// first in the "change typeface" cycle button's order - it
// discovers typefaces by scanning document.styleSheets for
// .glue-font* selectors in order (see text-edit.js's
// get_fonts()), so a lower priority here (5, vs 6 below)
// guarantees these sort first regardless of insertion order -
// usort() isn't guaranteed stable for equal priorities (see
// html.inc.php's _array_sort_by_prio())
foreach (site_custom_fonts() as $font) {
if (empty($font['name'])) {
continue;
}
_include_custom_font($font['name']);
$rule = '.glue-font-custom-'.$font['name'].' {'.nl();
$rule .= tab().'font-family: \''.$font['name'].'\';'.nl();
$rule .= '}';
html_add_css_inline($rule, 5);
}
$woff_fonts = _woff_fonts();
foreach ($woff_fonts as $font=>$styles) {
_include_woff_font($font);
// TODO (later): check css encoding
// the picker marker class takes a slug of the family name:
// the bundle's names carry spaces and digits (Press Start
// 2P), which a class selector cannot hold unescaped - the
// font-family inside the rule is still the real name, and
// that is what the picker reads and what gets stored
$slug = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $font));
$rule = '.glue-font-woff-'.$slug.' {'.nl();
// we use single quotes as they don't clash with inline styles
$rule .= tab().'font-family: \''.$font.'\';'.nl();
$rule .= '}';
html_add_css_inline($rule, 6);
}
}
}
}
function text_save_state($args)
{
$elem = $args['elem'];
$obj = $args['obj'];
if (get_first_item(elem_classes($elem)) != 'text') {
return false;
}
// make sure the type is set
$obj['type'] = 'text';
$obj['module'] = 'text';
// hook
invoke_hook('alter_save', ['obj'=>&$obj, 'elem'=>$elem]);
load_modules('glue');
$ret = save_object($obj);
if ($ret['#error']) {
load_msg('error', 'text_save_state: save_object returned '.quot($ret['#data']));
return false;
} else {
return true;
}
}