1 /****************************************************************************
2  *
3  * ftsynth.c
4  *
5  *   FreeType synthesizing code for emboldening and slanting (body).
6  *
7  * Copyright (C) 2000-2020 by
8  * David Turner, Robert Wilhelm, and Werner Lemberg.
9  *
10  * This file is part of the FreeType project, and may only be used,
11  * modified, and distributed under the terms of the FreeType project
12  * license, LICENSE.TXT.  By continuing to use, modify, or distribute
13  * this file you indicate that you have read the license and
14  * understand and accept it fully.
15  *
16  */
17 
18 
19 #include <freetype/ftsynth.h>
20 #include <freetype/internal/ftdebug.h>
21 #include <freetype/internal/ftobjs.h>
22 #include <freetype/ftoutln.h>
23 #include <freetype/ftbitmap.h>
24 
25 
26   /**************************************************************************
27    *
28    * The macro FT_COMPONENT is used in trace mode.  It is an implicit
29    * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
30    * messages during execution.
31    */
32 #undef  FT_COMPONENT
33 #define FT_COMPONENT  synth
34 
35 
36   /*************************************************************************/
37   /*************************************************************************/
38   /****                                                                 ****/
39   /****   EXPERIMENTAL OBLIQUING SUPPORT                                ****/
40   /****                                                                 ****/
41   /*************************************************************************/
42   /*************************************************************************/
43 
44   /* documentation is in ftsynth.h */
45 
46   FT_EXPORT_DEF( void )
FT_GlyphSlot_Oblique(FT_GlyphSlot slot)47   FT_GlyphSlot_Oblique( FT_GlyphSlot  slot )
48   {
49     FT_Matrix    transform;
50     FT_Outline*  outline;
51 
52 
53     if ( !slot )
54       return;
55 
56     outline = &slot->outline;
57 
58     /* only oblique outline glyphs */
59     if ( slot->format != FT_GLYPH_FORMAT_OUTLINE )
60       return;
61 
62     /* we don't touch the advance width */
63 
64     /* For italic, simply apply a shear transform, with an angle */
65     /* of about 12 degrees.                                      */
66 
67     transform.xx = 0x10000L;
68     transform.yx = 0x00000L;
69 
70     transform.xy = 0x0366AL;
71     transform.yy = 0x10000L;
72 
73     FT_Outline_Transform( outline, &transform );
74   }
75 
76 
77   /*************************************************************************/
78   /*************************************************************************/
79   /****                                                                 ****/
80   /****   EXPERIMENTAL EMBOLDENING SUPPORT                              ****/
81   /****                                                                 ****/
82   /*************************************************************************/
83   /*************************************************************************/
84 
85 
86   /* documentation is in ftsynth.h */
87 
88   FT_EXPORT_DEF( void )
FT_GlyphSlot_Embolden(FT_GlyphSlot slot)89   FT_GlyphSlot_Embolden( FT_GlyphSlot  slot )
90   {
91     FT_Library  library;
92     FT_Face     face;
93     FT_Error    error;
94     FT_Pos      xstr, ystr;
95 
96 
97     if ( !slot )
98       return;
99 
100     library = slot->library;
101     face    = slot->face;
102 
103     if ( slot->format != FT_GLYPH_FORMAT_OUTLINE &&
104          slot->format != FT_GLYPH_FORMAT_BITMAP  )
105       return;
106 
107     /* some reasonable strength */
108     xstr = FT_MulFix( face->units_per_EM,
109                       face->size->metrics.y_scale ) / 24;
110     ystr = xstr;
111 
112     if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
113       FT_Outline_EmboldenXY( &slot->outline, xstr, ystr );
114 
115     else /* slot->format == FT_GLYPH_FORMAT_BITMAP */
116     {
117       /* round to full pixels */
118       xstr &= ~63;
119       if ( xstr == 0 )
120         xstr = 1 << 6;
121       ystr &= ~63;
122 
123       /*
124        * XXX: overflow check for 16-bit system, for compatibility
125        *      with FT_GlyphSlot_Embolden() since FreeType 2.1.10.
126        *      unfortunately, this function return no informations
127        *      about the cause of error.
128        */
129       if ( ( ystr >> 6 ) > FT_INT_MAX || ( ystr >> 6 ) < FT_INT_MIN )
130       {
131         FT_TRACE1(( "FT_GlyphSlot_Embolden:" ));
132         FT_TRACE1(( "too strong emboldening parameter ystr=%ld\n", ystr ));
133         return;
134       }
135       error = FT_GlyphSlot_Own_Bitmap( slot );
136       if ( error )
137         return;
138 
139       error = FT_Bitmap_Embolden( library, &slot->bitmap, xstr, ystr );
140       if ( error )
141         return;
142     }
143 
144     if ( slot->advance.x )
145       slot->advance.x += xstr;
146 
147     if ( slot->advance.y )
148       slot->advance.y += ystr;
149 
150     slot->metrics.width        += xstr;
151     slot->metrics.height       += ystr;
152     slot->metrics.horiAdvance  += xstr;
153     slot->metrics.vertAdvance  += ystr;
154     slot->metrics.horiBearingY += ystr;
155 
156     /* XXX: 16-bit overflow case must be excluded before here */
157     if ( slot->format == FT_GLYPH_FORMAT_BITMAP )
158       slot->bitmap_top += (FT_Int)( ystr >> 6 );
159   }
160 
161 
162 /* END */
163