Skip to content

Commit

Permalink
Per #2402, update the implementation of CgFontCollection to use std::…
Browse files Browse the repository at this point in the history
…vector instead of dynamically allocating memory to avoid SonarQube findings.
  • Loading branch information
JohnHalleyGotway committed Feb 16, 2023
1 parent 0f24770 commit 8f2445a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 90 deletions.
119 changes: 34 additions & 85 deletions src/tools/other/mode_graphics/cgraph_font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ face = 0;
if ( !get_env(cg_font_env, gs_font_dir) ) {

mlog << Error
<< "\n\n CgFont::init_from_scratch() -> "
<< "\nCgFont::init_from_scratch() -> "
<< "unable to get environment variable \""
<< cg_font_env << "\"\n\n";

Expand Down Expand Up @@ -258,7 +258,8 @@ clear();

if ( (n < 0) || (n >= total_predef_fonts) ) {

mlog << Error << "\n\n CgFont::set_by_number(int) -> range check error\n\n";
mlog << Error << "\nCgFont::set_by_number(int) -> "
<< "range check error\n\n";

exit ( 1 );

Expand All @@ -274,7 +275,8 @@ full_afm_name << gs_font_dir << '/' << gs_font_dir << '/' << short_afm_name;

if ( !file_exists(full_afm_name.c_str()) ) {

mlog << Error << "\n\n CgFont::set_by_number(int) -> can't find afm file \"" << full_afm_name << "\"\n\n";
mlog << Error << "\nCgFont::set_by_number(int) -> "
<< "can't find afm file \"" << full_afm_name << "\"\n\n";

exit ( 1 );

Expand All @@ -284,7 +286,8 @@ afm = new Afm;

if ( !(afm->read(full_afm_name)) ) {

mlog << Error << "\n\n CgFont::set_by_number(int) -> trouble reading afm file \"" << full_afm_name << "\"\n\n";
mlog << Error << "\nCgFont::set_by_number(int) -> "
<< "trouble reading afm file \"" << full_afm_name << "\"\n\n";

exit ( 1 );

Expand Down Expand Up @@ -374,8 +377,6 @@ void CgFontCollection::init_from_scratch()

{

e = (CgFont **) 0;

clear();

return;
Expand All @@ -390,34 +391,28 @@ void CgFontCollection::clear()

{

if ( e ) {

int j, error;

for (j=0; j<Nalloc; ++j) {

if ( ! (e[j]) ) continue;

error = FT_Done_Face(e[j]->face);
int j, error;

if ( error ) {
for (j=0; j<Nalloc; ++j) {

mlog << Error << "\n\n CgFontCollection::clear() -> trouble closing typeface \"" << e[j]->short_pfb_name << "\"\n\n";
error = FT_Done_Face(e[j].face);

exit ( 1 );
if ( error ) {

}
mlog << Error << "\nCgFontCollection::clear() -> "
<< "trouble closing typeface \"" << e[j].short_pfb_name
<< "\"\n\n";

e[j]->face = 0;

if ( e[j] ) { delete e[j]; e[j] = (CgFont *) 0; }
exit ( 1 );

}

delete [] e; e = (CgFont **) 0;
e[j].face = 0;

}

e.clear();

Nelements = Nalloc = 0;

return;
Expand Down Expand Up @@ -445,7 +440,7 @@ for (j=0; j<Nelements; ++j) {

out << prefix << "Element # " << j << " ...\n";

e[j]->dump(out, depth + 1);
e[j].dump(out, depth + 1);

}

Expand All @@ -470,37 +465,13 @@ void CgFontCollection::extend(int n)

if ( n <= Nalloc ) return;

int j, k;
CgFont ** u = (CgFont **) 0;


k = n/alloc_inc;
int k = n/alloc_inc;

if ( n%alloc_inc ) ++k;

n = k*alloc_inc;

u = new CgFont * [n];

if ( !u ) {

mlog << Error << "\n\n CgFontCollection::extend(int) -> memory allocation error\n\n";

exit ( 1 );

}

for (j=0; j<n; ++j) u[j] = (CgFont *) 0;

if ( e ) {

for (j=0; j<Nelements; ++j) u[j] = e[j];

delete [] e; e = (CgFont **) 0;

}

e = u; u = (CgFont **) 0;
e.reserve(n);

Nalloc = n;

Expand All @@ -522,23 +493,11 @@ void CgFontCollection::assign(const CgFontCollection & c)

clear();

if ( !(c.e) ) return;
if ( c.e.size() == 0 ) return;

extend(c.Nelements);

int j;

for (j=0; j<(c.Nelements); ++j) {

if ( c.e[j] ) {

e[j] = new CgFont;

*(e[j]) = *(c.e[j]);

}

}
e = c.e;

Nelements = c.Nelements;

Expand All @@ -558,13 +517,9 @@ void CgFontCollection::add(const CgFont & f)

{

// if ( have_it(f) ) return;

extend(Nelements + 1);

e[Nelements] = new CgFont;

*(e[Nelements]) = f;
e.push_back(f);

++Nelements;

Expand All @@ -585,9 +540,7 @@ if ( have_it(f) ) return;

extend(Nelements + 1);

e[Nelements] = new CgFont;

*(e[Nelements]) = f;
e.push_back(f);

++Nelements;

Expand All @@ -608,7 +561,7 @@ int j;

for (j=0; j<Nelements; ++j) {

if ( same_font( *(e[j]), f ) ) return ( true );
if ( same_font( e[j], f ) ) return ( true );

}

Expand All @@ -620,15 +573,15 @@ return ( false );
////////////////////////////////////////////////////////////////////////


CgFont * CgFontCollection::lookup_by_ps_font_number(int n) const
CgFont * CgFontCollection::lookup_by_ps_font_number(int n)

{

int j;

for (j=0; j<Nelements; ++j) {

if ( e[j]->ps_font_number == n ) return ( e[j] );
if ( e[j].ps_font_number == n ) return ( & e[j] );

}

Expand All @@ -641,15 +594,15 @@ return ( (CgFont *) 0 );
////////////////////////////////////////////////////////////////////////


CgFont * CgFontCollection::lookup_by_ps_name(const char * name) const
CgFont * CgFontCollection::lookup_by_ps_name(const char * name)

{

int j;

for (j=0; j<Nelements; ++j) {

if ( e[j]->ps_name == name ) return ( e[j] );
if ( e[j].ps_name == name ) return ( & e[j] );

}

Expand All @@ -662,19 +615,20 @@ return ( (CgFont *) 0 );
////////////////////////////////////////////////////////////////////////


CgFont * CgFontCollection::operator[](int k) const
CgFont * CgFontCollection::operator[](int k)

{

if ( (k < 0) || (k >= Nelements) ) {

mlog << Error << "\n\n CgFont * CgFontCollection::operator[](int) const -> range check error\n\n";
mlog << Error << "\nCgFont * CgFontCollection::operator[](int) const -> "
<< "range check error\n\n";

exit ( 1 );

}

return ( e[k] );
return ( & e[k] );

}

Expand Down Expand Up @@ -702,8 +656,3 @@ return ( false );


////////////////////////////////////////////////////////////////////////





10 changes: 5 additions & 5 deletions src/tools/other/mode_graphics/cgraph_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include <iostream>
#include <vector>

#include "ft2build.h"
#include FT_FREETYPE_H
Expand Down Expand Up @@ -98,7 +99,7 @@ class CgFontCollection {

int Nalloc;

CgFont ** e;
std::vector<CgFont> e;

public:

Expand All @@ -117,12 +118,11 @@ class CgFontCollection {
void add (const CgFont &);
void add_no_repeat (const CgFont &);

CgFont * lookup_by_ps_name(const char *) const;
CgFont * lookup_by_ps_name(const char *);

CgFont * lookup_by_ps_font_number(int) const; // for builtin fonts
CgFont * lookup_by_ps_font_number(int); // for builtin fonts


CgFont * operator[](int) const;
CgFont * operator[](int);

};

Expand Down

0 comments on commit 8f2445a

Please sign in to comment.