LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / libreofficekit / source / gtk / tilebuffer.cxx
blob3c73c9dddf83e639a02393ab9fae1648c7e50673
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "tilebuffer.hxx"
12 #include <o3tl/unit_conversion.hxx>
15 /* ------------------
16 Utility functions
17 ------------------
19 // We know that VirtualDevices use a DPI of 96.
20 float pixelToTwip(float fInput, float zoom)
22 return o3tl::toTwips(fInput / zoom, o3tl::Length::px);
25 float twipToPixel(float fInput, float zoom)
27 return o3tl::convert(fInput * zoom, o3tl::Length::twip, o3tl::Length::px);
30 /* ----------------------------
31 Tile class member functions
32 ----------------------------
34 cairo_surface_t* Tile::getBuffer()
36 return m_pBuffer;
39 void Tile::setSurface(cairo_surface_t *buffer)
41 if (m_pBuffer == buffer)
42 return;
43 if (m_pBuffer)
44 cairo_surface_destroy(m_pBuffer);
45 if (buffer != nullptr)
46 cairo_surface_reference(buffer);
47 m_pBuffer = buffer;
50 /* ----------------------------------
51 TileBuffer class member functions
52 ----------------------------------
54 void TileBuffer::resetAllTiles()
56 for (auto & tile : m_mTiles)
58 tile.second.valid = false;
62 void TileBuffer::setInvalid(int x, int y, float fZoom, GTask* task,
63 GThreadPool* lokThreadPool)
65 int index = x * m_nWidth + y;
66 GError* error = nullptr;
67 if (m_mTiles.find(index) == m_mTiles.end())
68 return;
70 m_mTiles[index].valid = false;
72 LOEvent* pLOEvent = new LOEvent(LOK_PAINT_TILE);
73 pLOEvent->m_nPaintTileX = x;
74 pLOEvent->m_nPaintTileY = y;
75 pLOEvent->m_fPaintTileZoom = fZoom;
76 g_task_set_task_data(task, pLOEvent, LOEvent::destroy);
77 g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
78 if (error != nullptr)
80 g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
81 g_clear_error(&error);
85 Tile& TileBuffer::getTile(int x, int y, GTask* task,
86 GThreadPool* lokThreadPool)
88 int index = x * m_nWidth + y;
89 GError* error = nullptr;
91 if (m_mTiles.find(index) != m_mTiles.end() && !m_mTiles[index].valid)
93 g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
94 if (error != nullptr)
96 g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
97 g_clear_error(&error);
99 return m_mTiles[index];
101 else if(m_mTiles.find(index) == m_mTiles.end())
103 g_thread_pool_push(lokThreadPool, g_object_ref(task), &error);
104 if (error != nullptr)
106 g_warning("Unable to call LOK_PAINT_TILE: %s", error->message);
107 g_clear_error(&error);
109 return m_DummyTile;
112 return m_mTiles[index];
115 void TileBuffer::setTile(int x, int y, cairo_surface_t *surface)
117 int index = x * m_nWidth + y;
119 m_mTiles[index].setSurface(surface);
120 m_mTiles[index].valid = true;
123 bool TileBuffer::hasValidTile(int x, int y)
125 int index = x * m_nWidth + y;
126 auto it = m_mTiles.find(index);
127 return (it != m_mTiles.end()) && it->second.valid;
130 void LOEvent::destroy(void* pMemory)
132 LOEvent* pLOEvent = static_cast<LOEvent*>(pMemory);
133 delete pLOEvent;
136 GQuark
137 LOKTileBufferErrorQuark()
139 return g_quark_from_static_string("lok-tilebuffer-error");
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */