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

Emit cellcolor and/or textcolor in X(HT)ML #503

Merged
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
25 changes: 14 additions & 11 deletions src/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ struct Cell {
bool IsParentOf(const Cell *c) { return c->parent == this || (c->parent && IsParentOf(c->parent)); }

uint SwapColor(uint c) { return ((c & 0xFF) << 16) | (c & 0xFF00) | ((c & 0xFF0000) >> 16); }
wxString ToText(int indent, const Selection &s, int format, Document *doc) {
wxString ToText(int indent, const Selection &s, int format, Document *doc, bool inheritstyle) {
wxString str = text.ToText(indent, s, format);
if (format == A_EXPHTMLT && ((text.stylebits & STYLE_UNDERLINE) || (text.stylebits & STYLE_STRIKETHRU))) {
wxString spanstyle = L"text-decoration:";
Expand All @@ -218,31 +218,31 @@ struct Cell {
str.Append(L"</span>");
}
if (format == A_EXPCSV) {
if (grid) return grid->ToText(indent, s, format, doc);
if (grid) return grid->ToText(indent, s, format, doc, inheritstyle);
str.Replace(L"\"", L"\"\"");
return L"\"" + str + L"\"";
}
if (s.cursor != s.cursorend) return str;
str.Append(L"\n");
if (grid) str.Append(grid->ToText(indent, s, format, doc));
if (grid) str.Append(grid->ToText(indent, s, format, doc, inheritstyle));
if (format == A_EXPXML) {
str.Prepend(L">");
if (text.relsize) {
str.Prepend(L"\"");
str.Prepend(wxString() << -text.relsize);
str.Prepend(L" relsize=\"");
}
if (text.stylebits) {
if (parent ? text.stylebits ^ parent->text.stylebits : text.stylebits) {
str.Prepend(L"\"");
str.Prepend(wxString() << text.stylebits);
str.Prepend(L" stylebits=\"");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also check for these stylebits

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok please see last commit. It XORs the stylebits from child and parent. Only if they differ, stylebits will be emitted in XML export.

}
if (cellcolor != doc->Background()) {
if (cellcolor != (parent ? parent->cellcolor : doc->Background())) {
str.Prepend(L"\"");
str.Prepend(wxString() << cellcolor);
str.Prepend(L" colorbg=\"");
}
if (textcolor != 0x000000) {
if (textcolor != (parent ? parent->textcolor : 0x000000)) {
str.Prepend(L"\"");
str.Prepend(wxString() << textcolor);
str.Prepend(L" colorfg=\"");
Expand All @@ -257,12 +257,15 @@ struct Cell {
str.Append(L"</cell>\n");
} else if (format == A_EXPHTMLT) {
wxString style;
if (text.stylebits & STYLE_BOLD) style += L"font-weight: bold;";
if (text.stylebits & STYLE_ITALIC) style += L"font-style: italic;";
if (text.stylebits & STYLE_FIXED) style += L"font-family: monospace;";
if (cellcolor != doc->Background())
if (!inheritstyle || !parent || (text.stylebits & STYLE_BOLD) != (parent->text.stylebits & STYLE_BOLD))
style += (text.stylebits & STYLE_BOLD) ? L"font-weight: bold;" : L"font-weight: normal;";
if (!inheritstyle || !parent || (text.stylebits & STYLE_ITALIC) != (parent->text.stylebits & STYLE_ITALIC))
style += (text.stylebits & STYLE_ITALIC) ? L"font-style: italic;" : L"font-style: normal;";
if (!inheritstyle || !parent || (text.stylebits & STYLE_FIXED) != (parent->text.stylebits & STYLE_FIXED))
style += (text.stylebits & STYLE_FIXED) ? L"font-family: monospace;" : L"font-family: sans-serif;";
if (!inheritstyle || cellcolor != (parent ? parent->cellcolor : doc->Background()))
style += wxString::Format(L"background-color: #%06X;", SwapColor(cellcolor));
if (textcolor != 0x000000)
if (!inheritstyle || textcolor != (parent ? parent->textcolor : 0x000000))
style += wxString::Format(L"color: #%06X;", SwapColor(textcolor));
str.Prepend(style.IsEmpty() ? L"<td>" : L"<td style=\"" + style + L"\">");
str.Append(L' ', indent);
Expand Down
11 changes: 5 additions & 6 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ struct Document {

auto CopyEntireCells(wxString &s) {
sys->clipboardcopy = s;
wxString html = selected.g->ConvertToText(selected, 0, A_EXPHTMLT, this);
wxString html = selected.g->ConvertToText(selected, 0, A_EXPHTMLT, this, false);
auto htmlobj =
#ifdef __WXGTK__
new wxCustomDataObject(wxDF_HTML);
Expand All @@ -438,7 +438,7 @@ struct Document {
dragdata.Add(new wxBitmapDataObject(bm));
}
} else {
wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this);
wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this, false);
dragdata.Add(new wxTextDataObject(s));
if (!selected.TextEdit()) {
auto htmlobj = CopyEntireCells(s);
Expand Down Expand Up @@ -476,7 +476,7 @@ struct Document {
}
} else {
wxDataObjectComposite *clipboarddata = new wxDataObjectComposite();
wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this);
wxString s = selected.g->ConvertToText(selected, 0, A_EXPTEXT, this, false);
clipboarddata->Add(new wxTextDataObject(s));
if (!selected.TextEdit()) {
auto htmlobj = CopyEntireCells(s);
Expand Down Expand Up @@ -859,7 +859,7 @@ struct Document {
return _(L"Error writing to file!");
}
wxTextOutputStream dos(fos);
wxString content = root->ToText(0, Selection(), k, this);
wxString content = root->ToText(0, Selection(), k, this, true);
switch (k) {
case A_EXPXML:
dos.WriteString(
Expand All @@ -879,8 +879,7 @@ struct Document {
L"<html>\n<head>\n<style>\n"
L"body { font-family: sans-serif; }\n"
L"table, th, td { border: 1px solid #A0A0A0; border-collapse: collapse;"
L" padding: 3px; vertical-align: top; font-weight: normal;"
L" font-style: normal; font-family: sans-serif; }\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this deleted?

Copy link
Collaborator Author

@tobiolo tobiolo Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I got it: Because if we do inheritance and we have parent and child cell share e.g. same font-style that is not the default one (e.g. bold instead of normal), then the code will not add that font-style again for the child as it shall be inherited (this is the goal). But because no font-style is supplied in the child's style attribute then but a default one is defined in the style section in the header, that one is prioritized over the inherited one. In order to avoid that situation, do not create this overriding styles in the header.

L" padding: 3px; vertical-align: top; }\n"
L"li { }\n</style>\n"
L"<title>export of TreeSheets file ");
dos.WriteString(filename);
Expand Down
8 changes: 4 additions & 4 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ struct Grid {
}
}

wxString ToText(int indent, const Selection &s, int format, Document *doc) {
return ConvertToText(SelectAll(), indent + 2, format, doc);
wxString ToText(int indent, const Selection &s, int format, Document *doc, bool inheritstyle) {
return ConvertToText(SelectAll(), indent + 2, format, doc, inheritstyle);
};

wxString ConvertToText(const Selection &s, int indent, int format, Document *doc) {
wxString ConvertToText(const Selection &s, int indent, int format, Document *doc, bool inheritstyle) {
wxString r;
const int root_grid_spacing = 2; // Can't be adjusted in editor, so use a default.
const int font_size = 14 - indent / 2;
Expand All @@ -616,7 +616,7 @@ struct Grid {
font_size).wc_str());
foreachcellinsel(c, s) {
if (x == 0) Formatter(r, format, indent, L"<row>\n", L"<tr>\n", L"");
r.Append(c->ToText(indent, s, format, doc));
r.Append(c->ToText(indent, s, format, doc, inheritstyle));
if (format == A_EXPCSV) r.Append(x == xs - 1 ? '\n' : ',');
if (x == xs - 1) Formatter(r, format, indent, L"</row>\n", L"</tr>\n", L"");
}
Expand Down