Skip to content

Commit

Permalink
Merge pull request rism-digital#3948 from rism-digital/develop-fix-pe…
Browse files Browse the repository at this point in the history
…n-and-brush

Code refactoring for Pen and Brush classes
  • Loading branch information
lpugin authored Feb 10, 2025
2 parents 52f43f9 + 3323d26 commit 4a49b52
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 166 deletions.
13 changes: 9 additions & 4 deletions include/vrv/devicecontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class DeviceContext {
m_baseHeight = 0;
m_pushBack = false;
m_viewBoxFactor = (double)DEFINITION_FACTOR;
this->SetBrush(1.0);
this->SetPen(1, PEN_SOLID);
}
DeviceContext(ClassId classId)
{
Expand All @@ -91,8 +93,10 @@ class DeviceContext {
m_baseHeight = 0;
m_pushBack = false;
m_viewBoxFactor = (double)DEFINITION_FACTOR;
this->SetBrush(1.0);
this->SetPen(1, PEN_SOLID);
}
virtual ~DeviceContext() {}
virtual ~DeviceContext();
ClassId GetClassId() const { return m_classId; }
bool Is(ClassId classId) const { return (m_classId == classId); }
///@}
Expand Down Expand Up @@ -141,9 +145,10 @@ class DeviceContext {
* Non-virtual methods cannot be overridden and manage the Pen, Brush and FontInfo stacks
*/
///@{
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 SetBrush(float opacity, int color = COLOR_NONE);
void SetPen(int width, PenStyle style, int dashLength = 0, int gapLength = 0,
LineCapStyle lineCap = LINECAP_DEFAULT, LineJoinStyle lineJoin = LINEJOIN_DEFAULT, float opacity = 1.0,
int color = COLOR_NONE);
void SetFont(FontInfo *font);
void SetPushBack() { m_pushBack = true; }
void ResetBrush();
Expand Down
21 changes: 11 additions & 10 deletions include/vrv/devicecontextbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ enum LineJoinStyle : int8_t {
class Pen {
public:
Pen()
: m_color(COLOR_NONE)
, m_width(0)
: 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)
, m_color(COLOR_NONE)
{
}
Pen(int color, int width, PenStyle style, int dashLength, int gapLength, LineCapStyle lineCap,
LineJoinStyle lineJoin, float opacity)
: m_color(color)
, m_width(width)
Pen(int width, PenStyle style, int dashLength, int gapLength, LineCapStyle lineCap, LineJoinStyle lineJoin,
float opacity, int color)
: m_width(width)
, m_style(style)
, m_dashLength(dashLength)
, m_gapLength(gapLength)
, m_lineCap(lineCap)
, m_lineJoin(lineJoin)
, m_opacity(opacity)
, m_color(color)
{
}

Expand All @@ -98,27 +98,28 @@ class Pen {
void SetOpacity(float opacity) { m_opacity = opacity; }

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

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

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_color;
float m_opacity;
int m_color;
};

// ---------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions include/vrv/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,6 @@ class View {
* useful for changing the color, for example
*/
///@{
LayerElement *m_currentElement;
Layer *m_currentLayer;
Measure *m_currentMeasure;
Staff *m_currentStaff;
System *m_currentSystem;
Page *m_currentPage;
///@}

Expand Down
3 changes: 0 additions & 3 deletions src/bboxdevicecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ BBoxDeviceContext::BBoxDeviceContext(View *view, int width, int height, unsigned
m_drawingText = false;
m_textAlignment = HORIZONTALALIGNMENT_left;

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

m_update = update;

this->ResetGraphicRotation();
Expand Down
17 changes: 12 additions & 5 deletions src/devicecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ std::pair<double, double> BezierCurve::EstimateCurveParamForControlPoints() cons
// DeviceContext
//----------------------------------------------------------------------------

DeviceContext::~DeviceContext()
{
if (m_penStack.size() != 1) LogDebug("Pen stack should have only one pen");
if (m_brushStack.size() != 1) LogDebug("Brush stack should have only one brush");
if (!m_fontStack.empty()) LogDebug("Font stack should be empty");
}

const Resources *DeviceContext::GetResources(bool showWarning) const
{
if (!m_resources && showWarning) LogWarning("Requested resources unavailable.");
Expand All @@ -134,8 +141,8 @@ void DeviceContext::SetViewBoxFactor(double ppuFactor)
m_viewBoxFactor = double(DEFINITION_FACTOR) / ppuFactor;
}

void DeviceContext::SetPen(int color, int width, PenStyle style, int dashLength, int gapLength, LineCapStyle lineCap,
LineJoinStyle lineJoin, float opacity)
void DeviceContext::SetPen(int width, PenStyle style, int dashLength, int gapLength, LineCapStyle lineCap,
LineJoinStyle lineJoin, float opacity, int color)
{
switch (style) {
case PEN_SOLID: break;
Expand All @@ -154,12 +161,12 @@ void DeviceContext::SetPen(int color, int width, PenStyle style, int dashLength,
default: break; // solid brush by default
}

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

void DeviceContext::SetBrush(int color, float opacity)
void DeviceContext::SetBrush(float opacity, int color)
{
m_brushStack.push(Brush(color, opacity));
m_brushStack.push(Brush(opacity, color));
}

void DeviceContext::SetFont(FontInfo *font)
Expand Down
11 changes: 4 additions & 7 deletions src/svgdevicecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ SvgDeviceContext::SvgDeviceContext() : DeviceContext(SVG_DEVICE_CONTEXT)
m_originX = 0;
m_originY = 0;

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

m_smuflGlyphs.clear();

m_committed = false;
Expand Down Expand Up @@ -1283,11 +1280,11 @@ void SvgDeviceContext::DrawSvgBoundingBox(Object *object, View *view)
p.y = object->GetSelfBottom() - y * smuflGlyphFontSize / glyph->GetUnitsPerEm();
p.y += (fontPoint->y * smuflGlyphFontSize / glyph->GetUnitsPerEm());

this->SetPen(COLOR_GREEN, 10, PEN_SOLID);
this->SetBrush(COLOR_GREEN);
this->SetPen(10, PEN_SOLID, 0, 0, LINECAP_DEFAULT, LINEJOIN_DEFAULT, 1.0, COLOR_GREEN);
this->SetBrush(1.0, COLOR_GREEN);
this->DrawCircle(view->ToDeviceContextX(p.x), view->ToDeviceContextY(p.y), 5);
this->SetPen(COLOR_NONE, 1, PEN_SOLID);
this->SetBrush(COLOR_NONE);
this->ResetBrush();
this->ResetPen();
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ View::View()
m_slurHandling = SlurHandling::Initialize;

m_currentColor = COLOR_NONE;
m_currentElement = NULL;
m_currentLayer = NULL;
m_currentMeasure = NULL;
m_currentStaff = NULL;
m_currentSystem = NULL;
}

View::~View() {}
Expand All @@ -53,11 +48,6 @@ void View::SetDoc(Doc *doc)
m_doc = doc;
m_options = doc->GetOptions();
}
m_currentElement = NULL;
m_currentLayer = NULL;
m_currentMeasure = NULL;
m_currentStaff = NULL;
m_currentSystem = NULL;
m_currentPage = NULL;
m_pageIdx = 0;
}
Expand All @@ -82,12 +72,6 @@ void View::SetPage(int pageIdx, bool doLayout)
}
}

m_currentElement = NULL;
m_currentLayer = NULL;
m_currentMeasure = NULL;
m_currentStaff = NULL;
m_currentSystem = NULL;

OnPageChange();
DoRefresh();
}
Expand Down
Loading

0 comments on commit 4a49b52

Please sign in to comment.