Save 37% off PRO during our Black Friday Sale! »

CoreText のはなし

CoreText のはなし

5287c9b9576749092d3170ec738338b8?s=128

Yusuke Ito

April 14, 2012
Tweet

Transcript

  1. ͋ ͭ CoreText ͷ͸ͳ͠ Yusuke Ito, @novi_

  2. ֓ཁ ग़ݱ: 3.2 (iPad only) 2010/4/3 (3.0͔Β͕͋ͬͨ·ͩPrivate) ςΩετϨϯμϦϯάΤϯδϯ TextEdit, Cocoa(NSText*)Ͱ࢖ΘΕ͍ͯΔ

    may be Pages, iMovie, Keynote ....
  3. iOS ͱ OS X AttributedStringͱηοτͰ࢖͏ (ඳը͸CoreGraphics) RTLɾॎॻ͖༻άϦϑʹରԠ→ॎॻ͖ άϦϑͷஔ׵ (CTGlyphInfo) ҟମࣈʹ࢖͑Δ

    ϑΥϯτͷબ୒(σΟεΫϦϓλ) (CTFontDescriptor) ϑΥϯτϑΝΠϧͷύʔεɾ֤छϝλσʔλ (CTFont)
  4. CoreText.framework (iOS) ApplicationServices.framework/CoreText.framework (OS X) CݴޠͷΠϯλʔϑΣʔε CoreFoundationϕʔε CFTypeΛܧঝ(ͱͯ͠ৼΔ෣͏)

  5. NSAttributedString CFAttributedStringͱToll-free NSMutableAttributedString, CFMutableAttributedString म০จࣈ (৭ɾϑΥϯτɾߦؒ...) Λදݱ͢ΔΫϥε ෦෼จࣈྻʹରͯ͠ɺ৭(ଐੑ)ͳͲΛઃఆ͢Δ iOS4.0૬౰Ҏ্͔Β͸BlocksʹΑΔଐੑͷྻڍΛα ϙʔτ

  6. Ͳ͏Ͱ΋ྑ͍࿩ ☞ ౰࣌ iOS3.2 ☞ CoreText͕࢖͑ΔΑ͏ʹͳͬͨ ☞ ॎॻ͖͕Ͱ͖Δ!! (ΞςʹͳΒͳ͍υΩϡϝϯτʹΑ Δͱʣ

    ☞ ͔͠͠ɺग़དྷͳ͍!! ☞ Macͱಉ͡ίʔυͰ΋ग़དྷͳ͍!!
  7. ☞ υΩϡϝϯτʹ͸ࡌ͍ͬͯΔͷʹ... (࢖͑Δͱ͸ॻ͍ ͯ͋Δ) ☞ ͍ͭ΋௨Γग़དྷͳ͍ΜͩΖ͏→ఘΊΔ ☞ ͱΓ͋͑ͣAppleʹόάϨϙ ☞ ςετίʔυΛૹΕͱݴΘΕΔ

    ☞ iOSͱMac༻ʹ࡞ͬͯૹΔ ☞ εΫϦʔϯγϣοτΛૹΔ ☞ ͦͯ͠ɺԻࠫଡແ͠...
  8. ೥ޙ J04ϦϦʔεલ

  9. Apple: “ग़དྷͨΑʔ” ͓ͤʔΑ!!! ͱ͍͏͜ͱͰɺ ࠓ͸ॎॻ͖Ͱ͖·͢ɻ

  10. αϙʔτ͸͞Ε͕ͨ... ௒όΪʔͩͬͨ! ߦͷRTL(Right to Left)ɺॎॻ͖ܥ͕·ͱ΋ʹग़དྷͳ͍ RTLϞʔυͩͱάϦϑͷbounds͕͓͔͍͠ Unicodeॲཧ΋एׯ͓͔͍͠ (ؾ͕͢Δ) ೔ຊޠͱԤจϑΥϯτࠞ߹࣌ͷߦؒ໰୊ (࢓༷)

    ͍͔ͭ͘ͷτϦοΫͰमਖ਼Մೳ
  11. αϙʔτ͸͞Ε͕ͨ... ϑΥϯτσΟεΫϦϓλʹ೔ຊޠΛ৯ΘͤΔͱམͪΔ ώϥΪϊҎ֎·ͱ΋ʹϨϯμϦϯά͞Εͳ͍͜ͱ΋ (e.g.) IPA TrueType ϑΥϯτͷॎॻ͖͕·ͱ΋ʹͰ ͖ͳ͍ ԣ͸OKɺॎ͕μϝ (ϑΥϯτͷ࢓༷͔΋)

    FontForgeͰίϯόʔτͯ͠΋ͩΊ
  12. How to Use ࠶ͼ: AttributedStringͱηοτͰ࢖͏ ࣮ࡍͷඳը͸CoreGraphics (ྫ͑͹-drawRect:) View͸UIViewΛܧঝͯࣗ͠෼Ͱ࣮૷ ៉ྷͳ݁ՌΛಘΔʹ͸៉ྷͳAttributedStringΛ࡞ΕΔ ͔ɻͨͿΜϙΠϯτ͸ͬͪ͜ɻ

    ͱ͍͏͜ͱͰɺAttributedStringͷ͸ͳ͠
  13. 1. Attributed StringΛ࡞Δ ଐੑ͸ Core Text String Attributes Λ࢖͏ NSAttributedStringʹCFAttributedString

    NSMutableAttributedStringʹCFMutableAttributedString ී௨͸Cocoa(NS)Λ࢖͑͹OK
  14. 2. CoreTextͰϨϯμϦϯά 1Ͱ࡞੒ͨ͠NSAttributedString͔ΒCoreTextͷΦϒ δΣΫτΛ࡞੒ CTFramesetterCreateWithAttributedString ͳͲ ϨϯμϦϯά CTFramesetterCreateFrame → CTFrameʹม׵

  15. 3. CoreGraphicsͰඳը͢Δ 1.CTFrameͷDraw(CTFrameDraw)Λ࢖͏ 2.CTLineͷDrawΛ࢖͏→ࣗ෼Ͱߦ͝ͱඳը 1.ۉ౳ἧ͑Λ͢Δͱ͖͸͜ΕΛ࢖͏ 3.CTRunͷDrawΛ࢖͏ 4.CTRun͔ΒάϦϑCGGlyphΛऔΓग़ͯ͠ɺ CGContextShowGlyphsͰඳը͢Δɻ ͍ͣΕ͔ͷํ๏Ͱඳը

  16. CTFrameSetter NSAttributedString CTFrame CTFont CTGlyphInfo CTParagraph Style CTLine CTLine CTLine

    CTRun CoreGraphics -drawRect:౳ CTLine ϨϯμϦϯά ඳը ଐੑͱͯ͠ઃఆ ॳظԽ
  17. ׽ࣈ123 Hello World ׽ࣈ123 ... NSAttributedString Hello World CTLine ׽

    ࣈ 123 CTLine CTRun
  18. CTFrameSetter NSAttributedString CTFrame CTLine CTLine CTLine ϨϯμϦϯά ॳظԽ ࠓ·Ͱͷ͸Α͘঺հ͞Ε͍ͯΔ ΍Γํ

  19. ࣮ࡍCTFramesetter͸ɺ CTTypesetterΛ಺෦Ͱಈ͔͍ͯ͠Δ CTTypesetter NSAttributedString CTLine CTLine CTLine ࣗ෼ͰߦΛ࡞ͬͯ ϨϯμϦϯά ॳظԽ

    ΋͏Ұͭͷ΍Γํ: CTTypesetterΛ࢖͏ ී௨͸࠷ॳʹ঺հͨ͠΄͏Ͱे෼
  20. ͜͜ͰσβΠϯύλʔϯͷ࿩Ͱ΋ ΦϒδΣΫτ͸Mutable(Մม)͔Immutable(ෆม)͔ Mutable: NSMutable..., UIView, UIViewController... Immutable: NSObject, NSString, UIImage,

    UIColor... NSMutableAttributedString จࣈྻࣗମ͸Immutable (-initWithString:) จࣈྻͷଐੑ͸Mutable (-setAttributes:range:) CoreText͸͢΂ͯImmutable
  21. CTFrameSetter NSAttributedString NSMutableAttributed... CTFrame CTFont CTGlyphInfo CTParagraph Style CTLine CTLine

    CTLine CTRun CTLine ϨϯμϦϯά ଐੑͱͯ͠ઃఆ ΠχγϟϥΠβʔ CTFramesetterCreateWithAttributedString CTFramesetterCr eateFrame औΓग़͠ CTFrameGetLines औΓग़͠ CTLineGetGlyphRuns Imuutable Imuutable Imuutable Imuutable Imuutable Imuutable ಈతͳมߋ͸೉͍͠ͷͰ͏·͘ઃܭ͠·͠ΐ͏ɻ
  22. Ͱ͖Δ or Ͱ͖ͳ͍, FAQ ϦϯΫΤϥʔ iOS͸CoreText.frameworkͷ௥Ճ(ϦϯΫ)͕ඞཁ ඳը࣌ͷαΠζ͕ཉ͍͠ CTFramesetterSuggestFrameSizeWithConstraints ۉ౳഑ஔ CTLineCreateJustifiedLineͰCTLineΛม׵ͯ͠

    CTLine͝ͱඳը
  23. ը૾΍ಈը౳ΛຒΊࠐΜͰϨϯμϦϯά AttributedStringʹจࣈU+FFFCΛ௥هͯ͠ɺ kCTRunDelegateAttributeName ଐੑͰCTRunDelegateΛઃఆ ͢Δͱࢦఆͨ͠αΠζͷۭ͖ྖҬ͕Ͱ͖Δ→͜͜ʹޙͰࣗ෼Ͱ ViewΛஔ͘ ඳը࣌ʹCTRunGetAttributesͰCTRunDelegateΛऔಘ ͨͩ͠ɺView͸ࣗ෼Ͱஔ͍ͯͶ

  24. άϦϑͷஔ׵ kCTGlyphInfoAttributeNameଐੑͰ CTGlyphInfo Λ ηοτ લ͸iOSͰ͸ಈ͔ͳ͔͕࣮ͬͨ૷͞Εͨ ߦؒͳͲͷஈམͷઃఆ kCTParagraphStyleAttributeNameଐੑͰ CTParagraphStyle Ληοτ

  25. ೔ຊޠͱԤจࠞࡏ࣌ʹߦ͕ؒෆࣗવ ํ๏1: ߦؒΛࣗ෼Ͱۭ͚ͳ͕ΒɺCTLine͝ͱʹඳը ํ๏2: kCTParagraphStyleSpecifierMinimumLineSpacing, kCTParagraphStyleSpecifierMaximumLineSpacing Λ0.0 ʹηοτ (͜ͷଐੑ͸υΩϡϝϯτʹࡌ͍ͬͯͳ͍ͷ Ͱ஫ҙɻϔομϑΝΠϧࢀর)

  26. ॎॻ͖ (࣍ͷ2ͭͷ࡞ۀ͕ඞཁ) ॎॻ͖ͷάϦϑʹ͢Δ: kCTVerticalFormsAttributeName ߦΛॎஔ͖Ͱɺӈ͔Βࠨʹඳը͢Δ: ํ๏1: kCTFrameProgressionAttributeName (LineͳͲͷboundsऔಘͰόά͕͋Δؾ͕͢Δ) ํ๏2: CGContextͰMatrix(CGAffineTransform)Λ

    ηοτͯ͠ճసͤ͞Δ
  27. ςΩετΤσΟλతͳ΋ͷΛ࡞Δ UITextInput Protocol ͔Β࢝ΊΔ Text, Web, and Editing Programming Guide

    for iOS
  28. ͓͢͢ΊReference ެࣜ: ATSUIΛ࢖ͬͨUnicodeςΩετඳը ΋͏άάͬͯ΋ग़ͯ͜ͳ͍͕ɺWebArchiveʹ͋ͬͨɻ http://bit.ly/Hh7sP3 ެࣜ: Core Text Reference Attributed

    String Programming Guide Core Text String Attributes Reference NSMutableAttributedString Class Reference Strings Programming Guide for Core Foundation
  29. Reference 2 WWDC Video #110, 2010, Advanced Text Handling for

    iPhone OS #114, 2010, Advanced Cocoa Text Tips and Tricks #128, 2011, Advanced Text Processing Google ݕࡧ: “coretext filetype:pdf” “attributed string filetype:pdf” SlideShare, SpeakerDeck Github
  30. LTCoreText ͦΕͳΓʹܰྔ ϖʔδϯά/ ஈ૊Έ ॎஔ͖/ॎॻ͖ ΧελϜϏϡʔ౳ͷຒΊࠐΈ ಈతͳϖʔδૠೖɾ࡟আ ඇಉظϨΠΞ΢τॲཧ https://github.com/novi/LTCoreText ͓·͚:

    LTGridView  ్த͚ͩͲGridViewͷϥΠϒϥϦ ެ։தͷϥΠϒϥϦ
  31. demo LTCoreText

  32. Thank you ❡ https://github.com/novi http://speakerdeck.com/u/yusukeito http://about.me/yusukeito

  33. Appendix: RetinaͱCoreText Retinaͩͱएׯจࣈ͕ࡉ͘ݟ͑Δ (ͱࢥ͏) αϒϐΫηϧϨϯμϦϯάͷON/OFFʹΑͬͯ΋ҧ͏ Retina/ඇRetina, αϒϐΫηϧϨϯμϦϯάON/OFF ͷ4ύλʔϯ Mac͸σϑΥϧτON, iOS͸OFF

    ͝·͔͕͠ޮ͔ͳ͍ͷͰϑΥϯτͷ඼࣭͕෼͔Δ ώϥΪϊͷ͸͔ͬ͠Γ࡞ͬͯ͋Δҹ৅