diff --git a/core/src/font.rs b/core/src/font.rs index 772cec67f3ee..7ea80af863fc 100644 --- a/core/src/font.rs +++ b/core/src/font.rs @@ -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 } @@ -744,7 +748,8 @@ 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); @@ -752,7 +757,7 @@ pub trait FontLike<'gc> { 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); diff --git a/tests/tests/swfs/fonts/device_font_kerning/Test.as b/tests/tests/swfs/fonts/device_font_kerning/Test.as new file mode 100644 index 000000000000..019b5a3b6405 --- /dev/null +++ b/tests/tests/swfs/fonts/device_font_kerning/Test.as @@ -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); + } +} +} diff --git a/tests/tests/swfs/fonts/device_font_kerning/TestFont.sfd b/tests/tests/swfs/fonts/device_font_kerning/TestFont.sfd new file mode 100644 index 000000000000..f8b19489f53a --- /dev/null +++ b/tests/tests/swfs/fonts/device_font_kerning/TestFont.sfd @@ -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 diff --git a/tests/tests/swfs/fonts/device_font_kerning/TestFont.ttf b/tests/tests/swfs/fonts/device_font_kerning/TestFont.ttf new file mode 100644 index 000000000000..fe29929c4c87 Binary files /dev/null and b/tests/tests/swfs/fonts/device_font_kerning/TestFont.ttf differ diff --git a/tests/tests/swfs/fonts/device_font_kerning/output.expected.png b/tests/tests/swfs/fonts/device_font_kerning/output.expected.png new file mode 100644 index 000000000000..f738a5720db3 Binary files /dev/null and b/tests/tests/swfs/fonts/device_font_kerning/output.expected.png differ diff --git a/tests/tests/swfs/fonts/device_font_kerning/output.txt b/tests/tests/swfs/fonts/device_font_kerning/output.txt new file mode 100644 index 000000000000..4b091dca9069 --- /dev/null +++ b/tests/tests/swfs/fonts/device_font_kerning/output.txt @@ -0,0 +1,4 @@ +LM width: 50 +width: 50 +LM width: 50 +width: 50 diff --git a/tests/tests/swfs/fonts/device_font_kerning/test.swf b/tests/tests/swfs/fonts/device_font_kerning/test.swf new file mode 100644 index 000000000000..771c231c7133 Binary files /dev/null and b/tests/tests/swfs/fonts/device_font_kerning/test.swf differ diff --git a/tests/tests/swfs/fonts/device_font_kerning/test.toml b/tests/tests/swfs/fonts/device_font_kerning/test.toml new file mode 100644 index 000000000000..95576d823eba --- /dev/null +++ b/tests/tests/swfs/fonts/device_font_kerning/test.toml @@ -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"