-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
svg lettericons #55
Merged
Merged
svg lettericons #55
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4a460a2
first stab at RenderSVG
gurgeous 4bfbf94
svg lettericon paths and tests
gurgeous e35744d
no size in svg paths
gurgeous 1addc0f
fix test
gurgeous f272e2b
add iconserver to make test
gurgeous f8a44c8
don't allow size for svg paths
gurgeous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ package lettericon | |
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/xml" | ||
"errors" | ||
"fmt" | ||
"image" | ||
|
@@ -13,6 +15,7 @@ import ( | |
"math" | ||
"net/url" | ||
"path" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
|
@@ -30,7 +33,7 @@ const dpi = 72 | |
const fontSizeFactor = 0.6180340 // (by taste) | ||
const yOffsetFactor = 102.0 / 1024.0 // (by trial and error) :-) | ||
|
||
func Render(letter string, bgColor color.Color, width int, out io.Writer) error { | ||
func RenderPNG(letter string, bgColor color.Color, width int, out io.Writer) error { | ||
fg := pickForegroundColor(bgColor) | ||
|
||
rgba := image.NewRGBA(image.Rect(0, 0, width, width)) | ||
|
@@ -179,32 +182,52 @@ func ColorFromHex(hex string) (*color.RGBA, error) { | |
return &col, nil | ||
} | ||
|
||
func IconPath(letter string, size string, colr *color.RGBA) string { | ||
func IconPath(letter string, size string, colr *color.RGBA, format string) string { | ||
var parts []string | ||
|
||
// letter | ||
if letter == "" { | ||
letter = " " | ||
} else { | ||
letter = strings.ToUpper(letter) | ||
} | ||
parts = append(parts, letter) | ||
|
||
// size (maybe) | ||
if format == "png" { | ||
parts = append(parts, size) | ||
} | ||
|
||
// colr (maybe) | ||
if colr != nil { | ||
return fmt.Sprintf("/lettericons/%s-%s-%s.png", letter, size, colorfinder.ColorToHex(*colr)) | ||
parts = append(parts, colorfinder.ColorToHex(*colr)) | ||
} | ||
return fmt.Sprintf("/lettericons/%s-%s.png", letter, size) | ||
|
||
return fmt.Sprintf("/lettericons/%s.%s", strings.Join(parts, "-"), format) | ||
} | ||
|
||
const defaultIconSize = 144 | ||
|
||
// TODO: Sync with besticon.MaxIconSize ? | ||
const maxIconSize = 256 | ||
|
||
// path is like: lettericons/M-144-EFC25D.png | ||
func ParseIconPath(fullpath string) (string, *color.RGBA, int) { | ||
func ParseIconPath(fullpath string) (string, *color.RGBA, int, string) { | ||
fullpath = percentDecode(fullpath) | ||
|
||
_, filename := path.Split(fullpath) | ||
filename = strings.TrimSuffix(filename, ".png") | ||
|
||
// what is the format? | ||
format := filepath.Ext(filename) | ||
if !(format == ".png" || format == ".svg") { | ||
return "", nil, -1, "" | ||
} | ||
filename = strings.TrimSuffix(filename, format) | ||
format = format[1:] // remove period | ||
|
||
params := strings.Split(filename, "-") | ||
if len(params) < 1 || len(params[0]) < 1 { | ||
return "", nil, -1 | ||
return "", nil, -1, "" | ||
} | ||
|
||
charParam := firstRune(params[0]) | ||
|
@@ -230,7 +253,7 @@ func ParseIconPath(fullpath string) (string, *color.RGBA, int) { | |
col = DefaultBackgroundColor | ||
} | ||
|
||
return charParam, col, size | ||
return charParam, col, size, format | ||
} | ||
|
||
func MainLetterFromURL(URL string) string { | ||
|
@@ -281,6 +304,45 @@ func percentDecode(p string) string { | |
return u.Path | ||
} | ||
|
||
const svgTemplate = ` | ||
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> | ||
<rect x="0" y="0" width="100" height="100" fill="$BG_COLOR"/> | ||
<text x="50%" y="50%" dy="0.10em" font-family="Helvetica Neue, Helvetica, sans-serif" font-size="75" dominant-baseline="middle" text-anchor="middle" fill="$FG_COLOR">$LETTER</text> | ||
</svg> | ||
` | ||
|
||
// RenderSVG writes an SVG lettericon for this letter and color | ||
func RenderSVG(letter string, bgColor color.Color, out io.Writer) error { | ||
// xml escape letter | ||
var buf bytes.Buffer | ||
err := xml.EscapeText(&buf, []byte(letter)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// vars | ||
vars := map[string]string{ | ||
"$BG_COLOR": ColorToHex(bgColor), | ||
"$FG_COLOR": ColorToHex(pickForegroundColor(bgColor)), | ||
"$LETTER": buf.String(), | ||
Comment on lines
+340
to
+343
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like the lightweight templating approach for this👍 |
||
} | ||
|
||
// render SVG by replacing vars in template | ||
svg := strings.TrimSpace(svgTemplate) + "\n" | ||
for k, v := range vars { | ||
svg = strings.ReplaceAll(svg, k, v) | ||
} | ||
|
||
_, err = io.WriteString(out, svg) | ||
return err | ||
} | ||
|
||
// ColorToHex returns the #rrggbb hex string for a color | ||
func ColorToHex(c color.Color) string { | ||
r, g, b, _ := c.RGBA() | ||
return fmt.Sprintf("#%02x%02x%02x", r&0xff, g&0xff, b&0xff) | ||
} | ||
|
||
var fnt *truetype.Font | ||
|
||
var DefaultBackgroundColor *color.RGBA | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we even want to allow for the
size
to be in the path for.svg
? I'm thinking the fact the we allow it is kind of ok by Postel's law on the one hand but could lead to misunderstanding one the other (we're not using this).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let's turn off support for size entirely. We're not generating those with SVG so no need to parse them. Give me a sec here