-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-ttgObject.php
executable file
·189 lines (129 loc) · 4.59 KB
/
class-ttgObject.php
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
<?php
// flameslater (c) 2017 Bob Maple (bobm at burner dot com)
//
// Licensed under the Creative Commons Attribution-ShareAlike (CC BY-SA)
// https://creativecommons.org/licenses/by-sa/4.0/
// class-ttgObject
//
// Does rudimentary handling of Flame text setups (.ttg) in a template
// context using a %Field Name% search and replace syntax
class ttgObject {
var $ttgData; // Array of the whole .ttg file
var $ttgFields; // Array of field names found in the template
function ttgObject( $ttg_file = FALSE ) {
$ttgFields = array();
if( $ttg_file )
$this->Load( $ttg_file );
}
//
function Load( $ttg_file ) {
if( $this->ttgData = file( $ttg_file, FILE_IGNORE_NEW_LINES ) )
return( TRUE );
else
return( FALSE );
}
function FindFields() {
// Scan the ttg for %Fields% and generate an array for each one containing
// the array index in the template for the field
foreach( $this->ttgData as $curKey => $curVal ) {
if( preg_match( "/^Text.* 37 (.*) 37( |$)/", $curVal, $matches ) ) {
$field_name = $this->ASCIItoSTR( $matches[1] );
$this->ttgFields[$field_name]['line'] = $curKey;
}
}
if( count( $this->ttgFields ) )
return( TRUE );
else
return( FALSE );
}
function GetTTG() {
// Return a multiline string of the complete .ttg
return( implode( "\n", $this->ttgData ) );
}
function ReplaceField( $ttg_field, $string ) {
// Replace the $ttg_field in the template with $string
$ttg_key = $this->ttgFields[$ttg_field]['line'];
$this->ttgData[$ttg_key] = "Text " . $this->UTFtoDEC( $string, $codecount );
$this->ttgData[$ttg_key-1] = "TextLength " . $codecount;
}
function ASCIItoSTR( $ascii ) {
// Decode a string of space-separated ASCII codes into a normal string
// Not UTF8-aware - field names should be kept simple for now
$codes = explode( " ", trim( $ascii ) );
$string = "";
foreach( $codes as $curcode ) {
$string .= chr( $curcode );
}
return( $string );
}
function UTFtoDEC( $string, &$codecount ) {
// Decode a (possibly) UTF8-encoded string to a string of space-separated decimal codes
$utfcodes = array();
for( $i = 0; $i < strlen( $string ); ) {
$utfbytes = 1;
if( ($string[$i] & "\xE0") === "\xC0" ) $utfbytes = 2;
else if( ($string[$i] & "\xF0") === "\xE0" ) $utfbytes = 3;
else if( ($string[$i] & "\xF8") === "\xF0" ) $utfbytes = 4;
switch( $utfbytes ) {
case 1: $utfcodes[] = ord( $string[$i] );
$i++;
break;
case 2: $utfcodes[] = (( ord( $string[$i] ) & 0x1F) << 6) | (ord( $string[$i+1] ) & 0x3F);
$i += 2;
break;
case 3: $utfcodes[] = ((ord( $string[$i] ) & 0x0F) << 12) | ((ord( $string[$i+1] ) & 0x3F) << 6) | (ord( $string[$i+2] ) & 0x3F);
$i += 3;
break;
case 3: $utfcodes[] = ((ord( $chr[0] ) & 0x07) << 18) | ((ord( $chr[1] ) & 0x3F) << 12) | ((ord( $chr[2] ) & 0x3F) << 6) | (ord( $chr[3] ) & 0x3F);
$i += 4;
}
}
$codecount = count( $utfcodes );
return( implode( " ", $utfcodes ) );
}
/* function STRtoFlameText( $string ) {
// Takes a normal string and returns a multiline string of Kern, TextLength and Text commands
// to create the string in a Flame text setup file. Applies kerning pairs from kernpairs.php
global $kernpairs;
$curkern = 99;
$curline = "";
$flametext = array();
//$flametext[] = "Kern 0";
debugstr( "--- Making Flame string for [$string]" );
for( $i = 0; $i < strlen( $string ); $i++ ) {
// See if this character needs a kern tweak
if( isset( $kernpairs[$string{$i}][$string{($i+1)}] ) ) {
$kernval = $kernpairs[$string{$i}][$string{$i+1}];
debugstr( "Found kerning pair for " . $string{$i} . " and " . $string{$i+1} . " of $kernval" );
}
else {
// This character has no kerning pair def, so
// we reset to 0
debugstr( "Resetting kernval to 0" );
$kernval = 0;
}
if( $curkern != $kernval ) {
// Output what we have so far, if anything
if( strlen( $curline ) ) {
debugstr( "Flushing buffer" );
$flametext[] = "TextLength " . strlen( $curline );
$flametext[] = "Text " . strtoascii( $curline );
$curline = "";
}
debugstr( "Creating Kern line of $kernval" );
$curkern = $kernval;
$flametext[] = "Kern " . $kernval;
}
// Add character to the buffer
$curline .= $string[$i];
debugstr( "Adding [" . $string[$i] . "] to buffer" );
debugstr( " curline is now [$curline]" );
}
if( strlen( $curline ) ) {
debugstr( "Flushing buffer final" );
$flametext[] = "TextLength " . strlen( $curline );
$flametext[] = "Text " . strtoascii( $curline );
}
return( implode( "\n", $flametext ) );
} */
}