2013年2月4日月曜日

ルビ振り方法を模索。Text,Glyphs学習メモ。

漢字にルビを振ろうかと思い、模索中。

正規表現で、ひらがなを取得するコードを、以下の記事があげてくださっているので、メモ。
http://redwing.moo.jp/cocoa/archives/date/2012/04

そして、なんらかのデリミタで、文字列を分割する方法について、以下が参考になりそうだったので、メモ。 NSScannerなるものが便利そう。

http://hmdt.jp/cocoaProg/Foundation/NSScanner/NSScanner.html

あとは、ひらがな以外の漢字のindexを取得して、用意しておいたルビの配列から取り出して、表示、という感じか。ちゃんとその文字の上に表示しなくてはいけないから、位置取得も考えなきゃな。
それが一番ハードルが高かったりするかもしれません。..

まあ、気長にがんばるか。

:::::::::::::::::::::::::::::::
さて、以下は、Apple公式ドキュメントで、関連の参考になりそうなページURLメモです。

NSStringについて。
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035i

「Drawing Text With NSLayoutManager」という項目が、かなり重要そう。テキストを直接、位置を決めて描画するらしい。(まだざっとしか目を通していません。)

「Line Fragment Generation」
文字の画角(rectangle)についてなどの資料の様子。
(このページに「The built-in typesetters currently provided with text system support only horizontal text layout. However, the text system can support typesetters that lay out text along lines that run either horizontally or vertically, and in either direction. 」と書いてあるので、縦書きにもこの仕組みは対応できると。pagesあたりが、しっかり縦書きに対応して下さると、日本人としてはありがたい限りですが。)

で、「Layout Geometry: The NSTextContainer Class」に、ヘブライ語とアラブ語は、左から右、という動きのみをサポートしたtypesetterによってではなく、別のメカニズムで動いています」と書いてあるので、日本語も、その口かな、と。最近、テキストエディットが縦書き対応したので、pagesに適用されるのも近いかな、と思ったり。
Note:  The built-in typesetters currently support only top-to-bottom line movement and left-to-right sweep. These typesetters do handle bidirectional text (Hebrew and Arabic) by laying it out in the proper display order within the line fragments, but they do not use the line sweep mechanism.
話題がずれてきましたが。。

これも重要そう。「The Layout Manager」。
ここに詳しくレイアウトについて解説されている様子。
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Concepts/LayoutManager.html#//apple_ref/doc/uid/20001805-BBCFEBHA

で「TypeSetter」が、重要そう。
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Concepts/Typesetters.html#//apple_ref/doc/uid/20001806-BBCFEBHA

また、以下の「setTypesetterBehavior」の解説に、
Renders the glyphs at the specified positions and attributes into the graphicsContext.」と書いてあるので、graphicsContextの中の特定の位置と属性でglyphsを描画する、とのこと。
で、showCGGlyphs:positions:count:font:matrix:attributes:inContext:」というメソッドが紹介されており、positionsという部分がCGPointなので、検討する価値がありそう。
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSLayoutManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSLayoutManager/setTypesetterBehavior:


で、「CircleView」という面白いMacアプリのサンプルがあり、インストールしてみたら、グルグル円になった文章が回転して、楽しい。コードを読んでいけば、かなり勉強になりそう。Apple提供。
https://developer.apple.com/library/mac/#samplecode/CircleView/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008882

このサンプルを見ていると、縦書きの簡単な仕組みは作れそうですね。
ルビもいけるかな。
各文字の配置をプログラミングで制御しているので、シンプルです。

これは使えそうだ。
ありがたや。。。。。。


:::::::::::::::::::::::::::::::

ついでに、そもそもとして、Cocoaのテキスト設計全体についての解説ページ。
https://developer.apple.com/library/mac/#documentation/TextFonts/Conceptual/CocoaTextArchitecture/Introduction/Introduction.html


:::::::::::::::::::::::::::::::

さて、これから、まだまだ勉強です。
ルビの実現ぐらいで、ずいぶん勉強内容が増えそうですが、
他にも活用できそうなので、まあ、勉強あるのみ。
楽しく、無理なく参りませふ。

0 件のコメント:

コメントを投稿