tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

psblues.c (19403B)


      1 /****************************************************************************
      2 *
      3 * psblues.c
      4 *
      5 *   Adobe's code for handling Blue Zones (body).
      6 *
      7 * Copyright 2009-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 "psft.h"
     40 #include <freetype/internal/ftdebug.h>
     41 
     42 #include "psblues.h"
     43 #include "pshints.h"
     44 #include "psfont.h"
     45 
     46 
     47  /**************************************************************************
     48   *
     49   * The macro FT_COMPONENT is used in trace mode.  It is an implicit
     50   * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
     51   * messages during execution.
     52   */
     53 #undef  FT_COMPONENT
     54 #define FT_COMPONENT  cf2blues
     55 
     56 
     57  FT_LOCAL_DEF( void )
     58  cf2_blues_init( CF2_Blues  blues,
     59                  CF2_Font   font )
     60  {
     61    /* pointer to parsed font object */
     62    PS_Decoder*  decoder = font->decoder;
     63 
     64    CF2_Fixed  zoneHeight;
     65    CF2_Fixed  maxZoneHeight = 0;
     66    CF2_Fixed  csUnitsPerPixel;
     67 
     68    size_t  numBlueValues;
     69    size_t  numOtherBlues;
     70    size_t  numFamilyBlues;
     71    size_t  numFamilyOtherBlues;
     72 
     73    FT_Fixed*  blueValues;
     74    FT_Fixed*  otherBlues;
     75    FT_Fixed*  familyBlues;
     76    FT_Fixed*  familyOtherBlues;
     77 
     78    size_t     i;
     79    CF2_Fixed  emBoxBottom, emBoxTop;
     80 
     81 #if 0
     82    CF2_Int  unitsPerEm = font->unitsPerEm;
     83 
     84 
     85    if ( unitsPerEm == 0 )
     86      unitsPerEm = 1000;
     87 #endif
     88 
     89    FT_ZERO( blues );
     90    blues->scale = font->innerTransform.d;
     91 
     92    cf2_getBlueMetrics( decoder,
     93                        &blues->blueScale,
     94                        &blues->blueShift,
     95                        &blues->blueFuzz );
     96 
     97    cf2_getBlueValues( decoder, &numBlueValues, &blueValues );
     98    cf2_getOtherBlues( decoder, &numOtherBlues, &otherBlues );
     99    cf2_getFamilyBlues( decoder, &numFamilyBlues, &familyBlues );
    100    cf2_getFamilyOtherBlues( decoder, &numFamilyOtherBlues, &familyOtherBlues );
    101 
    102    /*
    103     * synthetic em box hint heuristic
    104     *
    105     * Apply this when ideographic dictionary (LanguageGroup 1) has no
    106     * real alignment zones.  Adobe tools generate dummy zones at -250 and
    107     * 1100 for a 1000 unit em.  Fonts with ICF-based alignment zones
    108     * should not enable the heuristic.  When the heuristic is enabled,
    109     * the font's blue zones are ignored.
    110     *
    111     */
    112 
    113    /* get em box from OS/2 typoAscender/Descender                      */
    114    /* TODO: FreeType does not parse these metrics.  Skip them for now. */
    115 #if 0
    116    FCM_getHorizontalLineMetrics( &e,
    117                                  font->font,
    118                                  &ascender,
    119                                  &descender,
    120                                  &linegap );
    121    if ( ascender - descender == unitsPerEm )
    122    {
    123      emBoxBottom = cf2_intToFixed( descender );
    124      emBoxTop    = cf2_intToFixed( ascender );
    125    }
    126    else
    127 #endif
    128    {
    129      emBoxBottom = CF2_ICF_Bottom;
    130      emBoxTop    = CF2_ICF_Top;
    131    }
    132 
    133    if ( cf2_getLanguageGroup( decoder ) == 1     &&
    134         ( numBlueValues == 0                   ||
    135           ( numBlueValues == 4               &&
    136             blueValues[0] < emBoxBottom      &&
    137             blueValues[1] < emBoxBottom      &&
    138             blueValues[2] > emBoxTop         &&
    139             blueValues[3] > emBoxTop         ) ) )
    140    {
    141      /*
    142       * Construct hint edges suitable for synthetic ghost hints at top
    143       * and bottom of em box.  +-CF2_MIN_COUNTER allows for unhinted
    144       * features above or below the last hinted edge.  This also gives a
    145       * net 1 pixel boost to the height of ideographic glyphs.
    146       *
    147       * Note: Adjust synthetic hints outward by epsilon (0x.0001) to
    148       *       avoid interference.  E.g., some fonts have real hints at
    149       *       880 and -120.
    150       */
    151 
    152      blues->emBoxBottomEdge.csCoord = emBoxBottom - CF2_FIXED_EPSILON;
    153      blues->emBoxBottomEdge.dsCoord = cf2_fixedRound(
    154                                         FT_MulFix(
    155                                           blues->emBoxBottomEdge.csCoord,
    156                                           blues->scale ) ) -
    157                                       CF2_MIN_COUNTER;
    158      blues->emBoxBottomEdge.scale   = blues->scale;
    159      blues->emBoxBottomEdge.flags   = CF2_GhostBottom |
    160                                       CF2_Locked |
    161                                       CF2_Synthetic;
    162 
    163      blues->emBoxTopEdge.csCoord = emBoxTop + CF2_FIXED_EPSILON +
    164                                    2 * font->darkenY;
    165      blues->emBoxTopEdge.dsCoord = cf2_fixedRound(
    166                                      FT_MulFix(
    167                                        blues->emBoxTopEdge.csCoord,
    168                                        blues->scale ) ) +
    169                                    CF2_MIN_COUNTER;
    170      blues->emBoxTopEdge.scale   = blues->scale;
    171      blues->emBoxTopEdge.flags   = CF2_GhostTop |
    172                                    CF2_Locked |
    173                                    CF2_Synthetic;
    174 
    175      blues->doEmBoxHints = TRUE;    /* enable the heuristic */
    176 
    177      return;
    178    }
    179 
    180    /* copy `BlueValues' and `OtherBlues' to a combined array of top and */
    181    /* bottom zones                                                      */
    182    for ( i = 0; i < numBlueValues; i += 2 )
    183    {
    184      blues->zone[blues->count].csBottomEdge = blueValues[i];
    185      blues->zone[blues->count].csTopEdge    = blueValues[i + 1];
    186 
    187      zoneHeight = SUB_INT32( blues->zone[blues->count].csTopEdge,
    188                              blues->zone[blues->count].csBottomEdge );
    189 
    190      if ( zoneHeight < 0 )
    191      {
    192        FT_TRACE4(( "cf2_blues_init: ignoring negative zone height\n" ));
    193        continue;   /* reject this zone */
    194      }
    195 
    196      if ( zoneHeight > maxZoneHeight )
    197      {
    198        /* take maximum before darkening adjustment      */
    199        /* so overshoot suppression point doesn't change */
    200        maxZoneHeight = zoneHeight;
    201      }
    202 
    203      /* adjust both edges of top zone upward by twice darkening amount */
    204      if ( i != 0 )
    205      {
    206        blues->zone[blues->count].csTopEdge    += 2 * font->darkenY;
    207        blues->zone[blues->count].csBottomEdge += 2 * font->darkenY;
    208      }
    209 
    210      /* first `BlueValue' is bottom zone; others are top */
    211      if ( i == 0 )
    212      {
    213        blues->zone[blues->count].bottomZone =
    214          TRUE;
    215        blues->zone[blues->count].csFlatEdge =
    216          blues->zone[blues->count].csTopEdge;
    217      }
    218      else
    219      {
    220        blues->zone[blues->count].bottomZone =
    221          FALSE;
    222        blues->zone[blues->count].csFlatEdge =
    223          blues->zone[blues->count].csBottomEdge;
    224      }
    225 
    226      blues->count += 1;
    227    }
    228 
    229    for ( i = 0; i < numOtherBlues; i += 2 )
    230    {
    231      blues->zone[blues->count].csBottomEdge = otherBlues[i];
    232      blues->zone[blues->count].csTopEdge    = otherBlues[i + 1];
    233 
    234      zoneHeight = SUB_INT32( blues->zone[blues->count].csTopEdge,
    235                              blues->zone[blues->count].csBottomEdge );
    236 
    237      if ( zoneHeight < 0 )
    238      {
    239        FT_TRACE4(( "cf2_blues_init: ignoring negative zone height\n" ));
    240        continue;   /* reject this zone */
    241      }
    242 
    243      if ( zoneHeight > maxZoneHeight )
    244      {
    245        /* take maximum before darkening adjustment      */
    246        /* so overshoot suppression point doesn't change */
    247        maxZoneHeight = zoneHeight;
    248      }
    249 
    250      /* Note: bottom zones are not adjusted for darkening amount */
    251 
    252      /* all OtherBlues are bottom zone */
    253      blues->zone[blues->count].bottomZone =
    254        TRUE;
    255      blues->zone[blues->count].csFlatEdge =
    256        blues->zone[blues->count].csTopEdge;
    257 
    258      blues->count += 1;
    259    }
    260 
    261    /* Adjust for FamilyBlues */
    262 
    263    /* Search for the nearest flat edge in `FamilyBlues' or                */
    264    /* `FamilyOtherBlues'.  According to the Black Book, any matching edge */
    265    /* must be within one device pixel                                     */
    266 
    267    csUnitsPerPixel = FT_DivFix( cf2_intToFixed( 1 ), blues->scale );
    268 
    269    /* loop on all zones in this font */
    270    for ( i = 0; i < blues->count; i++ )
    271    {
    272      size_t     j;
    273      CF2_Fixed  minDiff;
    274      CF2_Fixed  flatFamilyEdge, diff;
    275      /* value for this font */
    276      CF2_Fixed  flatEdge = blues->zone[i].csFlatEdge;
    277 
    278 
    279      if ( blues->zone[i].bottomZone )
    280      {
    281        /* In a bottom zone, the top edge is the flat edge.             */
    282        /* Search `FamilyOtherBlues' for bottom zones; look for closest */
    283        /* Family edge that is within the one pixel threshold.          */
    284 
    285        minDiff = CF2_FIXED_MAX;
    286 
    287        for ( j = 0; j < numFamilyOtherBlues; j += 2 )
    288        {
    289          /* top edge */
    290          flatFamilyEdge = familyOtherBlues[j + 1];
    291 
    292          diff = cf2_fixedAbs( SUB_INT32( flatEdge, flatFamilyEdge ) );
    293 
    294          if ( diff < minDiff && diff < csUnitsPerPixel )
    295          {
    296            blues->zone[i].csFlatEdge = flatFamilyEdge;
    297            minDiff                   = diff;
    298 
    299            if ( diff == 0 )
    300              break;
    301          }
    302        }
    303 
    304        /* check the first member of FamilyBlues, which is a bottom zone */
    305        if ( numFamilyBlues >= 2 )
    306        {
    307          /* top edge */
    308          flatFamilyEdge = familyBlues[1];
    309 
    310          diff = cf2_fixedAbs( SUB_INT32( flatEdge, flatFamilyEdge ) );
    311 
    312          if ( diff < minDiff && diff < csUnitsPerPixel )
    313            blues->zone[i].csFlatEdge = flatFamilyEdge;
    314        }
    315      }
    316      else
    317      {
    318        /* In a top zone, the bottom edge is the flat edge.                */
    319        /* Search `FamilyBlues' for top zones; skip first zone, which is a */
    320        /* bottom zone; look for closest Family edge that is within the    */
    321        /* one pixel threshold                                             */
    322 
    323        minDiff = CF2_FIXED_MAX;
    324 
    325        for ( j = 2; j < numFamilyBlues; j += 2 )
    326        {
    327          /* bottom edge */
    328          flatFamilyEdge = familyBlues[j];
    329 
    330          /* adjust edges of top zone upward by twice darkening amount */
    331          flatFamilyEdge += 2 * font->darkenY;      /* bottom edge */
    332 
    333          diff = cf2_fixedAbs( SUB_INT32( flatEdge, flatFamilyEdge ) );
    334 
    335          if ( diff < minDiff && diff < csUnitsPerPixel )
    336          {
    337            blues->zone[i].csFlatEdge = flatFamilyEdge;
    338            minDiff                   = diff;
    339 
    340            if ( diff == 0 )
    341              break;
    342          }
    343        }
    344      }
    345    }
    346 
    347    /* TODO: enforce separation of zones, including BlueFuzz */
    348 
    349    /* Adjust BlueScale; similar to AdjustBlueScale() in coretype */
    350    /* `bcsetup.c'.                                               */
    351 
    352    if ( maxZoneHeight > 0 )
    353    {
    354      if ( blues->blueScale > FT_DivFix( cf2_intToFixed( 1 ),
    355                                         maxZoneHeight ) )
    356      {
    357        /* clamp at maximum scale */
    358        blues->blueScale = FT_DivFix( cf2_intToFixed( 1 ),
    359                                      maxZoneHeight );
    360      }
    361 
    362      /*
    363       * TODO: Revisit the bug fix for 613448.  The minimum scale
    364       *       requirement catches a number of library fonts.  For
    365       *       example, with default BlueScale (.039625) and 0.4 minimum,
    366       *       the test below catches any font with maxZoneHeight < 10.1.
    367       *       There are library fonts ranging from 2 to 10 that get
    368       *       caught, including e.g., Eurostile LT Std Medium with
    369       *       maxZoneHeight of 6.
    370       *
    371       */
    372 #if 0
    373      if ( blueScale < .4 / maxZoneHeight )
    374      {
    375        tetraphilia_assert( 0 );
    376        /* clamp at minimum scale, per bug 0613448 fix */
    377        blueScale = .4 / maxZoneHeight;
    378      }
    379 #endif
    380 
    381    }
    382 
    383    /*
    384     * Suppress overshoot and boost blue zones at small sizes.  Boost
    385     * amount varies linearly from 0.5 pixel near 0 to 0 pixel at
    386     * blueScale cutoff.
    387     * Note: This boost amount is different from the coretype heuristic.
    388     *
    389     */
    390 
    391    if ( blues->scale < blues->blueScale )
    392    {
    393      blues->suppressOvershoot = TRUE;
    394 
    395      /* Change rounding threshold for `dsFlatEdge'.                    */
    396      /* Note: constant changed from 0.5 to 0.6 to avoid a problem with */
    397      /*       10ppem Arial                                             */
    398 
    399      blues->boost = cf2_doubleToFixed( .6 ) -
    400                       FT_MulDiv( cf2_doubleToFixed ( .6 ),
    401                                  blues->scale,
    402                                  blues->blueScale );
    403      if ( blues->boost > 0x7FFF )
    404      {
    405        /* boost must remain less than 0.5, or baseline could go negative */
    406        blues->boost = 0x7FFF;
    407      }
    408    }
    409 
    410    /* boost and darkening have similar effects; don't do both */
    411    if ( font->stemDarkened )
    412      blues->boost = 0;
    413 
    414    /* set device space alignment for each zone;    */
    415    /* apply boost amount before rounding flat edge */
    416 
    417    for ( i = 0; i < blues->count; i++ )
    418    {
    419      if ( blues->zone[i].bottomZone )
    420        blues->zone[i].dsFlatEdge = cf2_fixedRound(
    421                                      FT_MulFix(
    422                                        blues->zone[i].csFlatEdge,
    423                                        blues->scale ) -
    424                                      blues->boost );
    425      else
    426        blues->zone[i].dsFlatEdge = cf2_fixedRound(
    427                                      FT_MulFix(
    428                                        blues->zone[i].csFlatEdge,
    429                                        blues->scale ) +
    430                                      blues->boost );
    431    }
    432  }
    433 
    434 
    435  /*
    436   * Check whether `stemHint' is captured by one of the blue zones.
    437   *
    438   * Zero, one or both edges may be valid; only valid edges can be
    439   * captured.  For compatibility with CoolType, search top and bottom
    440   * zones in the same pass (see `BlueLock').  If a hint is captured,
    441   * return true and position the edge(s) in one of 3 ways:
    442   *
    443   * 1) If `BlueScale' suppresses overshoot, position the captured edge
    444   *    at the flat edge of the zone.
    445   * 2) If overshoot is not suppressed and `BlueShift' requires
    446   *    overshoot, position the captured edge a minimum of 1 device pixel
    447   *    from the flat edge.
    448   * 3) If overshoot is not suppressed or required, position the captured
    449   *    edge at the nearest device pixel.
    450   *
    451   */
    452  FT_LOCAL_DEF( FT_Bool )
    453  cf2_blues_capture( const CF2_Blues  blues,
    454                     CF2_Hint         bottomHintEdge,
    455                     CF2_Hint         topHintEdge )
    456  {
    457    /* TODO: validate? */
    458    CF2_Fixed  csFuzz = blues->blueFuzz;
    459 
    460    /* new position of captured edge */
    461    CF2_Fixed  dsNew;
    462 
    463    /* amount that hint is moved when positioned */
    464    CF2_Fixed  dsMove = 0;
    465 
    466    FT_Bool   captured = FALSE;
    467    CF2_UInt  i;
    468 
    469 
    470    /* assert edge flags are consistent */
    471    FT_ASSERT( !cf2_hint_isTop( bottomHintEdge ) &&
    472               !cf2_hint_isBottom( topHintEdge ) );
    473 
    474    /* TODO: search once without blue fuzz for compatibility with coretype? */
    475    for ( i = 0; i < blues->count; i++ )
    476    {
    477      if ( blues->zone[i].bottomZone           &&
    478           cf2_hint_isBottom( bottomHintEdge ) )
    479      {
    480        if ( SUB_INT32( blues->zone[i].csBottomEdge, csFuzz ) <=
    481               bottomHintEdge->csCoord                           &&
    482             bottomHintEdge->csCoord <=
    483               ADD_INT32( blues->zone[i].csTopEdge, csFuzz )     )
    484        {
    485          /* bottom edge captured by bottom zone */
    486 
    487          if ( blues->suppressOvershoot )
    488            dsNew = blues->zone[i].dsFlatEdge;
    489 
    490          else if ( SUB_INT32( blues->zone[i].csTopEdge,
    491                               bottomHintEdge->csCoord ) >=
    492                      blues->blueShift )
    493          {
    494            /* guarantee minimum of 1 pixel overshoot */
    495            dsNew = FT_MIN(
    496                      cf2_fixedRound( bottomHintEdge->dsCoord ),
    497                      SUB_INT32( blues->zone[i].dsFlatEdge,
    498                                 cf2_intToFixed( 1 ) ) );
    499          }
    500 
    501          else
    502          {
    503            /* simply round captured edge */
    504            dsNew = cf2_fixedRound( bottomHintEdge->dsCoord );
    505          }
    506 
    507          dsMove   = SUB_INT32( dsNew, bottomHintEdge->dsCoord );
    508          captured = TRUE;
    509 
    510          break;
    511        }
    512      }
    513 
    514      if ( !blues->zone[i].bottomZone && cf2_hint_isTop( topHintEdge ) )
    515      {
    516        if ( SUB_INT32( blues->zone[i].csBottomEdge, csFuzz ) <=
    517               topHintEdge->csCoord                              &&
    518             topHintEdge->csCoord <=
    519               ADD_INT32( blues->zone[i].csTopEdge, csFuzz )     )
    520        {
    521          /* top edge captured by top zone */
    522 
    523          if ( blues->suppressOvershoot )
    524            dsNew = blues->zone[i].dsFlatEdge;
    525 
    526          else if ( SUB_INT32( topHintEdge->csCoord,
    527                               blues->zone[i].csBottomEdge ) >=
    528                      blues->blueShift )
    529          {
    530            /* guarantee minimum of 1 pixel overshoot */
    531            dsNew = FT_MAX(
    532                      cf2_fixedRound( topHintEdge->dsCoord ),
    533                      blues->zone[i].dsFlatEdge + cf2_intToFixed( 1 ) );
    534          }
    535 
    536          else
    537          {
    538            /* simply round captured edge */
    539            dsNew = cf2_fixedRound( topHintEdge->dsCoord );
    540          }
    541 
    542          dsMove   = SUB_INT32( dsNew, topHintEdge->dsCoord );
    543          captured = TRUE;
    544 
    545          break;
    546        }
    547      }
    548    }
    549 
    550    if ( captured )
    551    {
    552      /* move both edges and flag them `locked' */
    553      if ( cf2_hint_isValid( bottomHintEdge ) )
    554      {
    555        bottomHintEdge->dsCoord = ADD_INT32( bottomHintEdge->dsCoord,
    556                                             dsMove );
    557        cf2_hint_lock( bottomHintEdge );
    558      }
    559 
    560      if ( cf2_hint_isValid( topHintEdge ) )
    561      {
    562        topHintEdge->dsCoord = ADD_INT32( topHintEdge->dsCoord, dsMove );
    563        cf2_hint_lock( topHintEdge );
    564      }
    565    }
    566 
    567    return captured;
    568  }
    569 
    570 
    571 /* END */