def test_locale_independence(self): # Ensure that >ASCII Unicode text is decoded correctly regardless of # the locale. text = "\N{GREEK SMALL LETTER MU}" with locale_context(locale.LC_CTYPE, ("en", "utf8")): gc = agg.GraphicsContextArray((200, 200)) f = Font("modern") with gc: gc.set_font(f) gc.translate_ctm(50, 50) tx0, _, _, _ = gc.get_text_extent(text) gc.show_text(text) x0, _ = gc.get_text_position() with locale_context(locale.LC_CTYPE, ("C", "ASCII")): gc = agg.GraphicsContextArray((200, 200)) f = Font("modern") with gc: gc.set_font(f) gc.translate_ctm(50, 50) tx1, _, _, _ = gc.get_text_extent(text) gc.show_text(text) x1, _ = gc.get_text_position() self.assertEqual(tx1, tx0) self.assertEqual(x1, x0)
def test_str_to_font(self): # Simple from_str = str_to_font("modern 10") from_ctor = Font(family=MODERN, size=10) self.assertEqual(from_ctor, from_str) # Some complexity from_str = str_to_font("roman bold italic 12") from_ctor = Font(family=ROMAN, weight=WEIGHT_BOLD, style=ITALIC, size=12) self.assertEqual(from_ctor, from_str) # Lots of complexity from_str = str_to_font("Times roman bold italic underline 72") from_ctor = Font( "Times", family=ROMAN, weight=WEIGHT_BOLD, style=ITALIC, size=72, underline=1, ) self.assertEqual(from_ctor, from_str)
def test_weight_warnings(self): # Don't use BOLD as a weight with self.assertWarns(DeprecationWarning): font = Font(weight=BOLD) self.assertEqual(font.weight, WEIGHT_BOLD) # Don't use BOLD as a style with self.assertWarns(DeprecationWarning): font = Font(style=BOLD) self.assertEqual(font.weight, WEIGHT_BOLD) self.assertEqual(font.style, NORMAL) # Don't use BOLD_ITALIC as a style with self.assertWarns(DeprecationWarning): font = Font(style=BOLD_ITALIC) self.assertEqual(font.weight, WEIGHT_BOLD) self.assertEqual(font.style, ITALIC) # Ignore BOLD style if weight is not normal with self.assertWarns(DeprecationWarning): font = Font(style=BOLD, weight=WEIGHT_LIGHT) self.assertEqual(font.weight, WEIGHT_LIGHT) self.assertEqual(font.style, NORMAL) with self.assertWarns(DeprecationWarning): font = Font(style=BOLD_ITALIC, weight=WEIGHT_LIGHT) self.assertEqual(font.weight, WEIGHT_LIGHT) self.assertEqual(font.style, ITALIC)
def test_find_font_empty_name(self): # This test relies on the fact there exists some fonts on the system # that the font manager can load. Ideally we should be able to redirect # the path from which the font manager loads font files, then this test # can be less fragile. font = Font(face_name="") spec = font.findfont() self.assertTrue(os.path.exists(spec.filename))
def test_find_font_some_face_name(self): font = Font(face_name="ProbablyNotFound") # There will be warnings as there will be no match for the requested # face name. with self.assertWarns(UserWarning): spec = font.findfont() self.assertTrue(os.path.exists(spec.filename))
def test_text_clip(self): with self.draw_and_check(): self.gc.clip_to_rect(23, 77, 100, 23) font = Font(family=MODERN) font.size = 24 self.gc.set_font(font) self.gc.set_text_position(23, 67) self.gc.show_text("hello kiva")
def test_find_font_name(self): font = Font(face_name="ProbablyNotFound") # There will be warnings as there will be no match for the requested # face name. with self.assertWarns(UserWarning): name = font.findfontname() # Name should be nonempty. self.assertGreater(len(name), 0)
def test_find_font_for_language(self): font = Font(face_name="") # Nearly every font supports Latin script, so this shouldn't fail spec = font.findfont(language="Latin") self.assertTrue(os.path.exists(spec.filename)) # There will be warnings for an unknown language with self.assertWarns(UserWarning): spec = font.findfont(language="FancyTalk") self.assertTrue(os.path.exists(spec.filename))
def test_add_application_font(self): path = os.path.join(data_dir, "TestTTF.ttf") family = "Test TTF" kivafont = Font(family) # Before adding the font with self.assertWarns(UserWarning): self.assertNotEqual(kivafont.findfont().filename, path) add_application_fonts([path]) # After adding the font self.assertEqual(kivafont.findfont().filename, path)
def test_text(self): for family in [ DECORATIVE, DEFAULT, ITALIC, MODERN, ROMAN, SCRIPT, TELETYPE]: for weight in range(100, 1001, 100): for style in [NORMAL, ITALIC]: with self.subTest(family=family, weight=weight, style=style): self.gc = self.create_graphics_context() with self.draw_and_check(): font = Font(family=family) font.size = 24 font.weight = weight font.style = style self.gc.set_font(font) self.gc.set_text_position(23, 67) self.gc.show_text("hello kiva")
def draw_meter(gc, location, color, text): """ Draws a meter of the given color, with the given text, at the given location. Parameters ---------- gc : GraphicsContext The graphics context doing the drawing. location : 2-tuple The point where the meter is to be drawn color : 3 or 4 component tuple (R, G, B[, A]) The color of the meter text : str The text to be placed in the center of the meter symbol """ font = Font('Times New Roman', size=20) with gc: gc.set_font(font) gc.set_fill_color(color) gc.set_line_width(3) gc.translate_ctm(*location) gc.arc(0, 0, 20, 0.0, tau) gc.draw_path() gc.set_fill_color((0., 0., 0., 1.0)) x, y, w, h = gc.get_text_extent(text) gc.show_text_at_point(text, -w / 2, -h / 2)
def test_no_antialias(self): gc = agg.GraphicsContextArray((200, 50), pix_format="bgra32") f = Font("modern") gc.set_font(f) gc.set_antialias(0) gc.show_text("hello") save(gc)
def test_get_set_font(self): gc = agg.GraphicsContextArray((5, 5)) font1 = Font("modern") gc.set_font(font1) font3 = gc.get_font() self.assertEqual(font1.face_name, font3.name) self.assertEqual(font1.size, font3.size) self.assertEqual(font1.family, font3.family) self.assertEqual(font1.style, font3.style) self.assertEqual(font1.encoding, font3.encoding)
def getFont(cls, font_name="Arial"): kiva_style = constants.NORMAL if "-" in font_name: font_name, style = font_name.split("-", 2) style = style.lower() if "bold" in style: kiva_style += constants.BOLD if "italic" in style: kiva_style += constants.ITALIC return Font(font_name, style=kiva_style)
def font_metrics_provider(): from reportlab.pdfgen.canvas import Canvas from reportlab.lib.pagesizes import letter from kiva.api import Font # a file will not be created unless save() is called on the context pdf_canvas = Canvas(filename="enable_tmp.pdf", pagesize=letter) gc = GraphicsContext(pdf_canvas) gc.set_font(Font()) return gc
def _draw_label(self, gc): with gc: font = Font(family=MODERN) gc.set_font(font) _x, _y, width, height = gc.get_text_extent(self.label) text_x = self.x + (self.width - width) / 2.0 text_y = self.y - height gc.show_text(self.label, (text_x, text_y))
def _draw_text(self, gc): """ Draw the shape's text. """ if len(self.text) > 0: gc.set_fill_color(self._get_text_color(self.event_state)) gc.set_font(Font(family=MODERN, size=16)) tx, ty, tw, th = gc.get_text_extent(self.text) dx, dy = self.bounds x, y = self.position gc.set_text_position(x + (dx - tw) / 2, y + (dy - th) / 2) gc.show_text(self.text)
def test_rotate(self): text = "hello" gc = agg.GraphicsContextArray((150, 150), pix_format="bgra32") f = Font("modern") gc.set_font(f) tx, ty, sx, sy = gc.get_text_extent(text) gc.translate_ctm(25, 25) gc.rotate_ctm(pi / 2.0) gc.translate_ctm(0, -sy) # gc.show_text(text) gc.set_stroke_color([1, 0, 0]) gc.set_fill_color([0.5, 0.5, 0.5]) gc.rect(tx, ty, sx, sy) gc.stroke_path() gc.show_text(text) save(gc)
def test(): allmetrics = [] for count in counts: start = time.time() for i in range(count): metrics = FMP() for face, size in fonts: metrics.set_font(Font(face, size)) for s in strings: metrics.get_text_extent(s) allmetrics.append(metrics) end = time.time() print("finished count=%d" % count) print(" total time:", end - start) print(" time/set_font:", (end - start) / float(count * len(fonts)))
def test_handling_text(gc): font = Font(face_name="Arial", size=32) gc.set_font(font) gc.translate_ctm(100.0, 100.0) gc.move_to(-5, 0) gc.line_to(5, 0) gc.move_to(0, 5) gc.line_to(0, -5) gc.move_to(0, 0) gc.stroke_path() txtRot = affine_from_rotation(PI / 6) gc.set_text_matrix(txtRot) gc.show_text("Hello") txtRot = invert(txtRot) gc.set_text_matrix(txtRot) gc.show_text("inverted")
def _draw_mainlayer(self, gc, view_bounds=None, mode="default"): if self.should_profile and self.profile_this is not None: # Only profile the first draw. self.should_profile = False self.profile_this.start("Drawing") start = now() gc.clear() width, height = self.bounds gc.save_state() if self.document is None: # fixme: The Mac backend doesn't accept style/width as non-integers # in set_font, but does for select_font... if sys.platform == "darwin": gc.select_font("Helvetica", 36) else: gc.set_font(Font("Helvetica", 36)) gc.show_text_at_point("Could not parse document.", 20, height - 56) gc.restore_state() if self.profile_this is not None: self.profile_this.stop() return try: # SVG origin is upper right with y positive is down. # Set up the transforms to fix this up. # FIXME: if the rendering stage fails, all subsequent renders are # vertically flipped gc.translate_ctm(0, height) # TODO: bother with zoom? # TODO: inspect the view bounds and scale to the shape of the # component? scale = 1.0 gc.scale_ctm(scale, -scale) self.document.render(gc) self.last_render = now() - start finally: gc.restore_state() if self.profile_this is not None: self.profile_this.stop()
# All rights reserved. # # This software is provided without warranty under the terms of the BSD # license included in LICENSE.txt and may be redistributed only under # the conditions described in the aforementioned license. The license # is also available online at http://www.enthought.com/licenses/BSD.txt # # Thanks for using Enthought open source! from time import perf_counter from kiva.agg import AffineMatrix, GraphicsContextArray from kiva.api import MODERN, Font gc = GraphicsContextArray((200, 200)) font = Font(family=MODERN) # print(font.size) font.size = 8 gc.set_font(font) t1 = perf_counter() # consecutive printing of text. with gc: gc.set_antialias(False) gc.set_fill_color((0, 1, 0)) gc.translate_ctm(50, 50) gc.rotate_ctm(3.1416 / 4) gc.show_text("hello") gc.translate_ctm(-50, -50)
def font_metrics_provider(): from kiva.api import Font gc = GraphicsContext((1, 1)) gc.set_font(Font()) return gc
def test_font_query_warnings(self): # Don't use BOLD as a weight font = Font() font.weight = BOLD with self.assertWarns(DeprecationWarning): query = font._make_font_query() self.assertEqual(query.get_weight(), WEIGHT_BOLD) # Don't use BOLD as a style font = Font() font.style = BOLD with self.assertWarns(DeprecationWarning): query = font._make_font_query() self.assertEqual(query.get_weight(), WEIGHT_BOLD) self.assertEqual(query.get_style(), "normal") # Don't use BOLD_ITALIC as a style font = Font() font.style = BOLD_ITALIC with self.assertWarns(DeprecationWarning): query = font._make_font_query() self.assertEqual(query.get_weight(), WEIGHT_BOLD) self.assertEqual(query.get_style(), "italic") # Ignore BOLD style if weight is not normal font = Font() font.weight = WEIGHT_LIGHT font.style = BOLD with self.assertWarns(DeprecationWarning): query = font._make_font_query() self.assertEqual(query.get_weight(), WEIGHT_LIGHT) self.assertEqual(query.get_style(), "normal") font = Font() font.weight = WEIGHT_LIGHT font.style = BOLD_ITALIC with self.assertWarns(DeprecationWarning): query = font._make_font_query() self.assertEqual(query.get_weight(), WEIGHT_LIGHT) self.assertEqual(query.get_style(), "italic")
def test_is_bold_true(self): for weight in range(600, 1001, 100): with self.subTest(weight=weight): font = Font(weight=weight) self.assertTrue(font.is_bold())
def test_show_text_at_point(self): gc = GraphicsContextArray((100, 100)) gc.set_font(Font()) gc.show_text_at_point(str("asdf"), 5, 5)
def test_is_bold_false(self): for weight in range(100, 501, 100): with self.subTest(weight=weight): font = Font(weight=weight) self.assertFalse(font.is_bold())
font_families = { "default": DEFAULT, "decorative": DECORATIVE, "roman": ROMAN, "script": SCRIPT, "swiss": SWISS, "modern": MODERN, } font_styles = {"italic": ITALIC} font_weights = {"bold": BOLD} font_noise = ["pt", "point", "family"] # Pick a default font that should work on all platforms. default_font_name = "modern 10" default_font = Font(family=MODERN, size=10) def bounding_box(components): "Compute the bounding box for a set of components" bxl, byb, bxr, byt = bounds_to_coordinates(components[0].bounds) for component in components[1:]: xl, yb, xr, yt = bounds_to_coordinates(component.bounds) bxl = min(bxl, xl) byb = min(byb, yb) bxr = max(bxr, xr) byt = max(byt, yt) return (bxl, byb, bxr, byt) def intersect_coordinates(coordinates1, coordinates2):
def font_metrics_provider(): gc = GraphicsContext((1, 1)) gc.set_font(Font()) return gc
# is also available online at http://www.enthought.com/licenses/BSD.txt # # Thanks for using Enthought open source! """ Test to see what level of click latency is noticeable. """ import time from traits.api import Float from enable.api import Component, Container, ColorTrait, black_color_trait from enable.examples._example_support import DemoFrame, demo_main from kiva.api import SWISS, Font font = Font(family=SWISS) class Box(Component): color = ColorTrait("red") delay = Float(0.50) def _draw_mainlayer(self, gc, view=None, mode="default"): if self.event_state == "clicked": print("waiting %0.4f seconds... " % self.delay, end=" ") time.sleep(self.delay) print("done.") with gc: gc.set_fill_color(self.color_)