Skip to content
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
9 changes: 7 additions & 2 deletions core/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ pub enum FontType {
}

impl FontType {
pub fn is_device(self) -> bool {
self == Self::Device
}

pub fn is_embedded(self) -> bool {
self != Self::Device
}
Expand Down Expand Up @@ -744,15 +748,16 @@ pub trait FontLike<'gc> {
// TODO [KJ] I'm not sure whether we should iterate over characters here or over code units.
// I suspect Flash Player does not support full UTF-16 when displaying and laying out text.
let mut char_indices = text.char_indices().peekable();
let has_kerning_info = self.has_kerning_info();
let kerning_enabled =
self.has_kerning_info() && (self.font_type().is_device() || params.kerning);
let mut x = Twips::ZERO;
while let Some((pos, c)) = char_indices.next() {
let c = c.unwrap_or(char::REPLACEMENT_CHARACTER);
if let Some(render_data) = self.get_glyph_render_data(c) {
let glyph = render_data.glyph;
let scale = params.height.get() as f32 / render_data.font.scale();
let mut advance = glyph.advance();
if has_kerning_info && params.kerning {
if kerning_enabled {
let next_char = char_indices.peek().cloned().unwrap_or((0, Ok('\0'))).1;
let next_char = next_char.unwrap_or(char::REPLACEMENT_CHARACTER);
advance += self.get_kerning_offset(c, next_char);
Expand Down
39 changes: 39 additions & 0 deletions tests/tests/swfs/fonts/device_font_kerning/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package {
import flash.display.*;
import flash.text.*;

[SWF(width="100", height="50")]
public class Test extends Sprite {
[Embed(source="TestFont.ttf", fontName="TestFont", embedAsCFF="false", unicodeRange="U+0061-U+0064")]
private var TestFont:Class;

public function Test() {
stage.scaleMode = "noScale";

addTextField(0, 0, false);
addTextField(0, 25, true);
}

private function addTextField(x:Number, y:Number, kerning:Boolean):void {
var field:TextField = new TextField();
field.type = "input";
field.width = 100;
field.x = x;
field.height = 25;
field.y = y;
field.border = true;

var tf:TextFormat = new TextFormat("TestFont", 10);
tf.kerning = kerning;
field.defaultTextFormat = tf;

field.text = "abcd";

var lm = field.getLineMetrics(0);
trace("LM width: " + lm.width);
trace("width: " + field.textWidth);

addChild(field);
}
}
}
126 changes: 126 additions & 0 deletions tests/tests/swfs/fonts/device_font_kerning/TestFont.sfd
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
SplineFontDB: 3.2
FontName: TestFont
FullName: TestFont
FamilyName: TestFont
Weight: Regular
Copyright: Copyright (c) 2025, Kamil Jarosz
UComments: "2024-7-24: Created with FontForge (http://fontforge.org)"
Version: 001.000
ItalicAngle: 0
UnderlinePosition: -76
UnderlineWidth: 38
Ascent: 800
Descent: 200
InvalidEm: 0
LayerCount: 2
Layer: 0 0 "Back" 1
Layer: 1 0 "Fore" 0
XUID: [1021 253 198287149 6396829]
StyleMap: 0x0000
FSType: 0
OS2Version: 0
OS2_WeightWidthSlopeOnly: 0
OS2_UseTypoMetrics: 1
CreationTime: 1721856925
ModificationTime: 1763285453
PfmFamily: 17
TTFWeight: 400
TTFWidth: 5
LineGap: 100
VLineGap: 0
OS2TypoAscent: 0
OS2TypoAOffset: 1
OS2TypoDescent: 0
OS2TypoDOffset: 1
OS2TypoLinegap: 100
OS2WinAscent: 0
OS2WinAOffset: 1
OS2WinDescent: 0
OS2WinDOffset: 1
HheadAscent: 0
HheadAOffset: 1
HheadDescent: 0
HheadDOffset: 1
OS2Vendor: 'PfEd'
Lookup: 258 0 0 "'kern' Horizontal Kerning in Latin lookup 0" { "'kern' Horizontal Kerning in Latin lookup 0-1" [150,15,0] } ['kern' ('latn' <'dflt' > ) ]
MarkAttachClasses: 1
DEI: 91125
Encoding: ISO8859-1
UnicodeInterp: none
NameList: AGL For New Fonts
DisplaySize: -48
AntiAlias: 1
FitToEm: 0
WinInfo: 0 30 10
BeginPrivate: 0
EndPrivate
BeginChars: 256 4

StartChar: a
Encoding: 97 97 0
Width: 1000
Flags: W
HStem: 0 800<0 1000>
LayerCount: 2
Fore
SplineSet
0 800 m 1
1000 800 l 5
1000 0 l 5
0 0 l 1
0 800 l 1
EndSplineSet
Kerns2: 2 1000 "'kern' Horizontal Kerning in Latin lookup 0-1"
EndChar

StartChar: c
Encoding: 99 99 1
Width: 1000
Flags: W
HStem: 0 800<0 1000>
LayerCount: 2
Fore
SplineSet
0 800 m 1
1000 800 l 5
1000 0 l 5
0 0 l 1
0 800 l 1
EndSplineSet
Kerns2: 3 500 "'kern' Horizontal Kerning in Latin lookup 0-1"
EndChar

StartChar: b
Encoding: 98 98 2
Width: 1000
Flags: W
HStem: 0 800<0 1000>
LayerCount: 2
Fore
SplineSet
0 800 m 1
1000 800 l 5
1000 0 l 5
0 0 l 1
0 800 l 1
EndSplineSet
Kerns2: 1 -500 "'kern' Horizontal Kerning in Latin lookup 0-1"
EndChar

StartChar: d
Encoding: 100 100 3
Width: 1000
Flags: W
HStem: 0 800<0 1000>
LayerCount: 2
Fore
SplineSet
0 800 m 1
1000 800 l 5
1000 0 l 5
0 0 l 1
0 800 l 1
EndSplineSet
EndChar
EndChars
EndSplineFont
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/tests/swfs/fonts/device_font_kerning/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LM width: 50
width: 50
LM width: 50
width: 50
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/tests/swfs/fonts/device_font_kerning/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
num_ticks = 1

[image_comparisons.output]
tolerance = 0

[player_options]
with_renderer = { optional = false, sample_count = 4 }

[fonts.a]
family = "TestFont"
path = "TestFont.ttf"
Loading