-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from mum4k/dot-display
Support displaying the dot character in SegmentDisplay.
- Loading branch information
Showing
20 changed files
with
1,714 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2019 Google Inc. | ||
// | ||
// 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 dotseg | ||
|
||
// attributes.go calculates attributes needed when determining placement of | ||
// segments. | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"math" | ||
|
||
"github.com/mum4k/termdash/align" | ||
"github.com/mum4k/termdash/internal/alignfor" | ||
"github.com/mum4k/termdash/internal/area" | ||
"github.com/mum4k/termdash/internal/segdisp" | ||
"github.com/mum4k/termdash/internal/segdisp/sixteen" | ||
) | ||
|
||
// attributes contains attributes needed to draw the segment display. | ||
// Refer to doc/segment_placement.svg for a visual aid and explanation of the | ||
// usage of the square roots. | ||
type attributes struct { | ||
// bcAr is the area the attributes were created for. | ||
bcAr image.Rectangle | ||
|
||
// segSize is the width of a vertical or height of a horizontal segment. | ||
segSize int | ||
|
||
// sixteen are attributes of a 16-segment display when placed on the same | ||
// area. | ||
sixteen *sixteen.Attributes | ||
} | ||
|
||
// newAttributes calculates attributes needed to place the segments for the | ||
// provided pixel area. | ||
func newAttributes(bcAr image.Rectangle) *attributes { | ||
segSize := segdisp.SegmentSize(bcAr) | ||
return &attributes{ | ||
bcAr: bcAr, | ||
segSize: segSize, | ||
sixteen: sixteen.NewAttributes(bcAr), | ||
} | ||
} | ||
|
||
// segArea returns the area for the specified segment. | ||
func (a *attributes) segArea(seg Segment) (image.Rectangle, error) { | ||
// Dots have double width of normal segments to fill more space in the | ||
// segment display. | ||
segSize := a.segSize * 2 | ||
|
||
// An area representing the dot which gets aligned and moved into position | ||
// below. | ||
dotAr := image.Rect( | ||
a.bcAr.Min.X, | ||
a.bcAr.Min.Y, | ||
a.bcAr.Min.X+segSize, | ||
a.bcAr.Min.Y+segSize, | ||
) | ||
mid, err := alignfor.Rectangle(a.bcAr, dotAr, align.HorizontalCenter, align.VerticalMiddle) | ||
if err != nil { | ||
return image.ZR, err | ||
} | ||
|
||
// moveBySize is the multiplier of segment size to determine by how many | ||
// pixels to move D1 and D2 up and down from the center. | ||
const moveBySize = 1.5 | ||
moveBy := int(math.Round(moveBySize * float64(segSize))) | ||
switch seg { | ||
case D1: | ||
moved, err := area.MoveUp(mid, moveBy) | ||
if err != nil { | ||
return image.ZR, err | ||
} | ||
return moved, nil | ||
|
||
case D2: | ||
moved, err := area.MoveDown(mid, moveBy) | ||
if err != nil { | ||
return image.ZR, err | ||
} | ||
return moved, nil | ||
|
||
case D3: | ||
// Align at the middle of the bottom. | ||
bot, err := alignfor.Rectangle(a.bcAr, dotAr, align.HorizontalCenter, align.VerticalBottom) | ||
if err != nil { | ||
return image.ZR, err | ||
} | ||
|
||
// Shift up to where the sixteen segment actually places its bottom | ||
// segments. | ||
diff := bot.Min.Y - a.sixteen.VertBotY | ||
// Shift further up by one segment size, since the dots have double width. | ||
diff += a.segSize | ||
moved, err := area.MoveUp(bot, diff) | ||
if err != nil { | ||
return image.ZR, err | ||
} | ||
return moved, nil | ||
|
||
default: | ||
return image.ZR, fmt.Errorf("cannot calculate area for %v(%d)", seg, seg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2019 Google Inc. | ||
// | ||
// 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 dotseg | ||
|
||
import ( | ||
"image" | ||
"testing" | ||
|
||
"github.com/kylelemons/godebug/pretty" | ||
) | ||
|
||
func TestAttributes(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
brailleAr image.Rectangle | ||
seg Segment | ||
want image.Rectangle | ||
wantErr bool | ||
}{ | ||
{ | ||
desc: "fails on unsupported segment", | ||
brailleAr: image.Rect(0, 0, 1, 1), | ||
seg: Segment(-1), | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
attr := newAttributes(tc.brailleAr) | ||
got, err := attr.segArea(tc.seg) | ||
if (err != nil) != tc.wantErr { | ||
t.Errorf("segArea => unexpected error: %v, wantErr: %v", err, tc.wantErr) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
if diff := pretty.Compare(tc.want, got); diff != "" { | ||
t.Errorf("segArea => unexpected diff (-want, +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.