-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCharacterize.php
165 lines (123 loc) · 3.33 KB
/
Characterize.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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Characterize{
private $rows;
private $max_length;
public function __construct()
{
}
public function init($max_length = 40, $is_print = false)
{
$this->rows = array();
$this->max_length = $max_length;
return $this;
}
public function make($text = '', $format = '', $size = null, $font_mode = null)
{
if(is_array($text)){
$column = count($text);
$data = array(
'type' => 'column',
'data' => $text,
'column' => $column
);
array_push($this->rows, $data);
}else{
$data = array(
'type' => 'row',
'text' => $text,
'format' => $format,
'font_mode' => $font_mode,
);
array_push($this->rows, $data);
}
return $this;
}
public function render(){
$finaldata = array();
foreach($this->rows as $a){
if($a['type'] == 'column'){
$final = $this->chunk_format($a['data'], $a['column']);
}else{
$prop = $this->get_properties($a['text']);
$final = $this->format($a['format'], $prop, $a['font_mode']);
}
// FINAL CHECK
$prop = $this->get_properties($final);
$final = $this->format('left', $prop);
array_push($finaldata, $final);
}
return $finaldata;
}
public function spacer($string = null)
{
if(!is_null($string)){
$this->make(str_repeat($string, $this->max_length));
}else{
$this->make(' ');
}
}
public function chunk_format($data, $column)
{
$final = "";
foreach($data as $d){
$prop = $this->get_properties($d[0], $d[2]);
$final .= $this->format($d[1], $prop);
}
return $final;
}
public function get_properties($text, $size = null)
{
// GET MAX LENGTH
$prop['max_length'] = $this->max_length;
if(!is_null($size)){
$prop['max_length'] = $size;
}
// GET TEXT
$prop['text'] = $text;
// GET TEXT LENGTH
$prop['strlen'] = strlen($text);
// GET HALF TEXT LENGTH
$prop['strlen_half'] = ($prop['strlen']/2);
// GET REDUCTION WITH MAX LENGTH
$prop['strlen_reduction_max'] = $prop['max_length'] - $prop['strlen'];
// GET DIVISION COLUMN WITH MAX LENGTH BASED ON TEXT LENGTH (DCMLT)
$prop['dcmlt'] = ($prop['max_length'] / $prop['strlen']);
// GET HALF DCLMT
$prop['dcmlt_halft'] = ($prop['dcmlt'] / 2);
// GET OFFSET POSITION
$prop['offset_pos'] = $prop['dcmlt_halft'] * $prop['strlen'];
// GET CENTER POSITION
$prop['center_pos'] = $prop['offset_pos'] - $prop['strlen_half'];
// print_r($prop);
return $prop;
}
public function format($format, $prop, $font_mode = null)
{
switch($format){
case 'center':
$push = $prop['center_pos'];
break;
case 'right':
$push = $prop['strlen_reduction_max'];
break;
default:
$push = 0;
break;
}
// GET TEXT LENGTH + SPACER LENGTH
$prop['strlen_spacer'] = $prop['strlen']+$push;
$prop['rest_spacer'] = $prop['max_length'] - $prop['strlen_spacer'];
$spacer_left = "";
if($push > 0){
$spacer_left = str_repeat(" ", $push);
}
$spacer_right = "";
if($prop['rest_spacer'] > 0){
$spacer_right = str_repeat(" ", $prop['rest_spacer']);
}
$result = $spacer_left.$prop['text'].$spacer_right;
$final = substr($result, 0, $prop['max_length']);
return $final;
}
}