Skip to content

Commit 1ce4818

Browse files
Merge pull request #18 from Omega-R/develop
Add typeface support for TextStyle
2 parents 9525b0a + 42721a7 commit 1ce4818

File tree

11 files changed

+158
-41
lines changed

11 files changed

+158
-41
lines changed
-4 Bytes
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/omega_r/com/omegatypesexample/MainActivity.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.graphics.BitmapFactory
55
import android.os.Bundle
66
import android.widget.ImageView
77
import android.widget.TextView
8+
import androidx.core.content.res.ResourcesCompat
89
import com.omega_r.libs.omegatypes.*
910
import kotlin.concurrent.thread
1011

@@ -16,7 +17,16 @@ class MainActivity : BaseActivity() {
1617
override fun onCreate(savedInstanceState: Bundle?) {
1718
super.onCreate(savedInstanceState)
1819
setContentView(R.layout.activity_main)
19-
val text = Text.from("test ") + Text.from(R.string.hello_world, Text.from(R.string.app_name), textStyle = TextStyle.bold())
20+
val text = Text.from("test ") +
21+
Text.from(
22+
R.string.hello_world,
23+
Text.from(R.string.app_name),
24+
textStyle = TextStyle.font(ResourcesCompat.getFont(this, R.font.noto_sans_regular)!!)
25+
) +
26+
Text.from(
27+
" SEMI BOLD",
28+
textStyle = TextStyle.font(ResourcesCompat.getFont(this, R.font.noto_sans_semi_bold)!!)
29+
)
2030
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
2131
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
2232

306 KB
Binary file not shown.
306 KB
Binary file not shown.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
jcenter()
1212
}
1313
dependencies {
14-
classpath 'com.android.tools.build:gradle:3.2.0'
14+
classpath 'com.android.tools.build:gradle:3.4.1'
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1616
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
1717
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Oct 03 15:12:40 MSK 2018
1+
#Wed May 29 11:12:26 MSK 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.omega_r.libs.omegatypes
2+
3+
import android.graphics.Typeface
4+
import android.os.Parcel
5+
import android.os.Process
6+
import java.util.ArrayList
7+
8+
object LeakyTypefaceStorageCompat {
9+
10+
private val lock = Any()
11+
12+
private val storage = ArrayList<Typeface>()
13+
14+
/**
15+
* Write typeface to parcel.
16+
*
17+
* You can't transfer Typeface to a different process. [readTypefaceFromParcel] will
18+
* return `null` if the [readTypefaceFromParcel] is called in a different process.
19+
*
20+
* @param typeface A [Typeface] to be written.
21+
* @param parcel A [Parcel] object.
22+
*/
23+
fun writeTypefaceToParcel(typeface: Typeface, parcel: Parcel) {
24+
parcel.writeInt(Process.myPid())
25+
synchronized(lock) {
26+
val id: Int
27+
val i = storage.indexOf(typeface)
28+
if (i != -1) {
29+
id = i
30+
} else {
31+
id = storage.size
32+
storage.add(typeface)
33+
}
34+
parcel.writeInt(id)
35+
}
36+
}
37+
38+
/**
39+
* Read typeface from parcel.
40+
*
41+
* If the [Typeface] was created in another process, this method returns null.
42+
*
43+
* @param parcel A [Parcel] object
44+
* @return A [Typeface] object.
45+
*/
46+
fun readTypefaceFromParcel(parcel: Parcel): Typeface? {
47+
val pid = parcel.readInt()
48+
val typefaceId = parcel.readInt()
49+
if (pid != Process.myPid()) {
50+
return null // The Typeface was created and written in another process.
51+
}
52+
synchronized(lock) {
53+
return storage[typefaceId]
54+
}
55+
}
56+
}

omegatypes/src/main/java/com/omega_r/libs/omegatypes/TextStyle.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ abstract class TextStyle : Serializable {
4242
fun strikethrough(): TextStyle = strikethroughTextStyle
4343

4444
@JvmStatic
45-
fun font(fontName: Text): TextStyle = FontTextStyle(fontName)
45+
fun font(fontName: Text): TextStyle = NameFontTextStyle(fontNameText = fontName)
4646

4747
@JvmStatic
48-
fun font(fontName: String): TextStyle = FontTextStyle(fontName.toText())
48+
fun font(fontName: String): TextStyle = NameFontTextStyle(fontNameText = fontName.toText())
49+
50+
@JvmStatic
51+
fun font(typeface: Typeface): TextStyle = TypefaceFontTextStyle(fontTypeface = typeface)
4952

5053
@JvmStatic
5154
fun size(size: Size): TextStyle = SizeTextStyle(size)
5255

5356
}
5457

55-
operator fun plus(textStyle: TextStyle?):TextStyle {
58+
operator fun plus(textStyle: TextStyle?): TextStyle {
5659
val list = mutableListOf<TextStyle>().apply {
5760
addTextStyle(this@TextStyle)
5861
addTextStyle(textStyle)
@@ -115,12 +118,20 @@ abstract class TextStyle : Serializable {
115118
}
116119
}
117120

118-
private class FontTextStyle(private val fontNameText: Text) : TextStyle() {
121+
private class NameFontTextStyle(private val fontNameText: Text) : TextStyle() {
122+
123+
override fun SpannableString.applyStyle(context: Context) {
124+
fontNameText.getString(context)?.let {
125+
setSpan(TypefaceSpan(it), 0, length, 0)
126+
}
127+
}
128+
129+
}
130+
131+
private class TypefaceFontTextStyle(private val fontTypeface: Typeface) : TextStyle() {
119132

120133
override fun SpannableString.applyStyle(context: Context) {
121-
fontNameText.getString(context)?.let {
122-
setSpan(TypefaceSpan(it),0, length, 0)
123-
}
134+
setSpan(TypefaceSpanCompat(typeface = fontTypeface), 0, length, 0)
124135
}
125136

126137
}

0 commit comments

Comments
 (0)