Commit Graph

81 Commits

Author SHA1 Message Date
Nigel Tao
cd38e8056d font/sfnt: make PostTable.ItalicAngle a float64
Previously, it was a float32, which obviously has 32 bits of state. Not
all of that 32 bit state space is meaningful, since NaN has multiple bit
representations. The underlying file format field (of type "Fixed" or
16.16 fixed point) is also 32 bits of state
(https://docs.microsoft.com/en-us/typography/opentype/spec/post).
Therefore, converting from 32 bit fixed point to 32 bit floating point
can be lossy. Instead, use 64 bit floating point. 53 significand bits
can losslessly represent all possible 16.16 fixed point values.

Using float64 is also arguably more Go-like, as the default type for the
ideal constant 0.5 is float64, not float32.

Change-Id: I5abe7979a020af2ac4784d6c2723ab8e39e38e34
Reviewed-on: https://go-review.googlesource.com/c/149837
Reviewed-by: Denys Smirnov <denis.smirnov.91@gmail.com>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-11-16 02:48:01 +00:00
Denys Smirnov
dd492a22e4 font/sfnt: support fonts with "true" apple version tag
OpenType specification mentions that fonts might contain Apple
version tag that is equal to "true" (see note in [1]). Currently,
the library returns a parsing error for those fonts.

CL adds Apple this version tag to the validation code and allows
fonts to omit OS/2 table, as mentioned in the TrueType spec [2].

[1] https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font

[2] https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6OS2.html

Change-Id: I13406fe97b091f24aaabf9b477df9c8fc8df883c
Reviewed-on: https://go-review.googlesource.com/c/146078
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-11-16 02:46:09 +00:00
Denys Smirnov
d9c2484c48 font/sfnt: parse and expose PostScript information
Currently the library only parses the version in PostScript table.
However use cases such as PDF document processing requires this
information to be exposed.

CL parses a minimal set of the fields from the PostScript table
and exposes it via new PostTable method.

Change-Id: Ia86eecea9f5aaf557c7e4737f2474966aa30cff2
Reviewed-on: https://go-review.googlesource.com/c/145797
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-11-15 06:11:51 +00:00
Dmitri Shuralyov
aa35264064 font/sfnt: rename ExampleRasterizeGlyph to Example_rasterizeGlyph
This change renames the example to follow correct naming convention,
as documented at https://godoc.org/testing#hdr-Examples. As a result,
it shows up in godoc.

This issue was caught and reported by vet:

	$ go vet golang.org/x/image/font/sfnt
	# golang.org/x/image/font/sfnt_test
	font/sfnt/example_test.go:19: ExampleRasterizeGlyph refers to unknown identifier: RasterizeGlyph

Fixes golang/go#28684

Change-Id: I2749c638c5f3ed15e4db0448bc7a5e2c12c056e5
Reviewed-on: https://go-review.googlesource.com/c/148576
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-09 00:22:02 +00:00
Denys Smirnov
fe2fa19765 font/sfnt: support post table version 1.0
Currently, the library produces a parsing error when the font
contains a PostScript table of version 1.0.

CL adds this version to the validation code and implements a glyph
name lookup for it.

Change-Id: Id4999d8b252e5c2d8e897f637ba31270336cfe9a
Reviewed-on: https://go-review.googlesource.com/c/146080
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-11-07 04:00:41 +00:00
Denys Smirnov
63626fb251 font/sfnt: fix hmtx table size validation
The library assumes the hmtx size to be equal to 2*nGlyph + 2*nHm,
which is a simplification of 4*nHm + 2*(nGlyph-nHm) as described
in the spec. However, fonts seen in the wild sometimes omit the
second term (left side bearings), making validation to fail.

CL fixes the validation code by allowing to omit the second term.

Fixes golang/go#28379

Change-Id: I2293e498e72f95e5fe08c2b375ea7b020d06cde3
Reviewed-on: https://go-review.googlesource.com/c/144080
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-11-02 02:16:09 +00:00
Denys Smirnov
2a2258ff36 font/sfnt: support parsing legacy fonts with OS2 table version <= 1
Library assumes that OS/2 header size is at least 96 bytes,
which is not the case for fonts with OS/2 table version <= 1.

This CL adds a version test and handles the legacy header.

Fixes golang/go#28339

Change-Id: I79bd8f8bbf262c1caaf4e66888446159b5e4fb43
Reviewed-on: https://go-review.googlesource.com/c/144079
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Elias Naur <elias.naur@gmail.com>
2018-10-25 23:34:08 +00:00
Elias Naur
991ec62608 image/font: expose caret slope
Change-Id: I775224dd3fc7e5b6c2fc5c4a7d3db83bb36d047d
Reviewed-on: https://go-review.googlesource.com/136255
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-09-26 01:56:37 +00:00
Elias Naur
e1a1ede689 font,font/sfnt: expose font x-Height and capHeight
Change-Id: I6e3e6e51c7e270e16413c23990f6df5e22cbfeb6
Reviewed-on: https://go-review.googlesource.com/135555
Run-TryBot: Elias Naur <elias.naur@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-09-20 23:51:50 +00:00
Elias Naur
cc896f830c font/sfnt,font/opentype: correct font height computation
According to https://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html
the line spacing of a font is computed as

line space = ascent - descent + linegap

Use that formula to compute the Height field of a Font's Metrics.

Change-Id: I1c44eca9f662e0aae68716cd8859670fe0b394d7
Reviewed-on: https://go-review.googlesource.com/120815
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2018-06-28 06:20:38 +00:00
Sebastien Binet
e5db4c4663 font/opentype: implement font.Face interface
This CL adds the type Face that implements the font.Face interface.
This CL also adds tests using gofont/goregular as an input font, using
github.com/golang/freetype/truetype as reference values.

Updates golang/go#22451.

Change-Id: I2a6945309331b251ec2ddec95b6e809ad10aa116
Reviewed-on: https://go-review.googlesource.com/73870
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2017-12-08 23:31:33 +00:00
Elias Naur
f7e31b4ea2 font/sfnt: add Metrics to Font
Change-Id: I4bfcf264e5ee7e4f3ddf89e289d730f230095401
Reviewed-on: https://go-review.googlesource.com/67330
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2017-10-13 01:36:00 +00:00
Nigel Tao
426cfd8eeb font/sfnt: implement hflex and hflex1.
Change-Id: I288a014a5b6c54e54ffa69b76a8b12fa7fdbc708
Reviewed-on: https://go-review.googlesource.com/43474
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-05-23 02:19:15 +00:00
Nigel Tao
d835a09709 font/sfnt: add ErrColoredGlyph.
Also add tests for the Noto proprietary fonts. Prior to this commit,
NotoColorEmoji.ttf was unsupported. It's still not well supported, but
the error message returned is now more informative.

Change-Id: I61a3301d7f2458a4b838eb1de7a73d6472e3486f
Reviewed-on: https://go-review.googlesource.com/43694
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-05-23 02:17:49 +00:00
Nigel Tao
f483456c9f font/sfnt: don't reject multiple kern subtables.
A future commit will actually use subtables past the first one, but for
now, ignore them instead of failing on their presence.

Also add tests for the DejaVu proprietary fonts. Prior to this commit,
DejaVuSans-ExtraLight.ttf was unsupported.

Change-Id: Ic78a59c5ab30e4091efa2a04b89b12cb786157db
Reviewed-on: https://go-review.googlesource.com/42192
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-05-04 00:22:41 +00:00
Nigel Tao
3210c0296b font/sfnt: support .dfont files.
Change-Id: Id7aa18c48c65586c688cee230ce87f4d88dae9b5
Reviewed-on: https://go-review.googlesource.com/40893
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-04-21 22:58:11 +00:00
Nigel Tao
7c3fafc74f font/sfnt: explicitly close glyph contours.
Change-Id: I4a59167cfe5d84f0ef6732711cca9b46a52b445c
Reviewed-on: https://go-review.googlesource.com/39930
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-04-10 07:42:48 +00:00
Nigel Tao
1de9a5bb2a font/sfnt: implement Font.Bounds.
Change-Id: I24ab4cfa74a791ebb8223b38e5d6624c74caa9f8
Reviewed-on: https://go-review.googlesource.com/39670
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-04-10 07:35:26 +00:00
Nigel Tao
84a6511894 font/sfnt: use charmap EncodeRune API.
Change-Id: I91d850d1816b43748580f6f8a8f19151472750d0
Reviewed-on: https://go-review.googlesource.com/39070
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Run-TryBot: Nigel Tao <nigeltao@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-04-05 22:33:02 +00:00
Nigel Tao
10ed294205 font/sfnt: implement Font.GlyphAdvance.
Change-Id: I3e15c6e634d858a87e73221bd9d5a9e3979d674a
Reviewed-on: https://go-review.googlesource.com/39250
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-04-05 22:26:46 +00:00
Nigel Tao
ce0faa1867 font/sfnt: flip the Y axis for LoadGlyph's Segments.
The underlying font format's Y axis increases up. The Go standard
graphics libraries' Y axis increases down. This change makes the Go API
consistent with the other Go libraries.

Also change Segment.Args from [6]fixed.Int26_6 to [3]fixed.Point26_6 to
emphasize that the Args are consistent with other fixed.Point26_6 use.

Change-Id: Idd7b89eb4d86890dea477ac2ef96ff8f6b1dee8d
Reviewed-on: https://go-review.googlesource.com/39072
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-31 23:39:16 +00:00
Nigel Tao
f36ba34967 font/sfnt: implement Font Dict Select.
Change-Id: I8c463b41421a35455701847520367add4727cbe3
Reviewed-on: https://go-review.googlesource.com/38871
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-30 23:40:04 +00:00
Nigel Tao
59d151bf14 font/sfnt: implement implicit vstem before hintmask.
Change-Id: I811bcf94b518dabcfbebd085ad3c6a47c17ef38e
Reviewed-on: https://go-review.googlesource.com/38288
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-30 01:47:42 +00:00
Nigel Tao
ecc1a9abb6 font/sfnt: fix argStack size check for moveto ops.
The *moveto ops don't take multiple args or arg pairs. For example, in
http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5177.Type2.pdf
page 16, the rmoveto args are listed as:
dx1 dy1
which is not the same as rlineto's args, which can be repeated:
{dxa dya}+

Change-Id: I5d13f686e604955909eb0b7e52f20ce5f0522c5d
Reviewed-on: https://go-review.googlesource.com/38289
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-30 01:44:25 +00:00
Nigel Tao
f03a046406 font/gofont: new Smallcaps; update other Go Fonts.
Bigelow & Holmes have prepared new versions of the Go Fonts. The version
numbers have changed from 2.004 to 2.008. ChangeLog:

* New! 2 new fonts, bringing the total to 12: Go Smallcaps and Go
  Smallcaps Italic.
* New! U+FFFD REPLACEMENT CHARACTER glyph added.
* U+00C5 LATIN CAPITAL LETTER A WITH RING ABOVE ring tweaked.
* U+00F0 LATIN SMALL LETTER ETH glyph lightened.
* Superfluous U+0005 <control> glyph removed.
* Split integral, chart draw, block, shade and related glyphs now span
  the full height and have fewer seams when tiled.
* Raised most capital letter diacritics and some lowercase diacritics.
* Mono character widths fixed to be uniform.
* Underline position adjusted.
* SFNT glyph order and cmap table optimized.
* SFNT glyph names (post table) regularized, consistent with the AGLFN.
* ttfautohint upgraded to the latest version, from 1.5 to 1.6.

Change-Id: I0985f07d58b984010199dd723ccdf5892ee59c78
Reviewed-on: https://go-review.googlesource.com/38870
Reviewed-by: Rob Pike <r@golang.org>
2017-03-30 00:58:26 +00:00
Nigel Tao
c0851fbc5b font/sfnt: support PostScript compound glyphs (subroutines).
Change-Id: I8aa10150aa004b1bc1128bf0b3d5c14b74ee089c
Reviewed-on: https://go-review.googlesource.com/38280
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-22 22:20:00 +00:00
Nigel Tao
2e35bd52b4 font/sfnt: increase proprietary_test coverage.
Recent commits (probably 792d36e1 "support TrueType compound glyphs" and
1995ed1a "support TrueType glyph transformations") let us decode all the
glyphs in the Adobe TrueType fonts, but I forgot to update the test
coverage in those commits.

Change-Id: I7b135005cd67082f84aee16d296debdedcc9488e
Reviewed-on: https://go-review.googlesource.com/38281
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-22 21:47:50 +00:00
Nigel Tao
2c9074b9aa font/sfnt: implement flagWeHaveATwoByTwo.
Change-Id: I9f4577b57f25a85ef0639c34215adf40df61c5ab
Reviewed-on: https://go-review.googlesource.com/38274
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-22 21:43:44 +00:00
Nigel Tao
fb59e5ba10 font/sfnt: rename a "stack" field to "argStack".
This will lessen the diff in a subsequent commit, which introduces a
call stack.

Change-Id: I478857675f47b53e6bd3ee270087a00b9d1d4d34
Reviewed-on: https://go-review.googlesource.com/38279
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2017-03-19 05:24:11 +00:00
Nigel Tao
88e2d806a0 font/sfnt: ignore version 1 kern tables.
The format is relatively complicated but rarely seen. It doesn't seem
worth the effort. Ignoring it is what Microsoft Windows and FreeType do.

Change-Id: I4415bd591c832650066de1d4e035b20a7230a4da
Reviewed-on: https://go-review.googlesource.com/38273
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-18 23:50:03 +00:00
Nigel Tao
6847effb9b font/sfnt: support font collections (.ttc and .otc files).
Also add tests for Apple proprietary fonts.

Change-Id: I5ce8efa2397bb01c5255d956a77c955ba1383105
Reviewed-on: https://go-review.googlesource.com/38272
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-18 07:04:21 +00:00
Nigel Tao
1995ed1a25 font/sfnt: support TrueType glyph transformations.
Change-Id: Iea2387b5e30dd0fff53e2808b25599c3be5b1cdb
Reviewed-on: https://go-review.googlesource.com/38210
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-18 06:57:45 +00:00
Nigel Tao
792d36e11d font/sfnt: support TrueType compound glyphs.
Change-Id: I129e3f7894ad0edccc9e8ca4a21fc9e60e23105b
Reviewed-on: https://go-review.googlesource.com/38111
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-15 23:27:06 +00:00
Nigel Tao
c1a19c11c3 font/sfnt: implement rcurveline and rlinecurve.
Change-Id: I563f9e4a76a5c91d2ef2b6ffccc78550ab582d08
Reviewed-on: https://go-review.googlesource.com/38110
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-15 23:13:12 +00:00
Nigel Tao
793f3be7da font/sfnt: implement {hh,vv}curveto.
Change-Id: I873f8b273d2fe9f39df7d333c36976f1b45239a0
Reviewed-on: https://go-review.googlesource.com/37917
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-12 00:23:23 +00:00
Nigel Tao
069db1da13 font/sfnt: make parseXxx dependencies explicit.
Change-Id: Ib0b76c48cd0b4288700458407077aae4e5911233
Reviewed-on: https://go-review.googlesource.com/37553
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-03-07 07:36:15 +00:00
Nigel Tao
c8ae36c95e font/sfnt: implement Font.Kern.
Change-Id: I9ffb93dd3ee08b8871dadf5bc36578710b800199
Reviewed-on: https://go-review.googlesource.com/37410
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-24 09:00:53 +00:00
Nigel Tao
b952c941a6 font/sfnt: check the versions of proprietary fonts.
Change-Id: If8d104f5d0e60b6270e7f6a0762432dfe66cfe48
Reviewed-on: https://go-review.googlesource.com/37174
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-22 03:10:26 +00:00
Nigel Tao
ed91dc314e font/sfnt: add a ppem arg to Font.LoadGlyph.
This lets us load a glyph at e.g. 12 pixels per em.

Change-Id: I048b3db89af8670782953a8361afe0e6373df9b0
Reviewed-on: https://go-review.googlesource.com/37175
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-22 03:05:44 +00:00
Nigel Tao
791b615328 font/sfnt: update test for adobe/SourceSansPro-Regular.ttf.
The latest version of that TTF file fixes the inconsistency between the
TTF and OTF versions.

Change-Id: I0b86f6ddc3f0326e538bdd18fe097be9ad8533ae
Reviewed-on: https://go-review.googlesource.com/37173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-17 22:17:21 +00:00
Nigel Tao
8491f88afc font/sfnt: implement Font.GlyphName.
This is based on the post table in the sfnt file.

Change-Id: I11f7a9bd9024cfc8f92adc5abb4d5356521f0df7
Reviewed-on: https://go-review.googlesource.com/36972
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-17 10:37:04 +00:00
Nigel Tao
153d857a8f font/sfnt: support Symbol fonts such as MS Webdings.
Change-Id: I1297246dad6288345b4b87b885fbeda06a6516ff
Reviewed-on: https://go-review.googlesource.com/37031
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-17 10:20:08 +00:00
Nigel Tao
05f0a469d9 font/sfnt: support non-zero offsets in format-4 cmap tables.
Change-Id: I52592fcde96ce2f3b7700a29169b517a813f9f3c
Reviewed-on: https://go-review.googlesource.com/36371
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-07 21:45:33 +00:00
Nigel Tao
993cf229e6 font/sfnt: fix proprietary fonts and cmap format 12.
Two recent commits ("proprietary fonts" and "cmap format 12") each
passed all of its own tests, but the combination wasn't tested until
both were submitted.

Change-Id: Ic4c2ae8deb1e4623ca5543672dc46d55bfce91a4
Reviewed-on: https://go-review.googlesource.com/36372
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-07 21:42:49 +00:00
Nigel Tao
28d9a8b4a3 font/sfnt: add tests for proprietary fonts.
Change-Id: I1886e24f726598654d2474f0219a8046ba184a9f
Reviewed-on: https://go-review.googlesource.com/36370
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-04 04:20:41 +00:00
Nigel Tao
c8ab450c16 font/sfnt: support cmap format 12.
Change-Id: I2791f5aec860bbb16c6c6945703827afd55c11dc
Reviewed-on: https://go-review.googlesource.com/36291
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-04 03:42:56 +00:00
Nigel Tao
55ae12acc9 font/sfnt: use ../testdata/cmapTest.ttf.
The index numbers can be verified by running ttx over the ttf file:

<GlyphOrder>
<!-- The 'id' attribute is only for humans; it is ignored when parsed. -->
<GlyphID id="0" name=".notdef"/>
<GlyphID id="1" name=".null"/>
<GlyphID id="2" name="nonmarkingreturn"/>
<GlyphID id="3" name="zero"/>
<GlyphID id="4" name="one"/>
<GlyphID id="5" name="two"/>
<GlyphID id="6" name="A"/>
<GlyphID id="7" name="B"/>
<GlyphID id="8" name="a"/>
<GlyphID id="9" name="ydieresis"/>
<GlyphID id="10" name="Amacron"/>
<GlyphID id="11" name="amacron"/>
<GlyphID id="12" name="uni4E2D"/>
<GlyphID id="13" name="u1F0A1"/>
<GlyphID id="14" name="u1F0B1"/>
<GlyphID id="15" name="u1F0B2"/>
</GlyphOrder>

Change-Id: I8c3b3f09ca25c14d92725d121481287cf57d0d01
Reviewed-on: https://go-review.googlesource.com/36080
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-03 07:45:48 +00:00
Nigel Tao
69afd001f7 font/sfnt: parse the cmap table.
Change-Id: I757d42c9caf419f549696543f0f156cfe3dbfe1a
Reviewed-on: https://go-review.googlesource.com/35512
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-02-02 02:23:30 +00:00
Nigel Tao
83686c5479 font/sfnt: parse the glyf table.
Change-Id: Ib7ff75d99d3641f68f621db4ba2279cc1bda8c3a
Reviewed-on: https://go-review.googlesource.com/35271
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-19 08:01:09 +00:00
Nigel Tao
1ff62c9216 math/fixed: add some Point and Rectangle methods.
These are analogous to the image.Point and image.Rectangle methods in
the standard library.

Change-Id: If8997421a9aeb31d29cfe5eefb79cc481a39df82
Reviewed-on: https://go-review.googlesource.com/34753
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-06 00:18:22 +00:00