Skip to content

Commit a39fed7

Browse files
committed
fix: new Paint constructor to quickly clone a Paint
1 parent 1c347cd commit a39fed7

File tree

3 files changed

+90
-71
lines changed

3 files changed

+90
-71
lines changed

src/canvas.android.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,11 @@ export class Paint extends ProxyClass<android.graphics.Paint> {
284284
}
285285
return this.mNative;
286286
}
287-
constructor() {
287+
constructor(paint?: Paint) {
288288
super();
289-
this.mNative = new android.graphics.Paint();
289+
if (paint) {
290+
this.mNative = new android.graphics.Paint(paint?.getNative());
291+
}
290292
this.mNative.setLinearText(true); // ensure we are drawing fonts correctly
291293
return this;
292294
}

src/canvas.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ColorParam = Color | number | string;
1818
// export * from './canvas.android'
1919

2020
export class Paint {
21+
constructor(paint?: Paint);
2122
color: ColorParam;
2223
setColor(color: ColorParam);
2324

src/canvas.ios.ts

Lines changed: 85 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,47 @@ export class FontMetrics implements IFontMetrics {
846846
}
847847

848848
export class Paint implements IPaint {
849+
mColor: Color = new Color('black');
850+
style: Style = Style.FILL;
851+
align: Align = Align.LEFT;
852+
mFont: Font;
853+
strokeWidth = 0;
854+
strokeMiter = 0;
855+
strokeCap: Cap = Cap.BUT;
856+
strokeJoin: Join = Join.BEVEL;
857+
antiAlias;
858+
dither;
859+
alpha = 255;
860+
currentContext: any;
861+
shadowLayer?: {
862+
radius: number;
863+
dx: number;
864+
dy: number;
865+
color: Color;
866+
};
867+
shader;
868+
pathEffect: PathEffect;
869+
xfermode: IPorterDuffXfermode;
870+
mTextAttribs: NSMutableDictionary<any, any>;
871+
constructor(paint?: Paint) {
872+
if (paint) {
873+
this.mColor = paint.mColor;
874+
this.style = paint.style;
875+
this.align = paint.align;
876+
this.mFont = paint.mFont;
877+
this.strokeWidth = paint.strokeWidth;
878+
this.strokeMiter = paint.strokeMiter;
879+
this.strokeCap = paint.strokeCap;
880+
this.strokeJoin = paint.strokeJoin;
881+
this.antiAlias = paint.antiAlias;
882+
this.dither = paint.dither;
883+
this.alpha = paint.alpha;
884+
this.shadowLayer = paint.shadowLayer;
885+
this.shader = paint.shader;
886+
this.pathEffect = paint.pathEffect;
887+
this.xfermode = paint.xfermode;
888+
}
889+
}
849890
getTextPath(text: string, start: number, end: number, x: number, y: number, path: Path) {
850891
const bPath = UIBezierPath.fromStringWithFont(text.slice(start, end), this.getUIFont());
851892
const bounds = bPath.bounds;
@@ -860,30 +901,11 @@ export class Paint implements IPaint {
860901

861902
public setTextAlign(align: Align): void {
862903
this.align = align;
863-
this._textAttribs = null;
904+
this.mTextAttribs = null;
864905
}
865906
public getTextAlign(): Align {
866907
return this.align;
867908
}
868-
_color: Color = new Color('black');
869-
style: Style = Style.FILL;
870-
align: Align = Align.LEFT;
871-
_font: Font;
872-
strokeWidth = 0;
873-
strokeMiter = 0;
874-
strokeCap: Cap = Cap.BUT;
875-
strokeJoin: Join = Join.BEVEL;
876-
antiAlias = false;
877-
dither = false;
878-
alpha = 255;
879-
currentContext: any;
880-
shadowLayer?: {
881-
radius: number;
882-
dx: number;
883-
dy: number;
884-
color: Color;
885-
};
886-
shader;
887909

888910
public setShadowLayer(radius: number, dx: number, dy: number, color: number | Color | string): void {
889911
color = color instanceof Color ? color : new Color(color as any);
@@ -908,7 +930,7 @@ export class Paint implements IPaint {
908930
this.strokeMiter = value;
909931
}
910932
public setARGB(a: number, r: number, g: number, b: number): void {
911-
this._color = new Color(a, r, g, b);
933+
this.mColor = new Color(a, r, g, b);
912934
}
913935
public measureText(text: string, start = 0, end?) {
914936
if (end === undefined) {
@@ -977,36 +999,33 @@ export class Paint implements IPaint {
977999
}
9781000
this.shader = value;
9791001
}
980-
constructor() {
981-
// this.font = Font.default;
982-
}
9831002
setFont(font: Font) {
984-
if (font === this._font) {
1003+
if (font === this.mFont) {
9851004
return;
9861005
}
987-
this._font = font;
988-
this._textAttribs = null;
1006+
this.mFont = font;
1007+
this.mTextAttribs = null;
9891008
}
9901009
public setTypeface(font: Font | UIFont): Font {
9911010
if (this.font === font) {
992-
return this._font;
1011+
return this.mFont;
9931012
}
9941013
if (font instanceof Font) {
9951014
this.setFont(font);
996-
return this._font;
1015+
return this.mFont;
9971016
} else if (font) {
998-
this._font['_uiFont'] = font;
1017+
this.mFont['_uiFont'] = font;
9991018
} else {
1000-
this._font = null;
1019+
this.mFont = null;
10011020
}
1002-
this._textAttribs = null;
1003-
return this._font;
1021+
this.mTextAttribs = null;
1022+
return this.mFont;
10041023
}
10051024
getFont() {
1006-
if (!this._font) {
1007-
this._font = Font.default;
1025+
if (!this.mFont) {
1026+
this.mFont = Font.default;
10081027
}
1009-
return this._font;
1028+
return this.mFont;
10101029
}
10111030
get font() {
10121031
return this.getFont();
@@ -1016,11 +1035,11 @@ export class Paint implements IPaint {
10161035
}
10171036

10181037
setFontFamily(familyName: string) {
1019-
if (this._font && this._font.fontFamily === familyName) {
1038+
if (this.mFont && this.mFont.fontFamily === familyName) {
10201039
return;
10211040
}
1022-
this._font = this.font.withFontFamily(familyName);
1023-
this._textAttribs = null;
1041+
this.mFont = this.font.withFontFamily(familyName);
1042+
this.mTextAttribs = null;
10241043
}
10251044
set fontFamily(familyName: string) {
10261045
this.setFontFamily(familyName);
@@ -1033,35 +1052,35 @@ export class Paint implements IPaint {
10331052
}
10341053

10351054
set fontWeight(weight: FontWeight) {
1036-
if (this._font && this._font.fontWeight === weight) {
1055+
if (this.mFont && this.mFont.fontWeight === weight) {
10371056
return;
10381057
}
10391058
this.setFontWeight(weight);
10401059
}
10411060
setFontWeight(weight: FontWeight) {
1042-
this._font = this.font.withFontWeight(weight);
1043-
this._textAttribs = null;
1061+
this.mFont = this.font.withFontWeight(weight);
1062+
this.mTextAttribs = null;
10441063
}
10451064
set fontStyle(style: FontStyle) {
10461065
this.setFontStyle(style);
10471066
}
10481067
setFontStyle(style: FontStyle) {
1049-
if (this._font && this._font.fontStyle === style) {
1068+
if (this.mFont && this.mFont.fontStyle === style) {
10501069
return;
10511070
}
1052-
this._font = this.font.withFontStyle(style);
1053-
this._textAttribs = null;
1071+
this.mFont = this.font.withFontStyle(style);
1072+
this.mTextAttribs = null;
10541073
}
10551074

10561075
getUIFont(): UIFont {
10571076
return this.font.getUIFont(UIFont.systemFontOfSize(UIFont.labelFontSize));
10581077
}
10591078
getUIColor() {
1060-
return this._color && (this._color.ios as UIColor);
1079+
return this.mColor && (this.mColor.ios as UIColor);
10611080
}
10621081

10631082
public getTextSize(): number {
1064-
return this._font ? this._font.fontSize : UIFont.labelFontSize;
1083+
return this.mFont ? this.mFont.fontSize : UIFont.labelFontSize;
10651084
}
10661085

10671086
set textSize(textSize) {
@@ -1071,40 +1090,40 @@ export class Paint implements IPaint {
10711090
return this.getTextSize();
10721091
}
10731092
setTextSize(textSize) {
1074-
if (this._font && this._font.fontSize === textSize) {
1093+
if (this.mFont && this.mFont.fontSize === textSize) {
10751094
return;
10761095
}
1077-
this._font = this.font.withFontSize(textSize);
1078-
this._textAttribs = null;
1096+
this.mFont = this.font.withFontSize(textSize);
1097+
this.mTextAttribs = null;
10791098
}
10801099
get color(): Color | number | string {
1081-
return this._color;
1100+
return this.mColor;
10821101
}
10831102
set color(color: Color | number | string) {
10841103
this.setColor(color);
10851104
}
10861105
setColor(color: Color | number | string) {
10871106
if (color instanceof Color) {
1088-
this._color = color;
1107+
this.mColor = color;
10891108
} else if (!!color) {
1090-
this._color = new Color(color as any);
1109+
this.mColor = new Color(color as any);
10911110
} else {
1092-
this._color = undefined;
1111+
this.mColor = undefined;
10931112
}
1094-
if (this._color) {
1113+
if (this.mColor) {
10951114
// on android setColor sets the alpha too
1096-
const c = this._color;
1115+
const c = this.mColor;
10971116
this.alpha = c.a;
10981117
// we want to ignore color alpha because on android it is not used but
10991118
// on ios it will add up
1100-
this._color = new Color(255, c.r, c.g, c.b);
1119+
this.mColor = new Color(255, c.r, c.g, c.b);
11011120
} else {
11021121
this.alpha = 255;
11031122
}
1104-
this._textAttribs = null;
1123+
this.mTextAttribs = null;
11051124
}
11061125
getColor(): Color {
1107-
return this._color;
1126+
return this.mColor;
11081127
}
11091128

11101129
clear() {
@@ -1113,12 +1132,10 @@ export class Paint implements IPaint {
11131132
this.shader = null;
11141133
}
11151134
}
1116-
pathEffect: PathEffect;
11171135
public setPathEffect(param0: PathEffect) {
11181136
this.pathEffect = param0;
11191137
}
11201138

1121-
xfermode: IPorterDuffXfermode;
11221139
public setXfermode(param0: IPorterDuffXfermode): IPorterDuffXfermode {
11231140
this.xfermode = param0;
11241141
return param0;
@@ -1200,26 +1217,25 @@ export class Paint implements IPaint {
12001217
// CGContextAddPath(ctx, path);
12011218
}
12021219
}
1203-
_textAttribs: NSMutableDictionary<any, any>;
12041220
getDrawTextAttribs() {
1205-
if (!this._textAttribs) {
1206-
this._textAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([this.getUIFont()], [NSFontAttributeName]);
1221+
if (!this.mTextAttribs) {
1222+
this.mTextAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([this.getUIFont()], [NSFontAttributeName]);
12071223
const color = this.getUIColor();
1208-
this._textAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([this.getUIFont()], [NSFontAttributeName]);
1224+
this.mTextAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([this.getUIFont()], [NSFontAttributeName]);
12091225
if (color) {
1210-
this._textAttribs.setObjectForKey(color, NSForegroundColorAttributeName);
1226+
this.mTextAttribs.setObjectForKey(color, NSForegroundColorAttributeName);
12111227
}
12121228
if (this.align === Align.CENTER) {
12131229
const paragraphStyle = NSMutableParagraphStyle.new();
12141230
paragraphStyle.alignment = NSTextAlignment.Center;
1215-
this._textAttribs.setObjectForKey(paragraphStyle, NSParagraphStyleAttributeName);
1231+
this.mTextAttribs.setObjectForKey(paragraphStyle, NSParagraphStyleAttributeName);
12161232
} else if (this.align === Align.RIGHT) {
12171233
const paragraphStyle = NSMutableParagraphStyle.new();
12181234
paragraphStyle.alignment = NSTextAlignment.Right;
1219-
this._textAttribs.setObjectForKey(paragraphStyle, NSParagraphStyleAttributeName);
1235+
this.mTextAttribs.setObjectForKey(paragraphStyle, NSParagraphStyleAttributeName);
12201236
}
12211237
}
1222-
return this._textAttribs;
1238+
return this.mTextAttribs;
12231239
}
12241240
}
12251241

0 commit comments

Comments
 (0)