@@ -179,55 +179,61 @@ def text_( # noqa: PLR0912
179
179
kwargs = self ._preprocess (** kwargs )
180
180
181
181
# Ensure inputs are either textfiles, x/y/text, or position/text
182
- if position is None :
183
- if any (v is not None for v in (x , y , text )) and textfiles is not None :
184
- raise GMTInvalidInput (
185
- "Provide either position only, or x/y pairs, or textfiles."
186
- )
187
- kind = data_kind (textfiles )
188
- if kind == "vectors" and text is None :
189
- raise GMTInvalidInput ("Must provide text with x/y pairs" )
190
- else :
191
- if any (v is not None for v in (x , y , textfiles )):
192
- raise GMTInvalidInput (
193
- "Provide either position only, or x/y pairs, or textfiles."
194
- )
195
- if text is None or is_nonstr_iter (text ):
196
- raise GMTInvalidInput ("Text can't be None or array." )
197
- kind = None
198
- textfiles = ""
182
+ if (
183
+ (textfiles is not None )
184
+ + (position is not None )
185
+ + (x is not None or y is not None )
186
+ ) != 1 :
187
+ raise GMTInvalidInput ("Provide either textfiles, x/y/text, or position/text." )
188
+
189
+ required_data = position is None
190
+ kind = data_kind (textfiles , required = required_data )
199
191
200
- # Build the -F option in gmt text.
192
+ if position is not None and (text is None or is_nonstr_iter (text )):
193
+ raise GMTInvalidInput ("'text' can't be None or array when 'position' is given." )
194
+ if textfiles is not None and text is not None :
195
+ raise GMTInvalidInput ("'text' can't be specified when 'textfiles' is given." )
196
+ if kind == "vectors" and text is None :
197
+ raise GMTInvalidInput ("Must provide text with x/y pairs." )
198
+
199
+ # Arguments that can accept arrays.
200
+ array_args = [
201
+ (angle , "+a" , "angle" ),
202
+ (font , "+f" , "font" ),
203
+ (justify , "+j" , "justify" ),
204
+ ]
205
+
206
+ # Build the -F option.
201
207
if kwargs .get ("F" ) is None and any (
202
208
v is not None for v in (position , angle , font , justify )
203
209
):
204
210
kwargs .update ({"F" : "" })
205
211
206
- extra_arrays = []
207
- for arg , flag in [(angle , "+a" ), (font , "+f" ), (justify , "+j" )]:
212
+ for arg , flag , _ in array_args :
208
213
if arg is True :
209
214
kwargs ["F" ] += flag
210
- elif is_nonstr_iter (arg ):
211
- kwargs ["F" ] += flag
212
- if flag == "+a" : # angle is numeric type
213
- extra_arrays .append (np .atleast_1d (arg ))
214
- else : # font or justify is str type
215
- extra_arrays .append (np .atleast_1d (arg ).astype (str ))
216
215
elif isinstance (arg , int | float | str ):
217
216
kwargs ["F" ] += f"{ flag } { arg } "
218
217
219
- if isinstance (position , str ):
220
- kwargs ["F" ] += f"+c{ position } +t{ text } "
221
-
222
- # If an array of transparency is given, GMT will read it from
223
- # the last numerical column per data record.
224
- if is_nonstr_iter (kwargs .get ("t" )):
225
- extra_arrays .append (kwargs ["t" ])
226
- kwargs ["t" ] = ""
227
-
228
- # Append text at last column. Text must be passed in as str type.
218
+ extra_arrays = []
229
219
confdict = {}
230
220
if kind == "vectors" :
221
+ for arg , flag , name in array_args :
222
+ if is_nonstr_iter (arg ):
223
+ kwargs ["F" ] += flag
224
+ # angle is numeric type and font/justify are str type.
225
+ if name == "angle" :
226
+ extra_arrays .append (np .atleast_1d (arg ))
227
+ else :
228
+ extra_arrays .append (np .atleast_1d (arg ).astype (str ))
229
+
230
+ # If an array of transparency is given, GMT will read it from the last numerical
231
+ # column per data record.
232
+ if is_nonstr_iter (kwargs .get ("t" )):
233
+ extra_arrays .append (np .atleast_1d (kwargs ["t" ]))
234
+ kwargs ["t" ] = True
235
+
236
+ # Append text to the last column. Text must be passed in as str type.
231
237
text = np .atleast_1d (text ).astype (str )
232
238
encoding = _check_encoding ("" .join (text ))
233
239
if encoding != "ascii" :
@@ -238,10 +244,23 @@ def text_( # noqa: PLR0912
238
244
239
245
if encoding not in {"ascii" , "ISOLatin1+" }:
240
246
confdict = {"PS_CHAR_ENCODING" : encoding }
247
+ else :
248
+ if isinstance (position , str ):
249
+ kwargs ["F" ] += f"+c{ position } +t{ text } "
250
+
251
+ for arg , _ , name in [* array_args , (kwargs .get ("t" ), "" , "transparency" )]:
252
+ if is_nonstr_iter (arg ):
253
+ msg = f"Argument of '{ name } ' must be a single value or True."
254
+ raise GMTInvalidInput (msg )
241
255
242
256
with Session () as lib :
243
257
with lib .virtualfile_in (
244
- check_kind = "vector" , data = textfiles , x = x , y = y , extra_arrays = extra_arrays
258
+ check_kind = "vector" ,
259
+ data = textfiles ,
260
+ x = x ,
261
+ y = y ,
262
+ extra_arrays = extra_arrays ,
263
+ required_data = required_data ,
245
264
) as vintbl :
246
265
lib .call_module (
247
266
module = "text" ,
0 commit comments