PDA

View Full Version : [MathML] problem displaying a math expression



tobast
23rd April 2013, 21:50
Hello,

Despite all my research on the web, I'm still failing to display correctly a math expression using the MathML engine of Qwt. So far, I created a minimalist "testing project", aiming to only display a MathML-formatted text fed in via a text file in the execution directory :


// includes erased here for clarity
int main(int argc, char** argv)
{
ifstream ifs("text");
std::string fullText="";
while(!ifs.eof())
{
std::string line;
getline(ifs,line);
fullText += line + "\n";
}

QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("utf-8")); // Strings in files are UTF-8 encoded
QwtText::setTextEngine(QwtText::MathMLText, new QwtMathMLTextEngine()); // Setting Qwt text engine to MathML

QApplication a(argc, argv);

QString locale = QLocale::system().name().section('_', 0, 0); // Installs a translator to the user's locale
QTranslator translator;
translator.load(QString("qt_") + locale, QLibraryInfo::location(QLibraryInfo::TranslationsP ath));
a.installTranslator(&translator);

QwtText text(QString(fullText.c_str()), QwtText::MathMLText);
QwtTextLabel* lbl = new QwtTextLabel(text);
lbl->setMinimumHeight(100);
lbl->setMinimumWidth(200);
lbl->show();


return a.exec();
}


My "text" file contains a MathML expression directly copy/pasted from Wikipedia, to display the solutions of a quadratic equation, which - I suppose - is syntaxically correct : http://pastebin.com/Nq6gYkvf.
This code results displaying only letters, not formatted at all, as seen in this screenshot :
8985

What am I doing wrong?

Thanks for your help,
Tobast.

Uwe
24th April 2013, 08:02
The MathML renderer is taken from the former Qt solutions package. I'm not sure how complete and powerful it is myself - but AFAIR the package ( no idea where to find it today ) contained a more advanced formula.

Uwe

tobast
24th April 2013, 10:43
Hello,

So, you would say that my implementation is right, but the engine cannot display that formula?

Thanks, Tobast

Uwe
25th April 2013, 07:02
Your code is o.k. - at least the way how you use Qwt - so I would expect to find the reason in the syntax of the string you are passing. To be honest I have forgotten about the details of this renderer and - as I said before - it would be best to find the example in the Qt solution package.

But as I'm interested myself I will try to write an example using MathML labels, so that it won't get lost anymore.

Uwe

agarny
7th August 2013, 16:38
Tobast/Uwe,

I was wondering whether any of you guys had managed to get the MathML renderer to work 'properly' using Qwt's MathML engine? FWIW, I was able to get things to work fine using QtMmlWidget (but I have decided to drop it in favour of Qwt).

Cheers, Alan.

agarny
8th August 2013, 09:48
FWIW, the MathML code given by Tobast is content MathML while it would seem that Qwt's MathML engine only accepts presentation MathML, which would explain the rendering Tobast got...

agarny
19th August 2013, 13:29
But as I'm interested myself I will try to write an example using MathML labels, so that it won't get lost anymore.

Uwe, I have had another look at the MathML renderer in Qwt and spotted several issues with it. I know it has never been fully tested, so it doesn't come as a big surprise to me.

Still, since you are interested in this issue, I was wondering whether you had had a chance to do anything about it or are planning to do something about it? It's just that I really need the MathML renderer to work in Qwt. So, either I wait for you to fix things or I do it myself (and then send you the code, of course, for possible revision and inclusion in Qwt). Just let me know.

Uwe
19th August 2013, 15:46
Uwe, I have had another look at the MathML renderer in Qwt and spotted several issues with it. I know it has never been fully tested, so it doesn't come as a big surprise to me.
This code is a leftover from the former Qt solutions package - all I did is to make it available as an option for being used as a QwtText plugin. In fact I don't know much about MathML myself and the quality of this renderer.

Unfortunately I havn't seen any better option for rendering formulas for Qt so far.


So, either I wait for you to fix things or I do it myself (and then send you the code, of course, for possible revision and inclusion in Qwt). Just let me know.
Please go ahead with whatever you believe that improves the implementation.

Uwe

agarny
19th August 2013, 16:11
Please go ahead with whatever you believe that improves the implementation.

Ok, I shall then. I just wanted to make sure that you were not already working on it.

agarny
22nd August 2013, 12:43
Ok, I had a bit of time yesterday to look into it and I now have Qwt's MathML renderer working with QwtTextLabel in a way which is similar to what could be done with the now discontinued QtMmlWidget.

You can find 'my' version of Qwt's MathML renderer at https://github.com/opencor/opencor/tree/master/src/plugins/3rdparty/Qwt/textengines/mathml. I 'cleaned' the code a bit, but nothing major.

I have tried the renderer with some presentation MathML code and it's working fine. At least, as fine as QtMmlWidget. Indeed, I noticed some issues with the renderer itself (see https://github.com/opencor/opencor/issues/225). I will try to fix them when I have a bit more time.

Uwe
25th August 2013, 12:45
I had a quick look at your changes and IMHO it seems to me like you are working around issues that have to do with rounding errors because of an integer based implementation. Your patches somehow fix these problems by adding some margins here changing a factor there. This might be a solution when using a formula with QwtTextLabel, but when scaling comes into the game ( f.e. a formula on the canvas painted with QwtPlotRenderer to PDF ) your approach will not be enough.

IMO a more promising approach would be to modify the renderer being completely floating point based ?

Uwe

agarny
25th August 2013, 13:23
I must confess that, so far, I have been working on getting the original QtMmlWidget's MathML renderer to work within Qwt (which is now the case). From there, I have been working on fixing some issues I have found with the original MathML renderer (still working on it).

This being said, I agree that a floating point based approach would be better. Maybe I should go through the code and, who knows, it might mean I don't have 'patch' certain things. Then again, there are some factors that were just plain wrong, so I had no choice but to 'fix' them. For example, say that you have x with i as a sub-script and j as a super-script. In this case, the MathML render uses g_script_size_multiplier to scale those scripts. However, the original value of g_script_size_multiplier is such that i and j would always overlap, hence I modified that value to 0.5 (from 0.7071) and as expected now everything is fine.

Anyway, it's still very much work in progress, but I am hoping to be done with it within the next few days. (I can't afford to spend too much time on this.) From there, I leave it to you to incorporate 'my' version of the MathML render to Qwt or not. What is certain is that you are better placed than I am to test the renderer for use within Qwt (my use of Qwt is somewhat limited).

Uwe
25th August 2013, 13:40
What about starting this way: I will set up a test application ( in qwt/playground/mathml ), where a MathML text is displayed in the center of a widget using the MathML renderer in its paint event. It will be using a transformation, that can be modified by the mouse ( scale factor + maybe a rotation ). Your contribution would be to set up a reasonable collection of MathML formulas, that can be loaded into the application.

Then we can see easily where the renderer needs polishing.

Uwe

agarny
25th August 2013, 14:01
Seems like a sensible approach to me. We should start by using a version of QtMmlWidget's MathML renderer that works with Qwt, but doesn't contain my 'fixes'/'patches'. From there, we should convert the code to use a floating point based approach. Finally, based on the results we get from the test application, we can see whether any of my 'fixes'/'patches' are still needed.

Uwe
25th August 2013, 14:52
I suggest we leave the forum and continue by Email. Please send me a couple of MathML formulas I can use as a start for the test application.

Uwe

agarny
25th August 2013, 15:13
Sorry, but I can't seem to find how to send a private message on here. Anyway, you can find some formulas here (https://github.com/opencor/opencor/issues/225) and then my email address from my GitHub account.

AlekseyK
25th May 2016, 02:32
Guys, what is the status of Your current development? Is it ready to use: http://www.qtcentre.org/threads/66100-Is-MathML-workable-in-Qwt ?

agarny
25th May 2016, 07:07
Uwe and I did exchange quite a few emails, and I ended up reworking the MathML engine quite a bit. I haven't done anything about it for quite a while, so I can't remember whether the version at https://github.com/uwerat/qwt-mml-dev is still up to date. So, just in case, you can find 'my' version at https://github.com/opencor/opencor/tree/master/src/plugins/thirdParty/Qwt/textengines/mathml. What is sure is that the equation you mention at http://www.qtcentre.org/threads/66100-Is-MathML-workable-in-Qwt can now safely be rendered.

AlekseyK
25th May 2016, 15:58
So, just in case, you can find 'my' version at https://github.com/opencor/opencor/tree/master/src/plugins/thirdParty/Qwt/textengines/mathml. What is sure is that the equation you mention at http://www.qtcentre.org/threads/66100-Is-MathML-workable-in-Qwt can now safely be rendered.

agarny, so You version mentioned above works fine, correct? Will try to replace qwt sources by Your version and rebuild. Thanks!

AlekseyK
25th May 2016, 21:43
agarny, Your sources works fine. Thank You very much!!!

Some bugs however:



formula background is white, need to be gray as button default color. autoFillBackground does not help.
Aslo font color changing through palette propery has effect: only for plain text, for MathML engine still black.
QwtText::setColor() has no effect
QwtText::setBackgroundBrush() has no effect
QwtText::setPaintAttribute() for color or background true or false has no effect


Any way to fix?

Note: need to add qwt_mml_entity_table.h/cpp files to mathml.pro also.

Uwe, any chance to include this patches into official version? Thanks!

agarny
30th May 2016, 22:25
Sorry AlekseyK, qtcentre.org wasn't working for me these past few days. Anyway, glad that the MathML renderer works for you.

Regarding the issues that you have found:

formula background is white, need to be gray as button default color. autoFillBackground does not help.
I made it white because that seemed more 'natural' to me, not to mention that this widget is not a button, so I don't see why it should be grey.
Aslo font color changing through palette propery has effect: only for plain text, for MathML engine still black.
I am not sure what you mean by this, sorry.
QwtText::setColor() has no effect.
QwtText::setBackgroundBrush() has no effect.
QwtText::setPaintAttribute() for color or background true or false has no effect.
I am not sure to what you are referring here. I mean, the widget is QwtMathMLDocument, so...?
need to add qwt_mml_entity_table.h/cpp files to mathml.pro also.
Quite possible. Personally, I use CMake, so...


I am sure there must be a way to fix your 'issues'. Unfortunately, I really don't have time for it at the moment, not least since it has yet to be officially included in Qwt. However, if Uwe was to officially incorporate my changes into Qwt, then I would certainly try to find the time to iron things out.

AlekseyK
31st May 2016, 00:03
Sorry AlekseyK, qtcentre.org wasn't working for me these past few days.
It was down.


Anyway, glad that the MathML renderer works for you.
Thank You very much for this.


Regarding the issues that you have found:
[LIST]
formula background is white, need to be gray as button default color. autoFillBackground does not help.
I made it white because that seemed more 'natural' to me, not to mention that this widget is not a button, so I don't see why it should be grey.
For document - yes. For label it should have label background, like in my example: http://www.qtcentre.org/attachment.php?attachmentid=11959&d=1464138969&thumb=1


Aslo font color changing through palette propery has effect: only for plain text, for MathML engine still black.
I am not sure what you mean by this, sorry.
Formula font color. It is always black. I'd like to change it.


QwtText::setColor() has no effect.
QwtText::setBackgroundBrush() has no effect.
QwtText::setPaintAttribute() for color or background true or false has no effect.
I am not sure to what you are referring here. I mean, the widget is QwtMathMLDocument, so...?
I use QwtTextLabel.


I am sure there must be a way to fix your 'issues'. Unfortunately, I really don't have time for it at the moment, not least since it has yet to be officially included in Qwt. However, if Uwe was to officially incorporate my changes into Qwt, then I would certainly try to find the time to iron things out.
Thank You. Hope Uwe will see this conversation and add patches to official Qwt sources.

Uwe
31st May 2016, 09:35
Hope Uwe will see this conversation and add patches to official Qwt sources.
Unfortunately I lost a lot of time with implementing all the spline interpolation algos ( out of personal interest ) and I'm also working on another open source project, what is the reason why I'm heavily delayed with 6.2 - at least I hope I can release 6.1.3 soon.

Alans version of the MathML stuff can be seen as the official version - it is planned to become part of Qwt 6.3. But always keep in mind, that its not my priority to offer a first class MathML package - all I ( and of course Alan in the first place ) did was to turn the orphaned MathML implementation from the Qt solutions package into something "usable".

Uwe

AlekseyK
31st May 2016, 09:59
Thank You, Uwe! Will wait for qwt updates. Of course good to have Alan's version earlier: in 6.2 or 6.1.3. :) Alan's version works fine, only good to fix issues mentioned above.