This repository was archived by the owner on May 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_truncate.php
More file actions
141 lines (134 loc) · 5.17 KB
/
smart_truncate.php
File metadata and controls
141 lines (134 loc) · 5.17 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
<?php
/*
* Automates the truncation of text for a given width based on the rendered font provided
* This type of truncation is very useful for truncating text that is rendered in a non-mono-spaced fonts.
* This class requires GD library in PHP
* The truncation performed in this class has been tuned for performance.
*
* For more information: http://github.com/mrinterweb/php-smart-truncate/tree/master
*
* STANDARD MIT LICENSE
*
* Copyright (c) <2009> <Sean McCleary (Swipht Technologies)>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
class SmartTruncate {
private $font, $points, $max_width, $m_width;
/**
* Constructor
* @param <string> $font path to TrueType font. Absolute paths are preferred
* @param <float> $points font size in points. Please convert font size to points
* @param <integer> $max_width target pixel width to truncate to
*/
public function SmartTruncate($font, $points, $max_width) {
if(!file_exists($font)) {
trigger_error('Font file not found');
}
$this->font = $font;
$this->points = $points;
$this->max_width = $max_width;
$this->m_width = $this->get_width('m');
}
/**
* Calculates the rendered width of the given string according to the font settings provided to the constructor.
* @param <string> $str
* @return <integer> Pixel width
*/
public function get_width($str) {
$char = false;
// the evaluated width of one character is not sufficient, more accurate to evaluate longer strings
if(strlen($str) == 1) {
$char = true;
$str = str_repeat($str, 20);
}
$d = imagettfbbox($this->points, 0, $this->font, $str);
$w = $d[2] - $d[0];
if($char) $w = $w / 20;
return $w;
}
/**
* Wrapper and public accessor for recursive_truncate
* This is the method you call when you want to use the truncator
* @param <string> $str target to truncate
* @param <string> $truncation_token '...', '…'
* @return <string>
*/
public function truncate($str, $truncation_token = '') {
$truncated = $this->recursive_truncate($str);
if($truncation_token && strlen($str) == strlen($truncated)) {
$truncation_token = '';
}
return $truncated . $truncation_token;
}
/**
* Recursive way of getting a truncated string
* @param <string> $first_chunk
* @param <string> $second_chunk
* @return <string> truncated string
*/
private function recursive_truncate($first_chunk, $second_chunk = '', $second_chunk_backup = '') {
if(!$second_chunk && $this->get_width($first_chunk) <= $this->max_width) {
# Nothing to do
return $first_chunk;
}
if(!$second_chunk) {
# split string in half
$chunks = str_split($first_chunk, ceil(strlen($first_chunk) / 2));
list($first_chunk, $second_chunk) = $chunks;
}
# determine if either half is longer than max_width if so throw out second half
$first_chunk_width = $this->get_width($first_chunk);
if($first_chunk_width > $this->max_width) {
# throw out second half and start over
return $this->recursive_truncate($first_chunk);
}
$second_chunk_width = $this->get_width($second_chunk);
if(
($first_chunk_width + $second_chunk_width) < $this->max_width &&
($first_chunk_width + $second_chunk_width + $this->m_width) > $this->max_width
) {
return $first_chunk . $second_chunk;
}
if(($combined_width = $this->get_width($first_chunk . $second_chunk)) < $this->max_width) {
# start itterative process to finish it off
$remaining_chars = substr($second_chunk_backup, - strlen($second_chunk));
$count = 0;
$max_count = strlen($second_chunk_backup);
$new_string = $first_chunk.$second_chunk;
while($combined_width < $this->max_width && $count < $max_count) {
$char = $remaining_chars[$count];
$char_width = $this->get_width($char);
$combined_width += $char_width;
if($combined_width <= $this->max_width) {
$new_string .= $char;
}
$count++;
}
return $new_string;
} else {
return $this->recursive_truncate($first_chunk, substr($second_chunk, 0, ceil(strlen($second_chunk) / 2)), $second_chunk);
}
}
}
?>