-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfontHelper-objectivec.rb
More file actions
136 lines (121 loc) · 4.86 KB
/
Copy pathfontHelper-objectivec.rb
File metadata and controls
136 lines (121 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (c) 2015, Pablo Alcalde. All rights reserved.
# Licensed under the MIT license <http://opensource.org/licenses/MIT>
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions
# of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
class FontHelper
attr_accessor :unicodes, :fontName, :saveDirectory
def initialize (ttfPath, saveDirectory)
@saveDirectory = saveDirectory
@unicodes = `otfinfo -u #{ttfPath}`.split("\n")
puts @unicodes
@fontName = `otfinfo --info #{ttfPath}`.split[1]
puts "\nFontName = #{@fontName} \n"
end
def unicodesArray()
unicodesStringArray = "@["
@unicodes.each_with_index do |unicode, index|
unicodeSplitted = unicode.split(" ")
if !self.isValidUnicodeCharacter(unicodeSplitted[0]) then next end
if unicodeSplitted.length > 1
unicodeValue = unicodeSplitted[0]
unicodeValue = unicodeValue.sub('uni', 'u')
unicodesStringArray += '@"\\' + unicodeValue + '"'
if index < @unicodes.length-1 then unicodesStringArray += ',' end
end
end
unicodesStringArray += '];'
return unicodesStringArray
end
def createImplementationFile()
template = File.open(File.dirname(__FILE__) + '/' + "template.m")
newFile = File.new(saveDirectory + '/' + "NSString+#{@fontName}.m", 'w');
template.each do |line|
line = self.replaceFontNameForLine(line)
if line.include? '<<UNICODES_ARRAY>>'
line = line.sub('<<UNICODES_ARRAY>>', self.unicodesArray() + "\n")
end
newFile.write(line)
end
puts "\nCreated implementation file:\n"
puts newFile.path + "\n\n"
end
def unicodesEnums()
unicodesString = ''
@unicodes.each_with_index do |unicode, index|
unicodeSplitted = unicode.split(" ")
if !self.isValidUnicodeCharacter(unicodeSplitted[0]) then next end
unicodeName = unicodeSplitted[2] ? unicodeSplitted[2] : ""
unicodesString += ' ' + @fontName + '_' + unicodeName
if index < @unicodes.length-1 then unicodesString += ',' + "\n" end
end
return unicodesString
end
def createHeaderFile()
template = File.open(File.dirname(__FILE__) + '/' + "template.h")
newFile = File.new(saveDirectory + '/' + "NSString+#{@fontName}.h", 'w');
template.each do |line|
line = self.replaceFontNameForLine(line)
if line.include? '<<UNICODES_ENUM>>'
line = line.sub('<<UNICODES_ENUM>>', self.unicodesEnums() + "\n")
end
newFile.write(line)
end
puts "\nCreated header file: \n"
puts newFile.path + "\n\n"
end
def replaceFontNameForLine(line)
newLine = line
if line.include? '<<FONTNAME>>'
newLine = line.gsub('<<FONTNAME>>', @fontName)
end
return newLine
end
def isValidUnicodeCharacter(uniChar)
case uniChar
when 'uni0001' then return false
when 'uni0020' then return false
when 'uniFFFF' then return false
when 'uniF500' then return false
else return true
end
end
end
# Main
# Lets check if user already has the brew package lcdf-typetools which provides the otfinfo library used by this script.
otfInfoInstalledVersion = `otfinfo --version; true`
if otfInfoInstalledVersion.split[3].nil? || otfInfoInstalledVersion.split[3] < "2.104"
puts "You don't have otfinfo 2.104 installed"
brewInstalled = !`brew --version; true`.include?('command not found')
if brewInstalled
puts "Trying to get otfinfo from homebrew, installing package lcdf-typetools.."
`brew install lcdf-typetools`
else
puts "Install homebrew and run this script again"
exit
end
end
ttfPath = nil
saveDirectory = nil
if ARGV[0] && File.file?(ARGV[0]) then ttfPath = ARGV[0] end
if ARGV[1] && File.directory?(ARGV[1]) then saveDirectory = ARGV[1].chomp("/") end
if ttfPath && saveDirectory
fontHelper = FontHelper.new(ttfPath, saveDirectory)
fontHelper.createHeaderFile
fontHelper.createImplementationFile
else
if ttfPath.nil?
puts "\nError: A valid path to a .ttf file must be provided as a first argument\n\n"
elsif saveDirectory.nil?
puts "\nError: A valid path to a directory where generated files will be saved must be provided as a second argument\n\n"
end
exit
end