22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . IO ;
5+ using System . Linq ;
56using System . Numerics ;
67using Pchp . Core ;
78using Pchp . Library . Streams ;
@@ -767,7 +768,7 @@ static Font CreateFontById(int fontInd)
767768 // TODO: cache statically
768769
769770 // Get the first available of specified sans serif system fonts
770- var result = SystemFonts . TryFind ( "Consolas" , out var fontFamily ) || SystemFonts . TryFind ( "Lucida Console" , out fontFamily ) || SystemFonts . TryFind ( "Arial" , out fontFamily ) || SystemFonts . TryFind ( "Verdana" , out fontFamily ) || SystemFonts . TryFind ( "Tahoma" , out fontFamily ) ;
771+ var result = SystemFonts . TryGet ( "Consolas" , out var fontFamily ) || SystemFonts . TryGet ( "Lucida Console" , out fontFamily ) || SystemFonts . TryGet ( "Arial" , out fontFamily ) || SystemFonts . TryGet ( "Verdana" , out fontFamily ) || SystemFonts . TryGet ( "Tahoma" , out fontFamily ) ;
771772
772773 // Couldn't find the system font.
773774 if ( ! result )
@@ -777,7 +778,7 @@ static Font CreateFontById(int fontInd)
777778 var fontStyle = FontStyle . Regular ;
778779 if ( fontInd == 3 || fontInd >= 5 )
779780 {
780- if ( fontFamily . IsStyleAvailable ( FontStyle . Bold ) )
781+ if ( fontFamily . TryGetMetrics ( FontStyle . Bold , out _ ) )
781782 {
782783 fontStyle = FontStyle . Bold ;
783784 }
@@ -811,12 +812,7 @@ static Font CreateFontByFontFile(Context ctx, string font_file, double size)
811812
812813 try
813814 {
814- family = new FontCollection ( ) . Install ( font_stream . RawStream ) ; // TODO: perf: global font collection cache
815-
816- if ( family == null )
817- {
818- throw new InvalidDataException ( ) ;
819- }
815+ family = new FontCollection ( ) . Add ( font_stream . RawStream ) ; // TODO: perf: global font collection cache
820816 }
821817 catch
822818 {
@@ -830,19 +826,19 @@ static Font CreateFontByFontFile(Context ctx, string font_file, double size)
830826
831827 FontStyle style ;
832828
833- if ( family . IsStyleAvailable ( FontStyle . Regular ) )
829+ if ( family . TryGetMetrics ( FontStyle . Regular , out _ ) )
834830 {
835831 style = FontStyle . Regular ;
836832 }
837- else if ( family . IsStyleAvailable ( FontStyle . Bold ) )
833+ else if ( family . TryGetMetrics ( FontStyle . Bold , out _ ) )
838834 {
839835 style = FontStyle . Bold ;
840836 }
841- else if ( family . IsStyleAvailable ( FontStyle . Italic ) )
837+ else if ( family . TryGetMetrics ( FontStyle . Italic , out _ ) )
842838 {
843839 style = FontStyle . Italic ;
844840 }
845- else if ( family . IsStyleAvailable ( FontStyle . BoldItalic ) )
841+ else if ( family . TryGetMetrics ( FontStyle . BoldItalic , out _ ) )
846842 {
847843 style = FontStyle . BoldItalic ;
848844 }
@@ -1034,7 +1030,7 @@ public static bool imagerectangle(PhpResource im, int x1, int y1, int x2, int y2
10341030
10351031 var rect = new RectangleF ( x1 , y1 , x2 - x1 , y2 - y1 ) ;
10361032
1037- var opt = new ShapeGraphicsOptions ( ) ;
1033+ var opt = new DrawingOptions ( ) ;
10381034 opt . GraphicsOptions . Antialias = img . AntiAlias ;
10391035
10401036 img . Image . Mutate ( o => o . Draw ( opt , FromRGBA ( col ) , 1.0f , rect ) ) ;
@@ -1109,8 +1105,8 @@ public static bool imagesettile(PhpResource image, PhpResource tile)
11091105 return null ;
11101106 }
11111107
1112- var rendererOptions = new RendererOptions ( font ) ;
1113- var textsize = TextMeasurer . Measure ( text , rendererOptions ) ;
1108+ var rendererOptions = new TextOptions ( font ) ;
1109+ var textsize = TextMeasurer . MeasureSize ( text , rendererOptions ) ;
11141110
11151111 // text transformation:
11161112 var matrix = ( angle == 0.0 ) ? Matrix3x2 . Identity : Matrix3x2 . CreateRotation ( ( float ) ( angle * - 2.0 * Math . PI / 360.0f ) ) ;
@@ -1213,9 +1209,11 @@ public static bool imageline(PhpResource im, int x1, int y1, int x2, int y2, int
12131209 var img = PhpGdImageResource . ValidImage ( im ) ;
12141210 if ( img != null )
12151211 {
1216- var opt = new ShapeGraphicsOptions ( ) ;
1212+ var opt = new DrawingOptions ( ) ;
12171213 opt . GraphicsOptions . Antialias = img . AntiAlias ;
1218- img . Image . Mutate ( o => o . DrawLines ( opt , GetAlphaColor ( img , color ) , 1.0f , new PointF [ ] { new PointF ( x1 , y1 ) , new PointF ( x2 , y2 ) } ) ) ;
1214+ img . Image . Mutate (
1215+ o => o . DrawLine ( opt , GetAlphaColor ( img , color ) , 1.0f , new PointF [ ] { new PointF ( x1 , y1 ) , new PointF ( x2 , y2 ) } )
1216+ ) ;
12191217
12201218 return true ;
12211219 }
@@ -1518,7 +1516,7 @@ public static bool imageellipse(PhpResource im, int cx, int cy, int w, int h, lo
15181516
15191517 var ellipse = new EllipsePolygon ( cx , cy , w , h ) ;
15201518
1521- var opt = new ShapeGraphicsOptions ( ) ;
1519+ var opt = new DrawingOptions ( ) ;
15221520 opt . GraphicsOptions . Antialias = img . AntiAlias ;
15231521
15241522 img . Image . Mutate ( o => o . Draw ( opt , GetAlphaColor ( img , col ) , 1.0f , ellipse ) ) ;
@@ -1536,7 +1534,7 @@ public static bool imagefilledellipse(PhpResource im, int cx, int cy, int w, int
15361534 return false ;
15371535
15381536 var ellipse = new EllipsePolygon ( cx , cy , w , h ) ;
1539- var opt = new ShapeGraphicsOptions ( ) ;
1537+ var opt = new DrawingOptions ( ) ;
15401538 opt . GraphicsOptions . Antialias = img . AntiAlias ;
15411539
15421540 if ( img . tiled != null )
@@ -1674,17 +1672,19 @@ public static int imagecolorresolvealpha(PhpResource im, int red, int green, int
16741672
16751673 static bool DrawText ( PhpResource im , int fontInd , int x , int y , string text , long col , bool up = false )
16761674 {
1677- PhpGdImageResource img = PhpGdImageResource . ValidImage ( im ) ;
1675+ var img = PhpGdImageResource . ValidImage ( im ) ;
16781676 if ( img == null )
1677+ {
16791678 return false ;
1679+ }
16801680
16811681 if ( x < 0 || y < 0 ) return true ;
16821682 if ( x > img . Image . Width || y > img . Image . Height ) return true ;
16831683
16841684 var font = CreateFontById ( fontInd ) ;
16851685 var color = FromRGBA ( col ) ;
16861686
1687- var opt = new TextGraphicsOptions ( ) ;
1687+ var opt = new DrawingOptions ( ) ;
16881688 opt . GraphicsOptions . Antialias = img . AntiAlias ;
16891689
16901690 if ( up )
@@ -1732,7 +1732,7 @@ static FontRectangle imagefontsize(int fontInd)
17321732 var font = CreateFontById ( fontInd ) ;
17331733 if ( font != null )
17341734 {
1735- var size = TextMeasurer . Measure ( "X" , new RendererOptions ( font ) ) ;
1735+ var size = TextMeasurer . MeasureSize ( "X" , new TextOptions ( font ) ) ;
17361736
17371737 if ( arr == null || arr . Length <= fontInd )
17381738 {
@@ -1823,9 +1823,10 @@ public static bool imagefilledarc(PhpResource im, long cx, long cy, long w, long
18231823 AdjustAnglesAndSize ( ref w , ref h , ref s , ref e , ref range ) ;
18241824
18251825 // Path Builder object to be used in all the branches
1826- PathBuilder pathBuilder = new PathBuilder ( ) ;
1826+ var pathBuilder = new PathBuilder ( ) ;
18271827 var color = FromRGBA ( col ) ;
1828- var pen = new Pen ( color , 1 ) ;
1828+
1829+ var pen = new SolidPen ( color , 1 ) ;
18291830
18301831 // edge points, used for both pie and chord
18311832 PointF startingPoint = new PointF ( cx + ( int ) ( Math . Cos ( s * Math . PI / 180 ) * ( w / 2.0 ) ) , cy + ( int ) ( Math . Sin ( s * Math . PI / 180 ) * ( h / 2.0 ) ) ) ;
@@ -2073,7 +2074,7 @@ public static bool imageopenpolygon(PhpResource image, PhpArray points, int num_
20732074
20742075 var pointsF = GetPointFsFromArray ( points , num_points ) ;
20752076
2076- img . Image . Mutate ( o => o . DrawLines ( new Pen ( FromRGBA ( color ) , 1.0f ) , pointsF ) ) ;
2077+ img . Image . Mutate ( o => o . DrawLine ( new SolidPen ( FromRGBA ( color ) , 1.0f ) , pointsF ) ) ;
20772078
20782079 return true ;
20792080 }
@@ -2107,29 +2108,19 @@ static bool Polygon(PhpResource im, PhpArray point, int num_points, long col, bo
21072108
21082109 if ( filled )
21092110 {
2110- IBrush brush ;
2111-
2112- switch ( col )
2111+ var brush = col switch
21132112 {
2114- case ( long ) ColorValues . TILED :
2115- brush = img . tiled ;
2116- break ;
2117- case ( long ) ColorValues . STYLED :
2118- brush = img . styled ;
2119- break ;
2120- case ( long ) ColorValues . BRUSHED :
2121- brush = img . brushed ;
2122- break ;
2123- default :
2124- brush = new SolidBrush ( FromRGBA ( col ) ) ;
2125- break ;
2126- }
2113+ ( long ) ColorValues . TILED => img . tiled ,
2114+ ( long ) ColorValues . STYLED => img . styled ,
2115+ ( long ) ColorValues . BRUSHED => img . brushed ,
2116+ _ => new SolidBrush ( FromRGBA ( col ) )
2117+ } ;
21272118
21282119 img . Image . Mutate ( o => o . FillPolygon ( brush , points ) ) ;
21292120 }
21302121 else
21312122 {
2132- img . Image . Mutate ( o => o . DrawPolygon ( new Pen ( FromRGBA ( col ) , 1.0f ) , points ) ) ;
2123+ img . Image . Mutate ( o => o . DrawPolygon ( new SolidPen ( FromRGBA ( col ) , 1.0f ) , points ) ) ;
21332124 }
21342125
21352126 return true ;
0 commit comments