include/svx/charmap.hxx             |    3 
 include/svx/searchcharmap.hxx       |    2 
 svx/source/dialog/charmap.cxx       |   13 ++-
 svx/source/dialog/searchcharmap.cxx |  152 +-----------------------------------
 4 files changed, 20 insertions(+), 150 deletions(-)

New commits:
commit 94d2d9a59450fa4bccd0a3a450d8b273271d2ff2
Author:     AmosAidoo <aidooamo...@gmail.com>
AuthorDate: Tue Nov 21 03:17:12 2023 +0100
Commit:     Andreas Heinisch <andreas.heini...@yahoo.de>
CommitDate: Wed Nov 22 21:49:57 2023 +0100

    tdf#154088 Merge code snippets for character retrieval and string 
construction
    
    The DrawChars_Impl method has been unified since the duplicates were 
identical
    and only differed in how they retrieved characters from a separate source. 
Each
    class now has its own implentation of GetCharFromIndex as a solution.
    
    Change-Id: Ic2d10a46a91ea8dee2c94b620e6745bf77ae9eab
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159773
    Tested-by: Jenkins
    Reviewed-by: Hossein <hoss...@libreoffice.org>

diff --git a/include/svx/charmap.hxx b/include/svx/charmap.hxx
index 8057eae3c1bd..1c440b6ee2c1 100644
--- a/include/svx/charmap.hxx
+++ b/include/svx/charmap.hxx
@@ -66,6 +66,7 @@ public:
 
     void                    SelectCharacter( sal_UCS4 cNew );
     virtual sal_UCS4        GetSelectCharacter() const;
+    virtual sal_UCS4        GetCharFromIndex(int index) const;
     void                    createContextMenu(const Point& rPosition);
 
     void            SetDoubleClickHdl( const Link<SvxShowCharSet*,void>& rLink 
) { aDoubleClkHdl = rLink; }
@@ -148,7 +149,7 @@ protected:
 
 protected:
     virtual bool KeyInput(const KeyEvent&) override;
-    virtual void            DrawChars_Impl(vcl::RenderContext& rRenderContext, 
int n1, int n2);
+    void            DrawChars_Impl(vcl::RenderContext& rRenderContext, int n1, 
int n2);
     void            InitSettings(vcl::RenderContext& rRenderContext);
     // abstraction layers are: Unicode<->MapIndex<->Pixel
     Point           MapIndexToPixel( int) const;
diff --git a/include/svx/searchcharmap.hxx b/include/svx/searchcharmap.hxx
index d05f3ea82728..b07d58a4c8ad 100644
--- a/include/svx/searchcharmap.hxx
+++ b/include/svx/searchcharmap.hxx
@@ -44,6 +44,7 @@ public:
 
     void                                SelectCharacter( const Subset* sub);
     virtual sal_UCS4                    GetSelectCharacter() const override;
+    virtual sal_UCS4                    GetCharFromIndex(int index) const 
override;
 
     virtual svx::SvxShowCharSetItem*    ImplGetItem( int _nPos ) override;
     virtual int                         LastInView() const override;
@@ -59,7 +60,6 @@ private:
     //to uniquely identify each appended element
     std::unordered_map<sal_Int32, sal_UCS4> m_aItemList;
 private:
-    virtual void DrawChars_Impl(vcl::RenderContext& rRenderContext, int n1, 
int n2) override;
     virtual void Paint(vcl::RenderContext& rRenderContext, const 
tools::Rectangle& rRect) override;
     virtual bool KeyInput(const KeyEvent& rKEvt) override;
 };
diff --git a/svx/source/dialog/charmap.cxx b/svx/source/dialog/charmap.cxx
index be2d991977b5..dcb1205584d7 100644
--- a/svx/source/dialog/charmap.cxx
+++ b/svx/source/dialog/charmap.cxx
@@ -534,12 +534,17 @@ void SvxShowCharSet::DrawChars_Impl(vcl::RenderContext& 
rRenderContext, int n1,
     tools::Rectangle aBoundRect;
     for (i = n1; i <= n2; ++i)
     {
+        sal_UCS4 charValue = GetCharFromIndex(i);
+
+        if (charValue == 0)
+            continue;
+
+        OUString aCharStr(&charValue, 1);
+
         Point pix = MapIndexToPixel(i);
         int x = pix.X();
         int y = pix.Y();
 
-        sal_UCS4 nChar = mxFontCharMap->GetCharFromIndex(i);
-        OUString aCharStr(&nChar, 1);
         int nTextWidth = rRenderContext.GetTextWidth(aCharStr);
         int tx = x + (nX - nTextWidth + 1) / 2;
         int ty = y + (nY - nTextHeight + 1) / 2;
@@ -672,6 +677,10 @@ sal_UCS4 SvxShowCharSet::GetSelectCharacter() const
     return getSelectedChar();
 }
 
+sal_UCS4 SvxShowCharSet::GetCharFromIndex(int index) const
+{
+    return mxFontCharMap->GetCharFromIndex(index);
+}
 
 void SvxShowCharSet::RecalculateFont(vcl::RenderContext& rRenderContext)
 {
diff --git a/svx/source/dialog/searchcharmap.cxx 
b/svx/source/dialog/searchcharmap.cxx
index dc3ef7bcecab..2a770eac57de 100644
--- a/svx/source/dialog/searchcharmap.cxx
+++ b/svx/source/dialog/searchcharmap.cxx
@@ -140,6 +140,12 @@ void SvxSearchCharSet::SelectCharacter( const Subset* sub )
     Invalidate();
 }
 
+sal_UCS4 SvxSearchCharSet::GetCharFromIndex(int index) const
+{
+    std::unordered_map<sal_Int32, sal_UCS4>::const_iterator got = 
m_aItemList.find(index);
+    return (got != m_aItemList.end()) ? got->second : 0;
+}
+
 void SvxSearchCharSet::Paint(vcl::RenderContext& rRenderContext, const 
tools::Rectangle&)
 {
     InitSettings(rRenderContext);
@@ -147,152 +153,6 @@ void SvxSearchCharSet::Paint(vcl::RenderContext& 
rRenderContext, const tools::Re
     DrawChars_Impl(rRenderContext, FirstInView(), LastInView());
 }
 
-void SvxSearchCharSet::DrawChars_Impl(vcl::RenderContext& rRenderContext, int 
n1, int n2)
-{
-    if (n1 > LastInView() || n2 < FirstInView())
-        return;
-
-    Size aOutputSize(GetOutputSizePixel());
-
-    const StyleSettings& rStyleSettings = 
Application::GetSettings().GetStyleSettings();
-    const Color aWindowTextColor(rStyleSettings.GetFieldTextColor());
-    Color aHighlightColor(rStyleSettings.GetHighlightColor());
-    Color aHighlightTextColor(rStyleSettings.GetHighlightTextColor());
-    Color aFaceColor(rStyleSettings.GetFaceColor());
-    Color aLightColor(rStyleSettings.GetLightColor());
-    Color aShadowColor(rStyleSettings.GetShadowColor());
-
-    int i;
-    rRenderContext.SetLineColor(aShadowColor);
-    for (i = 1; i < COLUMN_COUNT; ++i)
-    {
-        rRenderContext.DrawLine(Point(nX * i + m_nXGap, 0),
-                          Point(nX * i + m_nXGap, aOutputSize.Height()));
-    }
-    for (i = 1; i < ROW_COUNT; ++i)
-    {
-        rRenderContext.DrawLine(Point(0, nY * i + m_nYGap),
-                                Point(aOutputSize.Width(), nY * i + m_nYGap));
-    }
-
-    int nTextHeight = rRenderContext.GetTextHeight();
-    tools::Rectangle aBoundRect;
-    for (i = n1; i <= n2; ++i)
-    {
-        Point pix = MapIndexToPixel(i);
-        int x = pix.X();
-        int y = pix.Y();
-
-        OUStringBuffer buf;
-        std::unordered_map<sal_Int32, sal_UCS4>::const_iterator got = 
m_aItemList.find (i);
-        sal_UCS4 sName;
-
-        if(got == m_aItemList.end())
-            continue;
-        else
-            sName = got->second;
-
-        buf.appendUtf32(sName);
-        OUString aCharStr(buf.makeStringAndClear());
-        int nTextWidth = rRenderContext.GetTextWidth(aCharStr);
-        int tx = x + (nX - nTextWidth + 1) / 2;
-        int ty = y + (nY - nTextHeight + 1) / 2;
-        Point aPointTxTy(tx, ty);
-
-        // adjust position before it gets out of bounds
-        if (rRenderContext.GetTextBoundRect(aBoundRect, aCharStr) && 
!aBoundRect.IsEmpty())
-        {
-            // zero advance width => use ink width to center glyph
-            if (!nTextWidth)
-            {
-                aPointTxTy.setX( x - aBoundRect.Left() + (nX - 
aBoundRect.GetWidth() + 1) / 2 );
-            }
-
-            aBoundRect += aPointTxTy;
-
-            // shift back vertically if needed
-            int nYLDelta = aBoundRect.Top() - y;
-            int nYHDelta = (y + nY) - aBoundRect.Bottom();
-            if (nYLDelta <= 0)
-                aPointTxTy.AdjustY( -(nYLDelta - 1) );
-            else if (nYHDelta <= 0)
-                aPointTxTy.AdjustY(nYHDelta - 1 );
-
-            // shift back horizontally if needed
-            int nXLDelta = aBoundRect.Left() - x;
-            int nXHDelta = (x + nX) - aBoundRect.Right();
-            if (nXLDelta <= 0)
-                aPointTxTy.AdjustX( -(nXLDelta - 1) );
-            else if (nXHDelta <= 0)
-                aPointTxTy.AdjustX(nXHDelta - 1 );
-        }
-
-        // tdf#109214 - highlight the favorite characters
-        if (isFavChar(aCharStr, mxVirDev->GetFont().GetFamilyName()))
-        {
-            const Color aLineCol = rRenderContext.GetLineColor();
-            rRenderContext.SetLineColor(aHighlightColor);
-            rRenderContext.SetFillColor(COL_TRANSPARENT);
-            // Outer border
-            rRenderContext.DrawRect(tools::Rectangle(Point(x - 1, y - 1), 
Size(nX + 3, nY + 3)), 1, 1);
-            // Inner border
-            rRenderContext.DrawRect(tools::Rectangle(Point(x, y), Size(nX + 1, 
nY + 1)), 1, 1);
-            rRenderContext.SetLineColor(aLineCol);
-        }
-
-        Color aTextCol = rRenderContext.GetTextColor();
-        if (i != nSelectedIndex)
-        {
-            rRenderContext.SetTextColor(aWindowTextColor);
-            rRenderContext.DrawText(aPointTxTy, aCharStr);
-        }
-        else
-        {
-            Color aLineCol = rRenderContext.GetLineColor();
-            Color aFillCol = rRenderContext.GetFillColor();
-            rRenderContext.SetLineColor();
-            Point aPointUL(x + 1, y + 1);
-            if (HasFocus())
-            {
-                rRenderContext.SetFillColor(aHighlightColor);
-                rRenderContext.DrawRect(getGridRectangle(aPointUL, 
aOutputSize));
-
-                rRenderContext.SetTextColor(aHighlightTextColor);
-                rRenderContext.DrawText(aPointTxTy, aCharStr);
-            }
-            else
-            {
-                rRenderContext.SetFillColor(aFaceColor);
-                rRenderContext.DrawRect(getGridRectangle(aPointUL, 
aOutputSize));
-
-                rRenderContext.SetLineColor(aLightColor);
-                rRenderContext.DrawLine(aPointUL, Point(x + nX - 1, y + 1));
-                rRenderContext.DrawLine(aPointUL, Point(x + 1, y + nY - 1));
-
-                rRenderContext.SetLineColor(aShadowColor);
-                rRenderContext.DrawLine(Point(x + 1, y + nY - 1), Point(x + nX 
- 1, y + nY - 1));
-                rRenderContext.DrawLine(Point(x + nX - 1, y + nY - 1), Point(x 
+ nX - 1, y + 1));
-
-                rRenderContext.DrawText(aPointTxTy, aCharStr);
-            }
-            rRenderContext.SetLineColor(aLineCol);
-            rRenderContext.SetFillColor(aFillCol);
-        }
-        rRenderContext.SetTextColor(aTextCol);
-    }
-
-    // tdf#141319 - mark empty/unused cells
-    if (n2 - n1 < ROW_COUNT * COLUMN_COUNT)
-    {
-        rRenderContext.SetFillColor(rStyleSettings.GetDisableColor());
-        for (i = n2 - n1 + 1; i < ROW_COUNT * COLUMN_COUNT; i++)
-        {
-            rRenderContext.DrawRect(
-                tools::Rectangle(MapIndexToPixel(i + n1), Size(nX + 2, nY + 
2)));
-        }
-    }
-}
-
 sal_UCS4 SvxSearchCharSet::GetSelectCharacter() const
 {
     if( nSelectedIndex >= 0 )

Reply via email to