Skip to content

Commit

Permalink
Merge pull request #205 from Ryosuke839/#191-rafactor-character-entity
Browse files Browse the repository at this point in the history
Refactor character data to display NOTICE_LINE
  • Loading branch information
Ryosuke839 authored Jan 3, 2025
2 parents 43fa027 + 29b2374 commit bb9a995
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 109 deletions.
Binary file modified app/src/main/assets/namedb.zip
Binary file not shown.
169 changes: 87 additions & 82 deletions app/src/main/java/jp/ddo/hotmist/unicodepad/CharacterAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.view.*
import android.view.View.MeasureSpec
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.widget.NestedScrollView
Expand Down Expand Up @@ -68,8 +67,7 @@ internal class CharacterAdapter(private val activity: UnicodeActivity, private v
val str = StringBuilder()
if (!emoji) str.append(adapter.getItem(position))
val textPadding = (6 * activity.resources.displayMetrics.scaledDensity).toInt()
for (i in 0 until if (!emoji) 10 else 7) {
if (emoji && i == 5) continue
for (i in 0 until if (!emoji) 4 else 5) {
if (i == 2) {
val v = if (!emoji) db.getInt(itemid, cols[i]) else db.getInt(adapter.getItemString(position), emjs[i])
val desc = TextView(activity)
Expand All @@ -96,7 +94,7 @@ internal class CharacterAdapter(private val activity: UnicodeActivity, private v
break
}
if (r == null) continue
val l = r.split(if (emoji && i == 6) " " else "\n").toTypedArray()
val l = r.split("\n").toTypedArray()
for (s in l) {
if (i == 0) {
layout.addView(LinearLayout(activity).apply {
Expand Down Expand Up @@ -132,85 +130,92 @@ internal class CharacterAdapter(private val activity: UnicodeActivity, private v
hl.orientation = LinearLayout.HORIZONTAL
val it = TextView(activity)
it.gravity = Gravity.CENTER_VERTICAL
it.text = (if (!emoji) mods else mode)[i]
hl.addView(it, LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT))
if (i < 6) {
val desc = TextView(activity)
desc.text = s
desc.setTextIsSelectable(true)
hl.addView(desc, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
} else {
var cs = ""
var ps = ""
var ns: String? = null
Scanner(s).use { sc ->
var j = 0
while (sc.hasNext()) {
if (i == 9 && j == 0 && s[0] == '<') {
ns = sc.next()
++j
continue
}
val tgt = sc.nextInt(16)
cs += String(Character.toChars(tgt))
ps += String.format("U+%04X ", tgt)
if (i == 6) {
val n = db[tgt, "name"]
ns = n ?: "<not a character>"
break
if (!emoji && i == 3) {
val charMap = mapOf(
"*" to "\u2022 ",
"=" to "= ",
"%" to "\u203B ",
"x" to "\u2192 ",
"~" to "~ ",
":" to "\u2261 ",
"#" to "\u2248 ",
"@" to "\u2022 ",
)
it.text = charMap[s.substring(0, 1)] ?: s.substring(0, 1)
hl.addView(it, LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT))
if (s.startsWith("*") || s.startsWith("=") || s.startsWith("%") || s.startsWith("@")) {
it.gravity = Gravity.TOP
val desc = TextView(activity)
desc.text = s.substring(2)
desc.setTextIsSelectable(true)
hl.addView(desc, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
} else {
var cs = ""
var ps = ""
val ns = mutableListOf<String>()
Scanner(s.substring(2)).use { sc ->
while (sc.hasNext()) {
val ss = sc.next()
if (Regex("[0-9A-Fa-f]{4,6}").matches(ss)) {
val tgt = Integer.parseInt(ss, 16)
cs += String(Character.toChars(tgt))
ps += String.format("U+%04X ", tgt)
} else {
ns.add(ss)
}
}
if (i == 7 && j == 1) {
sc.useDelimiter("\n")
sc.skip(" ")
ns = if (sc.hasNext()) sc.next() else ""
break
}
if (ps.isEmpty()) continue
ps = ps.substring(0, ps.length - 1)
val ct = CharacterView(activity, null, android.R.attr.textAppearanceLarge)
ct.setPadding(0, 0, 0, 0)
ct.setPadding(UnicodeAdapter.padding, UnicodeAdapter.padding, UnicodeAdapter.padding, UnicodeAdapter.padding)
ct.drawSlash(false)
ct.setTextSize(UnicodeAdapter.fontsize)
ct.text = cs
ct.setTypeface(tf, locale)
hl.addView(ct, LinearLayout.LayoutParams((activity.resources.displayMetrics.scaledDensity * UnicodeAdapter.fontsize * 2 + UnicodeAdapter.padding * 2).toInt(), ViewGroup.LayoutParams.MATCH_PARENT))
val pt = TextView(activity, null, android.R.attr.textAppearanceSmall)
pt.setPadding(0, 0, 0, 0)
pt.gravity = Gravity.CENTER_VERTICAL
pt.text = ps
if (ns.isNotEmpty()) {
val nt = TextView(activity, null, android.R.attr.textAppearanceSmall)
nt.setPadding(0, 0, 0, 0)
nt.gravity = Gravity.CENTER_VERTICAL
nt.text = ns.joinToString(" ")
val vl = LinearLayout(activity)
vl.orientation = LinearLayout.VERTICAL
vl.addView(pt, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f))
vl.addView(nt, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f))
hl.addView(vl, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
} else hl.addView(pt, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
hl.id = 0x3F000000 + str.codePointCount(0, str.length)
str.append(cs)
hl.isEnabled = true
hl.isClickable = true
hl.isFocusable = true
hl.setOnClickListener { view ->
var j = 0
while (j < cs.length) {
val code = cs.codePointAt(j)
activity.adpPage.onItemClick(null, view, -1, code.toLong())
j += Character.charCount(code)
}
++j
}
}
if (ps.isEmpty()) continue
ps = ps.substring(0, ps.length - 1)
val ct = CharacterView(activity, null, android.R.attr.textAppearanceLarge)
ct.setPadding(0, 0, 0, 0)
ct.setPadding(UnicodeAdapter.padding, UnicodeAdapter.padding, UnicodeAdapter.padding, UnicodeAdapter.padding)
ct.drawSlash(false)
ct.setTextSize(UnicodeAdapter.fontsize)
ct.text = cs
ct.setTypeface(tf, locale)
hl.addView(ct, LinearLayout.LayoutParams((activity.resources.displayMetrics.scaledDensity * UnicodeAdapter.fontsize * 2 + UnicodeAdapter.padding * 2).toInt(), ViewGroup.LayoutParams.MATCH_PARENT))
val pt = TextView(activity, null, android.R.attr.textAppearanceSmall)
pt.setPadding(0, 0, 0, 0)
pt.gravity = Gravity.CENTER_VERTICAL
pt.text = ps
if (ns != null) {
val nt = TextView(activity, null, android.R.attr.textAppearanceSmall)
nt.setPadding(0, 0, 0, 0)
nt.gravity = Gravity.CENTER_VERTICAL
nt.text = ns
val vl = LinearLayout(activity)
vl.orientation = LinearLayout.VERTICAL
vl.addView(pt, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f))
vl.addView(nt, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f))
hl.addView(vl, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
} else hl.addView(pt, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
hl.id = 0x3F000000 + str.codePointCount(0, str.length)
str.append(cs)
hl.isEnabled = true
hl.isClickable = true
hl.isFocusable = true
hl.setOnClickListener { view ->
var j = 0
while (j < cs.length) {
val code = cs.codePointAt(j)
activity.adpPage.onItemClick(null, view, -1, code.toLong())
j += Character.charCount(code)
hl.setOnLongClickListener { view ->
activity.adpPage.showDesc(null, view.id - 0x3F000000, StringAdapter(str.toString(), activity, db))
true
}
hl.setBackgroundResource(reslist)
}
hl.setOnLongClickListener { view ->
activity.adpPage.showDesc(null, view.id - 0x3F000000, StringAdapter(str.toString(), activity, db))
true
}
hl.setBackgroundResource(reslist)
} else {
it.text = (if (!emoji) mods else mode)[i]
hl.addView(it, LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT))
val desc = TextView(activity)
desc.text = s
desc.setTextIsSelectable(true)
hl.addView(desc, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f))
}
hl.setPadding(textPadding, 0, textPadding, 0)
layout.addView(hl, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
Expand Down Expand Up @@ -244,9 +249,9 @@ internal class CharacterAdapter(private val activity: UnicodeActivity, private v
var checker = 10f
var lines = true
var shrink = true
private val cols = arrayOf("name", "utf8", "version", "comment", "alias", "formal", "xref", "vari", "decomp", "compat")
private val mods = arrayOf(null, "UTF-8: ", "from Unicode ", "\u2022 ", "= ", "\u203B ", "\u2192 ", "~ ", "\u2261 ", "\u2248 ")
private val emjs = arrayOf("name", "utf8", "version", "grp", "subgrp", "", "id")
private val mode = arrayOf(null, "UTF-8: ", "from Unicode Emoji ", "Group: ", "Subgroup: ", null, "")
private val cols = arrayOf("name", "utf8", "version", "lines")
private val mods = arrayOf(null, "UTF-8: ", "from Unicode ", "")
private val emjs = arrayOf("name", "utf8", "version", "grp", "subgrp")
private val mode = arrayOf(null, "UTF-8: ", "from Unicode Emoji ", "Group: ", "Subgroup: ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class NameDatabase(context: Context) {
try {
db.rawQuery("SELECT * FROM version_code;", null).use { cur ->
cur.moveToFirst()
if (cur.getInt(0) != 59) throw SQLiteException()
if (cur.getInt(0) != 60) throw SQLiteException()
}
} catch (e: SQLiteException) {
db.close()
Expand Down
48 changes: 22 additions & 26 deletions scripts/build_namedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,34 @@ def main():
print(ftp.login())
with sqlite3.connect('namedb') as con:
cur = con.cursor()
cur.execute('CREATE TABLE name_table (id integer NOT NULL PRIMARY KEY, words text NOT NULL, name text NOT NULL, version integer NOT NULL, comment text, alias text, formal text, xref text, vari text, decomp text, compat text);')
cur.execute('CREATE TABLE name_table (id integer NOT NULL PRIMARY KEY, words text NOT NULL, name text NOT NULL, version integer NOT NULL, lines text);')
characters = {}
class OneCharacter:
id = None
name = None
version = 0
comment = []
alias = []
formal = []
xref = []
vari = []
decomp = []
compat = []
words = []
lines = []
def __init__(self, code, name, version):
self.id = int(code, 16)
self.name = name
self.version = version
self.comment = []
self.alias = []
self.formal = []
self.xref = []
self.vari = []
self.decomp = []
self.compat = []
self.words = [name]
self.lines = []
def update(self):
nonlocal characters
if self.id in characters:
self.version = characters[self.id].version
characters[self.id] = self
def append_line(self, line_type, line, is_word=False):
if is_word:
self.words.append(line.replace('\'', '\'\''))
self.lines.append(line_type + ' ' + line.replace('\'', '\'\''))
def insert(self):
nonlocal cur
def list_to_str(l):
return '\'' + '\n'.join(l) + '\'' if len(l) > 0 else 'NULL'
exp = f'INSERT INTO name_table (id, words, name, version, comment, alias, formal, xref, vari, decomp, compat) values ({self.id}, \'{" ".join([self.name] + self.alias + self.formal)}\', \'{self.name}\', {self.version}, {list_to_str(self.comment)}, {list_to_str(self.alias)}, {list_to_str(self.formal)}, {list_to_str(self.xref)}, {list_to_str(self.vari)}, {list_to_str(self.decomp)}, {list_to_str(self.compat)});'
exp = f'INSERT INTO name_table (id, words, name, version, lines) values ({self.id}, \'{" ".join(self.words)}\', \'{self.name}\', {self.version}, {list_to_str(self.lines)});'
try:
cur.execute(exp)
except:
Expand All @@ -78,9 +72,9 @@ def oneline(line):
nonlocal current
if len(line) == 0:
return
elif line[0] in ['@', ';']:
elif line[0] in ['@', ';'] and not line.startswith('@+'):
return
elif line[0] == '\t':
elif line[0] == '\t' or line.startswith('@+'):
if current is None:
return
if len(line) < 3:
Expand All @@ -89,19 +83,21 @@ def oneline(line):
if line[1] == '\t':
return
if line[1] == '*':
current.comment.append(line[3:].replace('\'', '\'\''))
current.append_line('*', line[3:])
if line.startswith('@+'):
current.append_line('@', line[3:].lstrip('\t').lstrip('* '))
if line[1] == '=':
current.alias.append(line[3:].replace('\'', '\'\''))
current.append_line('=', line[3:], True)
if line[1] == '%':
current.formal.append(line[3:].replace('\'', '\'\''))
current.append_line('%', line[3:], True)
if line[1] == 'x':
current.xref.append((line.split(' ')[-1][:-1] if line[3] == '(' else line[3:]).lstrip('0').replace('\'', '\'\''))
current.append_line('x', (line.split(' - ')[0][4:] + ' ' + line.split(' ')[-1][:-1] if line[3] == '(' else line[3:]).lstrip('0'))
if line[1] == '~':
current.vari.append(' '.join(s.lstrip('0') for s in line[3:].split(' ')).replace('\'', '\'\''))
current.append_line('~', line[3:])
if line[1] == ':':
current.decomp.append(' '.join(s.lstrip('0') for s in line[3:].split(' ') if re.match('^[0-9A-F]+$', s)).replace('\'', '\'\''))
current.append_line(':', line[3:])
if line[1] == '#':
current.compat.append(' '.join(s.lstrip('0') for s in line[3:].split(' ') if re.match('^([0-9A-F]+|<.*>)$', s)).replace('\'', '\'\''))
current.append_line('#', line[3:])
else:
if current is not None:
current.update()
Expand Down Expand Up @@ -167,7 +163,7 @@ def emoji_line(line):
cur.execute('CREATE TABLE emoji_table1510 (id text NOT NULL PRIMARY KEY, name text NOT NULL, version integer NOT NULL, grp text NOT NULL, subgrp text NOT NULL, tone integer NOT NULL, direction integer NOT NULL);')
print(ftp.retrlines(f'RETR emoji-test.txt', emoji_line))
con.commit()
cur.execute('CREATE TABLE version_code as SELECT 59 as version;')
cur.execute('CREATE TABLE version_code as SELECT 60 as version;')
con.commit()
print('SELECT * FROM \'version_code\';')
cur.execute('SELECT * FROM \'version_code\';')
Expand Down

0 comments on commit bb9a995

Please sign in to comment.