@@ -144,6 +144,72 @@ static bool try_handle_register_font(Message *message, Context *ctx)
144144 return true;
145145}
146146
147+ static bool try_handle_measure_text (Message * message , Context * ctx )
148+ {
149+ GenMessage gen_message ;
150+ if (UNLIKELY (port_parse_gen_message (message -> message ,
151+ & gen_message ) != GenCallMessage )) {
152+ return false;
153+ }
154+
155+ term req = gen_message .req ;
156+ if (UNLIKELY (!term_is_tuple (req ) || term_get_tuple_arity (req ) < 3 )) {
157+ return false;
158+ }
159+ term cmd = term_get_tuple_element (req , 0 );
160+
161+ if (cmd != globalcontext_make_atom (ctx -> global ,
162+ "\xC" "measure_text" )) {
163+ return false;
164+ }
165+
166+ char * handle = interop_atom_to_string (ctx ,
167+ term_get_tuple_element (req , 1 ));
168+ EpdFont * loaded_font = NULL ;
169+ if (handle != NULL ) {
170+ loaded_font = ufont_manager_find_by_handle (ufont_manager , handle );
171+ free (handle );
172+ }
173+
174+ term text_bin = term_get_tuple_element (req , 2 );
175+ size_t text_len = term_binary_size (text_bin );
176+ char * text = malloc (text_len + 1 );
177+ if (text == NULL ) {
178+ BEGIN_WITH_STACK_HEAP (TUPLE_SIZE (2 ) + REF_SIZE , heap );
179+ term return_tuple = term_alloc_tuple (2 , & heap );
180+ term_put_tuple_element (return_tuple , 0 , gen_message .ref );
181+ term_put_tuple_element (return_tuple , 1 , ERROR_ATOM );
182+ display_message_send (gen_message .pid , return_tuple , ctx -> global );
183+ END_WITH_STACK_HEAP (heap , ctx -> global );
184+ return true;
185+ }
186+ memcpy (text , term_binary_data (text_bin ), text_len );
187+ text [text_len ] = '\0' ;
188+
189+ int width = 0 ;
190+ int height = 0 ;
191+ if (loaded_font != NULL ) {
192+ EpdFontProperties props = epd_font_properties_default ();
193+ EpdRect rect = epd_get_string_rect (loaded_font , text , 0 , 0 , 0 , & props );
194+ width = rect .width ;
195+ height = rect .height ;
196+ }
197+ free (text );
198+
199+ BEGIN_WITH_STACK_HEAP (TUPLE_SIZE (3 ) + TUPLE_SIZE (2 ) + REF_SIZE , heap );
200+ term result = term_alloc_tuple (3 , & heap );
201+ term_put_tuple_element (result , 0 , OK_ATOM );
202+ term_put_tuple_element (result , 1 , term_from_int (width ));
203+ term_put_tuple_element (result , 2 , term_from_int (height ));
204+ term return_tuple = term_alloc_tuple (2 , & heap );
205+ term_put_tuple_element (return_tuple , 0 , gen_message .ref );
206+ term_put_tuple_element (return_tuple , 1 , result );
207+ display_message_send (gen_message .pid , return_tuple , ctx -> global );
208+ END_WITH_STACK_HEAP (heap , ctx -> global );
209+
210+ return true;
211+ }
212+
147213void display_task_process_messages (void * arg )
148214{
149215 struct DisplayTaskArgs * args = arg ;
@@ -154,7 +220,8 @@ void display_task_process_messages(void *arg)
154220 Message * message ;
155221 xQueueReceive (args -> messages_queue , & message , portMAX_DELAY );
156222
157- if (!try_handle_register_font (message , args -> ctx )) {
223+ if (!try_handle_register_font (message , args -> ctx )
224+ && !try_handle_measure_text (message , args -> ctx )) {
158225 args -> process_message_fn (message , args -> ctx );
159226 }
160227
0 commit comments