processing_api.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // This file is Copyright (c) 2025 Victor Suarez Rovere <suarezvictor@gmail.com>
  2. // SPDX-License-Identifier: AGPL-3.0-only
  3. #ifndef __PROCESSING_API_H__
  4. #define __PROCESSING_API_H__
  5. #include "accel_canvas.h"
  6. #include <memory> //shared_ptr
  7. #include <string>
  8. namespace processing
  9. {
  10. class Font
  11. {
  12. public:
  13. typedef float fontsize_t;
  14. protected:
  15. std::string m_name;
  16. fontsize_t m_height;
  17. public:
  18. Font(const char *name, fontsize_t height) : m_name(name), m_height(height) { }
  19. const char *fontname() const { return m_name.c_str(); }
  20. fontsize_t height() const { return m_height; }
  21. };
  22. typedef std::shared_ptr<Font> PFont;
  23. //see https://processing.github.io/processing-javadocs/core/
  24. class PGraphics : public CanvasBase
  25. {
  26. typedef CanvasBase base;
  27. color_t m_bgColor, m_fillColor, m_strokeColor;
  28. coord_t m_strokeWidth;
  29. PFont m_currentFont;
  30. friend PFont createFont(const char *name, Font::fontsize_t fontsize, const char *buf, size_t bufsize);
  31. path_t m_lastpath;
  32. public:
  33. int& width;
  34. int& height;
  35. PGraphics() :
  36. m_bgColor(0x33, 0x33, 0x33),
  37. m_fillColor(0x66, 0x66, 0x66, 0xFF),
  38. m_strokeColor(0, 0, 0, 0), //start noStroke
  39. m_strokeWidth(1),
  40. width(m_width), height(m_height)
  41. {
  42. }
  43. void background(color_channel_t r, color_channel_t g, color_channel_t b, color_channel_t a = 255) { m_bgColor = color_t(r, g, b, a); }
  44. void background(color_t color) { m_bgColor = color; }
  45. color_t background() const { return m_bgColor; }
  46. void clear() { m_ren.clear(m_bgColor); }
  47. void cleartocheckers(int sqsize = 16);
  48. void fill(color_channel_t r, color_channel_t g, color_channel_t b, color_channel_t a = 255) { m_fillColor = color_t(r, g, b, a); }
  49. void fill(color_t color) { m_fillColor = color; }
  50. color_t fill() const { return m_fillColor; }
  51. void noFill() { m_fillColor.transparent(); }
  52. void stroke(color_channel_t r, color_channel_t g, color_channel_t b, color_channel_t a = 255) { m_strokeColor = color_t(r, g, b, a); }
  53. void stroke(color_t color) { m_strokeColor = color; }
  54. color_t stroke() const { return m_strokeColor; }
  55. void noStroke() { m_strokeColor.transparent(); }
  56. //
  57. // Font
  58. //
  59. void textFont(PFont font)
  60. {
  61. PGraphics::fontManager().load_font(font->fontname(), font->height());
  62. m_currentFont = font;
  63. }
  64. void text(std::string str, coord_t x, coord_t y)
  65. {
  66. agg::path_storage path(fontManager().text(str.c_str(), x, y));
  67. conv_path_t glyphs(path);
  68. render_fill(glyphs, m_fillColor);
  69. }
  70. void textNoCurves(std::string str, coord_t x, coord_t y)
  71. {
  72. agg::path_storage path(fontManager().text(str.c_str(), x, y));
  73. render_fill(path, m_fillColor);
  74. }
  75. //
  76. // Shape - 2D Primitives
  77. //
  78. void rect(coord_t x, coord_t y, coord_t w, coord_t h, coord_t r = 0)
  79. {
  80. agg::rounded_rect rect1 (x, y, w, h, r);
  81. render_fill(rect1, m_fillColor);
  82. }
  83. void ellipse(coord_t cx, coord_t cy, coord_t rx, coord_t ry)
  84. {
  85. m_lastpath.remove_all();
  86. agg::ellipse el(cx, cy, rx, ry);
  87. //printf("ellipse %d, %d, %d, %d, color %d,%d,%d,%d(r)\n", int(cx), int(cy), int(rx), int(ry), m_fillColor.r, m_fillColor.g, m_fillColor.b, m_fillColor.a);
  88. m_lastpath.concat_path(el);
  89. render_fill(m_lastpath, m_fillColor);
  90. }
  91. void line(coord_t x1, coord_t y1, coord_t x2, coord_t y2)
  92. {
  93. m_lastpath.remove_all();
  94. m_lastpath.move_to(x1, y1);
  95. m_lastpath.line_to(x2, y2);
  96. #ifdef ACCEL_LINE32_REGION
  97. if(m_strokeWidth == 1)
  98. {
  99. m_pixf.line(x1, y1, x2-x1, y2-y1, m_strokeColor); //hardware acclerated version
  100. return;
  101. }
  102. #endif
  103. render_stroke(m_lastpath, m_strokeColor, m_strokeWidth);
  104. }
  105. template <class T>
  106. void draw(T& path, coord_t dx, coord_t dy)
  107. {
  108. agg::trans_affine mtx;
  109. conv_path_t conv(path); //FIXME: optimize by using already converted paths
  110. agg::conv_transform<conv_path_t> trans_shape(conv, mtx);
  111. mtx.translate(dx, dy);
  112. render_fill(trans_shape, m_fillColor);
  113. }
  114. path_t& lastshape() { return m_lastpath; };
  115. void lastshape(coord_t dx, coord_t dy) { draw(lastshape(), dx, dy); }
  116. //
  117. // Shape - Attributes
  118. //
  119. void strokeWeight(coord_t weight) { m_strokeWidth = weight; }
  120. };
  121. static const PGraphics::color_t
  122. RED(0xff, 0, 0, 0xff),
  123. GREEN(0, 0xff, 0, 0xff),
  124. BLUE(0, 0, 0xff, 0xff),
  125. YELLOW(0xff, 0xff, 0, 0xff),
  126. WHITE(0xff, 0xff, 0xff, 0xff),
  127. DARKGRAY(0x40, 0x40, 0x40, 0xff),
  128. GRAY(0x80, 0x80, 0x80, 0xff),
  129. LIGHTGRAY(0xc0, 0xc0, 0xc0, 0xff),
  130. BLACK(0, 0, 0, 0xff),
  131. SHADOW(0, 0, 0, 96),
  132. LIGHT(0xff, 0xff, 0xff, 96);
  133. PFont createFont(const char *name, Font::fontsize_t fontsize, const char *buf = nullptr, size_t bufsize = 0);
  134. //
  135. //misc functions
  136. //
  137. static inline void println(std::string x) { printf("%s\n", x.c_str()); }
  138. static inline PGraphics::angle_radians_t radians(PGraphics::angle_degrees_t degrees)
  139. {
  140. return degrees*M_PI/PGraphics::angle_degrees_t(180.0);
  141. }
  142. } //namespace processing
  143. #endif // __PROCESSING_API_H__