diff --git a/CHANGELOG.md b/CHANGELOG.md
index b33c756f..f7a0f0f5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased (next)]
### Added
+- Add `text`, `width` and `height` members to `ObjectShape::Text`. (#278)
- Implement `ResourceReader` for appropiate functions. (#272) **Read the README's FAQ for more information about this change.**
+## Fixed
+- `ObjectShape::Text::kerning`'s default value, which should have been set to `true` instead of `false`. (#278)
+
## [Unreleased]
## Changed
- Updated `Image` docs. (#270)
diff --git a/assets/tiled_text_object.tmx b/assets/tiled_text_object.tmx
new file mode 100644
index 00000000..9a97304d
--- /dev/null
+++ b/assets/tiled_text_object.tmx
@@ -0,0 +1,8 @@
+
+
diff --git a/src/objects.rs b/src/objects.rs
index db3ed16c..89e7a943 100644
--- a/src/objects.rs
+++ b/src/objects.rs
@@ -147,6 +147,10 @@ pub enum ObjectShape {
kerning: bool,
halign: HorizontalAlignment,
valign: VerticalAlignment,
+ /// The actual text content of this object.
+ text: String,
+ width: f32,
+ height: f32,
},
}
@@ -306,7 +310,7 @@ impl ObjectData {
Ok(())
},
"text" => |attrs| {
- shape = Some(ObjectData::new_text(attrs)?);
+ shape = Some(ObjectData::new_text(attrs, parser, width, height)?);
Ok(())
},
"properties" => |_| {
@@ -365,7 +369,12 @@ impl ObjectData {
Ok(ObjectShape::Polygon { points })
}
- fn new_text(attrs: Vec) -> Result {
+ fn new_text(
+ attrs: Vec,
+ parser: &mut impl Iterator- ,
+ width: f32,
+ height: f32,
+ ) -> Result {
let (
font_family,
pixel_size,
@@ -388,7 +397,7 @@ impl ObjectData {
Some("italic") => italic ?= v.parse(),
Some("underline") => underline ?= v.parse(),
Some("strikeout") => strikeout ?= v.parse(),
- Some("kerning") => kerning ?= v.parse(),
+ Some("kerning") => kerning ?= v.parse::(),
Some("halign") => halign = match v.as_str() {
"left" => HorizontalAlignment::Left,
"center" => HorizontalAlignment::Center,
@@ -433,9 +442,20 @@ impl ObjectData {
let italic = italic == Some(1);
let underline = underline == Some(1);
let strikeout = strikeout == Some(1);
- let kerning = kerning == Some(1);
+ let kerning = kerning.map_or(true, |k| k == 1);
let halign = halign.unwrap_or_default();
let valign = valign.unwrap_or_default();
+ let contents = match parser.next().map_or_else(
+ || {
+ Err(Error::PrematureEnd(
+ "XML stream ended when trying to parse text contents".to_owned(),
+ ))
+ },
+ |r| r.map_err(Error::XmlDecodingError),
+ )? {
+ xml::reader::XmlEvent::Characters(contents) => contents,
+ _ => panic!("Text attribute contained anything but characters as content"),
+ };
Ok(ObjectShape::Text {
font_family,
@@ -449,6 +469,9 @@ impl ObjectData {
kerning,
halign,
valign,
+ text: contents,
+ width,
+ height,
})
}
diff --git a/tests/lib.rs b/tests/lib.rs
index 585f1e93..c8c4a258 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -1,7 +1,8 @@
use std::path::PathBuf;
use tiled::{
- Color, FiniteTileLayer, GroupLayer, Layer, LayerType, Loader, Map, ObjectLayer, ObjectShape,
- PropertyValue, ResourceCache, TileLayer, TilesetLocation, WangId,
+ Color, FiniteTileLayer, GroupLayer, HorizontalAlignment, Layer, LayerType, Loader, Map,
+ ObjectLayer, ObjectShape, PropertyValue, ResourceCache, TileLayer, TilesetLocation,
+ VerticalAlignment, WangId,
};
fn as_finite<'map>(data: TileLayer<'map>) -> FiniteTileLayer<'map> {
@@ -495,3 +496,53 @@ fn test_reading_wang_sets() {
let damage_value = &PropertyValue::FloatValue(32.1);
assert_eq!(readed_damage, damage_value);
}
+
+#[test]
+fn test_text_object() {
+ let mut loader = Loader::new();
+ let map = loader.load_tmx_map("assets/tiled_text_object.tmx").unwrap();
+
+ let group = map.get_layer(0).unwrap().as_object_layer().unwrap();
+ match &group.objects().next().unwrap().shape {
+ ObjectShape::Text {
+ font_family,
+ pixel_size,
+ wrap,
+ color,
+ bold,
+ italic,
+ underline,
+ strikeout,
+ kerning,
+ halign,
+ valign,
+ text,
+ width,
+ height,
+ } => {
+ assert_eq!(font_family.as_str(), "sans-serif");
+ assert_eq!(*pixel_size, 16);
+ assert_eq!(*wrap, false);
+ assert_eq!(
+ *color,
+ Color {
+ red: 85,
+ green: 255,
+ blue: 127,
+ alpha: 100
+ }
+ );
+ assert_eq!(*bold, true);
+ assert_eq!(*italic, true);
+ assert_eq!(*underline, true);
+ assert_eq!(*strikeout, true);
+ assert_eq!(*kerning, true);
+ assert_eq!(*halign, HorizontalAlignment::Center);
+ assert_eq!(*valign, VerticalAlignment::Bottom);
+ assert_eq!(text.as_str(), "Test");
+ assert_eq!(*width, 87.7188);
+ assert_eq!(*height, 21.7969);
+ }
+ _ => panic!(),
+ };
+}