From 3b1574a1607fead0b1118a93e0cbb3ae4e0b0c3d Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sun, 12 May 2019 13:26:09 +0300 Subject: [PATCH 1/3] Extract a common expression into a variable/const. This is Refactoring / code cleanup. See: * https://www.refactoring.com/ * https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/ Some small optimisations may have slipped in as well. --- src/extension/internal/wmf-inout.cpp | 127 ++++++++++++++------------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/src/extension/internal/wmf-inout.cpp b/src/extension/internal/wmf-inout.cpp index 4db33f5503..048804f8cd 100644 --- a/src/extension/internal/wmf-inout.cpp +++ b/src/extension/internal/wmf-inout.cpp @@ -238,6 +238,7 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat sprintf(tmpcolor,"%6.6X",sethexcolor(hatchColor)); break; } + auto & defs = d->defs; /* For both bkMode types set the PATH + FOREGROUND COLOR for the indicated standard hatch. This will be used late to compose, or recompose the transparent or opaque final hatch.*/ @@ -249,54 +250,54 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); } d->hatches.strings[d->hatches.count++]=strdup(hpathname); - d->defs += "\n"; + defs += "\n"; switch(hatchType){ case U_HS_HORIZONTAL: - d->defs += " defs += hpathname; - d->defs += "\" d=\"M 0 0 6 0\" style=\"fill:none;stroke:#"; - d->defs += tmpcolor; - d->defs += "\" />\n"; + defs += " \n"; break; case U_HS_VERTICAL: - d->defs += " defs += hpathname; - d->defs += "\" d=\"M 0 0 0 6\" style=\"fill:none;stroke:#"; - d->defs += tmpcolor; - d->defs += "\" />\n"; + defs += " \n"; break; case U_HS_FDIAGONAL: - d->defs += " defs += hpathname; - d->defs += "\" x1=\"-1\" y1=\"-1\" x2=\"7\" y2=\"7\" stroke=\"#"; - d->defs += tmpcolor; - d->defs += "\"/>\n"; + defs += " \n"; break; case U_HS_BDIAGONAL: - d->defs += " defs += hpathname; - d->defs += "\" x1=\"-1\" y1=\"7\" x2=\"7\" y2=\"-1\" stroke=\"#"; - d->defs += tmpcolor; - d->defs += "\"/>\n"; + defs += " \n"; break; case U_HS_CROSS: - d->defs += " defs += hpathname; - d->defs += "\" d=\"M 0 0 6 0 M 0 0 0 6\" style=\"fill:none;stroke:#"; - d->defs += tmpcolor; - d->defs += "\" />\n"; + defs += " \n"; break; case U_HS_DIAGCROSS: - d->defs += " defs += hpathname; - d->defs += "\" x1=\"-1\" y1=\"-1\" x2=\"7\" y2=\"7\" stroke=\"#"; - d->defs += tmpcolor; - d->defs += "\"/>\n"; - d->defs += " defs += hpathname; - d->defs += "\" x1=\"-1\" y1=\"7\" x2=\"7\" y2=\"-1\" stroke=\"#"; - d->defs += tmpcolor; - d->defs += "\"/>\n"; + defs += " \n"; + defs += " \n"; break; case U_HS_SOLIDCLR: case U_HS_DITHEREDCLR: @@ -305,12 +306,12 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat case U_HS_SOLIDBKCLR: case U_HS_DITHEREDBKCLR: default: - d->defs += " defs += hpathname; - d->defs += "\" d=\"M 0 0 6 0 6 6 0 6 z\" style=\"fill:#"; - d->defs += tmpcolor; - d->defs += ";stroke:none"; - d->defs += "\" />\n"; + defs += " \n"; break; } } @@ -372,12 +373,12 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat if(!idx){ // add it if not already present if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); } d->hatches.strings[d->hatches.count++]=strdup(hatchname); - d->defs += "\n"; - d->defs += " defs += hatchname; - d->defs += "\" xlink:href=\"#WMFhbasepattern\">\n"; - d->defs += refpath; - d->defs += " \n"; + defs += "\n"; + defs += " \n"; + defs += refpath; + defs += " \n"; idx = d->hatches.count; } } @@ -390,12 +391,12 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); } d->hatches.strings[d->hatches.count++]=strdup(hbkname); - d->defs += "\n"; - d->defs += " defs += hbkname; - d->defs += "\" x=\"0\" y=\"0\" width=\"6\" height=\"6\" fill=\"#"; - d->defs += bkcolor; - d->defs += "\" />\n"; + defs += "\n"; + defs += " \n"; } // this is the pattern, its name will show up in Inkscape's pattern selector @@ -404,15 +405,15 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat if(!idx){ // add it if not already present if(d->hatches.count == d->hatches.size){ enlarge_hatches(d); } d->hatches.strings[d->hatches.count++]=strdup(hatchname); - d->defs += "\n"; - d->defs += " defs += hatchname; - d->defs += "\" xlink:href=\"#WMFhbasepattern\">\n"; - d->defs += " defs += hbkname; - d->defs += "\" />\n"; - d->defs += refpath; - d->defs += " \n"; + defs += "\n"; + defs += " \n"; + defs += " \n"; + defs += refpath; + defs += " \n"; idx = d->hatches.count; } } -- GitLab From bf58af620a39804503faf941f3c6935d1ea778dd Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sun, 12 May 2019 14:25:00 +0300 Subject: [PATCH 2/3] Extract the TagEmitter.append method. This is Refactoring / code cleanup. See: * https://refactoring.com/catalog/extractMethod.html * https://en.wikipedia.org/wiki/Code_refactoring * https://www.refactoring.com/ * https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/ Some small optimisations may have slipped in as well. --- src/extension/internal/wmf-inout.cpp | 80 ++++++++++++++-------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/src/extension/internal/wmf-inout.cpp b/src/extension/internal/wmf-inout.cpp index 048804f8cd..2763d7c2c0 100644 --- a/src/extension/internal/wmf-inout.cpp +++ b/src/extension/internal/wmf-inout.cpp @@ -213,6 +213,28 @@ int Wmf::in_hatches(PWMF_CALLBACK_DATA d, char *test){ return(0); } +class TagEmitter +{ +public: + TagEmitter(Glib::ustring & defs, char * tmpcolor, char * hpathname): + defs(defs), tmpcolor(tmpcolor), hpathname(hpathname) + { + }; + void append(const char *prefix, const char * inner) + { + defs += " "; + defs += prefix; + defs += hpathname; + defs += inner; + defs += tmpcolor; + defs += "\" />\n"; + } + +protected: + Glib::ustring & defs; + char * tmpcolor, * hpathname; +}; + /* (Conditionally) add a hatch. If a matching hatch already exists nothing happens. If one does not exist it is added to the hatches list and also entered into . This is also used to add the path part of the hatches, which they reference with a xlink:href @@ -239,6 +261,7 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat break; } auto & defs = d->defs; + TagEmitter a(defs, tmpcolor, hpathname); /* For both bkMode types set the PATH + FOREGROUND COLOR for the indicated standard hatch. This will be used late to compose, or recompose the transparent or opaque final hatch.*/ @@ -253,51 +276,30 @@ uint32_t Wmf::add_hatch(PWMF_CALLBACK_DATA d, uint32_t hatchType, U_COLORREF hat defs += "\n"; switch(hatchType){ case U_HS_HORIZONTAL: - defs += " \n"; + a.append("\n"; + a.append("\n"; + a.append("\n"; + a.append("\n"; + a.append("\n"; - defs += " \n"; + a.append("\n"; + a.append(" Date: Sun, 12 May 2019 15:23:58 +0300 Subject: [PATCH 3/3] rename to avoid name clashes. --- src/extension/internal/wmf-inout.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extension/internal/wmf-inout.cpp b/src/extension/internal/wmf-inout.cpp index 2763d7c2c0..cea259dc42 100644 --- a/src/extension/internal/wmf-inout.cpp +++ b/src/extension/internal/wmf-inout.cpp @@ -216,8 +216,8 @@ int Wmf::in_hatches(PWMF_CALLBACK_DATA d, char *test){ class TagEmitter { public: - TagEmitter(Glib::ustring & defs, char * tmpcolor, char * hpathname): - defs(defs), tmpcolor(tmpcolor), hpathname(hpathname) + TagEmitter(Glib::ustring & p_defs, char * p_tmpcolor, char * p_hpathname): + defs(p_defs), tmpcolor(p_tmpcolor), hpathname(p_hpathname) { }; void append(const char *prefix, const char * inner) -- GitLab