Skip to content

Commit e78d502

Browse files
committed
Trim trailing zeroes in ttconv output
These could increase the file length in case of fonts with many composite glyphs.
1 parent b9c956e commit e78d502

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

extern/ttconv/pprdrv_tt.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ Fixed getFixed(BYTE *s)
109109

110110
/*
111111
** Get a 16 bit fixed point (2.14) number.
112+
**
113+
** The value range is from -2.0 to a little under 2.0. The two
114+
** most-significant bits encode the whole part in two's-complement
115+
** format and the remaining 14 bits encode the numerator n of the
116+
** fraction n/16384 to be added to the whole part.
117+
**
118+
** https://docs.microsoft.com/en-us/typography/opentype/otspec181/otff#data-types
112119
*/
113120
F2DOT14 getF2DOT14(BYTE *s)
114121
{
@@ -126,9 +133,32 @@ F2DOT14 getF2DOT14(BYTE *s)
126133
return val;
127134
}
128135

129-
float F2DOT14value(F2DOT14 f)
136+
/*
137+
** Convert a fixed-point 2.14 number to a newly-allocated string.
138+
** Use at most six decimals but remove trailing zeroes and possibly
139+
** the decimal point.
140+
*/
141+
char* F2DOT14value(F2DOT14 f)
130142
{
131-
return f.whole + (float)f.fraction/16384;
143+
const size_t maxlen = strlen("-1.234567") + 1;
144+
char *value = (char*)calloc(sizeof(char), maxlen);
145+
if (value == NULL ||
146+
snprintf(value, maxlen, "%.6f", f.whole + (float)f.fraction/16384) < 0) {
147+
abort();
148+
}
149+
150+
char *ptr = &value[maxlen-1];
151+
while (ptr > value && *ptr == '\0') {
152+
ptr--;
153+
}
154+
while (ptr > value && *ptr == '0') {
155+
*ptr-- = '\0';
156+
}
157+
if (ptr > value && *ptr == '.') {
158+
*ptr = '\0';
159+
}
160+
161+
return value;
132162
}
133163

134164

extern/ttconv/pprdrv_tt2.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ void GlyphToType3::load_char(TTFONT* font, BYTE *glyph)
409409
} /* end of load_char() */
410410

411411
/*
412-
** Emmit PostScript code for a composite character.
412+
** Emit PostScript code for a composite character.
413+
**
414+
** https://docs.microsoft.com/en-us/typography/opentype/spec/glyf#composite-glyph-description
413415
*/
414416
void GlyphToType3::do_composite(TTStreamWriter& stream, struct TTFONT *font, BYTE *glyph)
415417
{
@@ -480,10 +482,15 @@ void GlyphToType3::do_composite(TTStreamWriter& stream, struct TTFONT *font, BYT
480482
subglyph here. However, that doesn't seem to work with
481483
xpdf or gs (only acrobat), so instead, this just includes
482484
the subglyph here inline. */
483-
stream.printf("q %.6f %.6f %.6f %.6f %d %d cm\n",
484-
F2DOT14value(mat00), F2DOT14value(mat01),
485-
F2DOT14value(mat10), F2DOT14value(mat11),
486-
topost(arg1), topost(arg2));
485+
char *s00 = F2DOT14value(mat00), *s01 = F2DOT14value(mat01),
486+
*s10 = F2DOT14value(mat10), *s11 = F2DOT14value(mat11);
487+
488+
stream.printf("q %s %s %s %s %d %d cm\n",
489+
s00, s01, s10, s11, topost(arg1), topost(arg2));
490+
free(s00);
491+
free(s01);
492+
free(s10);
493+
free(s11);
487494
}
488495
else
489496
{

extern/ttconv/truetype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ ULONG getULONG(BYTE *p);
9494
USHORT getUSHORT(BYTE *p);
9595
Fixed getFixed(BYTE *p);
9696
F2DOT14 getF2DOT14(BYTE *p);
97-
float F2DOT14value(F2DOT14 f);
97+
char *F2DOT14value(F2DOT14 f);
9898

9999
/*
100100
** Get an funits word.

0 commit comments

Comments
 (0)