Wednesday, March 7, 2012

How to embed fonts in EPUB?

I tried to read Chinese font in Kobo's Wireless eReader (KOBO WI FI) recently, and I found a way to do it, maybe it is the only solution for KOBO WIFI.

So I prepared a step by step guide to explain how to make any exists epub file work as following, I also prepared several screenshots for you.

  1. Prepare the zip tool - 7zip, you can download it from here, and install it
  2. Rename the epub file to zip
  3. Extract files into a folder using 7zip
  4. Copy the font file DroidSansFallback.ttf into the unzipped folder
  5. Edit stylesheet.css add the font and use it, the original and changed css as following

  6. @namespace h "http://www.w3.org/1999/xhtml";
    
    body{
        margin:10px;
        font-size: 1.1em;
    }
    
    p{text-indent:2em; line-height:1.5em; margin-top:0; margin-bottom:0;}
    
    .catalog{
        margin:1pt;
        padding:0;
        text-indent:2em;
    }
    
    h1{text-align:right; margin-right:2em; page-break-before: always; font-size:1.6em; font-weight:bold;}
    
    h2{
        display: block;
        font-size: 1.2em;
        font-weight: bold;
        margin-bottom: 0.83em;
        margin-left: 0;
        margin-right: 0;
        margin-top: 1em;
        page-break-before: always;
    }
    
    .mbppagebreak {
        display: block;
        margin-bottom: 0;
        margin-left: 0;
        margin-right: 0;
        margin-top: 0;
        page-break-after: always
    }
    
    a{
        color: inherit;
        text-decoration: inherit;
        cursor: default
    }
    
    a[href]{
        color: blue;
        text-decoration: underline;
        cursor: pointer
    }
    
    .italic{
        font-style: italic
    }
    

    @font-face{
        font-family:"Droid Sans Fallback";
        font-style:normal;
        font-weight:normal;
        src:local("DroidSansFallback.ttf"), url("DroidSansFallback.ttf");
    }
    
    body{
        margin:5%;
        font-family:"Droid Sans Fallback";
    }
    
    p{
        text-indent:0.25in;
        margin:0;
        line-height:150%;
        text-align:justify;
        font-size:100%;
        font-family:"Droid Sans Fallback";
    }
    

  7. Edit content.opf to change any metadata not in latin to latin chracter

  8.     三国演义(毛评本)
        罗贯中
        三国演义(毛评本)
        zh-cn
        
        COAY.COM [http://www.coay.com]
        COAY.COM
        2610
        三国演义(毛评本)
    

        SanGuoYanYi
        LuoGuanZhong
        SanGuoYanYi
        zh-cn
        
        COAY.COM [http://www.coay.com]
        COAY.COM
        2610
        SanGuoYanYi
    

  9. Zip the folder content using 7zip
  10. Rename the zip file to epub
  11. Drop it into your KOBO WIFI and enjoy reading

This was tested on KOBO WIFI and android 2.1 tablet and smartphone.

Saturday, April 16, 2011

Wrapper Classes and Their Constructor Arguments

PrimitiveWrapper Class Constructor Arguments
booleanBooleanboolean or String
charCharacterchar
byteBytebyte or String
shortShortshort or String
intIntegerint or String
longLonglong or String
floatFloatfloat, double, or String
doubleDoubledouble or String

Default Values for Primitives and Refrence Types in Java

Variable TypeDefault Value
Object referencenull (not referencing any object)
byte, short, int, long0
float, double0.0
booleanfalse
char'\u0000'

SCJP Exam Watch 11 - Access instance variable/method from static mathod

One of the mistakes most often made by new Java programmers is attempting to access an instance variable (which means nonstatic variable) from the static main() method (which doesn't know anything about any instances, so it can't access the variable). The following code is an example of illegal access of a nonstatic variable from a static method:

class Foo {
    int x = 3;
    public static void main(String[] args) {
        system.out.println("x is " + x);
    }
}

Understand that this code will never compile, because you can't access a nonstatic (instance) variable from a static method. Just think of the compiler saying, "Hey, I have no idea which Foo object's x variable you're trying to print!". Remember, it's the class running the main() method, not an instance of the class.

Of course, the tricky part for the exam is that the question won't look as obvious as the preceding code. The problem you're being tested for accessing a nonstatic variable from a static method will be buried in code that might appear to be testing something else. For example, the preceding code would be more likely to appear as

class Foo {
    int x = 3;
    float y = 4.3f;
    public static void main(String[] args) {
        for (int z - x; z < ++x; z--, y = y + z) {
            // complicated looping and branching code
        }
    }
}
So, while you're trying to follow the logic, the real issue is that x and y can't be used within main(), because x and y are instance, not static, variables! The same applies for accessing nonstatic methods from a static method. The rule is, a static method of a class can't access a nonstatic (instance) method or variable of its own class.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Sunday, April 10, 2011

Compiler generated constractor code

class Foo {}
======>
class Foo {
    Foo() {
        super();
    }
}


class Foo {
    Foo() {}
}
======>
class Foo {
    Foo() {
        super();
    }
}


public class Foo {}
======>
public class Foo {
    public Foo() {
        super();
    }
}


class Foo {
    Foo(String s) {}
}
======>
class Foo {
    Foo(String s) {
        super();
    }
}


class Foo {
    Foo(String s) {
        super();
    }
}
======>

Nothing, compiler doesn't need to insert anything.



class Foo {
    void Foo() {}
}
======>
class Foo {
    void Foo() {}
    Foo() {
        super();
    }
}

(void Foo() is a method, not a constructor.)


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 10 - Methods return an abstract class or interface

Watch for methods that declare an abstract class or interface return type, and know that any object that passes the IS-A test (in other words, would test true using the instanceof operator) can be returned from that method - for example:

public abstract class Animal {}
public class Bear extends Animal {}
public class Test {
    public Animal go () {
        return new Bear();    // OK, Bear "is-a" Animal
    }
}

This code will compile, the return value is a subtype.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.