Home | History | Annotate | Download | only in cff
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  cf2font.c                                                              */
      4 /*                                                                         */
      5 /*    Adobe's code for font instances (body).                              */
      6 /*                                                                         */
      7 /*  Copyright 2007-2014 Adobe Systems Incorporated.                        */
      8 /*                                                                         */
      9 /*  This software, and all works of authorship, whether in source or       */
     10 /*  object code form as indicated by the copyright notice(s) included      */
     11 /*  herein (collectively, the "Work") is made available, and may only be   */
     12 /*  used, modified, and distributed under the FreeType Project License,    */
     13 /*  LICENSE.TXT.  Additionally, subject to the terms and conditions of the */
     14 /*  FreeType Project License, each contributor to the Work hereby grants   */
     15 /*  to any individual or legal entity exercising permissions granted by    */
     16 /*  the FreeType Project License and this section (hereafter, "You" or     */
     17 /*  "Your") a perpetual, worldwide, non-exclusive, no-charge,              */
     18 /*  royalty-free, irrevocable (except as stated in this section) patent    */
     19 /*  license to make, have made, use, offer to sell, sell, import, and      */
     20 /*  otherwise transfer the Work, where such license applies only to those  */
     21 /*  patent claims licensable by such contributor that are necessarily      */
     22 /*  infringed by their contribution(s) alone or by combination of their    */
     23 /*  contribution(s) with the Work to which such contribution(s) was        */
     24 /*  submitted.  If You institute patent litigation against any entity      */
     25 /*  (including a cross-claim or counterclaim in a lawsuit) alleging that   */
     26 /*  the Work or a contribution incorporated within the Work constitutes    */
     27 /*  direct or contributory patent infringement, then any patent licenses   */
     28 /*  granted to You under this License for that Work shall terminate as of  */
     29 /*  the date such litigation is filed.                                     */
     30 /*                                                                         */
     31 /*  By using, modifying, or distributing the Work you indicate that you    */
     32 /*  have read and understood the terms and conditions of the               */
     33 /*  FreeType Project License as well as those provided in this section,    */
     34 /*  and you accept them fully.                                             */
     35 /*                                                                         */
     36 /***************************************************************************/
     37 
     38 
     39 #include "cf2ft.h"
     40 
     41 #include "cf2glue.h"
     42 #include "cf2font.h"
     43 #include "cf2error.h"
     44 #include "cf2intrp.h"
     45 
     46 
     47   /* Compute a stem darkening amount in character space. */
     48   static void
     49   cf2_computeDarkening( CF2_Fixed   emRatio,
     50                         CF2_Fixed   ppem,
     51                         CF2_Fixed   stemWidth,
     52                         CF2_Fixed*  darkenAmount,
     53                         CF2_Fixed   boldenAmount,
     54                         FT_Bool     stemDarkened,
     55                         FT_Int*     darkenParams )
     56   {
     57     /*
     58      * Total darkening amount is computed in 1000 unit character space
     59      * using the modified 5 part curve as Adobe's Avalon rasterizer.
     60      * The darkening amount is smaller for thicker stems.
     61      * It becomes zero when the stem is thicker than 2.333 pixels.
     62      *
     63      * By default, we use
     64      *
     65      *   darkenAmount = 0.4 pixels   if scaledStem <= 0.5 pixels,
     66      *   darkenAmount = 0.275 pixels if 1 <= scaledStem <= 1.667 pixels,
     67      *   darkenAmount = 0 pixel      if scaledStem >= 2.333 pixels,
     68      *
     69      * and piecewise linear in-between:
     70      *
     71      *
     72      *   darkening
     73      *       ^
     74      *       |
     75      *       |      (x1,y1)
     76      *       |--------+
     77      *       |         \
     78      *       |          \
     79      *       |           \          (x3,y3)
     80      *       |            +----------+
     81      *       |        (x2,y2)         \
     82      *       |                         \
     83      *       |                          \
     84      *       |                           +-----------------
     85      *       |                         (x4,y4)
     86      *       +--------------------------------------------->   stem
     87      *                                                       thickness
     88      *
     89      *
     90      * This corresponds to the following values for the
     91      * `darkening-parameters' property:
     92      *
     93      *   (x1, y1) = (500, 400)
     94      *   (x2, y2) = (1000, 275)
     95      *   (x3, y3) = (1667, 275)
     96      *   (x4, y4) = (2333, 0)
     97      *
     98      */
     99 
    100     /* Internal calculations are done in units per thousand for */
    101     /* convenience. The x axis is scaled stem width in          */
    102     /* thousandths of a pixel. That is, 1000 is 1 pixel.        */
    103     /* The y axis is darkening amount in thousandths of a pixel.*/
    104     /* In the code, below, dividing by ppem and                 */
    105     /* adjusting for emRatio converts darkenAmount to character */
    106     /* space (font units).                                      */
    107     CF2_Fixed  stemWidthPer1000, scaledStem;
    108 
    109 
    110     *darkenAmount = 0;
    111 
    112     if ( boldenAmount == 0 && !stemDarkened )
    113       return;
    114 
    115     /* protect against range problems and divide by zero */
    116     if ( emRatio < cf2_floatToFixed( .01 ) )
    117       return;
    118 
    119     if ( stemDarkened )
    120     {
    121       FT_Int  x1 = darkenParams[0];
    122       FT_Int  y1 = darkenParams[1];
    123       FT_Int  x2 = darkenParams[2];
    124       FT_Int  y2 = darkenParams[3];
    125       FT_Int  x3 = darkenParams[4];
    126       FT_Int  y3 = darkenParams[5];
    127       FT_Int  x4 = darkenParams[6];
    128       FT_Int  y4 = darkenParams[7];
    129 
    130 
    131       /* convert from true character space to 1000 unit character space; */
    132       /* add synthetic emboldening effect                                */
    133 
    134       /* we have to assure that the computation of `scaledStem' */
    135       /* and `stemWidthPer1000' don't overflow                  */
    136 
    137       stemWidthPer1000 = FT_MulFix( stemWidth + boldenAmount, emRatio );
    138 
    139       if ( emRatio > CF2_FIXED_ONE                          &&
    140            stemWidthPer1000 <= ( stemWidth + boldenAmount ) )
    141       {
    142         stemWidthPer1000 = 0;                      /* to pacify compiler */
    143         scaledStem       = cf2_intToFixed( x4 );
    144       }
    145       else
    146       {
    147         scaledStem = FT_MulFix( stemWidthPer1000, ppem );
    148 
    149         if ( ppem > CF2_FIXED_ONE           &&
    150              scaledStem <= stemWidthPer1000 )
    151           scaledStem = cf2_intToFixed( x4 );
    152       }
    153 
    154       /* now apply the darkening parameters */
    155 
    156       if ( scaledStem < cf2_intToFixed( x1 ) )
    157         *darkenAmount = FT_DivFix( cf2_intToFixed( y1 ), ppem );
    158 
    159       else if ( scaledStem < cf2_intToFixed( x2 ) )
    160       {
    161         FT_Int  xdelta = x2 - x1;
    162         FT_Int  ydelta = y2 - y1;
    163         FT_Int  x      = stemWidthPer1000 -
    164                            FT_DivFix( cf2_intToFixed( x1 ), ppem );
    165 
    166 
    167         if ( !xdelta )
    168           goto Try_x3;
    169 
    170         *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
    171                           FT_DivFix( cf2_intToFixed( y1 ), ppem );
    172       }
    173 
    174       else if ( scaledStem < cf2_intToFixed( x3 ) )
    175       {
    176       Try_x3:
    177         {
    178           FT_Int  xdelta = x3 - x2;
    179           FT_Int  ydelta = y3 - y2;
    180           FT_Int  x      = stemWidthPer1000 -
    181                              FT_DivFix( cf2_intToFixed( x2 ), ppem );
    182 
    183 
    184           if ( !xdelta )
    185             goto Try_x4;
    186 
    187           *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
    188                             FT_DivFix( cf2_intToFixed( y2 ), ppem );
    189         }
    190       }
    191 
    192       else if ( scaledStem < cf2_intToFixed( x4 ) )
    193       {
    194       Try_x4:
    195         {
    196           FT_Int  xdelta = x4 - x3;
    197           FT_Int  ydelta = y4 - y3;
    198           FT_Int  x      = stemWidthPer1000 -
    199                              FT_DivFix( cf2_intToFixed( x3 ), ppem );
    200 
    201 
    202           if ( !xdelta )
    203             goto Use_y4;
    204 
    205           *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
    206                             FT_DivFix( cf2_intToFixed( y3 ), ppem );
    207         }
    208       }
    209 
    210       else
    211       {
    212       Use_y4:
    213         *darkenAmount = FT_DivFix( cf2_intToFixed( y4 ), ppem );
    214       }
    215 
    216       /* use half the amount on each side and convert back to true */
    217       /* character space                                           */
    218       *darkenAmount = FT_DivFix( *darkenAmount, 2 * emRatio );
    219     }
    220 
    221     /* add synthetic emboldening effect in character space */
    222     *darkenAmount += boldenAmount / 2;
    223   }
    224 
    225 
    226   /* set up values for the current FontDict and matrix */
    227 
    228   /* caller's transform is adjusted for subpixel positioning */
    229   static void
    230   cf2_font_setup( CF2_Font           font,
    231                   const CF2_Matrix*  transform )
    232   {
    233     /* pointer to parsed font object */
    234     CFF_Decoder*  decoder = font->decoder;
    235 
    236     FT_Bool  needExtraSetup = FALSE;
    237 
    238     /* character space units */
    239     CF2_Fixed  boldenX = font->syntheticEmboldeningAmountX;
    240     CF2_Fixed  boldenY = font->syntheticEmboldeningAmountY;
    241 
    242     CFF_SubFont  subFont;
    243     CF2_Fixed    ppem;
    244 
    245 
    246     /* clear previous error */
    247     font->error = FT_Err_Ok;
    248 
    249     /* if a CID fontDict has changed, we need to recompute some cached */
    250     /* data                                                            */
    251     subFont = cf2_getSubfont( decoder );
    252     if ( font->lastSubfont != subFont )
    253     {
    254       font->lastSubfont = subFont;
    255       needExtraSetup    = TRUE;
    256     }
    257 
    258     /* if ppem has changed, we need to recompute some cached data         */
    259     /* note: because of CID font matrix concatenation, ppem and transform */
    260     /*       do not necessarily track.                                    */
    261     ppem = cf2_getPpemY( decoder );
    262     if ( font->ppem != ppem )
    263     {
    264       font->ppem     = ppem;
    265       needExtraSetup = TRUE;
    266     }
    267 
    268     /* copy hinted flag on each call */
    269     font->hinted = (FT_Bool)( font->renderingFlags & CF2_FlagsHinted );
    270 
    271     /* determine if transform has changed;       */
    272     /* include Fontmatrix but ignore translation */
    273     if ( ft_memcmp( transform,
    274                     &font->currentTransform,
    275                     4 * sizeof ( CF2_Fixed ) ) != 0 )
    276     {
    277       /* save `key' information for `cache of one' matrix data; */
    278       /* save client transform, without the translation         */
    279       font->currentTransform    = *transform;
    280       font->currentTransform.tx =
    281       font->currentTransform.ty = cf2_intToFixed( 0 );
    282 
    283       /* TODO: FreeType transform is simple scalar; for now, use identity */
    284       /*       for outer                                                  */
    285       font->innerTransform   = *transform;
    286       font->outerTransform.a =
    287       font->outerTransform.d = cf2_intToFixed( 1 );
    288       font->outerTransform.b =
    289       font->outerTransform.c = cf2_intToFixed( 0 );
    290 
    291       needExtraSetup = TRUE;
    292     }
    293 
    294     /*
    295      * font->darkened is set to true if there is a stem darkening request or
    296      * the font is synthetic emboldened.
    297      * font->darkened controls whether to adjust blue zones, winding order,
    298      * and hinting.
    299      *
    300      */
    301     if ( font->stemDarkened != ( font->renderingFlags & CF2_FlagsDarkened ) )
    302     {
    303       font->stemDarkened =
    304         (FT_Bool)( font->renderingFlags & CF2_FlagsDarkened );
    305 
    306       /* blue zones depend on darkened flag */
    307       needExtraSetup = TRUE;
    308     }
    309 
    310     /* recompute variables that are dependent on transform or FontDict or */
    311     /* darken flag                                                        */
    312     if ( needExtraSetup )
    313     {
    314       /* StdVW is found in the private dictionary;                       */
    315       /* recompute darkening amounts whenever private dictionary or      */
    316       /* transform change                                                */
    317       /* Note: a rendering flag turns darkening on or off, so we want to */
    318       /*       store the `on' amounts;                                   */
    319       /*       darkening amount is computed in character space           */
    320       /* TODO: testing size-dependent darkening here;                    */
    321       /*       what to do for rotations?                                 */
    322 
    323       CF2_Fixed  emRatio;
    324       CF2_Fixed  stdHW;
    325       CF2_Int    unitsPerEm = font->unitsPerEm;
    326 
    327 
    328       if ( unitsPerEm == 0 )
    329         unitsPerEm = 1000;
    330 
    331       ppem = FT_MAX( cf2_intToFixed( 4 ),
    332                      font->ppem ); /* use minimum ppem of 4 */
    333 
    334 #if 0
    335       /* since vstem is measured in the x-direction, we use the `a' member */
    336       /* of the fontMatrix                                                 */
    337       emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->a );
    338 #endif
    339 
    340       /* Freetype does not preserve the fontMatrix when parsing; use */
    341       /* unitsPerEm instead.                                         */
    342       /* TODO: check precision of this                               */
    343       emRatio     = cf2_intToFixed( 1000 ) / unitsPerEm;
    344       font->stdVW = cf2_getStdVW( decoder );
    345 
    346       if ( font->stdVW <= 0 )
    347         font->stdVW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
    348 
    349       if ( boldenX > 0 )
    350       {
    351         /* Ensure that boldenX is at least 1 pixel for synthetic bold font */
    352         /* (similar to what Avalon does)                                   */
    353         boldenX = FT_MAX( boldenX,
    354                           FT_DivFix( cf2_intToFixed( unitsPerEm ), ppem ) );
    355 
    356         /* Synthetic emboldening adds at least 1 pixel to darkenX, while */
    357         /* stem darkening adds at most half pixel.  Since the purpose of */
    358         /* stem darkening (readability at small sizes) is met with       */
    359         /* synthetic emboldening, no need to add stem darkening for a    */
    360         /* synthetic bold font.                                          */
    361         cf2_computeDarkening( emRatio,
    362                               ppem,
    363                               font->stdVW,
    364                               &font->darkenX,
    365                               boldenX,
    366                               FALSE,
    367                               font->darkenParams );
    368       }
    369       else
    370         cf2_computeDarkening( emRatio,
    371                               ppem,
    372                               font->stdVW,
    373                               &font->darkenX,
    374                               0,
    375                               font->stemDarkened,
    376                               font->darkenParams );
    377 
    378 #if 0
    379       /* since hstem is measured in the y-direction, we use the `d' member */
    380       /* of the fontMatrix                                                 */
    381       /* TODO: use the same units per em as above; check this              */
    382       emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->d );
    383 #endif
    384 
    385       /* set the default stem width, because it must be the same for all */
    386       /* family members;                                                 */
    387       /* choose a constant for StdHW that depends on font contrast       */
    388       stdHW = cf2_getStdHW( decoder );
    389 
    390       if ( stdHW > 0 && font->stdVW > 2 * stdHW )
    391         font->stdHW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
    392       else
    393       {
    394         /* low contrast font gets less hstem darkening */
    395         font->stdHW = FT_DivFix( cf2_intToFixed( 110 ), emRatio );
    396       }
    397 
    398       cf2_computeDarkening( emRatio,
    399                             ppem,
    400                             font->stdHW,
    401                             &font->darkenY,
    402                             boldenY,
    403                             font->stemDarkened,
    404                             font->darkenParams );
    405 
    406       if ( font->darkenX != 0 || font->darkenY != 0 )
    407         font->darkened = TRUE;
    408       else
    409         font->darkened = FALSE;
    410 
    411       font->reverseWinding = FALSE; /* initial expectation is CCW */
    412 
    413       /* compute blue zones for this instance */
    414       cf2_blues_init( &font->blues, font );
    415     }
    416   }
    417 
    418 
    419   /* equivalent to AdobeGetOutline */
    420   FT_LOCAL_DEF( FT_Error )
    421   cf2_getGlyphOutline( CF2_Font           font,
    422                        CF2_Buffer         charstring,
    423                        const CF2_Matrix*  transform,
    424                        CF2_F16Dot16*      glyphWidth )
    425   {
    426     FT_Error  lastError = FT_Err_Ok;
    427 
    428     FT_Vector  translation;
    429 
    430 #if 0
    431     FT_Vector  advancePoint;
    432 #endif
    433 
    434     CF2_Fixed  advWidth = 0;
    435     FT_Bool    needWinding;
    436 
    437 
    438     /* Note: use both integer and fraction for outlines.  This allows bbox */
    439     /*       to come out directly.                                         */
    440 
    441     translation.x = transform->tx;
    442     translation.y = transform->ty;
    443 
    444     /* set up values based on transform */
    445     cf2_font_setup( font, transform );
    446     if ( font->error )
    447       goto exit;                      /* setup encountered an error */
    448 
    449     /* reset darken direction */
    450     font->reverseWinding = FALSE;
    451 
    452     /* winding order only affects darkening */
    453     needWinding = font->darkened;
    454 
    455     while ( 1 )
    456     {
    457       /* reset output buffer */
    458       cf2_outline_reset( &font->outline );
    459 
    460       /* build the outline, passing the full translation */
    461       cf2_interpT2CharString( font,
    462                               charstring,
    463                               (CF2_OutlineCallbacks)&font->outline,
    464                               &translation,
    465                               FALSE,
    466                               0,
    467                               0,
    468                               &advWidth );
    469 
    470       if ( font->error )
    471         goto exit;
    472 
    473       if ( !needWinding )
    474         break;
    475 
    476       /* check winding order */
    477       if ( font->outline.root.windingMomentum >= 0 ) /* CFF is CCW */
    478         break;
    479 
    480       /* invert darkening and render again                            */
    481       /* TODO: this should be a parameter to getOutline-computeOffset */
    482       font->reverseWinding = TRUE;
    483 
    484       needWinding = FALSE;    /* exit after next iteration */
    485     }
    486 
    487     /* finish storing client outline */
    488     cf2_outline_close( &font->outline );
    489 
    490   exit:
    491     /* FreeType just wants the advance width; there is no translation */
    492     *glyphWidth = advWidth;
    493 
    494     /* free resources and collect errors from objects we've used */
    495     cf2_setError( &font->error, lastError );
    496 
    497     return font->error;
    498   }
    499 
    500 
    501 /* END */
    502