The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

AttributePattern.vala in /libsvgbird

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Circle boundaries heads/master
1 /* 2 Copyright (C) 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using B; 16 using Math; 17 18 namespace SvgBird { 19 20 public enum AttributePatternType { 21 NONE, 22 ANYTHING, 23 LIST, 24 EQUALS, 25 STARTS_WITH 26 } 27 28 public class AttributePattern : GLib.Object { 29 public string name = ""; 30 public string? content = null; 31 public AttributePatternType type = AttributePatternType.NONE; 32 33 public AttributePattern copy () { 34 AttributePattern a = new AttributePattern (); 35 a.name = name; 36 a.content = content; 37 a.type = type; 38 return a; 39 } 40 41 public bool match (Attributes attributes) { 42 switch (type) { 43 case AttributePatternType.ANYTHING: 44 return match_attribute_name (attributes); 45 case AttributePatternType.LIST: 46 return match_list (attributes); 47 case AttributePatternType.EQUALS: 48 return attribute_equals (attributes); 49 case AttributePatternType.STARTS_WITH: 50 return attribute_start_with (attributes); 51 } 52 53 return false; 54 } 55 56 string remove_hypen (string content) { 57 int hyphen = content.index_of ("-"); 58 59 if (hyphen == -1) { 60 return content; 61 } 62 63 return content.substring (0, hyphen); 64 } 65 66 bool attribute_start_with (Attributes attributes) { 67 foreach (Attribute attribute in attributes) { 68 if (attribute.get_name () == name 69 && remove_hypen (attribute.get_content ()) == ((!) content)) { 70 return true; 71 } 72 } 73 74 return false; 75 } 76 77 bool attribute_equals (Attributes attributes) { 78 foreach (Attribute attribute in attributes) { 79 if (attribute.get_name () == name 80 && attribute.get_content () == ((!) content)) { 81 return true; 82 } 83 } 84 85 return false; 86 } 87 88 bool match_attribute_name (Attributes attributes) { 89 foreach (Attribute attribute in attributes) { 90 if (attribute.get_name () == name) { 91 return true; 92 } 93 } 94 95 return false; 96 } 97 98 bool match_list (Attributes attributes) { 99 if (content == null) { 100 return false; 101 } 102 103 string[] list = ((!) content).split (" "); 104 foreach (Attribute attribute in attributes) { 105 if (attribute.get_name () == name) { 106 107 string attribute_content = attribute.get_content (); 108 foreach (string list_item in list) { 109 if (attribute_content == list_item) { 110 return true; 111 } 112 } 113 } 114 } 115 116 return false; 117 } 118 119 public string to_string () { 120 string c; 121 122 if (content == null) { 123 c = "null"; 124 } else { 125 c = (!) content; 126 } 127 128 switch (type) { 129 case AttributePatternType.ANYTHING: 130 return "[" + name + "]"; 131 case AttributePatternType.LIST: 132 return "[" + name + "~=" + c + "]"; 133 case AttributePatternType.EQUALS: 134 return "[" + name + "=" + c + "]"; 135 case AttributePatternType.STARTS_WITH: 136 return "[" + name + "|=" + c + "]"; 137 } 138 139 return "No attributes."; 140 } 141 } 142 143 } 144