It is quite common for paper topographic maps to label the map graticule with ordinate values in a mixture of superscript and standard digits, eg. ⁴123⁰⁰⁰ to emphasise the significant digits.
The QGIS Map composer allows graticules (termed grids) to be drawn on the map, and the ordinate labels to be customised, but does not recognise HTML tags such as .
Is there any other way to achieve this effect?
Answer
Unicode has some superscript and subscript characters in it as described on Wikipedia
Here are two custom functions that can be entered in the QGIS Python Function Editor that superscript or subscript the digits in strings passed to them:
Superscript
@qgsfunction(args='auto', group='Custom')
def supscr_num(inputText, feature, parent):
""" Converts any digits in the input text into their Unicode superscript equivalent.
Expects a single string argument, returns a string"""
supScr = (u'\u2070',u'\u00B9',u'\u00B2',u'\u00B3',u'\u2074',u'\u2075',u'\u2076',u'\u2077',u'\u2078',u'\u2079')
outputText = ''
for char in inputText:
charPos = ord(char) - 48
if charPos <0 or charPos > 9:
outputText += char
else:
outputText += supScr[charPos]
return outputText
Subscript
@qgsfunction(args='auto', group='Custom')
def subscr_num(inputText, feature, parent):
""" Converts any digits in the input text into their Unicode subscript equivalent.
Expects a single string argument, returns a string"""
outputText = ''
for char in inputText:
charPos = ord(char) - 48
if charPos <0 or charPos > 9:
outputText += char
else:
outputText += unichr(charPos + 8320)
return outputText
These can then be used in an expression as follows:
supscr_num('4') || "123" || supscr_num('000')
Returning the ⁴123⁰⁰⁰ in the question
A more generic example of the same expression:
supscr_num(to_string(@grid_number // 100000)) || lpad ( to_string ( (@grid_number % 100000) // 1000),3,'0') || supscr_num(lpad (to_string(@grid_number % 1000),3,'0'))
Unfortunately there is incomplete support for the latin alphabet super and subscripts in unicode, so this technique cannot be extended completely.
EDIT - Note on fonts
Note that the unicode characters generated by the tool aren't present in every font. For example, using the following label expression:
'1234567890' || '\n' || subscr_num( '1234567890') || '\n' || supscr_num( '1234567890')
Generates the following using Arial:
But using Times New Roman all the digits are present:
No comments:
Post a Comment