Using Safari on iPhone to read CHM file

iPhone is a fancy toy with a lot of power but Apple deliberately locks a lot of the potential power. One thing I like to do on an iPhone is to be able to read CHM files. As a weekend project, I setup the tool chain for iPhone following the instructions. Then, I grabbed the source code of chmlib. With some minor modification, I was able to compile the chmlib as an iPhone binary library. That was very encouraging.

This provides a convenient way to make iPhone as a CHM reader. In the chmlib source code distribution, there is an example program that runs as a http-server that serves the content of a CHM as standard web page. The “mobileSafari” has no problem to render the results, but the fonts are usually too small to read and the text is typically rendered too wide such that a lot horizontal scrolling becomes annoyingly necessary.

I decided to combine some python code with the chm_http server from the chmlib source code. I modified the source code of chm_http so it can call python code to modify the HTML code in the CHM file, replacing the original CSS with new setting for reading on small screen. Furthermore, I found it was tedious to start the chm_http from a terminal every time when you want to read a different book. I wrote another small python script that can scan a directory and find all CHM files in the directory to output an index html page. At the end, I was able to use the mobileSafari pointing to the index page and select the book I want to read. The “chm_http” server would start automatically to get the book I like to read.

If you are interested in reading CHM on your iPhone. Get this iphoneCHM.tgz (the file would be upload soon). Copy the “chm_http2“, “rewriteHTML.py“, and “CHMServer” to “/usr/local/bin/” in your iPhone. Change the permission of these files such that you can run all of them. Put some chm files in /var/root/Media/CHM_Ebooks/. Open a terminal in the iPhone or ssh into the iPhone to run “CHMServer”. After that, ask the Safari to open this URL http://127.0.0.1:8000. You should see the links to the CHM files. You can now click on any of them and enjoy a nice reading time.

Tags: ,

Tomorrow

Tomorrow, a new day with new challenges! What a great feeling for entering the next stage of my career! Although I have to make a tough decision, it is indeed time to move on. I am really feeling the excitement of a new environment and a new career path now.

Tags:

用 python+OV 寫中文輸入法

前一陣子看到 lukhnos 在寫一個能讓 OV 用 Ruby 來寫 filter 的模組,一時心血來潮想看看如何用 python 來寫 OV 的 filter 的模組。花了點時間研究了一下如何在 C/C++ 中內藏 python。 雖說用 python 來寫程式作研究也有好一陣子,也曾經用過 SWIG 來控制用 C/C++ 寫的物理模擬程式, 在 C/C++ 中呼叫 python 倒是第一次實做。花了點時間寫了個 prototype,在 lukhnos 的協助下,搞定了一個讓 OV 可以用 python 寫 filter 的模組 (在 OV 的 svn repository: Modules/OVOFPythonBased/ 中)。

OV 的 filter 主要是呼叫一個叫 process 的 method。傳到 process 中的只是一個字串,所以實做 OV 的 filter 並不是太難。發展的過程中,大多數的時間花在看 python 的 C API 文件,熟悉如何在 C/C++ 中建立 python 的物件及將參數傳給 python 的 method。

讓 OV 的 filter 機制可以用 modern 的 python 或是 ruby 實做只是第一步。Dynamic language 的方便已經讓發展新的 filter 的工作大大的簡化。所以下一個就是看看能不能讓 OV 用 Python 或是 Ruby 來寫輸入法。在未來實驗類似酷音等比較複雜的自然語言處理的輸入法模組的時候,如果可以用 Python 或是 Ruby 來寫輸入法應該會有很大的助益。

寫 python based OV filter 時, 只需要定義好對應到 process 的 python method/function 就好了,python 的部份是完全的被動,python 的 code 並不需要管 C/C++ 的 class 與 instance,只需要實做一個叫 process 的函數就可以了。 但寫 OV 的輸入法模組的時候,有幾個 OV 的物件必須要傳到 Python 中,而且 Python 也最要能夠 subclass OV 中的 class 來保持 OV API 介面的一致。基本上要做下面幾件事:

(1) 用 SWIG 來把 OV 的 class 轉成 Python 的 class。
(2) 定義對應到 Python class 的 OV C/C++ class。
(3) 在 (2) 中最重要的一件事就是要把將 OV C/C++ 中 instance pointer 轉成 Python 可以認得的物件。

在這三項工作裡,最容易的部份是 (1)。基本上只要把 Framework/Headers/OpenVanilla.h 剪貼到 SWIG 的 interface 檔中就好,唯一要注意的地方是要讓 SWIG 知道要將 C++ 的 class 轉成 python 的 class。這要用到 SWIG 中的 directors 。請見 SWIG 的相關文件Modules/OVIMPython/ 中的 OVIMPython.i

接下來要就是要讓 OV C/C++ 知道 Python 的存在,主要要去 subclass 兩個 OV C/C++ 的 class, OVInputMethodContext 和 OVInputMethod, 讓 OV 的 loader 可以呼叫對應的 python 物件。請見 Modules/OVIMPython/OVIMPythonBased.cpp 中的 OVIMPythonBasedContextOVIMPythonBased class。 這兩個 wrap classes 作的事情很簡單,就是實做 C++ method 來呼叫對應的 Python instance method。但是之前的一個障礙就是 OVInputMethodContextOVInputMethod 中的 method 的參數裡大多是指向 C++ 的 instance 的 pointers。要怎麼把這些對應的 instance 變成 python 物件在傳給 python 倒是一個比較不容易的問題。也牽涉到 SWIG 怎麼把 C++ 物件映射到 python 物件的細節。

也許在 SWIG 有對應的解法,但我並沒有從 SWIG 的文件中看到顯而易見的方法來解決這個問題。後來是在研究 SWIG 產生的 python module 的檔案中找到 hint。對每一個要 wrap 的 C++ class,SWIG 會產生兩個對應的 python class。例如如果在 C++ 中有如下的宣告:

class OVKeyCode : public OVBase  {
public:
virtual int code()=0;
};

SWIG 會建立下面兩個 python class:

class OVKeyCode(OVBase):
...

class OVKeyCodePtr(OVKeyCode):
...

其中 OVKeyCodePtr 的 constructer ( __init__() in python ) 可以用 SWIG 中的表示 C/C++ pointer 的 python pointer object 建立對應的 python object。所以接下來要作的就是要把 C/C++ 的 pointer 轉成 python 中 pointer object。而 SWIG 的作法只是把 C/C++ 中的 pointer 的 address 和 type 換成特殊格式的字串,在 SWIG 所產生的 C/C++ 的 wrap 檔中有一個特別的函式 (char *SWIG_PackData(char *c, void *ptr, int sz) ) 就是把 C/C++ 的 pointer 換成字串,所以我們就可以用這個函式將 C/C++ 的 pointer 轉成對應的 python 字串然後透過 SWIG 產生的 aClassPtr 來產生 python 中的 aClass 的 instance,而這個 python instance 的 implementation 就是對應的 C/C++ implementation。

這樣的 mapping 實在有點太複雜而不直覺。還沒有真的詳讀 SWIG 的文件,不知道有沒有比較優雅的方式來作同樣的事。雖說如此,對要用 python 寫輸入法的人可以完全不去理 wrapper 本身及兩個語言的物件對應的複雜性,專注在用 python 來寫輸入法。 在lukhnos 稍早寫的用Python + OpenVanilla寫輸入法中有用 python 的 OV 輸入法的 minimum example。

我想這只是第一步,我自己來試著了解 embedding python 的小小練習。如果有空的話,再看看如何真的用 python 在 OV 裡作些有趣的事。

Tags:

Circular references in python

The following code create circular references in python:

>>> aRef = []
>>> aRef.append(aRef)
>>> print aRef
[[...]]

This creates a list object referred by a variable named “aRef”. the first element in the list object is a reference to itself. In this case, the “del aRef” dereference aRef to the list object. However, the reference count of the list object does not decrease to zero and the list object is not garbage collected, since the list object still refers to itself. In this case, the garbage collector in Python will periodically check if such circular references exist and the interpreter will collect them. The following is an example to manually collect the space used by circular referenced objects.

>>> import gc
>>> gc.collect()
0
>>> del aRef
>>> gc.collect()
1
>>> gc.collect()
0

Tags:

Maker Faire 2007

I went to Maker Faire yesterday. I have been a Make magazine subscriber since its debut and I like to hack and make things. Although I have been working some project for my work (in systems biology) last year very hard to a degree that I did not feel to do anything anymore. Since the project is near the end and I am in a position trying to seek new opportunities in my life, it might be a good time to revive some hacker/maker sprit in my heart. So, I could not miss the second Maker Faire,.

My families and I arrived the faire around 10 o’clock. We first spent some time on the Robot Pavilion to see quite a few different interesting robot. Some of them are quite extraordinary, e.g., robots driven by steam engine, self-balancing robot, and home-made Segway, etc.

Although there was a schedule for different presentation and demonstration, with a less than two years old toddler, we were just doing random walk in the faire ground afterward.

The largest Maker demonstration was at ___. Half of the hall was for “Maker” and half of the hall was for “Crafter”. Joy was joking about that most women can stay in the crafter part and most of men can stay in the maker part and this made the “Maker/Crafter Faire” as a great event for family. One can found some of those projects in Make demonstrated in the maker part. There was also a DIY lab to make a “Ybox” provided by Yahoo as sponsor. It was interesting to see that Yahoo seemed putting more effort for this “Maker” event. Compared to Yahoo, Google only put a few computers demonstrating Sketcher. On the other hand, Microsoft did demonstrate some of their cool lab technologies. I talked to a engineer showing me their new web mash-up tools, popfly. It seemed quite interesting, but I had yet to tried it out by myself. Well, at least, my daughter liked their “ducks”.

In the main Make demonstration, what I found quite interesting was the 3D Metal sculptures by Bathsheba. This was my first time to see the results of a 3D printer using metal as the material. It just is amazing to see new technology combining the beauty of math and science. Although I had seen some of the mathematical surfaces on a computer screen, it was just more exciting to see them as “real things”. It was very tempting to buy one of the sculptures and I would probably eventually order one sometime. Or, make one myself for myself in the future. I think I can do it by joining the TechShop and I can use their 3D ABS printer to construct some 3D sculptures too.

There were also all kind of bikes and human powered machines and quite a few DIY activities for kids and adults. Well, my one-and-half years old daughter, Emily was still too small for most of those activities but she seemed like the faire a lot. Oh, she liked the Wii remote controlled RC cars. Ha, this might be a good excuse to buy a Wii, for her and for me (to hack).

Later on, I found the booth Sun demonstrating their Java-power embedding system developing gadget “Sunspot”. The “Sunspot” seemed a cute and powerful little machine for all sorts of purposes, but it was a little bit expansive.

Overall, my families and I had a fun and inspiring day. All I hope is that I will be able to get time to develop some cool projects myself in the near future.

What are interesting:

IMG_3502.JPG

3D Metal sculptures by Bathsheba — exploring how math, science and sculpture meet (web site). ProMetal is the company which make the sculptures by a technology that can “print” metal layer by layer.

TechShop — A club which the members can use their machines for prototyping and DIY. The membership costs $100 per month. I find it is very attractive. If I decide to make something cool, no doubt I will get a membership and learn to use all cool machines they have. They have a 3D ABS printer (from Scicon?) that can be used for fast prototype.

Lots of robots.

Sunspot

Popfly

Moto labs

STL file format for 3D sculpture/prototype.

Pick the nose!!
IMG_3511.JPG

Yahoo, Ybox.tv

puzzle maker

some pictures

Tags:

Tokyo Tower at Night

Tokyo Tower At Night

The night view of Tokyo on the fifty-fifth floor in the Mori Tower.
六本木某大樓五十五層的東京夜景。

Tags: ,

Viacom vs. google

Well, actually, I just want to test video embedding. This clip is pretty
funny anyway.

Tags:

Demonstrate “quickhull” implementation in javascript

Quickhull is an algorithm that is similar to the quicksort using a divide and conquer strategy to find the convex hull for scattered points. The green line in the plot is the initial base line and gray lines are the intermediate base line. The final convex hull is shown in red.



source code: qh.js


< 

Tags: ,

The Chariman of Google

They must forget to google search “Chariman Schmidt”.
See also “Look up Chariman”.

This video about “googleTube” is fun too.

Tags: ,

Diggs in a box

I have been a fan of the “treemap” algorithm to visualize data with tree-like structure . Finally, I decide to implement one last Friday noon. After a few hours, I had a basic Treemap class in python. When I was testing the class, I found the algorithm can generate some interesting artistic painting effect.

The following is a composition using the Gene Ontology. I guess this is one of the many ways to visualize the so-called Systems Biology. You can also click on the image to see some other randomly generated compositions.

Picture 2

Inspired by Newsmap, I tried to see if I can use the python Treemap class and some javascript to map Diggs to a treemap. So, I spent a few hours in the last weekend making this little toy — Diggs in A Box.

Picture 3


It is rather primitive now and I can think a long TODO list to improve it. Hopefully, I will get some time to improve its functionality and appearance eventually. If you have any suggestions, please leave a message and I hope you find such presentation of data interesting and useful.

Tags: , , ,