Vivoe Lite 0.5.0
Lightweight GVA like HMI for military displays.
Loading...
Searching...
No Matches
renderer_cairo.h
Go to the documentation of this file.
1//
2// Copyright (c) 2023, DefenceX PTY LTD
3//
4// This file is part of the VivoeX project developed by DefenceX.
5//
6// Licensed under the Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
7// License. See the LICENSE file in the project root for full license details.
8//
12
13#ifndef HMICORE_RENDERER_CAIRO_H_
14#define HMICORE_RENDERER_CAIRO_H_
15
16#include <cairo.h>
17#include <gtk/gtk.h>
18#include <stdio.h>
19
20#include <iostream>
21#include <memory>
22#include <string>
23#include <vector>
24
26#include "hmicore/gva.h"
27#include "hmicore/renderer.h"
30
31namespace gva {
32
34struct Command {
36 DrawType command = DrawType::kCommandUndefined;
38 double height = 0;
40 double width = 0;
42 std::array<PointType, 3> points;
44 double radius = 0;
46 double angle1 = 0;
48 double angle2 = 0;
52 int32_t arg1 = 0;
54 int32_t arg2 = 0;
56 int32_t arg3 = 0;
58 int32_t arg4 = 0;
60 std::string text = "";
62 bool fill = false;
63};
64
66struct image_type {
68 std::string name;
70 cairo_surface_t *image;
73};
74
76enum class LineCapEnd { kLineCapButt, kLineCapRound, kLineCapSquare };
77
79typedef void (*CallbackType)(void *io, gpointer data);
80
82class RendererCairo : public Renderer {
83 public:
86
93 RendererCairo(uint32_t width, uint32_t height);
94
99 ~RendererCairo() override;
100
109 HandleType *Init(uint32_t width, uint32_t height, bool fullscreen = false);
110
111 // Pure Virtual functions
112 //
117 void SetPixel(uint32_t x, uint32_t y) override;
118
126 void SetColour(uint8_t red, uint8_t green, uint8_t blue) override;
127
133 void SetColour(uint32_t rgb);
134
140 void SetColourForeground(ColourType colour);
141
149 void SetColourForeground(uint8_t red, uint8_t green, uint8_t blue) override;
150
156 void SetColourForeground(uint32_t rgb);
157
163 void SetColourBackground(ColourType colour);
164
172 void SetColourBackground(uint8_t red, uint8_t green, uint8_t blue) override;
173
179 void SetColourBackground(uint32_t rgb);
180
186 void SetLineType(uint32_t type);
187
194 void SetLineThickness(uint32_t thickness, LineType fill);
195
203 void SetLineThickness(uint32_t thickness, LineType fill, LineCapEnd end);
204
212 uint32_t MovePen(int32_t x, int32_t y);
213
222 uint32_t DrawPen(uint32_t x, uint32_t y, bool close);
223
231 uint32_t DrawPen(uint32_t x, uint32_t y) { return DrawPen(x, y, false); }
232
240 uint32_t MovePenRaw(int32_t x, int32_t y);
241
249 uint32_t DrawPenRaw(int32_t x, int32_t y);
250
260 void DrawArcRaw(uint32_t x, uint32_t y, uint32_t radius, uint32_t angle1, uint32_t angle2);
261
271 uint32_t DrawLine(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2) override;
272
277 void Save();
278
283 void Restore();
284
291 void Scale(double x, double y);
292
299 void Translate(uint32_t x, uint32_t y);
300
306 void Rotate(double radians);
307
314 uint32_t ClosePath(bool fill);
315
324 void DrawCircle(uint32_t x, uint32_t y, uint32_t radius, bool fill) override;
325
335 void DrawRectangle(uint32_t x, uint32_t y, uint32_t width, uint32_t height, bool fill) override;
336
347 void DrawRoundedRectangle(uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t corner, bool fill);
348
360 void DrawTriangle(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3, bool fill);
361
368 uint32_t DrawColor(ColourType colour);
369
378 uint32_t DrawColor(uint8_t r, uint8_t g, uint8_t b) override;
379
393 uint32_t CurveTo(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3);
394
401 uint32_t DrawColor(uint32_t rgb);
402
411 void SetTextFont(uint32_t slope, widget::WeightType weight, std::string_view font_name, double size);
412
417 void Push();
418
423 void Pop();
424
432 uint32_t GetTextWidth(std::string_view str, uint32_t fontSize) const;
433
441 uint32_t GetTextHeight(std::string_view str, uint32_t fontSize) const;
442
450 void DrawString(uint32_t x, uint32_t y, std::string_view text);
451
459 void DrawLabel(uint32_t x, uint32_t y, std::string_view text);
460
468 void DrawTextCentre(uint32_t x, std::string_view text, uint32_t size);
469
479 uint32_t TextureRGB(uint32_t x, uint32_t y, unsigned char *buffer, std::string_view file) override;
480
489 uint32_t TextureRGB(uint32_t x, uint32_t y, unsigned char *buffer);
490
499 uint32_t TextureRGB(uint32_t x, uint32_t y, cairo_surface_t *surface);
500
501 // Cairo specific functions
502
507 void Draw();
508
513 void Reset() {
514 draw_commands_.clear();
515 image_list_.clear();
516 }
517
523 void SetHeight(uint32_t height) const { height_ = height; }
524
530 void SetWidth(uint32_t width) const { width_ = width; }
531
537 uint32_t GetHeight() const { return height_; }
538
544 uint32_t GetWidth() const { return height_; }
545
551 void SetSurface(cairo_surface_t *surface);
552
558 void DrawSurface(cairo_t *cr);
559
566 void Configure(uint32_t height, uint32_t width);
567
572 static void DestroySurface();
573
574 private:
575 int32_t current_handle_;
576 double scale_ = 1.0;
577
584 double IntToFloat(int c) const { return static_cast<double>(1) / 255 * c; }
585
586 //
587 // Render List
588 //
589 std::vector<Command> draw_commands_;
590
591 //
592 // Image List
593 //
594 std::vector<image_type> image_list_;
595
596 //
597 // Image Cache
598 //
599 std::vector<image_type> cache_image_list_;
600 ConfigData *config_;
601};
602
603} // namespace gva
604
605#endif // HMICORE_RENDERER_CAIRO_H_
Class definition of the HandleType.
Definition renderer_cairo_types.h:23
Class definition of the Renderer Cairo.
Definition renderer_cairo.h:82
void Configure(uint32_t height, uint32_t width)
Configure the surface to use new dimensions.
Definition renderer_cairo.cc:712
void Draw()
Redraw the screen.
Definition renderer_cairo.cc:39
void DrawArcRaw(uint32_t x, uint32_t y, uint32_t radius, uint32_t angle1, uint32_t angle2)
Definition renderer_cairo.cc:463
uint32_t MovePen(int32_t x, int32_t y)
Definition renderer_cairo.cc:359
void DrawTextCentre(uint32_t x, std::string_view text, uint32_t size)
Draw some text on the screen and centre it.
Definition renderer_cairo.cc:609
void Save()
Save the current state of the renderer.
Definition renderer_cairo.cc:399
uint32_t GetTextHeight(std::string_view str, uint32_t fontSize) const
Get the Text Height object.
Definition renderer_cairo.cc:580
void SetSurface(cairo_surface_t *surface)
Set the Surface attribute, used by application.
Definition renderer_cairo.cc:710
void Scale(double x, double y)
Scale the renderer.
Definition renderer_cairo.cc:411
void Restore()
Restore the last saved state of the renderer.
Definition renderer_cairo.cc:405
~RendererCairo() override
Destroy the Renderer Cairo object.
Definition renderer_cairo.cc:37
void Rotate(double radians)
Rotate the renderer.
Definition renderer_cairo.cc:427
uint32_t DrawLine(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2) override
Definition renderer_cairo.cc:442
void DrawRectangle(uint32_t x, uint32_t y, uint32_t width, uint32_t height, bool fill) override
Draw a rectangle.
Definition renderer_cairo.cc:474
void SetPixel(uint32_t x, uint32_t y) override
Definition renderer_cairo.cc:291
uint32_t TextureRGB(uint32_t x, uint32_t y, unsigned char *buffer, std::string_view file) override
Set a texture (file) to use as a bitmap.
Definition renderer_cairo.cc:613
void DrawTriangle(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3, bool fill)
Draw a triangle.
Definition renderer_cairo.cc:501
void DrawCircle(uint32_t x, uint32_t y, uint32_t radius, bool fill) override
Draw a circle.
Definition renderer_cairo.cc:451
uint32_t GetHeight() const
Get the Height object.
Definition renderer_cairo.h:537
uint32_t MovePenRaw(int32_t x, int32_t y)
Definition renderer_cairo.cc:369
static HandleType render_
Class definition of the HandleType.
Definition renderer_cairo.h:85
uint32_t DrawPenRaw(int32_t x, int32_t y)
Definition renderer_cairo.cc:389
void SetLineType(uint32_t type)
Set the Line Type object.
Definition renderer_cairo.cc:339
uint32_t DrawPen(uint32_t x, uint32_t y, bool close)
Definition renderer_cairo.cc:379
void SetHeight(uint32_t height) const
Set the Height object.
Definition renderer_cairo.h:523
uint32_t GetWidth() const
Get the Width object.
Definition renderer_cairo.h:544
uint32_t CurveTo(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t x3, uint32_t y3)
Adds a cubic Bézier spline to the path from the current point to position (x3 , y3 ) in user-space co...
Definition renderer_cairo.cc:525
void SetWidth(uint32_t width) const
Set the Width object.
Definition renderer_cairo.h:530
void DrawSurface(cairo_t *cr)
Draw the new cairo surface to the screen.
Definition renderer_cairo.cc:705
uint32_t ClosePath(bool fill)
Close drawing path.
Definition renderer_cairo.cc:434
void Reset()
Redraw the screen.
Definition renderer_cairo.h:513
uint32_t DrawColor(ColourType colour)
Set the Colour object to draw with.
Definition renderer_cairo.cc:538
void SetLineThickness(uint32_t thickness, LineType fill)
Set the Line Thickness object.
Definition renderer_cairo.cc:355
void SetTextFont(uint32_t slope, widget::WeightType weight, std::string_view font_name, double size)
Set the Text Font object.
Definition renderer_cairo.cc:548
void SetColourForeground(ColourType colour)
Set the Colour Foreground object.
Definition renderer_cairo.cc:309
void SetColourBackground(ColourType colour)
Set the Colour Background attribute.
Definition renderer_cairo.cc:317
void DrawString(uint32_t x, uint32_t y, std::string_view text)
Draw some text on the screen.
Definition renderer_cairo.cc:589
void Translate(uint32_t x, uint32_t y)
Translate the renderer.
Definition renderer_cairo.cc:419
static void DestroySurface()
Destroy the current cairo surface, usually called during shutdown.
Definition renderer_cairo.cc:719
void Pop()
Pop the last saved state of the renderer.
Definition renderer_cairo.cc:564
void DrawRoundedRectangle(uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t corner, bool fill)
Draw a rounded rectangle.
Definition renderer_cairo.cc:488
uint32_t GetTextWidth(std::string_view str, uint32_t fontSize) const
Get the Text Width object.
Definition renderer_cairo.cc:570
uint32_t DrawPen(uint32_t x, uint32_t y)
Definition renderer_cairo.h:231
void Push()
Push the current state of the renderer.
Definition renderer_cairo.cc:558
HandleType * Init(uint32_t width, uint32_t height, bool fullscreen=false)
Initalise the renderer with display parameters.
Definition renderer_cairo.cc:281
void DrawLabel(uint32_t x, uint32_t y, std::string_view text)
Draw an on screen label.
Definition renderer_cairo.cc:598
void SetColour(uint8_t red, uint8_t green, uint8_t blue) override
Set the Colour object.
Definition renderer_cairo.cc:293
Class defining the renderer.
Definition renderer.h:61
static uint32_t height_
The height of the renderer.
Definition renderer.h:245
friend class RendererCairo
The Cairo renderer.
Definition renderer.h:255
static uint32_t width_
The width of the renderer.
Definition renderer.h:247
LineType
Enum for the label states.
Definition gva.h:54
const gva::ColourType kHmiWhite
The RGB values for white.
Definition renderer.h:24
LineCapEnd
Struct definition of the LineType.
Definition renderer_cairo.h:76
DrawType
Class definition of the DrawType.
Definition renderer_cairo_types.h:40
Struct for the colour.
Definition gva.h:553
The Command structure.
Definition renderer_cairo.h:34
int32_t arg2
An argument value 2.
Definition renderer_cairo.h:54
int32_t arg4
An argument value 4.
Definition renderer_cairo.h:58
double width
The width value.
Definition renderer_cairo.h:40
double height
The height value.
Definition renderer_cairo.h:38
double angle2
The angle2 value.
Definition renderer_cairo.h:48
bool fill
Fill if true.
Definition renderer_cairo.h:62
DrawType command
The command type.
Definition renderer_cairo.h:36
int32_t arg1
An argument value 1.
Definition renderer_cairo.h:52
ColourType colour
The colour value.
Definition renderer_cairo.h:50
std::array< PointType, 3 > points
Points value.
Definition renderer_cairo.h:42
double angle1
The angle1 value.
Definition renderer_cairo.h:46
int32_t arg3
An argument value 3.
Definition renderer_cairo.h:56
double radius
The radius value.
Definition renderer_cairo.h:44
std::string text
String text.
Definition renderer_cairo.h:60
Struct definition of the image_type.
Definition renderer_cairo.h:66
cairo_surface_t * image
The images cairo surface.
Definition renderer_cairo.h:70
std::string name
The images string filename.
Definition renderer_cairo.h:68
bool from_cache
True if read from cache.
Definition renderer_cairo.h:72
WeightType
The line weight for boxes and tables.
Definition widget_types.h:33