avm2: Implement TextField.getParagraphLength

This commit is contained in:
Kamil Jarosz 2024-07-18 13:38:10 +02:00 committed by Nathan Adams
parent 670e5f9ac2
commit fe82a23b61
3 changed files with 49 additions and 4 deletions

View File

@ -164,10 +164,7 @@ package flash.text {
public native function getLineIndexOfChar(charIndex:int):int; public native function getLineIndexOfChar(charIndex:int):int;
public function getParagraphLength(charIndex:int):int { public native function getParagraphLength(charIndex:int):int;
stub_method("flash.text.TextField", "getParagraphLength");
return 0;
}
public static function isFontCompatible(fontName:String, fontStyle:String):Boolean { public static function isFontCompatible(fontName:String, fontStyle:String):Boolean {
stub_method("flash.text.TextField", "isFontCompatible"); stub_method("flash.text.TextField", "isFontCompatible");

View File

@ -1626,3 +1626,27 @@ pub fn get_first_char_in_paragraph<'gc>(
.unwrap_or(-1) .unwrap_or(-1)
.into()) .into())
} }
pub fn get_paragraph_length<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
args: &[Value<'gc>],
) -> Result<Value<'gc>, 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())
}

View File

@ -1965,6 +1965,30 @@ impl<'gc> EditText<'gc> {
Some(index) Some(index)
} }
pub fn paragraph_length_at(self, mut index: usize) -> Option<usize> {
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( fn execute_avm1_asfunction(
self, self,
context: &mut UpdateContext<'_, 'gc>, context: &mut UpdateContext<'_, 'gc>,