Home | History | Annotate | Download | only in psaux
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  psfont.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 <ft2build.h>
     40 #include FT_INTERNAL_CALC_H
     41 
     42 #include "psft.h"
     43 
     44 #include "psglue.h"
     45 #include "psfont.h"
     46 #include "pserror.h"
     47 #include "psintrp.h"
     48 
     49 
     50   /* Compute a stem darkening amount in character space. */
     51   static void
     52   cf2_computeDarkening( CF2_Fixed   emRatio,
     53                         CF2_Fixed   ppem,
     54                         CF2_Fixed   stemWidth,
     55                         CF2_Fixed*  darkenAmount,
     56                         CF2_Fixed   boldenAmount,
     57                         FT_Bool     stemDarkened,
     58                         FT_Int*     darkenParams )
     59   {
     60     /*
     61      * Total darkening amount is computed in 1000 unit character space
     62      * using the modified 5 part curve as Adobe's Avalon rasterizer.
     63      * The darkening amount is smaller for thicker stems.
     64      * It becomes zero when the stem is thicker than 2.333 pixels.
     65      *
     66      * By default, we use
     67      *
     68      *   darkenAmount = 0.4 pixels   if scaledStem <= 0.5 pixels,
     69      *   darkenAmount = 0.275 pixels if 1 <= scaledStem <= 1.667 pixels,
     70      *   darkenAmount = 0 pixel      if scaledStem >= 2.333 pixels,
     71      *
     72      * and piecewise linear in-between:
     73      *
     74      *
     75      *   darkening
     76      *       ^
     77      *       |
     78      *       |      (x1,y1)
     79      *       |--------+
     80      *       |         \
     81      *       |          \
     82      *       |           \          (x3,y3)
     83      *       |            +----------+
     84      *       |        (x2,y2)         \
     85      *       |                         \
     86      *       |                          \
     87      *       |                           +-----------------
     88      *       |                         (x4,y4)
     89      *       +--------------------------------------------->   stem
     90      *                                                       thickness
     91      *
     92      *
     93      * This corresponds to the following values for the
     94      * `darkening-parameters' property:
     95      *
     96      *   (x1, y1) = (500, 400)
     97      *   (x2, y2) = (1000, 275)
     98      *   (x3, y3) = (1667, 275)
     99      *   (x4, y4) = (2333, 0)
    100      *
    101      */
    102 
    103     /* Internal calculations are done in units per thousand for */
    104     /* convenience. The x axis is scaled stem width in          */
    105     /* thousandths of a pixel. That is, 1000 is 1 pixel.        */
    106     /* The y axis is darkening amount in thousandths of a pixel.*/
    107     /* In the code, below, dividing by ppem and                 */
    108     /* adjusting for emRatio converts darkenAmount to character */
    109     /* space (font units).                                      */
    110     CF2_Fixed  stemWidthPer1000, scaledStem;
    111     FT_Int     logBase2;
    112 
    113 
    114     *darkenAmount = 0;
    115 
    116     if ( boldenAmount == 0 && !stemDarkened )
    117       return;
    118 
    119     /* protect against range problems and divide by zero */
    120     if ( emRatio < cf2_doubleToFixed( .01 ) )
    121       return;
    122 
    123     if ( stemDarkened )
    124     {
    125       FT_Int  x1 = darkenParams[0];
    126       FT_Int  y1 = darkenParams[1];
    127       FT_Int  x2 = darkenParams[2];
    128       FT_Int  y2 = darkenParams[3];
    129       FT_Int  x3 = darkenParams[4];
    130       FT_Int  y3 = darkenParams[5];
    131       FT_Int  x4 = darkenParams[6];
    132       FT_Int  y4 = darkenParams[7];
    133 
    134 
    135       /* convert from true character space to 1000 unit character space; */
    136       /* add synthetic emboldening effect                                */
    137 
    138       /* `stemWidthPer1000' will not overflow for a legitimate font      */
    139 
    140       stemWidthPer1000 = FT_MulFix( stemWidth + boldenAmount, emRatio );
    141 
    142       /* `scaledStem' can easily overflow, so we must clamp its maximum  */
    143       /* value; the test doesn't need to be precise, but must be         */
    144       /* conservative.  The clamp value (default 2333) where             */
    145       /* `darkenAmount' is zero is well below the overflow value of      */
    146       /* 32767.                                                          */
    147       /*                                                                 */
    148       /* FT_MSB computes the integer part of the base 2 logarithm.  The  */
    149       /* number of bits for the product is 1 or 2 more than the sum of   */
    150       /* logarithms; remembering that the 16 lowest bits of the fraction */
    151       /* are dropped this is correct to within a factor of almost 4.     */
    152       /* For example, 0x80.0000 * 0x80.0000 = 0x4000.0000 is 23+23 and   */
    153       /* is flagged as possible overflow because 0xFF.FFFF * 0xFF.FFFF = */
    154       /* 0xFFFF.FE00 is also 23+23.                                      */
    155 
    156       logBase2 = FT_MSB( (FT_UInt32)stemWidthPer1000 ) +
    157                    FT_MSB( (FT_UInt32)ppem );
    158 
    159       if ( logBase2 >= 46 )
    160         /* possible overflow */
    161         scaledStem = cf2_intToFixed( x4 );
    162       else
    163         scaledStem = FT_MulFix( stemWidthPer1000, ppem );
    164 
    165       /* now apply the darkening parameters */
    166 
    167       if ( scaledStem < cf2_intToFixed( x1 ) )
    168         *darkenAmount = FT_DivFix( cf2_intToFixed( y1 ), ppem );
    169 
    170       else if ( scaledStem < cf2_intToFixed( x2 ) )
    171       {
    172         FT_Int  xdelta = x2 - x1;
    173         FT_Int  ydelta = y2 - y1;
    174         FT_Int  x      = stemWidthPer1000 -
    175                            FT_DivFix( cf2_intToFixed( x1 ), ppem );
    176 
    177 
    178         if ( !xdelta )
    179           goto Try_x3;
    180 
    181         *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
    182                           FT_DivFix( cf2_intToFixed( y1 ), ppem );
    183       }
    184 
    185       else if ( scaledStem < cf2_intToFixed( x3 ) )
    186       {
    187       Try_x3:
    188         {
    189           FT_Int  xdelta = x3 - x2;
    190           FT_Int  ydelta = y3 - y2;
    191           FT_Int  x      = stemWidthPer1000 -
    192                              FT_DivFix( cf2_intToFixed( x2 ), ppem );
    193 
    194 
    195           if ( !xdelta )
    196             goto Try_x4;
    197 
    198           *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
    199                             FT_DivFix( cf2_intToFixed( y2 ), ppem );
    200         }
    201       }
    202 
    203       else if ( scaledStem < cf2_intToFixed( x4 ) )
    204       {
    205       Try_x4:
    206         {
    207           FT_Int  xdelta = x4 - x3;
    208           FT_Int  ydelta = y4 - y3;
    209           FT_Int  x      = stemWidthPer1000 -
    210                              FT_DivFix( cf2_intToFixed( x3 ), ppem );
    211 
    212 
    213           if ( !xdelta )
    214             goto Use_y4;
    215 
    216           *darkenAmount = FT_MulDiv( x, ydelta, xdelta ) +
    217                             FT_DivFix( cf2_intToFixed( y3 ), ppem );
    218         }
    219       }
    220 
    221       else
    222       {
    223       Use_y4:
    224         *darkenAmount = FT_DivFix( cf2_intToFixed( y4 ), ppem );
    225       }
    226 
    227       /* use half the amount on each side and convert back to true */
    228       /* character space                                           */
    229       *darkenAmount = FT_DivFix( *darkenAmount, 2 * emRatio );
    230     }
    231 
    232     /* add synthetic emboldening effect in character space */
    233     *darkenAmount += boldenAmount / 2;
    234   }
    235 
    236 
    237   /* set up values for the current FontDict and matrix; */
    238   /* called for each glyph to be rendered               */
    239 
    240   /* caller's transform is adjusted for subpixel positioning */
    241   static void
    242   cf2_font_setup( CF2_Font           font,
    243                   const CF2_Matrix*  transform )
    244   {
    245     /* pointer to parsed font object */
    246     PS_Decoder*  decoder = font->decoder;
    247 
    248     FT_Bool  needExtraSetup = FALSE;
    249 
    250     CFF_VStoreRec*  vstore;
    251     FT_Bool         hasVariations = FALSE;
    252 
    253     /* character space units */
    254     CF2_Fixed  boldenX = font->syntheticEmboldeningAmountX;
    255     CF2_Fixed  boldenY = font->syntheticEmboldeningAmountY;
    256 
    257     CFF_SubFont  subFont;
    258     CF2_Fixed    ppem;
    259 
    260     CF2_UInt   lenNormalizedV = 0;
    261     FT_Fixed*  normalizedV    = NULL;
    262 
    263     /* clear previous error */
    264     font->error = FT_Err_Ok;
    265 
    266     /* if a CID fontDict has changed, we need to recompute some cached */
    267     /* data                                                            */
    268     subFont = cf2_getSubfont( decoder );
    269     if ( font->lastSubfont != subFont )
    270     {
    271       font->lastSubfont = subFont;
    272       needExtraSetup    = TRUE;
    273     }
    274 
    275     if ( !font->isT1 )
    276     {
    277       FT_Service_CFFLoad  cffload = (FT_Service_CFFLoad)font->cffload;
    278 
    279 
    280       /* check for variation vectors */
    281       vstore        = cf2_getVStore( decoder );
    282       hasVariations = ( vstore->dataCount != 0 );
    283 
    284       if ( hasVariations )
    285       {
    286 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
    287         /* check whether Private DICT in this subfont needs to be reparsed */
    288         font->error = cf2_getNormalizedVector( decoder,
    289                                                &lenNormalizedV,
    290                                                &normalizedV );
    291         if ( font->error )
    292           return;
    293 
    294         if ( cffload->blend_check_vector( &subFont->blend,
    295                                           subFont->private_dict.vsindex,
    296                                           lenNormalizedV,
    297                                           normalizedV ) )
    298         {
    299           /* blend has changed, reparse */
    300           cffload->load_private_dict( decoder->cff,
    301                                       subFont,
    302                                       lenNormalizedV,
    303                                       normalizedV );
    304           needExtraSetup = TRUE;
    305         }
    306 #endif
    307 
    308         /* copy from subfont */
    309         font->blend.font = subFont->blend.font;
    310 
    311         /* clear state of charstring blend */
    312         font->blend.usedBV = FALSE;
    313 
    314         /* initialize value for charstring */
    315         font->vsindex = subFont->private_dict.vsindex;
    316 
    317         /* store vector inputs for blends in charstring */
    318         font->lenNDV = lenNormalizedV;
    319         font->NDV    = normalizedV;
    320       }
    321     }
    322 
    323     /* if ppem has changed, we need to recompute some cached data         */
    324     /* note: because of CID font matrix concatenation, ppem and transform */
    325     /*       do not necessarily track.                                    */
    326     ppem = cf2_getPpemY( decoder );
    327     if ( font->ppem != ppem )
    328     {
    329       font->ppem     = ppem;
    330       needExtraSetup = TRUE;
    331     }
    332 
    333     /* copy hinted flag on each call */
    334     font->hinted = (FT_Bool)( font->renderingFlags & CF2_FlagsHinted );
    335 
    336     /* determine if transform has changed;       */
    337     /* include Fontmatrix but ignore translation */
    338     if ( ft_memcmp( transform,
    339                     &font->currentTransform,
    340                     4 * sizeof ( CF2_Fixed ) ) != 0 )
    341     {
    342       /* save `key' information for `cache of one' matrix data; */
    343       /* save client transform, without the translation         */
    344       font->currentTransform    = *transform;
    345       font->currentTransform.tx =
    346       font->currentTransform.ty = cf2_intToFixed( 0 );
    347 
    348       /* TODO: FreeType transform is simple scalar; for now, use identity */
    349       /*       for outer                                                  */
    350       font->innerTransform   = *transform;
    351       font->outerTransform.a =
    352       font->outerTransform.d = cf2_intToFixed( 1 );
    353       font->outerTransform.b =
    354       font->outerTransform.c = cf2_intToFixed( 0 );
    355 
    356       needExtraSetup = TRUE;
    357     }
    358 
    359     /*
    360      * font->darkened is set to true if there is a stem darkening request or
    361      * the font is synthetic emboldened.
    362      * font->darkened controls whether to adjust blue zones, winding order,
    363      * and hinting.
    364      *
    365      */
    366     if ( font->stemDarkened != ( font->renderingFlags & CF2_FlagsDarkened ) )
    367     {
    368       font->stemDarkened =
    369         (FT_Bool)( font->renderingFlags & CF2_FlagsDarkened );
    370 
    371       /* blue zones depend on darkened flag */
    372       needExtraSetup = TRUE;
    373     }
    374 
    375     /* recompute variables that are dependent on transform or FontDict or */
    376     /* darken flag                                                        */
    377     if ( needExtraSetup )
    378     {
    379       /* StdVW is found in the private dictionary;                       */
    380       /* recompute darkening amounts whenever private dictionary or      */
    381       /* transform change                                                */
    382       /* Note: a rendering flag turns darkening on or off, so we want to */
    383       /*       store the `on' amounts;                                   */
    384       /*       darkening amount is computed in character space           */
    385       /* TODO: testing size-dependent darkening here;                    */
    386       /*       what to do for rotations?                                 */
    387 
    388       CF2_Fixed  emRatio;
    389       CF2_Fixed  stdHW;
    390       CF2_Int    unitsPerEm = font->unitsPerEm;
    391 
    392 
    393       if ( unitsPerEm == 0 )
    394         unitsPerEm = 1000;
    395 
    396       ppem = FT_MAX( cf2_intToFixed( 4 ),
    397                      font->ppem ); /* use minimum ppem of 4 */
    398 
    399 #if 0
    400       /* since vstem is measured in the x-direction, we use the `a' member */
    401       /* of the fontMatrix                                                 */
    402       emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->a );
    403 #endif
    404 
    405       /* Freetype does not preserve the fontMatrix when parsing; use */
    406       /* unitsPerEm instead.                                         */
    407       /* TODO: check precision of this                               */
    408       emRatio     = cf2_intToFixed( 1000 ) / unitsPerEm;
    409       font->stdVW = cf2_getStdVW( decoder );
    410 
    411       if ( font->stdVW <= 0 )
    412         font->stdVW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
    413 
    414       if ( boldenX > 0 )
    415       {
    416         /* Ensure that boldenX is at least 1 pixel for synthetic bold font */
    417         /* (similar to what Avalon does)                                   */
    418         boldenX = FT_MAX( boldenX,
    419                           FT_DivFix( cf2_intToFixed( unitsPerEm ), ppem ) );
    420 
    421         /* Synthetic emboldening adds at least 1 pixel to darkenX, while */
    422         /* stem darkening adds at most half pixel.  Since the purpose of */
    423         /* stem darkening (readability at small sizes) is met with       */
    424         /* synthetic emboldening, no need to add stem darkening for a    */
    425         /* synthetic bold font.                                          */
    426         cf2_computeDarkening( emRatio,
    427                               ppem,
    428                               font->stdVW,
    429                               &font->darkenX,
    430                               boldenX,
    431                               FALSE,
    432                               font->darkenParams );
    433       }
    434       else
    435         cf2_computeDarkening( emRatio,
    436                               ppem,
    437                               font->stdVW,
    438                               &font->darkenX,
    439                               0,
    440                               font->stemDarkened,
    441                               font->darkenParams );
    442 
    443 #if 0
    444       /* since hstem is measured in the y-direction, we use the `d' member */
    445       /* of the fontMatrix                                                 */
    446       /* TODO: use the same units per em as above; check this              */
    447       emRatio = cf2_fixedFracMul( cf2_intToFixed( 1000 ), fontMatrix->d );
    448 #endif
    449 
    450       /* set the default stem width, because it must be the same for all */
    451       /* family members;                                                 */
    452       /* choose a constant for StdHW that depends on font contrast       */
    453       stdHW = cf2_getStdHW( decoder );
    454 
    455       if ( stdHW > 0 && font->stdVW > MUL_INT32( 2, stdHW ) )
    456         font->stdHW = FT_DivFix( cf2_intToFixed( 75 ), emRatio );
    457       else
    458       {
    459         /* low contrast font gets less hstem darkening */
    460         font->stdHW = FT_DivFix( cf2_intToFixed( 110 ), emRatio );
    461       }
    462 
    463       cf2_computeDarkening( emRatio,
    464                             ppem,
    465                             font->stdHW,
    466                             &font->darkenY,
    467                             boldenY,
    468                             font->stemDarkened,
    469                             font->darkenParams );
    470 
    471       if ( font->darkenX != 0 || font->darkenY != 0 )
    472         font->darkened = TRUE;
    473       else
    474         font->darkened = FALSE;
    475 
    476       font->reverseWinding = FALSE; /* initial expectation is CCW */
    477 
    478       /* compute blue zones for this instance */
    479       cf2_blues_init( &font->blues, font );
    480 
    481     } /* needExtraSetup */
    482   }
    483 
    484 
    485   /* equivalent to AdobeGetOutline */
    486   FT_LOCAL_DEF( FT_Error )
    487   cf2_getGlyphOutline( CF2_Font           font,
    488                        CF2_Buffer         charstring,
    489                        const CF2_Matrix*  transform,
    490                        CF2_F16Dot16*      glyphWidth )
    491   {
    492     FT_Error  lastError = FT_Err_Ok;
    493 
    494     FT_Vector  translation;
    495 
    496 #if 0
    497     FT_Vector  advancePoint;
    498 #endif
    499 
    500     CF2_Fixed  advWidth = 0;
    501     FT_Bool    needWinding;
    502 
    503 
    504     /* Note: use both integer and fraction for outlines.  This allows bbox */
    505     /*       to come out directly.                                         */
    506 
    507     translation.x = transform->tx;
    508     translation.y = transform->ty;
    509 
    510     /* set up values based on transform */
    511     cf2_font_setup( font, transform );
    512     if ( font->error )
    513       goto exit;                      /* setup encountered an error */
    514 
    515     /* reset darken direction */
    516     font->reverseWinding = FALSE;
    517 
    518     /* winding order only affects darkening */
    519     needWinding = font->darkened;
    520 
    521     while ( 1 )
    522     {
    523       /* reset output buffer */
    524       cf2_outline_reset( &font->outline );
    525 
    526       /* build the outline, passing the full translation */
    527       cf2_interpT2CharString( font,
    528                               charstring,
    529                               (CF2_OutlineCallbacks)&font->outline,
    530                               &translation,
    531                               FALSE,
    532                               0,
    533                               0,
    534                               &advWidth );
    535 
    536       if ( font->error )
    537         goto exit;
    538 
    539       if ( !needWinding )
    540         break;
    541 
    542       /* check winding order */
    543       if ( font->outline.root.windingMomentum >= 0 ) /* CFF is CCW */
    544         break;
    545 
    546       /* invert darkening and render again                            */
    547       /* TODO: this should be a parameter to getOutline-computeOffset */
    548       font->reverseWinding = TRUE;
    549 
    550       needWinding = FALSE;    /* exit after next iteration */
    551     }
    552 
    553     /* finish storing client outline */
    554     cf2_outline_close( &font->outline );
    555 
    556   exit:
    557     /* FreeType just wants the advance width; there is no translation */
    558     *glyphWidth = advWidth;
    559 
    560     /* free resources and collect errors from objects we've used */
    561     cf2_setError( &font->error, lastError );
    562 
    563     return font->error;
    564   }
    565 
    566 
    567 /* END */
    568