Skip to content

Commit

Permalink
Merge pull request #110 from BlackFoundryCom/palette-index
Browse files Browse the repository at this point in the history
Add --palette-index command line option; add getPalette() method to font
  • Loading branch information
justvanrossum authored Sep 3, 2022
2 parents 83ff93d + 8a5c924 commit 90ab48f
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 70 deletions.
2 changes: 2 additions & 0 deletions Lib/blackrenderer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def main():
parser.add_argument("--features", type=parseFeatures)
parser.add_argument("--variations", type=parseVariations)
parser.add_argument("--margin", type=float, default=20)
parser.add_argument("--palette-index", type=int, default=0)
parser.add_argument(
"--backend",
default=None,
Expand All @@ -47,6 +48,7 @@ def main():
margin=args.margin,
features=args.features,
variations=args.variations,
paletteIndex=args.palette_index,
backendName=args.backend,
)

Expand Down
7 changes: 7 additions & 0 deletions Lib/blackrenderer/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def __init__(self, path=None, *, fontNumber=0, lazy=True, ttFont=None, hbFont=No
def unitsPerEm(self):
return self.hbFont.face.upem

def getPalette(self, paletteIndex):
if not self.palettes:
return None
# clamp index
paletteIndex = max(0, min(paletteIndex, len(self.palettes) - 1))
return self.palettes[paletteIndex]

def setLocation(self, location):
if location is None:
location = {}
Expand Down
4 changes: 3 additions & 1 deletion Lib/blackrenderer/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def renderText(
margin=20,
features=None,
variations=None,
paletteIndex=0,
backendName=None,
lang=None,
script=None,
Expand All @@ -44,6 +45,7 @@ def renderText(
buf.language = lang
if variations:
font.setLocation(variations)
palette = font.getPalette(paletteIndex)

hb.shape(font.hbFont, buf, features)

Expand Down Expand Up @@ -73,7 +75,7 @@ def renderText(
for glyph in glyphLine:
with canvas.savedState():
canvas.translate(glyph.xOffset, glyph.yOffset)
font.drawGlyph(glyph.name, canvas)
font.drawGlyph(glyph.name, canvas, palette=palette)
canvas.translate(glyph.xAdvance, glyph.yAdvance)

if outputPath is not None:
Expand Down
Binary file added Tests/data/Nabla.subset.ttf
Binary file not shown.
Binary file added Tests/expectedOutput/glyph_nabla_A_1_cairo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/expectedOutput/glyph_nabla_A_1_skia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Tests/expectedOutput/glyph_nabla_A_1_svg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/expectedOutput/glyph_nabla_A_cairo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/expectedOutput/glyph_nabla_A_skia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Tests/expectedOutput/glyph_nabla_A_svg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
147 changes: 78 additions & 69 deletions Tests/test_glyph_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,97 +29,106 @@
"crash": dataDir / "crash.subset.otf",
"nested_paintglyph": dataDir / "nested-paintglyph.ttf",
"ftvartest": dataDir / "TestVariableCOLR-VF.ttf",
"nabla": dataDir / "Nabla.subset.ttf",
}


test_glyphs = [
("noto", "uni2693", None),
("noto", "uni2694", None),
("noto", "u1F30A", None),
("noto", "u1F943", None),
("mutator", "B", None),
("mutator", "D", {"wdth": 1000}),
("twemoji", "uni3299", None),
("more_samples", "cross_glyph", None),
("more_samples", "skew_0_15_center_500.0_500.0", None),
("more_samples", "skew_-10_20_center_500.0_500.0", None),
("more_samples", "skew_-10_20_center_1000_1000", None),
("more_samples", "transform_matrix_1_0_0_1_125_125", None),
("more_samples", "transform_matrix_1.5_0_0_1.5_0_0", None),
("more_samples", "transform_matrix_0.9659_0.2588_-0.2588_0.9659_0_0", None),
("more_samples", "transform_matrix_1.0_0.0_0.6_1.0_-300.0_0.0", None),
("more_samples", "clip_box_top_left", None),
("more_samples", "clip_box_bottom_left", None),
("more_samples", "clip_box_bottom_right", None),
("more_samples", "clip_box_top_right", None),
("more_samples", "clip_box_center", None),
("more_samples", "composite_DEST_OVER", None),
("more_samples", "composite_XOR", None),
("more_samples", "composite_OVERLAY", None),
("more_samples", "composite_SRC_IN", None),
("more_samples", "composite_PLUS", None),
("more_samples", "composite_LIGHTEN", None),
("more_samples", "composite_MULTIPLY", None),
("more_samples", "clip_shade_center", None),
("more_samples", "clip_shade_top_left", None),
("more_samples", "clip_shade_bottom_left", None),
("more_samples", "clip_shade_bottom_right", None),
("more_samples", "clip_shade_top_right", None),
("more_samples", "inset_clipped_radial_reflect", None),
# ("more_samples", "sweep", None),
("more_samples", "transformed_sweep", None),
("more_samples", "composite_colr_glyph", None),
("more_samples", "linear_repeat_0_1", None),
("more_samples", "linear_repeat_0.2_0.8", None),
("more_samples", "linear_repeat_0_1.5", None),
("more_samples", "linear_repeat_0.5_1.5", None),
("more_samples", "scale_0.5_1.5_center_500.0_500.0", None),
("more_samples", "scale_1.5_1.5_center_500.0_500.0", None),
("more_samples", "scale_0.5_1.5_center_0_0", None),
("more_samples", "scale_1.5_1.5_center_0_0", None),
("more_samples", "scale_0.5_1.5_center_1000_1000", None),
("more_samples", "scale_1.5_1.5_center_1000_1000", None),
("more_samples", "linear_gradient_extend_mode_pad", None),
("more_samples", "linear_gradient_extend_mode_repeat", None),
("more_samples", "linear_gradient_extend_mode_reflect", None),
("more_samples", "radial_gradient_extend_mode_pad", None),
("more_samples", "radial_gradient_extend_mode_repeat", None),
("more_samples", "radial_gradient_extend_mode_reflect", None),
("more_samples", "rotate_10_center_0_0", None),
("more_samples", "rotate_-10_center_1000_1000", None),
("more_samples", "rotate_25_center_500.0_500.0", None),
("more_samples", "rotate_-15_center_500.0_500.0", None),
("more_samples", "skew_25_0_center_0_0", None),
("more_samples", "skew_25_0_center_500.0_500.0", None),
("more_samples", "skew_0_15_center_0_0", None),
("more_samples", "upem_box_glyph", None),
("nested_paintglyph", "A", None),
("ftvartest", "A", {"wght": 400}),
("ftvartest", "A", {"wght": 700}),
("ftvartest", "B", {"wght": 400}),
("ftvartest", "B", {"wght": 700}),
("noto", "uni2693", None, 0),
("noto", "uni2694", None, 0),
("noto", "u1F30A", None, 0),
("noto", "u1F943", None, 0),
("mutator", "B", None, 0),
("mutator", "D", {"wdth": 1000}, 0),
("twemoji", "uni3299", None, 0),
("more_samples", "cross_glyph", None, 0),
("more_samples", "skew_0_15_center_500.0_500.0", None, 0),
("more_samples", "skew_-10_20_center_500.0_500.0", None, 0),
("more_samples", "skew_-10_20_center_1000_1000", None, 0),
("more_samples", "transform_matrix_1_0_0_1_125_125", None, 0),
("more_samples", "transform_matrix_1.5_0_0_1.5_0_0", None, 0),
("more_samples", "transform_matrix_0.9659_0.2588_-0.2588_0.9659_0_0", None, 0),
("more_samples", "transform_matrix_1.0_0.0_0.6_1.0_-300.0_0.0", None, 0),
("more_samples", "clip_box_top_left", None, 0),
("more_samples", "clip_box_bottom_left", None, 0),
("more_samples", "clip_box_bottom_right", None, 0),
("more_samples", "clip_box_top_right", None, 0),
("more_samples", "clip_box_center", None, 0),
("more_samples", "composite_DEST_OVER", None, 0),
("more_samples", "composite_XOR", None, 0),
("more_samples", "composite_OVERLAY", None, 0),
("more_samples", "composite_SRC_IN", None, 0),
("more_samples", "composite_PLUS", None, 0),
("more_samples", "composite_LIGHTEN", None, 0),
("more_samples", "composite_MULTIPLY", None, 0),
("more_samples", "clip_shade_center", None, 0),
("more_samples", "clip_shade_top_left", None, 0),
("more_samples", "clip_shade_bottom_left", None, 0),
("more_samples", "clip_shade_bottom_right", None, 0),
("more_samples", "clip_shade_top_right", None, 0),
("more_samples", "inset_clipped_radial_reflect", None, 0),
("more_samples", "transformed_sweep", None, 0),
("more_samples", "composite_colr_glyph", None, 0),
("more_samples", "linear_repeat_0_1", None, 0),
("more_samples", "linear_repeat_0.2_0.8", None, 0),
("more_samples", "linear_repeat_0_1.5", None, 0),
("more_samples", "linear_repeat_0.5_1.5", None, 0),
("more_samples", "scale_0.5_1.5_center_500.0_500.0", None, 0),
("more_samples", "scale_1.5_1.5_center_500.0_500.0", None, 0),
("more_samples", "scale_0.5_1.5_center_0_0", None, 0),
("more_samples", "scale_1.5_1.5_center_0_0", None, 0),
("more_samples", "scale_0.5_1.5_center_1000_1000", None, 0),
("more_samples", "scale_1.5_1.5_center_1000_1000", None, 0),
("more_samples", "linear_gradient_extend_mode_pad", None, 0),
("more_samples", "linear_gradient_extend_mode_repeat", None, 0),
("more_samples", "linear_gradient_extend_mode_reflect", None, 0),
("more_samples", "radial_gradient_extend_mode_pad", None, 0),
("more_samples", "radial_gradient_extend_mode_repeat", None, 0),
("more_samples", "radial_gradient_extend_mode_reflect", None, 0),
("more_samples", "rotate_10_center_0_0", None, 0),
("more_samples", "rotate_-10_center_1000_1000", None, 0),
("more_samples", "rotate_25_center_500.0_500.0", None, 0),
("more_samples", "rotate_-15_center_500.0_500.0", None, 0),
("more_samples", "skew_25_0_center_0_0", None, 0),
("more_samples", "skew_25_0_center_500.0_500.0", None, 0),
("more_samples", "skew_0_15_center_0_0", None, 0),
("more_samples", "upem_box_glyph", None, 0),
("nested_paintglyph", "A", None, 0),
("ftvartest", "A", {"wght": 400}, 0),
("ftvartest", "A", {"wght": 700}, 0),
("ftvartest", "B", {"wght": 400}, 0),
("ftvartest", "B", {"wght": 700}, 0),
("nabla", "A", None, 0),
("nabla", "A", None, 1),
]


@pytest.mark.parametrize("fontName, glyphName, location", test_glyphs)
@pytest.mark.parametrize("fontName, glyphName, location, paletteIndex", test_glyphs)
@pytest.mark.parametrize("backendName, surfaceClass", backends)
def test_renderGlyph(backendName, surfaceClass, fontName, glyphName, location):
def test_renderGlyph(
backendName, surfaceClass, fontName, glyphName, location, paletteIndex
):
font = BlackRendererFont(testFonts[fontName])
font.setLocation(location)

scaleFactor = 1 / 4
boundingBox = font.getGlyphBounds(glyphName)
boundingBox = scaleRect(boundingBox, scaleFactor, scaleFactor)
boundingBox = intRect(boundingBox)
palette = font.getPalette(paletteIndex)

surface = surfaceClass()
ext = surface.fileExtension
with surface.canvas(boundingBox) as canvas:
canvas.scale(scaleFactor)
font.drawGlyph(glyphName, canvas)
font.drawGlyph(glyphName, canvas, palette=palette)

locationString = "_" + _locationToString(location) if location else ""
fileName = f"glyph_{fontName}_{glyphName}{locationString}_{backendName}{ext}"
paletteString = "_" + str(paletteIndex) if paletteIndex else ""
fileName = (
f"glyph_{fontName}_{glyphName}{locationString}{paletteString}"
f"_{backendName}{ext}"
)
expectedPath = expectedOutputDir / fileName
outputPath = tmpOutputDir / fileName
surface.saveImage(outputPath)
Expand Down

0 comments on commit 90ab48f

Please sign in to comment.