Saturday, March 7, 2009

TeX parsing: TeXpp

In the project I'm currently working on we need to do some automatic transformation of LaTeX documents. The most important requirement is simple: never break neither meaning nor formatting of the document. Unfortunately this requirement is also the most difficult to fulfill.

In spite of big popularity of TeX there are not so many libraries for parsing it and none of that libraries fulfill stated requirement. The reason for it is simple: TeX format is extremely hard to parse because its grammar is context-dependent. You can never be sure about meaning of any given character without parsing and executing all commands in the document before that character.

The first attempt was to use LaTeX::TOM perl module and it somehow worked, but with many bugs and limitations. Then there was an idea to modify original tex program to extract information we need but it turned out to be non-manageable task in reasonable timeframe. It seems that not so many people are dare enough to touch that code: not only it is written in not-so-popular and somehow cryptic pseudo pascal, but the whole organization of the code is very different from modern programs. I'm not saying its bad, but its very unfamiliar and hard to work with for modern programmers.

So I've decided to implement my own solution. For now I have a lexer and a parser that builds basic document tree for tex documents. Currently only a few TeX commands are supported (actually the whole list is: \relax, \par, \show and \let) but the framework is ready and new commands can (and will) be added very easily. The resulting document tree allows reconstruction of original source document as well as modifying parts of it.

Today I've released the whole code at http://code.google.com/p/texpp/. How that code can be used ? Think about full intelligent TeX code completion, online error detection, TeX debugging, etc. Kile developers ?

Monday, June 30, 2008

Converting any window into plasmoid

Take a look at the following screenshots:

Besides familiar Clock and Calculator you can see several new plasmoids here:
  • Konsole
  • MPlayer
  • Speedcrunch calculator
  • Step physical simulator
No, I have not rewrote all that apps as plasmoids. Instead I've wrote an applet that allows you to convert any running application into plasmoid. Currently it works using XEmbed protocol and therefore have lots of problems:
  • You can't rotate these plasmoids and they don't work correctly with ZUI (however the latter can still be improved a bit)
  • Z-order of plasmoids is not respected (i.e. in case you have several overlapping plasmoids)
  • Switching between Desktop and Dashboard is not very smooth: embedded applications are only repainted after several-seconds delay
All these problems can be fixed by using Composite extension (and input redirection) instead of XEmbed but this will probably require some cooperation with KWin and will only work when Composite is enabled.

There is another problem: currently you have to manually re-configure all window-embedding plasmoids after restarting plasma. For KDE applications this can probably be partly fixed by using KDE session management, for other applications one can allow the user to setup command line of the application to start.

Despite all current limitation, I think this approach still has its own use-cases, for example when you want quick access to some of your applications from Dashboard (for example Konsole) or if you want something not-yet-available as plasmoid on your desktop (for example video player, advanced calculator, web-browser with your webmail, a game from kdegames, googleearth, ...).

If you still want to try it, the code is available in playground/base/plasma/applets/embed-win.

Thursday, June 28, 2007

Exams are over

Last two weeks I've spend almost all time preparing to my last exam and writing my diploma. Yesterday I've defended my diploma and got bachelor degree in physics. So now I have a lot of time and motivation to work on Step.

Monday, June 11, 2007

Annotations for objects

I've just implemented basic text annotations for Step. Now you can add text items to the scene, very soon you will be able to associate annotations with the objects. What I still can't decide is the appearance of the annotations. I see two possible options:
- text in a frame (like KNotes) resizeable by mouse.
- free-running text without a frame dynamically resizeable as you type

Wednesday, June 6, 2007

Editing and threading

Recently I've implemented threading in Step. This is (still) not parallel computation, but doing all calculations in one dedicated thread to keep GUI responsive and allow the user to abort calculations at any time.

In Step the user can edit the scene while simulation is running but this can't be done in the middle of timestep. So I need to use mutex to serialize access to the scene: the mutex is locked by calculating thread while performing timestep and by GUI thread while altering objects. The problem arises when calculation of one timestep becomes too long (for example if user sets too low tolerance): if GUI tries to lock the mutex it becomes unresponsive till the end of the timestep and the user can't abort the calculation !

The obvious solution is to abort current timestep as soon as the user starts altering the scene and restart it later. But this can slow down the simulation and make it not smooth. My solution is to first try locking with timeout and abort the timestep only if locking fails:

// Try to lock but do not wait mode than one frame
if(!mutex->tryLock(1000/_simulationFps)) {
abortCurrentFrame();
mutex->lock();
}

With such approach simulation will be slowed down only if it is already slow (i.e. when simulation of one frame simulation takes longer then 1/FPS). In the other case simulation will proceed smoothly and user actions will be delayed only slightly.

Tuesday, May 29, 2007

Step in EBN

Today I've noticed that Step is in EBN and I'm very happy with it (I've always thought that playgound are not there) ! There are some issues reported by Krazy which I'll eventually fix. The only bad thing is that for some reason EBN ignores API documentation for StepCore. Or maybe I need to do something special for EBN to see it ?

Collision handling

Recently Step has finally got collision handling. There are still lots to do but basic things are working: you can create some polygons, assign them velocities and see them collide. It has got more time then I expect mainly because of Real Life (specifically my job and university).

After reading lots of papers on collision detection and checking how it is implemented in ODE and Bullet I've decided to use Gilbert-Johnson-Keerthi algorithm for collision detection and Dantzig LCP solver for constraints (but as with ODE solvers collision solvers will be easily swappable). The main choice criteria was accuracy of the algorithm and possibility to add error estimation. Implementing GJK was quite easy, extending it to provide full collision manifold instead of one arbitrary collision point was slightly harder because of various subtle numerical problems.

Next items on my TODO list is constraint solver and performance optimizations for collision solver (by broadphase filtering and using coherency between adjacent time steps). In parallel with it I'll start to work on something from my SoC program to be in time with it.