def write_media(media: DalmatianMedia): filename = "{}/{}{}.svg".format(args.outdirectory, args.prefix, media.headers.get_text("name", "en")) if args.view == "default": media.to_xml_svg_file( media.create_page_pixel_coordinate_with_view( int(args.width), default_view), filename) elif args.view == "cropped": rect = media.get_brushstokes_points().get_containing_rect() cropped_view = DlmtView.from_string( "view i:2 lang en xy {} width {} height {} flags o tags all but [ ] -> cropped " .format(rect.xy, rect.width, rect.height)) media.to_xml_svg_file( media.create_page_pixel_coordinate_with_view( int(args.width), cropped_view), filename) else: media.to_xml_svg_file( media.create_page_pixel_coordinate(args.view, int(args.width)), filename) if "png" in args.format: write_png(filename, args.format)
def publishable_specimen(self, specimen) -> DalmatianMedia: name = "stencil-{}".format(specimen["hid"]) stencil = specimen["stencil"].copy() headers = DlmtHeaders() headers.set_brush_page_ratio(self.init.brush_page_ratio) headers.set_id_urn("olivierhuin/{}/{}".format(self.name, name)) headers.set_prefixes({ "brushes": "https://olih.github.io/brush/", "stencils": "https://olih.github.io/stencil/" }) headers.set_text("name", "en", name) headers.set_text("title", "en", "Evolved {}".format(name)) headers.set_text( "license", "en", "Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)") headers.set_text("attribution-name", "en", "Olivier Huin") headers.set_text("brushes-attribution-name", "en", "Olivier Huin") headers.set_text("author", "en", "Olivier Huin") headers.set_text("description", "en", specimen["summary"]) headers.set_url("content-url", "dlmt", "en", "https://olih.github.io/stencil/{}.dlmt".format(name)) headers.set_url( "license-url", "html", "en", "https://creativecommons.org/licenses/by-sa/4.0/legalcode") headers.set_url( "brushes-license-url", "html", "en", "https://creativecommons.org/licenses/by/4.0/legalcode") headers.set_url("author-url", "html", "en", "https://olih.github.io/stencil/profile.html") headers.set_url("attribution-url", "html", "en", "https://olih.github.io/stencil/profile.html") headers.set_url("brushes-attribution-url", "html", "en", "https://olih.github.io/brush/profile.html") headers.set_url("homepage-url", "html", "en", "https://github.com/olih/stencil") headers.set_url("repository-url", "html", "en", "https://github.com/olih/stencil") headers.set_copyright_year(today.year) headers.set_is_family_friendly(True) stencil["headers"] = headers.to_string_list() media = DalmatianMedia.from_obj(stencil) if media.check_references() != []: print(media.check_references()) raise Exception("Invalid format for stencil {}".format( specimen["id"])) return media
def save_svg(self): started_svg = time() self.delete_specimen_svg() specimens = self.content['specimens'] svg_count = 1 for specimen in specimens: if len(specimen["tags"]) > 0: if not args.brushes: continue if args.brushes and "preserve" in specimen["tags"]: continue svg_count = svg_count + 1 stencil = DalmatianMedia.from_obj(specimen["stencil"]) filename = "{}/eval-{}.svg".format( xpfs.get_directory(TypicalDir.EVALUATION), specimen["id"]) stencil.to_xml_svg_file( stencil.create_page_pixel_coordinate("i:1", 100), filename) print("New: {} : {}".format(specimen["id"], specimen["summary"])) finished_svg = time() print( "Saving to svg took {} seconds thus {} second per specimen".format( finished_svg - started_svg, (finished_svg - started_svg) / svg_count))
def mutation_specimen(self, specimen): # Create L-System product = ProductionGame.from_obj(specimen["product"]) product.produce() product_obj = product.to_obj() # Convert chain to brushstokes tortugaconfig = TortugaConfig().set_magnitude_page_ratio( self.init.magnitude_page_ratio) tortugaconfig.set_scale_magnitude_ratio_string( self.init.scale_magnitude_ratio) tortugaconfig.set_brushstoke_angle_offset( self.init.brushstoke_angle_offset) tortugaconfig.set_xy(self.init.xy) angles = mutate_fraction(specimen["angles"], self.pool.angles) magnitudes = mutate_fraction(specimen["magnitudes"], self.pool.magnitudes) tortugaconfig.set_angles_string(angles) tortugaconfig.set_magnitudes_string(magnitudes) tortugaconfig.set_brush_ids(self.init.brushids) tortugaconfig.set_chain(product.chain) brushstokes = TortugaProducer(tortugaconfig).produce() bstats = V2dList([bs.xy for bs in brushstokes]) # Create stencil aka DalmatianMedia stencil = DalmatianMedia(DlmtHeaders().set_brush_page_ratio( self.init.brush_page_ratio)) stencil.add_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags o tags all but [ ] -> everything" ) stencil.add_tag_description_string( "tag i:1 lang en same-as [] -> default") for brush in self.init.brushes: stencil.add_brush_string(brush) stencil.set_brushstrokes(brushstokes) allbr = stencil.page_brushstroke_list_for_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags o tags all but [ ] -> everything" ) fitbr = stencil.page_brushstroke_list_for_view_string( "view i:1 lang en xy 0 0 width 1 height 1 flags O tags all but [ ] -> everything" ) fitness = Fraction(len(fitbr), len(allbr)) ruleInfo = ", ".join( [r["s"] + "->" + r["r"] for r in product_obj["rules"]]) correlation = bstats.get_correlation() medianpoint = bstats.get_median_range(8) if correlation > 0.9 or correlation < -0.9: print("C", end="") return None if float(fitness) < 0.8: print("F", end="") return None if medianpoint.x < self.init.min_median_range.x: print("X", end="") return None if medianpoint.y < self.init.min_median_range.y: print("Y", end="") return None summary = "Stencil manually selected from carefully crafted generations using a Lindenmayer system approach based on angles [ {} ], magnitudes [ {} ] and the rules {} starting with {} resulting in {} brushstokes with a visibility of {:.2%}, correlation of {} and a median range of {}".format( angles, magnitudes, ruleInfo, product_obj["start"], len(brushstokes), float(fitness), correlation, medianpoint.to_float_string()) return { "id": self.inc_id(), "product": product_obj, "angles": angles, "magnitudes": magnitudes, "stencil": stencil.to_obj(), "summary": summary, "tags": "" }
def test_export_svg(self): headers = DlmtHeaders().set_brush_page_ratio(Fraction("1/100")) headers.set_id_urn("company/project/example123") headers.set_text("name", "en", "example123") headers.set_text("title", "en", "some relevant title") headers.set_text("license", "en", "No Rights Reserved (CC0)") headers.set_text("attribution-name", "en", "Public domain") headers.set_text("brushes-attribution-name", "en", "Public domain") headers.set_text("author", "en", "Public domain") headers.set_text("description", "en", "some description") headers.set_url("content-url", "dlmt", "en", "https://your-personal-website.com/example123.dlmt") headers.set_url( "license-url", "html", "en", "https://creativecommons.org/share-your-work/public-domain/cc0/") headers.set_url( "brushes-license-url", "html", "en", "https://creativecommons.org/share-your-work/public-domain/cc0/") headers.set_url("author-url", "html", "en", "https://your-personal-website.com/profile") headers.set_url("attribution-url", "html", "en", "https://your-personal-website.com/profile") headers.set_url("brushes-attribution-url", "html", "en", "https://your-personal-website.com/profile") headers.set_url("homepage-url", "html", "en", "https://your-personal-website.com/homepage") headers.set_url("repository-url", "html", "en", "https://your-personal-website.com/sourcecode") headers.set_copyright_year(2020) headers.set_is_family_friendly(True) media = DalmatianMedia(headers) media.add_view_string( "view i:1 lang en-gb xy 0 0 width 1/1 height 1/1 flags OC tags all but [ ] -> everything" ) media.add_view_string( "view i:2 lang en-gb xy 20/100 20/100 width 1/2 height 1/2 flags OC tags all but [ ] -> everything" ) media.add_tag_description_string( "tag i:1 lang en-gb same-as [] -> default tag") media.add_brush_string(homeBrush) for i in range(0, 90, 10): media.add_brushstroke_string( "brushstroke i:1 xy {}/100 10/100 scale 1 angle 0/1 tags [ i:1 ]" .format(i)) for i in range(0, 90, 10): media.add_brushstroke_string( "brushstroke i:1 xy {}/100 30/100 scale {}/10 angle 0/1 tags [ i:1 ]" .format(i, i + 1)) for i in range(0, 90, 10): media.add_brushstroke_string( "brushstroke i:1 xy {}/100 50/100 scale 3 angle {}/90 tags [ i:1 ]" .format(i, i)) media.to_xml_svg_file(media.create_page_pixel_coordinate("i:1", 100), "examples/one.svg") with open("examples/one.dlmt", 'w') as dlmtfile: dlmtfile.write(media.to_string()) media.to_xml_svg_file(media.create_page_pixel_coordinate("i:2", 100), "examples/one-zoom.svg")
def test_to_page_brushstroke_list_for_view_tags(self): viewallbut2 = DlmtView.from_string( "view i:1 lang en-gb xy 0 0 width 1/1 height 1/1 flags OC tags all but [ i:2 ]-> everything" ) viewnonebut3 = DlmtView.from_string( "view i:1 lang en-gb xy 0 0 width 1/1 height 1/1 flags OC tags none but [ i:3 ]-> everything" ) media = DalmatianMedia(DlmtHeaders().set_brush_page_ratio( Fraction("1/100"))) media.add_view(viewallbut2) media.add_tag_description_string( "tag i:1 lang en-gb same-as [] -> tag1") media.add_tag_description_string( "tag i:2 lang en-gb same-as [] -> tag2") media.add_tag_description_string( "tag i:3 lang en-gb same-as [] -> tag3") media.add_brush_string(homeBrush) media.add_brushstroke_string( "brushstroke i:1 xy 10/100 10/100 scale 1 angle 0/1 tags [ i:1 ]") media.add_brushstroke_string( "brushstroke i:1 xy 20/100 10/100 scale 1 angle 0/1 tags [ i:1, i:2 ]" ) media.add_brushstroke_string( "brushstroke i:1 xy 30/100 10/100 scale 1 angle 0/1 tags [ i:2 ]") media.add_brushstroke_string( "brushstroke i:1 xy 40/100 10/100 scale 1 angle 0/1 tags [ i:3 ]") self.assertEqual( len(media.page_brushstroke_list_for_view(viewallbut2)), 2) self.assertEqual( len(media.page_brushstroke_list_for_view(viewnonebut3)), 1)
def test_to_page_brushstroke_list_for_view_zoom_optimized(self): theview = DlmtView.from_string( "view i:2 lang en-gb xy 20/100 20/100 width 1/2 height 1/2 flags O tags all but [ ] -> everything" ) media = DalmatianMedia(DlmtHeaders().set_brush_page_ratio( Fraction("1/100"))) media.add_view(theview) media.add_tag_description_string( "tag i:1 lang en-gb same-as [] -> default tag") media.add_brush_string(homeBrush) for i in range(0, 90, 10): media.add_brushstroke_string( "brushstroke i:1 xy {}/100 30/100 scale 1 angle 0/1 tags [ i:1 ]" .format(i)) pblist = media.to_page_brushstroke_list() pblistforview = media.page_brushstroke_list_for_view(theview) self.assertNotEqual(pblistforview, pblist) self.assertNotEqual(len(pblistforview), len(pblist)) self.assertEqual(len(pblistforview), 4)
def test_to_page_brushstroke_list_scale(self): media = DalmatianMedia(DlmtHeaders().set_brush_page_ratio( Fraction("1/100"))) media.add_view_string( "view i:1 lang en-gb xy 0 0 width 1/1 height 1/1 flags OC tags all but [ ] -> everything" ) media.add_tag_description_string( "tag i:1 lang en-gb same-as [] -> default tag") media.add_brush_string(homeBrush) for i in range(0, 90, 10): media.add_brushstroke_string( "brushstroke i:1 xy {}/100 10/100 scale 2 angle 0/1 tags [ i:1 ]" .format(i)) pblist = media.to_page_brushstroke_list() self.assertEqual(len(pblist), 9) self.assertEqual( pblist[0].to_string(), "pbs path [ M -1/150 8/75,L 0 1/10,L 1/150 8/75,L 1/150 7/75,L -1/150 7/75 ] tags ['i:1']" ) self.assertEqual( pblist[1].to_string(), "pbs path [ M 7/75 8/75,L 1/10 1/10,L 8/75 8/75,L 8/75 7/75,L 7/75 7/75 ] tags ['i:1']" )
def test_convert(self): headers = DlmtHeaders() headers.set_page_coordinate_system_string( "system cartesian right-dir + up-dir -") headers.set_brush_coordinate_system_string( "system cartesian right-dir + up-dir - origin-x 1/2 origin-y 1/2") headers.set_brush_page_ratio(Fraction("1/30")) headers.set_prefixes({ "prefix1": "http://domain1.com", "prefix2": "http://domain2.com", "geospecies": "http://geospecies" }) headers.set_text("license", "en", "Creative Commons") headers.set_text("license", "fr", "Creative Communs") headers.set_text("title", "en", "Some english title") headers.set_text("title", "fr", "Titre en francais") headers.set_url( "license-url", "html", "en", "https://creativecommons.org/licenses/by-sa/4.0/legalcode") headers.set_url( "brushes-license-url", "json", "en", "https://creativecommons.org/licenses/by/4.0/legalcode") media = DalmatianMedia(headers) media.add_view_string( "view i:1 lang en-gb xy -1/2 -1/2 width 1/1 height 1/1 flags OC tags all but [ ] -> everything" ) media.add_tag_description_string( "tag i:1 lang en-gb same-as [ geospecies:bioclasses/P632y ] -> part of head" ) media.add_tag_description_string( "tag i:1 lang fr-fr same-as [] -> visage") media.add_tag_description_string("tag i:2 lang fr same-as [] -> pied") media.add_brush_string( "brush i:1 ext-id brushes:abc3F path [ M -1/3 1/3,L 2/3 1/4,L 1/4 2/3,L -2/3 1/2 ]" ) media.add_brush_string( "brush i:2 ext-id brushes:abc4F path [ M -1/3 1/3,L 2/3 3/4,L 1/4 2/3,L -2/3 1/2 ]" ) media.add_brushstroke_string( "brushstroke i:1 xy 1/15 1/100 scale 1/10 angle 0/1 tags [ i:1 ]") media.add_brushstroke_string( "brushstroke i:1 xy 2/15 2/100 scale 2/10 angle 0/1 tags [ i:1 ]") media.add_brushstroke_string( "brushstroke i:1 xy 3/15 3/100 scale 3/10 angle 0/1 tags [ i:1 ]") media.add_brushstroke_string( "brushstroke i:2 xy 4/15 5/100 scale 5/10 angle 0/1 tags [ i:1, i:2 ]" ) self.assertEqual(DalmatianMedia.from_obj(media.to_obj()), media) self.assertEqual(DalmatianMedia.from_string(str(media)), media) self.assertEqual(media.get_tag_ids(), set(["i:1", "i:2"])) self.assertEqual(media.get_brush_ids(), set(["i:1", "i:2"])) self.assertEqual(media.get_view_ids(), set(["i:1"])) self.assertEqual(media.get_used_brush_ids(), set(["i:1", "i:2"])) self.assertEqual(media.get_used_tag_ids(), set(["i:1", "i:2"])) self.assertEqual(media.get_used_short_prefixes(), set(["geospecies"])) self.assertEqual(media.check_references(), [])
def read_dlmt_file(filename: str) -> DalmatianMedia: with open(filename, 'r') as dlmtfile: return DalmatianMedia.from_string(dlmtfile.read())