Skip to content

Commit

Permalink
graphics: Render fractions graphically
Browse files Browse the repository at this point in the history
Render fractions using a grob

Fixes: #47

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
  • Loading branch information
c3d committed Feb 21, 2024
1 parent c8fceb9 commit ce709f4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/fraction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
// ****************************************************************************

#include "fraction.h"

#include "algebraic.h"
#include "grob.h"


RECORDER(fraction, 16, "Fractions");

Expand Down Expand Up @@ -88,6 +91,43 @@ EVAL_BODY(fraction)
}


GRAPH_BODY(fraction)
// ----------------------------------------------------------------------------
// Render a fraction in graphical mode
// ----------------------------------------------------------------------------
{
fraction_g obj = o;
// save<settings::font_id> save(g.font, settings::smaller_font(g.font));

// Render numerator and denominator
bignum_g num = obj->numerator();
bignum_g den = obj->denominator();
grob_g numg = num->graph(g);
grob_g deng = den->graph(g);

using pixsize = grob::pixsize;
pixsize nw = numg->width();
pixsize nh = numg->height();
pixsize dw = deng->width();
pixsize dh = deng->height();

pixsize gw = nw > dw ? nw : dw;
pixsize gh = nh + dh + 4;

grob_g result = grob::make(gw, gh);

surface ns = numg->pixels();
surface ds = deng->pixels();
surface rs = result->pixels();

rs.fill(0, 0, gw, gh, g.background);
rs.copy(ns, point((gw - nw)/2, 0));
rs.copy(ds, point((gw - dw)/2, nh + 4));
rs.fill(0, nh+1, gw, nh+2);
return result;
}


fraction_g fraction::make(integer_g n, integer_g d)
// ----------------------------------------------------------------------------
// Create a reduced fraction from n and d
Expand Down
1 change: 1 addition & 0 deletions src/fraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ struct fraction : algebraic
HELP_DECL(fraction);
EVAL_DECL(fraction);
RENDER_DECL(fraction);
GRAPH_DECL(fraction);
PREC_DECL(MULTIPLICATIVE);
};

Expand Down
1 change: 1 addition & 0 deletions src/grob.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ struct grapher
background(bg),
flat(flat)
{}
grapher(const grapher &other) = default;

size maxw;
size maxh;
Expand Down
24 changes: 24 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ struct settings
NUM_FONTS
};


static font_id smaller_font(font_id f)
// ------------------------------------------------------------------------
// Find the next smaller font
// ------------------------------------------------------------------------
{
switch (f)
{
default:
case HELP:
case STACK: return HELP;
case EDITOR: return STACK;
case LIB17:
case LIB18: return LIB17;
case LIB20: return LIB18;
case LIB22: return LIB20;
case LIB25: return LIB22;
case LIB28: return LIB25;
case SKR18:
case SKR24: return SKR18;
case FREE42: return FREE42;
}
}

#define ID(i) static const object::id ID_##i = object::ID_##i;
#include "ids.tbl"

Expand Down

0 comments on commit ce709f4

Please sign in to comment.