Tag Archive > use

Using Google Fonts Locally

» 03 February 2011 » In Guides, Web Design » 15 Comments

Google Fonts, or more popularly known as Google Web Fonts, provides a great collection of open source licensed (can be used commercially and non-commercially) true-type fonts. My favorite is Droid Sans, the font I’m currently using now sitewide.

Because I love Droid Sans, I want to use it everywhere. Even in my school projects. I just finished a project (that is due tomorrow) that demonstrates HTML frames. And I wan’t (of course) to use Droid Sans as the main font for the web page. And if I used the font as I regularly do:

<link href='http://fonts.googleapis.com/css?family=Droid+Sans:regular&amp;subset=latin' rel='stylesheet'> 

The font won’t be viewable offline. So the solution for this, is to download the font and use the CSS Google recommends. Take a look here.

@media screen {
@font-face {
  font-family: 'Droid Sans';
  font-style: normal;
  font-weight: normal;
  src: local('Droid Sans'), local('DroidSans'), url('http://themes.googleusercontent.com/font?kit=s-BiyweUPV0v-yRb-cjciC3USBnSvpkopQaUR-2r7iU') format('truetype');
}
}

But how do we download the font? That’s easy look at the url('...'):

src: local('Droid Sans'), local('DroidSans'), url('http://themes.googleusercontent.com/font?kit=s-BiyweUPV0v-yRb-cjciC3USBnSvpkopQaUR-2r7iU') format('truetype');

Just enter that URL to your browser, and viola!

Now that we got our font, we use the CSS, and we modify it a little bit:

@font-face {
  font-family: 'Droid Sans';
  font-style: normal;
  font-weight: normal;
  src: local('Droid Sans'), local('DroidSans'), url('path/to/DroidSans.ttf') format('truetype');
}

So this works for the latest stable versions of Chrome, Firefox, Opera, Safari, and.. Wait.. It’s not working in IE8. This is because IE lte 8 only accepts EOT type fonts. So how do we fix this? I’m not an IE user, but a big part of the Internet population uses IE, so we do not have a choice.

Good news is, you can convert TTF fonts to EOT. After that, save the file on the same directory where our TTF font is, and add it in the CSS.

@font-face {
  font-family: 'Droid Sans';
  font-style: normal;
  font-weight: normal;
  src: url('path/to/DroidSans.eot');
  src: local('Droid Sans'), local('DroidSans'), url('path/to/DroidSans.ttf') format('truetype');
}

Now, you have to remember that you have to place this on the top of your CSS document. Then you can just use this as a regular font in CSS:

body {
	font-family: 'Droid Sans', Arial, sans;
	color: #666;
}

Make sure, that the path to the font is always relevant to the CSS file. And that’s all for now. :)

Continue reading...

Tags: , , , , ,

Why Use Pointers?

» 22 November 2010 » In C/C++, Programming » 4 Comments

This is a very much asked questions, especially among students. Pointers, at first, is very hard to understand. All those asterisks * and ampersands & and the crazy assignments. Well, I assume you already understand pointers, or rather, you have a an idea how generally pointers operate. Then you might wanna ask, why use pointers?

Pointers Reviewed

As we all (should) know, pointers stores the address of a variable. So basically, when we call a pointer by name, we’re referring to the address. Let’s take this for example:

int *p1

The above declarations tell the compiler that p1 is a pointer to an integer. Means that you cannot assign an address of a character or a floating point to it. Actually you might want to try, but basically you’re just wasting time. Anyway, let’s say we have this variable a with the value of 5. And then we do this:

p1 = &a

We just assigned the address of a to the pointer p1. So if we are to print p1 it’s just like, we’re printing the value of &a which is the address of a. So how do we know that its the address of a? Answer: the ampersand &.

Accessing Values with Pointers

So on the above review example, we assigned &a to p1. But what if we are to access the value of a using pointers? Normally we access some variables value by its name. But now, since we are using pointers, we are going to access the value by the variable’s pointer. Say for example, we are to print the value of a. Normally we will do:

std::cout << a;

But if we are to use pointers, we do this:

std::cout << *p;

Remember, we assigned the address of a to p1, and if we are to access the value of a with a pointer, we use the asterisk * symbol, like *p1.

Now, to make it less confusing, let’s say we do not have any pointers. And we have &a as the address of a. To access its value, we have to use the asterisk symbol, and it should be like *&a. Still confused? I guess not.&a here is our reference to the value of a, and by using an asterisk, we dereferenced it. Making an asterisk * a Dereference Operator.

Using Pointers in Functions

Whenever we pass variables to a function, let’s say main called the function nUpdate(); which accepts 2 parameters, one is a variable of type int to be updated, and the other one is the constant integer to be added to the variable. Normally the function cannot directly alter the variable that will be passed to it, instead it will copy the variable and create another one with the same attributes and just return it to the main function. Here’s how it will look like:

int nUpdate(int a, const int b) {
    return a + b;
}

int main() {
    int a = 5;
    a = nUpdate(a, 6);
    return 0;
}

After main called nUpdate() the value of a now is 11. Am I right? Now, this is correct. But, there’s another way. Without wasting some bits, we can just directly update a in the function. How? Pointers. Now, this is one major use of pointers, and I’ll explain it further later. Using pointers, we are going to pass just the address of our variable a and the constant. So we have to make changes, on the parameter type of the function, and we need something that holds addresses. Ah, pointers.

int nUpdate(int *p, const int b) {
    *p += b;
}

int main() {
    int a = 5;
    nUpdate(&a, 6);
    return 0;
}

There we have it. We passed the address of a which is &a, and the pointer called p receives the address, and we accessed it by using our dereference operator * just like what we did earlier. Now there’s another way of doing the very same thing, except we will not be directly using a pointer. If you can remember earlier, and using our latest example, pointer p holds the address of a which is &a. So, we can do this:

int nUpdate(int &a, const int b) {
    a += b;
}

int main() {
    int a = 5;
    nUpdate(a, 6);
    std::cout << a;
    return 0;
}

This approach, is what I use. As you can see, our function now accepts an address of an integer variable. And in our main, we passed the variable a. But from the function’s point of view, instead of receiving the value of a, it grabs its address. And to access the value of that address we again should use the dereference operator *, and *&a is equal to a.

A Larger View

The past examples showed how useful pointers are in functions. But to show you more of its importance, look at a bigger view. Say there’s this large program, its main calls some function and passes loads of data, say millions of bytes (MB). Without the use of pointers, this is heavy. As the function will copy all those data to its scope in memory, and do its job. It will increase memory usage to be short. Now with the help of pointers, the function could just get the address of all those variables which holds the data and do its job. No copying, less memory usage.

Moving On

I surely hope that this is very clear for you now, if you are still confused, then C/C++ is not your type. Anyway, learning takes time and good luck with pointers!

Continue reading...

Tags: , , , ,