Text Cache

Discussion in 'RGSS3 Scripts (RMVX Ace)' started by Mithran, Mar 28, 2012.

  1. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    Text Cache v 1.03
    by Mithran​
    Introduction
    VX Ace is a huge improvement over VX in many different ways. However, although text processing has a number of new features, it has also introduced a number of new bugs.
    - Text is squeezed when drawn to the rect given by text_size. Subsequent text shrinking also occurs long before needed.
    - Bitmap#draw_text wraps around on itself after ~640 pixels.
    - Individually drawn characters for certain fonts are drawn incorrectly

    Features
    - Implements a text cache. Trades a small amount of memory for a large amount of processing speed (~2 to 5x as fast for most strings).
    - Draws each character with padding, accounting for its actual size when placed adjacent other characters. Each character is now drawn consistently whether it is drawn independently or not.
    - Automatically uses original method when dealing with "squeezed" text (manually squeezing text looks terrible, so this was left in)

    How to Use
    Install on its own script page in the materials section of the script editor above main and below default scripts.

    Script
    http://pastebin.com/fRFFYgRt

    FAQ
    q. Any simpler way to handle this?
    a. Add two pixels to width before attempting to draw text to its own text_size rect. Split up any string longer than 640 text_size width and draw each piece successively (or don't draw strings longer than 640 text_size width, you'd only need them for horizontally scrolling windows). Use a non-TrueType Font (like the default one, I believe). Following these three rules you'll likely never run into any of the bugs fixed by this script. This script will still allow faster text processing, if you choose to use it.

    q. I'm using draw_text every frame, it still lags.
    a. This script is not a substitute for poor procedures. draw_text is still very time consuming, and should be using sparingly as before.

    Credit and Thanks
    - Mithran

    Author's Notes
    May be distributed with a link to these forums, original unedited script, and with credit. May be used in any project, so long as the credit in the script remains intact (individual credit in the project as a whole is not required).

    Bug Reporting
    If you have a crashing bug, please provide the complete error code as well as a short description on how to reproduce. If you have a display bug, please include a screenshot and a short description.
     
    Last edited by a moderator: Jul 5, 2014
    #1
  2. Mihel

    Mihel Veteran Veteran

    Messages:
    384
    Likes Received:
    45
    Thanks for this! I though it was a problem with the font I was using.

    You should put some pictures here, most people are gonna skip bug-fixes scripts if they don't see a visual example.

    [​IMG]
     
    #2
  3. Dr.Yami

    Dr.Yami 。◕‿◕。 Developer

    Messages:
    994
    Likes Received:
    736
    Location:
    Finland
    First Language:
    Vietnamese
    Primarily Uses:
    Other
    Yeah, Awesome Bugfix XD
     
    #3
  4. Nathanial

    Nathanial Veteran Veteran

    Messages:
    3,249
    Likes Received:
    1,129
    Location:
    Kalamazoo, MI
    First Language:
    English
    Very awesome fix! Thank you :)
     
    #4
  5. Terumoc

    Terumoc Villager Member

    Messages:
    11
    Likes Received:
    1
    First Language:
    English
    Primarily Uses:
    RMMV
    This fixes so many instances of weirdness and annoyance I had with using different fonts, thank you so much for sharing this! This makes using VXAce even more enjoyable.

    I still have one other issue with text, but I'll be putting up a separate thread regarding that... but this has fixed the biggest issue which was cut off beginnings of text and weirdness in spacing.
     
    #5
  6. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    Updated to v1.01, fixed a crashing error when using F12 to reset. (Thanks Archeia_Nessiah for the report)

    Script has been updated at the link.
     
    #6
  7. Indrah

    Indrah Megane Berserker Veteran

    Messages:
    2,072
    Likes Received:
    2,031
    Location:
    Spain
    First Language:
    Spanish
    Primarily Uses:
    RMMV
    Great, I was about to report the F12 crash. Love this (Georgia, Imma use your fonty ass no matter how you complain >[)
     
    #7
  8. Kenen

    Kenen Veteran Veteran

    Messages:
    255
    Likes Received:
    152
    First Language:
    English
    Primarily Uses:
    RMMV
    This just made my day. I haven't been able to use my favorite typeface because of the missing character bug. Problem solved! Thanks so much, Mithran.
     
    #8
  9. Knightmare

    Knightmare Knight of the Night Veteran

    Messages:
    1,057
    Likes Received:
    107
    Location:
    Central USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Yo Myth! (brofist) I was wondering what happened to you. Haven't seen you in the community for a while. Nice to see you. Thanks for the bug fix also.
     
    #9
    ShinGamix likes this.
  10. Galenmereth

    Galenmereth I thought what I'd do was Veteran

    Messages:
    2,206
    Likes Received:
    1,915
    Location:
    Norway
    First Language:
    English
    Primarily Uses:
    RMMV
    I know it's been a while, but just thought I'd pop in and mention a problem. A tester got the following crash using an Intel chipset:

    http://pastebin.com/XKNmNNsy

    Crash happened on windows like the default Shop Sell window, Items, and so forth. Not normal message dialogues, and other menus.

    This does not happen with all Intel chipsets, nor most other GPU's I've tested, which is weird. But when I removed the script, the problem was gone :)
     
    #10
  11. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    Looks like something was trying to print either a null character, or something else that is reporting a zero-height. I had no handling for this, so I updated the script to skip any character reporting a 0 height as it would prevent the cached bitmap from being created. Let me know if that works.
     
    #11
    Galenmereth likes this.
  12. Napoleon

    Napoleon Veteran Veteran

    Messages:
    869
    Likes Received:
    96
    First Language:
    Dutch
    The pastebin still links the old 1.01 version. Where can we find the new version with the '0-height-fix'? Or is it not yet released and is it still awaiting 'testing from Galenmereth'?
     
    #12
  13. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    Actually I fixed it right before I posted last time (it was a one line change).  I've now updated the version numbers and changelog to match.
     
    Last edited by a moderator: Jul 1, 2014
    #13
  14. Archeia

    Archeia Level 99 Demi-fiend Staff Member Developer

    Messages:
    14,582
    Likes Received:
    14,281
    Location:
    Game Dev Salt Mines
    First Language:
    Filipino
    Primarily Uses:
    VNM
    Hey Mithran, just an inquiry. I was using this commercial font named Bookmania. Here's the kicker though. On certain PCs, it displays perfectly fine. But on my pc, it will draw it like this. Do you have any idea of the cause?
     
    #14
  15. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    That is odd.  Does it happen both with and without Text Cache?  Is it an OpenType font or TrueType?  I read somewhere that OpenType may have issues with RGSS3, but those issues were glyphing or outright crashes with certain fonts, I've never seen the above described issue.  Perhaps you are loading a corrupted copy of the font?  If installed, I'm not sure if the one in the game folder takes precedence or if the system one does.  I assume the garbage pixels only get drawn in RM and don't show up in a font viewer?  Depending on how each letter is constructed internally, it could also be an issue with different windows versions not scaling the font correctly.  RGSS3 uses automatic anti-aliasing when drawing text, so it could also be an internal error.
     
    #15
  16. Archeia

    Archeia Level 99 Demi-fiend Staff Member Developer

    Messages:
    14,582
    Likes Received:
    14,281
    Location:
    Game Dev Salt Mines
    First Language:
    Filipino
    Primarily Uses:
    VNM
    It happens both with and without Text Cache. Only in this PC though. It is an OTF though.

    I noticed that the reason for it is that RM squishes letter. If you added a lot of space, it will appear properly. But that does make me question how it will appear on other Computers since if it displays perfectly fine for others, then I did this so it would display text properly in mine, wouldn't that be a huge problem o_o

    The font is definitely not corrupted since it works perfectly on everything else like websites and my word document files and other pcs that loads the same font. 
     
    #16
  17. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    Are you using Bitmap#text_size to determine the area that the text should draw to for that example window?  When not using this script, any text drawn to a rect equal to the size of the text will "squeeze" it, but with this script, it adds 2 pixels of padding that is supposed[\i] to prevent the squeeze.  Maybe OTFs have a different size estimation and it differs based on system?  The size estimation should[\i] be uniform from system to system, and if not, it should at least be consistent[\i] enough that you can draw it to it's own text_size rect (after the 2 pixels buffer this script allows).  Beyond that, the actual squeezing is all internal RM, which makes me further question this odd behavior.

     

    Squeezed characters won't be cached and vice-versa, because I have no way to determine the actual dimensions of the "squeezed" character without having the script examine by pixel, and there are too many variations of squeezing for me to reliably keep in memory using the methods I am now.  When a text draw for the cache does take place, there is always plenty of empty space to draw the character, so no squeeze ever takes place there.

     

    Right now this script allows the default "squishing" algorithm to take over only in cases where the entire drawn text is determined to not have enough room to draw in the selected area.  However, that seems to be more and more a thing there should at least be a way around.  Does the text appear like the above in game for windows that it looks like it should be drawn correctly, like the message window, which already draws by character, or areas that it *looks* like there is plenty of room, like names in the menu screen, or is it everywhere in your game?  If its just the "squished" areas, how does it appear to other people for those areas?  None of the text in the image you posted looks particularly squeezed, which makes it seem like the squeezing isn't working at all.

     

    I'll take another look at this and at least add a flag to have the system completely ignore "squishing".  Altering the text myself via RGSS bitmap methods looks even worse than squishing it, though, so there isn't much I can do there.
     
    #17
    Archeia likes this.
  18. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    Update: 1.03


    Added various stuff relating to giving a bit more control over when and if the script turns off and allows the default text draw "squeezing" to take place instead of the cached draw and cleaned up the code a bit. Link is the same as in original description.
     
    #18
    Solistra and Archeia like this.
  19. Archeia

    Archeia Level 99 Demi-fiend Staff Member Developer

    Messages:
    14,582
    Likes Received:
    14,281
    Location:
    Game Dev Salt Mines
    First Language:
    Filipino
    Primarily Uses:
    VNM
    Mithran I was just wondering, is it possible for us to decrease the spacing in some fonts. Like instead of a letter spacing, half letter? Just an example:

    [​IMG]
     
    #19
  20. Mithran

    Mithran Global Moderators Global Mod

    Messages:
    404
    Likes Received:
    212
    First Language:
    English
    I've experimented with this some and can't quite get it right (eg., to the point where setting one variable for spacing, either as a ratio of the default amount of spacing or a specific pixel offset for pacing looks good for all letters in a given size for a specific font). In the case of the above example, though, is the top window a Window_Help or something inheriting from it? Those use draw_text_ex, which space the text a certain way no matter what the cached draw does. You would need separate adjustments for the character-by-character (draw_text_ex) drawing sections and the all-at-once (draw_text) drawing methods. It is true that my script makes everything draw character-by-character, but uses a completely different method to do so and does so using a slight amount of overdraw buffer on either side to prevent the cut-off letter glitch.


    Proper spacing is something that can be very finicky because each character has a specific way it is supposed to be spaced based on the font, but text_size only tells you the space the character is supposed to take up with spacing (supposedly) already considered, and that is the only thing you really have to go off of if you are trying to automatically space things. Without access to a full kerning table, that is.
     
    #20
    Archeia likes this.

Share This Page