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

[7_2] Fix the memory issue of the change_log #1647

Merged
merged 7 commits into from
Feb 9, 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
13 changes: 4 additions & 9 deletions TeXmacs/tests/tm/7_2_1.tm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<TeXmacs|2.1.2>

<style|<tuple|generic|no-page-numbers|british>>
<style|<tuple|generic|no-page-numbers|chinese>>

<\body>
\ ### \<#58A8\>\<#5E72\>V1.2.3
Expand All @@ -16,27 +16,22 @@
<associate|global-subject|>
<associate|global-title|v1.2.3.tm>
<associate|page-medium|paper>
<associate|page-screen-margin|false>
<associate|page-screen-margin|true>
</collection>
</initial>

<\references>
<\collection>
<associate|auto-1|<tuple|1|1>>
<associate|auto-2|<tuple|2|1>>
</collection>
</references>

<\auxiliary>
<\collection>
<\associate|toc>
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc>OSPP:\<#53EF\>\<#7F16\>\<#8F91\>PDF>
<datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-1><vspace|0.5fn>

<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|2<space|2spc>OSPP:
<vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc>OSPP:
Mogan Draw on WASM> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
<no-break><pageref|auto-2><vspace|0.5fn>
<no-break><pageref|auto-1><vspace|0.5fn>
</associate>
</collection>
</auxiliary>
25 changes: 25 additions & 0 deletions TeXmacs/tests/tm/7_2_2.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<TeXmacs|2.1.2>

<style|<tuple|beamer|no-page-numbers|chinese>>

<\body>
<screens|<\shown>
<tit|Title 1>

\;
</shown>|<\hidden>
\;

\;
</hidden>>
</body>

<\initial>
<\collection>
<associate|global-author|>
<associate|global-subject|>
<associate|global-title|v1.2.3.tm>
<associate|page-medium|beamer>
<associate|page-screen-margin|true>
</collection>
</initial>
5 changes: 5 additions & 0 deletions src/Kernel/Types/rectangles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ area (rectangle r) {
return w * h;
}

bool
is_zero (rectangle r) {
return (r->x1 == 0) && (r->x2 == 0) && (r->y1 == 0) && (r->y2 == 0);
}

/******************************************************************************
* Miscellaneous subroutines
******************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions src/Kernel/Types/rectangles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rectangle operator/ (rectangle r, double x);
rectangle thicken (rectangle r, SI width, SI height);
rectangle least_upper_bound (rectangle r1, rectangle r2);
double area (rectangle r);
bool is_zero (rectangle r);

typedef list<rectangle> rectangles;

Expand Down
5 changes: 3 additions & 2 deletions src/Typeset/Boxes/Basic/boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,12 +934,13 @@ box_rep::display_links (renderer ren) {
}

void
box_rep::position_at (SI x, SI y, array<rectangle>& change_log) {
box_rep::position_at (SI x, SI y, array<rectangle>& change_log,
std::shared_ptr<rectangle> changed) {
int i, n= subnr ();
x+= x0;
y+= y0;
for (i= 0; i < n; i++)
subbox (i)->position_at (x, y, change_log);
subbox (i)->position_at (x, y, change_log, changed);
}

void
Expand Down
29 changes: 18 additions & 11 deletions src/Typeset/Boxes/Composite/concat_boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,30 +624,37 @@ concat_box_rep::graphical_select (SI x1, SI y1, SI x2, SI y2) {

class phrase_box_rep : public concat_box_rep {
public:
array<rectangle>* logs_ptr;
SI ox, oy;
std::shared_ptr<rectangle> changed_ptr;
SI ox, oy;
phrase_box_rep (path ip, array<box> bs, array<SI> spc);
~phrase_box_rep ();
void position_at (SI x, SI y, array<rectangle>& change_log_ptr);
void position_at (SI x, SI y, array<rectangle>& logs,
std::shared_ptr<rectangle> changed);
void display (renderer ren);
};

phrase_box_rep::phrase_box_rep (path ip, array<box> bs, array<SI> spc)
: concat_box_rep (ip, bs, spc, false), logs_ptr (NULL) {}
: concat_box_rep (ip, bs, spc, false), changed_ptr (nullptr) {}

phrase_box_rep::~phrase_box_rep () {
if (logs_ptr != NULL) {
array<rectangle>& logs= *logs_ptr;
logs << rectangle (0, 0, 0, 0)
<< rectangle (ox + x3, oy + y3, ox + x4, oy + y4);
if (changed_ptr != nullptr) {
rectangle& changed_rect= *changed_ptr;
rectangle this_rect = rectangle (ox + x3, oy + y3, ox + x4, oy + y4);
if (!is_zero (changed_rect)) {
changed_rect= least_upper_bound (changed_rect, this_rect);
}
else {
changed_rect= this_rect;
}
}
}

void
phrase_box_rep::position_at (SI x, SI y, array<rectangle>& logs) {
phrase_box_rep::position_at (SI x, SI y, array<rectangle>& logs,
std::shared_ptr<rectangle> changed) {
x+= x0;
y+= y0;
if (logs_ptr == NULL) {
if (changed_ptr == nullptr) {
logs << rectangle (0, 0, 0, 0);
}
else {
Expand All @@ -656,7 +663,7 @@ phrase_box_rep::position_at (SI x, SI y, array<rectangle>& logs) {
ox= x;
oy= y;
logs << rectangle (ox + x3, oy + y3, ox + x4, oy + y4);
logs_ptr= &logs;
changed_ptr= changed;
}

void
Expand Down
40 changes: 24 additions & 16 deletions src/Typeset/Boxes/Modifier/change_boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,34 +708,42 @@ cell_box_rep::post_display (renderer& ren) {

class remember_box_rep : public change_box_rep {
public:
rectangles* logs_ptr;
SI ox, oy;
std::shared_ptr<rectangle> changed_ptr;
SI ox, oy;

public:
inline remember_box_rep (path ip, box b)
: change_box_rep (ip, true), logs_ptr (NULL) {
: change_box_rep (ip, true), changed_ptr (NULL) {
insert (b, 0, 0);
position ();
finalize ();
}
inline ~remember_box_rep () {
if (logs_ptr != NULL) {
rectangles& logs= *logs_ptr;
logs= rectangles (rectangle (ox + x3, oy + y3, ox + x4, oy + y4), logs);
logs= rectangles (rectangle (0, 0, 0, 0), logs);
// cout << " 8=X " << rectangle (ox+x3, oy+y3, ox+x4, oy+y4) << "\n";
if (changed_ptr != nullptr) {
rectangle& changed_rect= *changed_ptr;
rectangle this_rect = rectangle (ox + x3, oy + y3, ox + x4, oy + y4);
if (!is_zero (changed_rect)) {
changed_rect= least_upper_bound (changed_rect, this_rect);
}
else {
changed_rect= this_rect;
}
}
}
inline void position_at (SI x, SI y, rectangles& logs) {
inline void position_at (SI x, SI y, array<rectangle>& logs,
std::shared_ptr<rectangle> changed) {
x+= x0;
y+= y0;
if (logs_ptr == NULL) logs= rectangles (rectangle (0, 0, 0, 0), logs);
else
logs= rectangles (rectangle (ox + x3, oy + y3, ox + x4, oy + y4), logs);
ox = x;
oy = y;
logs = rectangles (rectangle (ox + x3, oy + y3, ox + x4, oy + y4), logs);
logs_ptr= &logs;
if (changed_ptr == nullptr) {
logs << rectangle (0, 0, 0, 0);
}
else {
logs << rectangle (ox + x3, oy + y3, ox + x4, oy + y4);
}
ox= x;
oy= y;
logs << rectangle (ox + x3, oy + y3, ox + x4, oy + y4);
changed_ptr= changed;
}
inline void display (renderer ren) { ren->apply_shadow (x1, y1, x2, y2); }
};
Expand Down
12 changes: 8 additions & 4 deletions src/Typeset/Bridge/impl_typesetter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#ifndef IMPL_TYPESETTER_H
#define IMPL_TYPESETTER_H
#include "Bridge/bridge.hpp"
#include "rectangles.hpp"
#include <memory>

class typesetter_rep {
public:
edit_env& env;
bridge br;
array<rectangle> change_log;
array<brush> old_bgs;
edit_env& env;
bridge br;
array<brush> old_bgs;

array<page_item> l; // current lines
stack_border sb; // border properties
Expand All @@ -29,6 +30,9 @@ class typesetter_rep {
hashmap<string, tree> old_patch;
bool paper;

private:
std::shared_ptr<rectangle> changed_ptr= std::make_shared<rectangle> ();

public:
typesetter_rep (edit_env& env, tree et, path ip);

Expand Down
37 changes: 26 additions & 11 deletions src/Typeset/Bridge/typesetter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,31 @@ typesetter_rep::typeset () {

box
typesetter_rep::typeset (SI& x1b, SI& y1b, SI& x2b, SI& y2b) {
x1 = x1b;
y1 = y1b;
x2 = x2b;
y2 = y2b;
x1= x1b;
y1= y1b;
x2= x2b;
y2= y2b;

// For old destroyed boxes, the changed_least_upper_bound will be updated
box b= typeset ();

// append pairs of rectangles to the change_log from the typesetting box
b->position_at (0, 0, change_log);
// Append pairs of rectangles to the change_log from the new typesetting box
array<rectangle> change_log;
b->position_at (0, 0, change_log, changed_ptr);

// Keeps the box rectangle in pair <before, after>
// if before is zero rect, we only need to rendered the after rect
// if after is zero rect, we only need to rendered the before rect
// if before != after, both of them need to be re-rendered
change_log= requires_update (change_log);

// Append the least upper bound to the change_log from the destroyed old boxes
if (!is_zero (*changed_ptr)) {
change_log << *changed_ptr;
}
// Reset the least upper bound for the old destroyed boxes
(*changed_ptr)= rectangle (0, 0, 0, 0);

rectangle r (0, 0, 0, 0);
if (N (change_log) != 0) r= least_upper_bound (change_log);

Expand All @@ -195,11 +211,10 @@ typesetter_rep::typeset (SI& x1b, SI& y1b, SI& x2b, SI& y2b) {
if (new_bgs[i] != old_bgs[i]) r= least_upper_bound (r, rs[i]);
old_bgs= new_bgs;

x1b = r->x1;
y1b = r->y1;
x2b = r->x2;
y2b = r->y2;
change_log= array<rectangle> ();
x1b= r->x1;
y1b= r->y1;
x2b= r->x2;
y2b= r->y2;
return b;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Typeset/boxes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "rectangles.hpp"
#include "renderer.hpp"
#include "tm_timer.hpp"
#include <memory>

#define STD_BOX 0
#define STACK_BOX 1
Expand Down Expand Up @@ -145,7 +146,8 @@ class box_rep : public abstract_struct {
virtual tree message (tree t, SI x, SI y, rectangles& rs);
virtual void loci (SI x, SI y, SI d, list<string>& ids, rectangles& rs);
virtual void display_links (renderer ren);
virtual void position_at (SI x, SI y, array<rectangle>& change_log);
virtual void position_at (SI x, SI y, array<rectangle>& logs,
std::shared_ptr<rectangle> changed);
virtual void collect_page_numbers (hashmap<string, tree>& h, tree page);
virtual void collect_page_colors (array<brush>& bs, array<rectangle>& rs);
virtual path find_tag (string name);
Expand Down
Loading