Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix #202 generates inconsistent text size across different devices for 1.1.x #203

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Build and Test
on:
name: CI
on:
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:

concurrency:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}
cancel-in-progress: true

Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
ruby-version: 2.7
bundler-cache: true

- name: Setup node 16
- name: Setup node
if: steps.verify-dev-changed-files.outputs.any_changed == 'true'
uses: actions/setup-node@v3
with:
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
assets/**
package.json
!example/ios/**

- uses: actions/cache@v3
name: Cache node_modules
if: steps.verify-android-changed-files.outputs.any_changed == 'true'
Expand Down Expand Up @@ -107,7 +107,7 @@ jobs:
ruby-version: 2.7
bundler-cache: true

- name: Setup node 16
- name: Setup node
uses: actions/setup-node@v3
if: steps.verify-android-changed-files.outputs.any_changed == 'true'
with:
Expand All @@ -119,7 +119,7 @@ jobs:
with:
distribution: 'zulu'
java-version: 11

- name: Install Gradle dependencies
if: steps.cache-gradle.outputs.cache-hit != 'true' && steps.verify-android-changed-files.outputs.any_changed == 'true'
run: |
Expand All @@ -128,7 +128,7 @@ jobs:

- name: Run unit tests
if: steps.verify-android-changed-files.outputs.any_changed == 'true'
run: |
run: |
cd example/android
./gradlew test --stacktrace

Expand All @@ -153,7 +153,7 @@ jobs:
name: Android Test
strategy:
matrix:
api-level: [24, 25, 29, 30, 31]
api-level: [24, 30, 31]
target: [default]
steps:
- name: Checkout the code
Expand Down Expand Up @@ -199,7 +199,7 @@ jobs:
ruby-version: 2.7
bundler-cache: true

- name: Setup node 16
- name: Setup node
uses: actions/setup-node@v3
if: steps.verify-android-changed-files.outputs.any_changed == 'true'
with:
Expand Down Expand Up @@ -262,7 +262,7 @@ jobs:
example/node_modules
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package.json') }}-${{ hashFiles('example/package.json') }}
fail-on-cache-miss: true

- name: Cache Pods
id: cache-pods
uses: actions/cache@v3
Expand All @@ -277,12 +277,12 @@ jobs:
with:
ruby-version: 2.7
bundler-cache: true

- name: Install Cocoapods
if: steps.verify-iOS-changed-files.outputs.any_changed == 'true'
run: gem install cocoapods -v ${{ matrix.cocoapods }}

- name: Setup node 16
- name: Setup node
if: steps.verify-iOS-changed-files.outputs.any_changed == 'true'
uses: actions/setup-node@v3
with:
Expand All @@ -301,13 +301,13 @@ jobs:

- name: Build
if: steps.verify-iOS-changed-files.outputs.any_changed == 'true'
run: |
run: |
cd example/ios
xcodebuild -workspace ImageMarkerExample.xcworkspace -scheme ImageMarkerExample -configuration Release -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12' | xcpretty

- name: Test
if: steps.verify-iOS-changed-files.outputs.any_changed == 'true'
run: |
run: |
cd example/ios
xcodebuild -workspace ImageMarkerExample.xcworkspace -scheme ImageMarkerExample -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 12' test | xcpretty

Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@


## [1.1.14](https://github.com/JimmyDaddy/react-native-image-marker/compare/v1.1.13...v1.1.14) (2024-01-24)


### Bug Fixes

* set the font size without considering the screen density ([da84135](https://github.com/JimmyDaddy/react-native-image-marker/commit/da8413520f8adc185048ea89a21c28a55f2689c5))


### Features

* replace positionOptions to postions ([8077617](https://github.com/JimmyDaddy/react-native-image-marker/commit/8077617b5b5eadf3d239d610853d76220852f5de))

## [1.1.13](https://github.com/JimmyDaddy/react-native-image-marker/compare/v1.1.12...v1.1.13) (2024-01-22)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data class TextOptions(val options: ReadableMap) {
throw MarkerError(ErrorCode.PARAMS_REQUIRED, "mark text is required")
}
val positionOptions =
if (null != options.getMap("positionOptions")) options.getMap("positionOptions") else null
if (null != options.getMap("position")) options.getMap("position") else null
x = if (positionOptions!!.hasKey("X")) Utils.handleDynamicToString(positionOptions.getDynamic("X")) else null
y = if (positionOptions.hasKey("Y")) Utils.handleDynamicToString(positionOptions.getDynamic("Y")) else null
positionEnum =
Expand Down Expand Up @@ -69,11 +69,12 @@ data class TextOptions(val options: ReadableMap) {
Typeface.DEFAULT
}
}
val textSize = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
style.fontSize,
context.resources.displayMetrics
)
// val textSize = TypedValue.applyDimension(
// TypedValue.COMPLEX_UNIT_SP,
// style.fontSize,
// context.resources.displayMetrics
// )
val textSize = style.fontSize
textPaint.isAntiAlias = true
textPaint.textSize = textSize
Log.i(Constants.IMAGE_MARKER_TAG, "textSize: " + textSize + " fontSize: " + style.fontSize + " displayMetrics: " + context.resources.displayMetrics)
Expand Down Expand Up @@ -121,7 +122,7 @@ data class TextOptions(val options: ReadableMap) {
).toInt()
}
val margin = DEFAULT_MARGIN
var position = Position(margin.toFloat(), margin.toFloat())
var position = Position(margin, margin)
if (positionEnum != null) {
position = Position.getTextPosition(
positionEnum,
Expand Down Expand Up @@ -168,9 +169,9 @@ data class TextOptions(val options: ReadableMap) {
if (style.textBackgroundStyle!!.cornerRadius != null) {
val path = Path()

path.addRoundRect(bgRect, style.textBackgroundStyle!!.cornerRadius!!.radii(bgRect), Path.Direction.CW);
path.addRoundRect(bgRect, style.textBackgroundStyle!!.cornerRadius!!.radii(bgRect), Path.Direction.CW)

canvas.drawPath(path, paint);
canvas.drawPath(path, paint)
} else {
canvas.drawRect(bgRect, paint)
}
Expand All @@ -179,6 +180,7 @@ data class TextOptions(val options: ReadableMap) {
Paint.Align.RIGHT -> x + textWidth
Paint.Align.CENTER -> x + textWidth / 2
Paint.Align.LEFT -> x
else -> x
}
canvas.translate(textX, y)
textLayout.draw(canvas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class TextStyle(val options: ReadableMap?) {
var shadowLayerStyle: ShadowLayerStyle?
var textBackgroundStyle: TextBackgroundStyle?
var underline: Boolean = if (options?.hasKey("underline") == true) options.getBoolean("underline") else false
var skewX: Float? = if (options?.hasKey("skewX") == true) options.getDouble("skewX")?.toFloat() else 0f
var skewX: Float? = if (options?.hasKey("skewX") == true) options.getDouble("skewX").toFloat() else 0f
var strikeThrough: Boolean = if (options?.hasKey("strikeThrough") == true) options.getBoolean("strikeThrough") else false
var textAlign: Align
var italic: Boolean = if (options?.hasKey("italic") == true) options.getBoolean("italic") else false
Expand Down
52 changes: 33 additions & 19 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,11 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else setBackgroundFormat(options[buttonIndex] as any);
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setBackgroundFormat(options[buttonIndex] as any);
}
}
);
}
Expand All @@ -309,8 +312,11 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else setWaterMarkType(options[buttonIndex] as any);
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setWaterMarkType(options[buttonIndex] as any);
}
}
);
}
Expand All @@ -332,8 +338,11 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else setSaveFormat(options[buttonIndex] as any);
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setSaveFormat(options[buttonIndex] as any);
}
}
);
}
Expand All @@ -359,8 +368,9 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setPosition(options[buttonIndex] as any);
}
}
Expand All @@ -384,8 +394,9 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setTextBgStretch(options[buttonIndex] as any);
}
}
Expand All @@ -404,8 +415,9 @@ function useViewModel() {
useModal: true,
},
(buttonIndex) => {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) return;
else {
if (buttonIndex === cancelButtonIndex || buttonIndex == null) {
return;
} else {
setTextAlign(options[buttonIndex] as any);
}
}
Expand Down Expand Up @@ -514,7 +526,7 @@ function useViewModel() {
},
},
{
text: `text marker normal`,
text: 'text marker normal',
positionOptions: {
position: Position.center,
},
Expand Down Expand Up @@ -560,13 +572,14 @@ function useViewModel() {
saveFormat: saveFormat,
});
}
setUri(
const resUri =
saveFormat === ImageFormat.base64
? path
: Platform.OS === 'android'
? 'file:' + path
: path
);
: path;
setUri(resUri);
console.log(resUri);
setLoading(false);
setShow(true);
const stat = await RNBlobUtil.fs.stat(path);
Expand Down Expand Up @@ -698,13 +711,14 @@ function useViewModel() {
saveFormat: saveFormat,
});
}
setUri(
const resUri =
saveFormat === ImageFormat.base64
? path
: Platform.OS === 'android'
? 'file:' + path
: path
);
: path;
setUri(resUri);
console.log(resUri);
setShow(true);
setLoading(false);
const stat = await RNBlobUtil.fs.stat(path);
Expand Down
2 changes: 1 addition & 1 deletion ios/RCTImageMarker/TextOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TextOptions: NSObject {
throw NSError(domain: ErrorDomainEnum.PARAMS_REQUIRED.rawValue, code: 0, userInfo: [NSLocalizedDescriptionKey: "text is required"])
}

if let positionOpts = opts["positionOptions"] as? [AnyHashable: Any] {
if let positionOpts = opts["position"] as? [AnyHashable: Any] {
self.X = Utils.handleDynamicToString(v: positionOpts["X"])
self.Y = Utils.handleDynamicToString(v: positionOpts["Y"])
self.position = positionOpts["position"] != nil ? RCTConvert.MarkerPosition(positionOpts["position"]) : .none
Expand Down
3 changes: 1 addition & 2 deletions ios/RCTImageMarker/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class TextStyle: NSObject {
self.shadow = nil
}
self.textBackground = try TextBackground(textBackgroundStyle: (opts["textBackgroundStyle"] as? [AnyHashable : Any]))
let scale = UIScreen.main.scale
let fontSize = opts["fontSize"] != nil ? (RCTConvert.cgFloat(opts["fontSize"]) * scale) : (14.0 * scale)
let fontSize = opts["fontSize"] != nil ? RCTConvert.cgFloat(opts["fontSize"]) : 14.0
self.font = UIFont(name: opts["fontName"] as? String ?? "", size: fontSize)
if self.font == nil {
self.font = UIFont.systemFont(ofSize: fontSize)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-image-marker",
"version": "1.1.13",
"version": "1.1.14",
"description": "Add text or icon watermark to your images",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export interface TextBackgroundStyle extends Padding {
* @example
* type: TextBackgroundType.stretchX
**/
type: TextBackgroundType | null;
type?: TextBackgroundType | null;
/**
* @description background color
* @example
Expand Down Expand Up @@ -452,6 +452,7 @@ export interface TextOptions {
**/
text: string;
/**
* @deprecated since 1.2.4 use position instead
* @description text position options
* @example
* positionOptions: {
Expand All @@ -462,6 +463,19 @@ export interface TextOptions {
* }
*/
positionOptions?: PositionOptions;

/**
* @description text position options
* @example
* positionOptions: {
* X: 10,
* Y: 10,
* // or
* // position: Position.center
* }
*/
position?: PositionOptions;

/**
* @description text style
* @example
Expand Down Expand Up @@ -922,6 +936,11 @@ class Marker {
};
}

options.watermarkTexts.forEach((item) => {
item.position = item.position || item.positionOptions;
delete item.positionOptions;
});

options.backgroundImage.src = srcObj;
// let mShadowStyle = shadowStyle || {};
// let mTextBackgroundStyle = textBackgroundStyle || {};
Expand Down
Loading