graphics.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
  2. // Very simple graphics library to do simple things.
  3. //
  4. // Might be useful to consider using Cairo instead and just have an interface
  5. // between that and the Canvas. Well, this is a quick set of things to get
  6. // started (and nicely self-contained).
  7. #ifndef RPI_GRAPHICS_H
  8. #define RPI_GRAPHICS_H
  9. #include "canvas.h"
  10. #include <stdint.h>
  11. #include <stddef.h>
  12. #include <map>
  13. namespace rgb_matrix {
  14. struct Color {
  15. Color() : r(0), g(0), b(0) {}
  16. Color(uint8_t rr, uint8_t gg, uint8_t bb) : r(rr), g(gg), b(bb) {}
  17. uint8_t r;
  18. uint8_t g;
  19. uint8_t b;
  20. };
  21. // Font loading bdf files. If this ever becomes more types, just make virtual
  22. // base class.
  23. class Font {
  24. public:
  25. // Initialize font, but it is only usable after LoadFont() has been called.
  26. Font();
  27. ~Font();
  28. bool LoadFont(const char *path);
  29. // Return height of font in pixels. Returns -1 if font has not been loaded.
  30. int height() const { return font_height_; }
  31. // Return baseline. Pixels from the topline to the baseline.
  32. int baseline() const { return base_line_; }
  33. // Return width of given character, or -1 if font is not loaded or character
  34. // does not exist.
  35. int CharacterWidth(uint32_t unicode_codepoint) const;
  36. // Draws the unicode character at position "x","y"
  37. // with "color" on "background_color" (background_color can be NULL for
  38. // transparency.
  39. // The "y" position is the baseline of the font.
  40. // If we don't have it in the font, draws the replacement character "�" if
  41. // available.
  42. // Returns how much we advance on the screen, which is the width of the
  43. // character or 0 if we didn't draw any chracter.
  44. int DrawGlyph(Canvas *c, int x, int y,
  45. const Color &color, const Color *background_color,
  46. uint32_t unicode_codepoint) const;
  47. // Same without background. Deprecated, use the one above instead.
  48. int DrawGlyph(Canvas *c, int x, int y, const Color &color,
  49. uint32_t unicode_codepoint) const;
  50. // Create a new font derived from this font, which represents an outline
  51. // of the original font, essentially pixels tracing around the original
  52. // letter.
  53. // This can be used in situations in which it is desirable to frame a letter
  54. // in a different color to increase contrast.
  55. // The ownership of the returned pointer is passed to the caller.
  56. Font *CreateOutlineFont() const;
  57. private:
  58. Font(const Font& x); // No copy constructor. Use references or pointer instead.
  59. struct Glyph;
  60. typedef std::map<uint32_t, Glyph*> CodepointGlyphMap;
  61. const Glyph *FindGlyph(uint32_t codepoint) const;
  62. int font_height_;
  63. int base_line_;
  64. CodepointGlyphMap glyphs_;
  65. };
  66. // -- Some utility functions.
  67. // Utility function: set an image from the given buffer containting pixels.
  68. //
  69. // Draw image of size "image_width" and "image_height" from pixel at
  70. // canvas-offset "canvas_offset_x", "canvas_offset_y". Image will be shown
  71. // cropped on the edges if needed.
  72. //
  73. // The canvas offset can be negative, i.e. the image start can be shifted
  74. // outside the image frame on the left/top edge.
  75. //
  76. // The buffer needs to be organized as rows with columns of three bytes
  77. // organized as rgb or bgr. Thus the size of the buffer needs to be exactly
  78. // (3 * image_width * image_height) bytes.
  79. //
  80. // The "image_buffer" parameters contains the data, "buffer_size_bytes" the
  81. // size in bytes.
  82. //
  83. // If "is_bgr" is true, the buffer is treated as BGR pixel arrangement instead
  84. // of RGB.
  85. // Returns 'true' if image was shown within canvas.
  86. bool SetImage(Canvas *c, int canvas_offset_x, int canvas_offset_y,
  87. const uint8_t *image_buffer, size_t buffer_size_bytes,
  88. int image_width, int image_height,
  89. bool is_bgr);
  90. // Draw text, a standard NUL terminated C-string encoded in UTF-8,
  91. // with given "font" at "x","y" with "color".
  92. // "color" always needs to be set (hence it is a reference),
  93. // "background_color" is a pointer to optionally be NULL for transparency.
  94. // "kerning_offset" allows for additional spacing between characters (can be
  95. // negative)
  96. // Returns how many pixels we advanced on the screen.
  97. int DrawText(Canvas *c, const Font &font, int x, int y,
  98. const Color &color, const Color *background_color,
  99. const char *utf8_text, int kerning_offset = 0);
  100. // Same without background. Deprecated, use the one above instead.
  101. int DrawText(Canvas *c, const Font &font, int x, int y, const Color &color,
  102. const char *utf8_text);
  103. // Draw text, a standard NUL terminated C-string encoded in UTF-8,
  104. // with given "font" at "x","y" with "color".
  105. // Draw text as above, but vertically (top down).
  106. // The text is a standard NUL terminated C-string encoded in UTF-8.
  107. // "font, "x", "y", "color" and "background_color" are same as DrawText().
  108. // "kerning_offset" allows for additional spacing between characters (can be
  109. // negative).
  110. // Returns font height to advance up on the screen.
  111. int VerticalDrawText(Canvas *c, const Font &font, int x, int y,
  112. const Color &color, const Color *background_color,
  113. const char *utf8_text, int kerning_offset = 0);
  114. // Draw a circle centered at "x", "y", with a radius of "radius" and with "color"
  115. void DrawCircle(Canvas *c, int x, int y, int radius, const Color &color);
  116. // Draw a line from "x0", "y0" to "x1", "y1" and with "color"
  117. void DrawLine(Canvas *c, int x0, int y0, int x1, int y1, const Color &color);
  118. } // namespace rgb_matrix
  119. #endif // RPI_GRAPHICS_H