VTK  9.7.20261203
vtkAnariOverlayCanvas.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
15
16#ifndef vtkAnariOverlayCanvas_h
17#define vtkAnariOverlayCanvas_h
18
19#include "vtkAnariOverlays.h" // For ImageView, RGBAColor, GlyphRun, DrawContext
20#include "vtkMath.h" // For vtkMath::IsNan
21
22#include <algorithm> // For std::clamp, std::max, std::min
23#include <cmath> // For std::ceil, std::floor
24#include <cstddef> // For std::size_t
25#include <cstdint> // For std::uint8_t
26#include <string_view> // For std::string_view
27
28namespace vtkAnariOverlays
29{
30VTK_ABI_NAMESPACE_BEGIN
31
37{
38 int X0 = 0;
39 int Y0 = 0;
40 int X1 = 0;
41 int Y1 = 0;
42
48 {
49 return { std::max(this->X0, o.X0), std::max(this->Y0, o.Y0), std::min(this->X1, o.X1),
50 std::min(this->Y1, o.Y1) };
51 }
52};
53
54// Converts an integral-valued pixel coordinate to int, clamped far inside
55// int range so huge coordinates can't overflow. NaN maps to 0.
56inline int ToPixel(float v)
57{
58 constexpr double limit = double(1 << 24);
59 return vtkMath::IsNan(v) ? 0 : static_cast<int>(std::clamp(double(v), -limit, limit));
60}
61
62// Smallest pixel rectangle covering the float rectangle [x0, x1] x [y0, y1];
63// any NaN gives an empty rectangle.
64inline PixelRect PixelBounds(float x0, float y0, float x1, float y1)
65{
67 {
68 return {};
69 }
70 return { ToPixel(std::floor(x0)), ToPixel(std::floor(y0)), ToPixel(std::ceil(x1)),
71 ToPixel(std::ceil(y1)) };
72}
73
90class Canvas
91{
92public:
93 explicit Canvas(const ImageView& image);
94
95 bool IsValid() const
96 {
97 return this->Image.Data && this->Image.Width > 0 && this->Image.Height > 0;
98 }
99 int GetFullWidth() const { return this->FullWidth; }
100 int GetFullHeight() const { return this->FullHeight; }
101
102 // Restrict drawing to `r` (logical pixels) intersected with the image.
103 void SetClip(const PixelRect& r) { this->Clip = this->Bounds.Intersect(r); }
104
105 // Source-over blend of `c` (straight alpha) scaled by `coverage` into the
106 // logical pixel (x, y). Pixels outside the clip are ignored.
107 void Blend(int x, int y, const RGBAColor& c, float coverage);
108
109 // Draws a single-color mask: `coverage` holds one byte per pixel (w * h,
110 // row 0 at the top), the alpha of `c` drawn there, and is not an RGBA
111 // image. Each byte scales c.A before blending. Its top-left pixel lands on
112 // logical pixel (left, top).
114 const std::uint8_t* coverage, int w, int h, int left, int top, const RGBAColor& c);
115
116 // Draws `run`'s coverage bitmap with its bottom-left corner at (x, y),
117 // rounded to whole logical pixels, shadow first when the style has one.
118 void DrawText(const GlyphRun& run, float x, float y, const TextStyle& style);
119
120private:
121 std::uint8_t* Pixel(int x, int y) const
122 {
123 const int ix = x - this->Image.Tile.OffsetX;
124 const int iy = y - this->Image.Tile.OffsetY;
125 return this->Image.Data + (std::size_t(iy) * std::size_t(this->Image.Width) + ix) * 4;
126 }
127
128 ImageView Image;
129 int FullWidth = 0;
130 int FullHeight = 0;
131 PixelRect Bounds;
132 PixelRect Clip;
133};
134
136{
137public:
138 explicit TextRenderer(const DrawContext& context)
139 : Provider(context.Glyphs)
140 , DPIScale(context.DPIScale > 0.f ? context.DPIScale : 1.f)
141 {
142 }
143
144 // Lays out `text` at the style's PixelSize times the DPI scale.
145 GlyphRun Layout(std::string_view text, const TextStyle& style) const;
146 // Lays out `text` at `pixelSize` image pixels, ignoring the style's size.
147 GlyphRun LayoutAtSize(std::string_view text, const TextStyle& style, float pixelSize) const;
148
149 float GetDPIScale() const { return this->DPIScale; }
150
151private:
152 GlyphProvider Provider;
153 float DPIScale;
154};
155
156VTK_ABI_NAMESPACE_END
157} // namespace vtkAnariOverlays
158
159#endif
160// VTK-HeaderTest-Exclude: vtkAnariOverlayCanvas.h
void SetClip(const PixelRect &r)
void Blend(int x, int y, const RGBAColor &c, float coverage)
void BlitCoverage(const std::uint8_t *coverage, int w, int h, int left, int top, const RGBAColor &c)
Canvas(const ImageView &image)
void DrawText(const GlyphRun &run, float x, float y, const TextStyle &style)
TextRenderer(const DrawContext &context)
GlyphRun Layout(std::string_view text, const TextStyle &style) const
GlyphRun LayoutAtSize(std::string_view text, const TextStyle &style, float pixelSize) const
static vtkTypeBool IsNan(double x)
Test if a number is equal to the special floating point value Not-A-Number (Nan).
Computes the portion of a dataset which is inside a selection.
PixelRect PixelBounds(float x0, float y0, float x1, float y1)
std::function< GlyphRun(std::string_view text, const TextStyle &style, float pixelSize)> GlyphProvider
Rasterizes text into a coverage bitmap.
An 8-bit coverage bitmap for a run of text, trimmed to its drawn pixels, row 0 at the TOP.
A tightly packed RGBA8 image.
An axis-aligned pixel rectangle covering [X0, X1) x [Y0, Y1).
PixelRect Intersect(const PixelRect &o) const
Returns the overlap of this rectangle and o.
CPU overlay drawing types for the pure-ANARI render window.