-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
348 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# 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. | ||
|
||
"""Helpers for dealing with COLR.""" | ||
|
||
|
||
from fontTools.ttLib.tables import otTables as ot | ||
from fontTools import ttLib | ||
from typing import Iterable | ||
|
||
|
||
def paints_of_type( | ||
font: ttLib.TTFont, paint_format: ot.PaintFormat | ||
) -> Iterable[ot.Paint]: | ||
result = [] | ||
|
||
def _callback(paint): | ||
if paint.Format == paint_format: | ||
result.append(paint) | ||
|
||
colr_table = font["COLR"].table | ||
for record in colr_table.BaseGlyphList.BaseGlyphPaintRecord: | ||
record.Paint.traverse(colr_table, _callback) | ||
|
||
return tuple(result) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# 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. | ||
|
||
"""Creates svg files from a COLR table.""" | ||
from absl import app | ||
from absl import flags | ||
from absl import logging | ||
from fontTools import ttLib | ||
from nanoemoji.colr_to_svg import colr_to_svg | ||
from nanoemoji import util | ||
from pathlib import Path | ||
from picosvg.geometric_types import Rect | ||
|
||
|
||
FLAGS = flags.FLAGS | ||
|
||
|
||
flags.DEFINE_string("output_dir", None, "Output dir. Files written to <glyph id>.svg.") | ||
flags.DEFINE_string( | ||
"log_level", | ||
"INFO", | ||
"The threshold for what messages will be logged. One of DEBUG, INFO, WARN, " | ||
"ERROR, or FATAL.", | ||
) | ||
|
||
|
||
def _view_box(font: ttLib.TTFont, glyph_name: str) -> Rect: | ||
pass | ||
|
||
|
||
def main(argv): | ||
logging.set_verbosity(FLAGS.log_level) | ||
|
||
font_file = util.only(lambda a: a.endswith(".ttf"), argv) | ||
out_dir = Path(FLAGS.output_dir) | ||
assert out_dir.is_dir(), f"{FLAGS.output_dir} is not a directory" | ||
|
||
font = ttLib.TTFont(font_file) | ||
assert "COLR" in font, f"No COLR table in {font_file}" | ||
logging.debug("Writing svgs from %s to %s", font_file, out_dir) | ||
|
||
for glyph_name, svg in colr_to_svg(lambda gn: _view_box(font, gn), font).items(): | ||
gid = font.getGlyphID(glyph_name) | ||
dest_file = out_dir / f"{gid:05d}.svg" | ||
with open(dest_file, "w") as f: | ||
f.write(svg.tostring(pretty_print=True)) | ||
|
||
|
||
if __name__ == "__main__": | ||
flags.mark_flag_as_required("output_dir") | ||
app.run(main) |
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.