135360
From 0ad2976627386e126ae310d85aa6d771b3df1353 Mon Sep 17 00:00:00 2001
135360
From: Pranav Kant <pranavk@gnome.org>
135360
Date: Thu, 4 Jun 2015 22:09:57 +0530
135360
Subject: [PATCH 017/398] lokdocview, tilebuffer: clean up
135360
135360
Improve documentation, style fixes
135360
135360
Change-Id: I5000e32e90cd8e3b75e8df2907673efc303a55fd
135360
(cherry picked from commit c074cfa4d48736d1703949ccfe1a6c534a2742ae)
135360
---
135360
 libreofficekit/source/gtk/lokdocview.cxx |  59 ++++++++++-------
135360
 libreofficekit/source/gtk/tilebuffer.cxx | 104 +++++++++++++++++-------------
135360
 libreofficekit/source/gtk/tilebuffer.hxx | 107 +++++++++++++++++++++++++------
135360
 3 files changed, 182 insertions(+), 88 deletions(-)
135360
135360
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
135360
index cfa71a357f22..2181dc9a21b1 100644
135360
--- a/libreofficekit/source/gtk/lokdocview.cxx
135360
+++ b/libreofficekit/source/gtk/lokdocview.cxx
135360
@@ -1,4 +1,4 @@
135360
-/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
135360
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
135360
 /*
135360
  * This file is part of the LibreOffice project.
135360
  *
135360
@@ -177,12 +177,12 @@ struct LOKDocView_Impl
135360
     /// Implementation of the timeout handler, invoked by handleTimeout().
135360
     gboolean handleTimeoutImpl();
135360
     /**
135360
-     * Renders the document to a number of tiles.
135360
+     * Renders the document to a number of visible tiles.
135360
      *
135360
      * This method is invoked only manually, not when some Gtk signal is
135360
      * emitted.
135360
      *
135360
-     * @param pPartial if 0, then the full document is rendered, otherwise only
135360
+     * @param pPartial if 0, then the full visible document is rendered, otherwise only
135360
      * the tiles that intersect with pPartial.
135360
      */
135360
     void renderDocument(GdkRectangle* pPartial);
135360
@@ -665,7 +665,7 @@ void LOKDocView_Impl::setTilesInvalid(const GdkRectangle& rRectangle)
135360
 
135360
     for (int i = aStart.x; i < aEnd.x; i++)
135360
         for (int j = aStart.y; j < aEnd.y; j++)
135360
-            m_pTileBuffer->tile_buffer_set_invalid(i, j);
135360
+            m_pTileBuffer->setInvalid(i, j);
135360
 }
135360
 
135360
 void LOKDocView_Impl::renderHandle(cairo_t* pCairo, const GdkRectangle& rCursor, cairo_surface_t* pHandle, GdkRectangle& rRectangle)
135360
@@ -801,7 +801,8 @@ void LOKDocView_Impl::renderDocument(GdkRectangle* pPartial)
135360
             GdkRectangle aTileRectangleTwips, aTileRectanglePixels;
135360
             bool bPaint = true;
135360
 
135360
-            // Determine size of the tile: the rightmost/bottommost tiles may be smaller and we need the size to decide if we need to repaint.
135360
+            // Determine size of the tile: the rightmost/bottommost tiles may
135360
+            // be smaller, and we need the size to decide if we need to repaint.
135360
             if (nColumn == nColumns - 1)
135360
                 aTileRectanglePixels.width = nDocumentWidthPixels - nColumn * nTileSizePixels;
135360
             else
135360
@@ -811,7 +812,8 @@ void LOKDocView_Impl::renderDocument(GdkRectangle* pPartial)
135360
             else
135360
                 aTileRectanglePixels.height = nTileSizePixels;
135360
 
135360
-            // Determine size and position of the tile in document coordinates, so we can decide if we can skip painting for partial rendering.
135360
+            // Determine size and position of the tile in document coordinates,
135360
+            // so we can decide if we can skip painting for partial rendering.
135360
             aTileRectangleTwips.x = pixelToTwip(nTileSizePixels, m_fZoom) * nColumn;
135360
             aTileRectangleTwips.y = pixelToTwip(nTileSizePixels, m_fZoom) * nRow;
135360
             aTileRectangleTwips.width = pixelToTwip(aTileRectanglePixels.width, m_fZoom);
135360
@@ -824,12 +826,14 @@ void LOKDocView_Impl::renderDocument(GdkRectangle* pPartial)
135360
 
135360
             if (bPaint)
135360
             {
135360
-                //                g_info("gettile: (%d %d)", nRow, nColumn);
135360
+                g_info("tile_buffer_get_tile (%d, %d)", nRow, nColumn);
135360
 
135360
-                Tile& currentTile = m_pTileBuffer->tile_buffer_get_tile(nRow, nColumn);
135360
-                GdkPixbuf* pPixBuf = currentTile.tile_get_buffer();
135360
+                Tile& currentTile = m_pTileBuffer->getTile(nRow, nColumn);
135360
+                GdkPixbuf* pPixBuf = currentTile.getBuffer();
135360
 
135360
-                gdk_cairo_set_source_pixbuf (pcairo, pPixBuf, twipToPixel(aTileRectangleTwips.x, m_fZoom), twipToPixel(aTileRectangleTwips.y, m_fZoom));
135360
+                gdk_cairo_set_source_pixbuf (pcairo, pPixBuf,
135360
+                                             twipToPixel(aTileRectangleTwips.x, m_fZoom),
135360
+                                             twipToPixel(aTileRectangleTwips.y, m_fZoom));
135360
                 cairo_paint(pcairo);
135360
             }
135360
         }
135360
@@ -942,7 +946,7 @@ gboolean LOKDocView_Impl::callbackImpl(CallbackData* pCallback)
135360
         }
135360
         else
135360
         {
135360
-            m_pTileBuffer->tile_buffer_reset_all_tiles();
135360
+            m_pTileBuffer->resetAllTiles();
135360
             renderDocument(0);
135360
         }
135360
     }
135360
@@ -1148,21 +1152,28 @@ static void lok_docview_init( GTypeInstance* pInstance, gpointer )
135360
     gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(pDocView),
135360
                                            pDocView->m_pImpl->m_pDrawingArea );
135360
 
135360
-    g_signal_connect(GTK_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
+    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
                      "expose-event",
135360
-                     GTK_SIGNAL_FUNC(LOKDocView_Impl::on_exposed), pDocView);
135360
-    g_signal_connect(GTK_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
+                     G_CALLBACK(LOKDocView_Impl::on_exposed), pDocView);
135360
+    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
                      "expose-event",
135360
-                     GTK_SIGNAL_FUNC(LOKDocView_Impl::renderOverlay), pDocView);
135360
+                     G_CALLBACK(LOKDocView_Impl::renderOverlay), pDocView);
135360
     gtk_widget_add_events(pDocView->m_pImpl->m_pDrawingArea,
135360
-                          GDK_BUTTON_PRESS_MASK
135360
-                          | GDK_BUTTON_RELEASE_MASK
135360
-                          | GDK_BUTTON_MOTION_MASK);
135360
-    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea), "button-press-event", G_CALLBACK(LOKDocView_Impl::signalButton), pDocView);
135360
-    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea), "button-release-event", G_CALLBACK(LOKDocView_Impl::signalButton), pDocView);
135360
-    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea), "motion-notify-event", G_CALLBACK(LOKDocView_Impl::signalMotion), pDocView);
135360
-
135360
-    gtk_signal_connect(GTK_OBJECT(pDocView), "destroy", GTK_SIGNAL_FUNC(LOKDocView_Impl::destroy), 0);
135360
+                           GDK_BUTTON_PRESS_MASK
135360
+                          |GDK_BUTTON_RELEASE_MASK
135360
+                          |GDK_BUTTON_MOTION_MASK);
135360
+
135360
+    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
+                     "button-press-event",
135360
+                     G_CALLBACK(LOKDocView_Impl::signalButton), pDocView);
135360
+    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
+                     "button-release-event",
135360
+                     G_CALLBACK(LOKDocView_Impl::signalButton), pDocView);
135360
+    g_signal_connect(G_OBJECT(pDocView->m_pImpl->m_pDrawingArea),
135360
+                     "motion-notify-event",
135360
+                     G_CALLBACK(LOKDocView_Impl::signalMotion), pDocView);
135360
+
135360
+    g_signal_connect(G_OBJECT(pDocView), "destroy", G_CALLBACK(LOKDocView_Impl::destroy), 0);
135360
 }
135360
 
135360
 SAL_DLLPUBLIC_EXPORT guint lok_docview_get_type()
135360
@@ -1253,7 +1264,7 @@ SAL_DLLPUBLIC_EXPORT void lok_docview_set_zoom ( LOKDocView* pDocView, float fZo
135360
     guint nRows = ceil((double)nDocumentHeightPixels / nTileSizePixels);
135360
     guint nColumns = ceil((double)nDocumentWidthPixels / nTileSizePixels);
135360
 
135360
-    pDocView->m_pImpl->m_pTileBuffer->tile_buffer_set_zoom(fZoom, nRows, nColumns);
135360
+    pDocView->m_pImpl->m_pTileBuffer->setZoom(fZoom, nRows, nColumns);
135360
 
135360
     if ( pDocView->m_pImpl->m_pDocument )
135360
         pDocView->m_pImpl->renderDocument(0);
135360
diff --git a/libreofficekit/source/gtk/tilebuffer.cxx b/libreofficekit/source/gtk/tilebuffer.cxx
135360
index 3e5e01f8d686..338038078ab3 100644
135360
--- a/libreofficekit/source/gtk/tilebuffer.cxx
135360
+++ b/libreofficekit/source/gtk/tilebuffer.cxx
135360
@@ -1,4 +1,4 @@
135360
-/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
135360
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
135360
 /*
135360
  * This file is part of the LibreOffice project.
135360
  *
135360
@@ -9,6 +9,10 @@
135360
 
135360
 #include "tilebuffer.hxx"
135360
 
135360
+/* ------------------
135360
+   Utility functions
135360
+   ------------------
135360
+*/
135360
 float pixelToTwip(float fInput, float zoom)
135360
 {
135360
     return (fInput / DPI / zoom) * 1440.0f;
135360
@@ -19,84 +23,96 @@ float twipToPixel(float fInput, float zoom)
135360
     return fInput / 1440.0f * DPI * zoom;
135360
 }
135360
 
135360
-GdkPixbuf* Tile::tile_get_buffer()
135360
+/* ----------------------------
135360
+   Tile class member functions
135360
+   ----------------------------
135360
+*/
135360
+GdkPixbuf* Tile::getBuffer()
135360
 {
135360
     return m_pBuffer;
135360
 }
135360
 
135360
-void Tile::tile_release()
135360
+void Tile::release()
135360
 {
135360
     g_object_unref (m_pBuffer);
135360
     m_pBuffer = NULL;
135360
 }
135360
 
135360
-void TileBuffer::tile_buffer_set_zoom(float newZoomFactor, int rows, int columns)
135360
+void Tile::setPixbuf(GdkPixbuf *buffer)
135360
+{
135360
+    m_pBuffer = buffer;
135360
+}
135360
+
135360
+/* ----------------------------------
135360
+   TileBuffer class member functions
135360
+   ----------------------------------
135360
+*/
135360
+void TileBuffer::setZoom(float newZoomFactor, int rows, int columns)
135360
 {
135360
     m_fZoomFactor = newZoomFactor;
135360
 
135360
-    tile_buffer_reset_all_tiles();
135360
+    resetAllTiles();
135360
 
135360
     // set new buffer width and height
135360
     m_nWidth = columns;
135360
     m_nHeight = rows;
135360
 }
135360
 
135360
-void TileBuffer::tile_buffer_reset_all_tiles()
135360
+void TileBuffer::resetAllTiles()
135360
 {
135360
     std::map<int, Tile>::iterator it = m_mTiles.begin();
135360
     for (; it != m_mTiles.end(); it++)
135360
-        {
135360
-            it->second.tile_release();
135360
-        }
135360
+    {
135360
+        it->second.release();
135360
+    }
135360
     m_mTiles.clear();
135360
 }
135360
 
135360
-void TileBuffer::tile_buffer_set_invalid(int x, int y)
135360
+void TileBuffer::setInvalid(int x, int y)
135360
 {
135360
     int index = x * m_nWidth + y;
135360
-    g_info("setting invalid : %d %d",x, y);
135360
+    g_info("Setting tile invalid (%d, %d)", x, y);
135360
     if (m_mTiles.find(index) != m_mTiles.end())
135360
-        {
135360
-            m_mTiles[index].valid = 0;
135360
-            m_mTiles[index].tile_release();
135360
-            m_mTiles.erase(index);
135360
-        }
135360
+    {
135360
+        m_mTiles[index].valid = 0;
135360
+        m_mTiles[index].release();
135360
+        m_mTiles.erase(index);
135360
+    }
135360
 }
135360
 
135360
-Tile& TileBuffer::tile_buffer_get_tile(int x, int y)
135360
+Tile& TileBuffer::getTile(int x, int y)
135360
 {
135360
     int index = x * m_nWidth + y;
135360
     if(m_mTiles.find(index) == m_mTiles.end() || !m_mTiles[index].valid)
135360
-        {
135360
+    {
135360
 
135360
-            GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, m_nTileSize, m_nTileSize);
135360
-            if (!pPixBuf){
135360
-                g_info ("error allocating memory to pixbuf");
135360
-            }
135360
-            unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
135360
-            GdkRectangle aTileRectangle;
135360
-            aTileRectangle.x = pixelToTwip(m_nTileSize, m_fZoomFactor) * y;
135360
-            aTileRectangle.y = pixelToTwip(m_nTileSize, m_fZoomFactor) * x;
135360
-
135360
-            g_info ("rendering (%d %d)", x, y);
135360
-            m_pLOKDocument->pClass->paintTile(m_pLOKDocument,
135360
-                                              // Buffer and its size, depends on the position only.
135360
-                                              pBuffer,
135360
-                                              m_nTileSize, m_nTileSize,
135360
-                                              // Position of the tile.
135360
-                                              aTileRectangle.x, aTileRectangle.y,
135360
-                                              // Size of the tile, depends on the zoom factor and the tile position only.
135360
-                                              pixelToTwip(m_nTileSize, m_fZoomFactor), pixelToTwip(m_nTileSize, m_fZoomFactor));
135360
-
135360
-            //create a mapping for it
135360
-            m_mTiles[index].tile_set_pixbuf(pPixBuf);
135360
-            m_mTiles[index].valid = 1;
135360
+        GdkPixbuf* pPixBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, m_nTileSize, m_nTileSize);
135360
+        if (!pPixBuf)
135360
+        {
135360
+            g_info ("Error allocating memory to pixbuf");
135360
+            return m_mTiles[index];
135360
         }
135360
 
135360
+        unsigned char* pBuffer = gdk_pixbuf_get_pixels(pPixBuf);
135360
+        GdkRectangle aTileRectangle;
135360
+        aTileRectangle.x = pixelToTwip(m_nTileSize, m_fZoomFactor) * y;
135360
+        aTileRectangle.y = pixelToTwip(m_nTileSize, m_fZoomFactor) * x;
135360
+
135360
+        g_info ("Rendering (%d, %d)", x, y);
135360
+        m_pLOKDocument->pClass->paintTile(m_pLOKDocument,
135360
+                                          pBuffer,
135360
+                                          m_nTileSize, m_nTileSize,
135360
+                                          aTileRectangle.x, aTileRectangle.y,
135360
+                                          pixelToTwip(m_nTileSize, m_fZoomFactor),
135360
+                                          pixelToTwip(m_nTileSize, m_fZoomFactor));
135360
+
135360
+        //create a mapping for it
135360
+        m_mTiles[index].setPixbuf(pPixBuf);
135360
+        m_mTiles[index].valid = 1;
135360
+    }
135360
+
135360
     return m_mTiles[index];
135360
 }
135360
 
135360
-void Tile::tile_set_pixbuf(GdkPixbuf *buffer)
135360
-{
135360
-    m_pBuffer = buffer;
135360
-}
135360
+
135360
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
135360
diff --git a/libreofficekit/source/gtk/tilebuffer.hxx b/libreofficekit/source/gtk/tilebuffer.hxx
135360
index 15b276f238aa..7e2132f4d512 100644
135360
--- a/libreofficekit/source/gtk/tilebuffer.hxx
135360
+++ b/libreofficekit/source/gtk/tilebuffer.hxx
135360
@@ -1,4 +1,4 @@
135360
-/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
135360
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
135360
 /*
135360
  * This file is part of the LibreOffice project.
135360
  *
135360
@@ -24,32 +24,60 @@ const int DPI = 96;
135360
 // Lets use a square of side 256 pixels for each tile.
135360
 const int nTileSizePixels = 256;
135360
 
135360
+/**
135360
+   Converts the pixel value to zoom independent twip value.
135360
+
135360
+   @param fInput value to convert
135360
+   @param zoom the current zoom level
135360
+
135360
+   @return the pixels value corresponding to given twip value
135360
+*/
135360
 float pixelToTwip(float fInput, float zoom);
135360
 
135360
+/**
135360
+   Converts the zoom independent twip value pixel value.
135360
+
135360
+   @param fInput value to convert
135360
+   @param zoom the current zoom level
135360
+
135360
+   @return the twip value corresponding to given pixel value
135360
+*/
135360
 float twipToPixel(float fInput, float zoom);
135360
 
135360
-/*
135360
-  This class represents a single tile in the tile buffer.
135360
-  TODO: Extend it to support features like double buffering
135360
+/**
135360
+   This class represents a single tile in the tile buffer.
135360
+   It encloses a reference to GdkPixBuf containing the pixel data of the tile.
135360
 */
135360
 class Tile
135360
 {
135360
  public:
135360
- Tile() : valid(0) {}
135360
-    ~Tile() {
135360
-    }
135360
+    Tile() : valid(0) {}
135360
+    ~Tile() { }
135360
 
135360
-    GdkPixbuf* tile_get_buffer();
135360
-    void tile_release();
135360
-    void tile_set_pixbuf(GdkPixbuf*);
135360
+    /**
135360
+       Tells if this tile is valid or not. Initialised to 0 (invalid) during
135360
+       object creation.
135360
+    */
135360
     bool valid;
135360
- private:
135360
+
135360
+    /// Function to get the pointer to enclosing GdkPixbuf
135360
+    GdkPixbuf* getBuffer();
135360
+    /// Destroys the enclosing GdkPixbuf object pointed to by m_pBuffer
135360
+    void release();
135360
+    /// Used to set the pixel buffer of this object
135360
+    void setPixbuf(GdkPixbuf*);
135360
+
135360
+private:
135360
+    /// Pixel buffer data for this tile
135360
     GdkPixbuf *m_pBuffer;
135360
 };
135360
 
135360
-/*
135360
-  TileBuffer is the buffer caching all the recently rendered tiles.
135360
-  The buffer is set to invalid when zoom factor changes.
135360
+/**
135360
+   This class represents the tile buffer which is responsible for managing,
135360
+   reusing and caching all the already rendered tiles. If the given tile is not
135360
+   present in the buffer, call to LOK Document's (m_pLOKDocument) paintTile
135360
+   method is made which fetches the rendered tile from LO core and store it in
135360
+   buffer for future reuse.
135360
 */
135360
 class TileBuffer
135360
 {
135360
@@ -67,19 +95,58 @@ class TileBuffer
135360
 
135360
     ~TileBuffer() {}
135360
 
135360
-    void tile_buffer_set_zoom(float zoomFactor, int rows, int columns);
135360
-    Tile& tile_buffer_get_tile(int x, int y);
135360
-    void tile_buffer_update();
135360
-    void tile_buffer_reset_all_tiles();
135360
-    void tile_buffer_set_invalid(int x, int y);
135360
+    /**
135360
+       Sets the zoom factor (m_fZoomFactor) for this tile buffer. Setting the
135360
+       zoom factor invalidates whole of the tile buffer, destroys all tiles
135360
+       contained within it, and sets new width, height values for tile
135360
+       buffer. The width, height value of tile buffer is the width and height of
135360
+       the table containing all possible tiles (rendered and non-rendered) that
135360
+       this buffer can have.
135360
+
135360
+       @param zoomFactor the new zoom factor value to set
135360
+     */
135360
+    void setZoom(float zoomFactor, int rows, int columns);
135360
+    /**
135360
+       Gets the underlying Tile object for given position. The position (0, 0)
135360
+       points to the left top most tile of the buffer.
135360
+
135360
+       If the tile is not cached by the tile buffer, it makes a paintTile call
135360
+       to LO core asking to render the given tile. It then stores the tile for
135360
+       future reuse.
135360
+
135360
+       @param x the tile along the x-axis of the buffer
135360
+       @param y the tile along the y-axis of the buffer
135360
+
135360
+       @return the tile at the mentioned position (x, y)
135360
+     */
135360
+    Tile& getTile(int x, int y);
135360
+    /// Destroys all the tiles in the tile buffer; also frees the memory allocated
135360
+    /// for all the Tile objects.
135360
+    void resetAllTiles();
135360
+    /**
135360
+       Marks the tile as invalid. The tile (0, 0) is the left topmost tile in
135360
+       the tile buffer.
135360
+
135360
+       @param x the position of tile along x-axis
135360
+       @param y the position of tile along y-axis
135360
+     */
135360
+    void setInvalid(int x, int y);
135360
+
135360
  private:
135360
+    /// Contains the reference to the LOK Document that this tile buffer is for.
135360
     LibreOfficeKitDocument *m_pLOKDocument;
135360
+    /// The side of each squared tile in pixels.
135360
     int m_nTileSize;
135360
+    /// The zoom factor that the tile buffer is currently rendered to.
135360
     float m_fZoomFactor;
135360
+    /// Stores all the tiles cached by this tile buffer.
135360
     std::map<int, Tile> m_mTiles;
135360
-    //TODO: Also set width and height when document size changes
135360
+    /// Width of the current tile buffer (number of columns)
135360
     int m_nWidth;
135360
+    /// Height of the current tile buffer (numbero of rows)
135360
     int m_nHeight;
135360
 };
135360
 
135360
 #endif // INCLUDED_TILEBUFFER_HXX
135360
+
135360
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
135360
-- 
135360
2.12.0
135360