forked from Slevinski/swis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsw.php
More file actions
546 lines (495 loc) · 12.9 KB
/
bsw.php
File metadata and controls
546 lines (495 loc) · 12.9 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
<?php
/**
* Binary SignWriting Library
*
* This file is part of SWIS: the SignWriting Icon Server.
*
* SWIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SWIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SWIS. If not, see <http://www.gnu.org/licenses/>.
*
* END Copyright
*
* @copyright 2007-2012 Stephen E Slevinski Jr
* @author Steve Slevinski (slevin@signpuddle.net)
* @version 1
* @section License
* GPL 3, http://www.opensource.org/licenses/gpl-3.0.html
* @brief Binary SignWriting encoding for symbols, numbers, and markers
* @file
*
*/
//include 'filesystem.php';
include 'database.php';
/** @defgroup bsw Binary SignWriting
* Encoding for symbols, numbers, and markers
*/
/** @defgroup key Symbol Key
* @ingroup bsw
* Symbol key is a subsection of the ISWA
*/
/** @defgroup range Symbol Ranges
* @ingroup bsw
* Symbol Ranges is a subsection of the ISWA
*/
/** @defgroup str BSW Strings
* @ingroup bsw
* Binary SignWriting string functions
*/
/** @defgroup num Numbers
* @ingroup bsw
* Number functions
*/
/**
* @brief symbol base to key for viewing
* @param $base symbol key prefix
* @return a symbol key for viewing
* @ingroup key
*/
function base2view($base){
$view = $base . '00';
if (isHand($base)){
if(!isSymGrp($base)){
$view = $base . '10';
}
}
return $view;
}
/**
* @brief returns the symbol group for a base or key
* @param $base symbol key prefix
* @return the key prefix for a group
* @ingroup key
*/
function base2group($base){
$base = substr($base,0,3);
$sg_list = array('100','10e','11e','144','14c','186','1a4','1ba','1cd','1f5', '205','216','22a','255','265','288','2a6','2b7','2d5','2e3', '2f7', '2ff','30a','32a','33b','359', '36d','376', '37f', '387');
foreach ($sg_list as $group){
if (hexdec($base)==hexdec($group)) return $group;
if (hexdec($base)<hexdec($group)) return $prev;
$prev = $group;
}
return $group;
}
/**
* List of key prefixes of the 30 groups
* @ingroup key
*/
$sg_list = array('100','10e','11e','144','14c','186','1a4','1ba','1cd','1f5', '205','216','22a','255','265','288','2a6','2b7','2d5','2e3', '2f7', '2ff','30a','32a','33b','359', '36d','376', '37f', '387');
/**
* @brief symbol base to key for viewing
* @param $base symbol key prefix
* @return boolean value if the key prefix is a group
* @ingroup key
*/
function isSymGrp($char){
$sg_list = array('100','10e','11e','144','14c','186','1a4','1ba','1cd','1f5', '205','216','22a','255','265','288','2a6','2b7','2d5','2e3', '2f7', '2ff','30a','32a','33b','359', '36d','376', '37f', '387');
return in_array($char,$sg_list);
}
/**
* List of key prefixes of the 7 categories
* @ingroup key
*/
$cat_list = array('100','205','2f7', '2ff','36d','37f','387');
/**
* @brief returns the symbol category for a base or key
* @param $base symbol key prefix
* @return the key prefix for a category
* @ingroup key
*/
function base2cat($base){
$cat_list = array('100','205','2f7', '2ff','36d','37f','387');
foreach ($cat_list as $cat){
if (hexdec($base)==hexdec($cat)) return $cat;
if (hexdec($base)<hexdec($cat)) return $prev;
$prev = $cat;
}
return $cat;
}
/**
* Global variable that contains BaseSymbol information
* @ingroup key
*/
$BaseSymbols = array();
/**
* @brief test if key is properly formatted
* @param $key symbol key
* @return boolean value if the key is properly formatted
* @ingroup key
*/
function isKey($key){
if ($key=='') return false;
$re_sym = 'S?[123][a-f0-9]{2}[012345][a-f0-9]';
$re_pattern = '/^' . $re_sym . '$/i';
$result = preg_match($re_pattern,$key,$matches);
if ($result) {
if ($key == $matches[0]) {
return true;
}
}
return false;
}
/**
* @brief test if key references a valid symbol
* @param $key symbol key
* @return boolean value if the key is a valid symbol
* @ingroup key
*/
function validKey($key){
if (!isKey($key)) return false;
global $BaseSymbols;
if (count($BaseSymbols)==0) $BaseSymbols = loadBaseSymbols();
$key = str_replace("S","",$key);
$len = strlen($key);
if ($len<3){ return false ;}//error
$hcode = substr($key,0,3);
$bs = $BaseSymbols[hexdec($hcode)];
if (!$bs){return false;}
$df = 0;
if ($len>3) $df = hexdec(substr($key,3,1));
$dr = 0;
if ($len>4) $dr = hexdec(substr($key,4,1));
//now for binary algebra!
$fillbin = pow(2,$df);
$bFill = $fillbin & $bs['fills'];
$rotbin = pow(2,$dr);
$bRot = $rotbin & $bs['rots'];
return $bFill and $bRot;
}
/**
* @brief returns the 16-bit symbol code of a key
* @param $key symbol key
* @return 16 bit symbol code
* @ingroup key
*/
function key2code($key){
$key = str_replace('S','',$key);
return ((hexdec(substr($key,0,3)) - 256) * 96) + ((hexdec(substr($key,3,1)))*16) + hexdec(substr($key,4,1))+1;
}
/**
* @brief returns the symbol key of a 16-bit symbol code
* @param $code 16-bit symbol code
* @return symbol key
* @ingroup key
*/
function code2key($code){
$ode = $code;
$base = intval($code/96);
$code = $code - $base*96;
$fill = intval(($code-1)/16);
$rot = $code - ($fill*16);
if ($fill==0 && $rot==0) {
$base--;
$fill=5;
$rot=16;
}
return dechex($base+256) . dechex($fill) . dechex($rot-1);
}
/**
* @brief test if symbol base is in range
* @param $start start of hex range
* @param $end end of hex range
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function inHexRange($start, $end, $char){
$char = substr(str_replace('S','',$char),0,3);
if (hexdec($char)>=hexdec($start) and hexdec($char)<=hexdec($end)){
return true;
} else {
return false;
}
}
/**
* @brief test if symbol base is in the ISWA
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isISWA($char){
return inHexRange("100","38b",$char);
}
/**
* @brief test if symbol base is a writing symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isWrit($char){
return inHexRange("100","37e",$char);
}
/**
* @brief test if symbol base is a hand symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isHand($char){
return inHexRange("100","204",$char);
}
/**
* @brief test if symbol base is a movement symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isMove($char){
return inHexRange("205","2f6",$char);
}
/**
* @brief test if symbol base is a dynamics symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isDyn($char){
return inHexRange("2f7","2fe",$char);
}
/**
* @brief test if symbol base is a head symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isHead($char){
return inHexRange("2ff","36c",$char);
}
/**
* @brief test if symbol base is a head symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function hasHead($text){
$re_sym = 'S(2ff|3[0-5][0-9a-f]|36[0-9a-c])';
$re_pattern = '/' . $re_sym . '/i';
$result = preg_match_all($re_pattern,$text,$matches);
return count($matches[0])>0;
}
/**
* @brief test if symbol base is a trunk symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isTrunk($char){
return inHexRange("36d","375",$char);
}
/**
* @brief test if symbol base is a limb symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isLimb($char){
return inHexRange("376","37e",$char);
}
/**
* @brief test if symbol base is a detailed location symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isLoc($char){
return inHexRange("37f","386",$char);
}
/**
* @brief test if symbol base is a punctuation symbol
* @param $char hex string to test in range
* @return boolean value
* @ingroup range
*/
function isPunc($char){
return inHexRange("387","38b",$char);
}
/**
* @brief returns the fill value of a bsw fill char
* @param $char bsw string of a fill value
* @return fill value
* @ingroup str
*/
function char2fill($char){
return dechex(hexdec($char)-hexdec('110'));
}
/**
* @brief returns the base fill char of a fill value
* @param $fill fill value
* @return bsw fill char
* @ingroup str
*/
function fill2char($fill){
return dechex(hexdec($fill)+hexdec('110'));
}
/**
* @brief returns the rotation value of a bsw rotation char
* @param $char bsw string of a rotation value
* @return rotation value
* @ingroup str
*/
function char2rot($char){
return dechex(hexdec($char)-hexdec('120'));
}
/**
* @brief returns the base rotation char of a rotation value
* @param $rotation rotation value
* @return bsw rotation char
* @ingroup str
*/
function rot2char($rot){
return dechex(hexdec($rot)+hexdec('120'));
}
/**
* @brief returns the bsw string of a symbol key
* @param $key symbol key
* @return bsw string
* @ingroup str
*/
function key2bsw($key){
$key = str_replace('S','',$key);
$base = dechex(hexdec(substr($key,0,3)) + hexdec('30'));
$fill = substr($key,3,1);
$rot = substr($key,4,1);
return $base . fill2char($fill) . rot2char($rot);
}
/**
* @brief returns the symbol key of a bsw string
* @param $bsw bsw string
* @return symbol key
* @ingroup str
*/
function bsw2key($bsw){
$bsw = str_replace(' ','',$bsw);
$bsw = str_replace('_','',$bsw);
$base = dechex(hexdec(substr($bsw,0,3)) - hexdec('30'));
$fill = substr($bsw,3,3);
$rot = substr($bsw,6,3);
return $base . char2fill($fill) . char2rot($rot);
}
/**
* @brief test if text is Binary SignWriting
* @param $text character string
* @return boolean value if text is Binary SignWriting with number characters
* @ingroup str
*/
function bswText($text){
$bsw_sym = '[123][0-9a-f]{2}11[0-5]12[0-9a-f]';
$bsw_coord = '([78][0-9a-f]{2}){2}';
$bsw_word = '(100(' . $bsw_sym. ')+)?10[1-4](' . $bsw_coord . ')(' . $bsw_sym . $bsw_coord . ')*';
$bsw_punc = '3b[7-9ab]11[0-5]12[0-9a-f]' . $bsw_coord;
$bsw_pattern = '/^(' . $bsw_word . '|' . $bsw_punc . ')( ' . $bsw_word . '| ' . $bsw_punc .')*$/i';
$result = preg_match($bsw_pattern,$text,$matches);
if ($result) {
if ($text == $matches[0]) {
return true;
}
}
return false;
}
/**
* @brief x,y values to irregular coordinate string
* @param $x X value
* @param $y Y value
* @return a coordinate string centered on 0x0
* @ingroup num
*/
function koord2str($x,$y){
$str = '';
if ($x<0) $str .= 'n';
$str .= abs($x);
$str .= 'x';
if ($y<0) $str .= 'n';
$str .= abs($y);
return $str;
}
/**
* @brief irregular coordinate string to array of x,y values
* @param $str coordinate string centered on 0x0
* @return an array of x,y values
* @ingroup num
*/
function str2koord($str) {
if (!$str) return array(0,0);
$str = str_replace('n','-',$str);
$parts = explode('x',$str);
$coord = array();
foreach ($parts as $part){
$coord[] = $part;
}
return $coord;
}
/**
* @brief x,y values to regular coordinate string
* @param $x X value
* @param $y Y value
* @return a coordinate string centered on 500x500
* @ingroup num
*/
function coord2str($x,$y){
$str = '';
$str .= $x + 500;
$str .= 'x';
$str .= $y + 500;
return $str;
}
/**
* @brief regular coordinate string to array of x,y values
* @param $str coordinate string centered on 500x500
* @return an array of x,y values
* @ingroup num
*/
function str2coord($str) {
if (!$str) return array(0,0);
$parts = explode('x',$str);
$coord = array();
foreach ($parts as $part){
$coord[] = $part -500;
}
return $coord;
}
/**
* @brief number value to BSW character
* @param $num number from -250 to 249
* @return BSW character
* @ingroup num
*/
function num2bsw($num) {
return dechex($num + hexdec('800'));
}
/**
* @brief BSW character to number value
* @param $bsw BSW Number character
* @return number from -250 to 249
* @ingroup num
*/
function bsw2num($bsw) {
return hexdec($bsw) - hexdec('800');
}
/**
* @brief coordinate array to BSW string
* @param $arr array of 0 based x,y values
* @return BSW String
* @ingroup num
*/
function coord2bsw($arr) {
return num2bsw($arr[0]) . num2bsw($arr[1]);
}
/**
* @brief BSW string to coordinate array
* @param $bsw string of 2 bsw numbers
* @return coordinate array
* @ingroup num
*/
function bsw2coord($bsw) {
return array(bsw2num(substr($bsw,0,3)),bsw2num(substr($bsw,3,3)));
}
?>