text/v2: refactoring

This commit is contained in:
Hajime Hoshi 2023-12-09 22:01:28 +09:00
parent d05afcbcfa
commit e0741dcd94
3 changed files with 155 additions and 9 deletions

35
text/v2/export_test.go Normal file
View File

@ -0,0 +1,35 @@
// Copyright 2023 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package text
import (
"golang.org/x/image/math/fixed"
)
func Fixed26_6ToFloat32(x fixed.Int26_6) float32 {
return fixed26_6ToFloat32(x)
}
func Fixed26_6ToFloat64(x fixed.Int26_6) float64 {
return fixed26_6ToFloat64(x)
}
func Float32ToFixed26_6(x float32) fixed.Int26_6 {
return float32ToFixed26_6(x)
}
func Float64ToFixed26_6(x float64) fixed.Int26_6 {
return float64ToFixed26_6(x)
}

View File

@ -18,7 +18,6 @@
package text
import (
"math"
"strings"
"golang.org/x/image/math/fixed"
@ -72,23 +71,19 @@ type Metrics struct {
}
func fixed26_6ToFloat32(x fixed.Int26_6) float32 {
return float32(x>>6) + float32(x&((1<<6)-1))/float32(1<<6)
return float32(x) / (1 << 6)
}
func fixed26_6ToFloat64(x fixed.Int26_6) float64 {
return float64(x>>6) + float64(x&((1<<6)-1))/float64(1<<6)
return float64(x) / (1 << 6)
}
func float32ToFixed26_6(x float32) fixed.Int26_6 {
i := float32(math.Floor(float64(x)))
frac := x - i
return fixed.Int26_6(i)<<6 + fixed.Int26_6(frac*(1<<6))
return fixed.Int26_6(x * (1 << 6))
}
func float64ToFixed26_6(x float64) fixed.Int26_6 {
i := math.Floor(x)
frac := x - i
return fixed.Int26_6(i)<<6 + fixed.Int26_6(frac*(1<<6))
return fixed.Int26_6(x * (1 << 6))
}
func glyphVariationCount(face Face) int {

View File

@ -240,3 +240,119 @@ func TestUnhashableFace(t *testing.T) {
}
}
}
func TestConvertToFixed26_6(t *testing.T) {
testCases := []struct {
In float64
Out fixed.Int26_6
}{
{
In: 0,
Out: 0,
},
{
In: 0.25,
Out: fixed.I(1) / 4,
},
{
In: 0.5,
Out: fixed.I(1) / 2,
},
{
In: 1.25,
Out: fixed.I(1) * 5 / 4,
},
{
In: 1,
Out: fixed.I(1),
},
{
In: -0.25,
Out: fixed.I(-1) / 4,
},
{
In: -0.5,
Out: fixed.I(-1) / 2,
},
{
In: -1,
Out: fixed.I(-1),
},
{
In: -1.25,
Out: fixed.I(-1) * 5 / 4,
},
}
for _, tc := range testCases {
got := text.Float32ToFixed26_6(float32(tc.In))
want := tc.Out
if got != want {
t.Errorf("Float32ToFixed26_6(%v): got: %v, want: %v", tc.In, got, want)
}
got = text.Float64ToFixed26_6(tc.In)
want = tc.Out
if got != want {
t.Errorf("Float32ToFixed26_6(%v): got: %v, want: %v", tc.In, got, want)
}
}
}
func TestConvertToFloat(t *testing.T) {
testCases := []struct {
In fixed.Int26_6
Out float64
}{
{
In: 0,
Out: 0,
},
{
In: fixed.I(1) / 4,
Out: 0.25,
},
{
In: fixed.I(1) / 2,
Out: 0.5,
},
{
In: fixed.I(1) * 5 / 4,
Out: 1.25,
},
{
In: fixed.I(1),
Out: 1,
},
{
In: fixed.I(-1) / 4,
Out: -0.25,
},
{
In: fixed.I(-1) / 2,
Out: -0.5,
},
{
In: fixed.I(-1),
Out: -1,
},
{
In: fixed.I(-1) * 5 / 4,
Out: -1.25,
},
}
for _, tc := range testCases {
got := text.Fixed26_6ToFloat32(tc.In)
want := float32(tc.Out)
if got != want {
t.Errorf("Fixed26_6ToFloat32(%v): got: %v, want: %v", tc.In, got, want)
}
got64 := text.Fixed26_6ToFloat64(tc.In)
want64 := tc.Out
if got64 != want64 {
t.Errorf("Fixed26_6ToFloat64(%v): got: %v, want: %v", tc.In, got64, want64)
}
}
}