Separate class for Stroke style

#704
presentation
Andreas Butti 7 years ago
parent 690e7ba9a3
commit e19d17385e
  1. 21
      src/control/Tool.h
  2. 6
      src/control/ToolHandler.cpp
  3. 2
      src/control/tools/StrokeHandler.cpp
  4. 2
      src/control/xojfile/LoadHandler.cpp
  5. 2
      src/control/xojfile/SaveHandler.cpp
  6. 1
      src/model/Font.h
  7. 115
      src/model/LineStyle.cpp
  8. 66
      src/model/LineStyle.h
  9. 83
      src/model/Stroke.cpp
  10. 21
      src/model/Stroke.h
  11. 37
      src/model/StrokeStyle.cpp
  12. 5
      src/model/StrokeStyle.h
  13. 1
      src/util/XournalTypeList.h
  14. 2
      src/view/StrokeView.cpp

@ -93,16 +93,17 @@ DrawingType drawingTypeFromString(string type);
enum ToolCapabilities
{
TOOL_CAP_NONE = 0,
TOOL_CAP_COLOR = 1 << 0,
TOOL_CAP_SIZE = 1 << 1,
TOOL_CAP_RULER = 1 << 2,
TOOL_CAP_RECTANGLE = 1 << 3,
TOOL_CAP_CIRCLE = 1 << 4,
TOOL_CAP_ARROW = 1 << 5,
TOOL_CAP_RECOGNIZER = 1 << 6,
TOOL_CAP_FILL = 1 << 7,
TOOL_CAP_COORDINATE_SYSTEM = 1 << 8,
TOOL_CAP_NONE = 0,
TOOL_CAP_COLOR = 1 << 0,
TOOL_CAP_SIZE = 1 << 1,
TOOL_CAP_RULER = 1 << 2,
TOOL_CAP_RECTANGLE = 1 << 3,
TOOL_CAP_CIRCLE = 1 << 4,
TOOL_CAP_ARROW = 1 << 5,
TOOL_CAP_RECOGNIZER = 1 << 6,
TOOL_CAP_FILL = 1 << 7,
TOOL_CAP_COORDINATE_SYSTEM = 1 << 8,
TOOL_CAP_DASH_LINE = 1 << 9,
};
class Tool

@ -40,7 +40,8 @@ void ToolHandler::initTools()
thickness[TOOL_SIZE_VERY_THICK] = 5.67;
t = new Tool("pen", TOOL_PEN, 0x3333CC,
TOOL_CAP_COLOR | TOOL_CAP_SIZE | TOOL_CAP_RULER | TOOL_CAP_RECTANGLE |
TOOL_CAP_CIRCLE | TOOL_CAP_ARROW | TOOL_CAP_RECOGNIZER | TOOL_CAP_FILL,
TOOL_CAP_CIRCLE | TOOL_CAP_ARROW | TOOL_CAP_RECOGNIZER | TOOL_CAP_FILL |
TOOL_CAP_DASH_LINE,
thickness);
tools[TOOL_PEN - TOOL_PEN] = t;
@ -62,7 +63,8 @@ void ToolHandler::initTools()
thickness[TOOL_SIZE_VERY_THICK] = 19.84;
t = new Tool("hilighter", TOOL_HILIGHTER, 0xFFFF00,
TOOL_CAP_COLOR | TOOL_CAP_SIZE | TOOL_CAP_RULER | TOOL_CAP_RECTANGLE |
TOOL_CAP_CIRCLE | TOOL_CAP_ARROW | TOOL_CAP_RECOGNIZER | TOOL_CAP_FILL,
TOOL_CAP_CIRCLE | TOOL_CAP_ARROW | TOOL_CAP_RECOGNIZER | TOOL_CAP_FILL |
TOOL_CAP_DASH_LINE,
thickness);
tools[TOOL_HILIGHTER - TOOL_PEN] = t;

@ -159,7 +159,7 @@ void StrokeHandler::onButtonReleaseEvent(const PositionInputData& pos)
}
}
if (stroke->getFill() != -1 || stroke->hasDashes())
if (stroke->getFill() != -1 || stroke->getLineStyle().hasDashes())
{
// The stroke is not filled on drawing time
// If the stroke has fill values, it needs to be re-rendered

@ -569,7 +569,7 @@ void LoadHandler::parseStroke()
const char* style = LoadHandlerHelper::getAttrib("style", true, this);
if (style != NULL)
{
StrokeStyle::parseStyle(stroke, style);
stroke->setLineStyle(StrokeStyle::parseStyle(style));
}
const char* tool = LoadHandlerHelper::getAttrib("tool", false, this);

@ -186,7 +186,7 @@ void SaveHandler::visitStrokeExtended(XmlPointNode* stroke, Stroke* s)
const double* dashes = NULL;
int dashCount = 0;
if (s->getDashes(dashes, dashCount))
if (s->getLineStyle().getDashes(dashes, dashCount))
{
stroke->setAttrib("style", StrokeStyle::formatStyle(dashes, dashCount));
}

@ -22,6 +22,7 @@ public:
XojFont();
virtual ~XojFont();
public:
string getName();
void setName(string name);

@ -0,0 +1,115 @@
#include "LineStyle.h"
#include <serializing/ObjectOutputStream.h>
#include <serializing/ObjectInputStream.h>
LineStyle::LineStyle()
{
XOJ_INIT_TYPE(LineStyle);
}
LineStyle::LineStyle(const LineStyle& other)
{
XOJ_INIT_TYPE(LineStyle);
*this = other;
}
LineStyle::~LineStyle()
{
XOJ_CHECK_TYPE(LineStyle);
g_free(this->dashes);
this->dashes = NULL;
this->dashCount = 0;
XOJ_RELEASE_TYPE(LineStyle);
}
void LineStyle::operator=(const LineStyle& other)
{
XOJ_CHECK_TYPE(LineStyle);
const double* dashes = NULL;
int dashCount = 0;
other.getDashes(dashes, dashCount);
setDashes(dashes, dashCount);
}
void LineStyle::serialize(ObjectOutputStream& out)
{
XOJ_CHECK_TYPE(LineStyle);
out.writeObject("LineStyle");
out.writeData(this->dashes, this->dashCount, sizeof(double));
out.endObject();
}
void LineStyle::readSerialized(ObjectInputStream& in)
{
XOJ_CHECK_TYPE(LineStyle);
in.readObject("LineStyle");
g_free(this->dashes);
this->dashes = NULL;
this->dashCount = 0;
in.readData((void**) &this->dashes, &this->dashCount);
in.endObject();
}
/**
* Get dash array and count
*
* @return true if dashed
*/
bool LineStyle::getDashes(const double*& dashes, int& dashCount) const
{
XOJ_CHECK_TYPE(LineStyle);
dashes = this->dashes;
dashCount = this->dashCount;
return this->dashCount > 0;
}
/**
* Set the dash array and count
*
* @param dashes Dash data, will be copied
* @param dashCount Count of entries
*/
void LineStyle::setDashes(const double* dashes, int dashCount)
{
XOJ_CHECK_TYPE(LineStyle);
g_free(this->dashes);
if (dashCount == 0 || dashes == NULL)
{
this->dashCount = 0;
this->dashes = NULL;
return;
}
this->dashes = (double*)g_malloc(dashCount * sizeof(double));
this->dashCount = dashCount;
memcpy(this->dashes, dashes, this->dashCount * sizeof(double));
}
/**
* Get dash array and count
*
* @return true if dashed
*/
bool LineStyle::hasDashes() const
{
XOJ_CHECK_TYPE(LineStyle);
return this->dashCount > 0;
}

@ -0,0 +1,66 @@
/*
* Xournal++
*
* Dash definition of a stroke
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/
#pragma once
#include <serializing/Serializeable.h>
#include <XournalType.h>
class LineStyle : public Serializeable
{
public:
LineStyle();
LineStyle(const LineStyle& other);
virtual ~LineStyle();
void operator=(const LineStyle& other);
public:
// Serialize interface
void serialize(ObjectOutputStream& out);
void readSerialized(ObjectInputStream& in);
public:
/**
* Get dash array and count
*
* @return true if dashed
*/
bool getDashes(const double*& dashes, int& dashCount) const;
/**
* @return true if dashed
*/
bool hasDashes() const;
/**
* Set the dash array and count
*
* @param dashes Dash data, will be copied
* @param dashCount Count of entries
*/
void setDashes(const double* dashes, int dashCount);
private:
XOJ_TYPE_ATTRIB;
/**
* Dash definition (NULL for no Dash)
*/
double* dashes = NULL;
/**
* Dash count (0 for no dash)
*/
int dashCount = 0;
};

@ -36,10 +36,6 @@ Stroke::~Stroke()
this->timestamp = 0;
g_free(this->dashes);
this->dashes = NULL;
this->dashCount = 0;
XOJ_RELEASE_TYPE(Stroke);
}
@ -54,14 +50,7 @@ void Stroke::applyStyleFrom(const Stroke* other)
setAudioFilename(other->getAudioFilename());
setTimestamp(other->getTimestamp());
setFill(other->getFill());
const double* dashes = NULL;
int dashCount = 0;
if (other->getDashes(dashes, dashCount))
{
setDashes(dashes, dashCount);
}
setLineStyle(other->getLineStyle());
}
Stroke* Stroke::cloneStroke() const
@ -105,7 +94,7 @@ void Stroke::serialize(ObjectOutputStream& out)
out.writeData(this->points, this->pointCount, sizeof(Point));
out.writeData(this->dashes, this->dashCount, sizeof(double));
this->lineStyle.serialize(out);
out.endObject();
}
@ -133,11 +122,7 @@ void Stroke::readSerialized(ObjectInputStream& in)
this->pointCount = 0;
in.readData((void**) &this->points, &this->pointCount);
g_free(this->dashes);
this->dashes = NULL;
this->dashCount = 0;
in.readData((void**) &this->dashes, &this->dashCount);
this->lineStyle.readSerialized(in);
in.endObject();
}
@ -208,54 +193,6 @@ double Stroke::getWidth() const
return this->width;
}
/**
* Get dash array and count
*
* @return true if dashed
*/
bool Stroke::getDashes(const double*& dashes, int& dashCount) const
{
XOJ_CHECK_TYPE(Stroke);
dashes = this->dashes;
dashCount = this->dashCount;
return this->dashCount > 0;
}
/**
* Set the dash array and count
*
* @param dashes Dash data, will be copied
* @param dashCount Count of entries
*/
void Stroke::setDashes(const double* dashes, int dashCount)
{
g_free(this->dashes);
if (dashCount == 0 || dashes == NULL)
{
this->dashCount = 0;
this->dashes = NULL;
return;
}
this->dashes = (double*)g_malloc(dashCount * sizeof(double));
this->dashCount = dashCount;
memcpy(this->dashes, dashes, this->dashCount * sizeof(double));
}
/**
* Get dash array and count
*
* @return true if dashed
*/
bool Stroke::hasDashes()
{
XOJ_CHECK_TYPE(Stroke);
return this->dashCount > 0;
}
bool Stroke::isInSelection(ShapeContainer* container)
{
XOJ_CHECK_TYPE(Stroke);
@ -414,6 +351,20 @@ StrokeTool Stroke::getToolType() const
return this->toolType;
}
void Stroke::setLineStyle(const LineStyle& style)
{
XOJ_CHECK_TYPE(Stroke);
this->lineStyle = style;
}
const LineStyle& Stroke::getLineStyle() const
{
XOJ_CHECK_TYPE(Stroke);
return this->lineStyle;
}
void Stroke::move(double dx, double dy)
{
XOJ_CHECK_TYPE(Stroke);

@ -13,6 +13,7 @@
#include "Element.h"
#include "Point.h"
#include "LineStyle.h"
#include <Arrayiterator.h>
@ -81,21 +82,8 @@ public:
void setToolType(StrokeTool type);
StrokeTool getToolType() const;
/**
* Get dash array and count
*
* @return true if dashed
*/
bool getDashes(const double*& dashes, int& dashCount) const;
bool hasDashes();
/**
* Set the dash array and count
*
* @param dashes Dash data, will be copied
* @param dashCount Count of entries
*/
void setDashes(const double* dashes, int dashCount);
const LineStyle& getLineStyle() const;
void setLineStyle(const LineStyle& style);
bool intersects(double x, double y, double halfSize, double* gap = NULL);
@ -143,8 +131,7 @@ private:
/**
* Dashed line
*/
double* dashes = NULL;
int dashCount = 0;
LineStyle lineStyle;
// Stroke timestamp, to match it to the audio stream
int timestamp;

@ -17,25 +17,21 @@ const double dotLinePattern[] = { 0.5, 3 };
#define PARSE_STYLE(name, def) \
if (strcmp(style, name) == 0) \
{ \
dashes = def; \
count = sizeof(def) / sizeof(def[0]); \
return true; \
LineStyle style; \
style.setDashes(def, sizeof(def) / sizeof(def[0])); \
return style; \
}
void StrokeStyle::parseStyle(Stroke* stroke, const char* style)
LineStyle StrokeStyle::parseStyle(const char* style)
{
const double* dashes = NULL;
int dashCount = 0;
if (StrokeStyle::parseStyle(style, dashes, dashCount))
{
stroke->setDashes(dashes, dashCount);
return;
}
PARSE_STYLE("dash", dashLinePattern);
PARSE_STYLE("dashdot", dashDotLinePattern);
PARSE_STYLE("dot", dotLinePattern);
if (strncmp("cust: ", style, 6) != 0)
{
return;
return LineStyle();
}
vector<double> dash;
@ -55,7 +51,7 @@ void StrokeStyle::parseStyle(Stroke* stroke, const char* style)
if (dash.size() == 0)
{
return;
return LineStyle();
}
double* dashesArr = new double[dash.size()];
@ -63,17 +59,12 @@ void StrokeStyle::parseStyle(Stroke* stroke, const char* style)
{
dashesArr[i] = dash[i];
}
stroke->setDashes(dashesArr, (int)dash.size());
delete[] dashes;
}
bool StrokeStyle::parseStyle(const char* style, const double*& dashes, int& count)
{
PARSE_STYLE("dash", dashLinePattern);
PARSE_STYLE("dashdot", dashDotLinePattern);
PARSE_STYLE("dot", dotLinePattern);
LineStyle ls;
ls.setDashes(dashesArr, (int)dash.size());
delete[] dashesArr;
return false;
return ls;
}
#define FORMAT_STYLE(name, def) \

@ -13,6 +13,8 @@
#include <XournalType.h>
#include "model/LineStyle.h"
class Stroke;
class StrokeStyle
@ -22,8 +24,7 @@ private:
virtual ~StrokeStyle();
public:
static void parseStyle(Stroke* stroke, const char* style);
static bool parseStyle(const char* style, const double*& dashes, int& count);
static LineStyle parseStyle(const char* style);
static string formatStyle(const double* dashes, int count);
public:

@ -262,6 +262,7 @@ XOJ_DECLARE_TYPE(FillUndoAction, 252);
XOJ_DECLARE_TYPE(FillUndoActionEntry, 253);
XOJ_DECLARE_TYPE(MoveLayerUndoAction, 254);
XOJ_DECLARE_TYPE(EmergencySaveRestore, 255);
XOJ_DECLARE_TYPE(LineStyle, 256);

@ -44,7 +44,7 @@ void StrokeView::applyDashed(double offset)
{
const double* dashes = NULL;
int dashCount = 0;
if (s->getDashes(dashes, dashCount))
if (s->getLineStyle().getDashes(dashes, dashCount))
{
cairo_set_dash(cr, dashes, dashCount, offset);
}

Loading…
Cancel
Save