Commit d3b19a0b authored by twanvl's avatar twanvl

Slightly nicer syntax highlighting

parent 368d5150
......@@ -95,7 +95,7 @@ void KeywordReminderTextValue::highlight(const String& code, const vector<Script
// becomes:
// bla <code>{<code-kw>if</code-kw> code "<code-string>x</code-string>" } bla
String new_value;
int in_brace = 0;
vector<int> in_brace; // types of braces we are in, 0 for code brace, 1 for string escapes
bool in_string = true;
vector<ScriptParseError>::const_iterator error = errors.begin();
for (size_t pos = 0 ; pos < code.size() ; ) {
......@@ -120,26 +120,30 @@ void KeywordReminderTextValue::highlight(const String& code, const vector<Script
new_value += _('\1'); // escape
++pos;
} else if (c == _('{')) {
in_brace++;
if (in_brace == 1) new_value += _("<code>");
if (in_string) in_string = false;
if (in_string) {
new_value += _("<code>");
in_brace.push_back(1);
in_string = false;
} else {
in_brace.push_back(0);
}
new_value += c;
++pos;
} else if (c == _('}') && !in_string) {
new_value += c;
in_brace--;
if (in_brace == 0) {
if (!in_brace.empty() && in_brace.back() == 1) {
new_value += _("</code>");
in_string = true;
}
if (!in_brace.empty()) in_brace.pop_back();
++pos;
} else if (c == _('"')) {
if (in_string) {
in_string = false;
new_value += _("\"<code-str>");
new_value += _("\"</code-str><code>");
} else {
in_string = true;
new_value += _("<code-str>\"");
new_value += _("</code><code-str>\"");
}
++pos;
} else if (c == _('\\') && in_string && pos + 1 < code.size()) {
......
......@@ -30,5 +30,11 @@ class ExportTemplate : public Packaged {
DECLARE_REFLECTION();
};
// ----------------------------------------------------------------------------- : ExportPackage
/// A package that is being written to when exporting
class ExportingPackage : public Package {
};
// ----------------------------------------------------------------------------- : EOF
#endif
......@@ -14,11 +14,10 @@ Font::Font()
: name()
, size(1)
, underline(false)
, weight_i(wxFONTWEIGHT_NORMAL), style_i(wxFONTSTYLE_NORMAL)
, scale_down_to(100000)
, shadow_displacement(0,0)
, separator_color(128,128,128)
, type(NORMAL)
, flags(FONT_NORMAL)
{}
bool Font::update(Context& ctx) {
......@@ -31,8 +30,9 @@ bool Font::update(Context& ctx) {
| underline .update(ctx)
| color .update(ctx)
| shadow_color.update(ctx);
weight_i = weight() == _("bold") ? wxBOLD : wxNORMAL;
style_i = style() == _("italic") ? wxITALIC : wxNORMAL;
flags = flags & (~FONT_BOLD & ~FONT_ITALIC)
| (weight() == _("bold") ? FONT_BOLD : FONT_NORMAL)
| (style() == _("italic") ? FONT_ITALIC : FONT_NORMAL);
return changes;
}
void Font::initDependencies(Context& ctx, const Dependency& dep) const {
......@@ -46,15 +46,20 @@ void Font::initDependencies(Context& ctx, const Dependency& dep) const {
shadow_color.initDependencies(ctx, dep);
}
FontP Font::make(bool bold, bool italic, bool placeholder_color, bool code_color, Color* other_color) const {
FontP Font::make(int add_flags, Color* other_color) const {
FontP f(new Font(*this));
if (bold) f->weight_i = wxFONTWEIGHT_BOLD;
if (italic) f->style_i = wxFONTSTYLE_ITALIC;
if (code_color) {
f->flags |= add_flags;
if (add_flags & FONT_CODE_STRING) {
f->color = Color(0,0,100);
}
if (add_flags & FONT_CODE) {
f->color = Color(128,0,0);
f->type = TYPEWRITER;
}
if (placeholder_color) {
if (add_flags & FONT_CODE_KW) {
f->color = Color(158,0,0);
f->flags |= FONT_BOLD;
}
if (add_flags & FONT_SOFT) {
f->color = f->separator_color;
f->shadow_displacement = RealSize(0,0); // no shadow
}
......@@ -66,13 +71,16 @@ FontP Font::make(bool bold, bool italic, bool placeholder_color, bool code_color
wxFont Font::toWxFont(double scale) const {
int size_i = scale * size;
if (name().empty()) {
int weight_i = flags & FONT_BOLD ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL;
int style_i = flags & FONT_ITALIC ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
if (flags & FONT_CODE) {
if (size_i < 2) size_i = wxNORMAL_FONT->GetPointSize();
return wxFont(size_i, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, weight_i, underline(), _("Courier New"));
} else if (name().empty()) {
wxFont font = *wxNORMAL_FONT;
font.SetPointSize(size > 1 ? size_i : scale * font.GetPointSize());
return font;
} else if (type == TYPEWRITER) {
return wxFont(size_i, wxFONTFAMILY_TELETYPE, weight_i, underline(), _("Courier New"));
} else if (style_i == wxFONTSTYLE_ITALIC && !italic_name().empty()) {
} else if (flags & FONT_ITALIC && !italic_name().empty()) {
return wxFont(size_i, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, weight_i, underline(), italic_name());
} else {
return wxFont(size_i, wxFONTFAMILY_DEFAULT, style_i, weight_i, underline(), name());
......
......@@ -17,6 +17,18 @@ DECLARE_POINTER_TYPE(Font);
// ----------------------------------------------------------------------------- : Font
enum FontFlags
{ FONT_NORMAL = 0
, FONT_BOLD = 0x01
, FONT_ITALIC = 0x02
, FONT_SOFT = 0x04
, FONT_CODE = 0x08
, FONT_CODE_KW = 0x10 // syntax highlighting
, FONT_CODE_STRING = 0x20 // syntax highlighting
, FONT_CODE_NUMBER = 0x40 // syntax highlighting
, FONT_CODE_OPER = 0x80 // syntax highlighting
};
/// A font for rendering text
/** Contains additional information about scaling, color and shadow */
class Font : public IntrusivePtrBase<Font> {
......@@ -26,16 +38,12 @@ class Font : public IntrusivePtrBase<Font> {
Scriptable<double> size; ///< Size of the font
Scriptable<String> weight, style; ///< Weight and style of the font (bold/italic)
Scriptable<bool> underline; ///< Underlined?
int weight_i, style_i; ///< wx constants for weight and style
double scale_down_to; ///< Smallest size to scale down to
Scriptable<Color> color; ///< Color to use
Scriptable<Color> shadow_color; ///< Color for shadow
RealSize shadow_displacement; ///< Position of the shadow
Color separator_color; ///< Color for <sep> text
enum {
NORMAL,
TYPEWRITER, ///< Use a typewriter font
} type;
int flags; ///< FontFlags for this font
Font();
......@@ -47,8 +55,8 @@ class Font : public IntrusivePtrBase<Font> {
/// Does this font have a shadow?
inline bool hasShadow() { return shadow_displacement.width != 0 || shadow_displacement.height != 0; }
/// Make a bold/italic/placeholder version of this font
FontP make(bool bold, bool italic, bool placeholder_color, bool code_color, Color* other_color) const;
/// Add style to a font, and optionally change the color
FontP make(int add_flags, Color* other_color) const;
/// Convert this font to a wxFont
wxFont toWxFont(double scale) const;
......
......@@ -8,6 +8,7 @@
#include <gui/html_export_window.hpp>
#include <gui/control/package_list.hpp>
#include <data/export_template.hpp>
#include <util/error.hpp>
// ----------------------------------------------------------------------------- : HtmlExportWindow
......
......@@ -10,7 +10,6 @@
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <data/export_template.hpp>
class PackageList;
DECLARE_POINTER_TYPE(Set);
......
......@@ -160,7 +160,14 @@ struct TextElementsFromString {
e = new SymbolTextElement(text, pos, pos + 1, style.symbol_font, &ctx);
bracket = false;
} else {
FontP font = style.font.make(bold > 0, italic > 0, soft > 0 || kwpph > 0, code > 0,
FontP font = style.font.make(
(bold > 0 ? FONT_BOLD : FONT_NORMAL) |
(italic > 0 ? FONT_ITALIC : FONT_NORMAL) |
(soft > 0 ? FONT_SOFT : FONT_NORMAL) |
(kwpph > 0 ? FONT_SOFT : FONT_NORMAL) |
(code > 0 ? FONT_CODE : FONT_NORMAL) |
(code_kw > 0 ? FONT_CODE_KW : FONT_NORMAL) |
(code_string > 0 ? FONT_CODE_STRING : FONT_NORMAL),
param > 0 || param_ref > 0
? &param_colors[(param_id++) % param_colors_count]
: nullptr);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment