From fe82a23b61b4c50c64333cae5d1cfb7e9f8c5608 Mon Sep 17 00:00:00 2001 From: Kamil Jarosz Date: Thu, 18 Jul 2024 13:38:10 +0200 Subject: [PATCH] avm2: Implement TextField.getParagraphLength --- core/src/avm2/globals/flash/text/TextField.as | 5 +--- .../src/avm2/globals/flash/text/text_field.rs | 24 +++++++++++++++++++ core/src/display_object/edit_text.rs | 24 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/core/src/avm2/globals/flash/text/TextField.as b/core/src/avm2/globals/flash/text/TextField.as index 339c4f69a..df43e4661 100644 --- a/core/src/avm2/globals/flash/text/TextField.as +++ b/core/src/avm2/globals/flash/text/TextField.as @@ -164,10 +164,7 @@ package flash.text { public native function getLineIndexOfChar(charIndex:int):int; - public function getParagraphLength(charIndex:int):int { - stub_method("flash.text.TextField", "getParagraphLength"); - return 0; - } + public native function getParagraphLength(charIndex:int):int; public static function isFontCompatible(fontName:String, fontStyle:String):Boolean { stub_method("flash.text.TextField", "isFontCompatible"); diff --git a/core/src/avm2/globals/flash/text/text_field.rs b/core/src/avm2/globals/flash/text/text_field.rs index 45023cbd4..aca628d1f 100644 --- a/core/src/avm2/globals/flash/text/text_field.rs +++ b/core/src/avm2/globals/flash/text/text_field.rs @@ -1626,3 +1626,27 @@ pub fn get_first_char_in_paragraph<'gc>( .unwrap_or(-1) .into()) } + +pub fn get_paragraph_length<'gc>( + activation: &mut Activation<'_, 'gc>, + this: Object<'gc>, + args: &[Value<'gc>], +) -> Result, Error<'gc>> { + let Some(this) = this + .as_display_object() + .and_then(|this| this.as_edit_text()) + else { + return Ok(Value::Undefined); + }; + + let char_index = args.get_i32(activation, 0)?; + if char_index < 0 { + return Ok((-1).into()); + } + + Ok(this + .paragraph_length_at(char_index as usize) + .map(|i| i as i32) + .unwrap_or(-1) + .into()) +} diff --git a/core/src/display_object/edit_text.rs b/core/src/display_object/edit_text.rs index c1347cb7d..7940377a8 100644 --- a/core/src/display_object/edit_text.rs +++ b/core/src/display_object/edit_text.rs @@ -1965,6 +1965,30 @@ impl<'gc> EditText<'gc> { Some(index) } + pub fn paragraph_length_at(self, mut index: usize) -> Option { + let start_index = self.paragraph_start_index_at(index)?; + let text = self.text(); + let length = text.len(); + + // When the index is equal to the text length, + // FP simulates a character at that point and returns + // the length of the last paragraph plus one. + if index == length { + return Some(1 + length - start_index); + } + + while index < length && !string_utils::swf_is_newline(text.at(index)) { + index += 1; + } + + // The trailing newline also counts to the length + if index < length && string_utils::swf_is_newline(text.at(index)) { + index += 1; + } + + Some(index - start_index) + } + fn execute_avm1_asfunction( self, context: &mut UpdateContext<'_, 'gc>,