Created
March 18, 2011 12:53
-
-
Save mindplay-dk/876016 to your computer and use it in GitHub Desktop.
A drop-in replacement for wordwrap() for proportional fonts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Proportional word-wrap (approximation) | |
* | |
* Attempts to work as a drop-in replacement for wordwrap() | |
* | |
* Based on http://www.php.net/manual/en/function.wordwrap.php#82580 | |
*/ | |
function pwordwrap($str, $width=75, $break="\n") | |
{ | |
$width *= 0.4; | |
static $charwidth = array( | |
0 => 0.1847, | |
1 => 0.2753, | |
2 => 0.3659, | |
3 => 0.4565, | |
4 => 0.5471, | |
5 => 0.6377, | |
6 => 0.7282, | |
7 => 0.9094, | |
8 => 1, | |
); | |
static $charset = array( | |
33 => 0, # ! | |
34 => 2, # " | |
35 => 4, # # | |
36 => 4, # $ | |
37 => 7, # % | |
38 => 5, # & | |
39 => 0, # ' | |
40 => 2, # ( | |
41 => 2, # ) | |
42 => 2, # * | |
43 => 4, # + | |
44 => 1, # , | |
45 => 2, # - | |
46 => 1, # . | |
47 => 1, # / | |
48 => 4, # 0 | |
49 => 4, # 1 | |
50 => 4, # 2 | |
51 => 4, # 3 | |
52 => 4, # 4 | |
53 => 4, # 5 | |
54 => 4, # 6 | |
55 => 4, # 7 | |
56 => 4, # 8 | |
57 => 4, # 9 | |
58 => 1, # : | |
59 => 1, # ; | |
60 => 4, # < | |
61 => 4, # = | |
62 => 4, # > | |
63 => 4, # ? | |
64 => 8, # @ | |
65 => 6, # A | |
66 => 5, # B | |
67 => 5, # C | |
68 => 5, # D | |
69 => 4, # E | |
70 => 4, # F | |
71 => 6, # G | |
72 => 5, # H | |
73 => 0, # I | |
74 => 3, # J | |
75 => 5, # K | |
76 => 4, # L | |
77 => 6, # M | |
78 => 5, # N | |
79 => 6, # O | |
80 => 4, # P | |
81 => 6, # Q | |
82 => 5, # R | |
83 => 5, # S | |
84 => 4, # T | |
85 => 5, # U | |
86 => 6, # V | |
87 => 7, # W | |
88 => 5, # X | |
89 => 6, # Y | |
90 => 5, # Z | |
91 => 1, # [ | |
92 => 1, # \ | |
93 => 1, # ] | |
94 => 3, # ^ | |
95 => 4, # _ | |
96 => 2, # ` | |
97 => 4, # a | |
98 => 4, # b | |
99 => 4, # c | |
100 => 4, # d | |
101 => 4, # e | |
102 => 2, # f | |
103 => 4, # g | |
104 => 4, # h | |
105 => 0, # i | |
106 => 0, # j | |
107 => 3, # k | |
108 => 0, # l | |
109 => 6, # m | |
110 => 4, # n | |
111 => 4, # o | |
112 => 4, # p | |
113 => 4, # q | |
114 => 2, # r | |
115 => 4, # s | |
116 => 1, # t | |
117 => 4, # u | |
118 => 4, # v | |
119 => 7, # w | |
120 => 4, # x | |
121 => 4, # y | |
122 => 4, # z | |
123 => 2, # { | |
124 => 0, # | | |
125 => 2, # } | |
126 => 4, # ~ | |
); | |
$lines = array(); | |
$line = ''; | |
$word = ''; | |
$wordw = 0; | |
$linew = 0; | |
for ($i=0; $i<strlen($str); $i++) | |
{ | |
$char = substr($str, $i, 1); | |
if ($char === ' ') | |
{ | |
$word = trim($word); | |
if (strlen($word)) | |
{ | |
if ($linew + $wordw >= $width) | |
{ | |
$lines[] = $line; | |
$line = $word; | |
$linew = $wordw; | |
} | |
else | |
{ | |
$line .= (strlen($line) ? ' ' : '') . $word; | |
$linew += $wordw; | |
} | |
} | |
$word = ''; | |
$wordw = 0; | |
} | |
else if ($char == "\n") | |
{ | |
$lines[] = $line . ' ' . $word; | |
$line = ''; | |
$linew = 0; | |
$word = ''; | |
$wordw = 0; | |
} | |
else | |
{ | |
$word .= $char; | |
$wordw += @$charwidth[$charset[ord($char)]]; | |
} | |
} | |
if (strlen($word)) | |
$line .= ' '.$word; | |
if (strlen($line)) | |
$lines[] = $line; | |
return implode($break, $lines); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment