diff --git a/changelog.d/registered-japanese-outlines.md b/changelog.d/registered-japanese-outlines.md new file mode 100644 index 000000000..3e5d42e10 --- /dev/null +++ b/changelog.d/registered-japanese-outlines.md @@ -0,0 +1 @@ +fix: **Registered Japanese outlines**: Larger registered-font glyphs such as `鬱` now render instead of falling back to blocks. diff --git a/src/primitives/canvas/font_ttf.zig b/src/primitives/canvas/font_ttf.zig index de7f88ee8..056f486f8 100644 --- a/src/primitives/canvas/font_ttf.zig +++ b/src/primitives/canvas/font_ttf.zig @@ -9,10 +9,10 @@ //! shaping, no CFF. The deterministic estimator (`text_metrics.zig`) //! derives its advance table from this face's `cmap`/`hmtx`, so layout //! measures with exactly the advances these outlines are inked at. -//! All parsing is bounds-checked against fixed budgets sized from -//! the bundled face's `maxp` (96 points / 16 contours per simple glyph, -//! composites well under both), and any glyph beyond the budget fails -//! with a recoverable error so callers can fall back to block glyphs. +//! All parsing is bounds-checked against fixed budgets sized from the +//! bundled and registered-face fixtures (237 points / 26 contours per +//! simple glyph), and any glyph beyond the budget fails with a recoverable +//! error so callers can fall back to block glyphs. //! //! The bundled face is embedded at comptime and its table directory is //! validated at comptime: a corrupt bundle is a compile error, and the @@ -32,8 +32,11 @@ pub const Error = error{ FontGlyphTooComplex, } || @import("vector.zig").Error; -pub const max_glyph_points: usize = 128; -pub const max_glyph_contours: usize = 24; +pub const max_glyph_points: usize = 237; +pub const max_glyph_contours: usize = 26; +/// A simple glyph emits at most one segment per point plus one move and +/// one close for each contour. +pub const max_simple_glyph_path_elements: usize = max_glyph_points + 2 * max_glyph_contours; pub const max_composite_depth: usize = 4; pub const max_composite_components: usize = 8; diff --git a/src/primitives/canvas/font_ttf_tests.zig b/src/primitives/canvas/font_ttf_tests.zig index b1f23d1ca..6070852a2 100644 --- a/src/primitives/canvas/font_ttf_tests.zig +++ b/src/primitives/canvas/font_ttf_tests.zig @@ -11,6 +11,7 @@ const drawing = @import("drawing.zig"); const PointF = geometry.PointF; const Affine = drawing.Affine; +const japanese_boundary_bytes = @embedFile("testdata/noto-japanese-boundary/NotoSansJP-japanese-boundary.ttf"); const grid_size: usize = 24; @@ -203,6 +204,29 @@ test "mono outlines rasterize within the vector budgets" { } } +test "registered Japanese outlines render at the parser boundary" { + const face = try font_ttf.Face.parse(japanese_boundary_bytes); + const expected = [_]u21{ 'ば', 'ぱ', 'ぼ', 'ぽ', 'ゑ', '鬱' }; + var max_emitted: usize = 0; + for (expected) |codepoint| { + const glyph = face.glyphIndex(codepoint); + try std.testing.expect(glyph != 0); + var builder = vector.PathBuilder(font_ttf.max_simple_glyph_path_elements){}; + try face.glyphOutline(glyph, Affine.identity(), &builder); + try std.testing.expect(builder.slice().len > 0); + max_emitted = @max(max_emitted, builder.slice().len); + } + try std.testing.expectEqual(@as(usize, 243), max_emitted); + try std.testing.expectEqual(@as(usize, 289), font_ttf.max_simple_glyph_path_elements); + + var mapped: usize = 0; + var codepoint: u21 = 0; + while (codepoint <= 0xFFFF) : (codepoint += 1) { + if (face.glyphIndex(codepoint) != 0) mapped += 1; + } + try std.testing.expectEqual(expected.len, mapped); +} + test "corrupt font bytes fail to parse without crashing" { try std.testing.expectError(error.FontParseFailed, font_ttf.Face.parse(&.{})); try std.testing.expectError(error.FontParseFailed, font_ttf.Face.parse(font_ttf.geist_regular_bytes[0..64])); diff --git a/src/primitives/canvas/reference.zig b/src/primitives/canvas/reference.zig index 862b9eda4..3330d1ce2 100644 --- a/src/primitives/canvas/reference.zig +++ b/src/primitives/canvas/reference.zig @@ -44,9 +44,8 @@ const render_fingerprints = @import("render_fingerprints.zig"); const vector = @import("vector.zig"); const font_ttf = @import("font_ttf.zig"); -/// Element budget for one glyph outline: the bundled face's densest -/// glyphs stay well under this (maxp: 96 points per simple glyph). -const reference_glyph_path_capacity: usize = 256; +/// Element budget for every simple glyph accepted by the bounded parser. +const reference_glyph_path_capacity: usize = font_ttf.max_simple_glyph_path_elements; const referenceBlurKernel = reference_blur.referenceBlurKernel; const referenceBlurSampleWithKernel = reference_blur.referenceBlurSampleWithKernel; @@ -1074,7 +1073,6 @@ fn referenceScaleRect(rect: geometry.RectF, scale: f32) geometry.RectF { return geometry.RectF.init(rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale); } - fn referencePixelCenter(x: usize, y: usize) geometry.PointF { return geometry.PointF.init(@as(f32, @floatFromInt(x)) + 0.5, @as(f32, @floatFromInt(y)) + 0.5); } diff --git a/src/primitives/canvas/testdata/noto-japanese-boundary/NotoSansJP-japanese-boundary.ttf b/src/primitives/canvas/testdata/noto-japanese-boundary/NotoSansJP-japanese-boundary.ttf new file mode 100644 index 000000000..752c45b65 Binary files /dev/null and b/src/primitives/canvas/testdata/noto-japanese-boundary/NotoSansJP-japanese-boundary.ttf differ diff --git a/src/primitives/canvas/testdata/noto-japanese-boundary/OFL.txt b/src/primitives/canvas/testdata/noto-japanese-boundary/OFL.txt new file mode 100644 index 000000000..89b1c616c --- /dev/null +++ b/src/primitives/canvas/testdata/noto-japanese-boundary/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2014-2021 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source' + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font Software, +subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/src/primitives/canvas/testdata/noto-japanese-boundary/README.md b/src/primitives/canvas/testdata/noto-japanese-boundary/README.md new file mode 100644 index 000000000..9c178d8e0 --- /dev/null +++ b/src/primitives/canvas/testdata/noto-japanese-boundary/README.md @@ -0,0 +1,18 @@ +# Noto Sans JP Japanese outline boundary fixture + +`NotoSansJP-japanese-boundary.ttf` is a Modified Version of Noto Sans JP 2.004-H2 containing five kana glyphs and the common-use kanji `鬱`. The fixture exercises Native SDK's registered-font bounds up to 237 points and 26 contours. The complete SIL Open Font License is in `OFL.txt`. + +- Source commit: `google/fonts@295d98a7a0c17c68f1341eaeea354e7960ea70d3` +- Source file SHA-256: `c2f3b4d463500a2ddcd3849cded1fceeb9fd6d1c32e6cbecd568453ba50fc68f` +- Static Regular SHA-256: `946280470c7f8dff9c7256a10c6fb06544c75e83553e906a1a0ad946211de7ed` +- Fixture SHA-256: `8a0c8b2e78eab29b4e26615a93695df8cc5eaae08ef981e517dc207069d9f308` +- Generator: FontTools 4.63.0 + +```sh +workdir=$(mktemp -d) +trap 'rm -rf "$workdir"' EXIT +curl -fsSL 'https://raw.githubusercontent.com/google/fonts/295d98a7a0c17c68f1341eaeea354e7960ea70d3/ofl/notosansjp/NotoSansJP%5Bwght%5D.ttf' -o "$workdir/NotoSansJP-wght.ttf" +export SOURCE_DATE_EPOCH=1784100876 +fonttools varLib.instancer "$workdir/NotoSansJP-wght.ttf" wght=400 --update-name-table --output="$workdir/NotoSansJP-Regular-400.ttf" +pyftsubset "$workdir/NotoSansJP-Regular-400.ttf" '--unicodes=U+3070,U+3071,U+307C,U+307D,U+3091,U+9B31' --output-file=NotoSansJP-japanese-boundary.ttf '--name-IDs=*' --name-legacy '--name-languages=*' +```