Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
Inkscape::Util Namespace Reference

Miscellaneous supporting code. More...

Namespaces

namespace  detail
 
namespace  GlibValue
 

Classes

class  AcceleratorKey
 Gtk::AccelKey but with equality and less-than operators. More...
 
class  ActionAccel
 The ActionAccel class stores the keyboard shortcuts for a given action and automatically keeps track of changes in the keybindings. More...
 
class  cached_map
 A cached_map<Tk, Tv> is designed for use by a factory that takes as input keys of type Tk and produces objects of type std::unique_ptr<Tv> in response. More...
 
class  const_char_ptr
 Non-owning reference to 'const char*' Main-purpose: avoid overloads of type f(char*, str&), f(str&, char*), f(char*, char*), ... More...
 
struct  Deleter
 Turn a function into a function object that can be used as a deleter for a smart pointer. More...
 
struct  DependencyRegisterer
 
struct  DependencyRegisterer< Depends< T, Ts... > >
 
struct  Depends
 
class  EnableSingleton
 
struct  EnumData
 Simplified management of enumerations of svg items with UI labels. More...
 
class  EnumDataConverter
 Simplified management of enumerations of svg items with UI labels. More...
 
class  EvaluatorException
 Special exception class for the expression evaluator. More...
 
class  EvaluatorQuantity
 EvaluatorQuantity: More...
 
class  EvaluatorToken
 EvaluatorToken. More...
 
class  ExpressionEvaluator
 ExpressionEvaluator. More...
 
class  FixedPoint
 
class  ForwardPointerIterator
 
class  ForwardPointerIterator< BaseType const, Strategy >
 
class  FuncLog
 A FuncLog is effectively a std::vector<std::function<void()>>, with the ability to hold move-only function types and enforced run-once semantics. More...
 
class  GObjectPtr
 A smart pointer that shares ownership of a GObject. More...
 
class  HybridPointer
 A helper class holding an owning or non-owning pointer depending on the memory management requirements. More...
 
class  Pool
 A Pool is a block allocator with the following characteristics: More...
 
class  ptr_shared
 
class  Quantity
 
class  StaticHolder
 
class  StaticHolderBase
 
class  StaticsBin
 The following system provides a way of dealing with statics/singletons with unusual lifetime requirements, specifically the requirement that they be destroyed before the end of main(). More...
 
struct  TreeifyResult
 
class  Unit
 
struct  UnitMetric
 
class  UnitTable
 

Typedefs

typedef int TokenType
 

Enumerations

enum  { TOKEN_NUM = 30000 , TOKEN_IDENTIFIER = 30001 , TOKEN_ANY = 40000 , TOKEN_END = 50000 }
 TokenType. More...
 
enum  UnitType {
  UNIT_TYPE_DIMENSIONLESS , UNIT_TYPE_LINEAR , UNIT_TYPE_LINEAR_SCALED , UNIT_TYPE_RADIAL ,
  UNIT_TYPE_TIME , UNIT_TYPE_FONT_HEIGHT , UNIT_TYPE_QTY , UNIT_TYPE_NONE = -1
}
 

Functions

double round_to_nearest_multiple_plus (double x, double c1, double c0)
 
double round_to_lower_multiple_plus (double x, double c1, double c0=0.0)
 
double round_to_upper_multiple_plus (double x, double const c1, double const c0=0)
 
template<typename T >
int constexpr floorlog2 (T x)
 Returns floor(log_2(x)), assuming x >= 1.
 
template<typename T , typename std::enable_if< std::is_integral< T >::value, bool >::type = true>
T constexpr safemod (T a, T b)
 Returns a mod b, always in the range 0..b-1, assuming b >= 1.
 
template<typename T , typename std::enable_if< std::is_integral< T >::value, bool >::type = true>
T constexpr round_down (T a, T b)
 Returns a rounded down to the nearest multiple of b, assuming b >= 1.
 
template<typename T , typename std::enable_if< std::is_integral< T >::value, bool >::type = true>
T constexpr round_up (T a, T b)
 Returns a rounded up to the nearest multiple of b, assuming b >= 1.
 
template<typename T >
safeclamp (T val, T lo, T hi)
 Just like std::clamp, except it doesn't deliberately crash if lo > hi due to rounding errors, so is safe to use with floating-point types.
 
template<auto f, typename T >
auto delete_with (T *p)
 Wrap a raw pointer in a std::unique_ptr with a custom function as the deleter.
 
Geom::Rect rounded_rectangle (const Cairo::RefPtr< Cairo::Context > &ctx, const Geom::Rect &rect, double radius)
 
void circle (const Cairo::RefPtr< Cairo::Context > &ctx, const Geom::Point &center, double radius)
 
void draw_border (const Cairo::RefPtr< Cairo::Context > &ctx, Geom::Rect rect, double radius, const Gdk::RGBA &color, int device_scale, bool circular)
 
void draw_standard_border (const Cairo::RefPtr< Cairo::Context > &ctx, Geom::Rect rect, bool dark_theme, double radius, int device_scale, bool circular)
 
std::optional< Gdk::RGBA > lookup_background_color (Glib::RefPtr< Gtk::StyleContext > &style)
 
std::optional< Gdk::RGBA > lookup_foreground_color (Glib::RefPtr< Gtk::StyleContext > &style)
 
const Glib::ustring empty_string ("")
 
template<typename T >
requires std::is_enum_v<T>
constexpr bool any_flag (T test)
 
template<typename T >
requires std::is_enum_v<T>
constexpr bool has_flag (T test, T flag)
 
template<typename T , unsigned int precision>
FixedPoint< T, precision > operator* (char a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
FixedPoint< T, precision > operator* (unsigned char a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
FixedPoint< T, precision > operator* (short a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
FixedPoint< T, precision > operator* (unsigned short a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
FixedPoint< T, precision > operator* (int a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
FixedPoint< T, precision > operator* (unsigned int a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
float operator* (float a, FixedPoint< T, precision > b)
 
template<typename T , unsigned int precision>
double operator* (double a, FixedPoint< T, precision > b)
 
ptr_shared vformat (char const *format, va_list args)
 
ptr_shared format (char const *format,...) G_GNUC_PRINTF(1
 
Glib::ustring format_size (std::size_t value)
 
Glib::ustring format_file_size (std::size_t value)
 
double read_number (gchar const *value, bool warning=true)
 
bool read_bool (gchar const *value, bool default_value)
 
std::vector< gdouble > read_vector (const gchar *value)
 
std::string format_number (double val, unsigned int precision=3)
 
bool equal (std::optional< std::string > const &a, char const *b)
 
auto to_opt (char const *s)
 
auto to_cstr (std::optional< std::string > const &s)
 
bool assign (std::optional< std::string > &a, char const *b)
 
static std::byte * round_up (std::byte *x, std::size_t m)
 
ptr_shared share_string (char const *string)
 
ptr_shared share_string (char const *string, std::size_t length)
 
ptr_shared share_unsafe (char const *string)
 
ptr_shared share_static_string (char const *string)
 
template<typename F , auto = [] {}>
SPDocumentcache_static_doc (F &&f)
 Wrapper for a static SPDocument to ensure it is destroyed early enough.
 
bool is_current_theme_dark (Gtk::Widget &widget)
 
std::tuple< std::uint32_t, std::uint32_t > get_checkerboard_colors (Gtk::Widget &widget)
 
TreeifyResult treeify (int N, std::function< bool(int, int)> const &contains)
 Given a collection of nodes 0 ... N - 1 and a containment function, attempt to organise the nodes into a tree (or forest) such that contains(i, j) is true precisely when i is an ancestor of j.
 
void trim (Glib::ustring &input, Glib::ustring const &also_remove="")
 Modifies a string in place, removing leading and trailing whitespace characters.
 
bool are_near (Quantity const &a, Quantity const &b, double eps=Geom::EPSILON)
 

Variables

template<auto Fp>
constexpr auto make_c_callback = detail::CallbackConverter<Fp>::result
 Given a member function, make_c_callback produces a pure function with an extra void* argument at the end, into which an object pointer can be passed.
 
template<auto Fp>
auto make_g_callback = reinterpret_cast<void(*)()>(make_c_callback<Fp>)
 A worse version of make_c_callback that also casts the result to a GCallback, losing even more type-safety.
 
constexpr bool NO_WARNING = false
 
const char DEG [] = "°"
 

Detailed Description

Miscellaneous supporting code.

This namespace contains miscellaneous low-level code: an implementation of garbage-collected lists, tuples, generic pointer iterators and length unit handling.

Typedef Documentation

◆ TokenType

Definition at line 112 of file expression-evaluator.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

TokenType.

Enumerator
TOKEN_NUM 
TOKEN_IDENTIFIER 
TOKEN_ANY 
TOKEN_END 

Definition at line 106 of file expression-evaluator.h.

◆ UnitType

Enumerator
UNIT_TYPE_DIMENSIONLESS 
UNIT_TYPE_LINEAR 
UNIT_TYPE_LINEAR_SCALED 
UNIT_TYPE_RADIAL 
UNIT_TYPE_TIME 
UNIT_TYPE_FONT_HEIGHT 
UNIT_TYPE_QTY 
UNIT_TYPE_NONE 

Definition at line 30 of file units.h.

Function Documentation

◆ any_flag()

template<typename T >
requires std::is_enum_v<T>
constexpr bool Inkscape::Util::any_flag ( test)
constexpr

Definition at line 121 of file enums.h.

References test().

Referenced by has_flag().

◆ are_near()

bool Inkscape::Util::are_near ( Quantity const &  a,
Quantity const &  b,
double  eps = Geom::EPSILON 
)
inline

◆ assign()

bool Inkscape::Util::assign ( std::optional< std::string > &  a,
char const *  b 
)
inline

◆ cache_static_doc()

template<typename F , auto = [] {}>
SPDocument * Inkscape::Util::cache_static_doc ( F &&  f)

Wrapper for a static SPDocument to ensure it is destroyed early enough.

SPDocuments cannot outlive FontFactory which in turn cannot outlive the end of main(). Because of these unusual lifetime requirements, managing a static SPDocument requires some extra work, which can be done by replacing this

static std::unique_ptr<SPDocument> doc = create_doc();

with this

SPDocument *doc = cache_static_doc([] { return create_doc(); });

Definition at line 27 of file static-doc.h.

Referenced by Inkscape::UI::Widget::MarkerComboBox::init_combo(), sp_gradient_load_from_svg(), and sp_marker_load_from_svg().

◆ circle()

void Inkscape::Util::circle ( const Cairo::RefPtr< Cairo::Context > &  ctx,
const Geom::Point center,
double  radius 
)

Definition at line 29 of file drawing-utils.cpp.

References Geom::Point::x(), and Geom::Point::y().

Referenced by draw_border().

◆ delete_with()

template<auto f, typename T >
auto Inkscape::Util::delete_with ( T *  p)

Wrap a raw pointer in a std::unique_ptr with a custom function as the deleter.

Example:

auto x = delete_with<g_free>(g_strdup(...));

Definition at line 26 of file delete-with.h.

References delete_with().

Referenced by delete_with().

◆ draw_border()

void Inkscape::Util::draw_border ( const Cairo::RefPtr< Cairo::Context > &  ctx,
Geom::Rect  rect,
double  radius,
const Gdk::RGBA &  color,
int  device_scale,
bool  circular 
)

◆ draw_standard_border()

void Inkscape::Util::draw_standard_border ( const Cairo::RefPtr< Cairo::Context > &  ctx,
Geom::Rect  rect,
bool  dark_theme,
double  radius,
int  device_scale,
bool  circular 
)

◆ empty_string()

const Glib::ustring Inkscape::Util::empty_string ( ""  )

◆ equal()

bool Inkscape::Util::equal ( std::optional< std::string > const &  a,
char const *  b 
)
inline

Definition at line 16 of file optstr.h.

Referenced by assign().

◆ floorlog2()

template<typename T >
int constexpr Inkscape::Util::floorlog2 ( x)
constexpr

Returns floor(log_2(x)), assuming x >= 1.

Definition at line 62 of file mathfns.h.

◆ format()

ptr_shared ptr_shared Inkscape::Util::format ( char const *  format,
  ... 
)
inline

Definition at line 33 of file format.h.

References format(), result, va_end(), va_start(), and vformat().

Referenced by format(), and vformat().

◆ format_file_size()

Glib::ustring Inkscape::Util::format_file_size ( std::size_t  value)

Definition at line 57 of file format_size.cpp.

References index, and Inkscape::size.

Referenced by Inkscape::UI::Widget::ImageProperties::update().

◆ format_number()

std::string Inkscape::Util::format_number ( double  val,
unsigned int  precision = 3 
)
inline

◆ format_size()

Glib::ustring Inkscape::Util::format_size ( std::size_t  value)

Definition at line 10 of file format_size.cpp.

References format_size().

Referenced by format_size(), and image_crop().

◆ get_checkerboard_colors()

std::tuple< std::uint32_t, std::uint32_t > Inkscape::Util::get_checkerboard_colors ( Gtk::Widget &  widget)

◆ has_flag()

template<typename T >
requires std::is_enum_v<T>
constexpr bool Inkscape::Util::has_flag ( test,
flag 
)
constexpr

◆ is_current_theme_dark()

bool Inkscape::Util::is_current_theme_dark ( Gtk::Widget &  widget)

◆ lookup_background_color()

std::optional< Gdk::RGBA > Inkscape::Util::lookup_background_color ( Glib::RefPtr< Gtk::StyleContext > &  style)

Definition at line 75 of file drawing-utils.cpp.

Referenced by Inkscape::UI::Widget::ColorSlider::draw_func().

◆ lookup_foreground_color()

std::optional< Gdk::RGBA > Inkscape::Util::lookup_foreground_color ( Glib::RefPtr< Gtk::StyleContext > &  style)

Definition at line 83 of file drawing-utils.cpp.

Referenced by Inkscape::UI::Widget::ColorSlider::draw_func().

◆ operator*() [1/8]

template<typename T , unsigned int precision>
FixedPoint< T, precision > Inkscape::Util::operator* ( char  a,
FixedPoint< T, precision >  b 
)

Definition at line 87 of file fixed_point.h.

◆ operator*() [2/8]

template<typename T , unsigned int precision>
double Inkscape::Util::operator* ( double  a,
FixedPoint< T, precision >  b 
)

Definition at line 95 of file fixed_point.h.

◆ operator*() [3/8]

template<typename T , unsigned int precision>
float Inkscape::Util::operator* ( float  a,
FixedPoint< T, precision >  b 
)

Definition at line 94 of file fixed_point.h.

◆ operator*() [4/8]

template<typename T , unsigned int precision>
FixedPoint< T, precision > Inkscape::Util::operator* ( int  a,
FixedPoint< T, precision >  b 
)

Definition at line 91 of file fixed_point.h.

◆ operator*() [5/8]

template<typename T , unsigned int precision>
FixedPoint< T, precision > Inkscape::Util::operator* ( short  a,
FixedPoint< T, precision >  b 
)

Definition at line 89 of file fixed_point.h.

◆ operator*() [6/8]

template<typename T , unsigned int precision>
FixedPoint< T, precision > Inkscape::Util::operator* ( unsigned char  a,
FixedPoint< T, precision >  b 
)

Definition at line 88 of file fixed_point.h.

◆ operator*() [7/8]

template<typename T , unsigned int precision>
FixedPoint< T, precision > Inkscape::Util::operator* ( unsigned int  a,
FixedPoint< T, precision >  b 
)

Definition at line 92 of file fixed_point.h.

◆ operator*() [8/8]

template<typename T , unsigned int precision>
FixedPoint< T, precision > Inkscape::Util::operator* ( unsigned short  a,
FixedPoint< T, precision >  b 
)

Definition at line 90 of file fixed_point.h.

◆ read_bool()

◆ read_number()

double Inkscape::Util::read_number ( gchar const *  value,
bool  warning = true 
)
inline

◆ read_vector()

std::vector< gdouble > Inkscape::Util::read_vector ( const gchar *  value)
inline

Definition at line 84 of file converters.h.

References end.

Referenced by SPFeColorMatrix::set(), SPFeFuncNode::set(), and SPFeConvolveMatrix::set().

◆ round_down()

template<typename T , typename std::enable_if< std::is_integral< T >::value, bool >::type = true>
T constexpr Inkscape::Util::round_down ( a,
b 
)
constexpr

Returns a rounded down to the nearest multiple of b, assuming b >= 1.

Definition at line 82 of file mathfns.h.

References safemod().

Referenced by Inkscape::DrawingPattern::renderPattern(), and round_up().

◆ round_to_lower_multiple_plus()

double Inkscape::Util::round_to_lower_multiple_plus ( double  x,
double  c1,
double  c0 = 0.0 
)
inline
Returns
x rounded to the lower multiple of c1 plus c0.
Note
If c1 == 0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero mean "ignore the grid in this dimension".

Definition at line 41 of file mathfns.h.

References round_to_lower_multiple_plus().

Referenced by Inkscape::GridSnapper::get_snap_lines(), Inkscape::GridSnapper::getSnapLinesAxonom(), and round_to_lower_multiple_plus().

◆ round_to_nearest_multiple_plus()

double Inkscape::Util::round_to_nearest_multiple_plus ( double  x,
double  c1,
double  c0 
)
inline
Returns
x rounded to the nearest multiple of c1 plus c0.
Note
If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero mean "ignore the grid in this dimension".

Definition at line 29 of file mathfns.h.

◆ round_to_upper_multiple_plus()

double Inkscape::Util::round_to_upper_multiple_plus ( double  x,
double const  c1,
double const  c0 = 0 
)
inline
Returns
x rounded to the upper multiple of c1 plus c0.
Note
If c1 == 0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero mean "ignore the grid in this dimension".

Definition at line 53 of file mathfns.h.

References round_to_upper_multiple_plus().

Referenced by Inkscape::GridSnapper::get_snap_lines(), Inkscape::GridSnapper::getSnapLinesAxonom(), and round_to_upper_multiple_plus().

◆ round_up() [1/2]

static std::byte * Inkscape::Util::round_up ( std::byte *  x,
std::size_t  m 
)
static

Definition at line 11 of file pool.cpp.

◆ round_up() [2/2]

template<typename T , typename std::enable_if< std::is_integral< T >::value, bool >::type = true>
T constexpr Inkscape::Util::round_up ( a,
b 
)
constexpr

Returns a rounded up to the nearest multiple of b, assuming b >= 1.

Definition at line 89 of file mathfns.h.

References round_down().

Referenced by Inkscape::Util::Pool::allocate(), and Inkscape::DrawingPattern::renderPattern().

◆ rounded_rectangle()

Geom::Rect Inkscape::Util::rounded_rectangle ( const Cairo::RefPtr< Cairo::Context > &  ctx,
const Geom::Rect rect,
double  radius 
)

◆ safeclamp()

template<typename T >
T Inkscape::Util::safeclamp ( val,
lo,
hi 
)

Just like std::clamp, except it doesn't deliberately crash if lo > hi due to rounding errors, so is safe to use with floating-point types.

(Note: compiles to branchless.)

Definition at line 99 of file mathfns.h.

Referenced by Inkscape::UI::Widget::Stores::snapshot_combine().

◆ safemod()

template<typename T , typename std::enable_if< std::is_integral< T >::value, bool >::type = true>
T constexpr Inkscape::Util::safemod ( a,
b 
)
constexpr

Returns a mod b, always in the range 0..b-1, assuming b >= 1.

Definition at line 74 of file mathfns.h.

Referenced by round_down().

◆ share_static_string()

ptr_shared Inkscape::Util::share_static_string ( char const *  string)
inline

Definition at line 95 of file share.h.

References share_unsafe().

◆ share_string() [1/2]

◆ share_string() [2/2]

ptr_shared Inkscape::Util::share_string ( char const *  string,
std::size_t  length 
)

Definition at line 25 of file share.cpp.

References Inkscape::GC::ATOMIC, share_string(), and share_unsafe().

◆ share_unsafe()

ptr_shared Inkscape::Util::share_unsafe ( char const *  string)
inline

Definition at line 90 of file share.h.

Referenced by share_static_string(), share_string(), and share_string().

◆ to_cstr()

◆ to_opt()

auto Inkscape::Util::to_opt ( char const *  s)
inline

◆ treeify()

TreeifyResult Inkscape::Util::treeify ( int  N,
std::function< bool(int, int)> const &  contains 
)

Given a collection of nodes 0 ... N - 1 and a containment function, attempt to organise the nodes into a tree (or forest) such that contains(i, j) is true precisely when i is an ancestor of j.

Definition at line 9 of file treeify.cpp.

References data, N, Inkscape::Util::TreeifyResult::preorder, and result.

Referenced by Inkscape::split_non_intersecting_paths().

◆ trim()

void Inkscape::Util::trim ( Glib::ustring &  input,
Glib::ustring const &  also_remove = "" 
)
inline

Modifies a string in place, removing leading and trailing whitespace characters.

Optionally, it can remove other characters or ranges in addition to whitespace.

Parameters
input- a reference to a Glib::ustring which will be modified in place.
also_remove- optional range of characters to remove in addition to whitespace. NOTE: these characters are inserted into a regex range (square brackets) and therefore may need to be regex-escaped. It is the responsibility of the user to pass a string that will work correctly in a regex range.

Definition at line 34 of file trim.h.

Referenced by Inkscape::UI::Dialog::SelectorsDialog::_addSelector(), Inkscape::UI::Dialog::SelectorsDialog::_getSelectorClasses(), Inkscape::UI::Dialog::SelectorsDialog::_removeClass(), Inkscape::UI::Dialog::SelectorsDialog::_removeFromSelector(), Inkscape::UI::Dialog::add_metadata(), Inkscape::UI::Dialog::InkscapePreferences::get_highlight_colors(), Inkscape::UI::Syntax::minify_svgd(), Inkscape::UI::Dialog::StyleDialog::parseStyle(), Inkscape::UI::Syntax::prettify_svgd(), Inkscape::UI::Dialog::StyleDialog::readStyleElement(), and Inkscape::UI::Dialog::XmlTree::stopNodeEditing().

◆ vformat()

ptr_shared Inkscape::Util::vformat ( char const *  format,
va_list  args 
)
inline

Definition at line 24 of file format.h.

References format(), result, and share_string().

Referenced by format().

Variable Documentation

◆ DEG

const char Inkscape::Util::DEG[] = "°"

Definition at line 41 of file units.h.

◆ make_c_callback

template<auto Fp>
constexpr auto Inkscape::Util::make_c_callback = detail::CallbackConverter<Fp>::result
constexpr

Given a member function, make_c_callback produces a pure function with an extra void* argument at the end, into which an object pointer can be passed.

Calling the pure function then invokes the original function on this object. In other words

make_c_callback<&X::f>(..., &x)

is equivalent to

x->f(...);

This is useful for passing member functions as callbacks to C code.

Note: Actually they're not completely equivalent in that some extra forwarding might go on. Specifically, if your member function takes a T (by value) then the resulting callback will also take a T by value (because make_c_callback always exactly preserves argument types). That means your T will have to be moved from the wrapping function's argument into the wrapped function's argument. This won't make much difference if you only use this with C-compatible types.

Definition at line 58 of file callback-converter.h.

◆ make_g_callback

template<auto Fp>
auto Inkscape::Util::make_g_callback = reinterpret_cast<void(*)()>(make_c_callback<Fp>)
inline

A worse version of make_c_callback that also casts the result to a GCallback, losing even more type-safety.

Commonly needed to interface with Glib and GTK. (See make_c_callback for more details.)

Definition at line 65 of file callback-converter.h.

◆ NO_WARNING

constexpr bool Inkscape::Util::NO_WARNING = false
constexpr

Definition at line 29 of file converters.h.

Referenced by SPFeColorMatrix::set().