AppleScript名:書式つきテキストを組み立ててサイズを取得して画像書き出し v3a |
— Created 2017-09-25 by Takaaki Naganoya — Modified 2017-09-27 by Shane Stanley — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSFont : a reference to current application’s NSFont property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSBezierPath : a reference to current application’s NSBezierPath property NSPNGFileType : a reference to current application’s NSPNGFileType property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSFontAttributeName : a reference to current application’s NSFontAttributeName property NSKernAttributeName : a reference to current application’s NSKernAttributeName property NSMutableParagraphStyle : a reference to current application’s NSMutableParagraphStyle property NSLigatureAttributeName : a reference to current application’s NSLigatureAttributeName property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString property NSUnderlineStyleAttributeName : a reference to current application’s NSUnderlineStyleAttributeName property NSParagraphStyleAttributeName : a reference to current application’s NSParagraphStyleAttributeName property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName set outPath to "~/Desktop/test9999.png" set fillColor to NSColor’s whiteColor –塗り色 set aFontSize to 48 set aString to "Takaaki Naganoya" & return & "長野谷隆昌" set anAssrStr to makeRTFfromParameters(aString, "HiraMinProN-W3", aFontSize, -2, (aFontSize * 1.5)) of me set aSize to anAssrStr’s |size|() log aSize –> {width:387.424, height:144.0} set attrStrWidth to width of aSize set attrStrHeight to height of aSize set {xPos, yPos} to {0, 0} –下地の画像を作成 set tmpImg1 to makeImageWithFilledColor(attrStrWidth, attrStrHeight, fillColor) of me –下地の画像の上にAttributed Stringを描画 set tmpImg2 to drawAttributedStringsOnImage(tmpImg1, anAssrStr, xPos, yPos) of me –PNG形式でファイルに保存 set aRes to saveImageRepAtPathAsPNG(tmpImg2, outPath) of me –画像のうえに指定のスタイル付きテキストを描画して画像を返す on drawAttributedStringsOnImage(anImage, anAssrStr, xPos, yPos) anImage’s lockFocus() anAssrStr’s drawAtPoint:(current application’s NSMakePoint(xPos, yPos)) anImage’s unlockFocus() return anImage end drawAttributedStringsOnImage –書式つきテキストを組み立てる on makeRTFfromParameters(aStr as string, fontName as string, aFontSize as real, aKerning as real, aLineSpacing as real) –Font set aVal1 to NSFont’s fontWithName:fontName |size|:aFontSize set aKey1 to (NSFontAttributeName) –Color set aVal2 to NSColor’s blackColor() set aKey2 to (NSForegroundColorAttributeName) –Kerning set aVal3 to aKerning set akey3 to (NSKernAttributeName) –Underline set aVal4 to 0 set akey4 to (NSUnderlineStyleAttributeName) –Ligature set aVal5 to 2 –all ligature ON set akey5 to (NSLigatureAttributeName) –縦書き指定–Japanese tategaki setting (Up to down, right to left direction drawing) set aVal6 to true set aKey6 to (current application’s NSVerticalGlyphFormAttributeName) –Paragraph space set aParagraphStyle to NSMutableParagraphStyle’s alloc()’s init() aParagraphStyle’s setMinimumLineHeight:(aLineSpacing) aParagraphStyle’s setMaximumLineHeight:(aLineSpacing) set akey7 to (NSParagraphStyleAttributeName) set keyList to {aKey1, aKey2, akey3, akey4, akey5, aKey6, akey7} set valList to {aVal1, aVal2, aVal3, aVal4, aVal5, aVal6, aParagraphStyle} set attrsDictionary to NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList set attrStr to NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary return attrStr end makeRTFfromParameters –指定サイズの画像を作成し、背景を指定色で塗る on makeImageWithFilledColor(aWidth, aHeight, fillColor) set anImage to NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(aWidth, aHeight)) anImage’s lockFocus() set theRect to {{x:0, y:0}, {width:aWidth, height:aHeight}} –AppleがmacOS 10.13に作ったバグのせいで、width→heightの順番にパラメータを書かないと異常動作する set theNSBezierPath to NSBezierPath’s bezierPath theNSBezierPath’s appendBezierPathWithRect:theRect fillColor’s |set|() theNSBezierPath’s fill() anImage’s unlockFocus() return anImage end makeImageWithFilledColor –画像を指定パスにPNG形式で保存 on saveImageRepAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes end saveImageRepAtPathAsPNG |
More from my site
(Visited 12 times, 1 visits today)