Skip to content

Implement Font type #248

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

Merged
merged 23 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
16 changes: 16 additions & 0 deletions docs/binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This document is based on:
- [SharedString](#sharedstring)
- [OptionalCoordinateFrame](#optionalcoordinateframe)
- [UniqueId](#uniqueid)
- [Font](#font)
- [Data Storage Notes](#data-storage-notes)
- [Integer Transformations](#integer-transformations)
- [Byte Interleaving](#byte-interleaving)
Expand Down Expand Up @@ -639,6 +640,21 @@ When interacting with the XML format, care must be taken because `UniqueId` is s

When an array of `UniqueId` values is present, the bytes are subject to [byte interleaving](#byte-interleaving).

### Font
**Type ID `0x20`**

The `Font` type is a struct composed of two `String` values, a `u8`, and a `u16`:

| Field Name | Format | Value |
| :----------- | :------------------ | :------------------------------ |
| Family | [`String`](#string) | The font family content url |
| Weight | `u16` | The weight of the font |
| Style | `u8` | The style of the font |
| CachedFaceId | [`String`](#string) | The cached content url of the ttf file |

The `Weight` and `Style` fields are stored as little-endian unsigned integers. These are usually treated like enums, and to assign them in Roblox Studio an Enum is used. Interestingly, the `Weight` is _always_ stored as a number in binary and xml, but `Style` is stored as a number in binary and as text in xml.

The `CachedFaceId` field is always present, but is allows to be an empty string (a string of length 0). When represented in xml, this property will be omitted if it is an empty string. This property is not visible via any user APIs in Roblox Studio.

## Data Storage Notes

Expand Down
8 changes: 5 additions & 3 deletions generate_reflection/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::collections::BTreeMap;

use rbx_dom_weak::types::{
Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence,
ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, Faces, Matrix3, NumberRange,
NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Region3int16, Tags,
UDim, UDim2, Variant, VariantType, Vector2, Vector2int16, Vector3, Vector3int16,
ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, Faces, Font, Matrix3,
NumberRange, NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect,
Region3int16, Tags, UDim, UDim2, Variant, VariantType, Vector2, Vector2int16, Vector3,
Vector3int16,
};
use serde::Serialize;

Expand Down Expand Up @@ -77,6 +78,7 @@ pub fn encode() -> anyhow::Result<String> {
values.insert("Faces", Faces::all().into());
values.insert("Float32", 15.0f32.into());
values.insert("Float64", 15123.0f64.into());
values.insert("Font", Font::default().into());
values.insert("Int32", 6014i32.into());
values.insert("Int64", 23491023i64.into());
values.insert("NumberRange", NumberRange::new(-36.0, 94.0).into());
Expand Down
38 changes: 37 additions & 1 deletion rbx_binary/src/deserializer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use rbx_dom_weak::{
types::{
Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence,
ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, Faces, Matrix3,
ColorSequenceKeypoint, Content, CustomPhysicalProperties, Enum, Faces, Font, Matrix3,
NumberRange, NumberSequence, NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Ref,
SharedString, Tags, UDim, UDim2, Variant, VariantType, Vector2, Vector3, Vector3int16,
},
Expand Down Expand Up @@ -863,6 +863,42 @@ impl<'a, R: Read> DeserializerState<'a, R> {
});
}
},
Type::Font => match canonical_type {
VariantType::Font => {
for referent in &type_info.referents {
let instance = self.instances_by_ref.get_mut(referent).unwrap();

let family = chunk.read_string()?;
let weight = chunk.read_le_u16()?.into();
let style = chunk.read_u8()?.into();
let cached_face_id = chunk.read_string()?;

let cached_face_id = if cached_face_id.is_empty() {
None
} else {
Some(cached_face_id)
};

instance.builder.add_property(
&canonical_name,
Font {
family,
weight,
style,
cached_face_id,
},
);
}
}
invalid_type => {
return Err(InnerError::PropTypeMismatch {
type_name: type_info.type_name.clone(),
prop_name,
valid_type_names: "Font",
actual_type_name: format!("{:?}", invalid_type),
});
}
},
Type::NumberSequence => match canonical_type {
VariantType::NumberSequence => {
for referent in &type_info.referents {
Expand Down
18 changes: 17 additions & 1 deletion rbx_binary/src/serializer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use rbx_dom_weak::{
types::{
Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence,
ColorSequenceKeypoint, Content, Enum, Faces, Matrix3, NumberRange, NumberSequence,
ColorSequenceKeypoint, Content, Enum, Faces, Font, Matrix3, NumberRange, NumberSequence,
NumberSequenceKeypoint, PhysicalProperties, Ray, Rect, Ref, SharedString, Tags, UDim,
UDim2, Variant, VariantType, Vector2, Vector3, Vector3int16,
},
Expand Down Expand Up @@ -709,6 +709,21 @@ impl<'dom, W: Write> SerializerState<'dom, W> {
chunk.write_interleaved_i32_array(offset_x.into_iter())?;
chunk.write_interleaved_i32_array(offset_y.into_iter())?;
}
Type::Font => {
for (i, rbx_value) in values {
if let Variant::Font(value) = rbx_value.as_ref() {
chunk.write_string(&value.family)?;
chunk.write_le_u16(value.weight.clone().into())?;
chunk.write_u8(value.style.clone().into())?;
match &value.cached_face_id {
Some(id) => chunk.write_string(id)?,
None => chunk.write_string("")?,
}
} else {
return type_mismatch(i, &rbx_value, "Font");
}
}
}
Type::Ray => {
for (i, rbx_value) in values {
if let Variant::Ray(value) = rbx_value.as_ref() {
Expand Down Expand Up @@ -1229,6 +1244,7 @@ impl<'dom, W: Write> SerializerState<'dom, W> {
VariantType::Tags => Variant::Tags(Tags::new()),
VariantType::Content => Variant::Content(Content::new()),
VariantType::Attributes => Variant::Attributes(Attributes::new()),
VariantType::Font => Variant::Font(Font::default()),
_ => return None,
})
}
Expand Down
1 change: 1 addition & 0 deletions rbx_binary/src/tests/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ binary_tests! {
two_terrainregions,
weldconstraint,
package_link,
text_label_with_font,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
source: rbx_binary/src/tests/util.rs
assertion_line: 33
expression: decoded_viewed
---
- referent: referent-0
name: TextLabel
class: TextLabel
properties:
Active:
Bool: false
AnchorPoint:
Vector2:
- 0
- 0
Attributes:
Attributes: {}
AutoLocalize:
Bool: true
AutomaticSize:
Enum: 0
BackgroundColor3:
Color3:
- 1
- 1
- 1
BackgroundTransparency:
Float32: 0
BorderColor3:
Color3:
- 0.10588236
- 0.16470589
- 0.20784315
BorderMode:
Enum: 0
BorderSizePixel:
Int32: 1
ClipsDescendants:
Bool: false
Draggable:
Bool: false
FontFace:
Font:
family: "rbxasset://fonts/families/RobotoMono.json"
weight: bold
style: italic
cachedFaceId: ~
LayoutOrder:
Int32: 0
LineHeight:
Float32: 1
MaxVisibleGraphemes:
Int32: -1
NextSelectionDown: "null"
NextSelectionLeft: "null"
NextSelectionRight: "null"
NextSelectionUp: "null"
Position:
UDim2:
- - 0
- 0
- - 0
- 0
RichText:
Bool: false
RootLocalizationTable: "null"
Rotation:
Float32: 0
Selectable:
Bool: false
SelectionBehaviorDown:
Enum: 0
SelectionBehaviorLeft:
Enum: 0
SelectionBehaviorRight:
Enum: 0
SelectionBehaviorUp:
Enum: 0
SelectionGroup:
Bool: false
SelectionImageObject: "null"
SelectionOrder:
Int32: 0
Size:
UDim2:
- - 0
- 200
- - 0
- 50
SizeConstraint:
Enum: 0
SourceAssetId:
Int64: -1
Tags:
Tags: []
Text:
String: My Text
TextColor3:
Color3:
- 0
- 0
- 0
TextScaled:
Bool: false
TextSize:
Float32: 14
TextStrokeColor3:
Color3:
- 0
- 0
- 0
TextStrokeTransparency:
Float32: 1
TextTransparency:
Float32: 0
TextTruncate:
Enum: 0
TextWrapped:
Bool: false
TextXAlignment:
Enum: 2
TextYAlignment:
Enum: 1
Visible:
Bool: true
ZIndex:
Int32: 1
children: []

Loading