Skip to content

Commit

Permalink
Merge pull request rism-digital#3945 from rism-digital/develop-pen-st…
Browse files Browse the repository at this point in the history
…yles

Code refactoring for pen and brush styles / opacity
  • Loading branch information
lpugin authored Feb 7, 2025
2 parents 065a406 + b43eae1 commit 12b2447
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 254 deletions.
2 changes: 1 addition & 1 deletion include/vrv/bboxdevicecontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BBoxDeviceContext : public DeviceContext {
* @name Setters
*/
///@{
void SetBackground(int color, int style = AxSOLID) override;
void SetBackground(int color, int style = PEN_SOLID) override;
void SetBackgroundImage(void *image, double opacity = 1.0) override {};
void SetBackgroundMode(int mode) override;
void SetTextForeground(int color) override;
Expand Down
8 changes: 4 additions & 4 deletions include/vrv/devicecontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ class DeviceContext {
* Non-virtual methods cannot be overridden and manage the Pen, Brush and FontInfo stacks
*/
///@{
void SetBrush(int color, int opacity);
void SetPen(
int color, int width, int style, int dashLength = 0, int gapLength = 0, int lineCap = 0, int lineJoin = 0);
void SetBrush(int color, float opacity = 1.0);
void SetPen(int color, int width, PenStyle style, int dashLength = 0, int gapLength = 0,
LineCapStyle lineCap = LINECAP_DEFAULT, LineJoinStyle lineJoin = LINEJOIN_DEFAULT, float opacity = 1.0);
void SetFont(FontInfo *font);
void SetPushBack() { m_pushBack = true; }
void ResetBrush();
void ResetPen();
void ResetFont();
void ResetPushBack() { m_pushBack = false; }
virtual void SetBackground(int color, int style = AxSOLID) = 0;
virtual void SetBackground(int color, int style = PEN_SOLID) = 0;
virtual void SetBackgroundImage(void *image, double opacity = 1.0) = 0;
virtual void SetBackgroundMode(int mode) = 0;
virtual void SetTextForeground(int color) = 0;
Expand Down
123 changes: 60 additions & 63 deletions include/vrv/devicecontextbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,26 @@ namespace vrv {

class Doc;

#define AxNONE -1
#define AxWHITE 255 << 16 | 255 << 8 | 255
#define AxBLACK 0
#define AxRED 255 << 16
#define AxBLUE 255
#define AxGREEN 255 << 8
#define AxCYAN 255 << 8 | 255
#define AxLIGHT_GREY 127 << 16 | 127 << 8 | 127
#undef max
#undef min

enum {
/* Pen styles */
AxSOLID = 100,
AxDOT,
AxLONG_DASH,
AxSHORT_DASH,
AxDOT_DASH,
AxUSER_DASH,
AxTRANSPARENT
};

enum {
/* Line cap styles */
AxCAP_UNKNOWN = 0,
AxCAP_BUTT,
AxCAP_ROUND,
AxCAP_SQUARE
};

enum {
/* Line join styles */
AxJOIN_UNKNOWN = 0,
AxJOIN_ARCS,
AxJOIN_BEVEL,
AxJOIN_MITER,
AxJOIN_MITER_CLIP,
AxJOIN_ROUND
#define COLOR_NONE -1
#define COLOR_WHITE 255 << 16 | 255 << 8 | 255
#define COLOR_BLACK 0
#define COLOR_RED 255 << 16
#define COLOR_BLUE 255
#define COLOR_GREEN 255 << 8
#define COLOR_CYAN 255 << 8 | 255
#define COLOR_LIGHT_GREY 127 << 16 | 127 << 8 | 127

enum PenStyle : int8_t { PEN_SOLID = 0, PEN_DOT, PEN_LONG_DASH, PEN_SHORT_DASH, PEN_DOT_DASH };

enum LineCapStyle : int8_t { LINECAP_DEFAULT = 0, LINECAP_BUTT, LINECAP_ROUND, LINECAP_SQUARE };

enum LineJoinStyle : int8_t {
LINEJOIN_DEFAULT = 0,
LINEJOIN_ARCS,
LINEJOIN_BEVEL,
LINEJOIN_MITER,
LINEJOIN_MITER_CLIP,
LINEJOIN_ROUND
};

// ---------------------------------------------------------------------------
Expand All @@ -71,53 +53,68 @@ enum {
class Pen {
public:
Pen()
: m_penColor(0), m_penWidth(0), m_dashLength(0), m_gapLength(0), m_lineCap(0), m_lineJoin(0), m_penOpacity(0.0)
: m_color(COLOR_NONE)
, m_width(0)
, m_style(PEN_SOLID)
, m_dashLength(0)
, m_gapLength(0)
, m_lineCap(LINECAP_DEFAULT)
, m_lineJoin(LINEJOIN_DEFAULT)
, m_opacity(1.0)
{
}
Pen(int color, int width, float opacity, int dashLength, int gapLength, int lineCap, int lineJoin)
: m_penColor(color)
, m_penWidth(width)
Pen(int color, int width, PenStyle style, int dashLength, int gapLength, LineCapStyle lineCap,
LineJoinStyle lineJoin, float opacity)
: m_color(color)
, m_width(width)
, m_style(style)
, m_dashLength(dashLength)
, m_gapLength(gapLength)
, m_lineCap(lineCap)
, m_lineJoin(lineJoin)
, m_penOpacity(opacity)
, m_opacity(opacity)
{
}

int GetColor() const { return m_penColor; }
void SetColor(int color) { m_penColor = color; }
int GetWidth() const { return m_penWidth; }
void SetWidth(int width) { m_penWidth = width; }
int GetColor() const { return m_color; }
void SetColor(int color) { m_color = color; }
int GetWidth() const { return m_width; }
void SetWidth(int width) { m_width = width; }
int GetDashLength() const { return m_dashLength; }
void SetDashLength(int dashLength) { m_dashLength = dashLength; }
int GetGapLength() const { return m_gapLength; }
void SetGapLength(int gapLength) { m_gapLength = gapLength; }
int GetLineCap() const { return m_lineCap; }
void SetLineCap(int lineCap) { m_lineCap = lineCap; }
int GetLineJoin() const { return m_lineJoin; }
void SetLineJoin(int lineJoin) { m_lineJoin = lineJoin; }
float GetOpacity() const { return m_penOpacity; }
void SetOpacity(float opacity) { m_penOpacity = opacity; }
LineCapStyle GetLineCap() const { return m_lineCap; }
void SetLineCap(LineCapStyle lineCap) { m_lineCap = lineCap; }
LineJoinStyle GetLineJoin() const { return m_lineJoin; }
void SetLineJoin(LineJoinStyle lineJoin) { m_lineJoin = lineJoin; }
PenStyle GetStyle() const { return m_style; }
void SetStyle(PenStyle style) { m_style = style; }
float GetOpacity() const { return m_opacity; }
void SetOpacity(float opacity) { m_opacity = opacity; }

private:
int m_penColor, m_penWidth, m_dashLength, m_gapLength, m_lineCap, m_lineJoin;
float m_penOpacity;
int m_color, m_width;
PenStyle m_style;
int m_dashLength, m_gapLength;
LineCapStyle m_lineCap;
LineJoinStyle m_lineJoin;
float m_opacity;
};

class Brush {
public:
Brush() : m_brushColor(0), m_brushOpacity(0.0) {}
Brush(int color, float opacity) : m_brushColor(color), m_brushOpacity(opacity) {}
Brush() : m_color(COLOR_NONE), m_opacity(1.0) {}
Brush(int color, float opacity) : m_color(color), m_opacity(opacity) {}

int GetColor() const { return m_brushColor; }
void SetColor(int color) { m_brushColor = color; }
float GetOpacity() const { return m_brushOpacity; }
void SetOpacity(float opacity) { m_brushOpacity = opacity; }
int GetColor() const { return m_color; }
void SetColor(int color) { m_color = color; }
float GetOpacity() const { return m_opacity; }
void SetOpacity(float opacity) { m_opacity = opacity; }

private:
int m_brushColor;
float m_brushOpacity;
int m_color;
float m_opacity;
};

// ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion include/vrv/svgdevicecontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SvgDeviceContext : public DeviceContext {
* @name Setters
*/
///@{
void SetBackground(int color, int style = AxSOLID) override;
void SetBackground(int color, int style = PEN_SOLID) override;
void SetBackgroundImage(void *image, double opacity = 1.0) override;
void SetBackgroundMode(int mode) override;
void SetTextForeground(int color) override;
Expand Down
3 changes: 1 addition & 2 deletions include/vrv/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,7 @@ class View {
// int staffSize, bool dimin, bool setBBGlyph = false);

void DrawThickBezierCurve(
DeviceContext *dc, Point bezier[4], int thickness, int staffSize, int penWidth, int penStyle = AxSOLID);
void DrawPartFilledRectangle(DeviceContext *dc, int x1, int y1, int x2, int y2, int fillSection);
DeviceContext *dc, Point bezier[4], int thickness, int staffSize, int penWidth, PenStyle penStyle = PEN_SOLID);
void DrawTextString(DeviceContext *dc, const std::u32string &str, TextDrawingParams &params);
void DrawDirString(DeviceContext *dc, const std::u32string &str, TextDrawingParams &params);
void DrawDynamString(DeviceContext *dc, const std::u32string &str, TextDrawingParams &params, Rend *rend);
Expand Down
4 changes: 2 additions & 2 deletions src/bboxdevicecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ BBoxDeviceContext::BBoxDeviceContext(View *view, int width, int height, unsigned
m_drawingText = false;
m_textAlignment = HORIZONTALALIGNMENT_left;

this->SetBrush(AxNONE, AxSOLID);
this->SetPen(AxNONE, 1, AxSOLID);
this->SetBrush(COLOR_NONE);
this->SetPen(COLOR_NONE, 1, PEN_SOLID);

m_update = update;

Expand Down
33 changes: 10 additions & 23 deletions src/devicecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,45 +134,32 @@ void DeviceContext::SetViewBoxFactor(double ppuFactor)
m_viewBoxFactor = double(DEFINITION_FACTOR) / ppuFactor;
}

void DeviceContext::SetPen(int color, int width, int style, int dashLength, int gapLength, int lineCap, int lineJoin)
void DeviceContext::SetPen(int color, int width, PenStyle style, int dashLength, int gapLength, LineCapStyle lineCap,
LineJoinStyle lineJoin, float opacity)
{
float opacityValue;

switch (style) {
case AxSOLID: opacityValue = 1.0; break;
case AxDOT:
case PEN_SOLID: break;
case PEN_DOT:
dashLength = dashLength ? dashLength : 1;
gapLength = gapLength ? gapLength : width * 3;
opacityValue = 1.0;
break;
case AxLONG_DASH:
case PEN_LONG_DASH:
dashLength = dashLength ? dashLength : width * 4;
gapLength = gapLength ? gapLength : width * 3;
opacityValue = 1.0;
break;
case AxSHORT_DASH:
case PEN_SHORT_DASH:
dashLength = dashLength ? dashLength : width * 2;
gapLength = gapLength ? gapLength : width * 3;
opacityValue = 1.0;
break;
case AxTRANSPARENT: opacityValue = 0.0; break;
default: opacityValue = 1.0; // solid brush by default
default: break; // solid brush by default
}

m_penStack.push(Pen(color, width, opacityValue, dashLength, gapLength, lineCap, lineJoin));
m_penStack.push(Pen(color, width, style, dashLength, gapLength, lineCap, lineJoin, opacity));
}

void DeviceContext::SetBrush(int color, int opacity)
void DeviceContext::SetBrush(int color, float opacity)
{
float opacityValue;

switch (opacity) {
case AxSOLID: opacityValue = 1.0; break;
case AxTRANSPARENT: opacityValue = 0.0; break;
default: opacityValue = 1.0; // solid brush by default
}

m_brushStack.push(Brush(color, opacityValue));
m_brushStack.push(Brush(color, opacity));
}

void DeviceContext::SetFont(FontInfo *font)
Expand Down
Loading

0 comments on commit 12b2447

Please sign in to comment.