[cff] Finish support for `random' operator. * src/cff/cfftypes.h (CFF_SubFontRec): Add `random' field. * src/cff/cffobjs.c: Updated. (cff_driver_init): Initialize random seed value. * src/cff/cffload.c (cff_random): New function. (cff_subfont_load): Add `face' argument. Update all callers. Initialize random number generator with a proper seed value. (cff_font_load): Add `face' argument. Update all callers. * src/cff/cffload.h: Updated. * src/cff/cf2intrp.c (CF2_FIXME): Removed. (cf2_interpT2CharString) <cf2_escRANDOM>: Implement opcode. * src/cff/cffgload.c (cff_decoder_parse_charstrings): Don't initialize random seed value. <cff_op_random>: Use new random seed framework.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
diff --git a/ChangeLog b/ChangeLog
index cb2b776..95873ed 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,30 @@
2017-02-20 Werner Lemberg <wl@gnu.org>
+ [cff] Finish support for `random' operator.
+
+ * src/cff/cfftypes.h (CFF_SubFontRec): Add `random' field.
+
+ * src/cff/cffobjs.c: Updated.
+ (cff_driver_init): Initialize random seed value.
+
+ * src/cff/cffload.c (cff_random): New function.
+ (cff_subfont_load): Add `face' argument.
+ Update all callers.
+ Initialize random number generator with a proper seed value.
+ (cff_font_load): Add `face' argument.
+ Update all callers.
+
+ * src/cff/cffload.h: Updated.
+
+ * src/cff/cf2intrp.c (CF2_FIXME): Removed.
+ (cf2_interpT2CharString) <cf2_escRANDOM>: Implement opcode.
+
+ * src/cff/cffgload.c (cff_decoder_parse_charstrings): Don't
+ initialize random seed value.
+ <cff_op_random>: Use new random seed framework.
+
+2017-02-20 Werner Lemberg <wl@gnu.org>
+
[cff] Sanitize `initialRandomSeed'.
* src/cff/cffload.c (cff_load_private_dict): Make
diff --git a/src/cff/cf2intrp.c b/src/cff/cf2intrp.c
index 6bf298a..62c696d 100644
--- a/src/cff/cf2intrp.c
+++ b/src/cff/cf2intrp.c
@@ -60,12 +60,6 @@
#define FT_COMPONENT trace_cf2interp
- /* some operators are not implemented yet */
-#define CF2_FIXME FT_TRACE4(( "cf2_interpT2CharString:" \
- " operator not implemented yet\n" ))
-
-
-
FT_LOCAL_DEF( void )
cf2_hintmask_init( CF2_HintMask hintmask,
FT_Error* error )
@@ -1268,10 +1262,23 @@
continue; /* do not clear the stack */
case cf2_escRANDOM: /* in spec */
- FT_TRACE4(( " random\n" ));
+ {
+ CF2_F16Dot16 r;
- CF2_FIXME;
- break;
+
+ FT_TRACE4(( " random\n" ));
+
+ /* only use the lower 16 bits of `random' */
+ /* to generate a number in the range (0;1] */
+ r = (CF2_F16Dot16)
+ ( ( decoder->current_subfont->random & 0xFFFF ) + 1 );
+
+ decoder->current_subfont->random =
+ cff_random( decoder->current_subfont->random );
+
+ cf2_stack_pushFixed( opStack, r );
+ }
+ continue; /* do not clear the stack */
case cf2_escMUL:
{
diff --git a/src/cff/cffgload.c b/src/cff/cffgload.c
index f8e80c1..4803b4b 100644
--- a/src/cff/cffgload.c
+++ b/src/cff/cffgload.c
@@ -457,7 +457,7 @@
decoder->glyph_width = sub->private_dict.default_width;
decoder->nominal_width = sub->private_dict.nominal_width;
- decoder->current_subfont = sub; /* for Adobe's CFF handler */
+ decoder->current_subfont = sub;
Exit:
return error;
@@ -913,7 +913,6 @@
FT_Byte* limit;
CFF_Builder* builder = &decoder->builder;
FT_Pos x, y;
- FT_Fixed seed;
FT_Fixed* stack;
FT_Int charstring_type =
decoder->cff->top_font.font_dict.charstring_type;
@@ -929,15 +928,6 @@
decoder->num_hints = 0;
decoder->read_width = 1;
- /* compute random seed from stack address of parameter */
- seed = (FT_Fixed)( ( (FT_Offset)(char*)&seed ^
- (FT_Offset)(char*)&decoder ^
- (FT_Offset)(char*)&charstring_base ) &
- FT_ULONG_MAX );
- seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
- if ( seed == 0 )
- seed = 0x7384;
-
/* initialize the decoder */
decoder->top = decoder->stack;
decoder->zone = decoder->zones;
@@ -2104,22 +2094,16 @@
break;
case cff_op_random:
- {
- FT_Fixed Rand;
-
-
- FT_TRACE4(( " rand\n" ));
+ FT_TRACE4(( " random\n" ));
- Rand = seed;
- if ( Rand >= 0x8000L )
- Rand++;
+ /* only use the lower 16 bits of `random' */
+ /* to generate a number in the range (0;1] */
+ args[0] = (FT_Fixed)
+ ( ( decoder->current_subfont->random & 0xFFFF ) + 1 );
+ args++;
- args[0] = Rand;
- seed = FT_MulFix( seed, 0x10000L - seed );
- if ( seed == 0 )
- seed += 0x2873;
- args++;
- }
+ decoder->current_subfont->random =
+ cff_random( decoder->current_subfont->random );
break;
case cff_op_mul:
diff --git a/src/cff/cffload.c b/src/cff/cffload.c
index 9122ff9..47626a7 100644
--- a/src/cff/cffload.c
+++ b/src/cff/cffload.c
@@ -1931,6 +1931,18 @@
}
+ FT_LOCAL_DEF( FT_UInt32 )
+ cff_random( FT_UInt32 r )
+ {
+ /* a 32bit version of the `xorshift' algorithm */
+ r ^= r << 13;
+ r ^= r >> 17;
+ r ^= r << 5;
+
+ return r;
+ }
+
+
/* There are 3 ways to call this function, distinguished by code. */
/* */
/* . CFF_CODE_TOPDICT for either a CFF Top DICT or a CFF Font DICT */
@@ -1944,7 +1956,8 @@
FT_Stream stream,
FT_ULong base_offset,
FT_UInt code,
- CFF_Font font )
+ CFF_Font font,
+ CFF_Face face )
{
FT_Error error;
CFF_ParserRec parser;
@@ -2041,6 +2054,54 @@
if ( error )
goto Exit;
+ if ( !cff2 )
+ {
+ /*
+ * Initialize the random number generator.
+ *
+ * . If we have a face-specific seed, use it.
+ * If non-zero, update it to a positive value.
+ *
+ * . Otherwise, use the seed from the CFF driver.
+ * If non-zero, update it to a positive value.
+ *
+ * . If the random value is zero, use the seed given by the subfont's
+ * `initialRandomSeed' value.
+ *
+ */
+ if ( face->root.internal->random_seed == -1 )
+ {
+ CFF_Driver driver = (CFF_Driver)FT_FACE_DRIVER( face );
+
+
+ subfont->random = (FT_UInt32)driver->random_seed;
+ if ( driver->random_seed )
+ {
+ do
+ {
+ driver->random_seed = (FT_Int32)cff_random( driver->random_seed );
+
+ } while ( driver->random_seed < 0 );
+ }
+ }
+ else
+ {
+ subfont->random = (FT_UInt32)face->root.internal->random_seed;
+ if ( face->root.internal->random_seed )
+ {
+ do
+ {
+ face->root.internal->random_seed =
+ (FT_Int32)cff_random( face->root.internal->random_seed );
+
+ } while ( face->root.internal->random_seed < 0 );
+ }
+ }
+
+ if ( !subfont->random )
+ subfont->random = (FT_UInt32)priv->initial_random_seed;
+ }
+
/* read the local subrs, if any */
if ( priv->local_subrs_offset )
{
@@ -2086,6 +2147,7 @@
FT_Stream stream,
FT_Int face_index,
CFF_Font font,
+ CFF_Face face,
FT_Bool pure_cff,
FT_Bool cff2 )
{
@@ -2283,7 +2345,8 @@
stream,
base_offset,
cff2 ? CFF2_CODE_TOPDICT : CFF_CODE_TOPDICT,
- font );
+ font,
+ face );
if ( error )
goto Exit;
@@ -2350,7 +2413,8 @@
base_offset,
cff2 ? CFF2_CODE_FONTDICT
: CFF_CODE_TOPDICT,
- font );
+ font,
+ face );
if ( error )
goto Fail_CID;
}
diff --git a/src/cff/cffload.h b/src/cff/cffload.h
index 8be6452..1709a66 100644
--- a/src/cff/cffload.h
+++ b/src/cff/cffload.h
@@ -61,11 +61,15 @@ FT_BEGIN_HEADER
FT_UInt cid );
+ FT_LOCAL( FT_UInt32 )
+ cff_random( FT_UInt32 r );
+
FT_LOCAL( FT_Error )
cff_font_load( FT_Library library,
FT_Stream stream,
FT_Int face_index,
CFF_Font font,
+ CFF_Face face,
FT_Bool pure_cff,
FT_Bool cff2 );
diff --git a/src/cff/cffobjs.c b/src/cff/cffobjs.c
index f0320af..67bf0cc 100644
--- a/src/cff/cffobjs.c
+++ b/src/cff/cffobjs.c
@@ -595,6 +595,7 @@
stream,
face_index,
cff,
+ face,
pure_cff,
cff2 );
if ( error )
@@ -1157,6 +1158,8 @@
{
CFF_Driver driver = (CFF_Driver)module;
+ FT_UInt32 seed;
+
/* set default property values, cf. `ftcffdrv.h' */
#ifdef CFF_CONFIG_OPTION_OLD_ENGINE
@@ -1176,6 +1179,18 @@
driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
+ /* compute random seed from some memory addresses */
+ seed = (FT_UInt32)( (FT_Offset)(char*)&seed ^
+ (FT_Offset)(char*)&module ^
+ (FT_Offset)(char*)module->memory );
+ seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 );
+
+ driver->random_seed = (FT_Int32)seed;
+ if ( driver->random_seed < 0 )
+ driver->random_seed = -driver->random_seed;
+ else if ( driver->random_seed == 0 )
+ driver->random_seed = 123456789;
+
return FT_Err_Ok;
}
diff --git a/src/cff/cfftypes.h b/src/cff/cfftypes.h
index 0c24ed5..8d43e28 100644
--- a/src/cff/cfftypes.h
+++ b/src/cff/cfftypes.h
@@ -321,6 +321,8 @@ FT_BEGIN_HEADER
FT_Byte** local_subrs; /* array of pointers */
/* into Local Subrs INDEX data */
+ FT_UInt32 random;
+
} CFF_SubFontRec;