Get the width or height of text in pixels
/*
==================================
= ENGLISH =
==================================
Date: Wed Sep 14 00:56:18 2005
UPDATED:
- Thu Sep 15 16:40:07 2005 -
* Make code more smaller
* Added properties .w and .h
* Removed Error Message "Missing Parameter"
* Removed support for 'u'(underline) style since its will not make any different for the result.
* Now check the 'dot' from the right of temporary windows since we know that the first
dot is 1(For height/y coordinate, check from the bottom). So it will reduce the loops again.
* Fixed the way fontname that have a space being handle. So either "times" or "times new
roman" will be accepted and working correctly(mIRC automatically change the font to the
default one if its not being installed)
- Thu Sep 15 02:57:57 2005 -
* Reduced the total of loops taken to check the 'dot', so its will not take too many times
to return the result. Where before this check every 'dot' on the windows, now its only
check and get two related 'dot'(1st and last)
* Fixed the way of getting the result, which before this didnt include a 'space' at the
start and end of the text. Now, its being considered by draw the text with a background
colour.
* Added support for 'bu' styles, the 'r'(reverse) and 'c'(colour) style will be stripped since its
will not make any different for the result.
- Wed Sep 14 14:08:59 2005 -
* Edit all the code, the last one simply didnt match the expectation.
However, this updated still got a disadvantage which need a long way
to return a result.
Name:$textlen
Author:zonIRC
Email:zonirc@email.com
Description:Get the width/height of text based on typeface and size specified.
Syntax:$textlen(<fontname>,<fontsize>,<text>)[.properties]
Properties:
.w - to get width length of text
.h - to get height of the text
If no properties specified, it will return 'WidthxHeight'
Example:
$textlen(verdana,10,a).w will return '5'
$textlen(verdana,10,A).w will return '7'
$textlen(times new roman,10,a).w will return '3'
$textlen(times new roman,10,A).w will return '6'
$textlen(verdana,10,A) will return '7x11'
$textlen(times new roman,10,A) will return '6x11'
$textlen(verdana,24,a).w will return '13'
$textlen(verdana,24,A).w will return '15'
$textlen(verdana,24,A).w will return '18'
$textlen(verdana,24,How long is the time taken to get the length of this text).w will return '660'
$textlen(verdana,24,How long is the time taken to get the length of this text).h will return '28'
$textlen(verdana,24,How long is the time taken to get the length of this text) will return '660x28'
Info:
- Its cant get the exact size of the text(if the text is too long-which is larger
than your screen width).
--- Example of the false length return ---------
$textlen(verdana,100,How long is the time taken to get the length of this text).w will return '1150'
$textlen(verdana,100,How long is the time taken).w will return '1150'
Both examples shown the same result even the text is not same because text cannot fit on the
temporary window that this snippet created. Which my screen resolution width is 1152 pixels, and
thats is the larger width that the text can be drawn(however the result will be less than that)
If you need to get the length of that text, I'm suggest you break each char(maybe) and loops
it until the last one(where you need to plus each result you get to know the exact length).
===========================================
= BAHASA MALAYSIA =
===========================================
Tarikh: Rabu Sep 14 00:56:18 2005
KEMASKINI:
- Khamis Sep 15 16:40:07 2005 -
* Kod semakin kecil dari segi saiz
* Penambahan 'properties' .w dan .h
* Mesej Ralat "Missing Parameter" di keluarkan.
* Stail 'u'(underline) di abaikan memandangkan ianya tidak memberi sebarang kesan
terhadap nilai yang diperolehi.
* Sekarang ianya hanya memeriksa 'dot' daripada kiri tetingkap memandangkan kita
mengetahui bahawasanya 'dot' pertama adalah 1(Untuk tinggi/koordinat y, ianya diperiksa
daripada bawah). Oleh itu, ianya mengurangkan 'loops'(pengulangan).
* Diperbaiki cara menguruskan nama muka taip yang mempunyai ruangan kosong. Oleh itu,
sama ada "times" atau "times new roman" di terima dan berjalan dengan betul(mIRC secara
automatik menukar muka taip kepada muka taip 'default' sekiranya ianya belum di 'install')
- Khamis Sep 15 02:57:57 2005 -
* 'Loops'(pengulangan) untuk memeriksa 'dot' dikurangkan, secara langsung memberi kesan
terhadap masa yang diperlukan untuk menghasilkan keputusan. Di mana, sebelum ini ia
memeriksa setiap 'dot' yang terdapat pada tetingkap, sekarang ia hanya memeriksa dan
mendapatkan 2 'dot' yang berkaitan(pertama dan terakhir).
* Formula untuk mendapatkan saiz di perbetulkan, di mana sebelum ini mengabaikan 'ruangan
kosong' pada permulaan dan pengakhiran teks. Sekarang ianya di ambil kira dengan
menggunakan 'drawtext' bersama warna latar belakang.
* Menyokong stail 'bu', manakala stail 'r'(reverse) dan 'c'(warna) di abaikan memandangkan ianya
tidak memberi sebarang kesan terhadap keputusan.
- Rabu Sep 14 14:08:59 2005 -
* Seluruh kod di buat semula memandangkan ianya tidak menepati apa yang dikehendaki. Begitupun,
kemaskini ini masih mempunyai beberapa kelemahan. Salah satu ialah pemprosesan kod mengambil
masa yang agak lama.
Nama:$textlen
Penulis:zonIRC
Email:zonirc@email.com
Deskripsi:Mendapatkan saiz panjang/ketinggian sesuatu teks berdasarkan jenis font
dan saiz yang dinyatakan.
Sintaks:$textlen(<muka_taip>,<saiz>,<teks>)[.properties]
Properties:
.w - mendapatkan saiz kelebaran(panjang) teks
.h - mendapatkan saiz ketinggian teks
Jika ia tidak dinyatakan, akan menghasilkan 'LebarxTinggi'
Contoh:
$textlen(verdana,10,a).w menghasilkan '5'
$textlen(verdana,10,A).w menghasilkan '7'
$textlen(times new roman,10,a).w menghasilkan '3'
$textlen(times new roman,10,A).w menghasilkan '6'
$textlen(verdana,10,A) menghasilkan '7x11'
$textlen(times new roman,10,A) menghasilkan '6x11'
$textlen(verdana,24,a).w menghasilkan '13'
$textlen(verdana,24,A).w menghasilkan '15'
$textlen(verdana,24,A).w menghasilkan '18'
$textlen(verdana,24,How long is the time taken to get the length of this text).w menghasilkan '660'
$textlen(verdana,24,How long is the time taken to get the length of this text).h menghasilkan '28'
$textlen(verdana,24,How long is the time taken to get the length of this text) menghasilkan '660x28'
Info:
- Ianya tidak akan menghasilkan jarak panjang sebenar bagi teks yang
dinyatakan sekiranya ianya terlalu panjang sehingga melebihi saiz lebar skrin.
--- Contoh nilai jarak panjang yang salah ---------
$textlen(verdana,100,How long is the time taken to get the length of this text) menghasilkan '1150'
$textlen(verdana,100,How long is the time taken) menghasilkan '1150'
Kedua-dua contoh memulangkan nilai yang sama walaupun teks yang dinyatakan berlainan, ini
disebabkan teks adalah terlalu panjang dan melebihi kelebaran paparan skrin. Di mana saiz lebar
resolusi bagi paparan skrin aku ialah 1152 piksel, dan itu merupakan saiz terlebar yang boleh
di paparkan(begitupun nilai yang dihasilkan akan kurang sedikit daripada saiz tersebut). Jika anda
tetap ingin mendapatkan saiz lebar panjang teks tersebut, aku cadangkan anda pisahkan setiap
karekter(kemungkinan) dan gunakan loops(pengulangan) untuk mendapatkan saiz setiap karekter
tersebut(kemudian anda perlu melakukan sedikit pengiraan dengan menambah setiap nilai yang
didapati untuk mendapatkan nilai yang sebenar).
*/
alias textlen {
var %win = @textlen, %e = echo $color(info) -s * $!textlen:
if (!$isid) { $replace(%e,$!t,/t) Unknown Command | return }
if (!$3) || ($2 !isnum) { %e Invalid Parameter - $!textlen(<fontname>,<fontsize>,<text>) | return }
if ($window(%win)) window -c %win
window -hnpdB +d %win -1 -1 $calc($len($3) * $2) $calc($2 * 2)
var %x = $window(%win).w, %y = $window(%win).h
drawtext -rpb %win 0 255 $+(",$1,") $2 1 1 $strip($3,rcu)
while (%x) {
if ($getdot(%win,%x,1) = 255) { %x = %x - 1 | break }
dec %x
}
while (%y) {
if ($getdot(%win,1,%y) = 255) { %y = %y - 1 | break }
dec %y
}
window -c %win
return $iif($prop,$replace($v1,w,%x,h,%y),$+(%x,x,%y))
}