summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-04-01 12:52:05 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-04-01 12:52:05 -0700
commit103579f030bfead4a1f821734dd6dbaf823c5527 (patch)
treecaec556059b3838deefa7a7999db4958f1399e7f
parent4208f2d0dd928a2e627a2284337302be62b3967a (diff)
Variable scope reductions as recommended by cppcheck
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--fonttosfnt.c5
-rw-r--r--struct.c28
-rw-r--r--util.c6
-rw-r--r--write.c74
4 files changed, 48 insertions, 65 deletions
diff --git a/fonttosfnt.c b/fonttosfnt.c
index 7828b79..e66d0d2 100644
--- a/fonttosfnt.c
+++ b/fonttosfnt.c
@@ -47,7 +47,6 @@ int
main(int argc, char **argv)
{
int i;
- int rc;
char *output = NULL;
FontPtr font;
@@ -113,11 +112,11 @@ main(int argc, char **argv)
"The creation of an OpenType font collection is recommended.\n");
if(i == argc) {
- rc = readFile(NULL, font);
+ int rc = readFile(NULL, font);
if(rc != 0)
exit(1);
} else while(i < argc) {
- rc = readFile(argv[i], font);
+ int rc = readFile(argv[i], font);
if(rc != 0)
exit(1);
i++;
diff --git a/struct.c b/struct.c
index 01f5e4d..a44b1e8 100644
--- a/struct.c
+++ b/struct.c
@@ -243,9 +243,8 @@ makeBitmap(StrikePtr strike, int code,
IndexSubTablePtr
makeIndexSubTables(StrikePtr strike, CmapPtr cmap)
{
- IndexSubTablePtr table, first, last;
- BitmapPtr bitmap0, bitmap;
- int index, n;
+ IndexSubTablePtr first, last;
+ int index;
first = NULL;
last = NULL;
@@ -262,6 +261,10 @@ makeIndexSubTables(StrikePtr strike, CmapPtr cmap)
index = 0;
while(index < 0xFFFF) {
int constantMetrics = 1;
+ int n;
+ IndexSubTablePtr table;
+ BitmapPtr bitmap0, bitmap;
+
bitmap0 = strikeBitmapIndex(strike, cmap, index);
if(bitmap0 == NULL) {
index++;
@@ -318,13 +321,12 @@ int
fontIndex(FontPtr font, int code)
{
StrikePtr strike;
- BitmapPtr bitmap;
if(code == 0)
return 0;
strike = font->strikes;
while(strike) {
- bitmap = STRIKE_BITMAP(strike, code);
+ BitmapPtr bitmap = STRIKE_BITMAP(strike, code);
if(bitmap)
return bitmap->index;
strike = strike->next;
@@ -338,11 +340,12 @@ makeCmap(FontPtr font)
CmapPtr cmap_head = NULL;
CmapPtr cmap_last = NULL;
CmapPtr cmap;
- int code, i, index, maxindex = 0;
+ int code, maxindex = 0;
code = 0;
while(code < FONT_CODES) {
- index = fontIndex(font, code);
+ int i;
+ int index = fontIndex(font, code);
if(index < 0) {
code++;
continue;
@@ -374,7 +377,7 @@ makeCmap(FontPtr font)
cmap_head->inverse = calloc(maxindex + 1, sizeof(int));
cmap = cmap_head;
while(cmap) {
- for(i = cmap->index;
+ for(int i = cmap->index;
i <= cmap->endCode - cmap->startCode + cmap->index; i++) {
cmap_head->inverse[i] =
i - cmap->index + cmap->startCode;
@@ -428,12 +431,10 @@ strikeBitmapIndex(StrikePtr strike, CmapPtr cmap, int index)
int
strikeMaxWidth(StrikePtr strike)
{
- BitmapPtr bitmap;
- int i;
int width_max = 0;
- for(i = 0; i < FONT_CODES; i++) {
- bitmap = STRIKE_BITMAP(strike, i);
+ for(int i = 0; i < FONT_CODES; i++) {
+ BitmapPtr bitmap = STRIKE_BITMAP(strike, i);
if(!bitmap)
continue;
if(bitmap->advanceWidth > width_max)
@@ -450,11 +451,10 @@ glyphMetrics(FontPtr font, int code,
int *x_max_return, int *y_max_return)
{
StrikePtr strike;
- BitmapPtr bitmap;
strike = font->strikes;
while(strike) {
- bitmap = STRIKE_BITMAP(strike, code);
+ BitmapPtr bitmap = STRIKE_BITMAP(strike, code);
if(bitmap) {
if(width_return)
*width_return =
diff --git a/util.c b/util.c
index 655b5cf..32460b1 100644
--- a/util.c
+++ b/util.c
@@ -132,12 +132,11 @@ vsprintf_alloc(const char *f, va_list args)
char *
makeUTF16(const char *string)
{
- int i;
int n = strlen(string);
char *value = malloc(2 * n);
if(!value)
return NULL;
- for(i = 0; i < n; i++) {
+ for(int i = 0; i < n; i++) {
value[2 * i] = '\0';
value[2 * i + 1] = string[i];
}
@@ -445,7 +444,6 @@ degreesToFraction(int deg, int *num, int *den)
{
double n, d;
double rad, val;
- int i;
if(deg <= -(60 * TWO_SIXTEENTH) || deg >= (60 * TWO_SIXTEENTH))
goto fail;
@@ -460,7 +458,7 @@ degreesToFraction(int deg, int *num, int *den)
val = atan2(n, d);
/* There must be a cleaner way */
- for(i = 1; i < 10000; i++) {
+ for(int i = 1; i < 10000; i++) {
if((int)(d * i) != 0.0 &&
fabs(atan2(ROUND(n * i), ROUND(d * i)) - val) < 0.05) {
*num = (int)ROUND(n * i);
diff --git a/write.c b/write.c
index 37129ef..223a431 100644
--- a/write.c
+++ b/write.c
@@ -208,7 +208,6 @@ readULONG(FILE *out)
void
fontMetrics(FontPtr font)
{
- int i, rc;
double sumAwidth = 0;
unsigned count = 0;
@@ -218,9 +217,9 @@ fontMetrics(FontPtr font)
font->metrics.minX = 10000 * TWO_SIXTEENTH;
font->metrics.minY = 10000 * TWO_SIXTEENTH;
- for(i = 0; i < FONT_CODES; i++) {
+ for(int i = 0; i < FONT_CODES; i++) {
int awidth, x0, y0, x1, y1;
- rc = glyphMetrics(font, i, &awidth, &x0, &y0, &x1, &y1);
+ int rc = glyphMetrics(font, i, &awidth, &x0, &y0, &x1, &y1);
if(rc < 0)
continue;
@@ -483,7 +482,6 @@ static unsigned
computeChecksum(FILE *out, int offset, int length)
{
int rc;
- int i;
unsigned sum = 0;
if(offset % 4 != 0) {
@@ -498,7 +496,7 @@ computeChecksum(FILE *out, int offset, int length)
}
/* This relies on the fact that we always pad tables with zeroes. */
- for(i = 0; i < length; i += 4) {
+ for(int i = 0; i < length; i += 4) {
sum += readULONG(out);
}
return sum;
@@ -507,12 +505,9 @@ computeChecksum(FILE *out, int offset, int length)
static int
fixupDir(FILE *out, FontPtr font, int numTables, int *offset, int *length)
{
- int rc, i;
- unsigned sum;
-
- for(i = 0; i < numTables; i++) {
- sum = computeChecksum(out, offset[i], length[i]);
- rc = fseek(out, 12 + 16 * i + 4, SEEK_SET);
+ for(int i = 0; i < numTables; i++) {
+ unsigned sum = computeChecksum(out, offset[i], length[i]);
+ int rc = fseek(out, 12 + 16 * i + 4, SEEK_SET);
if(rc != 0) {
perror("Couldn't seek");
return -1;
@@ -577,19 +572,18 @@ static int
outputRaster(FILE *out, char *raster, int width, int height, int stride,
int bit_aligned)
{
- int i, j;
int len = 0;
if(!bit_aligned || width % 8 == 0) {
- for(i = 0; i < height; i++) {
+ for(int i = 0; i < height; i++) {
writeCHARs(out, raster + i * stride, (width + 7) / 8);
len += (width + 7) / 8;
}
} else {
int bit = 0;
unsigned char v = 0;
- for(i = 0; i < height; i++) {
- for(j = 0; j < width; j++) {
+ for(int i = 0; i < height; i++) {
+ for(int j = 0; j < width; j++) {
if(BITREF(raster, stride, j, i))
v |= 1 << (7 - bit);
bit++;
@@ -613,9 +607,6 @@ static int
writeEBDT(FILE* out, FontPtr font)
{
StrikePtr strike;
- BitmapPtr bitmap;
- IndexSubTablePtr table;
- int i;
int offset;
int ebdt_start;
@@ -626,10 +617,10 @@ writeEBDT(FILE* out, FontPtr font)
strike = font->strikes;
while(strike) {
- table = strike->indexSubTables;
+ IndexSubTablePtr table = strike->indexSubTables;
while(table) {
- for(i = table->firstGlyphIndex; i <= table->lastGlyphIndex; i++) {
- bitmap = strikeBitmapIndex(strike, current_cmap, i);
+ for(int i = table->firstGlyphIndex; i <= table->lastGlyphIndex; i++) {
+ BitmapPtr bitmap = strikeBitmapIndex(strike, current_cmap, i);
bitmap->location = offset;
if(bit_aligned_flag && table->constantMetrics) {
/* image format 5 */
@@ -662,9 +653,8 @@ writeEBDT(FILE* out, FontPtr font)
static int
writeEBLC(FILE* out, FontPtr font)
{
- int i, rc, numstrikes, eblc_start, num, den;
+ int numstrikes, eblc_start, num, den;
StrikePtr strike;
- IndexSubTablePtr table;
degreesToFraction(font->italicAngle, &num, &den);
@@ -688,7 +678,7 @@ writeEBLC(FILE* out, FontPtr font)
writeULONG(out, 0xDEADFACE); /* indexTablesSize */
writeULONG(out, 0xDEADFACE); /* numberOfIndexSubTables */
writeULONG(out, 0); /* colorRef */
- for (i = 0; i <= 1; i++) {
+ for (int i = 0; i <= 1; i++) {
writeCHAR(out, font->pxMetrics.ascent); /* ascender */
writeCHAR(out, -font->pxMetrics.descent); /* descender */
writeBYTE(out, strikeMaxWidth(strike)); /* widthMax */
@@ -714,8 +704,9 @@ writeEBLC(FILE* out, FontPtr font)
/* indexSubTableArray, one per strike */
strike = font->strikes;
while(strike) {
- int endoffset;
+ int endoffset, rc;
int numtables = 0;
+ IndexSubTablePtr table;
strike->indexSubTableLocation = ftell(out);
table = strike->indexSubTables;
@@ -749,12 +740,11 @@ writeEBLC(FILE* out, FontPtr font)
/* actual indexSubTables */
strike = font->strikes;
while(strike) {
- table = strike->indexSubTables;
+ IndexSubTablePtr table = strike->indexSubTables;
while(table) {
- int location;
+ int location, rc;
int data_location;
int short_offsets;
- int offset;
location = ftell(out);
if (location == -1) {
@@ -777,7 +767,7 @@ writeEBLC(FILE* out, FontPtr font)
strikeBitmapIndex(strike, current_cmap,
table->firstGlyphIndex)->location;
short_offsets = 1;
- for(i = table->firstGlyphIndex; i <= table->lastGlyphIndex; i++) {
+ for(int i = table->firstGlyphIndex; i <= table->lastGlyphIndex; i++) {
if(strikeBitmapIndex(strike, current_cmap, i)->location -
data_location > 0xFFFF) {
short_offsets = 0;
@@ -822,9 +812,9 @@ writeEBLC(FILE* out, FontPtr font)
writeCHAR(out, bitmap->horiBearingY); /* vertBearingY */
writeBYTE(out, font->metrics.maxAwidth); /* vertAdvance */
} else {
- for(i = table->firstGlyphIndex;
+ for(int i = table->firstGlyphIndex;
i <= table->lastGlyphIndex; i++) {
- offset =
+ int offset =
strikeBitmapIndex(strike, current_cmap, i)->location -
data_location;
if(short_offsets)
@@ -975,10 +965,8 @@ writehhea(FILE* out, FontPtr font)
static int
writehmtx(FILE* out, FontPtr font)
{
- int rc, i;
-
- for(i = 0; i <= numglyphs; i++) {
- int code, width, lsb;
+ for(int i = 0; i <= numglyphs; i++) {
+ int code, width, lsb, rc;
code = findCode(current_cmap, i);
if(code < 0)
rc = -1;
@@ -1001,10 +989,8 @@ writehmtx(FILE* out, FontPtr font)
static int
writeloca(FILE* out, FontPtr font)
{
- int i;
-
/* All glyphs undefined -- loca table is empty, so offset 0 */
- for(i = 0; i < numglyphs; i++) {
+ for(int i = 0; i < numglyphs; i++) {
writeSHORT(out, 0);
}
writeSHORT(out, 0);
@@ -1035,14 +1021,13 @@ writemaxp(FILE* out, FontPtr font)
static int
writename(FILE* out, FontPtr font)
{
- int i;
int offset;
writeUSHORT(out, 0); /* format selector */
writeUSHORT(out, font->numNames);
writeUSHORT(out, 6 + font->numNames * 12); /* offset to string storage */
offset = 0;
- for(i = 0; i < font->numNames; i++) {
+ for(int i = 0; i < font->numNames; i++) {
writeUSHORT(out, 3); /* platform id -- Microsoft */
writeUSHORT(out, 1); /* encoding -- Unicode */
writeUSHORT(out, 0x409); /* language id -- American English */
@@ -1051,7 +1036,7 @@ writename(FILE* out, FontPtr font)
writeUSHORT(out, offset); /* string offset */
offset += font->names[i].size;
}
- for(i = 0; i < font->numNames; i++)
+ for(int i = 0; i < font->numNames; i++)
writeCHARs(out, font->names[i].value, font->names[i].size);
return 0;
}
@@ -1059,12 +1044,13 @@ writename(FILE* out, FontPtr font)
static int
writepost(FILE* out, FontPtr font)
{
- int i, rc, previous_width, width, fixed_pitch;
+ int previous_width, fixed_pitch;
fixed_pitch = 1;
previous_width = -1;
- for(i = 0; i < FONT_CODES; i++) {
- rc = glyphMetrics(font, i, &width, NULL, NULL, NULL, NULL);
+ for(int i = 0; i < FONT_CODES; i++) {
+ int width = previous_width;
+ int rc = glyphMetrics(font, i, &width, NULL, NULL, NULL, NULL);
if(rc < 0)
continue;
if(previous_width >= 0) {