[Libreoffice-commits] core.git: starmath/qa starmath/source

Takeshi Abe tabe at fixedpoint.jp
Fri Feb 27 04:05:47 PST 2015


 starmath/qa/extras/data/maction.mml   |   10 ++++
 starmath/qa/extras/mmlimport-test.cxx |    9 ++++
 starmath/source/mathmlimport.cxx      |   70 +++++++++++++++++++++++++++++++---
 starmath/source/mathmlimport.hxx      |    6 ++
 4 files changed, 89 insertions(+), 6 deletions(-)

New commits:
commit fe43c9227bd77c6471126b2553820c14b4721d6f
Author: Takeshi Abe <tabe at fixedpoint.jp>
Date:   Tue Feb 24 13:55:58 2015 +0900

    starmath: render the selected subexpression of MathML's <maction>
    
    ... specified by the selection attribute.
    For its expected behavior, see the section <maction> in MathML 1.01:
    <http://www.w3.org/TR/REC-MathML/chap3_6.html#sec3.6.1>
    
    Change-Id: I70c1b2cfe1afec730f3e67ba0938bbaf8ada8e23
    Reviewed-on: https://gerrit.libreoffice.org/14600
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>
    Tested-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/starmath/qa/extras/data/maction.mml b/starmath/qa/extras/data/maction.mml
new file mode 100644
index 0000000..3650087
--- /dev/null
+++ b/starmath/qa/extras/data/maction.mml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<math xmlns="http://www.w3.org/1998/Math/MathML">
+  <mrow>
+     <mtable>
+        <mtr><maction actiontype="toggle"><mn>1</mn><mn>0</mn><mn>0</mn></maction></mtr>
+        <mtr><maction actiontype="toggle" selection="2"><mn>0</mn><mn>2</mn><mn>0</mn></maction></mtr>
+        <mtr><maction actiontype="toggle" selection="3"><mn>0</mn><mn>0</mn><mn>3</mn></maction></mtr>
+     </mtable>
+  </mrow>
+</math>
diff --git a/starmath/qa/extras/mmlimport-test.cxx b/starmath/qa/extras/mmlimport-test.cxx
index 2fbeda7..adf80aa 100644
--- a/starmath/qa/extras/mmlimport-test.cxx
+++ b/starmath/qa/extras/mmlimport-test.cxx
@@ -30,9 +30,11 @@ public:
     virtual void tearDown() SAL_OVERRIDE;
 
     void testSimple();
+    void testMaction();
 
     CPPUNIT_TEST_SUITE(Test);
     CPPUNIT_TEST(testSimple);
+    CPPUNIT_TEST(testMaction);
     CPPUNIT_TEST_SUITE_END();
 
 private:
@@ -83,6 +85,13 @@ void Test::testSimple()
     loadURL(getURLFromSrc("starmath/qa/extras/data/simple.mml"));
 }
 
+void Test::testMaction()
+{
+    loadURL(getURLFromSrc("starmath/qa/extras/data/maction.mml"));
+    OUString sExpected("matrix {italic \"1\" ## italic \"2\" ## italic \"3\"}");
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("loaded text", sExpected, mxDocShell->GetText());
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
 
 }
diff --git a/starmath/source/mathmlimport.cxx b/starmath/source/mathmlimport.cxx
index 488797e..8a42baf 100644
--- a/starmath/source/mathmlimport.cxx
+++ b/starmath/source/mathmlimport.cxx
@@ -1756,12 +1756,16 @@ public:
 
 class SmXMLActionContext_Impl : public SmXMLRowContext_Impl
 {
+    size_t mnSelection; // 1-based
+
 public:
     SmXMLActionContext_Impl(SmXMLImport &rImport,sal_uInt16 nPrefix,
         const OUString& rLName) :
         SmXMLRowContext_Impl(rImport,nPrefix,rLName)
+      , mnSelection(1)
         {}
 
+    void StartElement(const uno::Reference<xml::sax::XAttributeList> &xAttrList) SAL_OVERRIDE;
     void EndElement() SAL_OVERRIDE;
 };
 
@@ -1957,6 +1961,13 @@ static const SvXMLTokenMapEntry aColorTokenMap[] =
     XML_TOKEN_MAP_END
 };
 
+static const SvXMLTokenMapEntry aActionAttrTokenMap[] =
+{
+    { XML_NAMESPACE_MATH,   XML_SELECTION,      XML_TOK_SELECTION },
+    XML_TOKEN_MAP_END
+};
+
+
 const SvXMLTokenMap& SmXMLImport::GetPresLayoutElemTokenMap()
 {
     if (!pPresLayoutElemTokenMap)
@@ -2022,6 +2033,12 @@ const SvXMLTokenMap& SmXMLImport::GetColorTokenMap()
     return *pColorTokenMap;
 }
 
+const SvXMLTokenMap& SmXMLImport::GetActionAttrTokenMap()
+{
+    if (!pActionAttrTokenMap)
+        pActionAttrTokenMap = new SvXMLTokenMap(aActionAttrTokenMap);
+    return *pActionAttrTokenMap;
+}
 
 
 SvXMLImportContext *SmXMLDocContext_Impl::CreateChildContext(
@@ -2589,18 +2606,58 @@ void SmXMLMultiScriptsContext_Impl::EndElement()
     ProcessSubSupPairs(bHasPrescripts);
 }
 
-void SmXMLActionContext_Impl::EndElement()
+void SmXMLActionContext_Impl::StartElement(const uno::Reference<xml::sax::XAttributeList> & xAttrList)
 {
-    /*For now we will just assume that the
-     selected attribute is one, and then just display
-     that expression alone, i.e. remove all expect the
-     first pushed one*/
+    sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
+    for (sal_Int16 i=0;i<nAttrCount;i++)
+    {
+        OUString sAttrName = xAttrList->getNameByIndex(i);
+        OUString aLocalName;
+        sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
+            GetKeyByAttrName(sAttrName,&aLocalName);
 
+        OUString sValue = xAttrList->getValueByIndex(i);
+        const SvXMLTokenMap &rAttrTokenMap =
+            GetSmImport().GetActionAttrTokenMap();
+        switch(rAttrTokenMap.Get(nPrefix,aLocalName))
+        {
+            case XML_TOK_SELECTION:
+                {
+                    sal_uInt32 n = sValue.toUInt32();
+                    if (n > 0) mnSelection = static_cast<size_t>(n);
+                }
+                break;
+            default:
+                break;
+        }
+    }
+}
+
+void SmXMLActionContext_Impl::EndElement()
+{
     SmNodeStack &rNodeStack = GetSmImport().GetNodeStack();
-    for (auto i=rNodeStack.size()-nElementCount;i > 1;i--)
+    auto nSize = rNodeStack.size();
+    if (nSize <= nElementCount) {
+        // not compliant to maction's specification, e.g., no subexpressions
+        return;
+    }
+    assert(mnSelection > 0);
+    if (nSize < nElementCount + mnSelection) {
+        // No selected subexpression exists, which is a MathML error;
+        // fallback to selecting the first
+        mnSelection = 1;
+    }
+    assert(nSize >= nElementCount + mnSelection);
+    for (auto i=nSize-(nElementCount+mnSelection); i > 0; i--)
+    {
+        rNodeStack.pop_front();
+    }
+    auto pSelected = rNodeStack.pop_front();
+    for (auto i=rNodeStack.size()-nElementCount; i > 0; i--)
     {
         rNodeStack.pop_front();
     }
+    rNodeStack.push_front(pSelected.release());
 }
 
 SvXMLImportContext *SmXMLImport::CreateContext(sal_uInt16 nPrefix,
@@ -2858,6 +2915,7 @@ SmXMLImport::~SmXMLImport() throw ()
     delete pColorTokenMap;
     delete pOperatorAttrTokenMap;
     delete pAnnotationAttrTokenMap;
+    delete pActionAttrTokenMap;
 }
 
 void SmXMLImport::SetViewSettings(const Sequence<PropertyValue>& aViewProps)
diff --git a/starmath/source/mathmlimport.hxx b/starmath/source/mathmlimport.hxx
index 0a1e700..c46742b 100644
--- a/starmath/source/mathmlimport.hxx
+++ b/starmath/source/mathmlimport.hxx
@@ -80,6 +80,7 @@ class SmXMLImport : public SvXMLImport
         SvXMLTokenMap *pPresScriptEmptyElemTokenMap;
         SvXMLTokenMap *pPresTableElemTokenMap;
         SvXMLTokenMap *pColorTokenMap;
+        SvXMLTokenMap *pActionAttrTokenMap;
 
         SmNodeStack aNodeStack;
         bool bSuccess;
@@ -241,6 +242,7 @@ public:
     const SvXMLTokenMap &GetPresScriptEmptyElemTokenMap();
     const SvXMLTokenMap &GetPresTableElemTokenMap();
     const SvXMLTokenMap &GetColorTokenMap();
+    const SvXMLTokenMap &GetActionAttrTokenMap();
 
     SmNodeStack & GetNodeStack()    { return aNodeStack; }
     SmNode *GetTree()
@@ -339,6 +341,10 @@ enum SmXMLAnnotationAttrTokenMap
     XML_TOK_ENCODING
 };
 
+enum SmXMLActionAttrTokenMap
+{
+    XML_TOK_SELECTION
+};
 
 
 #endif


More information about the Libreoffice-commits mailing list