Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight construct groupings #135

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/forscape_symbol_lexical_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ void SymbolLexicalPass::resolveAssignment(ParseNode pn) alloc_except {
break;

case OP_IDENTIFIER:
resolveExpr(rhs);
resolveAssignmentId(pn);
resolveExpr(rhs);
break;

case OP_SUBSCRIPT_ACCESS:
if(lexical_map.find(parse_tree.getSelection(lhs)) != lexical_map.end()){
resolvePotentialIdSub(lhs);
resolveExpr(rhs);
resolveAssignmentId(pn);
resolveExpr(rhs);
}else{
parse_tree.setOp(pn, OP_REASSIGN);
resolveAssignmentSubscript(pn, lhs, rhs);
Expand Down
4 changes: 4 additions & 0 deletions src/typeset_construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ void Construct::paint(Painter& painter) const {
for(Subphrase* arg : args) arg->paint(painter);
}

void Construct::paintGrouping(Painter&) const {
// DO NOTHING
}

double Construct::height() const noexcept {
return above_center + under_center;
}
Expand Down
1 change: 1 addition & 0 deletions src/typeset_construct.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Construct {
virtual void updateSizeFromChildSizes() noexcept = 0;
void updateLayout() noexcept;
void paint(Painter& painter) const;
virtual void paintGrouping(Painter& painter) const;
double height() const noexcept;
double width DEBUG_INIT_STALE;
double above_center DEBUG_INIT_STALE;
Expand Down
3 changes: 3 additions & 0 deletions src/typeset_constructs/typeset_big_integral.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class BigIntegralSuper0 final : public Construct {
painter.drawSymbol(x, y, getBigIntegralString(type));
}

// EVENTUALLY: it would be nice to highlight groupings for integrals, e.g. in ∫ x ⅆx
// treat the '∫' like an opening symbol and ⅆx like a closing one.

static void modifyFirstScript(Construct* con, Controller& c, Subphrase*);

static const std::vector<Construct::ContextAction> actions;
Expand Down
5 changes: 5 additions & 0 deletions src/typeset_constructs/typeset_binomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ class Binomial final : public Construct {
painter.drawSymbol(')', x + width - paren_width, y, paren_width, height());
}

virtual void paintGrouping(Painter& painter) const override {
painter.drawHighlightedGrouping(x, y, paren_width, height(), "(");
painter.drawHighlightedGrouping(x + width - paren_width, y, paren_width, height(), ")");
}

virtual bool increasesScriptDepth(uint8_t) const noexcept override{
return !parent->isLine();
}
Expand Down
15 changes: 15 additions & 0 deletions src/typeset_constructs/typeset_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ class Matrix final : public Construct {
painter.drawLine(x+width-BRACKET_WIDTH-MATRIX_RPADDING, y+BRACKET_VSHIFT+h, BRACKET_WIDTH, 0);
}

virtual void paintGrouping(Painter& painter) const override {
double h = height();
constexpr double m = 2.0; // margin
painter.drawHighlightBox(x+MATRIX_LPADDING-m, y+BRACKET_VSHIFT-m, BRACKET_WIDTH+m, h+2*m);
painter.drawHighlightBox(x+width-BRACKET_WIDTH-MATRIX_RPADDING, y+BRACKET_VSHIFT-m, BRACKET_WIDTH+m, h+2*m);

painter.drawHighlightLine(x+MATRIX_LPADDING, y+BRACKET_VSHIFT, BRACKET_WIDTH, 0);
painter.drawHighlightLine(x+MATRIX_LPADDING, y+BRACKET_VSHIFT, 0, h);
painter.drawHighlightLine(x+MATRIX_LPADDING, y+BRACKET_VSHIFT+h, BRACKET_WIDTH, 0);

painter.drawHighlightLine(x+width-BRACKET_WIDTH-MATRIX_RPADDING, y+BRACKET_VSHIFT, BRACKET_WIDTH, 0);
painter.drawHighlightLine(x+width-MATRIX_RPADDING, y+BRACKET_VSHIFT, 0, h);
painter.drawHighlightLine(x+width-BRACKET_WIDTH-MATRIX_RPADDING, y+BRACKET_VSHIFT+h, BRACKET_WIDTH, 0);
}

static std::vector<ContextAction> actions;

virtual const std::vector<ContextAction>& getContextActions(Subphrase* child) const noexcept override {
Expand Down
7 changes: 7 additions & 0 deletions src/typeset_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ void Model::paintGroupings(Painter& painter, const Marker& loc) const {
Typeset::Marker left = close_lookup->second;
left.text->paintGrouping(painter, left.index);
}

if(loc.atTextEnd())
if(Construct* con = loc.text->nextConstructInPhrase())
con->paintGrouping(painter);
if(loc.atTextStart())
if(Construct* con = loc.text->prevConstructInPhrase())
con->paintGrouping(painter);
}

void Model::paintNumberCommas(Painter& painter, double xL, double yT, double xR, double yB, const Selection& sel) const {
Expand Down
4 changes: 4 additions & 0 deletions src/typeset_painter.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ class Painter {
void setScriptLevel(uint8_t depth);
void setOffset(double x, double y);
void drawText(double x, double y, std::string_view text);
void drawHighlightBox(double x, double y, double w, double h);
void drawHighlightLine(double x, double y, double w, double h);
void drawHighlightText(double x, double y, double w, double h, std::string_view text);
void drawHighlightedGrouping(double x, double y, double w, std::string_view text);
void drawHighlightedGrouping(double x, double y, double w, double h, std::string_view text);
void drawSymbol(double x, double y, std::string_view text);
void drawLine(double x, double y, double w, double h);
void drawDashedLine(double x, double y, double w, double h);
Expand Down
47 changes: 44 additions & 3 deletions src/typeset_painter_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <typeset_themes.h>
#include <typeset_view.h>
#include <forscape_unicode.h>
#include <qt_compatability.h>

#ifndef NDEBUG
#include <iostream>
Expand Down Expand Up @@ -142,14 +143,33 @@ void Painter::drawText(double x, double y, std::string_view text){
painter.drawText(QPointF(x, y), q_str);
}

void Painter::drawHighlightedGrouping(double x, double y, double w, std::string_view text){
if(x >= xR || y >= yB || (y + CAPHEIGHT[depth]) <= yT || x + CHARACTER_WIDTHS[depth] < xL) return;
void Painter::drawHighlightBox(double x, double y, double w, double h) {
if(x >= xR || y >= yB || (y + h) <= yT || x + w < xL) return;

x += x_offset;

painter.setPen(getColour(COLOUR_GROUPINGBACKGROUND));
painter.setBrush(getColour(COLOUR_GROUPINGBACKGROUND));
painter.drawRect(x, y, w, ASCENT[depth]);
painter.drawRect(x, y, w, h);

painter.setPen(getColour(COLOUR_CURSOR));
painter.setBrush(getColour(COLOUR_CURSOR));
}

void Painter::drawHighlightLine(double x, double y, double w, double h) {
if(x >= xR || y >= yB || (y + h) <= yT || x + w < xL) return;

painter.setPen(getColour(COLOUR_GROUPINGHIGHLIGHT));
drawLine(x, y, w, h);
painter.setPen(getColour(COLOUR_CURSOR));
painter.setBrush(getColour(COLOUR_CURSOR));
}

void Painter::drawHighlightedGrouping(double x, double y, double w, std::string_view text){
if(x >= xR || y >= yB || (y + CAPHEIGHT[depth]) <= yT || x + CHARACTER_WIDTHS[depth] < xL) return;
drawHighlightBox(x, y, w, ASCENT[depth]);

x += x_offset;

painter.setPen(getColour(COLOUR_GROUPINGHIGHLIGHT));
y += CAPHEIGHT[depth];
Expand All @@ -159,6 +179,27 @@ void Painter::drawHighlightedGrouping(double x, double y, double w, std::string_
painter.setBrush(getColour(COLOUR_CURSOR));
}

void Painter::drawHighlightedGrouping(double x, double y, double w, double h, std::string_view text) {
if(x >= xR || y >= yB || (y + h) <= yT || x + w < xL) return;
drawHighlightBox(x, y, w, h * (1 + static_cast<double>(DESCENT[depth])/ASCENT[depth]));

x += x_offset;
painter.setPen(getColour(COLOUR_GROUPINGHIGHLIGHT));

double scale_x = w / CHARACTER_WIDTHS[depth];
double scale_y = h / ASCENT[depth];
x /= scale_x;
y = (y + h - DESCENT[depth]) / scale_y;

QTransform transform = painter.transform();
painter.scale(scale_x, scale_y);
painter.drawText(QPointF(x, y), toQString(text));
painter.setTransform(transform);

painter.setPen(getColour(COLOUR_CURSOR));
painter.setBrush(getColour(COLOUR_CURSOR));
}

void Painter::drawSymbol(double x, double y, std::string_view text){
if(x >= xR ||
y >= yB ||
Expand Down