Scroom  0.14
CommonOperations Class Reference

#include <layeroperations.hh>

Inheritance diagram for CommonOperations:
Inheritance graph
Collaboration diagram for CommonOperations:
Collaboration graph

Public Member Functions

void initializeCairo (cairo_t *cr) override
 
void drawState (cairo_t *cr, TileState s, Scroom::Utils::Rectangle< double > viewArea) override
 
Scroom::Utils::Stuff cacheZoom (const ConstTile::Ptr &tile, int zoom, Scroom::Utils::Stuff &cache) override
 
void draw (cairo_t *cr, const ConstTile::Ptr &tile, Scroom::Utils::Rectangle< double > tileArea, Scroom::Utils::Rectangle< double > viewArea, int zoom, Scroom::Utils::Stuff cache) override
 
- Public Member Functions inherited from LayerOperations
virtual int getBpp ()=0
 
virtual Scroom::Utils::Stuff cache (const ConstTile::Ptr &tile)
 
virtual void reduce (Tile::Ptr target, ConstTile::Ptr source, int x, int y)=0
 

Static Public Member Functions

static void drawPixelValue (cairo_t *cr, int x, int y, int size, int value)
 
static void drawPixelValue (cairo_t *cr, int x, int y, int size, int value, Color const &bgColor)
 

Additional Inherited Members

- Public Types inherited from LayerOperations
using Ptr = boost::shared_ptr< LayerOperations >
 

Member Function Documentation

◆ cacheZoom()

Scroom::Utils::Stuff CommonOperations::cacheZoom ( const ConstTile::Ptr tile,
int  zoom,
Scroom::Utils::Stuff cache 
)
overridevirtual

Cache data for use during later draw() calls.

This function is called for a given tile and zoom level, and then the results are passed to the draw() function. Because draw() is called relatively frequently (i.e. when scrolling), it is recommended to do cpu-intensive work in the cache() and cacheZoom() functions, and then re-use the data to make the draw() faster.

The default implementation returns an empty reference, meaning nothing is cached. As a result, the draw() function will receive an empty reference.

Parameters
tilethe Tile for which caching is requested
zoomthe requested zoom level
cachethe output of cache(const ConstTile::Ptr)

Reimplemented from LayerOperations.

Reimplemented in Operations1bppClipped.

128 {
129  // In: Cairo surface at zoom level 0
130  // Out: Cairo surface at requested zoom level
131  Scroom::Utils::Stuff result;
132  if(zoom >= 0)
133  {
134  // Don't zoom in. It is a waste of space
135  result = cache;
136  }
137  else if(!cache)
138  {
139  defect_message("Base caching failed to return anything");
140  }
141  else
142  {
143  const int divider = 1 << -zoom;
144  BitmapSurface::Ptr const source = boost::static_pointer_cast<BitmapSurface>(cache);
145  BitmapSurface::Ptr const target = BitmapSurface::create(tile->width / divider, tile->height / divider, CAIRO_FORMAT_ARGB32);
146  result = target;
147 
148  cairo_surface_t* surface = target->get();
149  cairo_t* cr = cairo_create(surface);
150  initializeCairo(cr);
151  cairo_scale(cr, 1.0 / divider, 1.0 / divider);
152  cairo_set_source_surface(cr, source->get(), 0, 0);
153  cairo_paint(cr);
154 
155  cairo_destroy(cr);
156  }
157 
158  return result;
159 }
Here is the call graph for this function:

◆ draw()

void CommonOperations::draw ( cairo_t *  cr,
const ConstTile::Ptr tile,
Scroom::Utils::Rectangle< double >  tileArea,
Scroom::Utils::Rectangle< double >  viewArea,
int  zoom,
Scroom::Utils::Stuff  cache 
)
overridevirtual

Draw the given tileArea into the given viewArea at the requested zoom level

Parameters
crThe canvas on which to draw
tileThe tile to take data from
tileAreaArea of the tile that needs to be drawn
viewAreaArea of the canvas that the tile needs to be drawn in
zoomThe requested zoom level. One pixel of your presentation should have size 2**zoom when drawn. zoom will be negative for all but the first layer.
cacheDepending on whether the cacheZoom() function finished already, this may either be an empty reference, or a reference to the value returned by cacheZoom()

Implements LayerOperations.

Reimplemented in Operations, Operations8bpp, and Operations1bpp.

167 {
168  // In: Cairo surface at requested zoom level
169  // Out: given surface rendered to the canvas
170  UNUSED(tile);
171 
172  setClip(cr, viewArea);
173 
174  if(!cache)
175  {
176  drawState(cr, TILE_UNLOADED, viewArea);
177  }
178  else
179  {
180  BitmapSurface::Ptr const source = boost::static_pointer_cast<BitmapSurface>(cache);
181 
182  if(zoom > 0)
183  {
184  // Ask Cairo to zoom in for us
185  const int multiplier = 1 << zoom;
186  auto origin = viewArea.getTopLeft() / multiplier - tileArea.getTopLeft();
187 
188  cairo_scale(cr, multiplier, multiplier);
189  cairo_set_source_surface(cr, source->get(), origin.x, origin.y);
190  cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST);
191  cairo_paint(cr);
192  }
193  else
194  {
195  // Cached bitmap is to scale
196  const int divider = 1 << -zoom;
197 
198  auto origin = viewArea.getTopLeft() - tileArea.getTopLeft() / divider;
199 
200  cairo_set_source_surface(cr, source->get(), origin.x, origin.y);
201  cairo_paint(cr);
202  }
203  }
204 }

Referenced by Operations1bpp::draw(), Operations8bpp::draw(), and Operations::draw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawPixelValue() [1/2]

void CommonOperations::drawPixelValue ( cairo_t *  cr,
int  x,
int  y,
int  size,
int  value 
)
static
97 {
98  std::ostringstream s;
99  s << value;
100  const std::string v = s.str();
101  const char* cstr = v.c_str();
102 
103  cairo_move_to(cr, x, y);
104  cairo_line_to(cr, x + size, y);
105  cairo_line_to(cr, x + size, y + size);
106  cairo_line_to(cr, x, y + size);
107  cairo_line_to(cr, x, y);
108  cairo_clip(cr);
109  // cairo_stroke(cr);
110 
111  cairo_text_extents_t extents;
112  cairo_text_extents(cr, cstr, &extents);
113 
114  const double xx = x + size / 2 - (extents.width / 2 + extents.x_bearing);
115  const double yy = y + size / 2 - (extents.height / 2 + extents.y_bearing);
116 
117  cairo_move_to(cr, xx, yy);
118  cairo_show_text(cr, cstr);
119 }

Referenced by Operations1bpp::draw(), Operations8bpp::draw(), and Operations::draw().

Here is the caller graph for this function:

◆ drawPixelValue() [2/2]

void CommonOperations::drawPixelValue ( cairo_t *  cr,
int  x,
int  y,
int  size,
int  value,
Color const &  bgColor 
)
static
122 {
123  bgColor.getContrastingBlackOrWhite().setColor(cr);
124  drawPixelValue(cr, x, y, size, value);
125 }
Here is the call graph for this function:

◆ drawState()

void CommonOperations::drawState ( cairo_t *  cr,
TileState  s,
Scroom::Utils::Rectangle< double >  viewArea 
)
overridevirtual

Draw the given state into the given viewArea

The associated tile is likely not loaded or not initialized or in whatever other TileState that doesn't allow its contents to be drawn.

Something needs to be drawn in the given viewArea anyway. Implementors might want to just draw an empty rectangle, maybe color-coded to reflect the state of the tile.

Implements LayerOperations.

73 {
74  Color c;
75 
76  switch(s)
77  {
78  case TILE_UNINITIALIZED:
79  c = Color(1, 1, 0.5); // Yellow
80  break;
81  case TILE_UNLOADED:
82  c = Color(0.5, 1, 0.5); // Green
83  break;
84  case TILE_LOADED:
85  c = Color(1, 0.5, 0.5); // Red
86  break;
87  case TILE_OUT_OF_BOUNDS:
88  default:
90  break;
91  }
92 
93  drawRectangle(cr, c, viewArea);
94 }
Here is the call graph for this function:

◆ initializeCairo()

void CommonOperations::initializeCairo ( cairo_t *  cr)
overridevirtual

Initialize the canvas for drawing the bitmap

When TiledBitmapInterface::redraw() is called, (a portion of) the Layer needs to be redrawn. TiledBitmap will compute which tiles are involved in the redraw, and call draw() or drawState() for each of them, as appropriate. However, before doing so, it will first call initializeCairo(). You can take this opportunity to set various properties that are needed in all subsequent calls to draw() and drawState(), such as anti-aliasing and line caps.

Implements LayerOperations.

67 {
68  cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
69  cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
70 }

The documentation for this class was generated from the following files:
Colors::OUT_OF_BOUNDS
const Color OUT_OF_BOUNDS
UNUSED
#define UNUSED(x)
Definition: unused.hh:10
Scroom::Utils::Rectangle::getTopLeft
xy_type getTopLeft() const
Definition: rectangle.hh:133
TILE_UNLOADED
@ TILE_UNLOADED
Definition: tiledbitmapinterface.hh:35
CommonOperations::initializeCairo
void initializeCairo(cairo_t *cr) override
Definition: layeroperations.cc:66
Scroom::Bitmap::BitmapSurface::Ptr
boost::shared_ptr< BitmapSurface > Ptr
Definition: bitmap-helpers.hh:25
TILE_LOADED
@ TILE_LOADED
Definition: tiledbitmapinterface.hh:36
TILE_OUT_OF_BOUNDS
@ TILE_OUT_OF_BOUNDS
Definition: tiledbitmapinterface.hh:37
drawRectangle
void drawRectangle(cairo_t *cr, Color const &c, Scroom::Utils::Rectangle< double > const &viewArea)
Definition: cairo-helpers.cc:62
LayerOperations::cache
virtual Scroom::Utils::Stuff cache(const ConstTile::Ptr &tile)
Definition: tiledbitmapinterface.hh:126
Scroom::Utils::Stuff
boost::shared_ptr< void > Stuff
Definition: stuff.hh:18
CommonOperations::drawState
void drawState(cairo_t *cr, TileState s, Scroom::Utils::Rectangle< double > viewArea) override
Definition: layeroperations.cc:72
CommonOperations::drawPixelValue
static void drawPixelValue(cairo_t *cr, int x, int y, int size, int value)
Definition: layeroperations.cc:96
TILE_UNINITIALIZED
@ TILE_UNINITIALIZED
Definition: tiledbitmapinterface.hh:34
defect_message
#define defect_message(m)
Definition: assertions.hh:43
setClip
void setClip(cairo_t *cr, double x, double y, double width, double height)
Definition: cairo-helpers.cc:47
Color
Definition: color.hh:34
create
void create(NewPresentationInterface *interface)
Definition: loader.cc:175