Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm2: Implement TextField.getLineIndexOfChar() #16906

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions core/src/avm2/globals/flash/text/TextField.as
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ package flash.text {
return 0;
}

public function getLineIndexOfChar(charIndex:int):int {
stub_method("flash.text.TextField", "getLineIndexOfChar");
return 0;
}
public native function getLineIndexOfChar(charIndex:int):int;

public function getParagraphLength(charIndex:int):int {
stub_method("flash.text.TextField", "getParagraphLength");
Expand Down
33 changes: 29 additions & 4 deletions core/src/avm2/globals/flash/text/text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,11 +1164,11 @@ pub fn get_line_length<'gc>(
return Err(make_error_2006(activation));
}

return if let Some(length) = this.line_length(line_num as usize) {
if let Some(length) = this.line_length(line_num as usize) {
Ok(length.into())
} else {
Err(make_error_2006(activation))
};
}
}

pub fn get_line_text<'gc>(
Expand Down Expand Up @@ -1208,11 +1208,11 @@ pub fn get_line_offset<'gc>(
return Err(make_error_2006(activation));
}

return if let Some(offset) = this.line_offset(line_num as usize) {
if let Some(offset) = this.line_offset(line_num as usize) {
Ok(offset.into())
} else {
Err(make_error_2006(activation))
};
}
}

pub fn get_bottom_scroll_v<'gc>(
Expand Down Expand Up @@ -1444,3 +1444,28 @@ pub fn get_selected_text<'gc>(
}
Ok("".into())
}

pub fn get_line_index_of_char<'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 index = args.get_i32(activation, 0)?;
if index < 0 {
// Docs say "throw RangeError", reality says "return -1".
return Ok(Value::Number(-1f64));
}

if let Some(line) = this.line_index_of_char(index as usize) {
Ok(line.into())
} else {
Ok(Value::Number(-1f64))
}
}
4 changes: 4 additions & 0 deletions core/src/display_object/edit_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,10 @@ impl<'gc> EditText<'gc> {
Some(first_box.start())
}

pub fn line_index_of_char(self, index: usize) -> Option<usize> {
self.0.read().layout.find_line_index_by_position(index)
}

fn execute_avm1_asfunction(
self,
context: &mut UpdateContext<'_, 'gc>,
Expand Down
15 changes: 14 additions & 1 deletion core/src/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::tag_utils::SwfMovie;
use crate::DefaultFont;
use gc_arena::Collect;
use ruffle_render::shape_utils::DrawCommand;
use std::cmp::{max, min};
use std::cmp::{max, min, Ordering};
use std::fmt::{Debug, Formatter};
use std::mem;
use std::ops::{Deref, Range};
Expand Down Expand Up @@ -773,6 +773,19 @@ impl<'gc> Layout<'gc> {
boxes_iter: None,
}
}

pub fn find_line_index_by_position(&self, position: usize) -> Option<usize> {
let result = self.lines.binary_search_by(|probe| {
if probe.end <= position {
Ordering::Less
} else if position < probe.start {
Ordering::Greater
} else {
Ordering::Equal
}
});
result.ok()
}
}

/// A `LayoutLine` represents a single line of text.
Expand Down
35 changes: 35 additions & 0 deletions tests/tests/swfs/avm2/edittext_get_line_index_of_char/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package {
import flash.display.Sprite;
import flash.text.TextField;

public class Test extends Sprite {
private var text1: TextField;
private var text2: TextField;
private var text3: TextField;

public function Test() {
text1 = new TextField();
text1.text = "line 1\n line2\n\n line 4\n";
text2 = new TextField();
text2.htmlText = "<p>line 1 </p><br/><li>line 2 </li>\n\n<li> line 4</li>";
text3 = new TextField();
text3.wordWrap = true;
text3.width = 50;
text3.text = "first second\nthird";

addChild(text1);
addChild(text2);
addChild(text3);
printValues("text1", text1, 25);
printValues("text2", text2, 25);
printValues("text3", text3, 20);
}

private function printValues(name:String, text:TextField, len: int):void {
trace("Field: " + name);
for (var i = -1; i < len; ++i) {
trace(" " + name + ".getLineIndexOfChar(" + i + ") = " + text.getLineIndexOfChar(i));
}
}
}
}
76 changes: 76 additions & 0 deletions tests/tests/swfs/avm2/edittext_get_line_index_of_char/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Field: text1
text1.getLineIndexOfChar(-1) = -1
text1.getLineIndexOfChar(0) = 0
text1.getLineIndexOfChar(1) = 0
text1.getLineIndexOfChar(2) = 0
text1.getLineIndexOfChar(3) = 0
text1.getLineIndexOfChar(4) = 0
text1.getLineIndexOfChar(5) = 0
text1.getLineIndexOfChar(6) = 0
text1.getLineIndexOfChar(7) = 1
text1.getLineIndexOfChar(8) = 1
text1.getLineIndexOfChar(9) = 1
text1.getLineIndexOfChar(10) = 1
text1.getLineIndexOfChar(11) = 1
text1.getLineIndexOfChar(12) = 1
text1.getLineIndexOfChar(13) = 1
text1.getLineIndexOfChar(14) = 2
text1.getLineIndexOfChar(15) = 3
text1.getLineIndexOfChar(16) = 3
text1.getLineIndexOfChar(17) = 3
text1.getLineIndexOfChar(18) = 3
text1.getLineIndexOfChar(19) = 3
text1.getLineIndexOfChar(20) = 3
text1.getLineIndexOfChar(21) = 3
text1.getLineIndexOfChar(22) = 3
text1.getLineIndexOfChar(23) = -1
text1.getLineIndexOfChar(24) = -1
Field: text2
text2.getLineIndexOfChar(-1) = -1
text2.getLineIndexOfChar(0) = 0
text2.getLineIndexOfChar(1) = 0
text2.getLineIndexOfChar(2) = 0
text2.getLineIndexOfChar(3) = 0
text2.getLineIndexOfChar(4) = 0
text2.getLineIndexOfChar(5) = 0
text2.getLineIndexOfChar(6) = 0
text2.getLineIndexOfChar(7) = 0
text2.getLineIndexOfChar(8) = 0
text2.getLineIndexOfChar(9) = 0
text2.getLineIndexOfChar(10) = 0
text2.getLineIndexOfChar(11) = 0
text2.getLineIndexOfChar(12) = 0
text2.getLineIndexOfChar(13) = 0
text2.getLineIndexOfChar(14) = 0
text2.getLineIndexOfChar(15) = 1
text2.getLineIndexOfChar(16) = 2
text2.getLineIndexOfChar(17) = 2
text2.getLineIndexOfChar(18) = 2
text2.getLineIndexOfChar(19) = 2
text2.getLineIndexOfChar(20) = 2
text2.getLineIndexOfChar(21) = 2
text2.getLineIndexOfChar(22) = 2
text2.getLineIndexOfChar(23) = -1
text2.getLineIndexOfChar(24) = -1
Field: text3
text3.getLineIndexOfChar(-1) = -1
text3.getLineIndexOfChar(0) = 0
text3.getLineIndexOfChar(1) = 0
text3.getLineIndexOfChar(2) = 0
text3.getLineIndexOfChar(3) = 0
text3.getLineIndexOfChar(4) = 0
text3.getLineIndexOfChar(5) = 0
text3.getLineIndexOfChar(6) = 1
text3.getLineIndexOfChar(7) = 1
text3.getLineIndexOfChar(8) = 1
text3.getLineIndexOfChar(9) = 1
text3.getLineIndexOfChar(10) = 1
text3.getLineIndexOfChar(11) = 1
text3.getLineIndexOfChar(12) = 1
text3.getLineIndexOfChar(13) = 2
text3.getLineIndexOfChar(14) = 2
text3.getLineIndexOfChar(15) = 2
text3.getLineIndexOfChar(16) = 2
text3.getLineIndexOfChar(17) = 2
text3.getLineIndexOfChar(18) = -1
text3.getLineIndexOfChar(19) = -1
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_ticks = 1