« 画像生成(背景色指定) | トップページ | [PLIST]値の取得 基本 »

[Fonts]文字見本画像を生成する

こんな感じに生成されます Adobecleanhanregular

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()



###################################
#####設定項目
###################################
###画像サイズ
set intWidth to 1280 as integer
set intHight to 720 as integer

###画像に印字するテキスト
set strInputText1 to "美しい日本語辻茨城飴" as text
set strInputText2 to "ABCDEFGHIJKLMNOPQRSTUVWXYZ" as text
set strInputText3 to "abcdefghijklmnopqrstuvwxyz" as text
set strInputText4 to "*01234567890123456789@" as text
set strInputText5 to "足の速い茶色のキツネがぐうたら犬を" as text

###余白 px
set numPadding to 10 as integer
###フォントサイズ
set numFontSize to 56 as integer
###カーニング値
set numKern to -1.8 as number
### テキスト色の透過 透過あり 0.0 - 1.0
set numAlpha to 1.0 as number
###ドロップシャドウの有無 true or false
set boolDropShadow to true as boolean

###################################################
#####スクリプトメニューから実行させない
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    set aliasPathToMe to path to me as alias
    tell application "Script Editor"
      open aliasPathToMe
    end tell
return "中止しました"
  end tell
else
  tell current application
    activate
  end tell
end if
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
tell application "Finder"
  set aliasDefaultLocation to path to desktop folder from user domain as alias
end tell
try
  set aliasFolderPath to (choose folder "保存先フォルダを選んでください" with prompt "保存先フォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as alias
on error
  log "エラーしました"
return
end try
###保存先フォルダパス
set strSaveFolderPath to (POSIX path of aliasFolderPath) as text

###################################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
#####RGB16bitColorに16bitRGB値を格納
tell application "Finder"
  set the listRGB16bitColor to (choose color default color {65535, 55535, 0, 0}) as list
  log class of listRGB16bitColor
end tell
##########Color Picker Value 16Bit
set numRcolor16Bit to item 1 of listRGB16bitColor as number
set numGcolor16Bit to item 2 of listRGB16bitColor as number
set numBcolor16Bit to item 3 of listRGB16bitColor as number

##########Standard RGB Value 8Bit
set numRcolor8Bit to numRcolor16Bit / 256 div 1 as number
set numGcolor8Bit to numGcolor16Bit / 256 div 1 as number
set numBcolor8Bit to numBcolor16Bit / 256 div 1 as number

##########NSColorValue Float
set numRcolorFloat to numRcolor8Bit / 255 as number
set numGcolorFloat to numGcolor8Bit / 255 as number
set numBcolorFloat to numBcolor8Bit / 255 as number

###################################
##### フォント収集
###################################
###フォント初期化
set appFontManager to refMe's NSFontManager
set appSharedMaanager to appFontManager's sharedFontManager()
###利用可能なフォントリスト
set ocidFontList to appSharedMaanager's availableFonts()
###格納用のレコード
set ocidFontNameeDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0

###フォントの数だけ繰り返し
repeat with itemFontList in ocidFontList
  ###PS名でフォントオブジェクトにして
  set ocidTempFont to (refMe's NSFont's fontWithName:(itemFontList) matrix:(missing value))
  ###収録文字数を調べ
  set intCharCnt to ocidTempFont's numberOfGlyphs as integer
  ###5000文字以上なら日本語もあるだろって事で
  if intCharCnt > 5000 then
    set strDisplayName to ocidTempFont's displayName as text
    ###レコードに追加キー名を表示名で 値はPS名
(ocidFontNameeDict's setObject:(itemFontList) forKey:(strDisplayName))
  end if
end repeat

###################################
##### ダイアログ
###################################
###ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if

###格納用の可変リスト
set ocidFontNameeArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
###ディレクトリのallKeysで値のみのリスト=表示名のリスト
ocidFontNameeArray's addObjectsFromArray:(ocidFontNameeDict's allKeys())
###並び替え
set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:(true) selector:"localizedStandardCompare:")
(ocidFontNameeArray's sortUsingDescriptors:{ocidSortDescriptor})
####デフォルト選択値
set strDefailtsFont to "Osaka−等幅" as text
##JSOC番号なので0スタート値なので1足す
set intDefaultsFont to (ocidFontNameeArray's indexOfObject:(strDefailtsFont)) + 1 as integer
###ASリストにする
set listFontList to ocidFontNameeArray as list
###ダイアログ
try
  set listResponse to (choose from list listFontList with title "選んでください" with prompt "フォントを選んでください\r複数可" default items (item intDefaultsFont of listFontList) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list
on error
  log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
end if
###################################
##### 選択した数だけ繰り返し
###################################
repeat with itemFontDisplayName in listResponse
  ###PS名
  set strBaseFileName to (ocidFontNameeDict's valueForKey:itemFontDisplayName) as text
  
  ###################################################
  ##画像生成開始
  set ocidBitmapFormat to refMe's NSBitmapFormatAlphaFirst
  set ocidColorSpaceName to refMe's NSDeviceRGBColorSpace
  set ocidNSBitmapImageFileType to refMe's NSBitmapImageFileTypePNG
  set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:intWidth pixelsHigh:intHight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
  
  ###################################################
  ###初期化
refMe's NSGraphicsContext's saveGraphicsState()
  ###Context
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###生成された画像でNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ###塗り色
  set ocidSetColor to (refMe's NSColor's colorWithDeviceRed:(numRcolorFloat) green:(numGcolorFloat) blue:(numBcolorFloat) alpha:1)
ocidSetColor's |set|()
  ##画像にする
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:intWidth, height:intHight}})
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  
  
  ###################################
  #####テキスト生成
  ###################################
  ###設定用のレコード
  set ocidTextAttr to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  ###################
  ###文字色
  ###黒
  set ocidTextColor to (refMe's NSColor's colorWithSRGBRed:(0.0) green:(0.0) blue:(0.0) alpha:(numAlpha))
  ###白
  ## set ocidTextColor to (refMe's NSColor's colorWithSRGBRed:(1) green:(1) blue:(1) alpha:(numAlpha))
(ocidTextAttr's setObject:(ocidTextColor) forKey:(refMe's NSForegroundColorAttributeName))
  ###################
  ##フォント
  set ocidFont to (refMe's NSFont's fontWithName:(strBaseFileName) |size|:numFontSize)
(ocidTextAttr's setObject:(ocidFont) forKey:(refMe's NSFontAttributeName))
  ###################
  ##行間
  set ocidStyle to refMe's NSParagraphStyle's defaultParagraphStyle
(ocidTextAttr's setObject:(ocidStyle) forKey:(refMe's NSParagraphStyleAttributeName))
  ###################
  ##カーニング
  set ocidKern to numKern
(ocidTextAttr's setObject:(ocidKern) forKey:(refMe's NSKernAttributeName))
  ###################
  if boolDropShadow is true then
    ##ドロップシャドウ
    set ocidShadow to refMe's NSShadow's alloc()'s init()
    ###テキスト色と同色にする場合
    set ocidShadowColor to (ocidTextColor's colorWithAlphaComponent:0.8)
    ###色指定する場合
    set ocidShadowColor to (refMe's NSColor's colorWithDeviceRed:0 green:0 blue:0 alpha:0.5)
(ocidShadow's setShadowColor:(ocidShadowColor))
(ocidShadow's setShadowOffset:(refMe's NSMakeSize(1, -1)))
(ocidShadow's setShadowBlurRadius:4)
(ocidTextAttr's setObject:(ocidShadow) forKey:(refMe's NSShadowAttributeName))
  end if
  ###################
  ##描画する文字列
  set ocidText1 to (refMe's NSString's stringWithString:(strInputText1))
  set ocidText2 to (refMe's NSString's stringWithString:(strInputText2))
  set ocidText3 to (refMe's NSString's stringWithString:(strInputText3))
  set ocidText4 to (refMe's NSString's stringWithString:(strInputText4))
  set ocidText5 to (refMe's NSString's stringWithString:(strInputText5))
  set ocidText6 to (refMe's NSString's stringWithString:(itemFontDisplayName))
  ###################
  ####描画されるテキストボックスのサイズ
  set ocidTextSize1 to (ocidText1's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize1's width as integer
  set ocidTextHigh to (ocidTextSize1's height) + 24 as integer
  set ocidTextOrigin1 to refMe's NSMakePoint((intWidth - ocidTextWide) / 2, (intHight - ocidTextHigh))
  
  set ocidTextSize2 to (ocidText2's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize2's width as integer
  set ocidTextHigh to (ocidTextSize2's height) + 24 as integer
  set ocidTextOrigin2 to refMe's NSMakePoint((intWidth - ocidTextWide) / 2, (intHight - (ocidTextHigh * 2)))
  
  set ocidTextSize3 to (ocidText3's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize3's width as integer
  set ocidTextHigh to (ocidTextSize3's height) + 24 as integer
  set ocidTextOrigin3 to refMe's NSMakePoint((intWidth - ocidTextWide) / 2, (intHight - (ocidTextHigh * 3)))
  
  set ocidTextSize4 to (ocidText4's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize4's width as integer
  set ocidTextHigh to (ocidTextSize4's height) + 24 as integer
  set ocidTextOrigin4 to refMe's NSMakePoint((intWidth - ocidTextWide) / 2, (intHight - (ocidTextHigh * 4)))
  
  set ocidTextSize5 to (ocidText5's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize5's width as integer
  set ocidTextHigh to (ocidTextSize5's height) + 24 as integer
  set ocidTextOrigin5 to refMe's NSMakePoint((intWidth - ocidTextWide) / 2, (intHight - (ocidTextHigh * 5)))
  
  set ocidTextSize6 to (ocidText6's sizeWithAttributes:(ocidTextAttr))
  set ocidTextWide to ocidTextSize6's width as integer
  set ocidTextHigh to (ocidTextSize6's height) + 24 as integer
  set ocidTextOrigin6 to refMe's NSMakePoint((intWidth - ocidTextWide) / 2, 36)
  
  
  ###################################
  #####テキスト描画 本処理
  ###################################
  ####NSGraphicsContextは透明アートボード
  set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
  ###初期化
refMe's NSGraphicsContext's saveGraphicsState()
  ###透明アートボードでNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
  ###テキスト描画する
(ocidText1's drawAtPoint:(ocidTextOrigin1) withAttributes:(ocidTextAttr))
(ocidText2's drawAtPoint:(ocidTextOrigin2) withAttributes:(ocidTextAttr))
(ocidText3's drawAtPoint:(ocidTextOrigin3) withAttributes:(ocidTextAttr))
(ocidText4's drawAtPoint:(ocidTextOrigin4) withAttributes:(ocidTextAttr))
(ocidText5's drawAtPoint:(ocidTextOrigin5) withAttributes:(ocidTextAttr))
(ocidText6's drawAtPoint:(ocidTextOrigin6) withAttributes:(ocidTextAttr))
  ####画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
  ###################################################
  ###プロパティ
  set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  ###
  set ocidColorSpace to refMe's NSColorSpace's displayP3ColorSpace()
  set ocidColorSpaceData to ocidColorSpace's ICCProfileData()
(ocidProperty's setObject:(ocidColorSpaceData) forKey:(refMe's NSImageColorSyncProfileData))
  ###
(ocidProperty's setObject:(false) forKey:(refMe's NSImageInterlaced))
  ###
  set numGamma to (1 / 2.2) as number
(ocidProperty's setObject:(false) forKey:(refMe's NSImageGamma))
  ####DATAに
  set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(ocidNSBitmapImageFileType) |properties|:(ocidProperty))
  ###################################
  #####保存
  ###################################
  ###パス
  set ocidSaveFolderPathStr to (refMe's NSString's stringWithString:(strSaveFolderPath))
  set ocidSaveFolderPath to ocidSaveFolderPathStr's stringByStandardizingPath()
  set ocidSaveFolderPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFolderPath) isDirectory:true)
  ###ファイル名はフォント名
  set ocidSaveFilePathURL to (ocidSaveFolderPathURL's URLByAppendingPathComponent:(strBaseFileName) isDirectory:false)
  set ocidFilePathURL to (ocidSaveFilePathURL's URLByAppendingPathExtension:"png")
  ###保存
  set boolDone to (ocidNSInlineData's writeToURL:(ocidFilePathURL) atomically:true)
  
  
end repeat



|

« 画像生成(背景色指定) | トップページ | [PLIST]値の取得 基本 »

AppleScript Fonts」カテゴリの記事