canvas.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * @file canvas.h
  3. * GUI: Canvas API
  4. */
  5. #pragma once
  6. #include <stdint.h>
  7. #include <gui/icon_animation.h>
  8. #include <gui/icon.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /** Color enumeration */
  13. typedef enum {
  14. ColorWhite = 0x00,
  15. ColorBlack = 0x01,
  16. ColorXOR = 0x02,
  17. } Color;
  18. /** Fonts enumeration */
  19. typedef enum {
  20. FontPrimary,
  21. FontSecondary,
  22. FontKeyboard,
  23. FontBigNumbers,
  24. // Keep last for fonts number calculation
  25. FontTotalNumber,
  26. } Font;
  27. /** Alignment enumeration */
  28. typedef enum {
  29. AlignLeft,
  30. AlignRight,
  31. AlignTop,
  32. AlignBottom,
  33. AlignCenter,
  34. } Align;
  35. /** Canvas Orientation */
  36. typedef enum {
  37. CanvasOrientationHorizontal,
  38. CanvasOrientationHorizontalFlip,
  39. CanvasOrientationVertical,
  40. CanvasOrientationVerticalFlip,
  41. } CanvasOrientation;
  42. /** Font Direction */
  43. typedef enum {
  44. CanvasDirectionLeftToRight,
  45. CanvasDirectionTopToBottom,
  46. CanvasDirectionRightToLeft,
  47. CanvasDirectionBottomToTop,
  48. } CanvasDirection;
  49. /** Font parameters */
  50. typedef struct {
  51. uint8_t leading_default;
  52. uint8_t leading_min;
  53. uint8_t height;
  54. uint8_t descender;
  55. } CanvasFontParameters;
  56. /** Canvas anonymous structure */
  57. typedef struct Canvas Canvas;
  58. /** Reset canvas drawing tools configuration
  59. *
  60. * @param canvas Canvas instance
  61. */
  62. void canvas_reset(Canvas* canvas);
  63. /** Commit canvas. Send buffer to display
  64. *
  65. * @param canvas Canvas instance
  66. */
  67. void canvas_commit(Canvas* canvas);
  68. /** Get Canvas width
  69. *
  70. * @param canvas Canvas instance
  71. *
  72. * @return width in pixels.
  73. */
  74. uint8_t canvas_width(const Canvas* canvas);
  75. /** Get Canvas height
  76. *
  77. * @param canvas Canvas instance
  78. *
  79. * @return height in pixels.
  80. */
  81. uint8_t canvas_height(const Canvas* canvas);
  82. /** Get current font height
  83. *
  84. * @param canvas Canvas instance
  85. *
  86. * @return height in pixels.
  87. */
  88. uint8_t canvas_current_font_height(const Canvas* canvas);
  89. /** Get font parameters
  90. *
  91. * @param canvas Canvas instance
  92. * @param font Font
  93. *
  94. * @return pointer to CanvasFontParameters structure
  95. */
  96. const CanvasFontParameters* canvas_get_font_params(const Canvas* canvas, Font font);
  97. /** Clear canvas
  98. *
  99. * @param canvas Canvas instance
  100. */
  101. void canvas_clear(Canvas* canvas);
  102. /** Set drawing color
  103. *
  104. * @param canvas Canvas instance
  105. * @param color Color
  106. */
  107. void canvas_set_color(Canvas* canvas, Color color);
  108. /** Set font swap
  109. * Argument String Rotation Description
  110. *
  111. * @param canvas Canvas instance
  112. * @param dir Direction font
  113. */
  114. void canvas_set_font_direction(Canvas* canvas, CanvasDirection dir);
  115. /** Invert drawing color
  116. *
  117. * @param canvas Canvas instance
  118. */
  119. void canvas_invert_color(Canvas* canvas);
  120. /** Set drawing font
  121. *
  122. * @param canvas Canvas instance
  123. * @param font Font
  124. */
  125. void canvas_set_font(Canvas* canvas, Font font);
  126. /** Set custom drawing font
  127. *
  128. * @param canvas Canvas instance
  129. * @param font Pointer to u8g2 const uint8_t* font array
  130. */
  131. void canvas_set_custom_u8g2_font(Canvas* canvas, const uint8_t* font);
  132. /** Draw string at position of baseline defined by x, y.
  133. *
  134. * @param canvas Canvas instance
  135. * @param x anchor point x coordinate
  136. * @param y anchor point y coordinate
  137. * @param str C-string
  138. */
  139. void canvas_draw_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str);
  140. /** Draw aligned string defined by x, y.
  141. *
  142. * Align calculated from position of baseline, string width and ascent (height
  143. * of the glyphs above the baseline)
  144. *
  145. * @param canvas Canvas instance
  146. * @param x anchor point x coordinate
  147. * @param y anchor point y coordinate
  148. * @param horizontal horizontal alignment
  149. * @param vertical vertical alignment
  150. * @param str C-string
  151. */
  152. void canvas_draw_str_aligned(
  153. Canvas* canvas,
  154. uint8_t x,
  155. uint8_t y,
  156. Align horizontal,
  157. Align vertical,
  158. const char* str);
  159. /** Get string width
  160. *
  161. * @param canvas Canvas instance
  162. * @param str C-string
  163. *
  164. * @return width in pixels.
  165. */
  166. uint16_t canvas_string_width(Canvas* canvas, const char* str);
  167. /** Get glyph width
  168. *
  169. * @param canvas Canvas instance
  170. * @param[in] symbol character
  171. *
  172. * @return width in pixels
  173. */
  174. uint8_t canvas_glyph_width(Canvas* canvas, char symbol);
  175. /** Draw bitmap picture at position defined by x,y.
  176. *
  177. * @param canvas Canvas instance
  178. * @param x x coordinate
  179. * @param y y coordinate
  180. * @param width width of bitmap
  181. * @param height height of bitmap
  182. * @param compressed_bitmap_data compressed bitmap data
  183. */
  184. void canvas_draw_bitmap(
  185. Canvas* canvas,
  186. uint8_t x,
  187. uint8_t y,
  188. uint8_t width,
  189. uint8_t height,
  190. const uint8_t* compressed_bitmap_data);
  191. /** Draw animation at position defined by x,y.
  192. *
  193. * @param canvas Canvas instance
  194. * @param x x coordinate
  195. * @param y y coordinate
  196. * @param icon_animation IconAnimation instance
  197. */
  198. void canvas_draw_icon_animation(
  199. Canvas* canvas,
  200. uint8_t x,
  201. uint8_t y,
  202. IconAnimation* icon_animation);
  203. /** Draw icon at position defined by x,y.
  204. *
  205. * @param canvas Canvas instance
  206. * @param x x coordinate
  207. * @param y y coordinate
  208. * @param icon Icon instance
  209. */
  210. void canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, const Icon* icon);
  211. /** Draw XBM bitmap
  212. *
  213. * @param canvas Canvas instance
  214. * @param x x coordinate
  215. * @param y y coordinate
  216. * @param w bitmap width
  217. * @param h bitmap height
  218. * @param bitmap pointer to XBM bitmap data
  219. */
  220. void canvas_draw_xbm(
  221. Canvas* canvas,
  222. uint8_t x,
  223. uint8_t y,
  224. uint8_t w,
  225. uint8_t h,
  226. const uint8_t* bitmap);
  227. /** Draw dot at x,y
  228. *
  229. * @param canvas Canvas instance
  230. * @param x x coordinate
  231. * @param y y coordinate
  232. */
  233. void canvas_draw_dot(Canvas* canvas, uint8_t x, uint8_t y);
  234. /** Draw box of width, height at x,y
  235. *
  236. * @param canvas Canvas instance
  237. * @param x x coordinate
  238. * @param y y coordinate
  239. * @param width box width
  240. * @param height box height
  241. */
  242. void canvas_draw_box(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height);
  243. /** Draw frame of width, height at x,y
  244. *
  245. * @param canvas Canvas instance
  246. * @param x x coordinate
  247. * @param y y coordinate
  248. * @param width frame width
  249. * @param height frame height
  250. */
  251. void canvas_draw_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height);
  252. /** Draw line from x1,y1 to x2,y2
  253. *
  254. * @param canvas Canvas instance
  255. * @param x1 x1 coordinate
  256. * @param y1 y1 coordinate
  257. * @param x2 x2 coordinate
  258. * @param y2 y2 coordinate
  259. */
  260. void canvas_draw_line(Canvas* canvas, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2);
  261. /** Draw circle at x,y with radius r
  262. *
  263. * @param canvas Canvas instance
  264. * @param x x coordinate
  265. * @param y y coordinate
  266. * @param r radius
  267. */
  268. void canvas_draw_circle(Canvas* canvas, uint8_t x, uint8_t y, uint8_t r);
  269. /** Draw disc at x,y with radius r
  270. *
  271. * @param canvas Canvas instance
  272. * @param x x coordinate
  273. * @param y y coordinate
  274. * @param r radius
  275. */
  276. void canvas_draw_disc(Canvas* canvas, uint8_t x, uint8_t y, uint8_t r);
  277. /** Draw triangle with given base and height lengths and their intersection coordinate
  278. *
  279. * @param canvas Canvas instance
  280. * @param x x coordinate of base and height intersection
  281. * @param y y coordinate of base and height intersection
  282. * @param base length of triangle side
  283. * @param height length of triangle height
  284. * @param dir CanvasDirection triangle orientation
  285. */
  286. void canvas_draw_triangle(
  287. Canvas* canvas,
  288. uint8_t x,
  289. uint8_t y,
  290. uint8_t base,
  291. uint8_t height,
  292. CanvasDirection dir);
  293. /** Draw glyph
  294. *
  295. * @param canvas Canvas instance
  296. * @param x x coordinate
  297. * @param y y coordinate
  298. * @param ch character
  299. */
  300. void canvas_draw_glyph(Canvas* canvas, uint8_t x, uint8_t y, uint16_t ch);
  301. /** Set transparency mode
  302. *
  303. * @param canvas Canvas instance
  304. * @param alpha transparency mode
  305. */
  306. void canvas_set_bitmap_mode(Canvas* canvas, bool alpha);
  307. /** Draw rounded-corner frame of width, height at x,y, with round value radius
  308. *
  309. * @param canvas Canvas instance
  310. * @param x x coordinate
  311. * @param y y coordinate
  312. * @param width frame width
  313. * @param height frame height
  314. * @param radius frame corner radius
  315. */
  316. void canvas_draw_rframe(
  317. Canvas* canvas,
  318. uint8_t x,
  319. uint8_t y,
  320. uint8_t width,
  321. uint8_t height,
  322. uint8_t radius);
  323. /** Draw rounded-corner box of width, height at x,y, with round value raduis
  324. *
  325. * @param canvas Canvas instance
  326. * @param x x coordinate
  327. * @param y y coordinate
  328. * @param width box width
  329. * @param height box height
  330. * @param radius box corner radius
  331. */
  332. void canvas_draw_rbox(
  333. Canvas* canvas,
  334. uint8_t x,
  335. uint8_t y,
  336. uint8_t width,
  337. uint8_t height,
  338. uint8_t radius);
  339. #ifdef __cplusplus
  340. }
  341. #endif