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

Add content to text shape, fix kerning default value #278

Merged
merged 4 commits into from
Sep 1, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions assets/tiled_text_object.tmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="1" height="1" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="2">
<objectgroup id="2" name="Object Layer 1">
<object id="1" x="-24.1094" y="-2.39844" width="87.7188" height="21.7969">
<text color="#6455ff7f" bold="1" italic="1" underline="1" strikeout="1" halign="center" valign="bottom">Test</text>
</object>
</objectgroup>
</map>
31 changes: 27 additions & 4 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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" => |_| {
Expand Down Expand Up @@ -365,7 +369,12 @@ impl ObjectData {
Ok(ObjectShape::Polygon { points })
}

fn new_text(attrs: Vec<OwnedAttribute>) -> Result<ObjectShape> {
fn new_text(
attrs: Vec<OwnedAttribute>,
parser: &mut impl Iterator<Item = XmlEventResult>,
width: f32,
height: f32,
) -> Result<ObjectShape> {
let (
font_family,
pixel_size,
Expand All @@ -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::<i32>(),
Some("halign") => halign = match v.as_str() {
"left" => HorizontalAlignment::Left,
"center" => HorizontalAlignment::Center,
Expand Down Expand Up @@ -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,
Expand All @@ -449,6 +469,9 @@ impl ObjectData {
kerning,
halign,
valign,
text: contents,
width,
height,
})
}

Expand Down
55 changes: 53 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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!(),
};
}
Loading