NISUS Archives

June 2010

NISUS@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Thu, 3 Jun 2010 23:15:40 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (124 lines)
On Jun 3, 2010, at 11:58 AM, Nobumi Iyanaga wrote:

> On Jun 1, 2010, at 8:00 PM, Kino wrote:
> 
>> Surely there would be other ways to do so but my usual procedure is
>> 
>> 	$variable = Cast to String 'something' # unnecessary if 'something' is already plain text
>> 	Push Target Text $variable
>> 		Set Font Name 'Hiragino Mincho Pro'
>> 	Pop Target Text
> 
> Ah, I didn't know these commands. I read the explanation in Nisus Macro Reference, but I don't understand well what is the point. Could you give me a more detailed explanation on these commands...?

But what details? All I know is that it allows you to apply some attributes on a variable (text object). Isn't it handy?

> On the other hand, I found that if I just insert the text, after setting the font to a certain font at the insertion point, it is not only the inserted text but some other characters are changed to that font...

Then, that would not be a good idea. I have never experienced an oddity of that kind when running macros using the procedure above-quoted such as the one below. 

> Another oddity was that if I use "Set Font Name 'Hiragino Mincho Pro'" command, the font changes to Hiragino Mincho Pro, but the character is marked as "Font Substituted" (highlighted in Red); this does not happen if I use the menu command "Format:Font:Hiragino Mincho Pro".

I have never experienced it either. Perhaps did you insert a Japanese string somewhere in a text portion in another language? Then, you need to apply the Japanese language on the string to be inserted, I guess.  This depends on how you have configured Preferences - Languages, though.

> Anyway, I found that the menu commands are not specially slow, and work well.
> 
> Another thing (unrelated to this subject) that I found in writing this macro is that the command "$text.find xxx"... is VERY VERY slow. I could replace them with "$text.rangeOfString xxx" -- but this should really be improved, like perl's "$text =~ /xxx/"... command.

Really? How did you test it? For me, find command seems to be 2 or 3 times slower but is it fair to call it "VERY VERY slow" for that? It is much more featured and it would not be a good idea to use it when rangeOfString is sufficient. I don't know what perl's equivalent of rangeOfString is but, as to s///, sed is much faster than perl and perl's tr/// is much faster than its s///. However, I don't think you will blame perl for that.


Kino


--

### Styles2LaTeX ###

# Mixed style attributes, e.g. "Bold Italic", "Bold and SmallCaps", are not supported.

$tagFont = 'Courier'
$tagSize = 9
$tagColor = Color.newWithHexTriplet 0x009933

$tags = $prop = $vals = Hash.new
$styles = Array.new ('it', 'bf', 'sc')
$tags{'it'} = '\textit{'
$prop{'it'} = 'italic'
$vals{'it'} = true
$tags{'bf'} = '\textbf{'
$prop{'bf'} = 'bold'
$vals{'bf'} = true
$tags{'sc'} = '\textsc{'
$prop{'sc'} = 'characterCase'
$vals{'sc'} = 3
$tags{'it.closing'} = $tags{'bf.closing'} = $tags{'sc.closing'} = '}'

Require Pro Version 1.3
$doc = Document.active
if $doc == undefined
	exit
end

foreach $key, $value in $tags
	$value = Cast to String $value
	Push Target Text $value
		Set Font Name $tagFont
		Set Font Size $tagSize
		Set Text Color $tagColor
	Pop Target Text
	$tags.setValueForKey $value, $key
end

$sels = Hash.new

$text = $doc.text
$i = 0
while $i < $text.length
	$attr = $text.displayAttributesAtIndex $i
	$range = $text.rangeOfDisplayAttributesAtIndex $i
	foreach $style in $styles
		if $attr.getProperty($prop{$style}) == $vals{$style}
			if $sels{$style} == undefined
				$sels{$style} = Array.new
			end
			$sel = TextSelection.new $text, $range
			$sels{$style}.appendValue $sel
		end
	end
	$i = $range.bound
end

foreach $key in $sels.keys
	foreach $i, $sel in reversed $sels{$key}
		if ! $i
			break
		elsif $sels{$key}[$i-1].text.isSameObject($sel.text)
			if $sels{$key}[$i-1].bound == $sel.location
				$sels{$key}[$i-1].length += $sel.length
				$sels{$key}.removeValueAtIndex $i
			end
		end
	end
end

$selToStyle = Hash.new
foreach $key in $sels.keys
	if $sels{$key}.count
		foreach $sel in $sels{$key}
			$selToStyle{$sel} = $key
		end
	end
end

$allSels = $selToStyle.keys
$allSels.sort

foreach $sel in reversed $allSels
	$style = $selToStyle{$sel}
	$sel.text.insertAtIndex $sel.bound, $tags{"$style.closing"}
	$sel.text.insertAtIndex $sel.location, $tags{$style}
end

### end of macro ###

ATOM RSS1 RSS2