@@ -2212,6 +2212,50 @@ FormElement* rewrite_attack_info(LetElement* in, const Env& env, FormPool& pool)
22122212 return elt;
22132213}
22142214
2215+ FormElement* rewrite_set_font_single (LetElement* in,
2216+ const Env& env,
2217+ FormPool& pool,
2218+ const char * deref_name,
2219+ const char * op_name,
2220+ bool cast_to_float) {
2221+ /*
2222+ (let ((v1-10 gp-0))
2223+ (set! (-> v1-10 scale) 0.6)
2224+ )
2225+ */
2226+
2227+ if (in->entries ().size () != 1 ) {
2228+ return nullptr ;
2229+ }
2230+ if (in->body ()->elts ().size () != 1 ) {
2231+ return nullptr ;
2232+ }
2233+
2234+ Form* font_obj_expr = in->entries ().at (0 ).src ;
2235+ RegisterAccess font_obj_reg = in->entries ().at (0 ).dest ;
2236+
2237+ if (env.get_variable_type (font_obj_reg, true ) != TypeSpec (" font-context" )) {
2238+ return nullptr ;
2239+ }
2240+
2241+ auto src_matcher =
2242+ cast_to_float ? Matcher::numeric_cast (" float" , Matcher::any (0 )) : Matcher::any (0 );
2243+ Matcher set_matcher = Matcher::set (Matcher::deref (Matcher::reg (font_obj_reg.reg ()), false ,
2244+ {DerefTokenMatcher::string (deref_name)}),
2245+ src_matcher);
2246+ auto set_mr = match (set_matcher, in->body ()->at (0 ));
2247+
2248+ if (!set_mr.matched ) {
2249+ return nullptr ;
2250+ }
2251+
2252+ auto elt = pool.alloc_element <GenericElement>(
2253+ GenericOperator::make_function (pool.form <ConstantTokenElement>(op_name)),
2254+ std::vector<Form*>{font_obj_expr, set_mr.maps .forms .at (0 )});
2255+ elt->parent_form = in->parent_form ;
2256+ return elt;
2257+ }
2258+
22152259/* !
22162260 * Attempt to rewrite a let as another form. If it cannot be rewritten, this will return nullptr.
22172261 */
@@ -2320,6 +2364,24 @@ FormElement* rewrite_let(LetElement* in, const Env& env, FormPool& pool, LetRewr
23202364 return as_call_parent_state;
23212365 }
23222366
2367+ auto as_font_scale = rewrite_set_font_single (in, env, pool, " scale" , " set-scale!" , false );
2368+ if (as_font_scale) {
2369+ stats.font_method ++;
2370+ return as_font_scale;
2371+ }
2372+
2373+ auto as_font_width = rewrite_set_font_single (in, env, pool, " width" , " set-width!" , true );
2374+ if (as_font_width) {
2375+ stats.font_method ++;
2376+ return as_font_width;
2377+ }
2378+
2379+ auto as_font_height = rewrite_set_font_single (in, env, pool, " height" , " set-height!" , true );
2380+ if (as_font_height) {
2381+ stats.font_method ++;
2382+ return as_font_height;
2383+ }
2384+
23232385 // nothing matched.
23242386 return nullptr ;
23252387}
@@ -2687,6 +2749,59 @@ FormElement* rewrite_with_dma_buf_add_bucket(LetElement* in, const Env& env, For
26872749 return elt;
26882750}
26892751
2752+ FormElement* rewrite_set_font_origin (LetElement* in, const Env& env, FormPool& pool) {
2753+ /*
2754+ (let ((v1-9 gp-0)
2755+ (a1-1 36)
2756+ (a0-4 140)
2757+ )
2758+ (set! (-> v1-9 origin x) (the float a1-1))
2759+ (set! (-> v1-9 origin y) (the float a0-4))
2760+ )
2761+ */
2762+ if (in->entries ().size () != 3 ) {
2763+ return nullptr ;
2764+ }
2765+ if (in->body ()->elts ().size () != 2 ) {
2766+ return nullptr ;
2767+ }
2768+
2769+ Form* font_obj_expr = in->entries ().at (0 ).src ;
2770+ RegisterAccess font_obj_reg = in->entries ().at (0 ).dest ;
2771+
2772+ if (env.get_variable_type (font_obj_reg, true ) != TypeSpec (" font-context" )) {
2773+ return nullptr ;
2774+ }
2775+
2776+ Form* x_val = in->entries ().at (1 ).src ;
2777+ Form* y_val = in->entries ().at (2 ).src ;
2778+
2779+ Matcher x_matcher = Matcher::set (
2780+ Matcher::deref (Matcher::reg (font_obj_reg.reg ()), false ,
2781+ {DerefTokenMatcher::string (" origin" ), DerefTokenMatcher::string (" x" )}),
2782+ Matcher::numeric_cast (" float" , Matcher::reg (in->entries ().at (1 ).dest .reg ())));
2783+ auto x_mr = match (x_matcher, in->body ()->at (0 ));
2784+
2785+ Matcher y_matcher = Matcher::set (
2786+ Matcher::deref (Matcher::reg (font_obj_reg.reg ()), false ,
2787+ {DerefTokenMatcher::string (" origin" ), DerefTokenMatcher::string (" y" )}),
2788+ Matcher::numeric_cast (" float" , Matcher::reg (in->entries ().at (2 ).dest .reg ())));
2789+ auto y_mr = match (y_matcher, in->body ()->at (1 ));
2790+
2791+ if (!x_mr.matched ) {
2792+ return nullptr ;
2793+ }
2794+ if (!y_mr.matched ) {
2795+ return nullptr ;
2796+ }
2797+
2798+ auto elt = pool.alloc_element <GenericElement>(
2799+ GenericOperator::make_function (pool.form <ConstantTokenElement>(" set-origin!" )),
2800+ std::vector<Form*>{font_obj_expr, x_val, y_val});
2801+ elt->parent_form = in->parent_form ;
2802+ return elt;
2803+ }
2804+
26902805FormElement* rewrite_launch_particles (LetElement* in, const Env& env, FormPool& pool) {
26912806 /*
26922807 * (let ((t9-0 sp-launch-particles-var)
@@ -2846,6 +2961,12 @@ FormElement* rewrite_multi_let(LetElement* in,
28462961 }
28472962 }
28482963
2964+ auto as_font_set_origin = rewrite_set_font_origin (in, env, pool);
2965+ if (as_font_set_origin) {
2966+ stats.font_method ++;
2967+ return as_font_set_origin;
2968+ }
2969+
28492970 return in;
28502971}
28512972
0 commit comments