Skip to content

Commit

Permalink
Support symbol style
Browse files Browse the repository at this point in the history
In symbol description it is possible now to use style descriptors with
`=` symbol, separating key and value. In this commit line width is
supported: key is `w` and value is floating point line width, e.g.
`w=2.5` means line width of 2.5.
  • Loading branch information
enzet committed Aug 4, 2024
1 parent 4118116 commit 76b2ebd
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 17 deletions.
23 changes: 21 additions & 2 deletions include/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ enum class ElementDescriptor {
/* Sort parameters sequence separated by `;`. */
std::string sortParameters(std::string parameters);

/* Style of a symbol. */
class SymbolStyle {

public:
float lineWidth = 0.4f;
bool shiftByCurved;
bool curveDiagonal;

SymbolStyle(std::vector<std::string> description);
};

/*
* Symbol element.
*
Expand Down Expand Up @@ -69,6 +80,7 @@ class Element {
void add(ElementDescriptor elementDescriptor);
void draw(
Painter* painter,
SymbolStyle style,
Vector center,
float size,
Vector step,
Expand All @@ -77,6 +89,7 @@ class Element {
/* Draw symbol element. */
void draw(
Painter* painter,
SymbolStyle style,
Vector center,
float size,
std::vector<Element> elements);
Expand All @@ -90,11 +103,14 @@ class Element {
class Symbol {

std::vector<Element> elements;
void add(Element element);

public:
/* Construct symbol from the string representation. */
Symbol(std::vector<std::string> reprs);
void add(Element element);
void draw(Painter* painter, Vector center, float size);

/* Get graphical representation of the symbol. */
void draw(Painter* painter, SymbolStyle style, Vector center, float size);
};

/*
Expand Down Expand Up @@ -123,6 +139,9 @@ static std::unordered_map<char, ElementDescriptor> const descriptorMap
/* Convert graphical element text representation into element descriptor. */
ElementDescriptor getElementDescriptor(char elementRepr);

std::pair<Symbol, SymbolStyle>
parseSymbolParameters(std::vector<std::string> parameters);

/*
* Parse graphs file.
*
Expand Down
7 changes: 5 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ void drawTable(

void drawSymbol(std::vector<std::string> parameters) {
Painter* painter = new TikzPainter("out/ipa_table.tex");
Symbol symbol = Symbol(parameters);
symbol.draw(painter, Vector(0, 0), 0.1f);

std::pair<Symbol, SymbolStyle> pair = parseSymbolParameters(parameters);
Symbol symbol = pair.first;
SymbolStyle style = pair.second;
symbol.draw(painter, style, Vector(0, 0), 0.1f);
painter->end();
std::cout << painter->getString();
}
Expand Down
81 changes: 68 additions & 13 deletions src/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,34 @@ void Element::add(ElementDescriptor elementDescriptor) {
}
}

inline float parseFloat(std::string floatValue) {
return std::stof(floatValue);
}

inline bool parseBool(std::string boolValue) {
return boolValue == "+";
}

SymbolStyle::SymbolStyle(std::vector<std::string> descriptions) {
for (std::string description : descriptions) {

std::vector<std::string> keyValue = split(description, '=');
std::string key = keyValue[0];
std::string value = keyValue[1];

if (key == "w") {
lineWidth = parseFloat(value);
} else if (key == "sc") {
shiftByCurved = parseBool(value);
} else if (key == "cd") {
curveDiagonal = parseBool(value);
}
}
}

void Element::draw(
Painter* painter,
SymbolStyle style,
Vector center,
float size,
Vector step,
Expand All @@ -109,6 +135,9 @@ void Element::draw(
Vector a = getNorm();
Vector b = direction;

std::string tikzStyle
= "line cap=round, line width=" + std::to_string(style.lineWidth);

if (isCurved) {

float m = isInwards ? 1 : -1;
Expand All @@ -119,8 +148,7 @@ void Element::draw(
// Line.
Vector start = step + b * (1 - CURVE_SIZE);
Vector end = step - b * (1 - CURVE_SIZE);
painter->line(
center + start * size, center + end * size, "line cap=round");
painter->line(center + start * size, center + end * size, tikzStyle);

// Curve.
Vector p = step + b;
Expand All @@ -133,7 +161,7 @@ void Element::draw(
center + p2 * size,
center + p3 * size,
center + p4 * size,
"line cap=round");
tikzStyle);

// Curve.
p = step - b;
Expand All @@ -146,7 +174,7 @@ void Element::draw(
center + p2 * size,
center + p3 * size,
center + p4 * size,
"line cap=round");
tikzStyle);

} else if (isPointed) {

Expand Down Expand Up @@ -233,7 +261,7 @@ void Element::draw(
center + p2 * size,
center + p3 * size,
center + p4 * size,
"line cap=round");
tikzStyle);
}
}

Expand All @@ -242,18 +270,24 @@ void Element::draw(
*/
void Element::draw(
Painter* painter,
SymbolStyle style,
Vector center,
float size,
std::vector<Element> elements) {

if (this->isDouble and position == 0) {
draw(painter, center, size, getNorm() * 0.3f, elements);
draw(painter, center, size, getNorm() * -1 * 0.3f, elements);
draw(painter, style, center, size, getNorm() * 0.3f, elements);
draw(painter, style, center, size, getNorm() * -1 * 0.3f, elements);
} else {
draw(painter, center, size, getNorm() * 1.0f, elements);
draw(painter, style, center, size, getNorm() * 1.0f, elements);
if (this->isDouble) {
draw(
painter, center, size, getNorm() * (1 - DOUBLE_SIZE), elements);
painter,
style,
center,
size,
getNorm() * (1 - DOUBLE_SIZE),
elements);
}
}
}
Expand All @@ -277,10 +311,11 @@ void Symbol::add(Element element) {
elements.push_back(element);
}

void Symbol::draw(Painter* painter, Vector center, float size) {
void Symbol::draw(
Painter* painter, SymbolStyle style, Vector center, float size) {

for (Element element : elements) {
element.draw(painter, center, size, elements);
element.draw(painter, style, center, size, elements);
}
}

Expand Down Expand Up @@ -318,6 +353,24 @@ parseGraphs(const std::string& path) {
return graphs;
}

std::pair<Symbol, SymbolStyle>
parseSymbolParameters(std::vector<std::string> parameters) {

std::vector<std::string> symbolParameters;
std::vector<std::string> styleParameters;

for (std::string parameter : parameters) {
if (parameter.find('=') != std::string::npos) {
styleParameters.push_back(parameter);
} else {
symbolParameters.push_back(parameter);
}
}
std::pair<Symbol, SymbolStyle> pair {
Symbol(symbolParameters), SymbolStyle(styleParameters)};
return pair;
}

void drawTikz(
Painter* painter,
std::string ipaSymbol,
Expand All @@ -335,8 +388,10 @@ void drawTikz(
return;
}

Symbol symbol = Symbol(reprs);
symbol.draw(painter, center + Vector(0.5, 0), 0.1f);
std::pair<Symbol, SymbolStyle> pair = parseSymbolParameters(reprs);
Symbol symbol = pair.first;
SymbolStyle style = pair.second;
symbol.draw(painter, style, center + Vector(0.5, 0), 0.1f);
}

void IpaSymbols::add(std::string parameters, std::string ipaSymbol) {
Expand Down

0 comments on commit 76b2ebd

Please sign in to comment.