Lettertype
Is het mogelijk om met php te controleren of er een speciaal lettertype op de coputer is geinstalleerd?
Mvg,
Rogier.
Nee. PHP is serverside.
Dat kan je dan in een sessie zetten en eventueel de pagina verversen indien het lettertype niet ondersteund wordt.
Ik werk aan een voorbeeld.
Dat is mooi!
Vul zelf $fonts = 'var fonts ... in
index.php
Code (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
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
<?php
session_start();
$_GET['p'] = (!empty($_GET['p']) ? $_GET['p'] : '');
switch ($_GET['p'])
{
case 'get_fonts':
// kijk wat er in $_GET['font'] zit
// to do: wat je wil aanvangen met de lettertypes die al dan niet kunnen gebruikt worden.
// bv. $_SESSION['fonts'] = $_GET['font']; ...
echo '<pre>'. print_r($_GET['font'], 1) .'</pre>';
break;
default :
$fonts = 'var fonts = Array("arial", "COMIC SANS MS", "Shocard Rodeo", "cursive");'.PHP_EOL;
echo'
<html>
<head>
<script src="ajax.js" language="javascript" type="text/javascript"></script>
<script src="js.js" language="javascript" type="text/javascript"></script>
<script language="javascript">
'. $fonts .'
</script>
</head>
<body>
<div id="message"></div>
</body onload="onload()">
</html>';
break;
}
?>
session_start();
$_GET['p'] = (!empty($_GET['p']) ? $_GET['p'] : '');
switch ($_GET['p'])
{
case 'get_fonts':
// kijk wat er in $_GET['font'] zit
// to do: wat je wil aanvangen met de lettertypes die al dan niet kunnen gebruikt worden.
// bv. $_SESSION['fonts'] = $_GET['font']; ...
echo '<pre>'. print_r($_GET['font'], 1) .'</pre>';
break;
default :
$fonts = 'var fonts = Array("arial", "COMIC SANS MS", "Shocard Rodeo", "cursive");'.PHP_EOL;
echo'
<html>
<head>
<script src="ajax.js" language="javascript" type="text/javascript"></script>
<script src="js.js" language="javascript" type="text/javascript"></script>
<script language="javascript">
'. $fonts .'
</script>
</head>
<body>
<div id="message"></div>
</body onload="onload()">
</html>';
break;
}
?>
js.js
Code (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
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
var http = new ajaxObject();
function onload()
{
var d = new Detector();
var b = Array();
var font = '';
if (fonts.length > 0 )
{
for (var i = 0; i < fonts.length; i++)
{
b[i] = d.test(fonts[i]);
if(b[i] == true)
{
if (font != '')
font +='&';
font += 'font['+ i +']='+ fonts[i];
}
}
}
get_fonts(font);
}
function get_fonts(fonts)
{
http.send(
'index.php?p=get_fonts&'+ fonts, // url
response_fonts, // callback
'get', // get / post
null // string of variables, eg. 'id='+ escape(id) +'&action=delete';
);
}
function response_fonts()
{
var response = http.response('text');
if ( response != null )
{
document.getElementById("message").innerHTML = response;
}
}
// code aangepast van ( zie hier onder )
/**
* JavaScript code to detect available availability of a
* particular font in a browser using JavaScript and CSS.
*
* Author : Lalit Patel
* Website: http://www.lalit.org/lab/jsoncookies
* License: Creative Commons Attribution-ShareAlike 2.5
* http://creativecommons.org/licenses/by-sa/2.5/
* Version: 0.15
* changed comparision font to serif from sans-serif,
* as in FF3.0 font of child element didn't fallback
* to parent element if the font is missing.
* Updated: 09 July 2009 10:52pm
*
*/
/**
* Actual function that does all the work. Returns an array with all the info.
* This test will fail for the font set as the default serif font.
*
* Usage: d = new Detector();
* d.test('font_name');
*/
function Detector()
{
this.h = document.getElementsByTagName("body")[0];
this.d = document.createElement("div");
this.s = document.createElement("span");
this.d.appendChild(this.s);
this.d.style.fontFamily = "sans"; //font for the parent element DIV.
this.s.style.fontFamily = "sans"; //serif font used as a comparator.
this.s.style.fontSize = "72px"; //we test using 72px font size, we may use any size. I guess larger the better.
this.s.innerHTML = "mmmmmmmmmmlil"; //we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
this.h.appendChild(this.d);
this.defaultWidth = this.s.offsetWidth; //now we have the defaultWidth
this.defaultHeight = this.s.offsetHeight; //and the defaultHeight, we compare other fonts with these.
this.h.removeChild(this.d);
//this.detailedTest = debug;
//this.test = test;
}
Detector.prototype.debug = function (font)
{
this.h.appendChild(this.d);
var f = [];
f[0] = this.s.style.fontFamily = font; // Name of the font
f[1] = this.s.offsetWidth; // Width
f[2] = this.s.offsetHeight; // Height
this.h.removeChild(this.d);
font = font.toLowerCase();
if (font == "serif") {
f[3] = true; // to set arial and sans-serif true
} else {
f[3] = (f[1] != this.defaultWidth || f[2] != this.defaultHeight); // Detected?
}
return f;
}
Detector.prototype.test = function (font)
{
/* test
* params:
* font - name of the font you wish to detect
* return:
* f[0] - Input font name.
* f[1] - Computed width.
* f[2] - Computed height.
* f[3] - Detected? (true/false).
*/
var f = this.debug(font);
return f[3];
}
function onload()
{
var d = new Detector();
var b = Array();
var font = '';
if (fonts.length > 0 )
{
for (var i = 0; i < fonts.length; i++)
{
b[i] = d.test(fonts[i]);
if(b[i] == true)
{
if (font != '')
font +='&';
font += 'font['+ i +']='+ fonts[i];
}
}
}
get_fonts(font);
}
function get_fonts(fonts)
{
http.send(
'index.php?p=get_fonts&'+ fonts, // url
response_fonts, // callback
'get', // get / post
null // string of variables, eg. 'id='+ escape(id) +'&action=delete';
);
}
function response_fonts()
{
var response = http.response('text');
if ( response != null )
{
document.getElementById("message").innerHTML = response;
}
}
// code aangepast van ( zie hier onder )
/**
* JavaScript code to detect available availability of a
* particular font in a browser using JavaScript and CSS.
*
* Author : Lalit Patel
* Website: http://www.lalit.org/lab/jsoncookies
* License: Creative Commons Attribution-ShareAlike 2.5
* http://creativecommons.org/licenses/by-sa/2.5/
* Version: 0.15
* changed comparision font to serif from sans-serif,
* as in FF3.0 font of child element didn't fallback
* to parent element if the font is missing.
* Updated: 09 July 2009 10:52pm
*
*/
/**
* Actual function that does all the work. Returns an array with all the info.
* This test will fail for the font set as the default serif font.
*
* Usage: d = new Detector();
* d.test('font_name');
*/
function Detector()
{
this.h = document.getElementsByTagName("body")[0];
this.d = document.createElement("div");
this.s = document.createElement("span");
this.d.appendChild(this.s);
this.d.style.fontFamily = "sans"; //font for the parent element DIV.
this.s.style.fontFamily = "sans"; //serif font used as a comparator.
this.s.style.fontSize = "72px"; //we test using 72px font size, we may use any size. I guess larger the better.
this.s.innerHTML = "mmmmmmmmmmlil"; //we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
this.h.appendChild(this.d);
this.defaultWidth = this.s.offsetWidth; //now we have the defaultWidth
this.defaultHeight = this.s.offsetHeight; //and the defaultHeight, we compare other fonts with these.
this.h.removeChild(this.d);
//this.detailedTest = debug;
//this.test = test;
}
Detector.prototype.debug = function (font)
{
this.h.appendChild(this.d);
var f = [];
f[0] = this.s.style.fontFamily = font; // Name of the font
f[1] = this.s.offsetWidth; // Width
f[2] = this.s.offsetHeight; // Height
this.h.removeChild(this.d);
font = font.toLowerCase();
if (font == "serif") {
f[3] = true; // to set arial and sans-serif true
} else {
f[3] = (f[1] != this.defaultWidth || f[2] != this.defaultHeight); // Detected?
}
return f;
}
Detector.prototype.test = function (font)
{
/* test
* params:
* font - name of the font you wish to detect
* return:
* f[0] - Input font name.
* f[1] - Computed width.
* f[2] - Computed height.
* f[3] - Detected? (true/false).
*/
var f = this.debug(font);
return f[3];
}
ajax.js
Code (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
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
function ajaxObject()
{
this.http = createHTTPHandler();
}
ajaxObject.prototype.send = function(url, response, method, passData)
{
if (method == 'post')
{
this.http.open("POST", url, true);
this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.http.onreadystatechange = response;
this.http.send( passData );
}
else // default GET
{
this.http.open("GET", url, true);
this.http.onreadystatechange = response;
this.http.send(null);
}
}
ajaxObject.prototype.response = function(type)
{
try
{
if (this.http.readyState == 4)
{
switch (type)
{
default:
return this.http.responseText;
break;
case 'json':
return eval('(' + this.http.responseText + ')');
break;
case 'xml':
return this.http.responseXml;
break;
}
}
else
return null;
}
catch(e)
{
}
return null;
}
function createHTTPHandler()
{
httphandler = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest)
{
try
{
httphandler = new XMLHttpRequest();
} catch(e)
{
httphandler = false;
}
// branch for IE/Windows ActiveX version
}
else if(window.ActiveXObject)
{
try
{
httphandler = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e)
{
try
{
httphandler = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e)
{
httphandler = false;
}
}
}
return httphandler;
}
{
this.http = createHTTPHandler();
}
ajaxObject.prototype.send = function(url, response, method, passData)
{
if (method == 'post')
{
this.http.open("POST", url, true);
this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.http.onreadystatechange = response;
this.http.send( passData );
}
else // default GET
{
this.http.open("GET", url, true);
this.http.onreadystatechange = response;
this.http.send(null);
}
}
ajaxObject.prototype.response = function(type)
{
try
{
if (this.http.readyState == 4)
{
switch (type)
{
default:
return this.http.responseText;
break;
case 'json':
return eval('(' + this.http.responseText + ')');
break;
case 'xml':
return this.http.responseXml;
break;
}
}
else
return null;
}
catch(e)
{
}
return null;
}
function createHTTPHandler()
{
httphandler = false;
// branch for native XMLHttpRequest object
if(window.XMLHttpRequest)
{
try
{
httphandler = new XMLHttpRequest();
} catch(e)
{
httphandler = false;
}
// branch for IE/Windows ActiveX version
}
else if(window.ActiveXObject)
{
try
{
httphandler = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e)
{
try
{
httphandler = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e)
{
httphandler = false;
}
}
}
return httphandler;
}
Bij mij is $_GET['font'][2] null. "Shocard Rodeo" is dus niet geïnstalleerd bij mij.
Gewijzigd op 01/01/1970 01:00:00 door Emmanuel Delay