You're viewing all posts tagged with hacking

Very simple implementation of Karplus-Strong algorithm in coffeescript

I’m a bit over excited as I just managed to successfully implement Karplus-Strong’s guitar string algorithm.

http://en.wikipedia.org/wiki/Karplus-Strong_string_synthesis

The Wikipedia description was too complicated for me. I had to get the original paper and try to study it. It’s rather old and the terminology was a bit weird, so it took me a while to really understand it.

This snippet assumes you have SRATE defined somewhere (the sample rate), and that you already managed to setup the audio output and the mixer and everything else. I’m using audiodata.js to output audio using Firefox4’s Audio Data API.

The function guitar here returns a function which takes a sample index/point and returns the sample. It assumes that it will be called with all the samples sequentially, otherwise it won’t work. Something like:

signal_fn = guitar(440) # 440 being the frequency we want to sound

point = 0
while not done
    sample = signal_fn(point++)
    # output sample

Not exactly that, but something like that.

Here’s the code:

period_len = (freq) -> Math.floor (SRATE/freq)

avg = (a, b) -> (a + b) / 2

ks_noise_sample = (val) ->
    # get either val or -val with 50% chance
    if Math.random() > 0.5
        val
    else
        -val

# karplus strong algorithm
guitar = (freq) ->
    samples = period_len freq
    table = new Float32Array(samples)
    getsample = (index) ->
        point = index % samples
        if index == point
            table[point] = ks_noise_sample(1)
        else
            prev = (index - 1) % samples
            table[point] = avg(table[point], table[prev])

Note that this is coffee-script, where the last expression in a function is returned, and A = B expressions return B. So, guitar returns the getsample function, and getsample return table[point] after we calculate it.

Here’s what this does:

Fill a table of length p with random values (actually: fill it with either A or -A, with 50% chance each). Where p is the period of the signal, which is the sample_rate/frequency.

To produce samples, just keep cycling on this table. That is, to get any point t, take table[t % p].

BUT, as you loop on the table, average each two adjacent samples. That is, everytime you get table[t] (except the first time around), let table[t] be (table[t] + table[t-1]) / 2.

This causes the signal to decay overtime, or something like that.

That’s it.

Html engine for arc

I was looking at the coffekup html engine, and as I was trying to use it, I realized something: it’s not really extensible. The same thing is true for Haml, and probably some other html DSLs out there.

While it’s interesting that:

.book
   .title = "Something"

compiles into:

<div class="book"><div class="title">Something</div></div>

It’s not awfully useful for large projects, and its only advantage is a nicer syntax.

The same goes for coffeekup. It’s certainly nice that one can write:

ul ->
   li "Item1"
   li "Item2"
   li "Item3"

But ultimately, I want to write something more like:

ulist "item1" "item2" "item3"

or, in s-expression syntax, I want to write:

(ulist "item1" "item2" "item3")

and have it automatically produce:

(ul (li "item1") (li "item2") (li "item3"))

In other words, I want to be able to define custom tags that blend in naturally with the rest of the syntax and don’t look any different; as if they were a natural part of the language:

 (div 'id "menu"
    (list "item1" "item2" "item3"))

The nice things about s-expressions is they’re perfect for building tree structures. But there’s a slight problem in that html trees are not pure trees of nodes only; each node can have attributes.

But I want to write trees with the least amount of ( parenthesis ) possible, so for a good syntax, I propose that symbols should be used to denote attributes, and then whatever follows the 'attr value pairs should be considered as child nodes; like this:

(node-name 'attr value 'attr value 'attr value node node node node)

There’s no ambiguity here, you know what are the attributes and what are the child nodes.

Once we have that, we’re gonna need a mechanism to define custom tags.

Something like mylist above could be defined like this:

(deftag mylist
    (ul (map li children)))

Here, children is implicit, it’s created by the deftag statement. deftag automatically parses the arguments passed to mylist and creates 'attrs and 'children variables. This means that custom tags can also take attributes and use them.

For example, mylist can specify a class for the ul element by using the 'class attribute passed to it:

(deftag mylist
    (ul 'class attr!class (map li children)))

This way, (mylist 'class "menu" "item1" "item2") is a short-hand for:

(ul 'class "menu" (li "item1") (li "item2))

To make this tolerant and robust, we can make nil attributes not count, so that (div 'class nil content) produces the same result as (div content)

These tags like (div ... stuff ..) don’t have to return strings; they can return tag objects. Then these tag objects can be traversed and the final html can be produced by printing to stdout, like the vanilla html.arc does.

I started working yesterday on implementing these ideas, and I already have a basic working version locally.

I can tell it:

arc> (render-html (div 'class "floating" (span "Piece of text") (p "Some paragraph")))
<div class="floating">
  <span>
    Piece of text
  </span>
  <p>
    Some paragraph
  </p>
</div>
nil

And I can define custom tags:

For example, a ul list:

(deftag items
      (e "ul" 'class attr!class
        (map [e "il" 'class attr!itemclass _] children)))

And usage:

arc> (render-html (items 'class "small_menu" 'itemclass "tiny_item" "item1" "item2" "item3"))
<ul class="small_menu">
  <il class="tiny_item">
    item1
  </il>
  <il class="tiny_item">
    item2
  </il>
  <il class="tiny_item">
    item3
  </il>
</ul>
nil

Notice how you can use arbitrary custom attributes.

And here’s a slightly more useful example:

(deftag csslink
    "takes a list of css files and creates tags to include them"
    (map [e "link" 'rel "stylesheet" 'type "text/css" 'href _] (flat children)))

And here it is in action:

arc> (render-html (csslink "s1.css" "s2.css" "s3.css"))
<link rel="stylesheet" type="text/css" href="s1.css">
</link>
<link rel="stylesheet" type="text/css" href="s2.css">
</link>
<link rel="stylesheet" type="text/css" href="s3.css">
</link>
nil

This is all good, but I wanted a way to make it templatize-able. What I mean is, I want to define a page template, that defines the general structure of the page, and put some placeholders in it, which can be populated later. Further more, it should be able to create new templates based on already existing ones.

And as I was thinking about how to do that, it hit me: this is already possible. Templates are nothing more than custom tags! The placeholders are the custom attributes! And since custom tags are based on other tags, then it’s already possible to create a template based on another template.

Here’s a little demonstration:

(deftag page
    (html
      (head (title attr!title)
            (apply jscript attr!js)
            (apply csslink attr!css))
      (body children)))

Here’s an example of how to use it:

(render-html 
     (page 'title "Html template" 
              'js '("first.js" "second.js") 
          (div "This is my content")))

And here’s the output:

<html>
  <head>
    <title>
      Html template
    </title>
    <script type="text/javascript" src="first.js">
    </script>
    <script type="text/javascript" src="second.js">
    </script>
  </head>
  <body>
    <div>
      This is my content
    </div>
  </body>
</html>

Here’s an example of a template based on the “page” template:

(deftag blogpage
    (page 'title "Blog post" 'js "blog.js" (div 'class "blogpost" children)))

(render-html (blogpage "This is my post"))

And the result, as expected:

<html>
  <head>
    <title>
      Blog post
    </title>
    <script type="text/javascript" src="blog.js">
    </script>
  </head>
  <body>
    <div class="blogpost">
      This is my post
    </div>
  </body>
</html>

This project is now 3 days old, I’m putting on github, and naming it “sehm”, as in S-Expression Html Markup.

https://github.com/hasenj/sehm

html template engines

OK, here’s what sucks about all html template engines.

They all focus on the wrong thing! They all focus on how to access variables from the application, and doing things like loops and things like that.

This is all good and great, but I think we’re now past that, there are new requirements of html templates:

Sane syntax

Do you see what’s wrong with this?

{% for item in list %}
    ... do something ..
{% endfor %}

The end tag has a begin and end tag!!

Why can’t it just be:

(for item in list
   (.... do something ...))

Yes, my brain has been infected by the lisp virus :(

This should also be the case for html tags.

Why write this:

<div id="sidebar"> ... content ... </div>

When you can write:

(div (id "sidebar") (.... content ...))

The point is not about using parenthesis, the point is having a single character to close an element.

Another good example that’s non-lispy is C functions:

div(id="sidebar") {
    .. content ..
}

Make it easy to produce cross-browser html code (and css)

If tags can be written like that, we must be able to make up our own tags.

This means having macros for most of the things that tags are used for. If, for a certain situation, a span makes sense in one browser where a div is required for another one, there’s no need for complex if statements to decide the tag name, you just make up a new element, say “weird_floating_thing” and it would get macro-expanded into the right thing.

Jinja2 has macros, but the syntax is so awkward (see above) that it’s not common place to use macros to replace tags, because {% call macro(params) %} .... {% endcall %} is more cumbersome to write than just a plain <tag> ... </tag>.

Be language-neutral

I’m not a lisp hacker. OK, I’m trying to learn it, but I probably won’t use it for my next web app. Lisp’s s-expressions (these familiar (tree (structures))) are naturally useful for writing custom ‘languages’ on top of lisp. You don’t need to make a template engine to parse (div (id "sidebar") "hello world"), it’s already a valid expression. You just need to define div as a macro or a function that somehow ends up translating that expression into <div id="sidebar">hello world</div>

The problem with this is, well, it probably won’t be standardized or well documented, everyone will make up his own tree structure and syntax rules. I think this is a disadvantage. I think for a case like this it’s really useful to have a standard ‘core’ template engine that is extensible but also reliable and ‘stable’.

Also, the requirements for html template engines are:

  1. common place
  2. not tied to any language
  3. have more to do with html than with any specific language or framework

So a language-neutral solution is more fit as a solution to these problems. I would really hate it if someone already has a solution for these html problems but his template engine only works in ruby! I’m a python person.

Extends to css and javascript

The syntax of javascript is also a bit awkward, it’d be nice to have nicer ways of writing functions which take anonymous functions which take dictionaries.

jQuery does a lot of nice thing to javascript, but there’s still the limit of the javascript syntax itself.

For css there’s a good solution: clevercss, the only thing missing from it is macros.

Update: for javascript, there’s coffee-script, and for css, there’s lesscss which is even better than clevercss (it has macros/mixins).

Update2: After 3 months of talking about it, I finally started it: https://github.com/hasenj/sehm There’s also sweetscript: https://github.com/evanrmurphy/SweetScript

Starting to like Arc

For general programming, I don’t know if Arc will hold up against python.

But one thing that Arc has the advantage of is that macros almost eliminate the need for special-purpose libraries that implement some DSL such as html templating.

This is a sketch of an html page I’m wanting to write

(html 
    (head
        (title "What's up")
        (script "mystuff.arc")
        (jscript "/javascript/native.js")
        (css "mycss.arc"))
    (body
        (form 
            (text-field 
                (name "what") (label "Remind me to")
                (gray-hint "Pay my phone bill") 
                (hover-hint "This will be the subject of your email"))
            (fuzzy-time-field "time")
            (nice-button "Remember to remind me, go!"))
        (footer "Find the source code on " (link "gitbut" github-url*))
        ))

It’s just a form on a page; nothing really that fancy. Let’s see if I can implement the lower levels of this so that such a code would render the page exactly the way I’d expect it to.

بعض الحركات البهلوانية في بايثون functional stuff in python

هذا الموضوع ماخوذ عن حوار في ‎#linuxac

ما هو الـ self في بايثون؟؟

تخيل ان عندك كائن obj من كلاس kls

حين تنادي:

obj.method()

لا تمرر شيئا الى method، و لكن عندما تعرفه داخل الكلاس، يجب ان تضع self:

def method(self):
    ....

لماذا؟
حسنا، لماذا بالضبط؟ لان مؤلفي بايثون هكذا ارادو،
و لكن يبقى السؤال، لماذا؟ هل هناك فائدة؟

نعم، هناك فائدة.

حين تنادي obj.method، قد تظن انك تنادي نفس الـ method المعرف داخل الكلاس، و لكن الحقيقة ليست كذلك.

الحقيقة انك تاخذ العضو method من الكائن obj و تقوم بمناداته — و انت لا تعرف حقيقة ما هو method، قد يكون function عادي، و قد يكون اكثر من ذلك ;)

كيف يكون اكثر من ذلك؟

في الحقيقة ان obj.method يقوم تلقائيا بتمرير الكائن obj الى الـ method المعرف داخل الكلاس.

كيف يمرره تلقائيا؟

حسنا، لغة بايثون تحتوي على خصائص الـ functional programming، اي يمكن تمرير فنكشن الى فنكشن اخر كبارامتر، و يمكن ارجاع فنكشن من فنكشن اخر، و يمكن تعيين فنكشن الى متغير و من ثم استدعاء الفنكشن عن طريق المتغير.

تخيل معي هذا الفنكشن البسيط جدا:

def f(x): print x

مجرد ياخذ كائن (رقم او نص .. او اي شيء) و يطبعه على الشاشة.

و الان تخيل:

def g(): f(10)

مجرد “بروكسي” بين الرقم 10 و بين الفنكشن f، يقوم بطباعة الرقم 10 عن طريق تمريره الى f

طيب، كل شي سهل لحد الان و يمكن فعل هذه الحركة حتى في سي و جافا.

و الان .. اول حركة بهلوانية:

def magic():
    x = 10
    def ninja():
        print x
    ninja()
    x = 20
    ninja()

ماللذي يحصل هنا؟؟ عرفنا فنكشن نينجا داخل ماجك، يقوم نينجا بطباعة الكائن x، و لكن الكائن x معرف خارج ننجا، و مع ذلك يمكنه الوصول اليه.

و الان لو عرفنا اكس على انه 10 قبل تعريف نينجا و قمنا باستدعائه، اكيد سيقوم بطباعة 10. و لكن لو غيرنا اكس الى 20، و قمنا باستدعاء نينجا، فإنه سيقوم بطباعة 20، اي ان الاكس داخل نينجا هو نفس الاكس اللذي نقوم بالتلاعب به.

def magic():
    x = 10
    def ninja():
        print x
    return ninja

هنا نقوم بتعريف نينجا داخل ماجك و نقوم بارجاعه الى من يستدعي ماجك

>>> magic()
<function ninja at 0x872b72c>

يعني عندما استدعينا ماجك، حصلنا على فنكشن اسمه ninja، يمكننا تخزين هذا الفنكشن في متغير و استدعائه:

>>> a = magic()
>>> a()
10

و هنا سنحصل على الرقم 10 .. و هو القيمة المخزنة داخل اكس .. المعرف داخل ماجك!

هل لاحظتم ماللذي حصل هنا؟ اكس معرف داخل ماجك، و لكنه لم يمت بمجرد الخروج من ماجك كما كان سيحدث لو كنا نبرمج في سي او جافا.

هذه الحركة اسمها closure، و يمكن البحث في كووكل عن closure functional programming للمزيد من الشروح

في السابق رأينا ان g هو مثل البروكسي بين 10 و بين f، و لكن في ماجك فإن ماجك نفسه ليس بروكسي، و لكنه يقوم بإنشاء بروكسي (نينجا) و ارجاعه لنا.

و الان ناتي الى المرحلة التالية، حيث نقوم بانشاء بروكسي لاي كائن اخر بطريقة ديناميكية:

def bind(obj):
    def kungfu():
        return f(obj)
    return kungfu

هنا وظيفة بايند ان يقوم بربط كائن ما مع الفنكشن f عن طريق انشاء بروكسي اسمع كون فو

>>> bind(5)
<function kungfu at 0x872b7d4>

هنا قام بايند بربط الرقم 5 مع f و اعطانا فنكشن (بروكسي) يقوم بتمرير 5 الى f

يمكن استخدامه بهذا الشكل:

>>> a = bind(5)
>>> b = bind(10)

هنا، a عبارة عن فنكشن يطبع 5، و b عبارة عن فنكشن يطبع 10

>>> a()
5
>>> b()
10

هل فهمتم شيئا؟

الموضوع صعب اذا كانت هذه اول مرة ترى مثل هذه الاشياء، و يحتاج فترة للهضم.

و الان، يمكننا انشاء بروكسي بين اي كائن و بين f:

>>> my_object = 10
>>> g = bind(my_object)
>>> g()
10

لاحظ هنا ان g يحتوي في داخله على كود يقوم بتمرير my_object تلقائيا الى f

يعني تخيل ان my_object هو ليس مجرد رقم 10 بل هو كائن مركب .. و نستطيع بكل بساطة ان نقوم بالصاق فنكشن جديد الى هذا الكائن بهذا الشكل:

my_object.f = bind(my_object)

هنا، نستطيع ان نستدعي:

my_object.f()

و هذا الـ f سيقوم بتمرير my_object تلقائيا الى الفنكشن الاصلي f اللذي عرفناه في البداية على انه يقوم بطباعة الكائن على الشاشة.

و لكن لنعد الى الفنكشن bind، شمعنى يعني فقط يمرر الى f؟

يمكننا تطويره قليلا و جعله يربط بين اي فنكشن و اي كائن مهما كان:

def bind2(object, function): 
    def inner():
        return function(object)
    return inner

لاحظ ما يحدث هنا:

my_object.f = bind2(my_object, f)

هنا تقريبا نفس ما يحصل في بايثون نفسها (بشكل مبسط جدا جدا) حيث نقوم باضافة حقل f الى الكائن my_object و هذا الحقل هو في الحقيقة فنكشن يقوم بتمرير الكائن الى الفنكشن f الرئيسي

الفرق ان بايثون تقوم بربط الكائن بالفنكشن عن طريق types.MethodType و هو يقوم بشيء مماثل تقريبا، و لكنه ليس فنكشن بل كلاس (constructor)، و ياخذ بارامتر اضافي و هو الكلاس، و طبعا لانه كلاس، فلا يقوم بارجاع فنكشن حقيقي بل يقوم بارجاع كائن يمكن استدعائه لانه يطبق خاصية __call__

يمكننا تخيل ما يحدث على هذا الشكل:

تخيل كلاس اسمه M و ليكن فارغا:

class M(object): pass
a = M()

ننشيء منه كائن و نسميه a

و الان نعرف فنكشن جديد:

def method1(self):
    print self.x

و الان نقوم بربط هذا الفنكشن مع الكائن a

a.m = types.MethodType(method1, a, M)

و هنا نلاحظ ان الحقل m هو عبارة عن “ميثود” بكل ما تعنيه الكلمة من معنى (في بايثون على الاقل)، و نلاحظ ان الفنكشن method1 ياخذ بارامتر هو self، و لكننا حين نستدعي a.m لا نمرر شيئا الى الفنكشن، و هذا لان الكائن a نفسه يمرر تلقائيا.

Practical Common Lisp

Reading Paul Graham’s excellent essays, he always keeps blabbing about how great lisp is. This of course makes me kinda interested in wanting to know what this “lisp” thing is, and maybe learn it and start using it.

I’m reading the book “Practical Common Lisp” by Peter Seibel, he also has a tech talk (at google) with the same title.

When you choose technology, you have to ignore what other people are doing, and consider only what will work the best
Paul Graham - Beating the Averages

التعرف التلقائي على اتجاه الفقرات في صفحات الويب - 2

قمت الان بتطبيق السكربت على هذا البلوك و كذلك على بلوكي الياباني و بلوكي الاخر على http://tech.hasenj.org

كتجربة عملية، لنجرب كتابة فقرة انكليزية:

This is webbidi in action, it’s figuring out which paragraphs are LTR and which are RTL, and then fixing them accordingly.

من اهداف السكربت ان يكون ذكيا بعض الشيء و لا يعتمد فقط على اول كلمة في الجملة، بل ياخذ في الحسبان انه قد تكون هناك فقرة عربية تبدأ بكلمة انكليزية، على سبيل المثال:

linux هو نظام تشغيل مفتوح المصدر، و هو النظام المفضل لدى الهاكرز (هواة البرمجة).

لاحظ ان الفقرة السابقة عربية مع انها بدأت بكلمة انكليزية، و هذا ليس من قبيل الصدفة، فهذا يحصل كثيرا في المقالات التقنية لاننا غالبا ما نتحدث عن منتجات و ادوات و برامج و تقنيات اجنبية.

كيف نحدد اذن اتجاه الفقرة؟

العملية ليست بذلك التعقيد، الاسلوب اللذي اتبعته يعتمد على فكرة بسيطة: تخيل ان هناك فقرة عربية تبدأ بكلمة انكليزية، ما هي الخاصية اللتي تحويها هذه الفقرة لكي نحكم عليها انها ليست فقرة انكليزية؟ بسيطة .. ستجد ان اغلب الكلمات في الفقرة عربية. لذلك ربما نريد ان نحدد اتجاه الفقرة عن طريق اتجاه اول جملة فيها و ليس اول كلمة فيها.

الفكرة ببساطة، نقوم بأخذ اول X كلمات، حيث X = 10 و ناخذ اتجاه اول كلمة على انه المرشح C، و نعتبر الاتجاه الاخر D ناخذ نسبة الاتجاهات C الى D، فإذا كان عدد الكلمات ذات الاتجاه C اكثر من P بالمئة (حيث P = 40) نعتبر ان اتجاه الفقرة هو C (و هو مرشحنا الاول)، اما اذا وجدنا ان نسبة الكلمات اللتي تحمل هذا الاتجاه اقل من P بالمئة، نعتبر الاتجاه الاخر هو اتجاه الجملة.

هذه الفكرة تعتمد على افتراض و هو ان عدد الكلمات الانكليزية سيكون قليلا في هذه الحالة. قد لا تنجح الفكرة في جميع الحالات، او قد تقودني التجارب الى تغيير قيم X و P، فقد اقوم باخذ عدد اكبر من الكلمات، او بدل الاعتماد على عدد الكلمات، قد ابحث عن اول جملة عن طريق البحث عن نقطة او سطر جديد او ما شابه.

و السبب في تحديد النسبة P برقم 40 (اي اقل من النصف) هو حالة خاصة واجهتها في البلوك الياباني و ذلك عند ذكر معاني الكلمات: فمعاني الكلمات عادة تكون اسطر قليلة، كل سطر يبدأ بكلمة من اللغة الاخرى يليها المعنى العربي، و هنا اريد ان يكون اتجاه الفقرة هو اتجاه اللغة الاخرى.

و طبعا في جميع الاحوال، مهما حاولنا تضبيط السكربت و جعله اكثر ذكاءاَ، ستظل هناك حالات تحتاج الى تدخل بشري لتحديد اتجاه الفقرة، و في هذه الحالة يمكن ان نعتمد على رموز اليونيكود الخاصة بتحديد الاتجاه مثل RLE و اخواتها، و هي رموز غير مرئية لهذا تسترعي شيئا من الحذر في الاستعمال. و هذه الجزئية لم اقم باضافتها بعد لكنها ضمن مخططات التطوير.

الكود بسيط و صغير، ادعوكم لقرائته على github

http://github.com/hasenj/bidiweb/blob/master/autobidi.js

لم اضع اشعار ترخيص في الملف (بعد) و لكن هو مشروع مفتوح المصدر و باختصار افعل به ما تشاء! حتى لو وضعته في برامج مغلقة او مواقع خاصة او برامج تجارية.. ليس لدي اي مانع. فقط ارجو المشاركة بالتحسينات في حال قمت باضافة خواص جديدة (لكنك غير مجبر على ذلك).

التعرف التلقائي على اتجاه الفقرات في صفحات الويب

قبل ايام بدأت افتتاح مدونة جديدة .. هدفها الاساس نشر مواد استخدمها لتعلم اللغة اليابانية: http://nihongo.hasenj.org

المدونة تقريبا لا تزال قيد الانشاء، لكني واجهت مشكلة: محاذاة النص العربي الى اليمين و النص الياباني الى اليسار!

المشكلة تكمن في ان markdown لا يحتوي اي وسيلة لتحديد اتجاه النص، و قد تجاوزت هذه المشكلة في هذه المدونة عن طريق حيلة TagsAsClasses كما نشرتها هنا

لكن هذه الحيلة تصلح فقط حين يكون هناك اتجاه واحد لجميع الفقرات (باستثناء الكود).

فكرت مليا في حل لهذه المشكلة

فكرت اولا باضافة دعم خاص للغة العربية في ماركداون و لكن ماذا عن اللغات الاخرى مثل تسكتايل؟

بعد تفكير ملي، وجدت ان افضل حل عملي هو عمل جافا سكربت يقوم بالتعرف تلقائيا على الفقرات العربية و تحويل اتجاهها لليمين.

هذا الحل محدود في انه يعمل على الوب فقط! و لكنه عملي .. لانه المجال اللذي نحتاجه فيه غالبا.

الكود هنا: http://github.com/hasenj/bidiweb

و هذه هي النتيجة:

sample

More on “Linux is an IDE”

In a previous post, I was ranting about the windows-based development environment at my day job, and by the end I said “Linux is an IDE”.

I didn’t actually come up with that: I took it from a comment on stackoverflow by dsm, on an answer to the question of IDEs on Linux

There are things in VS that are not obviously present in vim or emacs or whatever, like renaming across the entire project. That’s true; but that’s not the important thing in an IDE. It’s not always possible to implement anyway; I’ve only seen it for Java and C#, it’s not practical to implement for C++ or python. C++ is too hard to parse, while python is dynamic and attributes can be added/modified at run time, so no static analysis tool can provide a solid renaming functionality.

And Linux does have VS-like IDEs: NetBeans, Eclipse, KDevelop and QtCreator. They have a whole bunch of features that should make VS users feel less intimidated.

But that’s not the point, really.

A few days back I built firefox from source on an ubuntu virtual machine to try and isolate a bug we were experiencing.

It was a simple, 3-step process:

1- install the build dependencies 2- download the source 3- build

On windows, Step #1 would probably take hours of googling. Step 2 and 3 won’t be that hard, because in step 1 you would’ve had to download a whole bunch of linux tools :)

On ubuntu, step 1 is simply apt-get build-deb firefox This command installs all the build dependencies for firefox.

Step 2 is a simple Mercurial command to clone the repository, and step 3 is just “make” basically.

I went to the extra trouble of installing a virtual machine and installing ubuntu on it, just so that I wouldn’t have to deal with step #1 on a windows machine, because I’ve been there before, and I know it sucks.

My point is, linux is full of tools with a lot of features that are specifically developed to make programmers’ life easier.

There is a steep learning curve, but once you get it, your productivity will jump 10 folds.

Think of developing on windows as trying to type up a big document without knowing your way around the keyboard: you look for 2 seconds before finding each button!

While developing on linux is like typing the same document when you can touch-type.

Yes, it takes time to learn to type without looking at the keyboard, but it makes your life ridiculously easier.

Again, I didn’t exactly come up with this metaphore myself: it’s from the “OMG why do these crazy people use vim” article, (under micsonception #3).

wops, embedding levels, again

Last time I thought I “got” embedding levels rights.

Turns out, not really!

My mistake is I made some assumptions about the structure: I thought a 222 embedding run must be within a 11111 run, so it would be 11222221111, but as it turns out, it could simply be 22211. The code I posted in my previous post does not work for this case.

Man, I should’ve just read the specs from the start.

So, discard my previous code.

Btw, this fixes the bug I talked about with an LTR sentence crossing a line boundary:

fix

progress on scribus’ complex layout

It turned out there were still many issues left since my last post. (one of them I blogged about in the previous post).

Big Thanks to Zayed’s for testing and reporting issues.

There’s still much to do, but the biggest show-stopper now is when english words cross line boundaries (which is something I didn’t think about before).

ltr with right align

This could arguably be the intended behavior: depending on whether we want or don’t want automatic paragraph direction detection.

However, the real issue underlying this is that scribus has no “RTL” mode. There’s something similar, but it’s hidden deep in the properties box, and it reverses everything, on the character level.

The real meaning of embedding levels

As I usually do with a lot of things, I tend to skim through, glossing over the details. And so, I thought embedding levels were merely a coding scheme that I can ignore and just pretend that an “odd” level means an RTL run. (I’m referring to my adventure with adding arabic support (complex layout) to scribus’s text frame).

I was wrong.

The problem manifests itself clearly when some arabic sample contains numbers. Because numbers run from left to right, they have an even embedding level. Consider this:

R1 ## R2

Where R1, R2 are RTL segments, and ## is a number.

You’d think this break the text into 3 runs: RR LL RR, but this is wrong! the whole thing is an RTL run, but it has an LTR run inside it.

Meaning, the text is to be displayed (visually) like this:

R2 ## R1

The text starts from the right, (even though it’s aligned to the left in this post).

You can’t describe this with merely linear monodirectional segments: you’ll get the wrong result. You have to have a “tree” of runs, where a run could have child runs embedded within it, so that it doesn’t break the surrounding text at the wrong place.

That’s why embedding levels are they way they are.

The above example would resolve to embedding levels (roughly) of:

11 22 11

(I say roughly because I’m not entirely sure how spaces are handled). The level for the number (sequence of digits) is 2, not 0.

So what we need to do, is reverse inner runs before reversing outer ones.

For comparison:

Edit:

I had some code here before, but it was wrong too :) so I deleted it.